Adding and removing three.js lights at run time

If add new light then delete or clear previous Light object.

Please help me.

I am loading new Object for implemented some task , during loading the object added some light, object position, texture image are loaded.

Again or next new object to be load than previous data should be clear or deleted.

For Example:-
First Object loaded.


Second Object Loaded:-

=> Lights are effecting in second object. I want delete or previous lights during second object loaded.

Thanks.

Why don’t you just remove the lights from the scene graph?

How to remove Light ?
=> Remove Light
I am using this code but not working.
scene.remove(scene.getObjectByName(‘Light’));

=> Add light:
pointLight = new THREE.PointLight(0xff0000, 0.5);
pointLight.name = ‘Light’;
camera.add(pointLight);
scene.add(camera);

How to remove the previous light??

The code looks correct but you are missing something. If you change the number of lights, you have to update the materials of your scene via material.needsUpdate. See https://stackoverflow.com/questions/16879378/adding-and-removing-three-js-lights-at-run-time

This fact is also noted in the docs: How to update things, section Materials

1 Like

Its working…

material.needsUpdate is only work for texture image update during.

It is not effecting on Light object.

I have the same problem, I tried hard but my code is still not working. Any help will be appreciated.

My code is here: https://jsfiddle.net/taf4p9jz/

Since you are not having an animation loop in your scene, you have to render it again when changing the light settings:

Hi Mehment.

Hope you are doing good!

You are using wrong method for light removing.

You should remove light from the scene.

Example.

scene.children.forEach(function (e) {

if (e.name == ‘Light’) {

scene.remove(e);

}

});

Thanks, Mugen87.