Stardust: draw by a command buffer on the target camera, never frustum culled

Deletes the bounded Graphics.DrawProcedural, its hand-inflated Bounds, the GravityCamera field (and its scene reference) and the dead OnRenderObject block. The camera replays the buffer at AfterForwardOpaque; it is attached on enable, detached on disable and destroy, and released. Unity compile not yet run for this commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-16 04:48:41 +02:00
commit 71bf2a1bff
2 changed files with 35 additions and 27 deletions

View file

@ -16463,7 +16463,6 @@ MonoBehaviour:
NebulaTint: {fileID: 8400000, guid: d196505464047f04a97f28689041209b, type: 2}
Heightmap: {fileID: 8400000, guid: 2fc21a9d3e9647948b56e384cd9378b3, type: 2}
ParticleColors: {fileID: 2800000, guid: 56db63da398b2d04394ddbccedce943c, type: 3}
GravityCamera: {fileID: 1537342003}
--- !u!114 &1009503736
MonoBehaviour:
m_ObjectHideFlags: 0

View file

@ -34,7 +34,6 @@ public class Stardust : MonoBehaviour
public Texture Heightmap;
public Texture2D ParticleColors;
//public Transform TargetTransform;
public Camera GravityCamera;
private const int GROUP_SIZE = 128;
private int _updateParticlesKernel;
@ -57,6 +56,11 @@ public class Stardust : MonoBehaviour
private ComputeBuffer _quadPoints;
private const int QUAD_STRIDE = 12;
// The dust is drawn by a command buffer on TargetCamera: a camera replays its buffers at their event and never culls them,
// so the draw needs no bounds and no extent can fall short of how deep the field is.
private CommandBuffer _draw;
private const CameraEvent DrawEvent = CameraEvent.AfterForwardOpaque;
#endregion
#region setup
@ -91,6 +95,33 @@ public class Stardust : MonoBehaviour
new Vector3(-.5f, -.5f),
new Vector3(-.5f, .5f)
});
//The buffers never change after this, so the material is bound once
ParticleMaterial.SetBuffer("particles", _particlesBuffer);
ParticleMaterial.SetBuffer("quadPoints", _quadPoints);
//Start may run again (the scene harness calls it by hand): drop the previous draw before building the next
DetachDraw();
_draw?.Release();
_draw = new CommandBuffer { name = "Stardust" };
_draw.DrawProcedural(Matrix4x4.identity, ParticleMaterial, 0, MeshTopology.Triangles, 6, Span * Span);
AttachDraw();
}
void OnEnable() => AttachDraw();
void OnDisable() => DetachDraw();
void AttachDraw()
{
if (_draw != null && TargetCamera != null)
TargetCamera.AddCommandBuffer(DrawEvent, _draw);
}
void DetachDraw()
{
if (_draw != null && TargetCamera != null)
TargetCamera.RemoveCommandBuffer(DrawEvent, _draw);
}
#endregion
@ -104,7 +135,6 @@ public class Stardust : MonoBehaviour
// Particle lifetimes cycle on the flow clock (zone time), so the dust stops with the sim like everything the flow moves.
ParticleCalculation.SetFloat("phase", FlowField.Cycle(VolumeSampling.FlowClock, Period));
var pos = GravityCamera.transform.position;
ParticleCalculation.SetFloat("period", Period);
ParticleCalculation.SetFloat("spacing", Spacing);
ParticleCalculation.SetFloat("ceilingHeight", Ceiling);
@ -121,38 +151,17 @@ public class Stardust : MonoBehaviour
//Dispatch, launch threads on GPU
int numberOfGroups = Mathf.CeilToInt((float)Span * Span / GROUP_SIZE);
ParticleCalculation.Dispatch(_updateParticlesKernel, numberOfGroups, 1, 1);
ParticleMaterial.SetBuffer("particles", _particlesBuffer);
ParticleMaterial.SetBuffer("quadPoints", _quadPoints);
Graphics.DrawProcedural(ParticleMaterial, new Bounds(pos, new Vector3(Spacing * Span, 2048, Spacing * Span)), MeshTopology.Triangles, 6, Span * Span, TargetCamera, null, ShadowCastingMode.Off, false, 0);
}
#endregion
#region rendering
// void OnRenderObject()
// {
// if (Camera.current == TargetCamera || Camera.current.name == "SceneCamera")
// {
// //Bind resources to material
// ParticleMaterial.SetBuffer("particles", _particlesBuffer);
// ParticleMaterial.SetBuffer("quadPoints", _quadPoints);
// // ParticleMaterial.SetFloat("velocityStretch", ParticleVelocityStretch);
// // ParticleMaterial.SetVector("velocity", PlayerVelocity);
//
// //Set the pass
// ParticleMaterial.SetPass(0);
//
// //Draw
// Graphics.DrawProceduralNow(MeshTopology.Triangles, 6, Span * Span);
// }
// }
#endregion
#region cleanup
void OnDestroy()
{
// m_emitterProxies.Release();
// _temperature.Release();
DetachDraw();
_draw?.Release();
_draw = null;
if(_particlesBuffer!=null)
{
_particlesBuffer.Release();