diff --git a/.gitignore b/.gitignore index 4709183..eb4c9dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # Godot 4+ specific ignores .godot/ +tests/ diff --git a/addons/ShaderLib/UV/FlipbookUV.gd b/addons/ShaderLib/UV/FlipbookUV.gd new file mode 100644 index 0000000..71ffe15 --- /dev/null +++ b/addons/ShaderLib/UV/FlipbookUV.gd @@ -0,0 +1,86 @@ +@tool +class_name VisualShaderNodeUVFlipbook extends VisualShaderNodeCustom + +func _init() -> void: + set_input_port_default_value(1, 1) + set_input_port_default_value(2, 1) + set_input_port_default_value(3, 0) + set_input_port_default_value(4, 0) + set_input_port_default_value(5, 0.1) + +func _get_name() -> String: + return "Flipbook" + + +func _get_category() -> String: + return "UV" + + +func _get_description() -> String: + return "Creates a flipbook, or texture sheet animation, of the UVs supplied to input UV." + + +func _get_return_icon_type() -> VisualShaderNode.PortType: + return PORT_TYPE_VECTOR_2D + + +func _get_input_port_count() -> int: + return 6 + + +func _get_input_port_name(port: int) -> String: + match port: + 0: + return "uv" + 1: + return "rows" + 2: + return "columns" + 3: + return "start_frame" + 4: + return "end_frame" + 5: + return "anim_speed" + return "" + + +func _get_input_port_type(port: int) -> VisualShaderNode.PortType: + match port: + 0: + return PORT_TYPE_VECTOR_2D + 1, 2, 3, 4: + return PORT_TYPE_SCALAR_INT + 5: + 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 "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("FlipbooxUV.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 rows: String = input_vars[1] + var columns: String = input_vars[2] + var start_frame: String = input_vars[3] + var end_frame: String = input_vars[4] + var anim_speed: String = input_vars[5] + + return output_vars[0] + " = flipbook_uv(%s, %s, %s, %s, %s, %s);" % [uv, rows, columns, start_frame, end_frame, anim_speed] diff --git a/addons/ShaderLib/UV/FlipbooxUV.gdshaderinc b/addons/ShaderLib/UV/FlipbooxUV.gdshaderinc new file mode 100644 index 0000000..7a62c5c --- /dev/null +++ b/addons/ShaderLib/UV/FlipbooxUV.gdshaderinc @@ -0,0 +1,14 @@ +vec2 flipbook_uv(vec2 uv, int rows, int columns, int start_frame, int end_frame, float anim_speed){ + start_frame += int(fract(TIME * anim_speed) * float(end_frame)); + float _frame = float(clamp(start_frame, 0, end_frame)); + vec2 _off_per_frame = vec2((1.0 / float(columns)), (1.0 / float(rows))); + vec2 _sprite_size = vec2(uv.x / float(columns), uv.y / float(rows)); + vec2 _current_sprite = vec2(0.0, 1.0 - _off_per_frame.y); + _current_sprite.x += _frame * _off_per_frame.x; + float _row_index; + float _mod = modf(_frame / float(columns), _row_index); + _current_sprite.y -= _row_index * _off_per_frame.y; + _current_sprite.x -= _row_index * float(columns) * _off_per_frame.x; + vec2 _sprite_uv = (_sprite_size + _current_sprite); + return _sprite_uv; +} \ No newline at end of file