ros3djs icon indicating copy to clipboard operation
ros3djs copied to clipboard

Making directional light follow the camera (fix some code in Viewer)

Open trusktr opened this issue 3 years ago • 2 comments

https://github.com/RobotWebTools/ros3djs/blob/530f7ec63778c83eaba4382395ee68b2dbc15808/src/visualization/Viewer.js#L131

This is how:

this.directionalLight.position.copy(this.camera.localToWorld(new THREE.Vector3(-1, 1, 0)));

But there is a much easier way that makes more sense: make the directional light a child of the camera, and make sure the camera is added to the scene. So removing that from the draw() method, we would instead replace the this in the constructor,

		this.scene.add(this.directionalLight);

with this,

		this.scene.add(this.camera);
		this.camera.add(this.directionalLight);

. Now the light will move wherever the camera goes, without having to do any procedure to sync one matrix to the other.

trusktr avatar Oct 05 '21 01:10 trusktr

I assume the reason for this is so that the directional light's shadow projection frustum moves with the camera, so objects that are in range of the camera always have the directional light. That's the only one I could think of.

trusktr avatar Oct 05 '21 01:10 trusktr

This worked well for me, we should use this.

akashjinandra avatar Feb 21 '24 02:02 akashjinandra