@threlte/xr
Version:
Tools to more easily create VR and AR experiences with Threlte
46 lines (45 loc) • 1.7 kB
JavaScript
import { currentWritable, watch } from '@threlte/core';
import { createTeleportContext, useTeleportControls, getHandContext } from './context';
import { injectTeleportControlsPlugin } from './plugin.svelte';
import { setHandContext } from './context';
import { setupTeleportControls } from './setup';
import { teleportState } from '../../internal/stores';
let controlsCounter = 0;
export const teleportControls = (handedness, options) => {
if (useTeleportControls() === undefined) {
injectTeleportControlsPlugin();
createTeleportContext(options?.compute);
}
const context = useTeleportControls();
if (getHandContext(handedness) === undefined) {
const enabled = options?.enabled ?? true;
controlsCounter += enabled ? 1 : -1;
const ctx = {
hand: handedness,
active: currentWritable(false),
enabled: currentWritable(enabled),
hovered: currentWritable(undefined)
};
setHandContext(handedness, ctx);
setupTeleportControls(context, ctx, options?.fixedStep);
}
const handContext = getHandContext(handedness);
watch(handContext.enabled, (enabled) => {
controlsCounter += enabled ? 1 : -1;
teleportState.update((value) => {
value[handedness].enabled = controlsCounter > 0;
return value;
});
});
watch(handContext.active, (hovering) => {
teleportState.update((value) => {
value[handedness].hovering = hovering;
return value;
});
});
return {
enabled: handContext.enabled,
hovered: handContext.hovered,
active: handContext.active
};
};