1
0
mirror of https://github.com/DigvijaysinhGohil/Godot-Shader-Lib.git synced 2025-09-19 03:55:59 +08:00

Voronoi noise functions refactored

This commit is contained in:
Digvijaysinh Gohil
2024-09-29 23:42:44 +05:30
parent a98305fa5f
commit afae2c766e
3 changed files with 63 additions and 26 deletions

View File

@ -1,9 +1,6 @@
@tool
class_name VisualShaderNodeProceduralVoronoi extends ShaderLib
func _init() -> void:
output_port_for_preview = 0
func _get_name() -> String:
return "Voronoi"
@ -63,7 +60,7 @@ func _get_input_port_default_value(port: int) -> Variant:
2:
return 2.0
3:
return 2.0
return 5.0
_:
return null
_:
@ -113,13 +110,15 @@ func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shad
var cell_density: String = input_vars[1]
var angle_offset: String = input_vars[2]
var chebyshev_power: String = "0."
if distance_index == 2:
if input_vars[3]:
chebyshev_power = input_vars[3]
var output: String = output_vars[0]
var cells: String = output_vars[1]
return "voronoi_noise(%s, %s, %s, %s, %s, %s, %s);" % [uv, cell_density, angle_offset, distance_index, chebyshev_power, output, cells]
match distance_index:
1:
return "%s = voronoi_noise_manhattan(%s, %s, %s, %s);" % [output, uv, cell_density, angle_offset, cells]
2:
var chebyshev_power: String = input_vars[3]
return "%s = voronoi_noise_chebyshev(%s, %s, %s, %s, %s);" % [output, uv, cell_density, angle_offset, chebyshev_power, cells]
_:
return "%s = voronoi_noise_euclidean(%s, %s, %s, %s);" % [output, uv, cell_density, angle_offset, cells]

View File

@ -130,7 +130,7 @@ vec2 voronoi_random_vector(vec2 p) {
return fract(sin(p * matrix) * 46839.32);
}
void voronoi_noise(vec2 uv, float cell_density, float angle_offset, int distance_index, float chebyshev_power, out float output, out float cells){
float voronoi_noise_euclidean(vec2 uv, float cell_density, float angle_offset, out float cells){
vec2 grid_uv = fract(uv * cell_density);
vec2 grid_id = floor(uv * cell_density);
vec2 cell_id = vec2(0);
@ -142,18 +142,31 @@ void voronoi_noise(vec2 uv, float cell_density, float angle_offset, int distance
vec2 n = voronoi_random_vector(grid_id + offset);
vec2 p = offset + vec2(sin(n.x + angle_offset) * .5 + .5, cos(n.y + angle_offset) * .5 + .5);
float d = min_dist;
switch(distance_index){
case 1:
d = manhattan_distance_2d(grid_uv, p);
break;
case 2:
d = chebyshev_distance_2d(grid_uv, p, chebyshev_power);
break;
default:
d = distance(grid_uv, p);
break;
d = distance(grid_uv, p);
if(d < min_dist) {
min_dist = d;
cell_id = voronoi_random_vector(grid_id + offset);
}
}
}
cells = cell_id.y;
return min_dist;
}
float voronoi_noise_manhattan(vec2 uv, float cell_density, float angle_offset, out float cells){
vec2 grid_uv = fract(uv * cell_density);
vec2 grid_id = floor(uv * cell_density);
vec2 cell_id = vec2(0);
float min_dist = 100.;
for(float y = -1.; y <= 1.; y++) {
for(float x = -1.; x <= 1.; x++) {
vec2 offset = vec2(x, y);
vec2 n = voronoi_random_vector(grid_id + offset);
vec2 p = offset + vec2(sin(n.x + angle_offset) * .5 + .5, cos(n.y + angle_offset) * .5 + .5);
float d = min_dist;
d = manhattan_distance_2d(grid_uv, p);
if(d < min_dist) {
min_dist = d;
@ -162,8 +175,33 @@ void voronoi_noise(vec2 uv, float cell_density, float angle_offset, int distance
}
}
output = min_dist;
cells = cell_id.y;
return min_dist;
}
float voronoi_noise_chebyshev(vec2 uv, float cell_density, float angle_offset, float chebyshev_power, out float cells){
vec2 grid_uv = fract(uv * cell_density);
vec2 grid_id = floor(uv * cell_density);
vec2 cell_id = vec2(0);
float min_dist = 100.;
for(float y = -1.; y <= 1.; y++) {
for(float x = -1.; x <= 1.; x++) {
vec2 offset = vec2(x, y);
vec2 n = voronoi_random_vector(grid_id + offset);
vec2 p = offset + vec2(sin(n.x + angle_offset) * .5 + .5, cos(n.y + angle_offset) * .5 + .5);
float d = min_dist;
d = chebyshev_distance_2d(grid_uv, p, chebyshev_power);
if(d < min_dist) {
min_dist = d;
cell_id = voronoi_random_vector(grid_id + offset);
}
}
}
cells = cell_id.y;
return min_dist;
}
float ellipse_shape(vec2 uv, float width, float height) {

View File

@ -25,7 +25,9 @@ Generates a Voronoi or Worley noise based on input UV. Voronoi noise is generate
<br>`res://addons/ShaderLib/Procedural/Procedural.gdshaderinc`
**Method signature**
<br>`void voronoi_noise(vec2 uv, float cell_density, float angle_offset, int distance_index, float chebyshev_power, out float output, out float cells)`
<br>`float voronoi_noise_euclidean(vec2 uv, float cell_density, float angle_offset, out float cells)`
<br>`float voronoi_noise_manhattan(vec2 uv, float cell_density, float angle_offset, out float cells)`
<br>`float voronoi_noise_chebyshev(vec2 uv, float cell_density, float angle_offset, float chebyshev_power, out float cells)`
**Parameters**
|Name|Type|Description|
@ -33,8 +35,6 @@ Generates a Voronoi or Worley noise based on input UV. Voronoi noise is generate
|uv|vec2|Input UV value|
|cell_density|float|Density of generated cells|
|angle_offset|float|Offset values for points|
|distance_index|int|Distance matrix to use for Voronoi, 0 = Euclidean, 1 = Manhattan, 2 = Chebyshev|
|chebyshev_power|float|Power for Chebyshev distance|
|output|out float|Output noise value|
|cells|out float|Output raw cell data|
___