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.

2 Likes

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?

3 Likes

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.