Skip to content

Commit 760498c

Browse files
committed
Add WebXR controller support
1 parent bf3b0ef commit 760498c

25 files changed

+757
-455
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"present": "0.0.6",
4949
"promise-polyfill": "^3.1.0",
5050
"style-attr": "^1.0.2",
51-
"three": "dmarcos/three.js#poseTarget",
51+
"three": "dmarcos/three.js#webXRSupport",
5252
"three-bmfont-text": "^2.1.0",
5353
"webvr-polyfill": "^0.10.8"
5454
},

src/components/hand-controls.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ module.exports.Component = registerComponent('hand-controls', {
7878
this.onBorYTouchEnd = function () { self.handleButton('BorY', 'touchend'); };
7979
this.onSurfaceTouchStart = function () { self.handleButton('surface', 'touchstart'); };
8080
this.onSurfaceTouchEnd = function () { self.handleButton('surface', 'touchend'); };
81-
this.onControllerConnected = function () { self.setModelVisibility(true); };
82-
this.onControllerDisconnected = function () { self.setModelVisibility(false); };
8381

8482
el.addEventListener('controllerconnected', this.onControllerConnected);
8583
el.addEventListener('controllerdisconnected', this.onControllerDisconnected);
84+
85+
// Hidden by default.
86+
el.object3D.visible = false;
8687
},
8788

8889
play: function () {
@@ -182,8 +183,6 @@ module.exports.Component = registerComponent('hand-controls', {
182183
el.setObject3D('mesh', mesh);
183184
mesh.position.set(0, 0, 0);
184185
mesh.rotation.set(0, 0, 0);
185-
// hidden by default
186-
mesh.visible = false;
187186
el.setAttribute('vive-controls', controlConfiguration);
188187
el.setAttribute('oculus-touch-controls', controlConfiguration);
189188
el.setAttribute('windows-motion-controls', controlConfiguration);
@@ -341,12 +340,6 @@ module.exports.Component = registerComponent('hand-controls', {
341340
fromAction.play();
342341
toAction.play();
343342
fromAction.crossFadeTo(toAction, 0.15, true);
344-
},
345-
346-
setModelVisibility: function (visible) {
347-
var model = this.el.getObject3D('mesh');
348-
if (!model) { return; }
349-
model.visible = visible;
350343
}
351344
});
352345

src/components/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ require('./shadow');
2424
require('./sound');
2525
require('./text');
2626
require('./tracked-controls');
27+
require('./tracked-controls-webvr');
28+
require('./tracked-controls-webxr');
2729
require('./visible');
2830
require('./vive-controls');
2931
require('./vive-focus-controls');

src/components/look-controls.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ module.exports.Component = registerComponent('look-controls', {
2626
this.previousHMDPosition = new THREE.Vector3();
2727
this.hmdQuaternion = new THREE.Quaternion();
2828
this.hmdEuler = new THREE.Euler();
29+
this.poseMatrix = new THREE.Matrix4();
30+
this.posePosition = new THREE.Vector3();
31+
this.poseRotation = new THREE.Quaternion();
32+
this.poseScale = new THREE.Vector3();
2933
this.position = new THREE.Vector3();
3034
// To save / restore camera pose
3135
this.savedRotation = new THREE.Vector3();
@@ -38,6 +42,7 @@ module.exports.Component = registerComponent('look-controls', {
3842
this.pointerLocked = false;
3943
this.setupMouseControls();
4044
this.bindMethods();
45+
this.el.object3D.matrixAutoUpdate = false;
4146

4247
this.savedPose = {
4348
position: new THREE.Vector3(),
@@ -180,11 +185,26 @@ module.exports.Component = registerComponent('look-controls', {
180185
* Mouse-drag only enabled if HMD is not active.
181186
*/
182187
updateOrientation: function () {
183-
var el = this.el;
184188
var hmdEuler = this.hmdEuler;
189+
var object3D = this.el.object3D;
185190
var pitchObject = this.pitchObject;
186191
var yawObject = this.yawObject;
187192
var sceneEl = this.el.sceneEl;
193+
var poseMatrix = this.poseMatrix;
194+
var posePosition = this.posePosition;
195+
var poseRotation = this.poseRotation;
196+
var poseScale = this.poseScale;
197+
198+
// WebXR API updates applies headset pose to the object3D matrixWorld internally.
199+
// Reflect values back on position, rotation, scale so setAttribute returns expected values.
200+
if (sceneEl.is('vr-mode') && utils.device.isWebXRAvailable) {
201+
poseMatrix.elements = sceneEl.renderer.vr.getCameraPose().poseModelMatrix;
202+
poseMatrix.decompose(posePosition, poseRotation, poseScale);
203+
object3D.position.copy(posePosition);
204+
object3D.quaternion.copy(poseRotation);
205+
} else {
206+
object3D.updateMatrix();
207+
}
188208

189209
// In VR mode, THREE is in charge of updating the camera rotation.
190210
if (sceneEl.is('vr-mode') && sceneEl.checkHeadsetConnected()) { return; }
@@ -193,8 +213,8 @@ module.exports.Component = registerComponent('look-controls', {
193213
hmdEuler.setFromQuaternion(this.polyfillObject.quaternion, 'YXZ');
194214

195215
// On mobile, do camera rotation with touch events and sensors.
196-
el.object3D.rotation.x = hmdEuler.x + pitchObject.rotation.x;
197-
el.object3D.rotation.y = hmdEuler.y + yawObject.rotation.y;
216+
object3D.rotation.x = hmdEuler.x + pitchObject.rotation.x;
217+
object3D.rotation.y = hmdEuler.y + yawObject.rotation.y;
198218
},
199219

200220
/**
@@ -323,6 +343,8 @@ module.exports.Component = registerComponent('look-controls', {
323343
*/
324344
onEnterVR: function () {
325345
this.saveCameraPose();
346+
this.el.object3D.position.set(0, 0, 0);
347+
this.el.object3D.updateMatrix();
326348
},
327349

328350
/**

src/components/oculus-touch-controls.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
129129
this.el.setAttribute('tracked-controls', {
130130
id: data.hand === 'right' ? 'Oculus Touch (Right)' : 'Oculus Touch (Left)',
131131
controller: 0,
132+
hand: data.hand,
132133
orientationOffset: data.orientationOffset
133134
});
134135
this.loadModel();

0 commit comments

Comments
 (0)