Skip to content

Commit 52d7f1f

Browse files
authored
Merge pull request #3245 from dmarcos/object3Dmanipulation
Sync rotation/position with Object3D
2 parents 2ba4f3a + 2341040 commit 52d7f1f

File tree

5 files changed

+75
-13
lines changed

5 files changed

+75
-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: 52 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 () {
@@ -897,6 +897,53 @@ suite('a-entity', function () {
897897
data = el.getAttribute('geometry');
898898
assert.ok(el.components.geometry.data === data);
899899
});
900+
901+
test('returns position previously set with setAttribute', function () {
902+
var el = this.el;
903+
el.setAttribute('position', {x: 1, y: 2, z: 3});
904+
assert.shallowDeepEqual(el.getAttribute('position'), {x: 1, y: 2, z: 3});
905+
});
906+
907+
test('returns position set by modifying the object3D position', function () {
908+
var el = this.el;
909+
el.object3D.position.set(1, 2, 3);
910+
assert.shallowDeepEqual(el.getAttribute('position'), {x: 1, y: 2, z: 3});
911+
});
912+
913+
test('returns rotation previously set with setAttribute', function () {
914+
var el = this.el;
915+
el.setAttribute('rotation', {x: 10, y: 45, z: 50});
916+
assert.shallowDeepEqual(el.getAttribute('rotation'), {x: 10, y: 45, z: 50});
917+
});
918+
919+
test('returns rotation previously set by modifying the object3D rotation', function () {
920+
var el = this.el;
921+
el.object3D.rotation.set(Math.PI, Math.PI / 2, Math.PI / 4);
922+
assert.shallowDeepEqual(el.getAttribute('rotation'), {x: 180, y: 90, z: 45});
923+
});
924+
925+
test('returns rotation previously set by modifying the object3D quaternion', function () {
926+
var el = this.el;
927+
var quaternion = new THREE.Quaternion();
928+
var euler = new THREE.Euler();
929+
euler.order = 'YXZ';
930+
euler.set(Math.PI / 2, Math.PI, 0);
931+
quaternion.setFromEuler(euler);
932+
el.object3D.quaternion.copy(quaternion);
933+
assert.shallowDeepEqual(el.getAttribute('rotation'), {x: 90, y: 180, z: 0});
934+
});
935+
936+
test('returns scale previously set with setAttribute', function () {
937+
var el = this.el;
938+
el.setAttribute('scale', {x: 1, y: 2, z: 3});
939+
assert.shallowDeepEqual(el.getAttribute('scale'), {x: 1, y: 2, z: 3});
940+
});
941+
942+
test('returns scale set by modifying the object3D scale', function () {
943+
var el = this.el;
944+
el.object3D.scale.set(1, 2, 3);
945+
assert.shallowDeepEqual(el.getAttribute('scale'), {x: 1, y: 2, z: 3});
946+
});
900947
});
901948

902949
suite('removeAttribute', function () {
@@ -1312,12 +1359,12 @@ suite('a-entity', function () {
13121359
var el = this.el;
13131360
mixinFactory('material', {material: 'shader: flat'});
13141361
mixinFactory('position', {position: '1 2 3'});
1315-
mixinFactory('rotation', {rotation: '10 20 30'});
1362+
mixinFactory('rotation', {rotation: '10 20 45'});
13161363
el.setAttribute('mixin', ' material\t\nposition \t rotation\n ');
13171364
el.setAttribute('material', 'color: red');
13181365
assert.shallowDeepEqual(el.getAttribute('material'), {shader: 'flat', color: 'red'});
13191366
assert.shallowDeepEqual(el.getAttribute('position'), {x: 1, y: 2, z: 3});
1320-
assert.shallowDeepEqual(el.getAttribute('rotation'), {x: 10, y: 20, z: 30});
1367+
assert.shallowDeepEqual(el.getAttribute('rotation'), {x: 10, y: 20, z: 45});
13211368
assert.equal(el.mixinEls.length, 3);
13221369
});
13231370

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)