Cycles / Wavelength to RGB node:
authorThomas Dinges <blender@dingto.org>
Mon, 10 Jun 2013 21:55:41 +0000 (21:55 +0000)
committerThomas Dinges <blender@dingto.org>
Mon, 10 Jun 2013 21:55:41 +0000 (21:55 +0000)
* Added a node to convert wavelength (in nanometers, from 380nm to 780nm) to RGB values. This can be useful to match real world colors easier.

* Code cleanup:
** Moved color functions (xyz and hsv) into dedicated utility files.
** Remove svm_lerp(), use interp() instead.

Documentation:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Nodes/More#Wavelength

Example render:
http://www.pasteall.org/pic/show.php?id=53202

This is part of my GSoC 2013. (revisions 57322, 57326, 57335 and 57367 from soc-2013-dingto).

22 files changed:
intern/cycles/blender/blender_shader.cpp
intern/cycles/kernel/CMakeLists.txt
intern/cycles/kernel/shaders/CMakeLists.txt
intern/cycles/kernel/shaders/node_color.h
intern/cycles/kernel/shaders/node_sky_texture.osl
intern/cycles/kernel/shaders/node_wavelength.osl [new file with mode: 0644]
intern/cycles/kernel/svm/svm.h
intern/cycles/kernel/svm/svm_hsv.h
intern/cycles/kernel/svm/svm_mix.h
intern/cycles/kernel/svm/svm_sky.h
intern/cycles/kernel/svm/svm_types.h
intern/cycles/kernel/svm/svm_wavelength.h [new file with mode: 0644]
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h
intern/cycles/util/util_color.h
release/scripts/startup/nodeitems_builtins.py
source/blender/blenkernel/BKE_node.h
source/blender/blenkernel/intern/node.c
source/blender/nodes/CMakeLists.txt
source/blender/nodes/NOD_shader.h
source/blender/nodes/NOD_static_types.h
source/blender/nodes/shader/nodes/node_shader_wavelength.c [new file with mode: 0644]

index a5fe5dbf12f494744e95ea9672a05166c1a927b5..a9db10b21d77d426a9f6f8f526c1c54d6e51112b 100644 (file)
@@ -397,6 +397,9 @@ static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scen
                wire->use_pixel_size = b_wireframe_node.use_pixel_size();
                node = wire;
        }
+       else if (b_node.is_a(&RNA_ShaderNodeWavelength)) {
+               node = new WavelengthNode();
+       }
        else if (b_node.is_a(&RNA_ShaderNodeLightPath)) {
                node = new LightPathNode();
        }
