Rendering and Animating Hundreds of Thousands of Blocks with RenderMeshPrimitives

  • PublicPublic
  • 24 Sep, 2026
Rendering and Animating Hundreds of Thousands of Blocks with RenderMeshPrimitives

An image can be broken into a huge array of colored blocks using Graphics.RenderMeshPrimitives, then animated into waves, assemblies, repulsion, and collapses at playable frame rates. The approach starts by turning a source texture into a list of per-block position and color data. A horizontal block count is set gridWidthand the vertical count is derived from the image aspect ratio, so the source pixels are down-sampled at even intervals, while fully transparent and near-black pixels are skipped as background.

Each block is stored in a struct PixelBlock with the fields float3 position and float4 color, matching the HLSL layout exactly. In Awake method texture.GetPixels32 reads the source image, and the loop for maps each grid cell to a source pixel by grid coordinates, offsetting positions so that the grid center lands on the origin. Color bytes are divided by 255 to convert them into 0-1 values. A first naive pass creates one GameObject per block with a shared mesh, material, and MaterialPropertyBlock, but at around 100,000 blocks everything collapses to 6 FPS, because each object adds draw commands and component overhead.

The fix is Graphics.RenderMeshPrimitives an API purpose-built for drawing the same mesh many times. Instance data is uploaded once into a GraphicsBuffer (GraphicsBuffer.Target.Structured, sizeof(float) * 7 per element), then SetData(blocks) transfers the whole array from the CPU to the GPU in a single call. The stride argument must match the size of the HLSL struct and has to be updated whenever the fields change. A single call to Graphics.RenderMeshPrimitives(renderParams, cubeMesh, 0, blocks.Length) renders everything while the shader pulls position and color by SV_InstanceID. worldBounds must be large enough to contain all blocks: too small hides blocks and too large defeats culling; the buffer should be released in OnDisable, to avoid leaks.

At roughly 100,000 blocks, the GameObject version climbed to 54,525 draw calls and about 6 FPS, while the RenderMeshPrimitives version stayed at 42 draw calls for the whole scene and reached 700-865 FPS. Scaling up about tenfold to 1.04 million blocks kept draw calls at 42 and held the frame rate around 435 FPS; the figures are taken from Unity's Statistics window. Because the displacement is read from the buffer on the GPU side and the CPU issues just one draw command, the cost stays low even as the block count grows.

Animation follows two paths depending on whether the previous frame's state is needed. Motion defined purely by time runs entirely in the vertex shader: a wave adds sin along Z using _WaveAmplitude, _WaveFrequency and _WaveSpeed, while an assembly effect interpolates from a scattered position to the home position, driven by the value _Progress. Scatter directions come from GenerateHashedRandomFloat keyed by instance ID, so they are deterministic and require no stored state, while start thresholds shifted by height make the image rise from the bottom.

For motions that carry velocity or position across frames, a compute shader is used. The magnet effect stores current positions in a read-write buffer and repels blocks near the cursor, then every frame lerps positions toward their targets before drawing the update. The gravity collapse integrates velocity every frame into a BlockState, bouncing on landing with _Restitution and sleeping when speed drops below _SleepThreshold; _DeltaTime is clamped to 1/30 of a second to avoid integration blowups, and rest positions are precomputed from per-column density.