user345234674567
user345234674567

Reputation: 321

Three.js make Object3D Child face Camera

I have an Object3D which gets rotated and has a plane as child, how can I make the plane to always face the camera?

var obj = new THREE.Object3D()
var plane = new THREE.Mesh()

obj.add(plane)
plane.lookAt(camera.position)

Upvotes: 0

Views: 1874

Answers (1)

Falk Thiele
Falk Thiele

Reputation: 4494

Update for three.js r107:

plane.lookAt(camera.getWorldPosition(v));

is now doing what OP requested, whereas the old Solution is behaving differently. Updated the example: http://jsfiddle.net/7xj98f61/


Use this lookAtWorld addition from zz85

THREE.Object3D.prototype.lookAtWorld = function( vector ) {

  this.parent.worldToLocal( vector );
  this.lookAt( vector );

}

Or just hack it in like:

function animate() {
    /* ... */
    var vector = plane.parent.worldToLocal( camera.getWorldPosition() );
    plane.lookAt( vector );
}

Example: http://jsfiddle.net/L0rdzbej/201/

Upvotes: 5

Related Questions