1
0
mirror of https://github.com/DigvijaysinhGohil/Godot-Shader-Lib.git synced 2025-09-20 12:25:59 +08:00

SimpleNoise node added

^ GradientNoise algorithm improved
This commit is contained in:
Digvijaysinh Gohil
2023-10-13 15:44:54 +05:30
parent 0fa8d08c5b
commit 17b7bd7f48
2 changed files with 46 additions and 56 deletions

View File

@ -1,47 +1,28 @@
float noise_random_value(vec2 uv){
return fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453);
return fract(sin(dot(uv.xy, vec2(12.9898,78.233))) * 43758.5453123);
}
float noise_interpolate(float a, float b, float t){
return (1.0 - t) * a + (t * b);
}
float value_noise(vec2 uv){
vec2 i = floor(uv);
vec2 f = fract(uv);
f = f * f * (3.0 - 2.0 * f);
uv = abs(fract(uv) - 0.5);
vec2 c0 = i + vec2(0.0, 0.0);
vec2 c1 = i + vec2(1.0, 0.0);
vec2 c2 = i + vec2(0.0, 1.0);
vec2 c3 = i + vec2(1.0, 1.0);
float r0 = noise_random_value(c0);
float r1 = noise_random_value(c1);
float r2 = noise_random_value(c2);
float r3 = noise_random_value(c3);
float bottom_of_grid = noise_interpolate(r0, r1, f.x);
float top_of_grid = noise_interpolate(r2, r3, f.x);
float t = noise_interpolate(bottom_of_grid, top_of_grid, f.y);
return t;
float value_noise(vec2 uv) {
vec2 uv_index = floor(uv);
vec2 uv_fract = fract(uv);
float a = noise_random_value(uv_index);
float b = noise_random_value(uv_index + vec2(1.0, 0.0));
float c = noise_random_value(uv_index + vec2(0.0, 1.0));
float d = noise_random_value(uv_index + vec2(1.0, 1.0));
vec2 blur = smoothstep(0.0, 1.0, uv_fract);
return mix(a, b, blur.x) + ((c - a) * blur.y * (1.0 - blur.x)) + ((d - b) * blur.x * blur.y);
}
float simple_noise(vec2 uv, float scale){
float t = 0.0;
float freq = pow(2.0, float(0));
float amp = pow(0.5, float(3-0));
t += value_noise(vec2(uv.x * scale / freq, uv.y * scale / freq)) * amp;
freq = pow(2.0, float(1));
amp = pow(0.5, float(3-1));
t += value_noise(vec2(uv.x * scale / freq, uv.y * scale / freq)) * amp;
freq = pow(2.0, float(2));
amp = pow(0.5, float(3-2));
t += value_noise(vec2(uv.x * scale / freq, uv.y * scale / freq)) * amp;
return t;
int octaves = 6;
float amplitude = 0.5;
float frequency = scale;
float value = 0.0;
for(int i = 0; i < octaves; i++) {
value += amplitude * value_noise(frequency * uv);
amplitude *= 0.5;
frequency *= 2.0;
}
return value;
}