UNPKG

@threlte/xr

Version:

Tools to more easily create VR and AR experiences with Threlte

24 lines (23 loc) 899 B
import { injectPlugin, isInstanceOf, observe } from '@threlte/core'; import { untrack } from 'svelte'; import { usePointerControls } from './hook'; import { events } from './types'; export const injectPointerControlsPlugin = () => { injectPlugin('threlte-pointer-controls', (args) => { if (!isInstanceOf(args.ref, 'Object3D')) return; const hasEventHandlers = Object.entries(args.props).some(([key, value]) => { return value !== undefined && events.includes(key); }); if (!hasEventHandlers) return; const { addInteractiveObject, removeInteractiveObject } = usePointerControls(); observe.pre(() => [args.ref], ([ref]) => { addInteractiveObject(ref, args.props); return () => removeInteractiveObject(ref); }); return { pluginProps: events }; }); };