HSV Color Node for Cycles
authorDalai Felinto <dfelinto@gmail.com>
Fri, 2 Dec 2011 16:57:37 +0000 (16:57 +0000)
committerDalai Felinto <dfelinto@gmail.com>
Fri, 2 Dec 2011 16:57:37 +0000 (16:57 +0000)
.........................
note, the OSL code has a problem.
In the original node the input and output nodes have the same name (Color).
So this will be fixed here once Brecht come up with a nice autorenaming (or we do a doversion patch) for that.

14 files changed:
intern/cycles/app/cycles_xml.cpp
intern/cycles/blender/blender_shader.cpp
intern/cycles/kernel/CMakeLists.txt
intern/cycles/kernel/osl/nodes/CMakeLists.txt
intern/cycles/kernel/osl/nodes/node_color.h
intern/cycles/kernel/osl/nodes/node_hsv.osl [new file with mode: 0644]
intern/cycles/kernel/osl/nodes/node_mix.osl
intern/cycles/kernel/svm/svm.h
intern/cycles/kernel/svm/svm_hsv.h [new file with mode: 0644]
intern/cycles/kernel/svm/svm_mix.h
intern/cycles/kernel/svm/svm_types.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h
source/blender/nodes/shader/nodes/node_shader_hueSatVal.c

