What is best way to know if I'm closer to some mesh in scene?

I have few different models rendered multiple times using instanced mesh. they are static models and Ik the location of each model in 3d space. I wanna fire some event as the camera gets within some distance of any model, so what are the ways of doing this thing and what’s a good solution? Right now I’m just looking through all the model’s position and seeing if camera is close or not using distance to method. It kinda works fine but sometimes it does lags. So is there any alternative way of doing this which can give little better performance?

What do you mean “sometimes” :thinking: ?

Usually the performance in webgl is quite … static - so it’s either always bad, or always good. If it stutters only from time to time - you may be triggering the grabage collector (ex. by constantly creating new objects in the animation loop), or triggering recompilation of the materials (both of those are thread-blocking.) It feels unlikely that consistently looping through (even a large amount of) objects would just occasionally cause fps drops. :thinking:

(As for the opimisation itself - if you’d still like to lower the amount of objects looped through, consider using a spatial index or a similar kind of grouping based on objects’ positions.)

1 Like

Use this demo: https://codepen.io/maurizzzio/pen/pERqxV?editors=0010
Delete there two texts to run:
var controls = new THREE.OrbitControls(camera, renderer.domElement)

1 Like

Yeah, it is lagging sometimes is little weird and as I checked it now and realised it only slows down when i am really close to the model, where my whole screen is filled with that one model only, so is it possible due to the fragment shader, since it has to calculate lot of pixels and small details when i am close and top of that loop is running in background too? is that possible?

its great demo but in some way it is also using distanceTo method and if i have multiple meshes i will have runn that closest point funtion for every mesh’s position so in a way that is what I am doig rn too

and yeah I wanna make some planes visible when i am close to model, but for that i am just doing visibility true or false depending on distance, does that have extra effect on performance even though I am not removing or adding that plane mesh completely

distanceTo is also what THREE.LOD uses: three.js/LOD.js at 44b8fa7b452dd0d291b9b930fdfc5721cb6ebee9 · mrdoob/three.js · GitHub i don’t think that little calculation on a single or a few specific objects will cause any damage unless you’re dealing with a ton of them at which point you probably could do the same in the shader. but even if, the perf hit would still be constant. perhaps the lag you experience has another culprit, have you checked chromes perf readout? like @mjurczyk said, “sometimes” doesn’t sound right.

1 Like

use simple code to test what is lagging

<div id="time_1"></div>
<div id="time_2"></div>
function animate(){
var s=performance.now();
//and for renderer
var s=performance.now();


Thanks for update and quick response, looking for same issue and it’s work for me. really appreciate for help.

1 Like