CameraData Input Cycles Node
authorDalai Felinto <dfelinto@gmail.com>
Fri, 2 Dec 2011 20:36:13 +0000 (20:36 +0000)
committerDalai Felinto <dfelinto@gmail.com>
Fri, 2 Dec 2011 20:36:13 +0000 (20:36 +0000)
----------------------------
reviewed and approved by Brecht

Important note:
the camera Z is reverted compared to Blender render.
Now it goes from zero (camera) to positive (in front of the camera)

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_camera.osl [new file with mode: 0644]
intern/cycles/kernel/svm/svm.h
intern/cycles/kernel/svm/svm_camera.h [new file with mode: 0644]
intern/cycles/kernel/svm/svm_types.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h
source/blender/nodes/shader/nodes/node_shader_camera.c

index 00acae9295590565ebf972ebc33cc35986105df2..8abc060af4727f40478e5c46365db059353ee899 100644 (file)
@@ -454,6 +454,9 @@ static void xml_read_shader_graph(const XMLReadState& state, Shader *shader, pug
                        xml_read_ustring(&attr->attribute, node, "attribute");
                        snode = attr;
                }
+               else if(string_ieuals(node.name(), "camera")) {
+                       snode = new CameraNode();
+               }
                else if(string_iequals(node.name(), "fresnel")) {
                        snode = new FresnelNode();
                }
index e081ad8bdee5246c3205e400c6ca9c6494e6c7fe..43359bf5352968e1fc6c40e74802bd1ff4d24a47 100644 (file)
@@ -127,7 +127,6 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Node *
 
        switch(b_node.type()) {
                /* not supported */
-               case BL::ShaderNode::type_CAMERA: break;
                case BL::ShaderNode::type_CURVE_RGB: break;
                case BL::ShaderNode::type_CURVE_VEC: break;
                case BL::ShaderNode::type_GEOMETRY: break;
@@ -155,6 +154,10 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Node *
                        node = value;
                        break;
                }