index 912a1321d67349cd175e36052a2345acef6370d2..f87f5dec74124c55cb1fb2ef4a43c583ca76fc37 100644 (file)
@@ -80,6 +80,7 @@ set(SRC_SVM_HEADERS
        svm/svm_displace.h
        svm/svm_fresnel.h
        svm/svm_wireframe.h
+       svm/svm_wavelength.h
        svm/svm_gamma.h
        svm/svm_brightness.h
        svm/svm_geometry.h
index 4f0b5b25cb3c368a05335a4d2f881d3735b76f76..f2beb3da9c03922beddceb2a4d750729d72f69d8 100644 (file)
@@ -67,6 +67,7 @@ set(SRC_OSL
        node_velvet_bsdf.osl
        node_voronoi_texture.osl
        node_ward_bsdf.osl
+       node_wavelength.osl
        node_wave_texture.osl
        node_wireframe.osl
 )
index c6b5d740f6a08e58571ba86b14665e322a6608ad..92bd39b5828fb3449fffe37405ea366ddd3812a9 100644 (file)
@@ -58,6 +58,26 @@ color color_unpremultiply(color c, float alpha)
 
 /* Color Operations */
 
+color xyY_to_xyz(float x, float y, float Y)
+{
+       float X, Z;
+
+       if (y != 0.0) X = (x / y) * Y;
+       else X = 0.0;
+
+       if (y != 0.0 && Y != 0.0) Z = ((1.0 - x - y) / y) * Y;
+       else Z = 0.0;
+
+       return color(X, Y, Z);
+}
+
+color xyz_to_rgb(float x, float y, float z)
+{
+       return color( 3.240479 * x + -1.537150 * y + -0.498535 * z,
+                    -0.969256 * x +  1.875991 * y +  0.041556 * z,
+                     0.055648 * x + -0.204043 * y +  1.057311 * z);
+}
+
 color rgb_to_hsv(color rgb)
 {
        float cmax, cmin, h, s, v, cdelta;
index e9f7dfb3a2abfa8da8b48f1932d5340b8821b823..61788799d99c7a62de569a058ae52fa05256e36d 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include "stdosl.h"
+#include "node_color.h"
 
 struct KernelSunSky {
        /* sun direction in spherical and cartesian */
@@ -28,26 +29,6 @@ struct KernelSunSky {
        float perez_Y[5], perez_x[5], perez_y[5];
 };
 
-color xyY_to_xyz(float x, float y, float Y)
-{
-       float X, Z;
-
-       if (y != 0.0) X = (x / y) * Y;
-       else X = 0.0;
-
-       if (y != 0.0 && Y != 0.0) Z = ((1.0 - x - y) / y) * Y;
-       else Z = 0.0;
-
-       return color(X, Y, Z);
-}
-
-color xyz_to_rgb(float x, float y, float z)
-{
-       return color( 3.240479 * x + -1.537150 * y + -0.498535 * z,
-                    -0.969256 * x +  1.875991 * y +  0.041556 * z,
-                     0.055648 * x + -0.204043 * y +  1.057311 * z);
-}
-
 float sky_angle_between(float thetav, float phiv, float theta, float phi)
 {
        float cospsi = sin(thetav) * sin(theta) * cos(phi - phiv) + cos(thetav) * cos(theta);
diff --git a/intern/cycles/kernel/shaders/node_wavelength.osl b/intern/cycles/kernel/shaders/node_wavelength.osl
new file mode 100644 (file)
index 0000000..748b9e8
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2013, 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_wavelength(
+       float Wavelength = 500.0,
+       output color Color = 0.0)
+{
+       Color = wavelength_color(Wavelength);
+}
+
index cfb930d92e811dc0e48db68ae826438843854a67..7f0bb3163a1beab3001ec2028abd8a084dd8749f 100644 (file)
@@ -152,6 +152,7 @@ CCL_NAMESPACE_END
 #include "svm_displace.h"
 #include "svm_fresnel.h"
 #include "svm_wireframe.h"
+#include "svm_wavelength.h"
 #include "svm_camera.h"
 #include "svm_geometry.h"
 #include "svm_hsv.h"
@@ -362,6 +363,9 @@ __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderT
                                svm_node_wireframe(kg, sd, stack, node.y, node.z, node.w);
                                break;
 #ifdef __EXTRA_NODES__
+                       case NODE_WAVELENGTH:
+                               svm_node_wavelength(sd, stack, node.y, node.z);
+                               break;
                        case NODE_SET_DISPLACEMENT:
                                svm_node_set_displacement(sd, stack, node.y);
                                break;
index 348f13f59f259860a8ea582b02bcf08cbd750184..6e6a9dff159663bcfcddb9f4b1bfb5654971156f 100644 (file)
 
 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 = floorf(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 */
index 888e4d9645e82b633e1349dc248c06c899205ae1..d6a306af64dccb795cf9f253843c2dcf792d946e 100644 (file)
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#include "svm_hsv.h"
-
 CCL_NAMESPACE_BEGIN
 
-__device float3 svm_lerp(const float3 a, const float3 b, float t)
-{
-       return (a * (1.0f - t) + b * t);
-}
-
 __device float3 svm_mix_blend(float t, float3 col1, float3 col2)
 {
-       return svm_lerp(col1, col2, t);
+       return interp(col1, col2, t);
 }
 
 __device float3 svm_mix_add(float t, float3 col1, float3 col2)
 {
-       return svm_lerp(col1, col1 + col2, t);
+       return interp(col1, col1 + col2, t);
 }
 
 __device float3 svm_mix_mul(float t, float3 col1, float3 col2)
 {
-       return svm_lerp(col1, col1 * col2, t);
+       return interp(col1, col1 * col2, t);
 }
 
 __device float3 svm_mix_screen(float t, float3 col1, float3 col2)
@@ -75,7 +68,7 @@ __device float3 svm_mix_overlay(float t, float3 col1, float3 col2)
 
 __device float3 svm_mix_sub(float t, float3 col1, float3 col2)
 {
-       return svm_lerp(col1, col1 - col2, t);
+       return interp(col1, col1 - col2, t);
 }
 
 __device float3 svm_mix_div(float t, float3 col1, float3 col2)
@@ -93,7 +86,7 @@ __device float3 svm_mix_div(float t, float3 col1, float3 col2)
 
 __device float3 svm_mix_diff(float t, float3 col1, float3 col2)
 {
-       return svm_lerp(col1, fabs(col1 - col2), t);
+       return interp(col1, fabs(col1 - col2), t);
 }
 
 __device float3 svm_mix_dark(float t, float3 col1, float3 col2)
@@ -191,7 +184,7 @@ __device float3 svm_mix_hue(float t, float3 col1, float3 col2)
                hsv.x = hsv2.x;
                float3 tmp = hsv_to_rgb(hsv); 
 
-               outcol = svm_lerp(outcol, tmp, t);
+               outcol = interp(outcol, tmp, t);
        }
 
        return outcol;
@@ -238,7 +231,7 @@ __device float3 svm_mix_color(float t, float3 col1, float3 col2)
                hsv.y = hsv2.y;
                float3 tmp = hsv_to_rgb(hsv); 
 
-               outcol = svm_lerp(outcol, tmp, t);
+               outcol = interp(outcol, tmp, t);
        }
 
        return outcol;
index eaba4d183650f50fd41831ace156a5c2218f9e37..8b4e35816d09cf240cacc12840bb823f81ce9690 100644 (file)
 
 CCL_NAMESPACE_BEGIN
 
-__device float3 xyY_to_xyz(float x, float y, float Y)
-{
-       float X, Z;
-
-       if(y != 0.0f) X = (x / y) * Y;
-       else X = 0.0f;
-
-       if(y != 0.0f && Y != 0.0f) Z = (1.0f - x - y) / y * Y;
-       else Z = 0.0f;
-
-       return make_float3(X, Y, Z);
-}
-
-__device float3 xyz_to_rgb(float x, float y, float z)
-{
-       return make_float3(3.240479f * x + -1.537150f * y + -0.498535f * z,
-                                         -0.969256f * x +  1.875991f * y +  0.041556f * z,
-                                          0.055648f * x + -0.204043f * y +  1.057311f * z);
-}
-
 /*
  * "A Practical Analytic Model for Daylight"
  * A. J. Preetham, Peter Shirley, Brian Smits
index af188477a73fe0d57006d3fb3d901a050d10df57..e5b3e2b972f4773de09cdb8ad2fd788811d943b1 100644 (file)
@@ -52,6 +52,7 @@ typedef enum NodeType {
        NODE_CONVERT,
        NODE_FRESNEL,
        NODE_WIREFRAME,
+       NODE_WAVELENGTH,
        NODE_EMISSION_WEIGHT,
        NODE_TEX_GRADIENT,
        NODE_TEX_VORONOI,
diff --git a/intern/cycles/kernel/svm/svm_wavelength.h b/intern/cycles/kernel/svm/svm_wavelength.h
new file mode 100644 (file)
index 0000000..f9dd24d
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Adapted from Open Shading Language with this license:
+ *
+ * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
+ * All Rights Reserved.
+ *
+ * Modifications Copyright 2013, Blender Foundation.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ * * Neither the name of Sony Pictures Imageworks nor the names of its
+ *      contributors may be used to endorse or promote products derived from
+ *      this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+CCL_NAMESPACE_BEGIN
+
+/* Wavelength to RGB */
+
+__device void svm_node_wavelength(ShaderData *sd, float *stack, uint wavelength, uint color_out)
+{      
+       // CIE colour matching functions xBar, yBar, and zBar for
+       //       wavelengths from 380 through 780 nanometers, every 5
+       //       nanometers.  For a wavelength lambda in this range:
+       //                cie_colour_match[(lambda - 380) / 5][0] = xBar
+       //                cie_colour_match[(lambda - 380) / 5][1] = yBar
+       //                cie_colour_match[(lambda - 380) / 5][2] = zBar
+       const float cie_colour_match[81][3] = {
+               {0.0014,0.0000,0.0065}, {0.0022,0.0001,0.0105}, {0.0042,0.0001,0.0201},
+               {0.0076,0.0002,0.0362}, {0.0143,0.0004,0.0679}, {0.0232,0.0006,0.1102},
+               {0.0435,0.0012,0.2074}, {0.0776,0.0022,0.3713}, {0.1344,0.0040,0.6456},
+               {0.2148,0.0073,1.0391}, {0.2839,0.0116,1.3856}, {0.3285,0.0168,1.6230},
+               {0.3483,0.0230,1.7471}, {0.3481,0.0298,1.7826}, {0.3362,0.0380,1.7721},
+               {0.3187,0.0480,1.7441}, {0.2908,0.0600,1.6692}, {0.2511,0.0739,1.5281},
+               {0.1954,0.0910,1.2876}, {0.1421,0.1126,1.0419}, {0.0956,0.1390,0.8130},
+               {0.0580,0.1693,0.6162}, {0.0320,0.2080,0.4652}, {0.0147,0.2586,0.3533},
+               {0.0049,0.3230,0.2720}, {0.0024,0.4073,0.2123}, {0.0093,0.5030,0.1582},
+               {0.0291,0.6082,0.1117}, {0.0633,0.7100,0.0782}, {0.1096,0.7932,0.0573},
+               {0.1655,0.8620,0.0422}, {0.2257,0.9149,0.0298}, {0.2904,0.9540,0.0203},
+               {0.3597,0.9803,0.0134}, {0.4334,0.9950,0.0087}, {0.5121,1.0000,0.0057},
+               {0.5945,0.9950,0.0039}, {0.6784,0.9786,0.0027}, {0.7621,0.9520,0.0021},
+               {0.8425,0.9154,0.0018}, {0.9163,0.8700,0.0017}, {0.9786,0.8163,0.0014},
+               {1.0263,0.7570,0.0011}, {1.0567,0.6949,0.0010}, {1.0622,0.6310,0.0008},
+               {1.0456,0.5668,0.0006}, {1.0026,0.5030,0.0003}, {0.9384,0.4412,0.0002},
+               {0.8544,0.3810,0.0002}, {0.7514,0.3210,0.0001}, {0.6424,0.2650,0.0000},
+               {0.5419,0.2170,0.0000}, {0.4479,0.1750,0.0000}, {0.3608,0.1382,0.0000},
+               {0.2835,0.1070,0.0000}, {0.2187,0.0816,0.0000}, {0.1649,0.0610,0.0000},
+               {0.1212,0.0446,0.0000}, {0.0874,0.0320,0.0000}, {0.0636,0.0232,0.0000},
+               {0.0468,0.0170,0.0000}, {0.0329,0.0119,0.0000}, {0.0227,0.0082,0.0000},
+               {0.0158,0.0057,0.0000}, {0.0114,0.0041,0.0000}, {0.0081,0.0029,0.0000},
+               {0.0058,0.0021,0.0000}, {0.0041,0.0015,0.0000}, {0.0029,0.0010,0.0000},
+               {0.0020,0.0007,0.0000}, {0.0014,0.0005,0.0000}, {0.0010,0.0004,0.0000},
+               {0.0007,0.0002,0.0000}, {0.0005,0.0002,0.0000}, {0.0003,0.0001,0.0000},
+               {0.0002,0.0001,0.0000}, {0.0002,0.0001,0.0000}, {0.0001,0.0000,0.0000},
+               {0.0001,0.0000,0.0000}, {0.0001,0.0000,0.0000}, {0.0000,0.0000,0.0000}
+       };
+
+       float lambda_nm = stack_load_float(stack, wavelength);
+       float ii = (lambda_nm-380.0f) * (1.0f/5.0f);  // scaled 0..80
+       int i = float_to_int(ii);
+       float3 color;
+       
+       if (i < 0 || i >= 80) {
+               color = make_float3(0.0f, 0.0f, 0.0f);
+       }
+       else {
+               ii -= i;
+               const float *c = cie_colour_match[i];
+               color = interp(make_float3(c[0], c[1], c[2]), make_float3(c[3], c[4], c[5]), ii);
+       }
+       
+       color = xyz_to_rgb(color.x, color.y, color.z);
+       color *= 1.0f/2.52f;    // Empirical scale from lg to make all comps <= 1
+       
+       /* Clamp to zero if values are smaller */
+       color = max(color, make_float3(0.0f, 0.0f, 0.0f));
+
+       if(stack_valid(color_out))
+               stack_store_float3(stack, color_out, color);
+}
+
+CCL_NAMESPACE_END
+
index f73f04ba61ca7728de6262c583b9680336317aec..bec45e23f7ac4b691ebfc40e4d82292c226c1bff 100644 (file)
@@ -2953,6 +2953,30 @@ void WireframeNode::compile(OSLCompiler& compiler)
        compiler.add(this, "node_wireframe");
 }
 
+/* Wavelength */
+
+WavelengthNode::WavelengthNode()
+: ShaderNode("Wavelength")
+{
+       add_input("Wavelength", SHADER_SOCKET_FLOAT, 500.0f);
+       add_output("Color", SHADER_SOCKET_COLOR);
+}
+
+void WavelengthNode::compile(SVMCompiler& compiler)
+{
+       ShaderInput *wavelength_in = input("Wavelength");
+       ShaderOutput *color_out = output("Color");
+
+       compiler.stack_assign(wavelength_in);
+       compiler.stack_assign(color_out);
+       compiler.add_node(NODE_WAVELENGTH, wavelength_in->stack_offset, color_out->stack_offset, NULL);
+}
+
+void WavelengthNode::compile(OSLCompiler& compiler)
+{
+       compiler.add(this, "node_wavelength");
+}
+
 /* Output */
 
 OutputNode::OutputNode()
index 2c5d9b7177a6258859316f2b6d148faed6004d05..f19e07429067db1882780bc77d7566b4485f50e8 100644 (file)
@@ -457,6 +457,11 @@ public:
        bool use_pixel_size;
 };
 
+class WavelengthNode : public ShaderNode {
+public:
+       SHADER_NODE_CLASS(WavelengthNode)
+};
+
 class MathNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(MathNode)
index 5136ea5c5db582919b8aebd210b2886d9307c3b5..0de29371899721ba43a844f213ba431c446fea37 100644 (file)
@@ -40,6 +40,100 @@ __device float color_scene_linear_to_srgb(float c)
                return 1.055f * powf(c, 1.0f / 2.4f) - 0.055f;
 }
 