index 0bd0c48933d10c3b762849c0209f044f990e002b..00acae9295590565ebf972ebc33cc35986105df2 100644 (file)
@@ -446,6 +446,9 @@ static void xml_read_shader_graph(const XMLReadState& state, Shader *shader, pug
                else if(string_iequals(node.name(), "separate_rgb")) {
                        snode = new SeparateRGBNode();
                }
+               else if(string_iequals(node.name(), "hsv")) {
+                       snode = new HSVNode();
+               }
                else if(string_iequals(node.name(), "attribute")) {
                        AttributeNode *attr = new AttributeNode();
                        xml_read_ustring(&attr->attribute, node, "attribute");
index dfca9d7173a77255fd24ef5d23d366f0671a1d09..e081ad8bdee5246c3205e400c6ca9c6494e6c7fe 100644 (file)
@@ -131,7 +131,6 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Node *
                case BL::ShaderNode::type_CURVE_RGB: break;
                case BL::ShaderNode::type_CURVE_VEC: break;
                case BL::ShaderNode::type_GEOMETRY: break;
-               case BL::ShaderNode::type_HUE_SAT: break;
                case BL::ShaderNode::type_INVERT: break;
                case BL::ShaderNode::type_MATERIAL: break;
                case BL::ShaderNode::type_MATERIAL_EXT: break;
@@ -171,6 +170,10 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Node *
                        node = new CombineRGBNode();
                        break;
                }
+               case BL::ShaderNode::type_HUE_SAT: {
+                       node = new HSVNode();
+                       break;
+               }
                case BL::ShaderNode::type_RGBTOBW: {
                        node = new ConvertNode(SHADER_SOCKET_COLOR, SHADER_SOCKET_FLOAT);
                        break;
index abe81103d8064795288531848644f5b3af199379..a09fe1d726ee67b2eab6a0841f82f2268ac7a87c 100644 (file)
@@ -60,6 +60,7 @@ set(SRC_SVM_HEADERS
        svm/svm_fresnel.h
        svm/svm_geometry.h
        svm/svm_gradient.h
+       svm/svm_hsv.h
        svm/svm_image.h
        svm/svm_light_path.h
        svm/svm_magic.h
index 34729145f2809a675e8be7a5428b8c7fa2197a9d..50e763a6b83f3fd9e1b6a75305357accb56478c7 100644 (file)
@@ -21,6 +21,7 @@ set(SRC_OSL
        node_geometry.osl
        node_glass_bsdf.osl
        node_glossy_bsdf.osl
+       node_hsv.osl
        node_image_texture.osl
        node_light_path.osl
        node_magic_texture.osl
index b92973d1dfe1b45d2ade66dbd76e594e1e44895f..37d092eae786a8b92aa4204478936a551d7225cf 100644 (file)
@@ -48,3 +48,78 @@ color color_scene_linear_to_srgb(color c)
                color_scene_linear_to_srgb(c[2]));
 }
 
+/* Color Operations */
+
+color rgb_to_hsv(color rgb)
+{
+       float cmax, cmin, h, s, v, cdelta;
+       color c;
+
+       cmax = max(rgb[0], max(rgb[1], rgb[2]));
+       cmin = min(rgb[0], min(rgb[1], rgb[2]));
+       cdelta = cmax - cmin;
+
+       v = cmax;
+
+       if(cmax != 0.0) {
+               s = cdelta/cmax;
+       }
+       else {
+               s = 0.0;
+               h = 0.0;
+       }
+
+       if(s == 0.0) {
+               h = 0.0;
+       }
+       else {
+               c = (color(cmax, cmax, cmax) - rgb)/cdelta;
+
+               if(rgb[0] == cmax) h = c[2] - c[1];
+               else if(rgb[1] == cmax) h = 2.0 + c[0] -  c[2];
+               else h = 4.0 + c[1] - c[0];
+
+               h /= 6.0;
+
+               if(h < 0.0)
+                       h += 1.0;
+       }
+
+       return color(h, s, v);
+}
+
+color hsv_to_rgb(color hsv)
+{
+       float i, f, p, q, t, h, s, v;
+       color rgb;
+
+       h = hsv[0];
+       s = hsv[1];
+       v = hsv[2];
+
+       if(s==0.0) {
+               rgb = color(v, v, v);
+       }
+       else {
+               if(h==1.0)
+                       h = 0.0;
+               
+               h *= 6.0;
+               i = floor(h);
+               f = h - i;
+               rgb = color(f, f, f);
+               p = v*(1.0-s);
+               q = v*(1.0-(s*f));
+               t = v*(1.0-(s*(1.0-f)));
+               
+               if(i == 0.0) rgb = color(v, t, p);
+               else if(i == 1.0) rgb = color(q, v, p);
+               else if(i == 2.0) rgb = color(p, v, t);
+               else if(i == 3.0) rgb = color(p, q, v);
+               else if(i == 4.0) rgb = color(t, p, v);
+               else rgb = color(v, p, q);
+       }
+
+       return rgb;
+}
+
diff --git a/intern/cycles/kernel/osl/nodes/node_hsv.osl b/intern/cycles/kernel/osl/nodes/node_hsv.osl
new file mode 100644 (file)
index 0000000..da776c8
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2011, Blender Foundation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "stdosl.h"
+#include "node_color.h"
+
+shader node_hsv(
+       float Hue = 0.5,
+       float Saturation = 1.0,
+       float Value = 1.0,
+       float Fac = 0.5,
+       color ColorIn = color(0.0, 0.0, 0.0),
+       output color ColorOut = color(0.0, 0.0, 0.0))
+{
+       float t = clamp(Fac, 0.0, 1.0);
+       color Color = rgb_to_hsv(ColorIn);
+
+       Color[0] += Hue - 0.5;
+       Color[0] = fmod(Color[0], 1.0);
+       Color[1] *= Saturation;
+       Color[2] *= Value;
+
+       Color = hsv_to_rgb(Color);
+
+       ColorOut = mix(Color, ColorIn, t);
+}
+
index 582aa7b3c6032003320aeda0f2576ad73039cf87..8a462c995d369b15cd640630e7ad2029bcec66ef 100644 (file)
  */
 
 #include "stdosl.h"
-
-color rgb_to_hsv(color rgb)
-{
-       float cmax, cmin, h, s, v, cdelta;
-       color c;
-
-       cmax = max(rgb[0], max(rgb[1], rgb[2]));
-       cmin = min(rgb[0], min(rgb[1], rgb[2]));
-       cdelta = cmax - cmin;
-
-       v = cmax;
-
-       if(cmax != 0.0) {
-               s = cdelta/cmax;
-       }
-       else {
-               s = 0.0;
-               h = 0.0;
-       }
-
-       if(s == 0.0) {
-               h = 0.0;
-       }
-       else {
-               c = (color(cmax, cmax, cmax) - rgb)/cdelta;
-
-               if(rgb[0] == cmax) h = c[2] - c[1];
-               else if(rgb[1] == cmax) h = 2.0 + c[0] -  c[2];
-               else h = 4.0 + c[1] - c[0];
-
-               h /= 6.0;
-
-               if(h < 0.0)
-                       h += 1.0;
-       }
-
-       return color(h, s, v);
-}
-
-color hsv_to_rgb(color hsv)
-{
-       float i, f, p, q, t, h, s, v;
-       color rgb;
-
-       h = hsv[0];
-       s = hsv[1];
-       v = hsv[2];
-
-       if(s==0.0) {
-               rgb = color(v, v, v);
-       }
-       else {
-               if(h==1.0)
-                       h = 0.0;
-               
-               h *= 6.0;
-               i = floor(h);
-               f = h - i;
-               rgb = color(f, f, f);
-               p = v*(1.0-s);
-               q = v*(1.0-(s*f));
-               t = v*(1.0-(s*(1.0-f)));
-               
-               if(i == 0.0) rgb = color(v, t, p);
-               else if(i == 1.0) rgb = color(q, v, p);
-               else if(i == 2.0) rgb = color(p, v, t);
-               else if(i == 3.0) rgb = color(p, q, v);
-               else if(i == 4.0) rgb = color(t, p, v);
-               else rgb = color(v, p, q);
-       }
-
-       return rgb;
-}
+#include "node_color.h"
 
 color node_mix_blend(float t, color col1, color col2)
 {
index 3073aefc27299e037e188ee15dc79a7b29c31563..004875b0de5a0c17a2dc81339804415964a11444 100644 (file)
@@ -127,6 +127,7 @@ CCL_NAMESPACE_END
 #include "svm_displace.h"
 #include "svm_fresnel.h"
 #include "svm_geometry.h"
+#include "svm_hsv.h"
 #include "svm_image.h"
 #include "svm_light_path.h"
 #include "svm_magic.h"
@@ -261,6 +262,9 @@ __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderT
                        case NODE_COMBINE_RGB:
                                svm_node_combine_rgb(sd, stack, node.y, node.z, node.w);
                                break;
+                       case NODE_HSV:
+                               svm_node_hsv(kg, sd, stack, node.y, node.z, node.w, &offset);
+                               break;
                        case NODE_ATTR:
                                svm_node_attr(kg, sd, stack, node);
                                break;
diff --git a/intern/cycles/kernel/svm/svm_hsv.h b/intern/cycles/kernel/svm/svm_hsv.h
new file mode 100644 (file)
index 0000000..fc927f2
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2011, Blender Foundation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __SVM_HSV_H__
+#define __SVM_HSV_H__
+
+CCL_NAMESPACE_BEGIN
+
+__device float3 rgb_to_hsv(float3 rgb)
+{
+       float cmax, cmin, h, s, v, cdelta;
+       float3 c;
+
+       cmax = fmaxf(rgb.x, fmaxf(rgb.y, rgb.z));
+       cmin = min(rgb.x, min(rgb.y, rgb.z));
+       cdelta = cmax - cmin;
+
+       v = cmax;
+
+       if(cmax != 0.0f) {
+               s = cdelta/cmax;
+       }
+       else {
+               s = 0.0f;
+               h = 0.0f;
+       }
+
+       if(s == 0.0f) {
+               h = 0.0f;
+       }
+       else {
+               float3 cmax3 = make_float3(cmax, cmax, cmax);
+               c = (cmax3 - rgb)/cdelta;
+
+               if(rgb.x == cmax) h = c.z - c.y;
+               else if(rgb.y == cmax) h = 2.0f + c.x -  c.z;
+               else h = 4.0f + c.y - c.x;
+
+               h /= 6.0f;
+
+               if(h < 0.0f)
+                       h += 1.0f;
+       }
+
+       return make_float3(h, s, v);
+}
+
+__device float3 hsv_to_rgb(float3 hsv)
+{
+       float i, f, p, q, t, h, s, v;
+       float3 rgb;
+
+       h = hsv.x;
+       s = hsv.y;
+       v = hsv.z;
+
+       if(s==0.0f) {
+               rgb = make_float3(v, v, v);
+       }
+       else {
+               if(h==1.0f)
+                       h = 0.0f;
+               
+               h *= 6.0f;
+               i = floor(h);
+               f = h - i;
+               rgb = make_float3(f, f, f);
+               p = v*(1.0f-s);
+               q = v*(1.0f-(s*f));
+               t = v*(1.0f-(s*(1.0f-f)));
+               
+               if(i == 0.0f) rgb = make_float3(v, t, p);
+               else if(i == 1.0f) rgb = make_float3(q, v, p);
+               else if(i == 2.0f) rgb = make_float3(p, v, t);
+               else if(i == 3.0f) rgb = make_float3(p, q, v);
+               else if(i == 4.0f) rgb = make_float3(t, p, v);
+               else rgb = make_float3(v, p, q);
+       }
+
+       return rgb;
+}
+
+__device void svm_node_hsv(KernelGlobals *kg, ShaderData *sd, float *stack, uint in_color_offset, uint fac_offset, uint out_color_offset, int *offset)
+{
+       /* read extra data */
+       uint4 node1 = read_node(kg, offset);
+
+       float fac = stack_load_float(stack, fac_offset);
+       float3 in_color = stack_load_float3(stack, in_color_offset);
+       float3 color = in_color;
+
+       float hue = stack_load_float(stack, node1.y);
+       float sat = stack_load_float(stack, node1.z);
+       float val = stack_load_float(stack, node1.w);
+
+       color = rgb_to_hsv(color);
+
+       color.x += hue - 0.5f;
+       color.x = fmod(color.x, 1.0f);
+       color.y *= sat;
+       color.z *= val;
+
+       color = hsv_to_rgb(color);
+
+       color.x = fac*color.x + (1.0f - fac)*in_color.x;
+       color.y = fac*color.y + (1.0f - fac)*in_color.y;
+       color.z = fac*color.z + (1.0f - fac)*in_color.z;
+
+       if (stack_valid(out_color_offset))
+               stack_store_float3(stack, out_color_offset, color);
+}
+
+CCL_NAMESPACE_END
+
+#endif /* __SVM_HSV_H__ */
+
index c9e6cdf43b954c3693d3b883cabdbc408bf0e80a..e2274a2e691be12e1945e95a675625b91e8cc152 100644 (file)
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-CCL_NAMESPACE_BEGIN
-
-__device float3 rgb_to_hsv(float3 rgb)
-{
-       float cmax, cmin, h, s, v, cdelta;
-       float3 c;
-
-       cmax = fmaxf(rgb.x, fmaxf(rgb.y, rgb.z));
-       cmin = min(rgb.x, min(rgb.y, rgb.z));
-       cdelta = cmax - cmin;
-
-       v = cmax;
-
-       if(cmax != 0.0f) {
-               s = cdelta/cmax;
-       }
-       else {
-               s = 0.0f;
-               h = 0.0f;
-       }
-
-       if(s == 0.0f) {
-               h = 0.0f;
-       }
-       else {
-               float3 cmax3 = make_float3(cmax, cmax, cmax);
-               c = (cmax3 - rgb)/cdelta;
-
-               if(rgb.x == cmax) h = c.z - c.y;
-               else if(rgb.y == cmax) h = 2.0f + c.x -  c.z;
-               else h = 4.0f + c.y - c.x;
-
-               h /= 6.0f;
+#include "svm_hsv.h"
 
-               if(h < 0.0f)
-                       h += 1.0f;
-       }
-
-       return make_float3(h, s, v);
-}
-
-__device float3 hsv_to_rgb(float3 hsv)
-{
-       float i, f, p, q, t, h, s, v;
-       float3 rgb;
-
-       h = hsv.x;
-       s = hsv.y;
-       v = hsv.z;
-
-       if(s==0.0f) {
-               rgb = make_float3(v, v, v);
-       }
-       else {
-               if(h==1.0f)
-                       h = 0.0f;
-               
-               h *= 6.0f;
-               i = floor(h);
-               f = h - i;
-               rgb = make_float3(f, f, f);
-               p = v*(1.0f-s);
-               q = v*(1.0f-(s*f));
-               t = v*(1.0f-(s*(1.0f-f)));
-               
-               if(i == 0.0f) rgb = make_float3(v, t, p);
-               else if(i == 1.0f) rgb = make_float3(q, v, p);
-               else if(i == 2.0f) rgb = make_float3(p, v, t);
-               else if(i == 3.0f) rgb = make_float3(p, q, v);
-               else if(i == 4.0f) rgb = make_float3(t, p, v);
-               else rgb = make_float3(v, p, q);
-       }
-
-       return rgb;
-}
+CCL_NAMESPACE_BEGIN
 
 __device float3 svm_lerp(const float3 a, const float3 b, float t)
 {
index cde5e662fa7b4a997eedf77a8bf818ddb35b10ed..53bba644e41331cab519192ca2e52b7fd4e64277 100644 (file)
@@ -81,7 +81,8 @@ typedef enum NodeType {
        NODE_LAYER_WEIGHT = 4800,
        NODE_CLOSURE_VOLUME = 4900,
        NODE_SEPARATE_RGB = 5000,
-       NODE_COMBINE_RGB = 5100
+       NODE_COMBINE_RGB = 5100,
+       NODE_HSV = 5200
 } NodeType;
 
 typedef enum NodeAttributeType {
index 0020a3c5d1b9423203b60f3346a7dde223dc3f5c..45bb1511785bb377e4101afaf60901866faa9907 100644 (file)
@@ -1697,6 +1697,43 @@ void SeparateRGBNode::compile(OSLCompiler& compiler)
        compiler.add(this, "node_separate_rgb");
 }
 
+/* Separate RGB */
+HSVNode::HSVNode()
+: ShaderNode("hsv")
+{
+       add_input("Hue", SHADER_SOCKET_FLOAT);
+       add_input("Saturation", SHADER_SOCKET_FLOAT);
+       add_input("Value", SHADER_SOCKET_FLOAT);
+       add_input("Fac", SHADER_SOCKET_FLOAT);
+       add_input("Color", SHADER_SOCKET_COLOR);
+       add_output("Color", SHADER_SOCKET_COLOR);
+}
+
+void HSVNode::compile(SVMCompiler& compiler)
+{
+       ShaderInput *hue_in = input("Hue");
+       ShaderInput *saturation_in = input("Saturation");
+       ShaderInput *value_in = input("Value");
+       ShaderInput *fac_in = input("Fac");
+       ShaderInput *color_in = input("Color");
+       ShaderOutput *color_out = output("Color");
+
+       compiler.stack_assign(hue_in);
+       compiler.stack_assign(saturation_in);
+       compiler.stack_assign(value_in);
+       compiler.stack_assign(fac_in);
+       compiler.stack_assign(color_in);
+       compiler.stack_assign(color_out);
+
+       compiler.add_node(NODE_HSV, color_in->stack_offset, fac_in->stack_offset, color_out->stack_offset);
+       compiler.add_node(NODE_HSV, hue_in->stack_offset, saturation_in->stack_offset, value_in->stack_offset);
+}
+
+void HSVNode::compile(OSLCompiler& compiler)
+{
+       compiler.add(this, "node_hsv");
+}
+
 /* Attribute */
 
 AttributeNode::AttributeNode()
index 4cc76b0f71ea6b37b436199a904db2587341b88f..dfff955c4fe52f0b2b6c9a09923c847c458527f2 100644 (file)
@@ -302,6 +302,11 @@ public:
        SHADER_NODE_CLASS(SeparateRGBNode)
 };
 
+class HSVNode : public ShaderNode {
+public:
+       SHADER_NODE_CLASS(HSVNode)
+};
+
 class AttributeNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(AttributeNode)
index bfdcb5d0917e9e39b2376d0e1970d19575c72a38..0f85196a45d17fa7d7ab7404af03ab2a2050c469 100644 (file)
@@ -85,7 +85,7 @@ void register_node_type_sh_hue_sat(bNodeTreeType *ttype)
        static bNodeType ntype;
 
        node_type_base(ttype, &ntype, SH_NODE_HUE_SAT, "Hue Saturation Value", NODE_CLASS_OP_COLOR, NODE_OPTIONS);
-       node_type_compatibility(&ntype, NODE_OLD_SHADING);
+       node_type_compatibility(&ntype, NODE_OLD_SHADING|NODE_NEW_SHADING);
        node_type_socket_templates(&ntype, sh_node_hue_sat_in, sh_node_hue_sat_out);
        node_type_size(&ntype, 150, 80, 250);
        node_type_exec(&ntype, node_shader_exec_hue_sat);