[Bf-blender-cvs] [67d12bb5192] master: Shading: add direction modes and phase offset to wave texture node

Bartosz Moniewski noreply at git.blender.org
Mon Feb 17 13:24:12 CET 2020


Commit: 67d12bb5192d04a596e216f63cff2875fdd8cfbf
Author: Bartosz Moniewski
Date:   Mon Feb 17 12:31:38 2020 +0100
Branches: master
https://developer.blender.org/rB67d12bb5192d04a596e216f63cff2875fdd8cfbf

Shading: add direction modes and phase offset to wave texture node

* Direction mode X, Y and Z to align with axes rather than diagonal or
  spherical as previously. X is the new default, existing files will
  use diagonal or spherical for compatibility.
* Phase offset to offset the wave along its direction, for purposes like
  animation and distortion.

https://developer.blender.org/D6382

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

M	intern/cycles/blender/blender_shader.cpp
M	intern/cycles/kernel/shaders/node_wave_texture.osl
M	intern/cycles/kernel/svm/svm_types.h
M	intern/cycles/kernel/svm/svm_wave.h
M	intern/cycles/render/nodes.cpp
M	intern/cycles/render/nodes.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/shaders/material/gpu_shader_material_tex_wave.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_wave.c

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

diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp
index 635405e42c7..12009d6160c 100644
--- a/intern/cycles/blender/blender_shader.cpp
+++ b/intern/cycles/blender/blender_shader.cpp
@@ -770,6 +770,8 @@ static ShaderNode *add_node(Scene *scene,
     BL::ShaderNodeTexWave b_wave_node(b_node);
     WaveTextureNode *wave = new WaveTextureNode();
     wave->type = (NodeWaveType)b_wave_node.wave_type();
+    wave->bands_direction = (NodeWaveBandsDirection)b_wave_node.bands_direction();
+    wave->rings_direction = (NodeWaveRingsDirection)b_wave_node.rings_direction();
     wave->profile = (NodeWaveProfile)b_wave_node.wave_profile();
     BL::TexMapping b_texture_mapping(b_wave_node.texture_mapping());
     get_tex_mapping(&wave->tex_mapping, b_texture_mapping);
diff --git a/intern/cycles/kernel/shaders/node_wave_texture.osl b/intern/cycles/kernel/shaders/node_wave_texture.osl
index 5b27c570e42..5f97976e582 100644
--- a/intern/cycles/kernel/shaders/node_wave_texture.osl
+++ b/intern/cycles/kernel/shaders/node_wave_texture.osl
@@ -19,40 +19,81 @@
 
 /* Wave */
 
-float wave(point p, string type, string profile, float detail, float distortion, float dscale)
+float wave(point p,
+           string type,
+           string bands_direction,
+           string rings_direction,
+           string profile,
+           float detail,
+           float distortion,
+           float dscale,
+           float phase)
 {
+  /* Prevent precision issues on unit coordinates. */
+  p = (p + 0.000001) * 0.999999;
+
   float n = 0.0;
 
   if (type == "bands") {
-    n = (p[0] + p[1] + p[2]) * 10.0;
+    if (bands_direction == "x") {
+      n = p[0] * 20.0;
+    }
+    else if (bands_direction == "y") {
+      n = p[1] * 20.0;
+    }
+    else if (bands_direction == "z") {
+      n = p[2] * 20.0;
+    }
+    else { /* diagonal */
+      n = (p[0] + p[1] + p[2]) * 10.0;
+    }
   }
   else if (type == "rings") {
-    n = length(p) * 20.0;
+    point rp = p;
+    if (rings_direction == "x") {
+      rp *= point(0.0, 1.0, 1.0);
+    }
+    else if (rings_direction == "y") {
+      rp *= point(1.0, 0.0, 1.0);
+    }
+    else if (rings_direction == "z") {
+      rp *= point(1.0, 1.0, 0.0);
+    }
+    /* else: "spherical" */
+
+    n = length(rp) * 20.0;
   }
 
+  n += phase;
+
   if (distortion != 0.0) {
     n = n + (distortion * (fractal_noise(p * dscale, detail) * 2.0 - 1.0));
   }
 
   if (profile == "sine") {
-    return 0.5 + 0.5 * sin(n);
+    return 0.5 + 0.5 * sin(n - M_PI_2);
+  }
+  else if (profile == "saw") {
+    n /= M_2PI;
+    return n - floor(n);
   }
-  else {
-    /* Saw profile */
+  else { /* profile tri */
     n /= M_2PI;
-    n -= (int)n;
-    return (n < 0.0) ? n + 1.0 : n;
+    return abs(n - floor(n + 0.5)) * 2.0;
   }
 }
 
 shader node_wave_texture(int use_mapping = 0,
                          matrix mapping = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
                          string type = "bands",
+                         string bands_direction = "x",
+                         string rings_direction = "x",
                          string profile = "sine",
                          float Scale = 5.0,
                          float Distortion = 0.0,
                          float Detail = 2.0,
                          float DetailScale = 1.0,
+                         float PhaseOffset = 0.0,
                          point Vector = P,
                          output float Fac = 0.0,
                          output color Color = 0.0)
@@ -62,6 +103,14 @@ shader node_wave_texture(int use_mapping = 0,
   if (use_mapping)
     p = transform(mapping, p);
 
-  Fac = wave(p * Scale, type, profile, Detail, Distortion, DetailScale);
+  Fac = wave(p * Scale,
+             type,
+             bands_direction,
+             rings_direction,
+             profile,
+             Detail,
+             Distortion,
+             DetailScale,
+             PhaseOffset);
   Color = Fac;
 }
diff --git a/intern/cycles/kernel/svm/svm_types.h b/intern/cycles/kernel/svm/svm_types.h
index 828b43eb8e4..45c299e0acb 100644
--- a/intern/cycles/kernel/svm/svm_types.h
+++ b/intern/cycles/kernel/svm/svm_types.h
@@ -384,9 +384,24 @@ typedef enum NodeMusgraveType {
 
 typedef enum NodeWaveType { NODE_WAVE_BANDS, NODE_WAVE_RINGS } NodeWaveType;
 
-typedef enum NodeWaveProfiles {
+typedef enum NodeWaveBandsDirection {
+  NODE_WAVE_BANDS_DIRECTION_X,
+  NODE_WAVE_BANDS_DIRECTION_Y,
+  NODE_WAVE_BANDS_DIRECTION_Z,
+  NODE_WAVE_BANDS_DIRECTION_DIAGONAL
+} NodeWaveBandsDirection;
+
+typedef enum NodeWaveRingsDirection {
+  NODE_WAVE_RINGS_DIRECTION_X,
+  NODE_WAVE_RINGS_DIRECTION_Y,
+  NODE_WAVE_RINGS_DIRECTION_Z,
+  NODE_WAVE_RINGS_DIRECTION_SPHERICAL
+} NodeWaveRingsDirection;
+
+typedef enum NodeWaveProfile {
   NODE_WAVE_PROFILE_SIN,
   NODE_WAVE_PROFILE_SAW,
+  NODE_WAVE_PROFILE_TRI,
 } NodeWaveProfile;
 
 typedef enum NodeSkyType { NODE_SKY_OLD, NODE_SKY_NEW } NodeSkyType;
diff --git a/intern/cycles/kernel/svm/svm_wave.h b/intern/cycles/kernel/svm/svm_wave.h
index 50c868c0f82..64102535f7d 100644
--- a/intern/cycles/kernel/svm/svm_wave.h
+++ b/intern/cycles/kernel/svm/svm_wave.h
@@ -19,52 +19,101 @@ CCL_NAMESPACE_BEGIN
 /* Wave */
 
 ccl_device_noinline_cpu float svm_wave(NodeWaveType type,
+                                       NodeWaveBandsDirection bands_dir,
+                                       NodeWaveRingsDirection rings_dir,
                                        NodeWaveProfile profile,
                                        float3 p,
                                        float detail,
                                        float distortion,
-                                       float dscale)
+                                       float dscale,
+                                       float phase)
 {
+  /* Prevent precision issues on unit coordinates. */
+  p = (p + 0.000001f) * 0.999999f;
+
   float n;
 
-  if (type == NODE_WAVE_BANDS)
-    n = (p.x + p.y + p.z) * 10.0f;
-  else /* NODE_WAVE_RINGS */
-    n = len(p) * 20.0f;
+  if (type == NODE_WAVE_BANDS) {
+    if (bands_dir == NODE_WAVE_BANDS_DIRECTION_X) {
+      n = p.x * 20.0f;
+    }
+    else if (bands_dir == NODE_WAVE_BANDS_DIRECTION_Y) {
+      n = p.y * 20.0f;
+    }
+    else if (bands_dir == NODE_WAVE_BANDS_DIRECTION_Z) {
+      n = p.z * 20.0f;
+    }
+    else { /* NODE_WAVE_BANDS_DIRECTION_DIAGONAL */
+      n = (p.x + p.y + p.z) * 10.0f;
+    }
+  }
+  else { /* NODE_WAVE_RINGS */
+    float3 rp = p;
+    if (rings_dir == NODE_WAVE_RINGS_DIRECTION_X) {
+      rp *= make_float3(0.0f, 1.0f, 1.0f);
+    }
+    else if (rings_dir == NODE_WAVE_RINGS_DIRECTION_Y) {
+      rp *= make_float3(1.0f, 0.0f, 1.0f);
+    }
+    else if (rings_dir == NODE_WAVE_RINGS_DIRECTION_Z) {
+      rp *= make_float3(1.0f, 1.0f, 0.0f);
+    }
+    /* else: NODE_WAVE_RINGS_DIRECTION_SPHERICAL */
+
+    n = len(rp) * 20.0f;
+  }
+
+  n += phase;
 
   if (distortion != 0.0f)
     n += distortion * (fractal_noise_3d(p * dscale, detail) * 2.0f - 1.0f);
 
   if (profile == NODE_WAVE_PROFILE_SIN) {
-    return 0.5f + 0.5f * sinf(n);
+    return 0.5f + 0.5f * sinf(n - M_PI_2_F);
+  }
+  else if (profile == NODE_WAVE_PROFILE_SAW) {
+    n /= M_2PI_F;
+    return n - floorf(n);
   }
-  else { /* NODE_WAVE_PROFILE_SAW */
+  else { /* NODE_WAVE_PROFILE_TRI */
     n /= M_2PI_F;
-    n -= (int)n;
-    return (n < 0.0f) ? n + 1.0f : n;
+    return fabsf(n - floorf(n + 0.5f)) * 2.0f;
   }
 }
 
 ccl_device void svm_node_tex_wave(
     KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
 {
-  uint4 node2 = read_node(kg, offset);
+  uint4 defaults1 = read_node(kg, offset);
+  uint4 defaults2 = read_node(kg, offset);
 
-  uint type;
-  uint co_offset, scale_offset, detail_offset, dscale_offset, distortion_offset, color_offset,
-      fac_offset;
+  /* RNA properties */
+  uint type_offset, bands_dir_offset, rings_dir_offset, profile_offset;
+  /* Inputs, Outputs */
+  uint co_offset, scale_offset, distortion_offset, detail_offset, dscale_offset, phase_offset;
+  uint color_offset, fac_offset;
 
-  svm_unpack_node_uchar4(node.y, &type, &color_offset, &fac_offset, &dscale_offset);
-  svm_unpack_node_uchar4(node.z, &co_offset, &scale_offset, &detail_offset, &distortion_offset);
+  svm_unpack_node_uchar4(
+      node.y, &type_offset, &bands_dir_offset, &rings_dir_offset, &profile_offset);
+  svm_unpack_node_uchar4(node.z, &co_offset, &scale_offset, &distortion_offset, &detail_offset);
+  svm_unpack_node_uchar4(node.w, &dscale_offset, &phase_offset, &color_offset, &fac_offset);
 
   float3 co = stack_load_float3(stack, co_offset);
-  float scale = stack_load_float_default(stack, scale_offset, node2.x);
-  float detail = stack_load_float_default(stack, detail_offset, node2.y);
-  float distortion = stack_load_float_default(stack, distortion_offset, node2.z);
-  float dscale = stack_load_float_default(stack, dscale_offset, node2.w);
+  float scale = stack_load_float_default(stack, scale_offset, defaults1.x);
+  float detail = stack_load_float_default(stack, detail_offset, defaults1.y);
+  float distortion = stack_load_float_default(stack, distortion_offset, defaults1.z);
+  float dscale = stack_load_float_default(stack, dscale_offset, defaults1.w);
+  float phase = stack_load_float_default(stack, phase_offset, defaults2.x);
 
-  float f = svm_wave(
-      (NodeWaveType)type, (NodeWaveProfile)node.w, co * scale, detail, distortion, dscale);
+  float f = svm_wave((NodeWaveType)type_offset,
+                     (NodeWaveBandsDirection)bands_dir_offset,
+                     (NodeWaveRingsDirection)rings_dir_offset,
+                     (NodeWaveProfile)profile_offset,
+                     co * scale,
+                     detail,
+                     distortion,
+                     dscale,
+                     phase);
 
   if (stack_valid(fac_offset))
     stack_store_float(stack, fac_offset, f);
diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp
index 3f5c2aacc98..83b973011a1 100644
--- a/intern/cycles/render/nodes.cpp
+++ b/intern/cycles/render/nodes.cpp
@@ -1422,15 +1422,33 @@ NODE_DEFINE(WaveTextureNode)
   type_enum.insert(

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list