Detect distance from camera efficiently


What do you think is the best approach to have an efficient way to get geometries to hide depending on the distance of camera?

I have some buffer geometry meshes representing some cubes (a lot it’s why I need only 1 mesh with all cubes geometry in it for performance). 1 mesh for each zoom level.
Imagine a bunch of random cubes like some examples shows placed in a cubic space, this is 1 level. Then each cube is also a cubic space for a bunch of other smaller random cubes, and so on…

I want then each cube (separately) to hide when camera goes close to it to show the smaller cubes in it. The farther bigger cubes stay visible dont showing it’s nested cubes.
I need 2 distance triggers, 1 to load the smaller cubes hidden, and 1 to hide the containing cube to show the smaller.

Any advice to get something smooth and not lagging?

Did you try LODs? (you can invert them, so for further distance levels you show the object, and for close you hide it.)


Hi ! Thx for this quick reply!

No I didn’t try, because I didn’t know this object…

But I fact my cubes are not random, it was to illustrate, I need an API call (to get positions and scales) and a BuffGeom generation (possibly long loop) before being able to .addLevel : so I need to know a distance trigger before having a level added in a LOD…
(I really can’t load everything once at start it’s too much data).
Also, I need this to work with a perspective camera AND an orthogonal one too, so I don’t understand how to set the camera of the LOD object?

Orthographic :slight_smile: Haven’t used LOD with OrthographicCamera before, but I see no reason for it not to work - it’s just distance calculation - shouldn’t have anything to do with perspective.

  1. You define the distance trigger / levels in LOD - there are no preset values. So if you need it beforehand - just define them beforehand. :thinking:
  2. LOD is object specific - each model you’d load from your API will have a separate LOD instance. First you load the model, then you add it to LOD, then you add levels to LOD, then you add LOD to the scene.
  3. LOD works with distance from camera - regardless of its position in the world space. If LOD has visibility level set to 100.0, it will become visible when camera is closer than 100.0 units from the object - if camera is further away, it will then hide the object (or show lower resolution model, depending on how you set it up.)

Yes… typo here :wink:

Yep, I checked the source it should work with the current camera… :+1:
I just have to check the difference between distance and zoom… Generally an OrthoCam don’t move but is zoomed, but I have seen that LOD is using camera zoom also…

Okay, I just don’t know how… I’ll investigate raycasting!

I can’t there will be 1000s of LODs…

Btw, I didn’t said that LOD is not a good idea :wink: I’ll definitely try it I just didn’t had time since yesterday…

Thx for this advice!

1 Like