[Bf-blender-cvs] [bfa0ee13d53] master: Node: Mix node

Charlie Jolly noreply at git.blender.org
Wed Aug 31 01:15:29 CEST 2022


Commit: bfa0ee13d5395fa2cf622b6808e93d0a7cd4f3ea
Author: Charlie Jolly
Date:   Tue Aug 30 11:05:46 2022 +0100
Branches: master
https://developer.blender.org/rBbfa0ee13d5395fa2cf622b6808e93d0a7cd4f3ea

Node: Mix node

This patch is a response to T92588 and is implemented
as a Function/Shader node.

This node has support for Float, Vector and Color data types.

For Vector it supports uniform and non-uniform mixing.

For Color it now has the option to remove factor clamping.

It replaces the Mix RGB for Shader and Geometry node trees.

As discussed in T96219, this patch converts existing nodes
in .blend files. The old node is still available in the
Python API but hidden from the menus.

Reviewed By: HooglyBoogly, JacquesLucke, simonthommes, brecht

Maniphest Tasks: T92588

Differential Revision: https://developer.blender.org/D13749

===================================================================

M	intern/cycles/blender/shader.cpp
M	intern/cycles/kernel/osl/shaders/CMakeLists.txt
A	intern/cycles/kernel/osl/shaders/node_color_blend.h
A	intern/cycles/kernel/osl/shaders/node_mix_color.osl
A	intern/cycles/kernel/osl/shaders/node_mix_float.osl
A	intern/cycles/kernel/osl/shaders/node_mix_vector.osl
A	intern/cycles/kernel/osl/shaders/node_mix_vector_non_uniform.osl
M	intern/cycles/kernel/svm/color_util.h
M	intern/cycles/kernel/svm/mix.h
M	intern/cycles/kernel/svm/node_types_template.h
M	intern/cycles/kernel/svm/svm.h
M	intern/cycles/kernel/svm/types.h
M	intern/cycles/scene/constant_fold.cpp
M	intern/cycles/scene/constant_fold.h
M	intern/cycles/scene/shader_nodes.cpp
M	intern/cycles/scene/shader_nodes.h
M	release/scripts/startup/nodeitems_builtins.py
M	source/blender/blenkernel/BKE_node.h
M	source/blender/blenkernel/intern/node.cc
M	source/blender/blenloader/intern/versioning_300.c
M	source/blender/editors/space_node/drawnode.cc
M	source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp
M	source/blender/gpu/CMakeLists.txt
A	source/blender/gpu/shaders/material/gpu_shader_material_mix_color.glsl
M	source/blender/makesdna/DNA_node_types.h
M	source/blender/makesrna/intern/rna_nodetree.c
M	source/blender/nodes/NOD_shader.h
M	source/blender/nodes/NOD_static_types.h
M	source/blender/nodes/shader/CMakeLists.txt
A	source/blender/nodes/shader/nodes/node_shader_mix.cc
M	source/blender/nodes/shader/nodes/node_shader_mix_rgb.cc

===================================================================

diff --git a/intern/cycles/blender/shader.cpp b/intern/cycles/blender/shader.cpp
index 04eb1576330..9505f4ba58f 100644
--- a/intern/cycles/blender/shader.cpp
+++ b/intern/cycles/blender/shader.cpp
@@ -350,6 +350,33 @@ static ShaderNode *add_node(Scene *scene,
     mix->set_use_clamp(b_mix_node.use_clamp());
     node = mix;
   }
