atcComm
atcComm

Reputation: 5

Three.js place an object in rotating camera's fov

I have a fixed-position, perspective camera that rotates around all 3 axes via keyboard input. At random intervals, independent of user input, I need to place objects within the camera's field of view no matter what direction the camera is looking. The objects will also need to be offset specific x and y distances from the center of the camera's fov and offset a specific z distance from the camera's position. I cannot use camera.addChild because once the object is added I need to move the object via tweening independent of the camera's movements.

How can this be done?

Upvotes: 0

Views: 1256

Answers (1)

WestLangley
WestLangley

Reputation: 104763

You want to transform a point from camera space to world space.

In the camera's coordinate system, the camera is located at the origin, and is looking down its negative z-axis.

Place the object in front of the camera (in the camera's coordinate system).

object.position.set( x, y, - z ); // z is the distance in front of the camera, and is positive

Now, transform the object's position from camera space to world space:

object.position.applyMatrix4( camera.matrixWorld );

three.js r.69

Upvotes: 3

Related Questions