Skip to content

Commit 348431c

Browse files
committed
Sync rotation/position with Object3D
One can now edit the position and rotation of the object3D directly and getAttribute and setAttribute will return consistent values.
1 parent 5f305ec commit 348431c

File tree

5 files changed

+53
-13
lines changed

5 files changed

+53
-13
lines changed

src/components/camera.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ module.exports.Component = registerComponent('camera', {
158158
if (this.savedPose || !hasPositionalTracking) { return; }
159159

160160
this.savedPose = {
161-
position: el.getAttribute('position'),
161+
position: el.getAttribute('position').clone(),
162162
rotation: el.getAttribute('rotation')
163163
};
164164
},

src/core/a-entity.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var proto = Object.create(ANode.prototype, {
4343
this.object3D.el = this;
4444
this.object3DMap = {};
4545
this.parentEl = null;
46+
this.rotationObj = {};
4647
this.states = [];
4748
}
4849
},
@@ -720,7 +721,11 @@ var proto = Object.create(ANode.prototype, {
720721
getAttribute: {
721722
value: function (attr) {
722723
// If component, return component data.
723-
var component = this.components[attr];
724+
var component;
725+
if (attr === 'position') { return this.object3D.position; }
726+
if (attr === 'rotation') { return getRotation(this); }
727+
if (attr === 'scale') { return this.object3D.scale; }
728+
component = this.components[attr];
724729
if (component) { return component.data; }
725730
return window.HTMLElement.prototype.getAttribute.call(this, attr);
726731
},
@@ -851,5 +856,15 @@ function isComponent (componentName) {
851856
return true;
852857
}
853858

859+
function getRotation (entityEl) {
860+
var radToDeg = THREE.Math.radToDeg;
861+
var rotation = entityEl.object3D.rotation;
862+
var rotationObj = entityEl.rotationObj;
863+
rotationObj.x = radToDeg(rotation.x);
864+
rotationObj.y = radToDeg(rotation.y);
865+
rotationObj.z = radToDeg(rotation.z);
866+
return rotationObj;
867+
}
868+
854869
AEntity = registerElement('a-entity', {prototype: proto});
855870
module.exports = AEntity;

tests/core/a-entity.test.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,10 @@ suite('a-entity', function () {
347347
test('can update component data', function () {
348348
var el = this.el;
349349
el.setAttribute('position', '10 20 30');
350-
assert.deepEqual(el.getAttribute('position'), {x: 10, y: 20, z: 30});
350+
assert.shallowDeepEqual(el.getAttribute('position'), {x: 10, y: 20, z: 30});
351351

352352
el.setAttribute('position', {x: 30, y: 20, z: 10});
353-
assert.deepEqual(el.getAttribute('position'), {x: 30, y: 20, z: 10});
353+
assert.shallowDeepEqual(el.getAttribute('position'), {x: 30, y: 20, z: 10});
354354
});
355355

356356
test('can partially update multiple properties of a component', function () {
@@ -392,7 +392,7 @@ suite('a-entity', function () {
392392
test('can partially update vec3', function () {
393393
var el = this.el;
394394
el.setAttribute('position', {y: 20});
395-
assert.deepEqual(el.getAttribute('position'), {x: 0, y: 20, z: 0});
395+
assert.shallowDeepEqual(el.getAttribute('position'), {x: 0, y: 20, z: 0});
396396
});
397397

398398
test('can update component property with asymmetrical property type', function () {
@@ -1312,12 +1312,12 @@ suite('a-entity', function () {
13121312
var el = this.el;
13131313
mixinFactory('material', {material: 'shader: flat'});
13141314
mixinFactory('position', {position: '1 2 3'});
1315-
mixinFactory('rotation', {rotation: '10 20 30'});
1315+
mixinFactory('rotation', {rotation: '10 20 45'});
13161316
el.setAttribute('mixin', ' material\t\nposition \t rotation\n ');
13171317
el.setAttribute('material', 'color: red');
13181318
assert.shallowDeepEqual(el.getAttribute('material'), {shader: 'flat', color: 'red'});
13191319
assert.shallowDeepEqual(el.getAttribute('position'), {x: 1, y: 2, z: 3});
1320-
assert.shallowDeepEqual(el.getAttribute('rotation'), {x: 10, y: 20, z: 30});
1320+
assert.shallowDeepEqual(el.getAttribute('rotation'), {x: 10, y: 20, z: 45});
13211321
assert.equal(el.mixinEls.length, 3);
13221322
});
13231323

@@ -1336,6 +1336,31 @@ suite('a-entity', function () {
13361336
assert.equal(el.mixinEls.length, 0);
13371337
});
13381338
});
1339+
1340+
suite('getRotation', function () {
1341+
test('returns rotation previously set with setAttribute', function () {
1342+
var el = this.el;
1343+
el.setAttribute('rotation', {x: 10, y: 45, z: 50});
1344+
assert.shallowDeepEqual(el.getAttribute('rotation'), {x: 10, y: 45, z: 50});
1345+
});
1346+
1347+
test('returns rotation previously set by modifying the object3D rotation', function () {
1348+
var el = this.el;
1349+
el.object3D.rotation.set(Math.PI, Math.PI / 2, Math.PI / 4);
1350+
assert.shallowDeepEqual(el.getAttribute('rotation'), {x: 180, y: 90, z: 45});
1351+
});
1352+
1353+
test('returns rotation previously set by modifying the object3D quaternion', function () {
1354+
var el = this.el;
1355+
var quaternion = new THREE.Quaternion();
1356+
var euler = new THREE.Euler();
1357+
euler.order = 'YXZ';
1358+
euler.set(Math.PI / 2, Math.PI, 0);
1359+
quaternion.setFromEuler(euler);
1360+
el.object3D.quaternion.copy(quaternion);
1361+
assert.shallowDeepEqual(el.getAttribute('rotation'), {x: 90, y: 180, z: 0});
1362+
});
1363+
});
13391364
});
13401365

13411366
suite('a-entity component lifecycle management', function () {

tests/core/controls.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ suite('position controls on camera with WASD controls (integration unit test)',
2222
var keydownEvent;
2323
var position;
2424

25-
position = el.getAttribute('position');
25+
position = el.getAttribute('position').clone();
2626
keydownEvent = new Event('keydown');
2727
keydownEvent.code = 'KeyW';
2828
window.dispatchEvent(keydownEvent);
@@ -44,7 +44,7 @@ suite('position controls on camera with WASD controls (integration unit test)',
4444
var keydownEvent;
4545
var position;
4646

47-
position = el.getAttribute('position');
47+
position = el.getAttribute('position').clone();
4848
keydownEvent = new Event('keydown');
4949
keydownEvent.code = 'KeyA';
5050
window.dispatchEvent(keydownEvent);
@@ -66,7 +66,7 @@ suite('position controls on camera with WASD controls (integration unit test)',
6666
var keydownEvent;
6767
var position;
6868

69-
position = el.getAttribute('position');
69+
position = el.getAttribute('position').clone();
7070
keydownEvent = new Event('keydown');
7171
keydownEvent.code = 'KeyS';
7272
window.dispatchEvent(keydownEvent);
@@ -88,7 +88,7 @@ suite('position controls on camera with WASD controls (integration unit test)',
8888
var keydownEvent;
8989
var position;
9090

91-
position = el.getAttribute('position');
91+
position = el.getAttribute('position').clone();
9292
keydownEvent = new Event('keydown');
9393
keydownEvent.code = 'KeyD';
9494
window.dispatchEvent(keydownEvent);
@@ -111,7 +111,7 @@ suite('position controls on camera with WASD controls (integration unit test)',
111111
var position;
112112

113113
el.setAttribute('rotation', '0 90 0');
114-
position = el.getAttribute('position');
114+
position = el.getAttribute('position').clone();
115115
keydownEvent = new Event('keydown');
116116
keydownEvent.code = 'KeyW';
117117
window.dispatchEvent(keydownEvent);

tests/extras/primitives/primitives/a-torus.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ suite('a-torus', function () {
1414
});
1515

1616
test('has default position when created', function () {
17-
assert.deepEqual(this.torusEl.getAttribute('position'), {x: 0, y: 0, z: 0});
17+
assert.shallowDeepEqual(this.torusEl.getAttribute('position'), {x: 0, y: 0, z: 0});
1818
});
1919

2020
test('sets geometry.primitive', function () {

0 commit comments

Comments
 (0)