@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.
@@ -1,5 +1,23 @@
1
- import type { HitTestCallback } from '../types';
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 for an AR environment.
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;
@@ -1,44 +1,69 @@
1
1
  import * as THREE from 'three';
2
- import { onDestroy } from 'svelte';
3
- import { session } from '../internal/stores';
4
- import { useThrelte, useFrame } from '@threlte/core';
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 for an AR environment.
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 started = false;
12
- let hitTestSource;
13
- onDestroy(session.subscribe(async (value) => {
14
- if (value === undefined) {
15
- hitTestSource = undefined;
16
- if (started) {
17
- stop();
18
- started = false;
19
- }
20
- return;
21
- }
22
- const space = await value.requestReferenceSpace('viewer');
23
- hitTestSource = await value.requestHitTestSource?.({ space });
24
- if (!started) {
25
- start();
26
- started = true;
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
- const pose = hit.getPose(referenceSpace);
39
- if (pose === undefined)
40
- return;
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
@@ -33,4 +33,3 @@ export type XRHandEvent<Type = XRHandEventType> = Type extends 'connected' | 'di
33
33
  handedness: 'left' | 'right';
34
34
  target: null;
35
35
  } : never;
36
- export type HitTestCallback = (hitMatrix: THREE.Matrix4, hit: XRHitTestResult) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threlte/xr",
3
- "version": "0.0.8",
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.9"
21
+ "@threlte/core": "6.0.10"
22
22
  },
23
23
  "peerDependencies": {
24
24
  "svelte": ">=4",