I'm trying to recreate Sebastian Lague's slime simulation, but I'm stuck with one of the core concepts: Creating a texture from one kernel, but then editing it from another:.
Here's the interesting part of my code:
C#
// == Render everything == //
compute.Dispatch(_updateKernel, Mathf.CeilToInt(settings.numAgents / 64f), 1, 1);
compute.SetTexture(_updateKernel, "trail_map", trailMap);
Graphics.Blit(trailMap, renderTexture);
compute.SetTexture(_trailMapKernel, "to_diffuse_trail_map", renderTexture);
compute.Dispatch(_trailMapKernel, settings.width / 8, settings.height / 8, 1);
// == Update variables for next cycle == //
compute.SetTexture(_trailMapKernel, "to_process_trail_map", renderTexture);
Graphics.CopyTexture(trailMap, renderTexture);
Compute Shader
#pragma kernel Update
[numthreads(64,1,1)]
void Update (uint3 id : SV_DispatchThreadID)
{
if (id.x >= num_agents) { return; }
// *Removed* Declaring the Agent
float2 new_pos = agent.position + direction * delta_time * agent_species.move_speed;
// *Removed* Just some movement code
agents[id.x].position = new_pos;
trail_map[int2(new_pos.x, new_pos.y)] = 1;
}
#pragma kernel ProcessTrailMap
[numthreads(8,8,1)]
void ProcessTrailMap (uint3 id : SV_DispatchThreadID)
{
const float4 original_value = to_process_trail_map[id.xy];
const float4 evaporated_value = max(0, original_value - 1 * delta_time);
processed_trail_map[id.xy] = evaporated_value;
}
What I'm trying to do is move some "agents" around a texture (Update), copy it, and then edit in another Kernel (ProcessTrailMap). However, if I use processed_trail_map[id.xy] = evaporated_value;, the texture is not edited. This changes if I do something like processed_trail_map[id.xy] = 0.5