Raycast objects that aren't in scene

Hello everyone, I’m doing a map editor with threejs and i have too many objects, so the best way to increase performance is to detach the objects from the scene when they arent highlighted instead of setting them invisible as I was doing previously. The problem with that is that i can’t use raycast anymore to select as they aren’t on the scene anymore.
I thought invisible objects aren’t rendered but seems like i was wrong, and having 10k objects is no good. Any idea how i could select them? Is there any way to raycast objects that arent on the scene?
Or a different way i could handle it?
Thanks in advance.

Using Raycaster.intersectObjects() you can supply your own array of objects to intersect with.


Invisible objects are not rendered. However, raycasting also checks the visibility of objects which is questionable. There is already an issue for this here:

I suggested to neglect the visibility of objects when perform raycasting and use THREE.Layers instead to filter out unwanted objects. This approach seems to be more flexible. What do you think? Would such a change be helpful to you?


Thank you @yombo, seems like Raycaster.intersectObjects() passing my own array does the job, but its not really viable for 10k objects :confused:.
@Mugen87 I’ve tried with layers too, but the problem is still the performance: if the objects are in the scene when they are not visible or when they have a different layer the renderer performance is not good so the only option for me is to remove them from the sceen when they aren’t selected.

Invisible objects or objects which don’t pass the layers test are sorted out very early in the rendering process. The only thing that could slow down your app is the world matrix update which still happens for invisible objects. If you have some time, it would be very useful to know if this is actually the case in your app. Otherwise it would be great if you can share a link.

1 Like

@Mugen87 that could very well be it, but I’ve tried setting object.matrixAutoUpdate to false and I still experience lag. Here’s the code: https://github.com/Powback/MapEditor/tree/Vanilla_v2

It’s a map editor for a game (CEF is rendered on top of the game so we use three as the UI, rendering a 3D world on top of the game’s). When an object is created ingame we spawn an object on threejs that represents it and it’s updated when the game updates it. Each object can have children, and these have an AABB object as a child. When an object is selected we set its visibility to true, but even when we are just moving the camera the threejs render lags (with object.matrixAutoUpdate=false).

Edit: the three objects are only updated when the game updates it, so if nothing appart from moving the camera is happening there is no matrix update. Not sure if im doing something wrong.

Just reviving to comment that I’m having this same problem myself. I create lots of objects and most of them are invisible, but I still get the performance hit. matrixAutoUpdate = false does not help, and it’s not the fault of Raycasts either.

“Invisible objects are not rendered. However, raycasting also checks the visibility of objects which is questionable. There is already an issue for this here:”

Hello Mugen,

do you think this is still so on R124+ ?

Actually the following line is the last I deal with before pushing the object into a raycastable array:

box.visible = false;

Even I cannot see the generated box( es ) in the scene I can still raycast them ( which is unwished behaviour in this case ).

When performing raycaster.intersectObjects() don’t use scene.children, but instead use an array containing only the objects that you’d like to check for.

scene.add(mesh1, mesh2, mesh3, mesh4);
const raycastArray = [mesh1, mesh4];

// This will only check intersection with mesh1 & mesh4

This way you can keep your rendered scene objects separate from the objects you want to raycast to.

Thanks, that is what I do with

I was surprised by three still raycasting the objects in these arrays when they are set invisible. Instead of waiting for kinda “switch” I seem to be supposed to change the programs logic. Perhaps one if per raycast does not slow it all down that much…