Skip to content

Commit

Permalink
Refactor logics to determine hand model offset
Browse files Browse the repository at this point in the history
It will now happen when controller is connected, rather than at model load.

This allows to apply controller-specific offsets, as the actual model is known.
  • Loading branch information
Elettrotecnica committed Jun 26, 2024
1 parent 1f98ee6 commit b0adabf
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/components/hand-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,28 @@ module.exports.Component = registerComponent('hand-controls', {
mesh.mixer.update(delta / 1000);
},

onControllerConnected: function () {
this.el.object3D.visible = true;
onControllerConnected: function (evt) {
var el = this.el;
var hand = this.data.hand;
var mesh = this.el.getObject3D('mesh');

el.object3D.visible = true;

var handModelOrientationZ = hand === 'left' ? Math.PI / 2 : -Math.PI / 2;
// The WebXR standard defines the grip space such that a cylinder held in a closed hand points
// along the Z axis. The models currently have such a cylinder point along the X-Axis.
var handModelOrientationX = el.sceneEl.hasWebXR ? -Math.PI / 2 : 0;

// Pico4, at least on Wolvic, needs a different rotation offset
// for the hand model. Pico Browser claims to use oculus
// controllers instead; will load oculus-touch-controls and does
// not require this adjustment.
if (evt.detail.name === 'pico-controls') {
handModelOrientationX += Math.PI / 4;
}

mesh.position.set(0, 0, 0);
mesh.rotation.set(handModelOrientationX, 0, handModelOrientationZ);
},

onControllerDisconnected: function () {
Expand Down Expand Up @@ -199,35 +219,19 @@ module.exports.Component = registerComponent('hand-controls', {
var handmodelUrl = MODEL_URLS[handModelStyle + hand.charAt(0).toUpperCase() + hand.slice(1)];
this.loader.load(handmodelUrl, function (gltf) {
var mesh = gltf.scene.children[0];
var handModelOrientationZ = hand === 'left' ? Math.PI / 2 : -Math.PI / 2;
// The WebXR standard defines the grip space such that a cylinder held in a closed hand points
// along the Z axis. The models currently have such a cylinder point along the X-Axis.
var handModelOrientationX = el.sceneEl.hasWebXR ? -Math.PI / 2 : 0;
mesh.mixer = new THREE.AnimationMixer(mesh);
self.clips = gltf.animations;
el.setObject3D('mesh', mesh);
mesh.traverse(function (object) {
if (!object.isMesh) { return; }
object.material.color = new THREE.Color(handColor);
});
mesh.position.set(0, 0, 0);
mesh.rotation.set(handModelOrientationX, 0, handModelOrientationZ);
el.setAttribute('magicleap-controls', controlConfiguration);
el.setAttribute('vive-controls', controlConfiguration);
el.setAttribute('oculus-touch-controls', controlConfiguration);
el.setAttribute('pico-controls', controlConfiguration);
el.setAttribute('windows-motion-controls', controlConfiguration);
el.setAttribute('hp-mixed-reality-controls', controlConfiguration);

// Pico4, at least on Wolvic, needs a different rotation
// offset for the hand model. Pico Browser claims to use
// oculus controllers instead; will load oculus-touch-controls
// and does not require this adjustment.
el.addEventListener('controllerconnected', function (evt) {
if (evt.detail.name === 'pico-controls') {
mesh.rotation.x += Math.PI / 4;
}
}, {once: true});
});
}
},
Expand Down

0 comments on commit b0adabf

Please sign in to comment.