@threlte/xr 1.4.0 → 1.5.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/XR.svelte +13 -3
- package/dist/components/XR.svelte.d.ts +6 -0
- package/dist/internal/setupControllers.d.ts +2 -1
- package/dist/internal/setupControllers.js +3 -3
- package/dist/internal/setupHands.d.ts +2 -1
- package/dist/internal/setupHands.js +3 -3
- package/dist/plugins/pointerControls/plugin.svelte.js +11 -0
- package/dist/plugins/pointerControls/setup.svelte.js +34 -16
- package/package.json +2 -2
|
@@ -19,6 +19,8 @@ This should be placed within a Threlte `<Canvas />`.
|
|
|
19
19
|
-->
|
|
20
20
|
<script lang="ts">
|
|
21
21
|
import type { EventListener, WebXRManager, Event as ThreeEvent } from 'three'
|
|
22
|
+
import type { XRHandModelFactory } from 'three/examples/jsm/webxr/XRHandModelFactory.js'
|
|
23
|
+
import type { XRControllerModelFactory } from 'three/examples/jsm/webxr/XRControllerModelFactory.js'
|
|
22
24
|
import type { Snippet } from 'svelte'
|
|
23
25
|
import { useThrelte } from '@threlte/core'
|
|
24
26
|
import {
|
|
@@ -65,6 +67,12 @@ This should be placed within a Threlte `<Canvas />`.
|
|
|
65
67
|
/** Called after an XRSession is ended */
|
|
66
68
|
onsessionend?: (event: XRSessionEvent) => void
|
|
67
69
|
|
|
70
|
+
/** Optionally provide custom XRHandModelFactory */
|
|
71
|
+
handFactory?: XRHandModelFactory
|
|
72
|
+
|
|
73
|
+
/** Optionally provide custom XRControllerModelFactory */
|
|
74
|
+
controllerFactory?: XRControllerModelFactory
|
|
75
|
+
|
|
68
76
|
/** Called when an XRSession is hidden or unfocused. */
|
|
69
77
|
onvisibilitychange?: (event: XRSessionEvent) => void
|
|
70
78
|
|
|
@@ -81,7 +89,9 @@ This should be placed within a Threlte `<Canvas />`.
|
|
|
81
89
|
onvisibilitychange,
|
|
82
90
|
oninputsourceschange,
|
|
83
91
|
fallback,
|
|
84
|
-
children
|
|
92
|
+
children,
|
|
93
|
+
handFactory,
|
|
94
|
+
controllerFactory
|
|
85
95
|
}: Props = $props()
|
|
86
96
|
|
|
87
97
|
const { renderer, renderMode } = useThrelte()
|
|
@@ -90,8 +100,8 @@ This should be placed within a Threlte `<Canvas />`.
|
|
|
90
100
|
|
|
91
101
|
setupRaf()
|
|
92
102
|
setupHeadset()
|
|
93
|
-
setupControllers()
|
|
94
|
-
setupHands()
|
|
103
|
+
setupControllers(controllerFactory)
|
|
104
|
+
setupHands(handFactory)
|
|
95
105
|
|
|
96
106
|
const handleSessionStart: EventListener<object, 'sessionstart', WebXRManager> = (event) => {
|
|
97
107
|
isPresenting.current = true
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { WebXRManager, Event as ThreeEvent } from 'three';
|
|
2
|
+
import type { XRHandModelFactory } from 'three/examples/jsm/webxr/XRHandModelFactory.js';
|
|
3
|
+
import type { XRControllerModelFactory } from 'three/examples/jsm/webxr/XRControllerModelFactory.js';
|
|
2
4
|
import type { Snippet } from 'svelte';
|
|
3
5
|
interface Props {
|
|
4
6
|
/**
|
|
@@ -26,6 +28,10 @@ interface Props {
|
|
|
26
28
|
onsessionstart?: (event: ThreeEvent<'sessionstart', WebXRManager>) => void;
|
|
27
29
|
/** Called after an XRSession is ended */
|
|
28
30
|
onsessionend?: (event: XRSessionEvent) => void;
|
|
31
|
+
/** Optionally provide custom XRHandModelFactory */
|
|
32
|
+
handFactory?: XRHandModelFactory;
|
|
33
|
+
/** Optionally provide custom XRControllerModelFactory */
|
|
34
|
+
controllerFactory?: XRControllerModelFactory;
|
|
29
35
|
/** Called when an XRSession is hidden or unfocused. */
|
|
30
36
|
onvisibilitychange?: (event: XRSessionEvent) => void;
|
|
31
37
|
/** Called when available inputsources change */
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { XRControllerModelFactory } from 'three/examples/jsm/webxr/XRControllerModelFactory.js';
|
|
2
|
+
export declare const setupControllers: (factory?: XRControllerModelFactory) => void;
|
|
@@ -4,17 +4,17 @@ import { onMount } from 'svelte';
|
|
|
4
4
|
import { useHandTrackingState } from './useHandTrackingState.js';
|
|
5
5
|
import { controllers } from '../hooks/useController.svelte.js';
|
|
6
6
|
import { controllerEvents } from './state.svelte.js';
|
|
7
|
-
export const setupControllers = () => {
|
|
8
|
-
const factory = new XRControllerModelFactory();
|
|
7
|
+
export const setupControllers = (factory) => {
|
|
9
8
|
const { xr } = useThrelte().renderer;
|
|
10
9
|
const hasHands = useHandTrackingState();
|
|
11
10
|
const targetRaySpaces = [xr.getController(0), xr.getController(1)];
|
|
12
11
|
const indexMap = new Map();
|
|
13
12
|
targetRaySpaces.forEach((targetRay, index) => {
|
|
13
|
+
const model = (factory ?? new XRControllerModelFactory()).createControllerModel(targetRay);
|
|
14
14
|
indexMap.set(targetRay, {
|
|
15
15
|
targetRay,
|
|
16
16
|
grip: xr.getControllerGrip(index),
|
|
17
|
-
model
|
|
17
|
+
model
|
|
18
18
|
});
|
|
19
19
|
});
|
|
20
20
|
onMount(() => {
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { XRHandModelFactory } from 'three/examples/jsm/webxr/XRHandModelFactory.js';
|
|
2
|
+
export declare const setupHands: (factory?: XRHandModelFactory) => void;
|
|
@@ -4,17 +4,17 @@ import { onMount } from 'svelte';
|
|
|
4
4
|
import { hands } from '../hooks/useHand.svelte.js';
|
|
5
5
|
import { useHandTrackingState } from './useHandTrackingState.js';
|
|
6
6
|
import { handEvents } from './state.svelte.js';
|
|
7
|
-
export const setupHands = () => {
|
|
8
|
-
const factory = new XRHandModelFactory();
|
|
7
|
+
export const setupHands = (factory) => {
|
|
9
8
|
const { xr } = useThrelte().renderer;
|
|
10
9
|
const hasHands = useHandTrackingState();
|
|
11
10
|
const handSpaces = [xr.getHand(0), xr.getHand(1)];
|
|
12
11
|
const map = new Map();
|
|
13
12
|
handSpaces.forEach((handSpace, index) => {
|
|
13
|
+
const model = (factory ?? new XRHandModelFactory()).createHandModel(handSpace, 'mesh');
|
|
14
14
|
map.set(handSpace, {
|
|
15
15
|
hand: handSpace,
|
|
16
16
|
targetRay: xr.getController(index),
|
|
17
|
-
model
|
|
17
|
+
model
|
|
18
18
|
});
|
|
19
19
|
});
|
|
20
20
|
onMount(() => {
|
|
@@ -11,6 +11,17 @@ export const injectPointerControlsPlugin = () => {
|
|
|
11
11
|
if (!hasEventHandlers)
|
|
12
12
|
return;
|
|
13
13
|
const { addInteractiveObject, removeInteractiveObject } = usePointerControls();
|
|
14
|
+
$effect.pre(() => {
|
|
15
|
+
const ref = args.ref;
|
|
16
|
+
const props = args.props;
|
|
17
|
+
const hasEventHandlers = events.some((eventName) => typeof props[eventName] === 'function');
|
|
18
|
+
if (!hasEventHandlers)
|
|
19
|
+
return;
|
|
20
|
+
addInteractiveObject(ref, props);
|
|
21
|
+
return () => {
|
|
22
|
+
removeInteractiveObject(ref);
|
|
23
|
+
};
|
|
24
|
+
});
|
|
14
25
|
observe.pre(() => [args.ref], ([ref]) => {
|
|
15
26
|
addInteractiveObject(ref, args.props);
|
|
16
27
|
return () => removeInteractiveObject(ref);
|
|
@@ -6,7 +6,7 @@ import { useHand } from '../../hooks/useHand.svelte.js';
|
|
|
6
6
|
import { useFixed } from '../../internal/useFixed.js';
|
|
7
7
|
import { isPresenting, pointerIntersection } from '../../internal/state.svelte.js';
|
|
8
8
|
const getIntersectionId = (intersection) => {
|
|
9
|
-
return `${(intersection.eventObject || intersection.object).uuid}
|
|
9
|
+
return `${(intersection.eventObject || intersection.object).uuid}|${intersection.index}|${intersection.instanceId}`;
|
|
10
10
|
};
|
|
11
11
|
const EPSILON = 0.0001;
|
|
12
12
|
export const setupPointerControls = (context, handContext, fixedStep = 1 / 40) => {
|
|
@@ -37,31 +37,49 @@ export const setupPointerControls = (context, handContext, fixedStep = 1 / 40) =
|
|
|
37
37
|
handleEvent('onclick', event);
|
|
38
38
|
};
|
|
39
39
|
function cancelPointer(intersections) {
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
if (handContext.hovered.size === 0)
|
|
41
|
+
return;
|
|
42
|
+
const toRemove = [];
|
|
43
|
+
for (const [id, hoveredObj] of handContext.hovered) {
|
|
44
|
+
// When no objects were hit or the hovered object wasn't found underneath the cursor
|
|
42
45
|
// we call pointerout and delete the object from the hovered elements map
|
|
43
46
|
if (intersections.length === 0 ||
|
|
44
47
|
!intersections.some((hit) => hit.object === hoveredObj.object &&
|
|
45
48
|
hit.index === hoveredObj.index &&
|
|
46
49
|
hit.instanceId === hoveredObj.instanceId)) {
|
|
47
|
-
|
|
48
|
-
handContext.hovered.delete(getIntersectionId(hoveredObj));
|
|
49
|
-
const events = dispatchers.get(eventObject);
|
|
50
|
-
if (events !== undefined) {
|
|
51
|
-
// Clear out intersects, they are outdated by now
|
|
52
|
-
const data = { ...hoveredObj, intersections };
|
|
53
|
-
events.onpointerout?.(data);
|
|
54
|
-
events.onpointerleave?.(data);
|
|
55
|
-
// Deal with cancelation
|
|
56
|
-
handContext.pointerOverTarget.set(false);
|
|
57
|
-
cancelPointer([]);
|
|
58
|
-
}
|
|
50
|
+
toRemove.push([id, hoveredObj]);
|
|
59
51
|
}
|
|
60
52
|
}
|
|
53
|
+
for (const [id, hoveredObj] of toRemove) {
|
|
54
|
+
const { eventObject } = hoveredObj;
|
|
55
|
+
handContext.hovered.delete(id);
|
|
56
|
+
const events = dispatchers.get(eventObject);
|
|
57
|
+
if (events !== undefined) {
|
|
58
|
+
// Clear out intersects, they are outdated by now
|
|
59
|
+
const data = { ...hoveredObj, intersections };
|
|
60
|
+
events.onpointerout?.(data);
|
|
61
|
+
events.onpointerleave?.(data);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (handContext.hovered.size === 0) {
|
|
65
|
+
handContext.pointerOverTarget.set(false);
|
|
66
|
+
}
|
|
61
67
|
}
|
|
62
68
|
const getHits = () => {
|
|
63
69
|
const intersections = [];
|
|
64
|
-
const
|
|
70
|
+
const rawHits = context.raycaster.intersectObjects(context.interactiveObjects, true);
|
|
71
|
+
const seen = new Set();
|
|
72
|
+
const hits = rawHits.filter((hit) => {
|
|
73
|
+
const key = hit.instanceId !== undefined
|
|
74
|
+
? `${hit.object.uuid}|${hit.instanceId}`
|
|
75
|
+
: hit.index !== undefined
|
|
76
|
+
? `${hit.object.uuid}|${hit.index}`
|
|
77
|
+
: hit.object.uuid;
|
|
78
|
+
if (seen.has(key))
|
|
79
|
+
return false;
|
|
80
|
+
seen.add(key);
|
|
81
|
+
return true;
|
|
82
|
+
});
|
|
65
83
|
const filtered = context.filter === undefined ? hits : context.filter(hits, context, handContext);
|
|
66
84
|
pointerIntersection[handedness] = filtered[0];
|
|
67
85
|
// Bubble up the events, find the event source (eventObject)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@threlte/xr",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"author": "Micheal Parks <michealparks1989@gmail.com> (https://parks.lol)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Tools to more easily create VR and AR experiences with Threlte",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"typescript-eslint": "^8.32.0",
|
|
29
29
|
"vite": "^7.1.4",
|
|
30
30
|
"vite-plugin-mkcert": "^1.17.5",
|
|
31
|
-
"@threlte/core": "8.5.
|
|
31
|
+
"@threlte/core": "8.5.4"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"svelte": ">=5",
|