This project focuses on efficiently generating a cubic voxel chunk mesh using GPU. The meshing and drawing operations achieve a high performance, running at approximately 650 FPS on an RTX 4090. The mesh generation itself is even faster, exceeding 1000 FPS when the camera is disabled.
On HDRP with RTX 270 its around 300fps +- 20fps
On URP with RTX 2070 its around 500fps +- 75fps,
movie_015.mp4
This example utilizes a 3D noise library to generate voxel data. Users can adjust parameters like frequency, amplitude, offset, and speed of the noise through the Unity inspector.
- Creating a mesh on the GPU
- Utilizing Shader Graph with a custom function to draw a mesh using Graphics.DrawProceduralIndirect or Graphics.RenderPrimitivesIndirect. This functionality became achievable with Unity's recent addition of the VertexId node to Shader Graph.
- Generate Voxels Compute: Generates voxel data (0/1) using 3D noise.
- Feedback Compute: Iterates all voxels to calculate the count of vertices and indices required for the mesh.
- Voxelizer Compute: Iterates all voxels to write vertex and index data into the buffers.
- Drawing the Mesh: Utilizes Graphics.DrawProceduralIndirect or Graphics.RenderPrimitivesIndirect with data from index and vertex buffers.
The mesh is exclusively generated on the GPU and avoids CPU readback to create a Mesh object, which would be significantly slower (5 FPS).
UVs would require to write custom shader (not in Shader Graph) as Shader Graph don't allow to use Vertex ID node with fragment node, or some magic workaround to somehow use vertex id to get UVs.