Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBartosz Moniewski <monio>2019-12-06 00:07:43 +0300
committerBrecht Van Lommel <brechtvanlommel@gmail.com>2019-12-07 21:06:27 +0300
commit074c00f9d6cc720461e6c8c7c13ed4e408ef372e (patch)
tree0d4efaa01952af47f5ea7a70cb14d764a2f597af
parent9c1134015c34ff19f3ff27c89321c019c67904b8 (diff)
Shaders: noise and wave distortion now work uniformly instead of diagonally
Previously Noise and Wave texture nodes would use noise functions within a [0,1] range for distortion effects. We either add or subtract noise from coordinates, never do both at same time. This led to the texture drastically shifting on the diagonal axis of a plane / cube. This behavior makes the Distortion input hard to control or animate. Capabilities of driving it with other texture are also limited, diagonal shifting is very apparent. This was fixed by offsetting the noise function to a signed range and making it zero-centered. This way noise is uniformly added and subtracted from coordinates. Texture pattern sticks to main coordinates which makes it way easier to control. This change is not strictly backwards compatible, there is versioning to ensure the scale of the distortion remains similar, but the particular pattern can be a little different. Differential Revision: https://developer.blender.org/D6177
-rw-r--r--intern/cycles/kernel/shaders/node_noise_texture.osl20
-rw-r--r--intern/cycles/kernel/shaders/node_wave_texture.osl2
-rw-r--r--intern/cycles/kernel/svm/svm_noisetex.h20
-rw-r--r--intern/cycles/kernel/svm/svm_wave.h2
-rw-r--r--source/blender/blenloader/intern/versioning_cycles.c54
-rw-r--r--source/blender/gpu/shaders/material/gpu_shader_material_tex_noise.glsl20
-rw-r--r--source/blender/gpu/shaders/material/gpu_shader_material_tex_wave.glsl2
7 files changed, 87 insertions, 33 deletions
diff --git a/intern/cycles/kernel/shaders/node_noise_texture.osl b/intern/cycles/kernel/shaders/node_noise_texture.osl
index e3da2d16371..6cff1cdab2c 100644
--- a/intern/cycles/kernel/shaders/node_noise_texture.osl
+++ b/intern/cycles/kernel/shaders/node_noise_texture.osl
@@ -59,7 +59,7 @@ float noise_texture(float co, float detail, float distortion, output color Color
{
float p = co;
if (distortion != 0.0) {
- p += safe_noise(p + random_float_offset(0.0)) * distortion;
+ p += safe_snoise(p + random_float_offset(0.0)) * distortion;
}
float value = fractal_noise(p, detail);
@@ -73,8 +73,8 @@ float noise_texture(vector2 co, float detail, float distortion, output color Col
{
vector2 p = co;
if (distortion != 0.0) {
- p += vector2(safe_noise(p + random_vector2_offset(0.0)) * distortion,
- safe_noise(p + random_vector2_offset(1.0)) * distortion);
+ p += vector2(safe_snoise(p + random_vector2_offset(0.0)) * distortion,
+ safe_snoise(p + random_vector2_offset(1.0)) * distortion);
}
float value = fractal_noise(p, detail);
@@ -88,9 +88,9 @@ float noise_texture(vector3 co, float detail, float distortion, output color Col
{
vector3 p = co;
if (distortion != 0.0) {
- p += vector3(safe_noise(p + random_vector3_offset(0.0)) * distortion,
- safe_noise(p + random_vector3_offset(1.0)) * distortion,
- safe_noise(p + random_vector3_offset(2.0)) * distortion);
+ p += vector3(safe_snoise(p + random_vector3_offset(0.0)) * distortion,
+ safe_snoise(p + random_vector3_offset(1.0)) * distortion,
+ safe_snoise(p + random_vector3_offset(2.0)) * distortion);
}
float value = fractal_noise(p, detail);
@@ -104,10 +104,10 @@ float noise_texture(vector4 co, float detail, float distortion, output color Col
{
vector4 p = co;
if (distortion != 0.0) {
- p += vector4(safe_noise(p + random_vector4_offset(0.0)) * distortion,
- safe_noise(p + random_vector4_offset(1.0)) * distortion,
- safe_noise(p + random_vector4_offset(2.0)) * distortion,
- safe_noise(p + random_vector4_offset(3.0)) * distortion);
+ p += vector4(safe_snoise(p + random_vector4_offset(0.0)) * distortion,
+ safe_snoise(p + random_vector4_offset(1.0)) * distortion,
+ safe_snoise(p + random_vector4_offset(2.0)) * distortion,
+ safe_snoise(p + random_vector4_offset(3.0)) * distortion);
}
float value = fractal_noise(p, detail);
diff --git a/intern/cycles/kernel/shaders/node_wave_texture.osl b/intern/cycles/kernel/shaders/node_wave_texture.osl
index 60591b79b33..a706c442368 100644
--- a/intern/cycles/kernel/shaders/node_wave_texture.osl
+++ b/intern/cycles/kernel/shaders/node_wave_texture.osl
@@ -31,7 +31,7 @@ float wave(point p, string type, string profile, float detail, float distortion,
}
if (distortion != 0.0) {
- n = n + (distortion * fractal_noise(p * dscale, detail));
+ n = n + (distortion * (fractal_noise(p * dscale, detail) * 2.0 - 1.0));
}
if (profile == "sine") {
diff --git a/intern/cycles/kernel/svm/svm_noisetex.h b/intern/cycles/kernel/svm/svm_noisetex.h
index c5a1e43a729..12884c6cb25 100644
--- a/intern/cycles/kernel/svm/svm_noisetex.h
+++ b/intern/cycles/kernel/svm/svm_noisetex.h
@@ -55,7 +55,7 @@ ccl_device void noise_texture_1d(
{
float p = co;
if (distortion != 0.0f) {
- p += noise_1d(p + random_float_offset(0.0f)) * distortion;
+ p += snoise_1d(p + random_float_offset(0.0f)) * distortion;
}
*value = fractal_noise_1d(p, detail);
@@ -71,8 +71,8 @@ ccl_device void noise_texture_2d(
{
float2 p = co;
if (distortion != 0.0f) {
- p += make_float2(noise_2d(p + random_float2_offset(0.0f)) * distortion,
- noise_2d(p + random_float2_offset(1.0f)) * distortion);
+ p += make_float2(snoise_2d(p + random_float2_offset(0.0f)) * distortion,
+ snoise_2d(p + random_float2_offset(1.0f)) * distortion);
}
*value = fractal_noise_2d(p, detail);
@@ -88,9 +88,9 @@ ccl_device void noise_texture_3d(
{
float3 p = co;
if (distortion != 0.0f) {
- p += make_float3(noise_3d(p + random_float3_offset(0.0f)) * distortion,
- noise_3d(p + random_float3_offset(1.0f)) * distortion,
- noise_3d(p + random_float3_offset(2.0f)) * distortion);
+ p += make_float3(snoise_3d(p + random_float3_offset(0.0f)) * distortion,
+ snoise_3d(p + random_float3_offset(1.0f)) * distortion,
+ snoise_3d(p + random_float3_offset(2.0f)) * distortion);
}
*value = fractal_noise_3d(p, detail);
@@ -106,10 +106,10 @@ ccl_device void noise_texture_4d(
{
float4 p = co;
if (distortion != 0.0f) {
- p += make_float4(noise_4d(p + random_float4_offset(0.0f)) * distortion,
- noise_4d(p + random_float4_offset(1.0f)) * distortion,
- noise_4d(p + random_float4_offset(2.0f)) * distortion,
- noise_4d(p + random_float4_offset(3.0f)) * distortion);
+ p += make_float4(snoise_4d(p + random_float4_offset(0.0f)) * distortion,
+ snoise_4d(p + random_float4_offset(1.0f)) * distortion,
+ snoise_4d(p + random_float4_offset(2.0f)) * distortion,
+ snoise_4d(p + random_float4_offset(3.0f)) * distortion);
}
*value = fractal_noise_4d(p, detail);
diff --git a/intern/cycles/kernel/svm/svm_wave.h b/intern/cycles/kernel/svm/svm_wave.h
index 402c1c87414..50c868c0f82 100644
--- a/intern/cycles/kernel/svm/svm_wave.h
+++ b/intern/cycles/kernel/svm/svm_wave.h
@@ -33,7 +33,7 @@ ccl_device_noinline_cpu float svm_wave(NodeWaveType type,
n = len(p) * 20.0f;
if (distortion != 0.0f)
- n += distortion * fractal_noise_3d(p * dscale, detail);
+ 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);
diff --git a/source/blender/blenloader/intern/versioning_cycles.c b/source/blender/blenloader/intern/versioning_cycles.c
index 0fe7f97e4ee..91faf724c08 100644
--- a/source/blender/blenloader/intern/versioning_cycles.c
+++ b/source/blender/blenloader/intern/versioning_cycles.c
@@ -1205,6 +1205,51 @@ static void update_voronoi_node_square_distance(bNodeTree *ntree)
}
}
+/* Noise and Wave Texture nodes: Restore previous Distortion range.
+ * In 2.81 we used noise() for distortion, now we use snoise() which has twice the range.
+ * To fix this we halve distortion value, directly or by adding multiply node for used sockets.
+ */
+static void update_noise_and_wave_distortion(bNodeTree *ntree)
+{
+ bool need_update = false;
+
+ for (bNode *node = ntree->nodes.first; node; node = node->next) {
+ if (node->type == SH_NODE_TEX_NOISE || node->type == SH_NODE_TEX_WAVE ) {
+
+ bNodeSocket *sockDistortion = nodeFindSocket(node, SOCK_IN, "Distortion");
+ float *distortion = cycles_node_socket_float_value(sockDistortion);
+
+ if (socket_is_used(sockDistortion)) {
+ bNode *distortionInputNode = sockDistortion->link->fromnode;
+ bNodeSocket *distortionInputSock = sockDistortion->link->fromsock;
+
+ bNode *mulNode = nodeAddStaticNode(NULL, ntree, SH_NODE_MATH);
+ mulNode->custom1 = NODE_MATH_MULTIPLY;
+ mulNode->locx = node->locx;
+ mulNode->locy = node->locy - 240.0f;
+ mulNode->flag |= NODE_HIDDEN;
+ bNodeSocket *mulSockA = BLI_findlink(&mulNode->inputs, 0);
+ bNodeSocket *mulSockB = BLI_findlink(&mulNode->inputs, 1);
+ *cycles_node_socket_float_value(mulSockB) = 0.5f;
+ bNodeSocket *mulSockOut = nodeFindSocket(mulNode, SOCK_OUT, "Value");
+
+ nodeRemLink(ntree, sockDistortion->link);
+ nodeAddLink(ntree, distortionInputNode, distortionInputSock, mulNode, mulSockA);
+ nodeAddLink(ntree, mulNode, mulSockOut, node, sockDistortion);
+
+ need_update = true;
+ }
+ else if (*distortion != 0.0f) {
+ *distortion = *distortion * 0.5f;
+ }
+ }
+ }
+
+ if (need_update) {
+ ntreeUpdateTree(NULL, ntree);
+ }
+}
+
void blo_do_versions_cycles(FileData *UNUSED(fd), Library *UNUSED(lib), Main *bmain)
{
/* Particle shape shared with Eevee. */
@@ -1435,4 +1480,13 @@ void do_versions_after_linking_cycles(Main *bmain)
}
FOREACH_NODETREE_END;
}
+
+ if (!MAIN_VERSION_ATLEAST(bmain, 282, 4)) {
+ FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
+ if (ntree->type == NTREE_SHADER) {
+ update_noise_and_wave_distortion(ntree);
+ }
+ }
+ FOREACH_NODETREE_END;
+ }
}
diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_tex_noise.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_tex_noise.glsl
index e56b4a1d135..6aeb23b1f99 100644
--- a/source/blender/gpu/shaders/material/gpu_shader_material_tex_noise.glsl
+++ b/source/blender/gpu/shaders/material/gpu_shader_material_tex_noise.glsl
@@ -37,7 +37,7 @@ void node_noise_texture_1d(
{
float p = w * scale;
if (distortion != 0.0) {
- p += noise(p + random_float_offset(0.0)) * distortion;
+ p += snoise(p + random_float_offset(0.0)) * distortion;
}
value = fractal_noise(p, detail);
@@ -52,8 +52,8 @@ void node_noise_texture_2d(
{
vec2 p = co.xy * scale;
if (distortion != 0.0) {
- p += vec2(noise(p + random_vec2_offset(0.0)) * distortion,
- noise(p + random_vec2_offset(1.0)) * distortion);
+ p += vec2(snoise(p + random_vec2_offset(0.0)) * distortion,
+ snoise(p + random_vec2_offset(1.0)) * distortion);
}
value = fractal_noise(p, detail);
@@ -68,9 +68,9 @@ void node_noise_texture_3d(
{
vec3 p = co * scale;
if (distortion != 0.0) {
- p += vec3(noise(p + random_vec3_offset(0.0)) * distortion,
- noise(p + random_vec3_offset(1.0)) * distortion,
- noise(p + random_vec3_offset(2.0)) * distortion);
+ p += vec3(snoise(p + random_vec3_offset(0.0)) * distortion,
+ snoise(p + random_vec3_offset(1.0)) * distortion,
+ snoise(p + random_vec3_offset(2.0)) * distortion);
}
value = fractal_noise(p, detail);
@@ -85,10 +85,10 @@ void node_noise_texture_4d(
{
vec4 p = vec4(co, w) * scale;
if (distortion != 0.0) {
- p += vec4(noise(p + random_vec4_offset(0.0)) * distortion,
- noise(p + random_vec4_offset(1.0)) * distortion,
- noise(p + random_vec4_offset(2.0)) * distortion,
- noise(p + random_vec4_offset(3.0)) * distortion);
+ p += vec4(snoise(p + random_vec4_offset(0.0)) * distortion,
+ snoise(p + random_vec4_offset(1.0)) * distortion,
+ snoise(p + random_vec4_offset(2.0)) * distortion,
+ snoise(p + random_vec4_offset(3.0)) * distortion);
}
value = fractal_noise(p, detail);
diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_tex_wave.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_tex_wave.glsl
index fa79e3dc310..957aa606a79 100644
--- a/source/blender/gpu/shaders/material/gpu_shader_material_tex_wave.glsl
+++ b/source/blender/gpu/shaders/material/gpu_shader_material_tex_wave.glsl
@@ -11,7 +11,7 @@ float calc_wave(
}
if (distortion != 0.0) {
- n += distortion * fractal_noise(p * detail_scale, detail);
+ n += distortion * (fractal_noise(p * detail_scale, detail) * 2.0 - 1.0);
}
if (wave_profile == 0) { /* profile sin */