diff --git a/root.gd b/root.gd new file mode 100644 index 0000000..017468d --- /dev/null +++ b/root.gd @@ -0,0 +1,30 @@ +extends Node2D + +@onready var sim_viewport := $SubViewport +@onready var display_sprite := $Sprite2D + +var tex_a: Texture2D +var tex_b: Texture2D +var swap = false + +func _ready(): + # Create textures + tex_a = ImageTexture.create_from_image( + Image.create(sim_viewport.size.x, sim_viewport.size.y, false, Image.FORMAT_RGBAF) + ) + tex_b = tex_a.duplicate() + + # Assign one texture to the simulation shader as input + $SubViewport/ColorRect.material.set("shader_parameter/trail_tex", tex_a) + display_sprite.texture = tex_a + +func _process(_delta): + # Swap textures each frame + swap = !swap + var src = tex_a if swap else tex_b + var dst = tex_b if swap else tex_a + + # Assign src as input and dst as render target + $SubViewport/ColorRect.material.set("shader_parameter/trail_tex", src) + sim_viewport.render_target_update_mode = SubViewport.UPDATE_ALWAYS + display_sprite.texture = dst diff --git a/root.gd.uid b/root.gd.uid new file mode 100644 index 0000000..1547549 --- /dev/null +++ b/root.gd.uid @@ -0,0 +1 @@ +uid://d1bbllvcal2uw diff --git a/root.gdshader b/root.gdshader index b70be26..4d78157 100644 --- a/root.gdshader +++ b/root.gdshader @@ -1,5 +1,7 @@ shader_type canvas_item; +uniform sampler2D trail_tex; + uniform float sensor_distance = 2.0; uniform float sensor_angle = 15.0; uniform float rotation_angle = 23.0; @@ -7,10 +9,11 @@ uniform float move_distance = 2.67; uniform float decay_factor = 0.75; void fragment() { - COLOR = vec4(0.05, 0.05, 0.05, 1.0); -} - -//void light() { -// // Called for every pixel for every light affecting the CanvasItem. -// // Uncomment to replace the default light processing function with this one. -//} + vec2 uv = UV; + // Read current trail value + vec4 trail = texture(trail_tex, uv); + // Apply decay + trail *= decay_factor; + + COLOR = trail; +} \ No newline at end of file diff --git a/root.tscn b/root.tscn index ab87db5..e04c0c7 100644 --- a/root.tscn +++ b/root.tscn @@ -1,6 +1,15 @@ -[gd_scene load_steps=4 format=3 uid="uid://bf4ohdg0sfl8l"] +[gd_scene load_steps=5 format=3 uid="uid://bf4ohdg0sfl8l"] [ext_resource type="Shader" uid="uid://b3wu436nky225" path="res://root.gdshader" id="1_pq8q7"] +[ext_resource type="Script" uid="uid://d1bbllvcal2uw" path="res://root.gd" id="1_pyidc"] + +[sub_resource type="ShaderMaterial" id="ShaderMaterial_pq8q7"] +shader = ExtResource("1_pq8q7") +shader_parameter/sensor_distance = 2.0 +shader_parameter/sensor_angle = 15.0 +shader_parameter/rotation_angle = 23.0 +shader_parameter/move_distance = 2.67 +shader_parameter/decay_factor = 0.75 [sub_resource type="ShaderMaterial" id="ShaderMaterial_pyidc"] shader = ExtResource("1_pq8q7") @@ -10,12 +19,15 @@ shader_parameter/rotation_angle = 23.0 shader_parameter/move_distance = 2.67 shader_parameter/decay_factor = 0.75 -[sub_resource type="QuadMesh" id="QuadMesh_vvh5c"] -material = SubResource("ShaderMaterial_pyidc") -size = Vector2(1152, 648) - [node name="Node2D" type="Node2D"] +script = ExtResource("1_pyidc") -[node name="MeshInstance2D" type="MeshInstance2D" parent="."] -position = Vector2(576, 324) -mesh = SubResource("QuadMesh_vvh5c") +[node name="Sprite2D" type="Sprite2D" parent="."] +material = SubResource("ShaderMaterial_pq8q7") + +[node name="SubViewport" type="SubViewport" parent="."] + +[node name="ColorRect" type="ColorRect" parent="SubViewport"] +material = SubResource("ShaderMaterial_pyidc") +offset_right = 40.0 +offset_bottom = 40.0