@threlte/xr 0.0.8 → 0.0.9
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/hooks/useHitTest.d.ts +21 -3
- package/dist/hooks/useHitTest.js +58 -33
- package/dist/types.d.ts +0 -1
- package/package.json +2 -2
|
@@ -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/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.9",
|
|
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",
|