Reputation: 4668
Plane has no lookAt
method, so I can't just do lookAt(camera.position)
. Instead, it's defined with its normal, represented as a Vector3. How do I make this plane always face the camera?
Upvotes: 0
Views: 1886
Reputation: 8896
Plane.setFromNormalAndCoplanarPoint
can be used to accomplish this.
For a simple example, let's assume your camera is orbiting the origin. In this case, you can set up your plane to face the camera very easily:
// assume plane is a THREE.Plane
plane.setFromNormalAndCoplanarPoint( camera.position.clone().normalize(), scene.position )
This uses the camera's (normalized) position as the plane's normal, which works in this case.
If your camera is somewhere in free space, looking in an arbitrary direction, this is a little harder, but it's mostly visualization.
Remember, that a camera lives in its own "space," where it resides at the origin, and looks down the -Z
axis. Now imagine where your plane would need to be created within that space. Let's just say that it could be placed at ( 10, 10, -10 )
. We can again use some easy operations to compute the normal. The "hard" part is moving everything into world space, but three.js has convenience functions for that, too.
// in camera space...
let planePosition = new THREE.Vector3( 10, 10, -10 )
let normal = planePosition.clone().inverse().normalize()
// convert to world space...
camera.localToWorld( planePosition )
camera.localToWorld( normal )
normal.sub( camera.position ) // fix the normal based on the camera position
plane.setFromNormalAndCoplanarPoint( normal, planePosition )
One note about the code above, planePosition.clone().inverse().normalize()
gets a copy of the plane position, inverts it across the origin, then normalizes it. This has the effect of creating a normal which will point at the camera because the camera sits at the origin in its own space. This is why you need to adjust the normal by the camera's position after converting everything to world coordinates.
Upvotes: 1