@threlte/xr
Version:
Tools to more easily create VR and AR experiences with Threlte
40 lines (39 loc) • 1.63 kB
JavaScript
import { Raycaster } from 'three';
import { currentWritable, observe } from '@threlte/core';
import { createTeleportContext, useTeleportControls, getHandContext } from './context.js';
import { injectTeleportControlsPlugin } from './plugin.svelte.js';
import { setHandContext } from './context.js';
import { setupTeleportControls } from './setup.svelte.js';
import { defaultComputeFunction } from './compute.js';
import { teleportState } from '../../internal/state.svelte.js';
export const teleportControls = (handedness, options) => {
if (useTeleportControls() === undefined) {
injectTeleportControlsPlugin();
createTeleportContext();
}
const context = useTeleportControls();
if (getHandContext(handedness) === undefined) {
const ctx = {
hand: handedness,
active: currentWritable(false),
enabled: currentWritable(options?.enabled ?? true),
hovered: currentWritable(undefined),
raycaster: new Raycaster(),
compute: options?.compute ?? defaultComputeFunction
};
setHandContext(handedness, ctx);
setupTeleportControls(context, ctx, options?.fixedStep);
}
const handContext = getHandContext(handedness);
observe.pre(() => [handContext.enabled], ([enabled]) => {
teleportState[handedness].enabled = enabled;
});
observe.pre(() => [handContext.active], ([hovering]) => {
teleportState[handedness].hovering = hovering;
});
return {
enabled: handContext.enabled,
hovered: handContext.hovered,
active: handContext.active
};
};