Transparent AABB sort

I am trying to sort transparent AABBs of varying sizes but not overlapping, back to front.

My use-case is rendering gaussian splats passed through OGC3DTiles.
The AABBs contain transparent point objects, in an InstancedMesh, that are sorted independently in workers.

I come up with this scenario:

The red AABB is closer to the camera according to euclidean distance yet it should be rendered behind.

The only way I see to sort these AABBs is to compute rays for each corner of one AABB and ray-cast onto the second AABB until I find a ray where the intersection is further or closer than the corner used to define the ray.

Intuitively, it feels like there’s a shortcut using the distance along each axis separately but I can’t figure it out.

This is definitely a general question.
Perhaps a question more closely related to three.js would be to know if this is handled in the normal sorting for Mesh objects?

Ok I got part of the solution. It’s related to separating axis theorem I guess, not sure there’s a name for this:

Find an axis aligned plane that separates the 2 AABBs and the distance along it’s axis can be used to sort them.

Seems quite trivial now :sweat_smile:

Yep, it is trivial until you end up with two AABBs that touch like the red and the blue AABBs in your image… but the right side of the red is at 2.0000000000000004, while the left side of blue is at 2.

The turbulence of the least significant digits of floats is a nightmare.

Try to guess according to JS which one is bigger: 2 or (√2)²

1 Like

Yeah, the full z-sorting of AABBs is definitely not trivial.

I tried sorting via a “directed acyclic graph” and while it sort of works, there’s tons of corner cases.

luckily my data is in a kind of octree, not really a true octree but close enough that I can traverse it “depth first”.

checkmate:

yes but these are not axis aligned :grinning:

Oh whoops. my bad… I missed that nuance. :smiley:

1 Like