Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point SpotLight in same direction as camera three.js (Flashlight)

I'm really new in this stuff. I want to make a simple 3D scene, where i can fly around with PointerLockControls, but i want also to have some kind of flashlight. So spot light should point same direction as camera does.

I have made spotlight to follow camera but its target is bound to 0,0,0.

What is the best way to achieve this?

Thank you.

like image 889
Pavel Galaton Avatar asked May 09 '13 07:05

Pavel Galaton


2 Answers

The SpotLight target is an Object3D, not a Vector3.

spotlight.target = myObject;

The best solution in your case is to use a PointLight instead, and use this pattern:

scene.add( camera );
camera.add( pointLight );

If you still want to use a spotlight, then do something like this:

scene.add( camera );
camera.add( spotLight.target );
spotLight.target.position.set( 0, 0, -1 );
spotLight.position.copy( camera.position ); // and reset spotlight position if camera moves

It is not generally required that the camera be added as a child of the scene, but it is required in this case because the light is added as a child of the camera.

three.js r.69

like image 172
WestLangley Avatar answered Sep 28 '22 18:09

WestLangley


I had the same problem which I solved as follows:

flashlight = new THREE.SpotLight(0xffffff,4,40);
camera.add(flashlight);
flashlight.position.set(0,0,1);
flashlight.target = camera;

Since a SpotLight's .target needs to be an object (and not a position) I found it mentally easier to simply place the flashlight directly behind the camera, and then aim it at the camera. Thus the light shines through the camera and lights up the things in front of it.

This approach is fine if you are after a flashlight effect where the flashlight is held close to the chest (central to the body) and not off on one side.

like image 21
Tim Avatar answered Sep 28 '22 17:09

Tim