From 1458ecd84f7f0777e8b12be7d6f52b6e31f21ff1 Mon Sep 17 00:00:00 2001 From: Onek8 Date: Tue, 13 May 2025 20:49:49 +0000 Subject: [PATCH 1/3] Update leenkx/blender/lnx/material/cycles_functions.py --- .../blender/lnx/material/cycles_functions.py | 58 ++++++++++++++----- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/leenkx/blender/lnx/material/cycles_functions.py b/leenkx/blender/lnx/material/cycles_functions.py index 1bd3e34..f0a8419 100644 --- a/leenkx/blender/lnx/material/cycles_functions.py +++ b/leenkx/blender/lnx/material/cycles_functions.py @@ -170,32 +170,64 @@ vec3 random3(const vec3 c) { r.y = fract(512.0 * j); return r - 0.5; } -float tex_musgrave_f(const vec3 p) { + +float noise_tex(const vec3 p) { const float F3 = 0.3333333; const float G3 = 0.1666667; + vec3 s = floor(p + dot(p, vec3(F3))); vec3 x = p - s + dot(s, vec3(G3)); vec3 e = step(vec3(0.0), x - x.yzx); - vec3 i1 = e*(1.0 - e.zxy); - vec3 i2 = 1.0 - e.zxy*(1.0 - e); + vec3 i1 = e * (1.0 - e.zxy); + vec3 i2 = 1.0 - e.zxy * (1.0 - e); + vec3 x1 = x - i1 + G3; - vec3 x2 = x - i2 + 2.0*G3; - vec3 x3 = x - 1.0 + 3.0*G3; - vec4 w, d; - w.x = dot(x, x); - w.y = dot(x1, x1); - w.z = dot(x2, x2); - w.w = dot(x3, x3); - w = max(0.6 - w, 0.0); + vec3 x2 = x - i2 + 2.0 * G3; + vec3 x3 = x - 1.0 + 3.0 * G3; + + vec4 w; + w.x = max(0.6 - dot(x, x), 0.0); + w.y = max(0.6 - dot(x1, x1), 0.0); + w.z = max(0.6 - dot(x2, x2), 0.0); + w.w = max(0.6 - dot(x3, x3), 0.0); + + w = w * w; + w = w * w; + + vec4 d; d.x = dot(random3(s), x); d.y = dot(random3(s + i1), x1); d.z = dot(random3(s + i2), x2); d.w = dot(random3(s + 1.0), x3); - w *= w; - w *= w; + d *= w; return clamp(dot(d, vec4(52.0)), 0.0, 1.0); } + +float tex_musgrave_f(const vec3 p, float detail, float distortion) { + // Apply distortion to the input coordinates smoothly with noise_tex + vec3 distorted_p = p + distortion * vec3( + noise_tex(p + vec3(5.2, 1.3, 7.1)), + noise_tex(p + vec3(1.7, 9.2, 3.8)), + noise_tex(p + vec3(8.3, 2.8, 4.5)) + ); + + float value = 0.0; + float amplitude = 1.0; + float frequency = 1.0; + + // Use 'detail' as number of octaves, clamped between 1 and 8 + int octaves = int(clamp(detail, 1.0, 8.0)); + + for (int i = 0; i < octaves; i++) { + value += amplitude * noise_tex(distorted_p * frequency); + frequency *= 2.0; + amplitude *= 0.5; + } + + return clamp(value, 0.0, 1.0); +} + """ # col: the incoming color From 953ad8391c186d0f2f5ae838a2c9e91f91346bec Mon Sep 17 00:00:00 2001 From: Onek8 Date: Tue, 13 May 2025 20:51:26 +0000 Subject: [PATCH 2/3] Update leenkx/blender/lnx/material/cycles_nodes/nodes_texture.py --- leenkx/blender/lnx/material/cycles_nodes/nodes_texture.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/leenkx/blender/lnx/material/cycles_nodes/nodes_texture.py b/leenkx/blender/lnx/material/cycles_nodes/nodes_texture.py index 7170309..4c2968e 100644 --- a/leenkx/blender/lnx/material/cycles_nodes/nodes_texture.py +++ b/leenkx/blender/lnx/material/cycles_nodes/nodes_texture.py @@ -254,10 +254,10 @@ if bpy.app.version < (4, 1, 0): co = 'bposition' scale = c.parse_value_input(node.inputs['Scale']) - # detail = c.parse_value_input(node.inputs[2]) - # distortion = c.parse_value_input(node.inputs[3]) - - res = f'tex_musgrave_f({co} * {scale} * 0.5)' + detail = c.parse_value_input(node.inputs[3]) + distortion = c.parse_value_input(node.inputs[4]) + + res = f'tex_musgrave_f({co} * {scale} * 0.5, {detail}, {distortion})' return res From 2b46d6ebca4cc2b69bbe598e88bcaa60646036f7 Mon Sep 17 00:00:00 2001 From: Onek8 Date: Tue, 13 May 2025 20:55:03 +0000 Subject: [PATCH 3/3] Update leenkx/blender/lnx/material/cycles_nodes/nodes_texture.py --- leenkx/blender/lnx/material/cycles_nodes/nodes_texture.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/leenkx/blender/lnx/material/cycles_nodes/nodes_texture.py b/leenkx/blender/lnx/material/cycles_nodes/nodes_texture.py index 4c2968e..e0615c1 100644 --- a/leenkx/blender/lnx/material/cycles_nodes/nodes_texture.py +++ b/leenkx/blender/lnx/material/cycles_nodes/nodes_texture.py @@ -278,11 +278,11 @@ def parse_tex_noise(node: bpy.types.ShaderNodeTexNoise, out_socket: bpy.types.No distortion = c.parse_value_input(node.inputs[5]) if bpy.app.version >= (4, 1, 0): if node.noise_type == "FBM": + state.curshader.add_function(c_functions.str_tex_musgrave) if out_socket == node.outputs[1]: - state.curshader.add_function(c_functions.str_tex_musgrave) - res = 'vec3(tex_musgrave_f({0} * {1}), tex_musgrave_f({0} * {1} + 120.0), tex_musgrave_f({0} * {1} + 168.0))'.format(co, scale, detail, distortion) + res = 'vec3(tex_musgrave_f({0} * {1}, {2}, {3}), tex_musgrave_f({0} * {1} + 120.0, {2}, {3}), tex_musgrave_f({0} * {1} + 168.0, {2}, {3}))'.format(co, scale, detail, distortion) else: - res = f'tex_musgrave_f({co} * {scale} * 1.0)' + res = f'tex_musgrave_f({co} * {scale} * 1.0, {detail}, {distortion})' else: if out_socket == node.outputs[1]: res = 'vec3(tex_noise({0} * {1},{2},{3}), tex_noise({0} * {1} + 120.0,{2},{3}), tex_noise({0} * {1} + 168.0,{2},{3}))'.format(co, scale, detail, distortion)