1
0
mirror of https://github.com/DigvijaysinhGohil/Godot-Shader-Lib.git synced 2025-09-19 11:56:01 +08:00

Simple noise node revamped

This commit is contained in:
Digvijaysinh Gohil
2024-03-19 18:22:06 +05:30
parent 600079e3e5
commit de8d5e472e
3 changed files with 36 additions and 27 deletions

View File

@ -17,7 +17,7 @@ func _get_return_icon_type() -> VisualShaderNode.PortType:
return PORT_TYPE_SCALAR
func _get_input_port_count() -> int:
return 2
return 3
func _get_input_port_name(port: int) -> String:
match port:
@ -25,7 +25,8 @@ func _get_input_port_name(port: int) -> String:
return "uv"
1:
return "scale"
return ""
_:
return "octaves"
func _get_input_port_type(port: int) -> VisualShaderNode.PortType:
match port:
@ -33,15 +34,20 @@ func _get_input_port_type(port: int) -> VisualShaderNode.PortType:
return PORT_TYPE_VECTOR_2D
1:
return PORT_TYPE_SCALAR
return PORT_TYPE_SCALAR
_:
return PORT_TYPE_SCALAR_INT
func _get_input_port_default_value(port: int) -> Variant:
match port:
1:
return 10.0
2:
return 6
_:
return null
func _get_default_input_port(type: PortType) -> int:
func _get_output_port_count() -> int:
return 1
@ -62,5 +68,6 @@ func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shad
uv = input_vars[0]
var scale: String = input_vars[1]
var octaves: String = input_vars[2]
return output_vars[0] + " = simple_noise(%s, %s);" % [uv, scale]
return output_vars[0] + " = simple_noise(%s, %s, %s);" % [uv, scale, octaves]

View File

@ -1,32 +1,33 @@
float noise_random_value(vec2 uv){
return fract(sin(dot(uv.xy, vec2(12.9898,78.233))) * 43758.5453123);
float simple_noise_random(vec2 point) {
return fract(sin(point.x * 100. + point.y * 654.125) * 55647.8745);
}
float value_noise(vec2 uv){
vec2 _floor = floor(uv);
vec2 _fraction = fract(uv);
_fraction = _fraction * _fraction * (3.0 - 2.0 * _fraction);
vec2 _corner = vec2(1.0, 0.0);
float value_noise(vec2 uv) {
vec2 grid_uv = fract(uv);
vec2 grid_id = floor(uv);
grid_uv = grid_uv * grid_uv * (3. - 2. * grid_uv);
float _c0 = noise_random_value(_floor + _corner.yy);
float _c1 = noise_random_value(_floor + _corner.xy);
float _c2 = noise_random_value(_floor + _corner.yx);
float _c3 = noise_random_value(_floor + _corner.xx);
float bottom_left = simple_noise_random(grid_id);
float bottom_right = simple_noise_random(grid_id + vec2(1, 0));
float bottom = mix(bottom_left, bottom_right, grid_uv.x);
vec2 _blur = smoothstep(0.0, 1.0, _fraction);
float mix_one = mix(_c0, _c1, _blur.x) + (_c2 - _c0) * _blur.y * (1.0 - _blur.x) + (_c3 - _c1) * _blur.x * _blur.y;
return mix_one;
float top_left = simple_noise_random(grid_id + vec2(0, 1));
float top_right = simple_noise_random(grid_id + vec2(1, 1));
float top = mix(top_left, top_right, grid_uv.x);
return mix(bottom, top, grid_uv.y);
}
float simple_noise(vec2 uv, float scale){
int octaves = 6;
float amplitude = 0.25;
float value = 0.0;
float simple_noise(vec2 uv, float scale, int octaves) {
octaves = clamp(octaves, 1, 6);
float noise = value_noise(uv * scale);
float amplitude = 1.;
for(int i = 0; i < octaves; i++) {
value += amplitude * value_noise(scale * uv);
amplitude *= 0.85;
scale *= 3.0;
for(int i = 1; i < octaves; i++) {
scale *= 2.;
amplitude /= 2.;
noise += value_noise(uv * scale) * amplitude;
}
return value;
return noise / 2.;
}