+               case BL::ShaderNode::type_CAMERA: {
+                       node = new CameraNode();
+                       break;
+               }
                case BL::ShaderNode::type_MIX_RGB: {
                        BL::ShaderNodeMixRGB b_mix_node(b_node);
                        MixNode *mix = new MixNode();
index a09fe1d726ee67b2eab6a0841f82f2268ac7a87c..59f7e04c9d8fd51218105c694f640dca5df02730 100644 (file)
@@ -54,6 +54,7 @@ set(SRC_SVM_HEADERS
        svm/svm.h
        svm/svm_attribute.h
        svm/svm_bsdf.h
+       svm/svm_camera.h
        svm/svm_closure.h
        svm/svm_convert.h
        svm/svm_displace.h
index 50e763a6b83f3fd9e1b6a75305357accb56478c7..02f4a5a55bf9b26bc54b63ee5bbfbb5a93b7a7ec 100644 (file)
@@ -7,6 +7,7 @@ set(SRC_OSL
        node_background.osl
        node_blend_texture.osl
        node_bump.osl
+       node_camera.osl
        node_clouds_texture.osl
        node_convert_from_color.osl
        node_convert_from_float.osl
diff --git a/intern/cycles/kernel/osl/nodes/node_camera.osl b/intern/cycles/kernel/osl/nodes/node_camera.osl
new file mode 100644 (file)
index 0000000..0b50084
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * 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"
+
+shader node_camera(
+       output vector ViewVector = vector(0.0, 0.0, 0.0),
+       output float ViewZDepth = 0.0,
+       output float ViewDistance = 0.0)
+{
+       ViewVector = (vector)transform("world", "camera", P);
+
+       ViewZDepth = fabs(ViewVector[2]);
+       ViewDistance = lenght(ViewVector);
+
+       ViewVector = normalize(ViewVector);
+}
+
index 004875b0de5a0c17a2dc81339804415964a11444..443715600ca1b452de7598c829c8c9cd8d44769f 100644 (file)
@@ -126,6 +126,7 @@ CCL_NAMESPACE_END
 #include "svm_convert.h"
 #include "svm_displace.h"
 #include "svm_fresnel.h"
+#include "svm_camera.h"
 #include "svm_geometry.h"
 #include "svm_hsv.h"
 #include "svm_image.h"
@@ -232,6 +233,9 @@ __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderT
                                svm_node_tex_magic(kg, sd, stack, node, &offset);
                                break;
 #endif
+                       case NODE_CAMERA:
+                               svm_node_camera(kg, sd, stack, node.y, node.z, node.w);
+                               break;
                        case NODE_GEOMETRY:
                                svm_node_geometry(sd, stack, node.y, node.z);
                                break;
diff --git a/intern/cycles/kernel/svm/svm_camera.h b/intern/cycles/kernel/svm/svm_camera.h
new file mode 100644 (file)
index 0000000..cf161dd
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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.
+ */
+
+CCL_NAMESPACE_BEGIN
+
+__device void svm_node_camera(KernelGlobals *kg, ShaderData *sd, float *stack, uint out_vector, uint out_zdepth, uint out_distance)
+{
+       float distance;
+       float zdepth;
+       float3 vector;
+
+       Transform tfm = kernel_data.cam.worldtocamera;
+       vector = transform(&tfm, sd->P);
+       zdepth = vector.z;
+       distance = len(vector);
+
+       if (stack_valid(out_vector))
+               stack_store_float3(stack, out_vector, normalize(vector));
+
+       if (stack_valid(out_zdepth))
+               stack_store_float(stack, out_zdepth, zdepth);
+
+       if (stack_valid(out_distance))
+               stack_store_float(stack, out_distance, distance);
+}
+
+CCL_NAMESPACE_END
+
index 53bba644e41331cab519192ca2e52b7fd4e64277..fcd345b9359c9a9f397e313897de0b8c1df72cbc 100644 (file)
@@ -82,7 +82,8 @@ typedef enum NodeType {
        NODE_CLOSURE_VOLUME = 4900,
        NODE_SEPARATE_RGB = 5000,
        NODE_COMBINE_RGB = 5100,
-       NODE_HSV = 5200
+       NODE_HSV = 5200,
+       NODE_CAMERA = 5300
 } NodeType;
 
 typedef enum NodeAttributeType {
index 45bb1511785bb377e4101afaf60901866faa9907..9f12fe6002fbd79bfbf17309b0aafad35cc3e9b6 100644 (file)
@@ -1804,6 +1804,33 @@ void AttributeNode::compile(OSLCompiler& compiler)
        compiler.add(this, "node_attribute");
 }
 
+/* Camera */
+
+CameraNode::CameraNode()
+: ShaderNode("camera")
+{
+       add_output("View Vector",  SHADER_SOCKET_VECTOR);
+       add_output("View Z Depth",  SHADER_SOCKET_FLOAT);
+       add_output("View Distance",  SHADER_SOCKET_FLOAT);
+}
+
+void CameraNode::compile(SVMCompiler& compiler)
+{
+       ShaderOutput *vector_out = output("View Vector");
+       ShaderOutput *z_depth_out = output("View Z Depth");
+       ShaderOutput *distance_out = output("View Distance");
+
+       compiler.stack_assign(vector_out);
+       compiler.stack_assign(z_depth_out);
+       compiler.stack_assign(distance_out);
+       compiler.add_node(NODE_CAMERA, vector_out->stack_offset, z_depth_out->stack_offset, distance_out->stack_offset);
+}
+
+void CameraNode::compile(OSLCompiler& compiler)
+{
+       compiler.add(this, "node_camera");
+}
+
 /* Fresnel */
 
 FresnelNode::FresnelNode()
index dfff955c4fe52f0b2b6c9a09923c847c458527f2..fd5271d843414e65ccf450d21a4a7842095ae7d7 100644 (file)
@@ -315,6 +315,11 @@ public:
        ustring attribute;
 };
 
+class CameraNode : public ShaderNode {
+public:
+       SHADER_NODE_CLASS(CameraNode)
+};
+
 class FresnelNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(FresnelNode)
index d95cc1460df7dc154d3390ad5468df37e8602e8e..3ec75dfa4fddc8197324386b9f8129e586073ad4 100644 (file)
@@ -62,7 +62,7 @@ void register_node_type_sh_camera(bNodeTreeType *ttype)
        static bNodeType ntype;
 
        node_type_base(ttype, &ntype, SH_NODE_CAMERA, "Camera Data", NODE_CLASS_INPUT, 0);
-       node_type_compatibility(&ntype, NODE_OLD_SHADING);
+       node_type_compatibility(&ntype, NODE_OLD_SHADING|NODE_NEW_SHADING);
        node_type_socket_templates(&ntype, NULL, sh_node_camera_out);
        node_type_size(&ntype, 95, 95, 120);
        node_type_storage(&ntype, "node_camera", NULL, NULL);