fenomas / noa

Experimental voxel game engine.
MIT License
608 stars 86 forks source link

Alpha blended mesh ordering issue #177

Open MCArth opened 2 years ago

MCArth commented 2 years ago

Hi,

I've noticed some issues with alpha blended meshes, where the sorting order chosen by Babylon is wrong (and inconsistent as you move the camera)

It seems very likely its due to Babylon sorting meshes according to the center of the bounding sphere (at least from what I can tell):

Avoid having heavily-stretched alpha blended meshes (i.e. large planes); since the center of its bounding sphere is used for depth sorting, doing this may result in a mesh being sorted as far away from the camera but actually closer to many other meshes.

https://doc.babylonjs.com/divingDeeper/materials/advanced/transparent_rendering#rendering-order

It can be seen in test with the shadow:

image

(and when I zoom out)

image

I imagine this could be fixed by raycasting towards the mesh from the camera and manually setting alpha indexes dependent on if you hit a transparent voxel before reaching the mesh

I'm wondering if you've thought about this at all or have a simpler solution?

MCArth commented 2 years ago

I've also noticed this issue affecting non-opaque blocks in noa-test, which seems like less of an easy fix image

But I couldn't get it to affect the a/b/c/d/1/2 blocks (I assume they are alpha-tested instead of blended?)

MCArth commented 1 year ago

As of babylon 5.0, it sounds like there's a feature that will fix this, though I haven't tested it - https://doc.babylonjs.com/divingDeeper/materials/advanced/transparent_rendering#generality

It notes there could be a perf hit (and it's also applied to alpha-tested meshes, which is unneeded)