mirror of
https://github.com/DigvijaysinhGohil/Godot-Shader-Lib.git
synced 2025-01-07 01:43:35 +08:00
RoundedRectangle node added
This commit is contained in:
parent
7630aeb6ed
commit
cc67c7ebfd
69
addons/ShaderLib/Procedural/Shapes/RoundedRectangle.gd
Normal file
69
addons/ShaderLib/Procedural/Shapes/RoundedRectangle.gd
Normal file
@ -0,0 +1,69 @@
|
||||
@tool
|
||||
class_name VisualShaderNodeProceduralRoundedRectangle extends VisualShaderNodeCustom
|
||||
|
||||
func _init() -> void:
|
||||
_set_input_port_default_value(1, 0.5)
|
||||
_set_input_port_default_value(2, 0.5)
|
||||
_set_input_port_default_value(3, 1.0)
|
||||
|
||||
output_port_for_preview = 0
|
||||
|
||||
func _get_name() -> String:
|
||||
return "RoundedRectangle"
|
||||
|
||||
func _get_category() -> String:
|
||||
return "Procedural/Shapes"
|
||||
|
||||
func _get_description() -> String:
|
||||
return "Generates a rounded rectangle shape based on input UV at the size specified by inputs width and height. The radius of each corner is defined by input radius."
|
||||
|
||||
func _get_return_icon_type() -> VisualShaderNode.PortType:
|
||||
return PORT_TYPE_SCALAR
|
||||
|
||||
func _get_input_port_count() -> int:
|
||||
return 4
|
||||
|
||||
func _get_input_port_name(port: int) -> String:
|
||||
match port:
|
||||
0:
|
||||
return "uv"
|
||||
1:
|
||||
return "width"
|
||||
2:
|
||||
return "height"
|
||||
3:
|
||||
return "radius"
|
||||
return ""
|
||||
|
||||
func _get_input_port_type(port: int) -> VisualShaderNode.PortType:
|
||||
match port:
|
||||
0:
|
||||
return PORT_TYPE_VECTOR_2D
|
||||
1, 2, 3:
|
||||
return PORT_TYPE_SCALAR
|
||||
return PORT_TYPE_SCALAR
|
||||
|
||||
func _get_output_port_count() -> int:
|
||||
return 1
|
||||
|
||||
func _get_output_port_name(port: int) -> String:
|
||||
return "output"
|
||||
|
||||
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("RoundedRectangle.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 width: String = input_vars[1]
|
||||
var height: String = input_vars[2]
|
||||
var radius: String = input_vars[3]
|
||||
|
||||
return output_vars[0] + " = rounded_rectangle_shape(%s, %s, %s, %s);" % [uv, width, height, radius]
|
@ -0,0 +1,7 @@
|
||||
float rounded_rectangle_shape(vec2 uv, float width, float height, float radius){
|
||||
radius /= 10.0;
|
||||
radius = max(min(min(abs(radius * 2.0), abs(width)), abs(height)), 1e-5);
|
||||
uv = abs(uv * 2.0 - 1.0) - vec2(width, height) + radius;
|
||||
float _distance = length(max(vec2(0.0), uv)) / radius;
|
||||
return clamp((1.0 - _distance) / fwidth(_distance), 0.0, 1.0);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user