Skip to content

PaulDemeulenaere/triangle-sorting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Triangle Sorting

Overview

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.

Demo

Live Demo: ▶️ https://pauldemeulenaere.github.io/triangle-sorting/ ◀️

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.

Technical Approach

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:

  1. Distance Calculation (GenerateList.compute): For each triangle, compute the distance from its centroid to the camera position (in mesh local space)
  2. Bitonic Sort (SortList.compute): Sort triangles by distance using an optimized bitonic sort algorithm with multiple kernel variants
  3. Index Buffer Reordering (ReorderIndexBuffer.compute): Regenerate the mesh index buffer based on the sorted order

Additional

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

About

Minimal setup of mesh triangle sorting based on camera distance

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors