UNPKG

@threlte/xr

Version:

Tools to more easily create VR and AR experiences with Threlte

42 lines (41 loc) 1.69 kB
import { Vector3 } from 'three'; import { currentWritable } from '@threlte/core'; import { defaultComputeFunction } from './compute.js'; import { injectTouchControlsPlugin } from './plugin.svelte.js'; import { setupTouchControls } from './setup.svelte.js'; import { getControlsContext, getHandContext, setControlsContext, setHandContext, setInternalContext } from './context.js'; export const touchControls = (handedness, options) => { if (getControlsContext() === undefined) { injectTouchControlsPlugin(); setInternalContext(); setControlsContext({ interactiveObjects: [] }); } const context = getControlsContext(); if (getHandContext(handedness) === undefined) { const ctx = { hand: handedness, enabled: currentWritable(options?.enabled ?? true), pointer: currentWritable(new Vector3()), pointerOverTarget: currentWritable(false), origin: new Vector3(), originValid: false, lastEvent: undefined, initialClick: [0, 0, 0], initialHits: [], hovered: new Map(), down: false, joint: options?.joint ?? 'index-finger-tip', hoverRadius: options?.hoverRadius ?? 0.03, downRadius: options?.downRadius ?? 0.01, compute: options?.compute ?? defaultComputeFunction, filter: options?.filter }; setHandContext(handedness, ctx); setupTouchControls(context, ctx, options?.fixedStep); } const handContext = getHandContext(handedness); return { enabled: handContext.enabled, hovered: handContext.hovered }; };