Merge pull request #1617 from Simonrazer/master

Upgrade Wave Texture node + Fix noise texture Value output
This commit is contained in:
Lubos Lenco 2020-03-16 19:48:06 +01:00 committed by GitHub
commit 3991ec7d47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 5 deletions

View file

@ -575,7 +575,18 @@ def parse_vector(node, socket):
else:
co = 'bposition'
scale = parse_value_input(node.inputs[1])
res = 'vec3(tex_wave_f({0} * {1}))'.format(co, scale)
distortion = parse_value_input(node.inputs[2])
detail = parse_value_input(node.inputs[3])
detail_scale = parse_value_input(node.inputs[4])
if node.wave_profile == 'SIN':
wave_profile = 0
else:
wave_profile = 1
if node.wave_type == 'BANDS':
wave_type = 0
else:
wave_type = 1
res = 'vec3(tex_wave_f({0} * {1},{2},{3},{4},{5},{6}))'.format(co, scale, wave_type, wave_profile, distortion, detail, detail_scale)
if sample_bump:
write_bump(node, res)
return res
@ -1263,7 +1274,7 @@ def parse_value(node, socket):
co = parse_vector_input(node.inputs[0])
else:
co = 'bposition'
scale = parse_value_input(node.inputs[1])
scale = parse_value_input(node.inputs[2])
# detail = parse_value_input(node.inputs[2])
# distortion = parse_value_input(node.inputs[3])
res = 'tex_noise({0} * {1})'.format(co, scale)
@ -1299,7 +1310,18 @@ def parse_value(node, socket):
else:
co = 'bposition'
scale = parse_value_input(node.inputs[1])
res = 'tex_wave_f({0} * {1})'.format(co, scale)
distortion = parse_value_input(node.inputs[2])
detail = parse_value_input(node.inputs[3])
detail_scale = parse_value_input(node.inputs[4])
if node.wave_profile == 'SIN':
wave_profile = 0
else:
wave_profile = 1
if node.wave_type == 'BANDS':
wave_type = 0
else:
wave_type = 1
res = 'tex_wave_f({0} * {1},{2},{3},{4},{5},{6})'.format(co, scale, wave_type, wave_profile, distortion, detail, detail_scale)
if sample_bump:
write_bump(node, res)
return res

View file

@ -189,8 +189,70 @@ float tex_brick_f(vec3 p) {
"""
str_tex_wave = """
float tex_wave_f(const vec3 p) {
return 1.0 - sin((p.x + p.y) * 10.0);
// <https://www.shadertoy.com/view/4dS3Wd>
// By Morgan McGuire @morgan3d, http://graphicscodex.com
float hash(vec2 p) { return fract(1e4 * sin(17.0 * p.x + p.y * 0.1) * (0.1 + abs(sin(p.y * 13.0 + p.x)))); }
float noise(vec2 x) {
vec2 i = floor(x);
vec2 f = fract(x);
// Four corners in 2D of a tile
float a = hash(i);
float b = hash(i + vec2(1.0, 0.0));
float c = hash(i + vec2(0.0, 1.0));
float d = hash(i + vec2(1.0, 1.0));
// Simple 2D lerp using smoothstep envelope between the values.
// return vec3(mix(mix(a, b, smoothstep(0.0, 1.0, f.x)),
// mix(c, d, smoothstep(0.0, 1.0, f.x)),
// smoothstep(0.0, 1.0, f.y)));
// Same code, with the clamps in smoothstep and common subexpressions
// optimized away.
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y;
}
// Shader-code from adapted from Blender
// https://github.com/sobotka/blender/blob/master/source/blender/gpu/shaders/material/gpu_shader_material_tex_wave.glsl & /gpu_shader_material_fractal_noise.glsl
float fractal_noise(vec2 p, float octaves)
{
float fscale = 1.0;
float amp = 1.0;
float sum = 0.0;
octaves = clamp(octaves, 0.0, 16.0);
int n = int(octaves);
for (int i = 0; i <= n; i++) {
float t = noise(fscale * p);
sum += t * amp;
amp *= 0.5;
fscale *= 2.0;
}
float rmd = octaves - floor(octaves);
if (rmd != 0.0) {
float t = noise(fscale * p);
float sum2 = sum + t * amp;
sum *= float(pow(2, n)) / float(pow(2, n + 1) - 1.0);
sum2 *= float(pow(2, n + 1)) / float(pow(2, n + 2) - 1);
return (1.0 - rmd) * sum + rmd * sum2;
}
else {
sum *= float(pow(2, n)) / float(pow(2, n + 1) - 1);
return sum;
}
}
float tex_wave_f(const vec3 p, int type, int profile, float dist, float detail, float detail_scale) {
float n;
if(type == 0) n = (p.x + p.y) * 9.5;
else n = length(p.xy) * 13.0;
if(dist != 0.0) n += dist * fractal_noise(vec2(p.xy)*detail_scale,detail) * 2.0 - 1.0;
if(profile == 0) { return 0.5 + 0.5 * sin(n - 3.14159265359); }
else {
n /= 2.0 * 3.14159265359;
return n - floor(n);
}
}
"""