# Hide objects farawy from player character

Is there an easy way to hide every mesh that is further from a playcer character (certain scene object) than maximal distance?
Is there faster way than computng num_of_meshes - 1 distances (pc <-> object) and hiding each mesh which further away than certain threshold?

Hi pietrko,

Depending on the effect youâ€™re going for, probably the easiest way is to just set `camera.maxZ` to your preferred maximum distance. This will cause the camera to â€śclipâ€ť things that are too far away such that the user wonâ€™t see them. As a point of interest, that effectively does compute a whole bunch of distances, as you mention (it actually computes way more than n - 1), but it does so on the GPU in a way thatâ€™s massively parallel. If your goal is something other than visually hiding things, there are more complicated options; but if this accomplishes what youâ€™re going for, itâ€™s almost certainly the easiest way. Hope this helps, and best of luck!

1 Like

Thanks!

but this relies distance from camera, I use arcRotateCamera which means distance to camera isnâ€™t the distance to character.

Ah, gotcha; without a Playground example, I misunderstood your use case. Again, it depends on your objective somewhat, but probably the easiest approach is going to be to cull by distance, though that doesnâ€™t necessarily mean computing a full Euclidean distance per mesh per frame as there are many possible optimizations.

• You could do your comparisons based on squared distance instead of distance to avoid a square root and speed up the computations involved.
• You could use Manhattan distance (basically just comparing coordinates instead of computing 3D distance) to simplify the calculations involved even further.
• You could use a scheduling strategy to recalculate distances only infrequently. For example, if your sight radius is r and there is some other small distance k, if you calculate all distances to all meshes from the characterâ€™s current position p, any that are further than r + k could go in a special list that you donâ€™t check again until the character has moved at least k distance from p, at which point you repeat the procedure.

If you have a really high density of things in your scene, you might need to chunk them â€“ add many to a local â€śchunk,â€ť then just calculate distances to chunk centroids instead of all entities â€“ or use a spatial data structure. However, unless youâ€™re approaching Skyrim levels of object density, these last are probably just going to add more complexity than theyâ€™re worth.

4 Likes

Thanks!

Lemme put an example, there are two goals I want to achieve:

1. Check the little city on water at the very top of the screen. It shouldnâ€™t be visible (fog could be sufficient here). Same goes for trees.
2. Entities (other characters) that are beyond this circular fogOfWar shouldnâ€™t be visible.

USING FOG
Yea the problem I have a custom fog that depends on the distance from player characer (the little guy in the middle) instead of the camera.
I would need to replace every shader out there to have custom fog that works for anything other than
my terrain (only terrain has custom shader atm).
Or maybe thereâ€™s simpler way?

And yea I really do have high object density, thereâ€™s like 100k small trees to be displayed, currently they are SPS, but they work really weird with dynamic terrain.

I think @syntheticmagus tricks would be the best you could add on top of it a tiled like compute if you are in an open world. So at least you only go through 4 tiles of meshes maximum to speed up the search a little.

1 Like

Out of the examples thread nr 909

https://playground.babylonjs.com/#UVCDAS#3

2 Likes