@threlte/xr 0.0.8 → 0.0.10
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 +3 -2
- package/dist/components/ARButton.svelte.d.ts +7 -0
- package/dist/components/VRButton.svelte +2 -2
- package/dist/components/VRButton.svelte.d.ts +1 -0
- package/dist/components/XRButton.svelte +6 -3
- package/dist/components/XRButton.svelte.d.ts +1 -0
- package/dist/hooks/useHitTest.d.ts +21 -3
- package/dist/hooks/useHitTest.js +58 -33
- package/dist/internal/headset.js +3 -1
- package/dist/types.d.ts +0 -1
- package/package.json +2 -2
|
@@ -14,13 +14,14 @@
|
|
|
14
14
|
</script>
|
|
15
15
|
|
|
16
16
|
<XRButton
|
|
17
|
-
{...$$restProps}
|
|
18
|
-
mode='immersive-ar'
|
|
19
17
|
sessionInit={{
|
|
20
18
|
domOverlay: typeof document !== 'undefined' ? { root: document.body } : undefined,
|
|
21
19
|
requiredFeatures: ['plane-detection'],
|
|
22
20
|
optionalFeatures: ['hit-test', 'light-estimation', 'dom-overlay', 'dom-overlay-for-handheld-ar']
|
|
23
21
|
}}
|
|
22
|
+
{...$$restProps}
|
|
23
|
+
mode='immersive-ar'
|
|
24
|
+
|
|
24
25
|
on:click
|
|
25
26
|
on:error
|
|
26
27
|
/>
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="webxr" />
|
|
1
2
|
import { SvelteComponent } from "svelte";
|
|
2
3
|
declare const __propDef: {
|
|
3
4
|
props: {
|
|
@@ -221,10 +222,16 @@ declare const __propDef: {
|
|
|
221
222
|
'on:close'?: import("svelte/elements").EventHandler<Event, HTMLButtonElement> | null | undefined;
|
|
222
223
|
'on:fullscreenchange'?: import("svelte/elements").EventHandler<Event, HTMLButtonElement> | null | undefined;
|
|
223
224
|
'on:fullscreenerror'?: import("svelte/elements").EventHandler<Event, HTMLButtonElement> | null | undefined;
|
|
225
|
+
sessionInit?: XRSessionInit & {
|
|
226
|
+
domOverlay?: {
|
|
227
|
+
root: HTMLElement;
|
|
228
|
+
} | undefined;
|
|
229
|
+
};
|
|
224
230
|
};
|
|
225
231
|
events: {
|
|
226
232
|
click: CustomEvent<{
|
|
227
233
|
state: "unsupported" | "insecure" | "blocked" | "supported";
|
|
234
|
+
nativeEvent: MouseEvent;
|
|
228
235
|
}>;
|
|
229
236
|
error: CustomEvent<Error>;
|
|
230
237
|
} & {
|
|
@@ -14,11 +14,11 @@
|
|
|
14
14
|
</script>
|
|
15
15
|
|
|
16
16
|
<XRButton
|
|
17
|
-
{...$$restProps}
|
|
18
|
-
mode='immersive-vr'
|
|
19
17
|
sessionInit={{
|
|
20
18
|
optionalFeatures: ['local-floor', 'bounded-floor', 'hand-tracking', 'layers']
|
|
21
19
|
}}
|
|
20
|
+
{...$$restProps}
|
|
21
|
+
mode='immersive-vr'
|
|
22
22
|
on:click
|
|
23
23
|
on:error
|
|
24
24
|
/>
|
|
@@ -27,11 +27,11 @@ export let sessionInit = void 0;
|
|
|
27
27
|
export let force = void 0;
|
|
28
28
|
export let styled = true;
|
|
29
29
|
const dispatch = createEventDispatcher();
|
|
30
|
-
const handleButtonClick = async (state) => {
|
|
30
|
+
const handleButtonClick = async (nativeEvent, state) => {
|
|
31
31
|
if (!$initialized) {
|
|
32
32
|
throw new Error("The <XR> component was not created. This is required to start an XR session.");
|
|
33
33
|
}
|
|
34
|
-
dispatch("click", { state });
|
|
34
|
+
dispatch("click", { state, nativeEvent });
|
|
35
35
|
if (state !== "supported")
|
|
36
36
|
return;
|
|
37
37
|
try {
|
|
@@ -65,7 +65,10 @@ $:
|
|
|
65
65
|
</script>
|
|
66
66
|
|
|
67
67
|
{#await getXRSupportState(mode) then state}
|
|
68
|
-
<button
|
|
68
|
+
<button
|
|
69
|
+
{...$$restProps}
|
|
70
|
+
on:click={(event) => handleButtonClick(event, state)}
|
|
71
|
+
>
|
|
69
72
|
{#if state === 'unsupported'}
|
|
70
73
|
{modeText} unsupported
|
|
71
74
|
{:else if state === 'insecure'}
|
|
@@ -1,5 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="webxr" />
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
export type HitTestCallback = (hitMatrix: THREE.Matrix4, hit: XRHitTestResult | undefined) => void;
|
|
4
|
+
export type UseHitTestOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* The ray source when performing hit testing.
|
|
7
|
+
*
|
|
8
|
+
* @default 'viewer'
|
|
9
|
+
*/
|
|
10
|
+
source?: 'viewer' | 'leftInput' | 'rightInput';
|
|
11
|
+
};
|
|
2
12
|
/**
|
|
3
|
-
* Use this hook to perform a hit test
|
|
13
|
+
* Use this hook to perform a hit test per frame in an AR environment.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* useHitTest((hitMatrix, hit) => {
|
|
17
|
+
* mesh.matrix.copy(hitMatrix)
|
|
18
|
+
* }, {
|
|
19
|
+
* source: 'viewer' | 'leftInput' | 'rightInput' // Default 'viewer'
|
|
20
|
+
* })
|
|
21
|
+
* ```
|
|
4
22
|
*/
|
|
5
|
-
export declare const useHitTest: (hitTestCallback: HitTestCallback) => void;
|
|
23
|
+
export declare const useHitTest: (hitTestCallback: HitTestCallback, options?: UseHitTestOptions) => void;
|
package/dist/hooks/useHitTest.js
CHANGED
|
@@ -1,44 +1,69 @@
|
|
|
1
1
|
import * as THREE from 'three';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { useThrelte, useFrame, watch, currentWritable } from '@threlte/core';
|
|
3
|
+
import { useXR } from './useXR';
|
|
4
|
+
import { useController } from './useController';
|
|
5
5
|
/**
|
|
6
|
-
* Use this hook to perform a hit test
|
|
6
|
+
* Use this hook to perform a hit test per frame in an AR environment.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* useHitTest((hitMatrix, hit) => {
|
|
10
|
+
* mesh.matrix.copy(hitMatrix)
|
|
11
|
+
* }, {
|
|
12
|
+
* source: 'viewer' | 'leftInput' | 'rightInput' // Default 'viewer'
|
|
13
|
+
* })
|
|
14
|
+
* ```
|
|
7
15
|
*/
|
|
8
|
-
export const useHitTest = (hitTestCallback) => {
|
|
16
|
+
export const useHitTest = (hitTestCallback, options = {}) => {
|
|
17
|
+
const source = options.source ?? 'viewer';
|
|
9
18
|
const { xr } = useThrelte().renderer;
|
|
19
|
+
const xrState = useXR();
|
|
10
20
|
const hitMatrix = new THREE.Matrix4();
|
|
11
|
-
let
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
let hitTestSource = currentWritable(undefined);
|
|
22
|
+
if (source === 'viewer') {
|
|
23
|
+
watch(xrState.session, async (session) => {
|
|
24
|
+
if (session === undefined)
|
|
25
|
+
return;
|
|
26
|
+
const space = await session.requestReferenceSpace('viewer');
|
|
27
|
+
hitTestSource.set(await session.requestHitTestSource?.({ space }));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
const controller = useController(source === 'leftInput' ? 'left' : 'right');
|
|
32
|
+
const hand = useController(source === 'leftInput' ? 'left' : 'right');
|
|
33
|
+
watch([xrState.session, controller], async ([session, input]) => {
|
|
34
|
+
if (input === undefined || session === undefined)
|
|
35
|
+
return;
|
|
36
|
+
const space = input.inputSource.targetRaySpace;
|
|
37
|
+
hitTestSource.set(await session.requestHitTestSource?.({ space }));
|
|
38
|
+
});
|
|
39
|
+
watch([xrState.session, hand], async ([session, input]) => {
|
|
40
|
+
if (input === undefined || session === undefined)
|
|
41
|
+
return;
|
|
42
|
+
const space = input.inputSource.targetRaySpace;
|
|
43
|
+
hitTestSource.set(await session.requestHitTestSource?.({ space }));
|
|
44
|
+
});
|
|
45
|
+
}
|
|
29
46
|
const { start, stop } = useFrame(() => {
|
|
30
|
-
if (hitTestSource === undefined)
|
|
31
|
-
return;
|
|
32
|
-
const [hit] = xr.getFrame().getHitTestResults(hitTestSource);
|
|
33
|
-
if (hit === undefined)
|
|
34
|
-
return;
|
|
35
47
|
const referenceSpace = xr.getReferenceSpace();
|
|
36
|
-
if (referenceSpace === null)
|
|
37
|
-
return;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
48
|
+
if (referenceSpace === null || hitTestSource.current === undefined) {
|
|
49
|
+
return hitTestCallback(hitMatrix, undefined);
|
|
50
|
+
}
|
|
51
|
+
const [hit] = xr.getFrame().getHitTestResults(hitTestSource.current);
|
|
52
|
+
const pose = hit?.getPose(referenceSpace);
|
|
53
|
+
if (pose === undefined) {
|
|
54
|
+
return hitTestCallback(hitMatrix, undefined);
|
|
55
|
+
}
|
|
41
56
|
hitMatrix.fromArray(pose.transform.matrix);
|
|
42
57
|
hitTestCallback(hitMatrix, hit);
|
|
43
58
|
}, { autostart: false });
|
|
59
|
+
watch(hitTestSource, (testSource) => {
|
|
60
|
+
if (testSource === undefined) {
|
|
61
|
+
stop();
|
|
62
|
+
// Execute callback one last time to inform consumers of no hits.
|
|
63
|
+
hitTestCallback(hitMatrix, undefined);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
start();
|
|
67
|
+
}
|
|
68
|
+
});
|
|
44
69
|
};
|
package/dist/internal/headset.js
CHANGED
|
@@ -11,7 +11,9 @@ export const useUpdateHeadset = () => {
|
|
|
11
11
|
if (space === null)
|
|
12
12
|
return;
|
|
13
13
|
const pose = xr.getFrame().getViewerPose(space);
|
|
14
|
-
|
|
14
|
+
// Although pose is only typed as possibly undefined,
|
|
15
|
+
// It can be null on android chrome when using phone AR.
|
|
16
|
+
if (pose === undefined || pose === null)
|
|
15
17
|
return;
|
|
16
18
|
const { position, orientation } = pose.transform;
|
|
17
19
|
headset.position.set(position.x, position.y, position.z);
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@threlte/xr",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"author": "Micheal Parks <michealparks1989@gmail.com> (https://parks.lol)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"devDependencies": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"typescript": "^5.1.6",
|
|
19
19
|
"vite": "^4.4.6",
|
|
20
20
|
"vite-plugin-mkcert": "^1.16.0",
|
|
21
|
-
"@threlte/core": "6.0.
|
|
21
|
+
"@threlte/core": "6.0.10"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
24
|
"svelte": ">=4",
|