1
0
mirror of https://github.com/DigvijaysinhGohil/Godot-Shader-Lib.git synced 2025-01-07 01:43:35 +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,19 +1,28 @@
vec2 gradient_noise_dir(vec2 p){
p = vec2(mod(p.x,289.0), mod(p.y, 289.0));
float x = (34.0 * p.x + 1.0) * mod(p.x, 289.0) + p.y;
x = (34.0 * x + 1.0) * mod(x, 289.0);
x = fract(x / 41.0) * 2.0 - 1.0;
return normalize(vec2(x - floor(x + 0.5), abs(x) - 0.5));
vec2 gradient_modulo(vec2 divident, vec2 divisor){
vec2 _positive_divident = mod(divident, divisor) + divisor;
return mod(_positive_divident, divisor);
}
float gradient_noise(vec2 uv, float scale){
vec2 p = uv * scale;
vec2 ip = floor(p);
vec2 fp = fract(p);
float d00 = dot(gradient_noise_dir(ip), fp);
float d01 = dot(gradient_noise_dir(ip + vec2(0, 1)), fp - vec2(0, 1));
float d10 = dot(gradient_noise_dir(ip + vec2(1, 0)), fp - vec2(1, 0));
float d11 = dot(gradient_noise_dir(ip + vec2(1, 1)), fp - vec2(1, 1));
fp = fp * fp * fp * (fp * (fp * 6.0 - 15.0) + 10.0);
return mix(mix(d00, d01, fp.y), mix(d10, d11, fp.y), fp.x) + 0.5;
vec2 gradient_random(vec2 uv){
uv = vec2(dot(uv, vec2(127.1,311.7)), dot(uv, vec2(269.5,183.3)));
return -1.0 + 2.0 * fract(sin(uv) * 43758.5453123);
}
float gradient_noise(vec2 uv, float scale) {
uv = uv * float(scale);
vec2 _period = vec2(30.0, 60.0);
vec2 _cells_minimum = floor(uv);
vec2 _cells_maximum = ceil(uv);
vec2 _uv_fract = fract(uv);
_cells_minimum = gradient_modulo(_cells_minimum, _period);
_cells_maximum = gradient_modulo(_cells_maximum, _period);
vec2 _blur = smoothstep(0.0, 1.0, _uv_fract);
vec2 _lowerLeftDirection = gradient_random(vec2(_cells_minimum.x, _cells_minimum.y));
vec2 _lowerRightDirection = gradient_random(vec2(_cells_maximum.x, _cells_minimum.y));
vec2 _upperLeftDirection = gradient_random(vec2(_cells_minimum.x, _cells_maximum.y));
vec2 _upperRightDirection = gradient_random(vec2(_cells_maximum.x, _cells_maximum.y));
vec2 _fraction = fract(uv);
float _mix_one = mix(dot(_lowerLeftDirection, _fraction - vec2(0, 0)), dot(_lowerRightDirection, _fraction - vec2(1, 0)), _blur.x);
float _mix_two = mix(dot(_upperLeftDirection, _fraction - vec2(0, 1)), dot(_upperRightDirection, _fraction - vec2(1, 1)), _blur.x);
return mix(_mix_one, _mix_two, _blur.y) * 0.8 + 0.5;
}

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;
}