diff --git a/shaders/agent_shader.gdshader b/shaders/agent_shader.gdshader index a35ada2..136d1d2 100644 --- a/shaders/agent_shader.gdshader +++ b/shaders/agent_shader.gdshader @@ -1,8 +1,17 @@ shader_type canvas_item; +// Snippets of code drawn from https://gist.github.com/Bleuje/1e497df4505ca24c39ab3930a95700b3 + uniform sampler2D sim_texture; -void fragment() { - vec3 c = texture(sim_texture, UV).rgb; - COLOR = vec4(c, 1.0); +float gn(in vec2 coordinate, in float seed){ + return fract(tan(distance(coordinate*(seed+0.118446744073709551614), vec2(0.118446744073709551614, 0.314159265358979323846264)))*0.141421356237309504880169); +} + +void fragment() { + vec3 orig = texture(sim_texture, UV).rgb; + float agent = orig.r; + float trail = orig.g; + + COLOR = vec4(agent, trail, orig.b, 1.0); } diff --git a/shaders/dissolve.gdshader b/shaders/dissolve.gdshader index baf9398..c297ccc 100644 --- a/shaders/dissolve.gdshader +++ b/shaders/dissolve.gdshader @@ -5,7 +5,16 @@ uniform sampler2D sim_texture; uniform float decay_factor; void fragment() { - float c = texture(sim_texture, UV).r; - c = c * decay_factor; - COLOR = vec4(c, c, c, 1.0); + vec3 orig = texture(sim_texture, UV).rgb; + float trail = texture(sim_texture, UV).g; + + float sum = 0.0; + for(int i=-1;i<=1;i++) { + for(int j=-1;j<=1;j++) { + sum += texture(sim_texture, UV).g; + } + } + trail = sum / 9.0; + trail = trail * decay_factor; + COLOR = vec4(orig.r, trail, orig.b, 1.0); } diff --git a/slime_simulation.gd b/slime_simulation.gd index 668988d..924cfd2 100644 --- a/slime_simulation.gd +++ b/slime_simulation.gd @@ -12,8 +12,8 @@ extends Node2D @onready var trail_shader: ShaderMaterial = $SubViewport2/Pass.material const MAX_TEXTURES: int = 2 -var next_texture: int = 0 -var frame_count: int = 0 +var _next_texture: int = 0 +var _frame_count: int = 0 func _ready(): @@ -21,12 +21,12 @@ func _ready(): func _process(_delta: float) -> void: - next_texture = (next_texture + 1) % MAX_TEXTURES + _next_texture = (_next_texture + 1) % MAX_TEXTURES RenderingServer.call_on_render_thread( - _render_process.bind(next_texture, frame_count, decay_factor) + _render_process.bind(_next_texture, _frame_count, decay_factor) ) - frame_count = frame_count + 1 + _frame_count = _frame_count + 1 # Below is for rendering @@ -42,14 +42,14 @@ func _create_initial_sim_texture(width: int, height: int) -> ImageTexture: return sim_texture -func _initialize_render(width: int, height: int) -> void: +func _initialize_render(_width: int, _height: int) -> void: rd = RenderingServer.get_rendering_device() trail_shader.set_shader_parameter("sim_texture", pass_viewport.get_texture().get_rid()) trail_shader.set_shader_parameter("decay_factor", decay_factor) -func _render_process(next_texture: int, frame_count: int, decay_factor: float) -> void: +func _render_process(__next_texture: int, frame_count: int, decay_factor: float) -> void: if frame_count > 0: agent_shader.set_shader_parameter("sim_texture", pass2_viewport.get_texture().get_rid()) diff --git a/slime_simulation.tscn b/slime_simulation.tscn index 46f6b0c..933ed57 100644 --- a/slime_simulation.tscn +++ b/slime_simulation.tscn @@ -32,7 +32,7 @@ size = Vector2(1920, 1080) [sub_resource type="ShaderMaterial" id="ShaderMaterial_g2c28"] shader = ExtResource("3_s6nlv") shader_parameter/sim_texture = SubResource("Texture2DRD_s6nlv") -shader_parameter/decay_factor = 0.95 +shader_parameter/decay_factor = 0.99 [node name="SlimeSimulation" type="Node2D"] script = ExtResource("1_s6nlv")