[Bf-blender-cvs] [ad992a82164] soc-2019-cycles-procedural: Initial implementation of improved voronoi node

OmarSquircleArt noreply at git.blender.org
Fri Jun 21 23:32:05 CEST 2019


Commit: ad992a821645360924b9447e008b449c37272c77
Author: OmarSquircleArt
Date:   Fri Jun 21 23:31:02 2019 +0200
Branches: soc-2019-cycles-procedural
https://developer.blender.org/rBad992a821645360924b9447e008b449c37272c77

Initial implementation of improved voronoi node

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

M	intern/cycles/blender/blender_session.cpp
M	intern/cycles/blender/blender_shader.cpp
M	intern/cycles/kernel/shaders/CMakeLists.txt
M	intern/cycles/kernel/shaders/node_texture.h
M	intern/cycles/kernel/shaders/node_voronoi_texture.osl
A	intern/cycles/kernel/shaders/vector2.h
A	intern/cycles/kernel/shaders/vector4.h
M	intern/cycles/kernel/svm/svm.h
M	intern/cycles/kernel/svm/svm_mapping_util.h
M	intern/cycles/kernel/svm/svm_math_util.h
M	intern/cycles/kernel/svm/svm_types.h
M	intern/cycles/kernel/svm/svm_voronoi.h
M	intern/cycles/kernel/svm/svm_white_noise.h
M	intern/cycles/render/nodes.cpp
M	intern/cycles/render/nodes.h
M	intern/cycles/subd/subd_split.cpp
M	intern/cycles/util/util_hash.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/editors/space_node/drawnode.c
M	source/blender/gpu/shaders/gpu_shader_material.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_session.cpp b/intern/cycles/blender/blender_session.cpp
index 70bbcf67d6a..9c9f4ac9345 100644
--- a/intern/cycles/blender/blender_session.cpp
+++ b/intern/cycles/blender/blender_session.cpp
@@ -1489,8 +1489,8 @@ void BlenderSession::update_resumable_tile_manager(int num_samples)
 
   /* Round after doing the multiplications with num_chunks and num_samples_per_chunk
    * to allow for many small chunks. */
-  int rounded_range_start_sample = (int)floor(range_start_sample + 0.5f);
-  int rounded_range_num_samples = max((int)floor(range_num_samples + 0.5f), 1);
+  int rounded_range_start_sample = (int)floorf(range_start_sample + 0.5f);
+  int rounded_range_num_samples = max((int)floorf(range_num_samples + 0.5f), 1);
 
   /* Make sure we don't overshoot. */
   if (rounded_range_start_sample + rounded_range_num_samples > num_samples) {
diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp
index ff9340afd63..05f68742027 100644
--- a/intern/cycles/blender/blender_shader.cpp
+++ b/intern/cycles/blender/blender_shader.cpp
@@ -726,9 +726,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.dimensions();
     voronoi->feature = (NodeVoronoiFeature)b_voronoi_node.feature();
+    voronoi->distance = (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/shaders/CMakeLists.txt b/intern/cycles/kernel/shaders/CMakeLists.txt
index aec9b1eaede..7f5f3d8525c 100644
--- a/intern/cycles/kernel/shaders/CMakeLists.txt
+++ b/intern/cycles/kernel/shaders/CMakeLists.txt
@@ -101,6 +101,8 @@ set(SRC_OSL_HEADERS
   node_texture.h
   stdosl.h
   oslutil.h
+  vector2.h
+  vector4.h
 )
 
 set(SRC_OSO
diff --git a/intern/cycles/kernel/shaders/node_texture.h b/intern/cycles/kernel/shaders/node_texture.h
index e1f3b900ee5..98aa0b7f1e2 100644
--- a/intern/cycles/kernel/shaders/node_texture.h
+++ b/intern/cycles/kernel/shaders/node_texture.h
@@ -14,77 +14,6 @@
  * limitations under the License.
  */
 
-/* Voronoi / Worley like */
-
-color cellnoise_color(point p)
-{
-  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 color(r, g, b);
-}
-
-void voronoi(point p, float e, float da[4], point pa[4])
-{
-  /* returns distances in da and point coords in pa */
-  int xx, yy, zz, xi, yi, zi;
-
-  xi = (int)floor(p[0]);
-  yi = (int)floor(p[1]);
-  zi = (int)floor(p[2]);
-
-  da[0] = 1e10;
-  da[1] = 1e10;
-  da[2] = 1e10;
-  da[3] = 1e10;
-
-  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 d = dot(pd, pd);
-
-        vp += point(xx, yy, zz);
-
-        if (d < da[0]) {
-          da[3] = da[2];
-          da[2] = da[1];
-          da[1] = da[0];
-          da[0] = d;
-
-          pa[3] = pa[2];
-          pa[2] = pa[1];
-          pa[1] = pa[0];
-          pa[0] = vp;
-        }
-        else if (d < da[1]) {
-          da[3] = da[2];
-          da[2] = da[1];
-          da[1] = d;
-
-          pa[3] = pa[2];
-          pa[2] = pa[1];
-          pa[1] = vp;
-        }
-        else if (d < da[2]) {
-          da[3] = da[2];
-          da[2] = d;
-
-          pa[3] = pa[2];
-          pa[2] = vp;
-        }
-        else if (d < da[3]) {
-          da[3] = d;
-          pa[3] = vp;
-        }
-      }
-    }
-  }
-}
-
 /* Noise Bases */
 
 float safe_noise(point p, string type)
