How to determine if world coord is viewable from camera position?

I have HTML overlay elements tied to world coordinates. I need to find an easy, performant way to determine if those coordinates are viewable from the camera position. I can easily test if they are in the view, but what I need is to determine if the point in occluded by objects. I found an example that uses the depth buffer to do a z test but this does not perform very well at all. FPS gets cut in half. Here is the example:

Here is a similar example:

I was considering creating meshes at the world locations to perform occlusion testing on. Are there any good examples of occlusion tests that I could adapt to my scenario? How can either of those examples be modified to test for occlusion without using the slow calculations using the z buffer?

Occlusion testing is definitely a good candidate here:
Occlusion Query Sample | Babylon.js Playground (


Based on the example, I implemented occlusion testing but it only successfully tests against objects that were created through BABYLON. I have loaded objects into the scene from a GLB file and those meshes are not included in the occlusion tests. Is there something that I have to enable on the imported meshes to have them be part of the occlusion tests?

Here is a modified version of the example where the plane will occlude the box, but the BoomBox does not.

this is probably because the boombox is rendered AFTER the box meaning the box was rendered into the back buffer and THEN overwritten

it is mainly cause the bounding box is visible as well this works correctly:

I think you have the occlusion backwards. I am trying to determine when the box is occluded by the BoomBox and your example is the other way around. Based on what @Deltakosh said, I changed the ordered of created meshes and now this example works as expected:

Thanks to both of you for the help!

1 Like

Follow up question. The occlusion algorithm seems to have trouble at large distances. Works great within around 100 meters, but at about 200 meters the occlusion test seems to randomly think the object is occluded when it is definitely not. I am not sure how best to update the example to show this, but that is what I am seeing in my project. Any ideas what would cause this?

We unfortunately do not have much control on this part, the algorithm directly matches with GL values:


  • AbstractMesh.OCCLUSION_ALGORITHM_TYPE_CONSERVATIVE (Default Value) which is mapped to GL_ANY_SAMPLES_PASSED_CONSERVATIVE which is a false positive algorithm that is faster than GL_ANY_SAMPLES_PASSED but less accurate.

so you might try the accurate one ?

Thanks for the suggestion. I tried both options and the results were the same. I will do more research on the OpenGL side and see if there is a solution or work around. I may disable occlusion checking beyond certain distances. Other than that, this solution works great.

Now that I have this working with an associated mesh, what would be the best option to “hide” that reference mesh but still have it available to do the occlusion check? If I disable it, won’t it always fail the occlusion check? What about setting opacity to 0, will that also fail the occlusion check?

For now, I set alpha to 0.1 and it is mostly disappeared and the occlusion checks still work.

yep as long as the pixel are covered you are good to go

1 Like

Hi @Psyfun just checking in if you have any more questions :slight_smile: