1
0
mirror of https://github.com/DigvijaysinhGohil/Godot-Shader-Lib.git synced 2025-01-10 03:08:46 +08:00

43 lines
1.2 KiB
Plaintext
Raw Normal View History

2024-03-19 19:38:22 +05:30
#include "res://addons/ShaderLib/Maths/Vector/Distance/Manhattan2D.gdshaderinc"
#include "res://addons/ShaderLib/Maths/Vector/Distance/Chebyshev2D.gdshaderinc"
2024-03-19 13:03:56 +05:30
vec2 voronoi_random_vector(vec2 p) {
mat2 matrix = mat2(vec2(15.27, 47.63), vec2(99.41, 89.98));
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){
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);
2024-03-19 18:40:25 +05:30
vec2 p = offset + vec2(sin(n.x + angle_offset) * .5 + .5, cos(n.y + angle_offset) * .5 + .5);
2024-03-19 13:03:56 +05:30
float d = min_dist;
switch(distance_index){
case 1:
2024-03-19 19:38:22 +05:30
d = manhattan_distance_2d(grid_uv, p);
2024-03-19 13:03:56 +05:30
break;
case 2:
2024-03-19 19:38:22 +05:30
d = chebyshev_distance_2d(grid_uv, p, chebyshev_power);
2024-03-19 13:03:56 +05:30
break;
default:
2024-03-19 19:38:22 +05:30
d = distance(grid_uv, p);
2024-03-19 13:03:56 +05:30
break;
}
if(d < min_dist) {
min_dist = d;
cell_id = voronoi_random_vector(grid_id + offset);
2023-10-12 19:46:55 +05:30
}
}
}
2024-03-19 13:03:56 +05:30
output = min_dist;
cells = cell_id.y;
2023-10-12 19:46:55 +05:30
}