@threlte/xr
Version:
Tools to more easily create VR and AR experiences with Threlte
18 lines (17 loc) • 900 B
JavaScript
import { Vector3 } from 'three';
import { controllers } from '../../hooks/useController.svelte.js';
import { hands } from '../../hooks/useHand.svelte.js';
const origin = new Vector3();
const forward = new Vector3();
export const defaultComputeFunction = (_context, handContext) => {
const targetRay = controllers[handContext.hand]?.targetRay ?? hands[handContext.hand]?.targetRay;
if (targetRay === undefined)
return;
// Read origin/direction from matrixWorld so the ray is in real world space,
// even when an ancestor (e.g. <XROrigin>) has a non-identity transform.
// Force an update because this runs before the frame's scene.updateMatrixWorld.
targetRay.updateWorldMatrix(true, false);
origin.setFromMatrixPosition(targetRay.matrixWorld);
forward.set(0, 0, -1).transformDirection(targetRay.matrixWorld);
handContext.raycaster.set(origin, forward);
};