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


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.



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.

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 :slight_smile: 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