Cycles / Vector Transform node:
authorThomas Dinges <blender@dingto.org>
Wed, 31 Jul 2013 20:05:13 +0000 (20:05 +0000)
committerThomas Dinges <blender@dingto.org>
Wed, 31 Jul 2013 20:05:13 +0000 (20:05 +0000)
* Code cleanup to avoid duplicated enum code.
* Added a third type for conversion next to Point and Vector: Normal. This is basically the same result as with the Vector type, but normalizes the vector at the end.

Thanks to Brecht for code review!

intern/cycles/blender/blender_shader.cpp
intern/cycles/kernel/shaders/node_vector_transform.osl
intern/cycles/kernel/svm/svm_types.h
intern/cycles/kernel/svm/svm_vector_transform.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h
source/blender/makesdna/DNA_node_types.h
source/blender/makesrna/intern/rna_nodetree.c

index d0b83d9e55379e50f40406b8adb1669b2c694d8d..469ba15d291fa7a7746f892495faf18935f08f95 100644 (file)
@@ -260,8 +260,8 @@ static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scen
                BL::ShaderNodeVectorTransform b_vector_transform_node(b_node);
                VectorTransformNode *vtransform = new VectorTransformNode();
                vtransform->type = VectorTransformNode::type_enum[b_vector_transform_node.type()];
-               vtransform->convert_from = VectorTransformNode::convert_from_enum[b_vector_transform_node.convert_from()];
-               vtransform->convert_to = VectorTransformNode::convert_to_enum[b_vector_transform_node.convert_to()];
+               vtransform->convert_from = VectorTransformNode::convert_space_enum[b_vector_transform_node.convert_from()];
+               vtransform->convert_to = VectorTransformNode::convert_space_enum[b_vector_transform_node.convert_to()];
                node = vtransform;
        }
        else if (b_node.is_a(&RNA_ShaderNodeNormal)) {
index 746aab9d08b8e7415b5fbdaeb02b194849df3afd..2a501b25ceac732da409b264a01056a46643cd41 100644 (file)
 
 shader node_vector_transform(
        string type = "Vector",
-       string convert_from = "World",
-       string convert_to = "Object",
+       string convert_from = "world",
+       string convert_to = "object",
        vector VectorIn = vector(0.0, 0.0, 0.0),
        output vector VectorOut = vector(0.0, 0.0, 0.0))
 {
-       if (type == "Vector") {
+       if (type == "Vector" || type == "Normal") {
                VectorOut = transform(convert_from, convert_to, VectorIn);
+               if (type == "Normal")
+                       VectorOut = normalize(VectorOut);
        }
        else if (type == "Point") {
-               point Point = point(VectorIn[0], VectorIn[1], VectorIn[2]);
+               point Point = (point)VectorIn;
                VectorOut = transform(convert_from, convert_to, Point);
        }
 }
index ecbc43a16dab35480b2d173fab17ab3e0e2bc34e..dbfb8d48fbf317b9b4918331a66fea6943f373d5 100644 (file)
@@ -232,20 +232,15 @@ typedef enum NodeVectorMath {
 
 typedef enum NodeVectorTransformType {
        NODE_VECTOR_TRANSFORM_TYPE_VECTOR,
-       NODE_VECTOR_TRANSFORM_TYPE_POINT
+       NODE_VECTOR_TRANSFORM_TYPE_POINT,
+       NODE_VECTOR_TRANSFORM_TYPE_NORMAL
 } NodeVectorTransformType;
 
-typedef enum NodeVectorTransformConvertFrom {
-       NODE_VECTOR_TRANSFORM_CONVERT_FROM_WORLD,
-       NODE_VECTOR_TRANSFORM_CONVERT_FROM_OBJECT,
-       NODE_VECTOR_TRANSFORM_CONVERT_FROM_CAMERA
-} NodeVectorTransformConvertFrom;
-
-typedef enum NodeVectorTransformConvertTo {
-       NODE_VECTOR_TRANSFORM_CONVERT_TO_WORLD,
-       NODE_VECTOR_TRANSFORM_CONVERT_TO_OBJECT,
-       NODE_VECTOR_TRANSFORM_CONVERT_TO_CAMERA
-} NodeVectorTransformConvertTo;
+typedef enum NodeVectorTransformConvertSpace {
+       NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD,
+       NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT,
+       NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA
+} NodeVectorTransformConvertSpace;
 
 typedef enum NodeConvert {
        NODE_CONVERT_FV,
index 8b5369ab62682d4e13f55da6d52d24f567f24de8..17dd72d4f3030b4f38aefd680ba35ca1cd317d0b 100644 (file)
@@ -31,23 +31,24 @@ __device void svm_node_vector_transform(KernelGlobals *kg, ShaderData *sd, float
        float3 in = stack_load_float3(stack, vector_in);
        
        NodeVectorTransformType type = (NodeVectorTransformType)itype;
-       NodeVectorTransformConvertFrom from = (NodeVectorTransformConvertFrom)ifrom;
-       NodeVectorTransformConvertTo to = (NodeVectorTransformConvertTo)ito;
+       NodeVectorTransformConvertSpace from = (NodeVectorTransformConvertSpace)ifrom;
+       NodeVectorTransformConvertSpace to = (NodeVectorTransformConvertSpace)ito;
        
        Transform tfm;
        int is_object = (sd->object != ~0);
+       int is_direction = (type == NODE_VECTOR_TRANSFORM_TYPE_VECTOR || type == NODE_VECTOR_TRANSFORM_TYPE_NORMAL);
        
        /* From world */
-       if(from == NODE_VECTOR_TRANSFORM_CONVERT_FROM_WORLD) {
-               if(to == NODE_VECTOR_TRANSFORM_CONVERT_TO_CAMERA) {
+       if(from == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD) {
+               if(to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA) {
                        tfm = kernel_data.cam.worldtocamera;
-                       if(type == NODE_VECTOR_TRANSFORM_TYPE_VECTOR)
+                       if(is_direction)
                                in = transform_direction(&tfm, in);
                        else
                                in = transform_point(&tfm, in);
                }
-               else if (to == NODE_VECTOR_TRANSFORM_CONVERT_TO_OBJECT && is_object) {
-                       if(type == NODE_VECTOR_TRANSFORM_TYPE_VECTOR)
+               else if (to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT && is_object) {
+                       if(is_direction)
                                object_inverse_dir_transform(kg, sd, &in);
                        else
                                object_inverse_position_transform(kg, sd, &in);
@@ -55,16 +56,16 @@ __device void svm_node_vector_transform(KernelGlobals *kg, ShaderData *sd, float
        }
        
        /* From camera */
-       else if (from == NODE_VECTOR_TRANSFORM_CONVERT_FROM_CAMERA) {
-               if(to == NODE_VECTOR_TRANSFORM_CONVERT_TO_WORLD || to == NODE_VECTOR_TRANSFORM_CONVERT_TO_OBJECT) {
+       else if (from == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA) {
+               if(to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD || to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT) {
                        tfm = kernel_data.cam.cameratoworld;
-                       if(type == NODE_VECTOR_TRANSFORM_TYPE_VECTOR)
+                       if(is_direction)
                                in = transform_direction(&tfm, in);
                        else
                                in = transform_point(&tfm, in);
                }
-               if(to == NODE_VECTOR_TRANSFORM_CONVERT_TO_OBJECT && is_object) {
-                       if(type == NODE_VECTOR_TRANSFORM_TYPE_VECTOR)
+               if(to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT && is_object) {
+                       if(is_direction)
                                object_inverse_dir_transform(kg, sd, &in);
                        else
                                object_inverse_position_transform(kg, sd, &in);
@@ -72,22 +73,26 @@ __device void svm_node_vector_transform(KernelGlobals *kg, ShaderData *sd, float
        }
        
        /* From object */
-       else if(from == NODE_VECTOR_TRANSFORM_CONVERT_FROM_OBJECT) {
-               if((to == NODE_VECTOR_TRANSFORM_CONVERT_TO_WORLD || to == NODE_VECTOR_TRANSFORM_CONVERT_TO_CAMERA) && is_object) {
-                       if(type == NODE_VECTOR_TRANSFORM_TYPE_VECTOR)
+       else if(from == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT) {
+               if((to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD || to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA) && is_object) {
+                       if(is_direction)
                                object_dir_transform(kg, sd, &in);
                        else
                                object_position_transform(kg, sd, &in);
                }
-               if(to == NODE_VECTOR_TRANSFORM_CONVERT_TO_CAMERA) {
+               if(to == NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA) {
                        tfm = kernel_data.cam.worldtocamera;
-                       if(type == NODE_VECTOR_TRANSFORM_TYPE_VECTOR)
+                       if(is_direction)
                                in = transform_direction(&tfm, in);
                        else
                                in = transform_point(&tfm, in);
                }
        }
        
+       /* Normalize Normal */
+       if(type == NODE_VECTOR_TRANSFORM_TYPE_NORMAL)
+               in = normalize(in);
+       
        /* Output */    
        if(stack_valid(vector_out)) {
                        stack_store_float3(stack, vector_out, in);
index faaaf61b2be1b9a4cd9da474acd5d648084281a8..b5107315d4c0232e3cab3421676455c5d8b45d41 100644 (file)
@@ -3251,35 +3251,24 @@ static ShaderEnum vector_transform_type_init()
 
        enm.insert("Vector", NODE_VECTOR_TRANSFORM_TYPE_VECTOR);
        enm.insert("Point", NODE_VECTOR_TRANSFORM_TYPE_POINT);
+       enm.insert("Normal", NODE_VECTOR_TRANSFORM_TYPE_NORMAL);
 
        return enm;
 }
 
-static ShaderEnum vector_transform_convert_from_init()
+static ShaderEnum vector_transform_convert_space_init()
 {
        ShaderEnum enm;
 
-       enm.insert("world", NODE_VECTOR_TRANSFORM_CONVERT_FROM_WORLD);
-       enm.insert("object", NODE_VECTOR_TRANSFORM_CONVERT_FROM_OBJECT);
-       enm.insert("camera", NODE_VECTOR_TRANSFORM_CONVERT_FROM_CAMERA);
-
-       return enm;
-}
-
-static ShaderEnum vector_transform_convert_to_init()
-{
-       ShaderEnum enm;
-
-       enm.insert("world", NODE_VECTOR_TRANSFORM_CONVERT_TO_WORLD);
-       enm.insert("object", NODE_VECTOR_TRANSFORM_CONVERT_TO_OBJECT);
-       enm.insert("camera", NODE_VECTOR_TRANSFORM_CONVERT_TO_CAMERA);
+       enm.insert("world", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD);
+       enm.insert("object", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT);
+       enm.insert("camera", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA);
 
        return enm;
 }
 
 ShaderEnum VectorTransformNode::type_enum = vector_transform_type_init();
-ShaderEnum VectorTransformNode::convert_from_enum = vector_transform_convert_from_init();
-ShaderEnum VectorTransformNode::convert_to_enum = vector_transform_convert_to_init();
+ShaderEnum VectorTransformNode::convert_space_enum = vector_transform_convert_space_init();
 
 void VectorTransformNode::compile(SVMCompiler& compiler)
 {
@@ -3290,7 +3279,7 @@ void VectorTransformNode::compile(SVMCompiler& compiler)
        compiler.stack_assign(vector_out);
 
        compiler.add_node(NODE_VECTOR_TRANSFORM,
-               compiler.encode_uchar4(type_enum[type], convert_from_enum[convert_from], convert_to_enum[convert_to]),
+               compiler.encode_uchar4(type_enum[type], convert_space_enum[convert_from], convert_space_enum[convert_to]),
                compiler.encode_uchar4(vector_in->stack_offset, vector_out->stack_offset));
 }
 
index ed4d24c774a645855758311bfc84979a5df8150e..46b426ea20b0a95a5cf3d7559b738242f5a7a983 100644 (file)
@@ -513,8 +513,7 @@ public:
        ustring convert_to;
        
        static ShaderEnum type_enum;
-       static ShaderEnum convert_from_enum;
-       static ShaderEnum convert_to_enum;
+       static ShaderEnum convert_space_enum;
 };
 
 class BumpNode : public ShaderNode {
index 780cc3718656dcb2205eab57146800b30211b351..9ff4392242eabf2663dcc9a261b2363c2f5edfcc 100644 (file)
@@ -878,14 +878,11 @@ typedef struct NodeShaderNormalMap {
 /* vector transform */
 #define SHD_VECT_TRANSFORM_TYPE_VECTOR 0
 #define SHD_VECT_TRANSFORM_TYPE_POINT  1
+#define SHD_VECT_TRANSFORM_TYPE_NORMAL 2
 
-#define SHD_VECT_TRANSFORM_FROM_WORLD  0
-#define SHD_VECT_TRANSFORM_FROM_OBJECT 1
-#define SHD_VECT_TRANSFORM_FROM_CAMERA 2
-
-#define SHD_VECT_TRANSFORM_TO_WORLD            0
-#define SHD_VECT_TRANSFORM_TO_OBJECT   1
-#define SHD_VECT_TRANSFORM_TO_CAMERA   2
+#define SHD_VECT_TRANSFORM_SPACE_WORLD 0
+#define SHD_VECT_TRANSFORM_SPACE_OBJECT        1
+#define SHD_VECT_TRANSFORM_SPACE_CAMERA        2
 
 /* toon modes */
 #define SHD_TOON_DIFFUSE       0
index 4111617085c37e986e84c4fdc81ffb2e575e2317..535c279c02ff76ceaf82f5c6e11dcd1a2bb8c048 100644 (file)
@@ -3514,20 +3514,14 @@ static void def_sh_vect_transform(StructRNA *srna)
        static EnumPropertyItem prop_vect_type_items[] = {
                {SHD_VECT_TRANSFORM_TYPE_VECTOR, "VECTOR",  0, "Vector",   ""},
                {SHD_VECT_TRANSFORM_TYPE_POINT,  "POINT",   0, "Point",    ""},
+               {SHD_VECT_TRANSFORM_TYPE_NORMAL, "NORMAL",  0, "Normal",   ""},
                {0, NULL, 0, NULL, NULL}
        };
 
-       static EnumPropertyItem prop_vect_from_items[] = {
-               {SHD_VECT_TRANSFORM_FROM_WORLD,  "WORLD",   0, "World",    ""},
-               {SHD_VECT_TRANSFORM_FROM_OBJECT, "OBJECT",  0, "Object",   ""},
-               {SHD_VECT_TRANSFORM_FROM_CAMERA, "CAMERA",  0, "Camera",   ""},
-               {0, NULL, 0, NULL, NULL}
-       };
-
-       static EnumPropertyItem prop_vect_to_items[] = {
-               {SHD_VECT_TRANSFORM_TO_WORLD,  "WORLD",   0, "World",    ""},
-               {SHD_VECT_TRANSFORM_TO_OBJECT, "OBJECT",  0, "Object",   ""},
-               {SHD_VECT_TRANSFORM_TO_CAMERA, "CAMERA",  0, "Camera",   ""},
+       static EnumPropertyItem prop_vect_space_items[] = {
+               {SHD_VECT_TRANSFORM_SPACE_WORLD,  "WORLD",   0, "World",    ""},
+               {SHD_VECT_TRANSFORM_SPACE_OBJECT, "OBJECT",  0, "Object",   ""},
+               {SHD_VECT_TRANSFORM_SPACE_CAMERA, "CAMERA",  0, "Camera",   ""},
                {0, NULL, 0, NULL, NULL}
        };
 
@@ -3541,12 +3535,12 @@ static void def_sh_vect_transform(StructRNA *srna)
        RNA_def_property_update(prop, 0, "rna_Node_update");
        
        prop = RNA_def_property(srna, "convert_from", PROP_ENUM, PROP_NONE);
-       RNA_def_property_enum_items(prop, prop_vect_from_items);
+       RNA_def_property_enum_items(prop, prop_vect_space_items);
        RNA_def_property_ui_text(prop, "Convert From", "Space to convert from");
        RNA_def_property_update(prop, 0, "rna_Node_update");
        
        prop = RNA_def_property(srna, "convert_to", PROP_ENUM, PROP_NONE);
-       RNA_def_property_enum_items(prop, prop_vect_to_items);
+       RNA_def_property_enum_items(prop, prop_vect_space_items);
        RNA_def_property_ui_text(prop, "Convert To", "Space to convert to");
        RNA_def_property_update(prop, 0, "rna_Node_update");
 }