+__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 = floorf(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 float3 xyY_to_xyz(float x, float y, float Y)
+{
+       float X, Z;
+
+       if(y != 0.0f) X = (x / y) * Y;
+       else X = 0.0f;
+
+       if(y != 0.0f && Y != 0.0f) Z = (1.0f - x - y) / y * Y;
+       else Z = 0.0f;
+
+       return make_float3(X, Y, Z);
+}
+
+__device float3 xyz_to_rgb(float x, float y, float z)
+{
+       return make_float3(3.240479f * x + -1.537150f * y + -0.498535f * z,
+                                         -0.969256f * x +  1.875991f * y +  0.041556f * z,
+                                          0.055648f * x + -0.204043f * y +  1.057311f * z);
+}
+
 #ifndef __KERNEL_OPENCL__
 
 __device float3 color_srgb_to_scene_linear(float3 c)
index a4e9d4d51a57e81b8ffc2493c314bae8e098714c..fe9e06301f3fa5ffe0c628c9f51825f7cefd7074 100644 (file)
@@ -218,6 +218,7 @@ shader_node_categories = [
         NodeItem("ShaderNodeVectorMath"),
         NodeItem("ShaderNodeSeparateRGB"),
         NodeItem("ShaderNodeCombineRGB"),
+        NodeItem("ShaderNodeWavelength"),
         ]),
     ShaderNewNodeCategory("SH_NEW_SCRIPT", "Script", items=[
         NodeItem("ShaderNodeScript"),
index ef51abc419e8f3eb75540c059da59c7fd1f97062..55f68fcb8bdf2ccc3b15a44168aec406f235ddd7 100644 (file)
@@ -741,6 +741,7 @@ struct ShadeResult;
 #define SH_NODE_SUBSURFACE_SCATTERING  177
 #define SH_NODE_WIREFRAME                              178
 #define SH_NODE_BSDF_TOON                              179
+#define SH_NODE_WAVELENGTH                             180
 
 /* custom defines options for Material node */
 #define SH_NODE_MAT_DIFF   1
index e4eb7c49ace171795167ba61a0dd5dc682f783f3..f805005e911d1fe2e8e66baccd26f50c9d10a0ed 100644 (file)
@@ -3399,6 +3399,7 @@ static void registerShaderNodes(void)
        register_node_type_sh_value();
        register_node_type_sh_rgb();
        register_node_type_sh_wireframe();
+       register_node_type_sh_wavelength();
        register_node_type_sh_mix_rgb();
        register_node_type_sh_valtorgb();
        register_node_type_sh_rgbtobw();
index 59f25326edb57b0ad45121f3ffb2357eb41417ea..cc1a5f8cee9a50679a48bdd4c2a0a7ce178b2f16 100644 (file)
@@ -145,6 +145,7 @@ set(SRC
        shader/nodes/node_shader_valToRgb.c
        shader/nodes/node_shader_value.c
        shader/nodes/node_shader_wireframe.c
+       shader/nodes/node_shader_wavelength.c
        shader/nodes/node_shader_vectMath.c
        shader/nodes/node_shader_add_shader.c
        shader/nodes/node_shader_ambient_occlusion.c
index 993a48495ad5d51af9cbee7f586bb3599601679e..edb0ba6f4759a203d45954628588f640738fbb5f 100644 (file)
@@ -80,6 +80,7 @@ void register_node_type_sh_light_falloff(void);
 void register_node_type_sh_object_info(void);
 void register_node_type_sh_fresnel(void);
 void register_node_type_sh_wireframe(void);
+void register_node_type_sh_wavelength(void);
 void register_node_type_sh_layer_weight(void);
 void register_node_type_sh_tex_coord(void);
 void register_node_type_sh_particle_info(void);
index bc6d5d598d21360884a24e067ee85ac057d8e812..8e3896f40f79920f9a7d66bba3c948052ae5fb10 100644 (file)
@@ -96,6 +96,7 @@ DefNode( ShaderNode,     SH_NODE_OBJECT_INFO,        0,                      "OB
 DefNode( ShaderNode,     SH_NODE_PARTICLE_INFO,      0,                      "PARTICLE_INFO",      ParticleInfo,     "Particle Info",     ""       )
 DefNode( ShaderNode,     SH_NODE_HAIR_INFO,          0,                      "HAIR_INFO",          HairInfo,         "Hair Info",         ""       )
 DefNode( ShaderNode,     SH_NODE_WIREFRAME,          def_sh_tex_wireframe,   "WIREFRAME",          Wireframe,        "Wireframe",         ""       )
+DefNode( ShaderNode,     SH_NODE_WAVELENGTH,         0,                      "WAVELENGTH",         Wavelength,       "Wavelength",        ""       )
 DefNode( ShaderNode,     SH_NODE_BUMP,               def_sh_bump,            "BUMP",               Bump,             "Bump",              ""       )
 DefNode( ShaderNode,     SH_NODE_NORMAL_MAP,         def_sh_normal_map,      "NORMAL_MAP",         NormalMap,        "Normal Map",        ""       )
 DefNode( ShaderNode,     SH_NODE_TANGENT,            def_sh_tangent,         "TANGENT",            Tangent,          "Tangent",           ""       )
diff --git a/source/blender/nodes/shader/nodes/node_shader_wavelength.c b/source/blender/nodes/shader/nodes/node_shader_wavelength.c
new file mode 100644 (file)
index 0000000..182769e
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * 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.
+ *
+ * The Original Code is Copyright (C) 2005 Blender Foundation.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include "../node_shader_util.h"
+
+/* **************** Wavelength ******************** */
+static bNodeSocketTemplate sh_node_wavelength_in[] = {
+       {       SOCK_FLOAT, 1, N_("Wavelength"),        500.0f, 0.0f, 0.0f, 0.0f, 380.0f, 780.0f},
+       {       -1, 0, ""       }
+};
+
+static bNodeSocketTemplate sh_node_wavelength_out[] = {
+       {       SOCK_RGBA, 0, N_("Color")},
+       {       -1, 0, ""       }
+};
+
+/* node type definition */
+void register_node_type_sh_wavelength(void)
+{
+       static bNodeType ntype;
+
+       sh_node_type_base(&ntype, SH_NODE_WAVELENGTH, "Wavelength", NODE_CLASS_CONVERTOR, 0);
+       node_type_compatibility(&ntype, NODE_NEW_SHADING);
+       node_type_size_preset(&ntype, NODE_SIZE_MIDDLE);
+       node_type_socket_templates(&ntype, sh_node_wavelength_in, sh_node_wavelength_out);
+       node_type_init(&ntype, NULL);
+       node_type_storage(&ntype, "", NULL, NULL);
+
+       nodeRegisterType(&ntype);
+}