diff --git a/intern/cycles/kernel/shaders/node_voronoi_texture.osl b/intern/cycles/kernel/shaders/node_voronoi_texture.osl
index 34c86d5b98d..be18bda1d9b 100644
--- a/intern/cycles/kernel/shaders/node_voronoi_texture.osl
+++ b/intern/cycles/kernel/shaders/node_voronoi_texture.osl
@@ -15,150 +15,1029 @@
  */
 
 #include "stdosl.h"
-#include "node_texture.h"
-
-void voronoi_m(point p, string metric, float e, float da[4], point pa[4])
-{
-  /* 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;
-
-  xi = (int)floor(p[0]);
-  yi = (int)floor(p[1]);
-  zi = (int)floor(p[2]);
-
-  da[0] = 1e10;
-  da[1] = 1e10;
-  da[2] = 1e10;
-  da[3] = 1e10;
-
-  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 d = 0.0;
-        if (metric == "distance") {
-          d = dot(pd, pd);
+#include "vector2.h"
+#include "vector4.h"
+
+#define vector3 point
+
+/* **** Hash a float or vector[234] into a float [0, 1] **** */
+
+float hash_01(float k)
+{
+  return hashnoise(k);
+}
+
+float hash_01(vector2 k)
+{
+  return hashnoise(k.x, k.y);
+}
+
+float hash_01(vector3 k)
+{
+  return hashnoise(k);
+}
+
+float hash_01(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_01_vector2(vector2 k)
+{
+  return vector2(hash_01(k), hash_01(vector3(k.x, k.y, 1.0)));
+}
+
+vector3 hash_01_vector3(vector3 k)
+{
+  return vector3(
+      hash_01(k), hash_01(vector4(k[0], k[1], k[2], 1.0)), hash_01(vector4(k[0], k[1], k[2], 2.0)));
+}
+
+vector4 hash_01_vector4(vector4 k)
+{
+  return vector4(hash_01(k),
+                 hash_01(vector4(k.w, k.x, k.y, k.z)),
+                 hash_01(vector4(k.z, k.w, k.x, k.y)),
+                 hash_01(vector4(k.y, k.z, k.w, k.x)));
+}
+
+/* **** Hash a float or a vec[234] into a color [0, 1] **** */
+
+color hash_01_color(float k)
+{
+  return color(hash_01(k), hash_01(vector2(k, 1.0)), hash_01(vector2(k, 2.0)));
+}
+
+color hash_01_color(vector2 k)
+{
+  return color(hash_01(k), hash_01(vector3(k.x, k.y, 1.0)), hash_01(vector3(k.x, k.y, 2.0)));
+}
+
+color hash_01_color(vector3 k)
+{
+  return color(
+      hash_01(k), hash_01(vector4(k[0], k[1], k[2], 1.0)), hash_01(vector4(k[0], k[1], k[2], 2.0)));
+}
+
+color hash_01_color(vector4 k)
+{
+  return color(
+      hash_01(k), hash_01(vector4(k.z, k.x, k.w, k.y)), hash_01(vector4(k.w, k.z, k.y, k.x)));
+}
+
+/* **** 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 **** */
+
+// OSL ternary operator only works with floats for some reason.
+
+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)
+{
+  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);
+}
+
+/* **** Voronoi Texture **** */
+
+// Each of the following functions computes a certain voronoi feature in a certain dimension.
+// Independent functions are used because every feature/dimension have a different search area.
+//
+// This code is based on the following:
+// Base code : http://www.iquilezles.org/www/articles/smoothvoronoi/smoothvoronoi.htm
+// Smoothing : https://iquilezles.untergrund.net/www/articles/smin/smin.htm
+// Distance To Edge Method : https://www.shadertoy.com/view/llG3zy
+
+/* **** 1D Voronoi **** */
+
+float voronoi_distance(float a, float b, string metric, float exponent)
+{
+  return abs(a - b);
+}
+
+void voronoi_f1_1d(float w,
+                   float exponent,
+                   float jitter,
+                   string metric,
+                   output float outDistance,
+                   output color outColor,
+                   output float outW)
+{
+  float cellPosition = floor(w);
+  float localPosition = w - cellPosition;
+
+  float minDistance = 8.0;
+  float targetOffset, targetPosition;
+  for (int i = -1; i <= 1; i++) {
+    float cellOffset = float(i);
+    float pointPosition = cellOffset + hash_01(cellPosition + cellOffset) * jitter;
+    float distanceToPoint = voronoi_distance(pointPosition, localPosition, metric, exponent);
+    if (distanceToPoint < minDistance) {
+      targetOffset = cellOffset;
+      minDistance = distanceToPoint;
+      targetPosition = pointPosition;
+    }
+  }
+  outDistance = minDistance;
+  outColor = hash_01_color(cellPosition + targetOffset);
+  outW = targetPosition + cellPosition;
+}
+
+void voronoi_smooth_f1_1d(float w,
+                          float smoothness,
+                          float exponent,
+                          float jitter,
+                          string metric,
+                          output float outDistance,
+                          output color outColor,
+                          output float outW)
+{
+  float cellPosition = floor(w);
+  float localPosition = w - cellPosition;
+
+  float smoothDistance = 0.0;
+  float smoothPosition = 0.0;
+  color smoothColor = color(0.0);
+  for (int i = -3; i <= 3; i++) {
+    float cellOffset = float(i);
+    float pointPosition = cellOffset + hash_01(cellPosition + cellOffset) * jitter;
+    float distanceToPoint = voronoi_distance(pointPosition, localPosition, metric, exponent);
+    float weight = exp(-smoothness * distanceToPoint);
+    smoothDistance += weight;
+    

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list