UNPKG

@threlte/xr

Version:

Tools to more easily create VR and AR experiences with Threlte

62 lines (61 loc) 2.12 kB
import { Raycaster } from 'three'; import { getContext, setContext } from 'svelte'; import { defaultComputeFunction } from './compute'; const handContextKeys = { left: Symbol('teleport-controls-context-left-hand'), right: Symbol('teleport-controls-context-right-hand') }; const contextKey = Symbol('teleport-controls-context'); export const getHandContext = (hand) => { return getContext(handContextKeys[hand]); }; export const setHandContext = (hand, context) => { setContext(handContextKeys[hand], context); }; export const useTeleportControls = () => { return getContext(contextKey); }; export const createTeleportContext = (compute) => { const addSurface = (mesh, events) => { // check if the object is already in the list if (context.interactiveObjects.indexOf(mesh) > -1) { return; } context.interactiveObjects.push(mesh); context.surfaces.set(mesh.uuid, mesh); context.dispatchers.set(mesh, events); }; const removeSurface = (mesh) => { const index = context.interactiveObjects.indexOf(mesh); context.interactiveObjects.splice(index, 1); context.surfaces.delete(mesh.uuid); context.dispatchers.delete(mesh); }; const addBlocker = (mesh) => { // check if the object is already in the list if (context.interactiveObjects.indexOf(mesh) > -1) { return; } context.interactiveObjects.push(mesh); context.blockers.set(mesh.uuid, mesh); }; const removeBlocker = (mesh) => { const index = context.interactiveObjects.indexOf(mesh); context.interactiveObjects.splice(index, 1); context.blockers.delete(mesh.uuid); }; const context = { interactiveObjects: [], surfaces: new Map(), blockers: new Map(), dispatchers: new WeakMap(), raycaster: new Raycaster(), compute: compute ?? defaultComputeFunction, addBlocker, removeBlocker, addSurface, removeSurface }; setContext(contextKey, context); return context; };