diff options
author | OmarSquircleArt <omar.squircleart@gmail.com> | 2019-09-04 18:54:32 +0300 |
---|---|---|
committer | OmarSquircleArt <omar.squircleart@gmail.com> | 2019-09-04 18:54:32 +0300 |
commit | 23564583a4988778b4c43496fd21818b286f6ba1 (patch) | |
tree | 3dc149c4e5c26ea2aac460ed582cb31def988470 /source/blender/gpu/shaders/material/gpu_shader_material_tex_noise.glsl | |
parent | 45d4c925799e94c6d442a9a9066af2d3305724e1 (diff) |
Shading: Extend Noise node to other dimenstions.
This patch extends perlin noise to operate in 1D, 2D, 3D, and 4D
space. The noise code has also been refactored to be more readable.
The Color output and distortion patterns changed, so this patch
breaks backward compatibility. This is due to the fact that we
now use random offsets as noise seeds, as opposed to swizzling
and constants offsets.
Reviewers: brecht, JacquesLucke
Differential Revision: https://developer.blender.org/D5560
Diffstat (limited to 'source/blender/gpu/shaders/material/gpu_shader_material_tex_noise.glsl')
-rw-r--r-- | source/blender/gpu/shaders/material/gpu_shader_material_tex_noise.glsl | 106 |
1 files changed, 93 insertions, 13 deletions
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 30e27da16e8..e56b4a1d135 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 @@ -1,19 +1,99 @@ -void node_tex_noise( - vec3 co, float scale, float detail, float distortion, out vec4 color, out float fac) +/* The following offset functions generate random offsets to be added to texture + * coordinates to act as a seed since the noise functions don't have seed values. + * A seed value is needed for generating distortion textures and color outputs. + * The offset's components are in the range [100, 200], not too high to cause + * bad precision and not to small to be noticeable. We use float seed because + * OSL only support float hashes. + */ + +float random_float_offset(float seed) +{ + return 100.0 + hash_float_to_float(seed) * 100.0; +} + +vec2 random_vec2_offset(float seed) +{ + return vec2(100.0 + hash_vec2_to_float(vec2(seed, 0.0)) * 100.0, + 100.0 + hash_vec2_to_float(vec2(seed, 1.0)) * 100.0); +} + +vec3 random_vec3_offset(float seed) +{ + return vec3(100.0 + hash_vec2_to_float(vec2(seed, 0.0)) * 100.0, + 100.0 + hash_vec2_to_float(vec2(seed, 1.0)) * 100.0, + 100.0 + hash_vec2_to_float(vec2(seed, 2.0)) * 100.0); +} + +vec4 random_vec4_offset(float seed) +{ + return vec4(100.0 + hash_vec2_to_float(vec2(seed, 0.0)) * 100.0, + 100.0 + hash_vec2_to_float(vec2(seed, 1.0)) * 100.0, + 100.0 + hash_vec2_to_float(vec2(seed, 2.0)) * 100.0, + 100.0 + hash_vec2_to_float(vec2(seed, 3.0)) * 100.0); +} + +void node_noise_texture_1d( + vec3 co, float w, float scale, float detail, float distortion, out float value, out vec4 color) +{ + float p = w * scale; + if (distortion != 0.0) { + p += noise(p + random_float_offset(0.0)) * distortion; + } + + value = fractal_noise(p, detail); + color = vec4(value, + fractal_noise(p + random_float_offset(1.0), detail), + fractal_noise(p + random_float_offset(2.0), detail), + 1.0); +} + +void node_noise_texture_2d( + vec3 co, float w, float scale, float detail, float distortion, out float value, out vec4 color) +{ + 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); + } + + value = fractal_noise(p, detail); + color = vec4(value, + fractal_noise(p + random_vec2_offset(2.0), detail), + fractal_noise(p + random_vec2_offset(3.0), detail), + 1.0); +} + +void node_noise_texture_3d( + vec3 co, float w, float scale, float detail, float distortion, out float value, out vec4 color) { vec3 p = co * scale; - int hard = 0; if (distortion != 0.0) { - vec3 r, offset = vec3(13.5, 13.5, 13.5); - r.x = noise(p + offset) * distortion; - r.y = noise(p) * distortion; - r.z = noise(p - offset) * distortion; - p += r; + 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); + } + + value = fractal_noise(p, detail); + color = vec4(value, + fractal_noise(p + random_vec3_offset(3.0), detail), + fractal_noise(p + random_vec3_offset(4.0), detail), + 1.0); +} + +void node_noise_texture_4d( + vec3 co, float w, float scale, float detail, float distortion, out float value, out vec4 color) +{ + 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); } - fac = noise_turbulence(p, detail, hard); - color = vec4(fac, - noise_turbulence(vec3(p.y, p.x, p.z), detail, hard), - noise_turbulence(vec3(p.y, p.z, p.x), detail, hard), - 1); + value = fractal_noise(p, detail); + color = vec4(value, + fractal_noise(p + random_vec4_offset(4.0), detail), + fractal_noise(p + random_vec4_offset(5.0), detail), + 1.0); } |