This repository showcases a minimal setup of triangle sorting based on camera distance. The implementation uses compute shaders to regenerate a suitable index buffer every frame, supporting both 16-bit and 32-bit index formats.
Live Demo:
The demo relies on experimental WebGPU Unity support. Browser compatibility (non-exhaustive testing):
- Chrome 146.0.7680.178 ✅
- Safari 26.3.1 (21623.2.7.111.2)
⚠️ Minor artifact (missing triangle in non-static mesh) - Firefox 149.0 ❌
Note: These unexpected behaviors may occur due to potential issues in the compute shaders.
This approach is a simplified setup based on the short video showcased here.
triangle-sorting-r.mp4
The system employs a GPU-based pipeline that runs entirely on compute shaders:
- Distance Calculation (
GenerateList.compute): For each triangle, compute the distance from its centroid to the camera position (in mesh local space) - Bitonic Sort (
SortList.compute): Sort triangles by distance using an optimized bitonic sort algorithm with multiple kernel variants - Index Buffer Reordering (
ReorderIndexBuffer.compute): Regenerate the mesh index buffer based on the sorted order
Unity Version: Unity 6000.5.0b2 (4427344da5e9)
Source:
- Static mesh: Created with boolean operations in Blender -
SimpleMesh.blend - Skinned mesh: From GameKit3D - Chomper character
- GPU Sorting has been isolated from VFX internal sorting - Sort.compute