zworld/engine/render/pass/depth_sorter.go
2024-01-14 22:56:06 +08:00

27 lines
760 B
Go

package pass
import (
"sort"
"zworld/engine/object/mesh"
"zworld/engine/render/uniform"
"zworld/plugins/math/vec3"
)
func DepthSortGroups(cache MaterialCache, frame int, cam uniform.Camera, meshes []mesh.Mesh) DrawGroups {
eye := cam.Eye.XYZ()
// perform back-to-front depth sorting
// we use the closest point on the meshes bounding sphere as a heuristic
sort.SliceStable(meshes, func(i, j int) bool {
// return true if meshes[i] is further away than meshes[j]
first, second := meshes[i].BoundingSphere(), meshes[j].BoundingSphere()
di := vec3.Distance(eye, first.Center) - first.Radius
dj := vec3.Distance(eye, second.Center) - second.Radius
return di > dj
})
// sort meshes by material
return OrderedGroups(cache, frame, meshes)
}