[Bf-blender-cvs] [613b37bc2c8] master: Shading: Add More Features To The Voronoi Node.

OmarSquircleArt noreply at git.blender.org
Thu Sep 12 13:12:18 CEST 2019


Commit: 613b37bc2c81202a34346b40465923e8f47cebbf
Author: OmarSquircleArt
Date:   Thu Sep 12 13:09:31 2019 +0200
Branches: master
https://developer.blender.org/rB613b37bc2c81202a34346b40465923e8f47cebbf

Shading: Add More Features To The Voronoi Node.

This patch allows the Voronoi node to operate in 1D, 2D, and 4D space.
It also adds a Randomness input to control the randomness of the texture.
Additionally, it adds three new modes of operation:

- Smooth F1: A smooth version of F1 Voronoi with no discontinuities.
- Distance To Edge: Returns the distance to the edges of the cells.
- N-Sphere Radius: Returns the radius of the n-sphere inscribed in
the cells. In other words, it is half the distance between the
closest feature point and the feature point closest to it.

And it removes the following three modes of operation:

- F3.
- F4.
- Cracks.

The Distance metric is now called Euclidean, and it computes the actual
euclidean distance as opposed to the old method of computing the squared
euclidean distance.

This breaks backward compatibility in many ways, including the base case.

Reviewers: brecht, JacquesLucke

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

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

M	intern/cycles/blender/blender_shader.cpp
M	intern/cycles/kernel/closure/bsdf_util.h
M	intern/cycles/kernel/shaders/CMakeLists.txt
A	intern/cycles/kernel/shaders/node_hash.h
M	intern/cycles/kernel/shaders/node_voronoi_texture.osl
M	intern/cycles/kernel/svm/svm.h
M	intern/cycles/kernel/svm/svm_noise.h
M	intern/cycles/kernel/svm/svm_types.h
M	intern/cycles/kernel/svm/svm_voronoi.h
M	intern/cycles/render/nodes.cpp
M	intern/cycles/render/nodes.h
M	intern/cycles/util/util_math.h
M	intern/cycles/util/util_math_float2.h
M	intern/cycles/util/util_math_float3.h
M	intern/cycles/util/util_math_float4.h
M	source/blender/blenkernel/BKE_blender_version.h
M	source/blender/blenloader/intern/versioning_cycles.c
M	source/blender/editors/space_node/drawnode.c
M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/intern/gpu_material_library.h
D	source/blender/gpu/shaders/material/gpu_shader_material_cell_noise.glsl
M	source/blender/gpu/shaders/material/gpu_shader_material_math_util.glsl
M	source/blender/gpu/shaders/material/gpu_shader_material_tex_voronoi.glsl
M	source/blender/makesdna/DNA_node_types.h
M	source/blender/makesrna/intern/rna_nodetree.c
M	source/blender/nodes/shader/nodes/node_shader_tex_voronoi.c

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

diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp
index db01167cc2a..257c799a3e0 100644
--- a/intern/cycles/blender/blender_shader.cpp
+++ b/intern/cycles/blender/blender_shader.cpp
@@ -733,9 +733,9 @@ static ShaderNode *add_node(Scene *scene,
   else if (b_node.is_a(&RNA_ShaderNodeTexVoronoi)) {
     BL::ShaderNodeTexVoronoi b_voronoi_node(b_node);
     VoronoiTextureNode *voronoi = new VoronoiTextureNode();
-    voronoi->coloring = (NodeVoronoiColoring)b_voronoi_node.coloring();
-    voronoi->metric = (NodeVoronoiDistanceMetric)b_voronoi_node.distance();
+    voronoi->dimensions = b_voronoi_node.voronoi_dimensions();
     voronoi->feature = (NodeVoronoiFeature)b_voronoi_node.feature();
+    voronoi->metric = (NodeVoronoiDistanceMetric)b_voronoi_node.distance();
     BL::TexMapping b_texture_mapping(b_voronoi_node.texture_mapping());
     get_tex_mapping(&voronoi->tex_mapping, b_texture_mapping);
     node = voronoi;
diff --git a/intern/cycles/kernel/closure/bsdf_util.h b/intern/cycles/kernel/closure/bsdf_util.h
index 3bce47caedb..a73dee1b045 100644
--- a/intern/cycles/kernel/closure/bsdf_util.h
+++ b/intern/cycles/kernel/closure/bsdf_util.h
@@ -134,20 +134,6 @@ ccl_device float schlick_fresnel(float u)
   return m2 * m2 * m;  // pow(m, 5)
 }
 
-ccl_device float smooth_step(float edge0, float edge1, float x)
-{
-  float result;
-  if (x < edge0)
-    result = 0.0f;
-  else if (x >= edge1)
-    result = 1.0f;
-  else {
-    float t = (x - edge0) / (edge1 - edge0);
-    result = (3.0f - 2.0f * t) * (t * t);
-  }
-  return result;
-}
-
 /* Calculate the fresnel color which is a blend between white and the F0 color (cspec0) */
 ccl_device_forceinline float3
 interpolate_fresnel_color(float3 L, float3 H, float ior, float F0, float3 cspec0)
diff --git a/intern/cycles/kernel/shaders/CMakeLists.txt b/intern/cycles/kernel/shaders/CMakeLists.txt
index a45c43e01ed..38ba113a184 100644
--- a/intern/cycles/kernel/shaders/CMakeLists.txt
+++ b/intern/cycles/kernel/shaders/CMakeLists.txt
@@ -97,6 +97,7 @@ set(SRC_OSL
 set(SRC_OSL_HEADERS
   node_color.h
   node_fresnel.h
+  node_hash.h
   node_noise.h
   node_ramp_util.h
   stdosl.h
diff --git a/intern/cycles/kernel/shaders/node_hash.h b/intern/cycles/kernel/shaders/node_hash.h
new file mode 100644
index 00000000000..7affe432bf2
--- /dev/null
+++ b/intern/cycles/kernel/shaders/node_hash.h
@@ -0,0 +1,81 @@
+#include "stdosl.h"
+#include "vector2.h"
+#include "vector4.h"
+
+#define vector3 point
+
+/* **** Hash a float or vector[234] into a float [0, 1] **** */
+
+float hash_float_to_float(float k)
+{
+  return hashnoise(k);
+}
+
+float hash_vector2_to_float(vector2 k)
+{
+  return hashnoise(k.x, k.y);
+}
+
+float hash_vector3_to_float(vector3 k)
+{
+  return hashnoise(k);
+}
+
+float hash_vector4_to_float(vector4 k)
+{
+  return hashnoise(vector3(k.x, k.y, k.z), k.w);
+}
+
+/* **** Hash a vector[234] into a vector[234] [0, 1] **** */
+
+vector2 hash_vector2_to_vector2(vector2 k)
+{
+  return vector2(hash_vector2_to_float(k), hash_vector3_to_float(vector3(k.x, k.y, 1.0)));
+}
+
+vector3 hash_vector3_to_vector3(vector3 k)
+{
+  return vector3(hash_vector3_to_float(k),
+                 hash_vector4_to_float(vector4(k[0], k[1], k[2], 1.0)),
+                 hash_vector4_to_float(vector4(k[0], k[1], k[2], 2.0)));
+}
+
+vector4 hash_vector4_to_vector4(vector4 k)
+{
+  return vector4(hash_vector4_to_float(k),
+                 hash_vector4_to_float(vector4(k.w, k.x, k.y, k.z)),
+                 hash_vector4_to_float(vector4(k.z, k.w, k.x, k.y)),
+                 hash_vector4_to_float(vector4(k.y, k.z, k.w, k.x)));
+}
+
+/* **** Hash a float or a vec[234] into a color [0, 1] **** */
+
+color hash_float_to_color(float k)
+{
+  return color(hash_float_to_float(k),
+               hash_vector2_to_float(vector2(k, 1.0)),
+               hash_vector2_to_float(vector2(k, 2.0)));
+}
+
+color hash_vector2_to_color(vector2 k)
+{
+  return color(hash_vector2_to_float(k),
+               hash_vector3_to_float(vector3(k.x, k.y, 1.0)),
+               hash_vector3_to_float(vector3(k.x, k.y, 2.0)));
+}
+
+color hash_vector3_to_color(vector3 k)
+{
+  return color(hash_vector3_to_float(k),
+               hash_vector4_to_float(vector4(k[0], k[1], k[2], 1.0)),
+               hash_vector4_to_float(vector4(k[0], k[1], k[2], 2.0)));
+}
+
+color hash_vector4_to_color(vector4 k)
+{
+  return color(hash_vector4_to_float(k),
+               hash_vector4_to_float(vector4(k.z, k.x, k.w, k.y)),
+               hash_vector4_to_float(vector4(k.w, k.z, k.y, k.x)));
+}
+
+#undef vector3
diff --git a/intern/cycles/kernel/shaders/node_voronoi_texture.osl b/intern/cycles/kernel/shaders/node_voronoi_texture.osl
index 0d547b4b615..5de4aeef943 100644
--- a/intern/cycles/kernel/shaders/node_voronoi_texture.osl
+++ b/intern/cycles/kernel/shaders/node_voronoi_texture.osl
@@ -15,158 +15,1013 @@
  */
 
 #include "stdosl.h"
+#include "vector2.h"
+#include "vector4.h"
+#include "node_hash.h"
 
-color cellnoise_color(point p)
+#define vector3 point
+
+/* **** Distance Functions **** */
+
+float distance(float a, float b)
+{
+  return abs(a - b);
+}
+
+float distance(vector2 a, vector2 b)
+{
+  return length(a - b);
+}
+
+float distance(vector4 a, vector4 b)
+{
+  return length(a - b);
+}
+
+/* **** Safe Division **** */
+
+vector2 safe_divide(vector2 a, float b)
+{
+  return vector2((b != 0.0) ? a.x / b : 0.0, (b != 0.0) ? a.y / b : 0.0);
+}
+
+vector4 safe_divide(vector4 a, float b)
 {
-  float r = cellnoise(p);
-  float g = cellnoise(point(p[1], p[0], p[2]));
-  float b = cellnoise(point(p[1], p[2], p[0]));
+  return vector4((b != 0.0) ? a.x / b : 0.0,
+                 (b != 0.0) ? a.y / b : 0.0,
+                 (b != 0.0) ? a.z / b : 0.0,
+                 (b != 0.0) ? a.w / b : 0.0);
+}
+
+/*
+ * Smooth Voronoi:
+ *
+ * - https://wiki.blender.org/wiki/User:OmarSquircleArt/GSoC2019/Documentation/Smooth_Voronoi
+ *
+ * Distance To Edge:
+ *
+ * - https://www.shadertoy.com/view/llG3zy
+ *
+ */
 
-  return color(r, g, b);
+/* **** 1D Voronoi **** */
+
+float voronoi_distance(float a, float b, string metric, float exponent)
+{
+  return abs(a - b);
 }
 
-void voronoi_m(point p, string metric, float e, float da[4], point pa[4])
+void voronoi_f1_1d(float w,
+                   float exponent,
+                   float randomness,
+                   string metric,
+                   output float outDistance,
+                   output color outColor,
+                   output float outW)
 {
-  /* Compute the distance to and the position of the four closest neighbors to p.
-   *
-   * The neighbors are randomly placed, 1 each in a 3x3x3 grid (Worley pattern).
-   * The distances and points are returned in ascending order, i.e. da[0] and pa[0] will
-   * contain the distance to the closest point and its coordinates respectively.
-   */
-  int xx, yy, zz, xi, yi, zi;
+  float cellPosition = floor(w);
+  float localPosition = w - cellPosition;
 
-  xi = (int)floor(p[0]);
-  yi = (int)floor(p[1]);
-  zi = (int)floor(p[2]);
+  float minDistance = 8.0;
+  float targetOffset, targetPosition;
+  for (int i = -1; i <= 1; i++) {
+    float cellOffset = float(i);
+    float pointPosition = cellOffset + hash_float_to_float(cellPosition + cellOffset) * randomness;
+    float distanceToPoint = voronoi_distance(pointPosition, localPosition, metric, exponent);
+    if (distanceToPoint < minDistance) {
+      targetOffset = cellOffset;
+      minDistance = distanceToPoint;
+      targetPosition = pointPosition;
+    }
+  }
+  outDistance = minDistance;
+  outColor = hash_float_to_color(cellPosition + targetOffset);
+  outW = targetPosition + cellPosition;
+}
 
-  da[0] = 1e10;
-  da[1] = 1e10;
-  da[2] = 1e10;
-  da[3] = 1e10;
+void voronoi_smooth_f1_1d(float w,
+                          float smoothness,
+                          float exponent,
+                          float randomness,
+                          string metric,
+                          output float outDistance,
+                          output color outColor,
+                          output float outW)
+{
+  float cellPosition = floor(w);
+  float localPosition = w - cellPosition;
 
-  for (xx = xi - 1; xx <= xi + 1; xx++) {
-    for (yy = yi - 1; yy <= yi + 1; yy++) {
-      for (zz = zi - 1; zz <= zi + 1; zz++) {
-        point ip = point(xx, yy, zz);
-        point vp = (point)cellnoise_color(ip);
-        point pd = p - (vp + ip);
+  float smoothDistance = 8.0;
+  float smoothPosition = 0.0;
+  color smoothColor = color(0.0);
+  for (int i = -2; i <= 2; i++) {
+    float cellOffset = float(i);
+    float pointPosition = cellOffset + hash_float_to_float(cellPosition + cellOffset) * randomness;
+    float distanceToPoint = voronoi_distance(pointPosition, localPosition, metric, exponent);
+    float h = smoothstep(0.0, 1.0, 0.5 + 0.5 * (smoothDistance - distanceToPoint) / smoothness);
+    float correctionFactor = smoothness * h * (1.0 - h);
+    smoothDistance = mix(smoothDistance, distanceToPoint, h) - correctionFactor;
+    correctionFactor /= 1.0 + 3.0 * smoothness;
+    color cellColor = hash_float_to_color(cellPosition + cellOffset);
+    smoothColor = mix(smoothColor, cellColor, h) - correctionFactor;
+    smoothPosition = mix(smoothPosition, pointPosition, h) - correctionFactor;
+  }
+  outDistance = smoothDistance;
+  outColor = smoothColor;
+  outW = cellPosition + smoothPosition;
+}
+
+void voronoi_f2_1d(float w,
+                   float exponent,
+                   float randomness,
+                   string metric,
+                   output float outDistance,
+                   output color outColor,
+                   output float outW)
+{
+  float cellPosition = floor(w);
+  float localPosition = w - cellPosition;
+
+  float distanceF1 = 8.0;
+  float distanceF2 = 8.0;
+  float offsetF1 = 0.0;
+  float positionF1 = 0.0;
+  float offsetF2, positionF2;
+  for (int i = -1; i <= 1; i++) {
+    float cellOffset = float(i);
+    float pointPosition = cellOffset + hash_float_to_float(cellPosition + cellOffset) * randomness;
+    float distanceToPoint = voronoi_distance(pointPosition, localPosition, metric, exponent);
+    if (distanceT

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list