1
0
mirror of https://github.com/DigvijaysinhGohil/Godot-Shader-Lib.git synced 2025-09-20 04:15:58 +08:00

RotateUV node added

^ Code refactored for various nodes
This commit is contained in:
Digvijaysinh Gohil
2023-10-12 18:31:51 +05:30
parent 53f176db68
commit aef7f401af
4 changed files with 92 additions and 1 deletions

View File

@ -8,6 +8,8 @@ func _init() -> void:
set_input_port_default_value(4, 0)
set_input_port_default_value(5, 0.1)
set_output_port_for_preview(0)
func _get_name() -> String:
return "Flipbook"

View File

@ -6,6 +6,8 @@ func _init() -> void:
_set_input_port_default_value(2, 10.0)
_set_input_port_default_value(3, Vector2(0.0, 0.0))
set_output_port_for_preview(0)
func _get_name() -> String:
return "RadialShear"

View File

@ -0,0 +1,71 @@
@tool
class_name VisualShaderNodeUVRotate extends VisualShaderNodeCustom
func _init() -> void:
_set_input_port_default_value(1, Vector2(0.5, 0.5))
_set_input_port_default_value(2, 0.0)
_set_input_port_default_value(3, false)
set_output_port_for_preview(0)
func _get_name() -> String:
return "Rotate"
func _get_category() -> String:
return "UV"
func _get_description() -> String:
return "Rotates value of input UV around a reference point defined by input center by the amount of input rotation."
func _get_return_icon_type() -> VisualShaderNode.PortType:
return PORT_TYPE_VECTOR_2D
func _get_input_port_count() -> int:
return 4
func _get_input_port_name(port: int) -> String:
match port:
0:
return "uv"
1:
return "center"
2:
return "rotation"
3:
return "use degrees"
return ""
func _get_input_port_type(port: int) -> VisualShaderNode.PortType:
match port:
0, 1:
return PORT_TYPE_VECTOR_2D
2:
return PORT_TYPE_SCALAR
3:
return PORT_TYPE_BOOLEAN
return PORT_TYPE_SCALAR
func _get_output_port_count() -> int:
return 1
func _get_output_port_name(port: int) -> String:
return "uv"
func _get_output_port_type(port: int) -> VisualShaderNode.PortType:
return PORT_TYPE_VECTOR_2D
func _get_global_code(mode: Shader.Mode) -> String:
var code: String = preload("RotateUV.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 center: String = input_vars[1]
var rotation: String = input_vars[2]
var use_degrees: String = input_vars[3]
return output_vars[0] + " = rotate_uv(%s, %s, %s, %s);" % [uv, center, rotation, use_degrees]

View File

@ -0,0 +1,16 @@
vec2 rotate_uv(vec2 uv, vec2 center, float rotation, bool use_degrees){
if(use_degrees){
rotation = rotation * (3.1415926/180.0);
}
vec2 _uv = uv;
_uv -= center;
float _s = sin(rotation);
float _c = cos(rotation);
mat2 _rotation_matrix = mat2(vec2(_c, -_s), vec2(_s, _c));
_rotation_matrix *= 0.5;
_rotation_matrix += 0.5;
_rotation_matrix = _rotation_matrix * 2.0 - 1.0;
_uv.xy = _uv.xy * _rotation_matrix;
_uv += center;
return _uv;
}