@threlte/xr 1.0.8 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/ARButton.svelte.d.ts +11 -24
- package/dist/components/Controller.svelte +31 -27
- package/dist/components/Controller.svelte.d.ts +7 -5
- package/dist/components/Hand.svelte +24 -28
- package/dist/components/Hand.svelte.d.ts +6 -4
- package/dist/components/Headset.svelte.d.ts +4 -2
- package/dist/components/VRButton.svelte.d.ts +5 -18
- package/dist/components/XR.svelte +55 -54
- package/dist/components/XR.svelte.d.ts +29 -27
- package/dist/components/XRButton.svelte +3 -3
- package/dist/components/XRButton.svelte.d.ts +26 -23
- package/dist/components/internal/Cursor.svelte.d.ts +4 -2
- package/dist/components/internal/PointerCursor.svelte +8 -7
- package/dist/components/internal/PointerCursor.svelte.d.ts +5 -3
- package/dist/components/internal/ShortRay.svelte +3 -3
- package/dist/components/internal/ShortRay.svelte.d.ts +5 -3
- package/dist/components/internal/TeleportCursor.svelte +8 -7
- package/dist/components/internal/TeleportCursor.svelte.d.ts +5 -3
- package/dist/components/internal/TeleportRay.svelte +5 -5
- package/dist/components/internal/TeleportRay.svelte.d.ts +5 -3
- package/dist/hooks/currentReadable.svelte.d.ts +5 -0
- package/dist/hooks/currentReadable.svelte.js +11 -0
- package/dist/hooks/useController.svelte.d.ts +13 -0
- package/dist/hooks/useController.svelte.js +22 -0
- package/dist/hooks/useHand.svelte.d.ts +12 -0
- package/dist/hooks/{useHand.js → useHand.svelte.js} +8 -5
- package/dist/hooks/{useHandJoint.d.ts → useHandJoint.svelte.d.ts} +1 -1
- package/dist/hooks/useHandJoint.svelte.js +21 -0
- package/dist/hooks/useHeadset.js +1 -1
- package/dist/hooks/useHitTest.svelte.js +67 -0
- package/dist/hooks/useTeleport.js +1 -2
- package/dist/hooks/useXR.d.ts +6 -7
- package/dist/hooks/useXR.js +10 -12
- package/dist/index.d.ts +5 -5
- package/dist/index.js +4 -4
- package/dist/internal/raf.d.ts +1 -0
- package/dist/internal/raf.js +2 -0
- package/dist/internal/setupControllers.js +13 -14
- package/dist/internal/setupHands.js +11 -9
- package/dist/internal/{setupHeadset.js → setupHeadset.svelte.js} +6 -6
- package/dist/internal/setupRaf.svelte.js +17 -0
- package/dist/internal/state.svelte.d.ts +51 -0
- package/dist/internal/state.svelte.js +40 -0
- package/dist/internal/useHandTrackingState.js +10 -7
- package/dist/lib/getXRSessionOptions.d.ts +1 -1
- package/dist/lib/toggleXRSession.d.ts +3 -3
- package/dist/lib/toggleXRSession.js +3 -3
- package/dist/plugins/pointerControls/compute.js +2 -6
- package/dist/plugins/pointerControls/index.js +4 -10
- package/dist/plugins/pointerControls/{setup.js → setup.svelte.js} +18 -19
- package/dist/plugins/teleportControls/compute.js +2 -6
- package/dist/plugins/teleportControls/index.js +4 -10
- package/dist/plugins/teleportControls/{setup.js → setup.svelte.js} +9 -10
- package/dist/types.d.ts +12 -18
- package/package.json +10 -10
- package/dist/hooks/useController.d.ts +0 -9
- package/dist/hooks/useController.js +0 -19
- package/dist/hooks/useHand.d.ts +0 -8
- package/dist/hooks/useHandJoint.js +0 -20
- package/dist/hooks/useHitTest.js +0 -79
- package/dist/internal/setupRaf.js +0 -16
- package/dist/internal/stores.d.ts +0 -43
- package/dist/internal/stores.js +0 -42
- /package/dist/hooks/{useHitTest.d.ts → useHitTest.svelte.d.ts} +0 -0
- /package/dist/internal/{setupHeadset.d.ts → setupHeadset.svelte.d.ts} +0 -0
- /package/dist/internal/{setupRaf.d.ts → setupRaf.svelte.d.ts} +0 -0
- /package/dist/plugins/pointerControls/{setup.d.ts → setup.svelte.d.ts} +0 -0
- /package/dist/plugins/teleportControls/{setup.d.ts → setup.svelte.d.ts} +0 -0
|
@@ -2,16 +2,15 @@ import { XRControllerModelFactory } from 'three/examples/jsm/webxr/XRControllerM
|
|
|
2
2
|
import { useThrelte } from '@threlte/core';
|
|
3
3
|
import { onMount } from 'svelte';
|
|
4
4
|
import { useHandTrackingState } from './useHandTrackingState';
|
|
5
|
-
import {
|
|
6
|
-
import { controllerEvents } from './
|
|
5
|
+
import { controllers } from '../hooks/useController.svelte';
|
|
6
|
+
import { controllerEvents } from './state.svelte';
|
|
7
7
|
export const setupControllers = () => {
|
|
8
8
|
const factory = new XRControllerModelFactory();
|
|
9
|
-
const stores = { left, right, none: gaze };
|
|
10
9
|
const { xr } = useThrelte().renderer;
|
|
11
10
|
const hasHands = useHandTrackingState();
|
|
12
|
-
const
|
|
11
|
+
const targetRaySpaces = [xr.getController(0), xr.getController(1)];
|
|
13
12
|
const indexMap = new Map();
|
|
14
|
-
|
|
13
|
+
targetRaySpaces.forEach((targetRay, index) => {
|
|
15
14
|
indexMap.set(targetRay, {
|
|
16
15
|
targetRay,
|
|
17
16
|
grip: xr.getControllerGrip(index),
|
|
@@ -23,24 +22,24 @@ export const setupControllers = () => {
|
|
|
23
22
|
if (hasHands())
|
|
24
23
|
return;
|
|
25
24
|
const { data } = event;
|
|
26
|
-
controllerEvents[data.handedness]?.
|
|
25
|
+
controllerEvents[data.handedness]?.[`on${event.type}`]?.(event);
|
|
27
26
|
};
|
|
28
27
|
function handleConnected(event) {
|
|
29
28
|
const { model, targetRay, grip } = indexMap.get(this);
|
|
30
29
|
const { data: inputSource } = event;
|
|
31
|
-
|
|
30
|
+
controllers[event.data.handedness] = {
|
|
32
31
|
inputSource,
|
|
33
32
|
targetRay,
|
|
34
33
|
grip,
|
|
35
34
|
model
|
|
36
|
-
}
|
|
35
|
+
};
|
|
37
36
|
dispatch(event);
|
|
38
37
|
}
|
|
39
38
|
const handleDisconnected = (event) => {
|
|
40
39
|
dispatch(event);
|
|
41
|
-
|
|
40
|
+
controllers[event.data.handedness] = undefined;
|
|
42
41
|
};
|
|
43
|
-
for (const targetRay of
|
|
42
|
+
for (const targetRay of targetRaySpaces) {
|
|
44
43
|
targetRay.addEventListener('connected', handleConnected);
|
|
45
44
|
targetRay.addEventListener('disconnected', handleDisconnected);
|
|
46
45
|
targetRay.addEventListener('select', dispatch);
|
|
@@ -51,7 +50,7 @@ export const setupControllers = () => {
|
|
|
51
50
|
targetRay.addEventListener('squeezeend', dispatch);
|
|
52
51
|
}
|
|
53
52
|
return () => {
|
|
54
|
-
for (const targetRay of
|
|
53
|
+
for (const targetRay of targetRaySpaces) {
|
|
55
54
|
targetRay.removeEventListener('connected', handleConnected);
|
|
56
55
|
targetRay.removeEventListener('disconnected', handleDisconnected);
|
|
57
56
|
targetRay.removeEventListener('select', dispatch);
|
|
@@ -61,9 +60,9 @@ export const setupControllers = () => {
|
|
|
61
60
|
targetRay.removeEventListener('squeezestart', dispatch);
|
|
62
61
|
targetRay.removeEventListener('squeezeend', dispatch);
|
|
63
62
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
controllers.left = undefined;
|
|
64
|
+
controllers.right = undefined;
|
|
65
|
+
controllers.none = undefined;
|
|
67
66
|
};
|
|
68
67
|
});
|
|
69
68
|
};
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { XRHandModelFactory } from 'three/examples/jsm/webxr/XRHandModelFactory.js';
|
|
2
2
|
import { useThrelte } from '@threlte/core';
|
|
3
3
|
import { onMount } from 'svelte';
|
|
4
|
-
import {
|
|
4
|
+
import { hands } from '../hooks/useHand.svelte';
|
|
5
5
|
import { useHandTrackingState } from './useHandTrackingState';
|
|
6
|
-
import { handEvents } from './
|
|
6
|
+
import { handEvents } from './state.svelte';
|
|
7
7
|
export const setupHands = () => {
|
|
8
8
|
const factory = new XRHandModelFactory();
|
|
9
|
-
const stores = { left, right };
|
|
10
9
|
const { xr } = useThrelte().renderer;
|
|
11
10
|
const hasHands = useHandTrackingState();
|
|
12
11
|
const handSpaces = [xr.getHand(0), xr.getHand(1)];
|
|
@@ -24,23 +23,26 @@ export const setupHands = () => {
|
|
|
24
23
|
return;
|
|
25
24
|
const handEvent = event;
|
|
26
25
|
const handedness = 'handedness' in handEvent ? handEvent.handedness : handEvent.data.handedness;
|
|
27
|
-
handEvents[handedness]?.
|
|
26
|
+
handEvents[handedness]?.[`on${event.type}`]?.(event);
|
|
28
27
|
};
|
|
29
28
|
function handleConnected(event) {
|
|
30
29
|
const { model, targetRay } = map.get(this);
|
|
31
30
|
const { data } = event;
|
|
32
31
|
const { handedness, hand: inputSource } = data;
|
|
33
|
-
|
|
32
|
+
if (handedness === 'none' || inputSource === undefined) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
hands[handedness] = {
|
|
34
36
|
hand: this,
|
|
35
37
|
model,
|
|
36
38
|
inputSource,
|
|
37
39
|
targetRay
|
|
38
|
-
}
|
|
40
|
+
};
|
|
39
41
|
dispatch(event);
|
|
40
42
|
}
|
|
41
43
|
const handleDisconnected = (event) => {
|
|
42
44
|
dispatch(event);
|
|
43
|
-
|
|
45
|
+
hands[event.data.handedness] = undefined;
|
|
44
46
|
};
|
|
45
47
|
for (const handSpace of handSpaces) {
|
|
46
48
|
handSpace.addEventListener('connected', handleConnected);
|
|
@@ -55,8 +57,8 @@ export const setupHands = () => {
|
|
|
55
57
|
handSpace.removeEventListener('pinchstart', dispatch);
|
|
56
58
|
handSpace.removeEventListener('pinchend', dispatch);
|
|
57
59
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
+
hands.left = undefined;
|
|
61
|
+
hands.right = undefined;
|
|
60
62
|
};
|
|
61
63
|
});
|
|
62
64
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Group } from 'three';
|
|
2
|
-
import { useThrelte, useTask
|
|
3
|
-
import {
|
|
2
|
+
import { useThrelte, useTask } from '@threlte/core';
|
|
3
|
+
import { isPresenting } from './state.svelte';
|
|
4
4
|
export const headset = new Group();
|
|
5
5
|
export const setupHeadset = () => {
|
|
6
6
|
const { renderer, camera, scheduler, renderStage } = useThrelte();
|
|
@@ -16,8 +16,8 @@ export const setupHeadset = () => {
|
|
|
16
16
|
if (pose === undefined || pose === null)
|
|
17
17
|
return;
|
|
18
18
|
const { position, orientation } = pose.transform;
|
|
19
|
-
headset.position.
|
|
20
|
-
headset.quaternion.
|
|
19
|
+
headset.position.copy(position);
|
|
20
|
+
headset.quaternion.copy(orientation);
|
|
21
21
|
}, {
|
|
22
22
|
autoStart: false,
|
|
23
23
|
autoInvalidate: false,
|
|
@@ -31,8 +31,8 @@ export const setupHeadset = () => {
|
|
|
31
31
|
autoInvalidate: false,
|
|
32
32
|
stage
|
|
33
33
|
});
|
|
34
|
-
|
|
35
|
-
if (
|
|
34
|
+
$effect.pre(() => {
|
|
35
|
+
if (isPresenting.current) {
|
|
36
36
|
immersiveFrame.start();
|
|
37
37
|
nonImmersiveFrame.stop();
|
|
38
38
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { session } from './state.svelte';
|
|
2
|
+
import { raf } from './raf';
|
|
3
|
+
export const setupRaf = () => {
|
|
4
|
+
if (typeof window === 'undefined')
|
|
5
|
+
return;
|
|
6
|
+
$effect.pre(() => {
|
|
7
|
+
const currentSession = session.current;
|
|
8
|
+
if (currentSession === undefined) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const originalTick = raf.tick;
|
|
12
|
+
raf.tick = (fn) => currentSession.requestAnimationFrame(fn);
|
|
13
|
+
return () => {
|
|
14
|
+
raf.tick = originalTick;
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { WebXRManager, Intersection } from 'three';
|
|
2
|
+
import type { XRControllerEvents, XRHandEvents } from '../types';
|
|
3
|
+
interface ControllerEvents {
|
|
4
|
+
left?: XRControllerEvents;
|
|
5
|
+
right?: XRControllerEvents;
|
|
6
|
+
}
|
|
7
|
+
interface HandEvents {
|
|
8
|
+
left?: XRHandEvents;
|
|
9
|
+
right?: XRHandEvents;
|
|
10
|
+
}
|
|
11
|
+
declare class Presenting {
|
|
12
|
+
current: boolean;
|
|
13
|
+
}
|
|
14
|
+
declare class IsHandTracking {
|
|
15
|
+
current: boolean;
|
|
16
|
+
}
|
|
17
|
+
declare class Session {
|
|
18
|
+
current: XRSession | undefined;
|
|
19
|
+
}
|
|
20
|
+
declare class ReferenceSpaceType {
|
|
21
|
+
current: XRReferenceSpaceType | undefined;
|
|
22
|
+
}
|
|
23
|
+
declare class XR {
|
|
24
|
+
current: WebXRManager | undefined;
|
|
25
|
+
}
|
|
26
|
+
declare class PointerState {
|
|
27
|
+
enabled: boolean;
|
|
28
|
+
hovering: boolean;
|
|
29
|
+
}
|
|
30
|
+
declare class IntersectionState {
|
|
31
|
+
left: Intersection<import("three").Object3D<import("three").Object3DEventMap>> | undefined;
|
|
32
|
+
right: Intersection<import("three").Object3D<import("three").Object3DEventMap>> | undefined;
|
|
33
|
+
}
|
|
34
|
+
export declare const isPresenting: Presenting;
|
|
35
|
+
export declare const isHandTracking: IsHandTracking;
|
|
36
|
+
export declare const session: Session;
|
|
37
|
+
export declare const referenceSpaceType: ReferenceSpaceType;
|
|
38
|
+
export declare const xr: XR;
|
|
39
|
+
export declare const controllerEvents: ControllerEvents;
|
|
40
|
+
export declare const handEvents: HandEvents;
|
|
41
|
+
export declare const teleportState: {
|
|
42
|
+
left: PointerState;
|
|
43
|
+
right: PointerState;
|
|
44
|
+
};
|
|
45
|
+
export declare const pointerState: {
|
|
46
|
+
left: PointerState;
|
|
47
|
+
right: PointerState;
|
|
48
|
+
};
|
|
49
|
+
export declare const teleportIntersection: IntersectionState;
|
|
50
|
+
export declare const pointerIntersection: IntersectionState;
|
|
51
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
class Presenting {
|
|
2
|
+
current = $state(false);
|
|
3
|
+
}
|
|
4
|
+
class IsHandTracking {
|
|
5
|
+
current = $state(false);
|
|
6
|
+
}
|
|
7
|
+
class Session {
|
|
8
|
+
current = $state.raw();
|
|
9
|
+
}
|
|
10
|
+
class ReferenceSpaceType {
|
|
11
|
+
current = $state.raw();
|
|
12
|
+
}
|
|
13
|
+
class XR {
|
|
14
|
+
current = $state.raw();
|
|
15
|
+
}
|
|
16
|
+
class PointerState {
|
|
17
|
+
enabled = $state(false);
|
|
18
|
+
hovering = $state(false);
|
|
19
|
+
}
|
|
20
|
+
class IntersectionState {
|
|
21
|
+
left = $state.raw();
|
|
22
|
+
right = $state.raw();
|
|
23
|
+
}
|
|
24
|
+
export const isPresenting = new Presenting();
|
|
25
|
+
export const isHandTracking = new IsHandTracking();
|
|
26
|
+
export const session = new Session();
|
|
27
|
+
export const referenceSpaceType = new ReferenceSpaceType();
|
|
28
|
+
export const xr = new XR();
|
|
29
|
+
export const controllerEvents = {};
|
|
30
|
+
export const handEvents = {};
|
|
31
|
+
export const teleportState = {
|
|
32
|
+
left: new PointerState(),
|
|
33
|
+
right: new PointerState()
|
|
34
|
+
};
|
|
35
|
+
export const pointerState = {
|
|
36
|
+
left: new PointerState(),
|
|
37
|
+
right: new PointerState()
|
|
38
|
+
};
|
|
39
|
+
export const teleportIntersection = new IntersectionState();
|
|
40
|
+
export const pointerIntersection = new IntersectionState();
|
|
@@ -4,14 +4,17 @@ import { useThrelte } from '@threlte/core';
|
|
|
4
4
|
* connection or disconnection event. This is the way to do that.
|
|
5
5
|
*/
|
|
6
6
|
export const useHandTrackingState = () => {
|
|
7
|
-
const {
|
|
7
|
+
const { renderer } = useThrelte();
|
|
8
8
|
return () => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
const sources = renderer.xr.getSession()?.inputSources;
|
|
10
|
+
if (sources === undefined) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
for (const source of sources) {
|
|
14
|
+
if (source.hand !== undefined) {
|
|
15
|
+
return true;
|
|
13
16
|
}
|
|
14
|
-
}
|
|
15
|
-
return
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
16
19
|
};
|
|
17
20
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const getXRSessionOptions: (referenceSpaceType
|
|
1
|
+
export declare const getXRSessionOptions: (referenceSpaceType?: XRReferenceSpaceType, sessionInit?: XRSessionInit) => XRSessionInit | undefined;
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* @param force Whether this button should only enter / exit an `XRSession`. Default is to toggle both ways
|
|
7
7
|
* @returns
|
|
8
8
|
*/
|
|
9
|
-
export declare const toggleXRSession: (sessionMode: XRSessionMode, sessionInit?:
|
|
9
|
+
export declare const toggleXRSession: (sessionMode: XRSessionMode, sessionInit?: XRSessionInit & {
|
|
10
10
|
domOverlay?: {
|
|
11
11
|
root: HTMLElement;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
12
|
+
};
|
|
13
|
+
}, force?: "enter" | "exit") => Promise<XRSession | undefined>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { session, referenceSpaceType, xr } from '../internal/
|
|
1
|
+
import { session, referenceSpaceType, xr } from '../internal/state.svelte';
|
|
2
2
|
import { getXRSessionOptions } from './getXRSessionOptions';
|
|
3
3
|
/**
|
|
4
4
|
* Starts / ends an XR session.
|
|
@@ -18,7 +18,7 @@ export const toggleXRSession = async (sessionMode, sessionInit, force) => {
|
|
|
18
18
|
// Exit a session if entered
|
|
19
19
|
if (hasSession) {
|
|
20
20
|
await currentSession.end();
|
|
21
|
-
session.
|
|
21
|
+
session.current = undefined;
|
|
22
22
|
return;
|
|
23
23
|
}
|
|
24
24
|
if (xr.current === undefined) {
|
|
@@ -28,6 +28,6 @@ export const toggleXRSession = async (sessionMode, sessionInit, force) => {
|
|
|
28
28
|
const options = getXRSessionOptions(referenceSpaceType.current, sessionInit);
|
|
29
29
|
const nextSession = await navigator.xr.requestSession(sessionMode, options);
|
|
30
30
|
await xr.current.setSession(nextSession);
|
|
31
|
-
session.
|
|
31
|
+
session.current = nextSession;
|
|
32
32
|
return nextSession;
|
|
33
33
|
};
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import { Vector3 } from 'three';
|
|
2
|
-
import {
|
|
3
|
-
const controllers = {
|
|
4
|
-
left: useController('left'),
|
|
5
|
-
right: useController('right')
|
|
6
|
-
};
|
|
2
|
+
import { controllers } from '../../hooks/useController.svelte';
|
|
7
3
|
const forward = new Vector3();
|
|
8
4
|
export const defaultComputeFunction = (context, handContext) => {
|
|
9
|
-
const targetRay = controllers[handContext.hand]
|
|
5
|
+
const targetRay = controllers[handContext.hand]?.targetRay;
|
|
10
6
|
if (targetRay === undefined)
|
|
11
7
|
return;
|
|
12
8
|
forward.set(0, 0, -1).applyQuaternion(targetRay.quaternion);
|
|
@@ -2,9 +2,9 @@ import { Raycaster, Vector3 } from 'three';
|
|
|
2
2
|
import { currentWritable, watch } from '@threlte/core';
|
|
3
3
|
import { defaultComputeFunction } from './compute';
|
|
4
4
|
import { injectPointerControlsPlugin } from './plugin.svelte';
|
|
5
|
-
import { setupPointerControls } from './setup';
|
|
5
|
+
import { setupPointerControls } from './setup.svelte';
|
|
6
6
|
import { getControlsContext, getHandContext, setControlsContext, setHandContext, setInternalContext } from './context';
|
|
7
|
-
import { pointerState } from '../../internal/
|
|
7
|
+
import { pointerState } from '../../internal/state.svelte';
|
|
8
8
|
let controlsCounter = 0;
|
|
9
9
|
export const pointerControls = (handedness, options) => {
|
|
10
10
|
if (getControlsContext() === undefined) {
|
|
@@ -36,16 +36,10 @@ export const pointerControls = (handedness, options) => {
|
|
|
36
36
|
const handContext = getHandContext(handedness);
|
|
37
37
|
watch(handContext.enabled, (enabled) => {
|
|
38
38
|
controlsCounter += enabled ? 1 : -1;
|
|
39
|
-
pointerState.
|
|
40
|
-
value[handedness].enabled = controlsCounter > 0;
|
|
41
|
-
return value;
|
|
42
|
-
});
|
|
39
|
+
pointerState[handedness].enabled = controlsCounter > 0;
|
|
43
40
|
});
|
|
44
41
|
watch(handContext.pointerOverTarget, (hovering) => {
|
|
45
|
-
pointerState.
|
|
46
|
-
value[handedness].hovering = hovering;
|
|
47
|
-
return value;
|
|
48
|
-
});
|
|
42
|
+
pointerState[handedness].hovering = hovering;
|
|
49
43
|
});
|
|
50
44
|
return {
|
|
51
45
|
enabled: handContext.enabled,
|
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
import { Vector3 } from 'three';
|
|
2
|
-
import { watch } from '@threlte/core';
|
|
2
|
+
import { observe, watch } from '@threlte/core';
|
|
3
3
|
import { getInternalContext } from './context';
|
|
4
|
-
import {
|
|
5
|
-
import { useHand } from '../../hooks/useHand';
|
|
6
|
-
import { useXR } from '../../hooks/useXR';
|
|
4
|
+
import { controllers } from '../../hooks/useController.svelte';
|
|
5
|
+
import { useHand } from '../../hooks/useHand.svelte';
|
|
7
6
|
import { useFixed } from '../../internal/useFixed';
|
|
8
|
-
import { pointerIntersection } from '../../internal/
|
|
7
|
+
import { isPresenting, pointerIntersection } from '../../internal/state.svelte';
|
|
9
8
|
const getIntersectionId = (intersection) => {
|
|
10
9
|
return `${(intersection.eventObject || intersection.object).uuid}/${intersection.index}${intersection.instanceId ?? ''}`;
|
|
11
10
|
};
|
|
12
11
|
const EPSILON = 0.0001;
|
|
13
12
|
export const setupPointerControls = (context, handContext, fixedStep = 1 / 40) => {
|
|
14
13
|
const handedness = handContext.hand;
|
|
15
|
-
const controller =
|
|
14
|
+
const controller = $derived(controllers[handedness]);
|
|
16
15
|
const hand = useHand(handedness);
|
|
17
16
|
const { dispatchers } = getInternalContext();
|
|
18
17
|
let hits = [];
|
|
@@ -64,7 +63,7 @@ export const setupPointerControls = (context, handContext, fixedStep = 1 / 40) =
|
|
|
64
63
|
const intersections = [];
|
|
65
64
|
const hits = context.raycaster.intersectObjects(context.interactiveObjects, true);
|
|
66
65
|
const filtered = context.filter === undefined ? hits : context.filter(hits, context, handContext);
|
|
67
|
-
pointerIntersection[handedness]
|
|
66
|
+
pointerIntersection[handedness] = filtered[0];
|
|
68
67
|
// Bubble up the events, find the event source (eventObject)
|
|
69
68
|
for (const hit of filtered) {
|
|
70
69
|
let eventObject = hit.object;
|
|
@@ -158,7 +157,7 @@ export const setupPointerControls = (context, handContext, fixedStep = 1 / 40) =
|
|
|
158
157
|
};
|
|
159
158
|
const { start, stop } = useFixed(() => {
|
|
160
159
|
hits = processHits();
|
|
161
|
-
const targetRay = controller
|
|
160
|
+
const targetRay = controller?.targetRay;
|
|
162
161
|
if (targetRay === undefined)
|
|
163
162
|
return;
|
|
164
163
|
if (targetRay.position.distanceTo(lastPosition) > EPSILON) {
|
|
@@ -169,18 +168,18 @@ export const setupPointerControls = (context, handContext, fixedStep = 1 / 40) =
|
|
|
169
168
|
fixedStep,
|
|
170
169
|
autoStart: false
|
|
171
170
|
});
|
|
172
|
-
|
|
173
|
-
if (
|
|
171
|
+
observe.pre(() => [controller, handContext.enabled], ([controller, $enabled]) => {
|
|
172
|
+
if (controller === undefined)
|
|
174
173
|
return;
|
|
175
174
|
const removeHandlers = () => {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
175
|
+
controller.targetRay.removeEventListener('selectstart', handlePointerDown);
|
|
176
|
+
controller.targetRay.removeEventListener('selectend', handlePointerUp);
|
|
177
|
+
controller.targetRay.removeEventListener('select', handleClick);
|
|
179
178
|
};
|
|
180
|
-
if (enabled) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
179
|
+
if ($enabled) {
|
|
180
|
+
controller.targetRay.addEventListener('selectstart', handlePointerDown);
|
|
181
|
+
controller.targetRay.addEventListener('selectend', handlePointerUp);
|
|
182
|
+
controller.targetRay.addEventListener('select', handleClick);
|
|
184
183
|
return removeHandlers;
|
|
185
184
|
}
|
|
186
185
|
else {
|
|
@@ -207,8 +206,8 @@ export const setupPointerControls = (context, handContext, fixedStep = 1 / 40) =
|
|
|
207
206
|
return;
|
|
208
207
|
}
|
|
209
208
|
});
|
|
210
|
-
|
|
211
|
-
if (isPresenting && enabled) {
|
|
209
|
+
observe.pre(() => [isPresenting.current, handContext.enabled], ([isPresenting, $enabled]) => {
|
|
210
|
+
if (isPresenting && $enabled) {
|
|
212
211
|
start();
|
|
213
212
|
}
|
|
214
213
|
else {
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import { Vector3 } from 'three';
|
|
2
|
-
import {
|
|
3
|
-
const controllers = {
|
|
4
|
-
left: useController('left'),
|
|
5
|
-
right: useController('right')
|
|
6
|
-
};
|
|
2
|
+
import { controllers } from '../../hooks/useController.svelte';
|
|
7
3
|
const forward = new Vector3();
|
|
8
4
|
export const defaultComputeFunction = (context, handContext) => {
|
|
9
|
-
const targetRay = controllers[handContext.hand]
|
|
5
|
+
const targetRay = controllers[handContext.hand]?.targetRay;
|
|
10
6
|
if (targetRay === undefined)
|
|
11
7
|
return;
|
|
12
8
|
forward.set(0, 0, -1).applyQuaternion(targetRay.quaternion);
|
|
@@ -2,8 +2,8 @@ import { currentWritable, watch } from '@threlte/core';
|
|
|
2
2
|
import { createTeleportContext, useTeleportControls, getHandContext } from './context';
|
|
3
3
|
import { injectTeleportControlsPlugin } from './plugin.svelte';
|
|
4
4
|
import { setHandContext } from './context';
|
|
5
|
-
import { setupTeleportControls } from './setup';
|
|
6
|
-
import { teleportState } from '../../internal/
|
|
5
|
+
import { setupTeleportControls } from './setup.svelte';
|
|
6
|
+
import { teleportState } from '../../internal/state.svelte';
|
|
7
7
|
let controlsCounter = 0;
|
|
8
8
|
export const teleportControls = (handedness, options) => {
|
|
9
9
|
if (useTeleportControls() === undefined) {
|
|
@@ -26,16 +26,10 @@ export const teleportControls = (handedness, options) => {
|
|
|
26
26
|
const handContext = getHandContext(handedness);
|
|
27
27
|
watch(handContext.enabled, (enabled) => {
|
|
28
28
|
controlsCounter += enabled ? 1 : -1;
|
|
29
|
-
teleportState.
|
|
30
|
-
value[handedness].enabled = controlsCounter > 0;
|
|
31
|
-
return value;
|
|
32
|
-
});
|
|
29
|
+
teleportState[handedness].enabled = controlsCounter > 0;
|
|
33
30
|
});
|
|
34
31
|
watch(handContext.active, (hovering) => {
|
|
35
|
-
teleportState.
|
|
36
|
-
value[handedness].hovering = hovering;
|
|
37
|
-
return value;
|
|
38
|
-
});
|
|
32
|
+
teleportState[handedness].hovering = hovering;
|
|
39
33
|
});
|
|
40
34
|
return {
|
|
41
35
|
enabled: handContext.enabled,
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { useController } from '../../hooks/useController';
|
|
1
|
+
import { observe } from '@threlte/core';
|
|
2
|
+
import { controllers } from '../../hooks/useController.svelte';
|
|
4
3
|
import { useTeleport } from '../../hooks/useTeleport';
|
|
5
4
|
import { useFixed } from '../../internal/useFixed';
|
|
6
|
-
import { teleportIntersection } from '../../internal/
|
|
5
|
+
import { isPresenting, teleportIntersection } from '../../internal/state.svelte';
|
|
7
6
|
export const setupTeleportControls = (context, handContext, fixedStep = 1 / 40) => {
|
|
8
7
|
const handedness = handContext.hand;
|
|
9
|
-
const controller =
|
|
8
|
+
const controller = $derived(controllers[handedness]);
|
|
10
9
|
const teleport = useTeleport();
|
|
11
10
|
const handleHoverEnd = () => {
|
|
12
11
|
handContext.hovered.set(undefined);
|
|
13
|
-
teleportIntersection[handedness]
|
|
12
|
+
teleportIntersection[handedness] = undefined;
|
|
14
13
|
};
|
|
15
14
|
const { start, stop } = useFixed(() => {
|
|
16
|
-
const gamepad = controller
|
|
15
|
+
const gamepad = controller?.inputSource.gamepad;
|
|
17
16
|
if (gamepad === undefined) {
|
|
18
17
|
return;
|
|
19
18
|
}
|
|
@@ -45,14 +44,14 @@ export const setupTeleportControls = (context, handContext, fixedStep = 1 / 40)
|
|
|
45
44
|
}
|
|
46
45
|
return;
|
|
47
46
|
}
|
|
48
|
-
teleportIntersection[handedness]
|
|
47
|
+
teleportIntersection[handedness] = intersect;
|
|
49
48
|
handContext.hovered.set(intersect);
|
|
50
49
|
}, {
|
|
51
50
|
fixedStep,
|
|
52
51
|
autoStart: false
|
|
53
52
|
});
|
|
54
|
-
|
|
55
|
-
if (isPresenting && enabled) {
|
|
53
|
+
observe.pre(() => [isPresenting.current, handContext.enabled], ([isPresenting, $enabled]) => {
|
|
54
|
+
if (isPresenting && $enabled) {
|
|
56
55
|
start();
|
|
57
56
|
}
|
|
58
57
|
else {
|
package/dist/types.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { XRControllerModel } from 'three/examples/jsm/webxr/XRControllerMod
|
|
|
3
3
|
import type { XRHandModel } from 'three/examples/jsm/webxr/XRHandModelFactory.js';
|
|
4
4
|
export type XRSessionEventType = 'sessionstart' | 'sessionend' | 'visibilitychange' | 'frameratechange';
|
|
5
5
|
export type XRControllerEventType = 'select' | 'selectstart' | 'selectend' | 'squeeze' | 'squeezeend' | 'squeezestart' | 'disconnected' | 'connected';
|
|
6
|
-
export
|
|
6
|
+
export interface XRControllerEvents {
|
|
7
7
|
onconnected?: XRControllerEventCallback<'connected'>;
|
|
8
8
|
ondisconnected?: XRControllerEventCallback<'disconnected'>;
|
|
9
9
|
onselect?: XRControllerEventCallback<'select'>;
|
|
@@ -12,30 +12,24 @@ export type XRControllerEvents = {
|
|
|
12
12
|
onsqueeze?: XRControllerEventCallback<'squeeze'>;
|
|
13
13
|
onsqueezeend?: XRControllerEventCallback<'squeezeend'>;
|
|
14
14
|
onsqueezestart?: XRControllerEventCallback<'squeezestart'>;
|
|
15
|
-
}
|
|
15
|
+
}
|
|
16
16
|
export type XRHandEventType = 'pinchstart' | 'pinchend' | 'connected' | 'disconnected';
|
|
17
|
-
export
|
|
18
|
-
type: Type;
|
|
19
|
-
target: XRSession;
|
|
20
|
-
}) => void;
|
|
21
|
-
export type XRControllerEvent<Type = XRControllerEventType> = Event & {
|
|
22
|
-
type: Type;
|
|
23
|
-
target: Group;
|
|
17
|
+
export interface XRControllerEvent<Type extends string = XRControllerEventType> extends Event<Type, Group> {
|
|
24
18
|
data: XRInputSource;
|
|
25
|
-
}
|
|
26
|
-
export type XRControllerEventCallback<Type = XRControllerEventType> = (event: XRControllerEvent<Type>) => void;
|
|
27
|
-
export
|
|
19
|
+
}
|
|
20
|
+
export type XRControllerEventCallback<Type extends string = XRControllerEventType> = (event: XRControllerEvent<Type>) => void;
|
|
21
|
+
export interface XRController {
|
|
28
22
|
targetRay: XRTargetRaySpace;
|
|
29
23
|
grip: XRGripSpace;
|
|
30
24
|
model?: XRControllerModel | undefined;
|
|
31
25
|
inputSource: XRInputSource;
|
|
32
|
-
}
|
|
33
|
-
export
|
|
26
|
+
}
|
|
27
|
+
export interface XRHandObject {
|
|
34
28
|
targetRay: XRTargetRaySpace;
|
|
35
29
|
hand: XRHandSpace;
|
|
36
30
|
model?: XRHandModel;
|
|
37
|
-
inputSource:
|
|
38
|
-
}
|
|
31
|
+
inputSource: XRHand;
|
|
32
|
+
}
|
|
39
33
|
export type XRHandEvent<Type = XRHandEventType> = Type extends 'connected' | 'disconnected' ? {
|
|
40
34
|
type: Type;
|
|
41
35
|
target: XRHandSpace;
|
|
@@ -46,9 +40,9 @@ export type XRHandEvent<Type = XRHandEventType> = Type extends 'connected' | 'di
|
|
|
46
40
|
target: null;
|
|
47
41
|
} : never;
|
|
48
42
|
export type XRHandEventCallback<Type> = (event: XRHandEvent<Type>) => void;
|
|
49
|
-
export
|
|
43
|
+
export interface XRHandEvents {
|
|
50
44
|
onconnected?: XRHandEventCallback<'connected'>;
|
|
51
45
|
ondisconnected?: XRHandEventCallback<'disconnected'>;
|
|
52
46
|
onpinchstart?: XRHandEventCallback<'pinchstart'>;
|
|
53
47
|
onpinchend?: XRHandEventCallback<'pinchend'>;
|
|
54
|
-
}
|
|
48
|
+
}
|