mirror of
https://github.com/DigvijaysinhGohil/Godot-Shader-Lib.git
synced 2025-01-07 01:43:35 +08:00
Voronoi node added
This commit is contained in:
parent
cddef53ccf
commit
c03d81fcfd
72
addons/ShaderLib/Procedural/Noise/Voronoi.gd
Normal file
72
addons/ShaderLib/Procedural/Noise/Voronoi.gd
Normal file
@ -0,0 +1,72 @@
|
||||
@tool
|
||||
class_name VisualShaderNodeProceduralVoronoi extends VisualShaderNodeCustom
|
||||
|
||||
func _init() -> void:
|
||||
_set_input_port_default_value(1, 5.0)
|
||||
_set_input_port_default_value(2, 2.0)
|
||||
|
||||
output_port_for_preview = 0
|
||||
|
||||
func _get_name() -> String:
|
||||
return "Voronoi"
|
||||
|
||||
func _get_category() -> String:
|
||||
return "Procedural/Noise"
|
||||
|
||||
func _get_description() -> String:
|
||||
return "Generates a Voronoi or Worley noise based on input UV."
|
||||
|
||||
func _get_return_icon_type() -> VisualShaderNode.PortType:
|
||||
return PORT_TYPE_SCALAR
|
||||
|
||||
func _get_input_port_count() -> int:
|
||||
return 3
|
||||
|
||||
func _get_input_port_name(port: int) -> String:
|
||||
match port:
|
||||
0:
|
||||
return "uv"
|
||||
1:
|
||||
return "cell density"
|
||||
2:
|
||||
return "angle offset"
|
||||
return ""
|
||||
|
||||
func _get_input_port_type(port: int) -> VisualShaderNode.PortType:
|
||||
match port:
|
||||
0:
|
||||
return PORT_TYPE_VECTOR_2D
|
||||
1, 2:
|
||||
return PORT_TYPE_SCALAR
|
||||
return PORT_TYPE_SCALAR
|
||||
|
||||
func _get_output_port_count() -> int:
|
||||
return 2
|
||||
|
||||
func _get_output_port_name(port: int) -> String:
|
||||
match port:
|
||||
0:
|
||||
return "output"
|
||||
1:
|
||||
return "cells"
|
||||
return ""
|
||||
|
||||
func _get_output_port_type(port: int) -> VisualShaderNode.PortType:
|
||||
return PORT_TYPE_SCALAR
|
||||
|
||||
func _get_global_code(mode: Shader.Mode) -> String:
|
||||
var code: String = preload("Voronoi.gdshaderinc").code
|
||||
return code
|
||||
|
||||
func _get_code(input_vars: Array[String], output_vars: Array[String], mode: Shader.Mode, type: VisualShader.Type) -> String:
|
||||
var uv: String = "UV"
|
||||
|
||||
if input_vars[0]:
|
||||
uv = input_vars[0]
|
||||
|
||||
var cell_density: String = input_vars[1]
|
||||
var angle_offset: String = input_vars[2]
|
||||
var output: String = output_vars[0]
|
||||
var cells: String = output_vars[1]
|
||||
|
||||
return "voronoi_noise(%s,%s, %s, %s, %s);" % [uv, cell_density, angle_offset, output, cells]
|
25
addons/ShaderLib/Procedural/Noise/Voronoi.gdshaderinc
Normal file
25
addons/ShaderLib/Procedural/Noise/Voronoi.gdshaderinc
Normal file
@ -0,0 +1,25 @@
|
||||
void voronoi_noise(vec2 uv, float cell_density, float angle_offset, out float output, out float cells){
|
||||
vec2 g = floor(uv * cell_density);
|
||||
vec2 f = fract(uv * cell_density);
|
||||
float t = 8.0;
|
||||
vec3 res = vec3(8.0, 0.0, 0.0);
|
||||
mat2 m = mat2(vec2(15.27, 47.63), vec2(99.41, 89.98));
|
||||
|
||||
for(int y=-1; y<=1; y++)
|
||||
{
|
||||
for(int x=-1; x<=1; x++)
|
||||
{
|
||||
vec2 lattice = vec2(float(x), float(y));
|
||||
vec2 new_uv = lattice + g;
|
||||
new_uv = fract(sin(new_uv * m) * 46839.32);
|
||||
vec2 offset = vec2(sin(new_uv.y * angle_offset) * 0.5 + 0.5, cos(new_uv.x * angle_offset) * 0.5 + 0.5);
|
||||
float d = distance(lattice + offset, f);
|
||||
if(d < res.x)
|
||||
{
|
||||
res = vec3(d, offset.x, offset.y);
|
||||
output = res.x;
|
||||
cells = res.y;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user