diff --git a/components/look-at/examples/basic/index.html b/components/look-at/examples/basic/index.html
index c0f433d1..18c57d39 100644
--- a/components/look-at/examples/basic/index.html
+++ b/components/look-at/examples/basic/index.html
@@ -6,6 +6,28 @@
+
@@ -29,13 +51,18 @@
+ position="0 0 1" toggleable-camera>
+
+
+
+
+
diff --git a/components/look-at/index.js b/components/look-at/index.js
index eecb2fef..c493b75f 100644
--- a/components/look-at/index.js
+++ b/components/look-at/index.js
@@ -17,7 +17,7 @@ delete AFRAME.components['look-at'];
*/
AFRAME.registerComponent('look-at', {
schema: {
- default: '',
+ default: '0 0 0',
parse: function (value) {
// A static position to look at.
@@ -39,6 +39,9 @@ AFRAME.registerComponent('look-at', {
init: function () {
this.target3D = null;
this.vector = new THREE.Vector3();
+ this.cameraListener = AFRAME.utils.bind(this.cameraListener, this);
+ this.el.addEventListener('componentinitialized', this.cameraListener);
+ this.el.addEventListener('componentremoved', this.cameraListener);
},
/**
@@ -48,7 +51,6 @@ AFRAME.registerComponent('look-at', {
update: function () {
var self = this;
var target = self.data;
- var object3D = self.el.object3D;
var targetEl;
// No longer looking at anything (i.e., look-at="").
@@ -58,7 +60,7 @@ AFRAME.registerComponent('look-at', {
// Look at a position.
if (typeof target === 'object') {
- return object3D.lookAt(new THREE.Vector3(target.x, target.y, target.z));
+ return this.lookAt(new THREE.Vector3(target.x, target.y, target.z));
}
// Assume target is a string.
@@ -83,27 +85,42 @@ AFRAME.registerComponent('look-at', {
return function (t) {
// Track target object position. Depends on parent object keeping global transforms up
// to state with updateMatrixWorld(). In practice, this is handled by the renderer.
- var target;
var target3D = this.target3D;
- var object3D = this.el.object3D;
- var vector = this.vector;
-
if (target3D) {
target3D.getWorldPosition(vec3);
- if (this.el.getObject3D('camera')) {
- // Flip the vector to -z, looking away from target for camera entities. When using
- // lookat from THREE camera objects, this is applied for you, but since the camera is
- // nested into a Object3D, we need to apply this manually.
- // vector.subVectors(object3D.position, vec3).add(object3D.position);
- } else {
- vector = vec3;
- }
- object3D.lookAt(vector);
+ this.lookAt(vec3);
}
- };
+ }
})(),
+ remove: function () {
+ this.el.removeEventListener('componentinitialized', this.cameraListener);
+ this.el.removeEventListener('componentremoved', this.cameraListener);
+ },
+
beginTracking: function (targetEl) {
this.target3D = targetEl.object3D;
+ },
+
+ cameraListener: function (e) {
+ if (e.detail && e.detail.name === 'camera') {
+ this.update();
+ }
+ },
+
+ lookAt: function (position) {
+ var vector = this.vector;
+ var object3D = this.el.object3D;
+
+ if (this.el.getObject3D('camera')) {
+ // Flip the vector to -z, looking away from target for camera entities. When using
+ // lookat from THREE camera objects, this is applied for you, but since the camera is
+ // nested into a Object3D, we need to apply this manually.
+ vector.subVectors(object3D.position, position).add(object3D.position);
+ } else {
+ vector.copy(position);
+ }
+
+ object3D.lookAt(vector);
}
});