@threlte/xr
Version:
Tools to more easily create VR and AR experiences with Threlte
41 lines (40 loc) • 1.17 kB
JavaScript
import { getHandState } from '../internal/inputSources.svelte.js';
import { runeToCurrentReadable } from './currentReadable.svelte.js';
const handObjects = new WeakMap();
const toXRHandObject = (state) => {
if (state === undefined)
return undefined;
let hand = handObjects.get(state);
if (hand !== undefined)
return hand;
hand = {
targetRay: state.targetRay,
hand: state.hand,
model: state.model,
inputSource: state.inputSource.hand
};
handObjects.set(state, hand);
return hand;
};
class Hands {
get left() {
return toXRHandObject(getHandState('left'));
}
get right() {
return toXRHandObject(getHandState('right'));
}
}
export const hands = new Hands();
/**
* Provides a reference to a current XRHand, filtered by handedness.
*/
export const useHand = (handedness) => {
switch (handedness) {
case 'left':
return runeToCurrentReadable(() => hands.left);
case 'right':
return runeToCurrentReadable(() => hands.right);
default:
throw new Error('useHand handedness must be left or right.');
}
};