I’m seeking advice on how to remove the spotlight effect caused by a glb model. Currently, I have a button that showcases the model by adding ambient and directional lighting to highlight certain areas while rotating. However, when I stop showcasing the model, the lighting persists instead of reverting to its original state. I’ve attempted to traverse through the scene and remove the added light, but this approach hasn’t been successful. Does anyone have insights or solutions to this issue? Any help would be greatly appreciated.
const { camera, scene, gl } = useThree();
const [spotlightOn, setSpotlightOn] = useState(false);
scene.add( ambientLight );
scene.add( directionalLight );
//Spotlight function
const showcase = Spotlight(scene, camera, gl); //In here is where the lighting gets manipulated.
const showcaseButton= () => {
if (showcase) {
if (spotlightOn) {
showcase.stop() //This should have removed all the lighting because I have scene.remove all the lughting before retuning.
} else {
showcase.start()
}
setSpotlightOn((prev) => !prev);
}
};
you should never call .add or .remove, you can pretend these functions don’t exist. you add lights, meshes and so on declaratively, and that’s how you control if they’re on, off or mounted or unmounted. you don’t alter the scene imperatively, you shouldn’t traverse.
mounting or not mounting a light is as simple as this:
in react your view is the deterministic outcome of a function, it frees you from mutation, traversal and all this vanilla chaos which ends up in spaghetti code and race conditions, and this is what i’m guessing you have. it would be better not to mix the old way with the new, or you’ll just invite all the chaos back.