+  else if (b_node.is_a(&RNA_ShaderNodeMix)) {
+    BL::ShaderNodeMix b_mix_node(b_node);
+    if (b_mix_node.data_type() == BL::ShaderNodeMix::data_type_VECTOR) {
+      if (b_mix_node.factor_mode() == BL::ShaderNodeMix::factor_mode_UNIFORM) {
+        MixVectorNode *mix_node = graph->create_node<MixVectorNode>();
+        mix_node->set_use_clamp(b_mix_node.clamp_factor());
+        node = mix_node;
+      }
+      else {
+        MixVectorNonUniformNode *mix_node = graph->create_node<MixVectorNonUniformNode>();
+        mix_node->set_use_clamp(b_mix_node.clamp_factor());
+        node = mix_node;
+      }
+    }
+    else if (b_mix_node.data_type() == BL::ShaderNodeMix::data_type_RGBA) {
+      MixColorNode *mix_node = graph->create_node<MixColorNode>();
+      mix_node->set_blend_type((NodeMix)b_mix_node.blend_type());
+      mix_node->set_use_clamp(b_mix_node.clamp_factor());
+      mix_node->set_use_clamp_result(b_mix_node.clamp_result());
+      node = mix_node;
+    }
+    else {
+      MixFloatNode *mix_node = graph->create_node<MixFloatNode>();
+      mix_node->set_use_clamp(b_mix_node.clamp_factor());
+      node = mix_node;
+    }
+  }
   else if (b_node.is_a(&RNA_ShaderNodeSeparateRGB)) {
     node = graph->create_node<SeparateRGBNode>();
   }
@@ -1072,7 +1099,9 @@ static bool node_use_modified_socket_name(ShaderNode *node)
   return true;
 }
 
-static ShaderInput *node_find_input_by_name(ShaderNode *node, BL::NodeSocket &b_socket)
+static ShaderInput *node_find_input_by_name(BL::Node b_node,
+                                            ShaderNode *node,
+                                            BL::NodeSocket &b_socket)
 {
   string name = b_socket.identifier();
   ShaderInput *input = node->input(name.c_str());
@@ -1082,6 +1111,35 @@ static ShaderInput *node_find_input_by_name(ShaderNode *node, BL::NodeSocket &b_
     if (string_startswith(name, "Shader")) {
       string_replace(name, "Shader", "Closure");
     }
+
+    /* Map mix node internal name for shader. */
+    if (b_node.is_a(&RNA_ShaderNodeMix)) {
+      if (string_endswith(name, "Factor_Float")) {
+        string_replace(name, "Factor_Float", "Factor");
+      }
+      else if (string_endswith(name, "Factor_Vector")) {
+        string_replace(name, "Factor_Vector", "Factor");
+      }
+      else if (string_endswith(name, "A_Float")) {
+        string_replace(name, "A_Float", "A");
+      }
+      else if (string_endswith(name, "B_Float")) {
+        string_replace(name, "B_Float", "B");
+      }
+      else if (string_endswith(name, "A_Color")) {
+        string_replace(name, "A_Color", "A");
+      }
+      else if (string_endswith(name, "B_Color")) {
+        string_replace(name, "B_Color", "B");
+      }
+      else if (string_endswith(name, "A_Vector")) {
+        string_replace(name, "A_Vector", "A");
+      }
+      else if (string_endswith(name, "B_Vector")) {
+        string_replace(name, "B_Vector", "B");
+      }
+    }
+
     input = node->input(name.c_str());
 
     if (!input) {
@@ -1111,7 +1169,9 @@ static ShaderInput *node_find_input_by_name(ShaderNode *node, BL::NodeSocket &b_
   return input;
 }
 
-static ShaderOutput *node_find_output_by_name(ShaderNode *node, BL::NodeSocket &b_socket)
+static ShaderOutput *node_find_output_by_name(BL::Node b_node,
+                                              ShaderNode *node,
+                                              BL::NodeSocket &b_socket)
 {
   string name = b_socket.identifier();
   ShaderOutput *output = node->output(name.c_str());
@@ -1122,6 +1182,21 @@ static ShaderOutput *node_find_output_by_name(ShaderNode *node, BL::NodeSocket &
       name = "Closure";
       output = node->output(name.c_str());
     }
+    /* Map internal name for shader. */
+    if (b_node.is_a(&RNA_ShaderNodeMix)) {
+      if (string_endswith(name, "Result_Float")) {
+        string_replace(name, "Result_Float", "Result");
+        output = node->output(name.c_str());
+      }
+      else if (string_endswith(name, "Result_Color")) {
+        string_replace(name, "Result_Color", "Result");
+        output = node->output(name.c_str());
+      }
+      else if (string_endswith(name, "Result_Vector")) {
+        string_replace(name, "Result_Vector", "Result");
+        output = node->output(name.c_str());
+      }
+    }
   }
 
   return output;
@@ -1267,7 +1342,11 @@ static void add_nodes(Scene *scene,
       if (node) {
         /* map node sockets for linking */
         for (BL::NodeSocket &b_input : b_node.inputs) {
-          ShaderInput *input = node_find_input_by_name(node, b_input);
+          if (b_input.is_unavailable()) {
+            /* Skip unavailable sockets. */
+            continue;
+          }
+          ShaderInput *input = node_find_input_by_name(b_node, node, b_input);
           if (!input) {
             /* XXX should not happen, report error? */
             continue;
@@ -1277,7 +1356,11 @@ static void add_nodes(Scene *scene,
           set_default_value(input, b_input, b_data, b_ntree);
         }
         for (BL::NodeSocket &b_output : b_node.outputs) {
-          ShaderOutput *output = node_find_output_by_name(node, b_output);
+          if (b_output.is_unavailable()) {
+            /* Skip unavailable sockets. */
+            continue;
+          }
+          ShaderOutput *output = node_find_output_by_name(b_node, node, b_output);
           if (!output) {
             /* XXX should not happen, report error? */
             continue;
diff --git a/intern/cycles/kernel/osl/shaders/CMakeLists.txt b/intern/cycles/kernel/osl/shaders/CMakeLists.txt
index 741bce7c399..c79af3f6112 100644
--- a/intern/cycles/kernel/osl/shaders/CMakeLists.txt
+++ b/intern/cycles/kernel/osl/shaders/CMakeLists.txt
@@ -57,6 +57,10 @@ set(SRC_OSL
   node_math.osl
   node_mix.osl
   node_mix_closure.osl
+  node_mix_color.osl
+  node_mix_float.osl
+  node_mix_vector.osl
+  node_mix_vector_non_uniform.osl
   node_musgrave_texture.osl
   node_noise_texture.osl
   node_normal.osl
@@ -109,6 +113,7 @@ file(GLOB SRC_OSL_HEADER_DIST ${OSL_SHADER_DIR}/*.h)
 
 set(SRC_OSL_HEADERS
   node_color.h
+  node_color_blend.h
   node_fresnel.h
   node_hash.h
   node_math.h
diff --git a/intern/cycles/kernel/osl/shaders/node_color_blend.h b/intern/cycles/kernel/osl/shaders/node_color_blend.h
new file mode 100644
index 00000000000..ab4b4809a97
--- /dev/null
+++ b/intern/cycles/kernel/osl/shaders/node_color_blend.h
@@ -0,0 +1,264 @@
+/* SPDX-License-Identifier: Apache-2.0
+ * Copyright 2011-2022 Blender Foundation */
+
+color node_mix_blend(float t, color col1, color col2)
+{
+  return mix(col1, col2, t);
+}
+
+color node_mix_add(float t, color col1, color col2)
+{
+  return mix(col1, col1 + col2, t);
+}
+
+color node_mix_mul(float t, color col1, color col2)
+{
+  return mix(col1, col1 * col2, t);
+}
+
+color node_mix_screen(float t, color col1, color col2)
+{
+  float tm = 1.0 - t;
+
+  return color(1.0) - (color(tm) + t * (color(1.0) - col2)) * (color(1.0) - col1);
+}
+
+color node_mix_overlay(float t, color col1, color col2)
+{
+  float tm = 1.0 - t;
+
+  color outcol = col1;
+
+  if (outcol[0] < 0.5)
+    outcol[0] *= tm + 2.0 * t * col2[0];
+  else
+    outcol[0] = 1.0 - (tm + 2.0 * t * (1.0 - col2[0])) * (1.0 - outcol[0]);
+
+  if (outcol[1] < 0.5)
+    outcol[1] *= tm + 2.0 * t * col2[1];
+  else
+    outcol[1] = 1.0 - (tm + 2.0 * t * (1.0 - col2[1])) * (1.0 - outcol[1]);
+
+  if (outcol[2] < 0.5)
+    outcol[2] *= tm + 2.0 * t * col2[2];
+  else
+    outcol[2] = 1.0 - (tm + 2.0 * t * (1.0 - col2[2])) * (1.0 - outcol[2]);
+
+  return outcol;
+}
+
+color node_mix_sub(float t, color col1, color col2)
+{
+  return mix(col1, col1 - col2, t);
+}
+
+color node_mix_div(float t, color col1, color col2)
+{
+  float tm = 1.0 - t;
+
+  color outcol = col1;
+
+  if (col2[0] != 0.0)
+    outcol[0] = tm * outcol[0] + t * outcol[0] / col2[0];
+  if (col2[1] != 0.0)
+    outcol[1] = tm * outcol[1] + t * outcol[1] / col2[1];
+  if (col2[2] != 0.0)
+    outcol[2] = tm * outcol[2] + t * outcol[2] / col2[2];
+
+  return outcol;
+}
+
+color node_mix_diff(float t, color col1, color col2)
+{
+  return mix(col1, abs(col1 - col2), t);
+}
+
+color node_mix_dark(float t, color col1, color col2)
+{
+  return mix(col1, min(col1, col2), t);
+}
+
+color node_mix_light(float t, color col1, color col2)
+{
+  return mix(col1, max(col1, col2), t);
+}
+
+color node_mix_dodge(float t, color col1, color col2)
+{
+  color outcol = col1;
+
+  if (outcol[0] != 0.0) {
+    float tmp = 1.0 - t * col2[0];
+    if (tmp <= 0.0)
+      outcol[0] = 1.0;
+    else if ((tmp = outcol[0] / tmp) > 1.0)
+      outcol[0] = 1.0;
+    else
+      outcol[0] = tmp;
+  }
+  if (outcol[1] != 0.0) {
+    float tmp = 1.0 - t * col2[1];
+    if (tmp <= 0.0)
+      outcol[1] = 1.0;
+    else if ((tmp = outcol[1] / tmp) > 1.0)
+      outcol[1] = 1.0;
+    else
+      outcol[1] = tmp;
+  }
+  if (outcol[2] != 0.0) {
+    float tmp = 1.0 - t * col2[2];
+    if (tmp <= 0.0)
+      outcol[2] = 1.0;
+    else if ((tmp = outcol[2] / tmp) > 1.0)
+      outcol[2] = 1.0;
+    else
+      outcol[2] = tmp;
+  }
+
+  return outcol;
+}
+
+color node_mix_burn(float t, color col1, color col2)
+{
+  float tmp, tm = 1.0 - t;
+
+  color outcol = col1;
+
+  tmp = tm + t * col2[0];
+  if (tmp <= 0.0)
+    outcol[0] = 0.0;
+  else if ((tmp = (1.0 - (1.0 - outcol[0]) / tmp)) < 0.0)
+    outcol[0] = 0.0;
+  else if (tmp > 1.0)
+    outcol[0] = 1.0;
+  else
+    outcol[0] = tmp;
+
+  tmp = tm + t * col2[1];
+  if (tmp <= 0.0)
+    outcol[1] = 0.0;
+  else if ((tmp = (1.0 - (1.0 - outcol[1]) / tmp)) < 0.0)
+    outcol[1] = 0.0;
+  else if (tmp > 1.0)
+    outcol[1] = 1.0;
+  else
+    outcol[1] = tmp;
+
+  tmp = tm + t * col2[2];
+  if (tmp <= 0.0)
+    outcol[2] = 0.0;
+  else if ((tmp = (1.0 - (1.0 - outcol[2]) / tmp)) < 0.0)
+    outcol[2] = 0.0;
+  else if (tmp > 1.0)
+    outcol[2] = 1.0;
+  else
+    out

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list