@opsydyn/effect-three-foldkit 0.4.0 → 1.0.0-next.0
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/CHANGELOG.md +39 -0
- package/README.md +29 -23
- package/dist/boxSelect.d.ts +91 -117
- package/dist/boxSelect.d.ts.map +1 -1
- package/dist/browser.d.ts +3 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +32 -0
- package/dist/browser.js.map +10 -0
- package/dist/element.d.ts +13 -869
- package/dist/element.d.ts.map +1 -1
- package/dist/index.d.ts +17 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +173 -2383
- package/dist/index.js.map +14 -38
- package/dist/inspector.d.ts +4 -21
- package/dist/inspector.d.ts.map +1 -1
- package/dist/measuringTape.d.ts +39 -46
- package/dist/measuringTape.d.ts.map +1 -1
- package/dist/mount.d.ts +17 -11
- package/dist/mount.d.ts.map +1 -1
- package/dist/overlayView.d.ts +7 -18
- package/dist/overlayView.d.ts.map +1 -1
- package/dist/pivotControls.d.ts +64 -76
- package/dist/pivotControls.d.ts.map +1 -1
- package/dist/transformGizmo.d.ts +113 -125
- package/dist/transformGizmo.d.ts.map +1 -1
- package/dist/view.d.ts +37 -4
- package/dist/view.d.ts.map +1 -1
- package/dist/viewportGizmo.d.ts +107 -119
- package/dist/viewportGizmo.d.ts.map +1 -1
- package/package.json +15 -10
- package/src/boxSelect.ts +12 -133
- package/src/browser.ts +16 -0
- package/src/element.ts +19 -106
- package/src/index.ts +35 -16
- package/src/inspector.ts +70 -93
- package/src/measuringTape.ts +28 -92
- package/src/mount.ts +53 -286
- package/src/overlayView.ts +36 -57
- package/src/pivotControls.ts +8 -94
- package/src/transformGizmo.ts +8 -108
- package/src/view.ts +89 -51
- package/src/viewportGizmo.ts +8 -97
- package/dist/assets.d.ts +0 -53
- package/dist/assets.d.ts.map +0 -1
- package/dist/dom/animationFrames.d.ts +0 -12
- package/dist/dom/animationFrames.d.ts.map +0 -1
- package/dist/dom/canvas.d.ts +0 -26
- package/dist/dom/canvas.d.ts.map +0 -1
- package/dist/dom/latch.d.ts +0 -15
- package/dist/dom/latch.d.ts.map +0 -1
- package/dist/dom/models.d.ts +0 -10
- package/dist/dom/models.d.ts.map +0 -1
- package/dist/dom/overlays.d.ts +0 -24
- package/dist/dom/overlays.d.ts.map +0 -1
- package/dist/dom/pointer.d.ts +0 -49
- package/dist/dom/pointer.d.ts.map +0 -1
- package/dist/dom/postprocessing.d.ts +0 -22
- package/dist/dom/postprocessing.d.ts.map +0 -1
- package/dist/dom/property.d.ts +0 -15
- package/dist/dom/property.d.ts.map +0 -1
- package/dist/dom/reconciled.d.ts +0 -10
- package/dist/dom/reconciled.d.ts.map +0 -1
- package/dist/dom/renderSettings.d.ts +0 -58
- package/dist/dom/renderSettings.d.ts.map +0 -1
- package/dist/dom/typedScenes.d.ts +0 -14
- package/dist/dom/typedScenes.d.ts.map +0 -1
- package/dist/dom/updates.d.ts +0 -8
- package/dist/dom/updates.d.ts.map +0 -1
- package/dist/dom/viewport.d.ts +0 -40
- package/dist/dom/viewport.d.ts.map +0 -1
- package/dist/dom/webgl.d.ts +0 -12
- package/dist/dom/webgl.d.ts.map +0 -1
- package/dist/dom/webgpu.d.ts +0 -24
- package/dist/dom/webgpu.d.ts.map +0 -1
- package/dist/dom/webgpuPostprocessing.d.ts +0 -4
- package/dist/dom/webgpuPostprocessing.d.ts.map +0 -1
- package/dist/dom/xr.d.ts +0 -53
- package/dist/dom/xr.d.ts.map +0 -1
- package/dist/dom/xrGlide.d.ts +0 -49
- package/dist/dom/xrGlide.d.ts.map +0 -1
- package/dist/dom/xrHub.d.ts +0 -45
- package/dist/dom/xrHub.d.ts.map +0 -1
- package/dist/dom/xrInputs.d.ts +0 -40
- package/dist/dom/xrInputs.d.ts.map +0 -1
- package/dist/dom/xrSpaces.d.ts +0 -33
- package/dist/dom/xrSpaces.d.ts.map +0 -1
- package/dist/dom/xrTarget.d.ts +0 -36
- package/dist/dom/xrTarget.d.ts.map +0 -1
- package/dist/environment.d.ts +0 -64
- package/dist/environment.d.ts.map +0 -1
- package/dist/xr.d.ts +0 -34
- package/dist/xr.d.ts.map +0 -1
- package/src/assets.ts +0 -151
- package/src/dom/animationFrames.ts +0 -32
- package/src/dom/canvas.ts +0 -54
- package/src/dom/latch.ts +0 -36
- package/src/dom/models.ts +0 -37
- package/src/dom/overlays.ts +0 -63
- package/src/dom/pointer.ts +0 -163
- package/src/dom/postprocessing.ts +0 -116
- package/src/dom/property.ts +0 -43
- package/src/dom/reconciled.ts +0 -51
- package/src/dom/renderSettings.ts +0 -117
- package/src/dom/typedScenes.ts +0 -21
- package/src/dom/updates.ts +0 -14
- package/src/dom/viewport.ts +0 -97
- package/src/dom/webgl.ts +0 -231
- package/src/dom/webgpu.ts +0 -220
- package/src/dom/webgpuPostprocessing.ts +0 -102
- package/src/dom/xr.ts +0 -211
- package/src/dom/xrGlide.ts +0 -114
- package/src/dom/xrHub.ts +0 -118
- package/src/dom/xrInputs.ts +0 -309
- package/src/dom/xrSpaces.ts +0 -287
- package/src/dom/xrTarget.ts +0 -71
- package/src/environment.ts +0 -78
- package/src/xr.ts +0 -36
package/src/dom/xrHub.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The page's immersive sessions: which mounted scenes can be presented, and the one
|
|
3
|
-
* session (WebXR allows one at a time) presenting a scene.
|
|
4
|
-
*
|
|
5
|
-
* @module
|
|
6
|
-
*/
|
|
7
|
-
import type { Xr, XrInput } from '@opsydyn/effect-three';
|
|
8
|
-
import type { Object3D } from 'three';
|
|
9
|
-
|
|
10
|
-
import { presentableRendererOf } from './renderSettings.ts';
|
|
11
|
-
import { type Presentation, present, sessionInit, type XrMode, type XrSystem } from './xr.ts';
|
|
12
|
-
import type { Follow, LendGeometry } from './xrSpaces.ts';
|
|
13
|
-
|
|
14
|
-
export type { XrMode } from './xr.ts';
|
|
15
|
-
|
|
16
|
-
/** A mounted scene that can be presented. */
|
|
17
|
-
export type XrTarget = {
|
|
18
|
-
readonly canvas: HTMLCanvasElement;
|
|
19
|
-
/** The scene's current `xr` description; `null` while it does not ask to be presented. */
|
|
20
|
-
readonly settings: () => Xr | null;
|
|
21
|
-
/** Draws one frame now (the session calls it for each of its frames). */
|
|
22
|
-
readonly draw: () => void;
|
|
23
|
-
/** The scene's three.js root, where input sources are drawn while it presents. */
|
|
24
|
-
readonly scene: Object3D;
|
|
25
|
-
/** The scene's element: the DOM overlay's root, when the scene asks for one. */
|
|
26
|
-
readonly element: Element;
|
|
27
|
-
/** Places the groups that follow tracked spaces. */
|
|
28
|
-
readonly follow: Follow;
|
|
29
|
-
/** Lends a geometry to the scene's store, for detected meshes. */
|
|
30
|
-
readonly lend: LendGeometry;
|
|
31
|
-
/** Input from the session: aims, selects, sticks, buttons and surfaces. */
|
|
32
|
-
readonly input: (input: XrInput) => void;
|
|
33
|
-
/** The session began presenting this scene in `mode`, or (with `null`) ended. */
|
|
34
|
-
readonly presenting: (mode: XrMode | null) => void;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export type XrHub = {
|
|
38
|
-
/** Makes a scene presentable; returns a function that withdraws it (ending its session). */
|
|
39
|
-
readonly register: (target: XrTarget) => () => void;
|
|
40
|
-
readonly supported: (mode: XrMode) => Promise<boolean>;
|
|
41
|
-
/**
|
|
42
|
-
* Requests a session and presents the first registered scene that describes `xr`.
|
|
43
|
-
* Call from a user gesture's handling: browsers require one to start a session.
|
|
44
|
-
*/
|
|
45
|
-
readonly enter: (mode: XrMode) => Promise<void>;
|
|
46
|
-
readonly exit: () => Promise<void>;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
type Current = { readonly target: XrTarget; readonly presentation: Presentation };
|
|
50
|
-
|
|
51
|
-
/** Creates the hub for a page; `system` is `navigator.xr` when the browser has WebXR. */
|
|
52
|
-
export const createXrHub = (system: () => XrSystem | undefined): XrHub => {
|
|
53
|
-
const targets = new Set<XrTarget>();
|
|
54
|
-
let current: Current | undefined;
|
|
55
|
-
|
|
56
|
-
const presentable = (): { readonly target: XrTarget; readonly settings: Xr } | undefined => {
|
|
57
|
-
for (const target of targets) {
|
|
58
|
-
const settings = target.settings();
|
|
59
|
-
if (settings !== null) return { target, settings };
|
|
60
|
-
}
|
|
61
|
-
return undefined;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const enter = async (mode: XrMode): Promise<void> => {
|
|
65
|
-
if (current !== undefined) return;
|
|
66
|
-
const xr = system();
|
|
67
|
-
if (xr === undefined) throw new Error('WebXR is not available in this browser');
|
|
68
|
-
const chosen = presentable();
|
|
69
|
-
if (chosen === undefined) throw new Error('no mounted scene describes xr');
|
|
70
|
-
const renderer = presentableRendererOf(chosen.target.canvas);
|
|
71
|
-
if (renderer === undefined) throw new Error('the scene is not drawn by a three.js renderer');
|
|
72
|
-
const session = await xr.requestSession(
|
|
73
|
-
mode,
|
|
74
|
-
sessionInit(chosen.settings, mode, chosen.target.element),
|
|
75
|
-
);
|
|
76
|
-
const { target } = chosen;
|
|
77
|
-
// Each frame: follow the description (a new origin moves the player), then draw.
|
|
78
|
-
const draw = (): void => {
|
|
79
|
-
const settings = target.settings();
|
|
80
|
-
if (settings !== null) current?.presentation.update(settings);
|
|
81
|
-
target.draw();
|
|
82
|
-
};
|
|
83
|
-
const ended = (): void => {
|
|
84
|
-
current = undefined;
|
|
85
|
-
target.presenting(null);
|
|
86
|
-
};
|
|
87
|
-
const presentation = await present(
|
|
88
|
-
renderer,
|
|
89
|
-
session,
|
|
90
|
-
chosen.settings,
|
|
91
|
-
{
|
|
92
|
-
scene: target.scene,
|
|
93
|
-
draw,
|
|
94
|
-
follow: target.follow,
|
|
95
|
-
lend: target.lend,
|
|
96
|
-
input: target.input,
|
|
97
|
-
},
|
|
98
|
-
ended,
|
|
99
|
-
);
|
|
100
|
-
current = { target, presentation };
|
|
101
|
-
target.presenting(mode);
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
return {
|
|
105
|
-
register: (target) => {
|
|
106
|
-
targets.add(target);
|
|
107
|
-
return () => {
|
|
108
|
-
targets.delete(target);
|
|
109
|
-
if (current?.target === target) void current.presentation.session.end();
|
|
110
|
-
};
|
|
111
|
-
},
|
|
112
|
-
supported: async (mode) => (await system()?.isSessionSupported(mode)) ?? false,
|
|
113
|
-
enter,
|
|
114
|
-
exit: async () => {
|
|
115
|
-
await current?.presentation.session.end();
|
|
116
|
-
},
|
|
117
|
-
};
|
|
118
|
-
};
|
package/src/dom/xrInputs.ts
DELETED
|
@@ -1,309 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A session's input sources, each frame: where each points (`Aim`), its gamepad's sticks
|
|
3
|
-
* and buttons, in AR where its ray meets a real surface, and drawing its ray and a hand's
|
|
4
|
-
* joints into the presented scene.
|
|
5
|
-
*
|
|
6
|
-
* Behavioural references: pmndrs/xr at `0e1acd8` (`packages/xr/src/pointer` ray-casts
|
|
7
|
-
* along every source's target ray each frame; `controller-locomotion.ts` reads the
|
|
8
|
-
* `xr-standard` thumbstick, axes 2 and 3, forward negative; `hit-test.ts` requests a hit
|
|
9
|
-
* test source per space and reads `getHitTestResults` each frame), and three.js's
|
|
10
|
-
* `XRHandPrimitiveModel` at 0.186.1 (a sphere per hand joint, sized by its radius).
|
|
11
|
-
*
|
|
12
|
-
* @module
|
|
13
|
-
*/
|
|
14
|
-
import type {
|
|
15
|
-
Ray,
|
|
16
|
-
Vec3,
|
|
17
|
-
Xr,
|
|
18
|
-
XrButtonName,
|
|
19
|
-
XrHand,
|
|
20
|
-
XrInput,
|
|
21
|
-
XrStickDirection,
|
|
22
|
-
} from '@opsydyn/effect-three';
|
|
23
|
-
import {
|
|
24
|
-
BufferGeometry,
|
|
25
|
-
Float32BufferAttribute,
|
|
26
|
-
Group,
|
|
27
|
-
InstancedMesh,
|
|
28
|
-
Line,
|
|
29
|
-
LineBasicMaterial,
|
|
30
|
-
Matrix4,
|
|
31
|
-
MeshBasicMaterial,
|
|
32
|
-
Quaternion,
|
|
33
|
-
SphereGeometry,
|
|
34
|
-
Vector3,
|
|
35
|
-
} from 'three';
|
|
36
|
-
|
|
37
|
-
/** A target ray pose as a ray in the reference space: from its position along its −z. */
|
|
38
|
-
export const rayOf = (pose: XRPose): Ray => {
|
|
39
|
-
const { position, orientation } = pose.transform;
|
|
40
|
-
const { x, y, z, w } = orientation;
|
|
41
|
-
// −z rotated by the orientation quaternion.
|
|
42
|
-
return {
|
|
43
|
-
origin: [position.x, position.y, position.z],
|
|
44
|
-
direction: [-2 * (x * z + w * y), -2 * (y * z - w * x), -(1 - 2 * (x * x + y * y))],
|
|
45
|
-
};
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/** The `xr-standard` mapping's buttons, by index. */
|
|
49
|
-
const buttonNames: ReadonlyArray<XrButtonName> = [
|
|
50
|
-
'trigger',
|
|
51
|
-
'squeeze',
|
|
52
|
-
'touchpad',
|
|
53
|
-
'thumbstick',
|
|
54
|
-
'primary',
|
|
55
|
-
'secondary',
|
|
56
|
-
];
|
|
57
|
-
|
|
58
|
-
/** Past this far a stick is pushed; back under the lower mark it is centred again. */
|
|
59
|
-
const stickThresholds = { push: 0.6, release: 0.3 } as const;
|
|
60
|
-
|
|
61
|
-
/** Where a stick at `(x, y)` points, keeping `previous` until it clearly changes. */
|
|
62
|
-
export const stickDirection = (
|
|
63
|
-
x: number,
|
|
64
|
-
y: number,
|
|
65
|
-
previous: XrStickDirection,
|
|
66
|
-
): XrStickDirection => {
|
|
67
|
-
if (Math.max(Math.abs(x), Math.abs(y)) < stickThresholds.release) return 'center';
|
|
68
|
-
if (Math.max(Math.abs(x), Math.abs(y)) < stickThresholds.push) return previous;
|
|
69
|
-
if (Math.abs(x) >= Math.abs(y)) return x < 0 ? 'left' : 'right';
|
|
70
|
-
return y < 0 ? 'forward' : 'back';
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const handOf = (source: XRInputSource): XrHand => source.handedness;
|
|
74
|
-
|
|
75
|
-
/** What the visuals show: a line along each ray, spheres on each hand's joints. */
|
|
76
|
-
const rayLength = 1.5;
|
|
77
|
-
const jointCount = 25;
|
|
78
|
-
const rayMaterial = new LineBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0.6 });
|
|
79
|
-
const jointMaterial = new MeshBasicMaterial({ color: 0xe8e4dc });
|
|
80
|
-
const jointGeometry = new SphereGeometry(1, 10, 8);
|
|
81
|
-
|
|
82
|
-
type Visual = { readonly ray: Line; readonly joints: InstancedMesh | undefined };
|
|
83
|
-
|
|
84
|
-
const makeVisual = (source: XRInputSource, group: Group): Visual => {
|
|
85
|
-
const geometry = new BufferGeometry();
|
|
86
|
-
geometry.setAttribute('position', new Float32BufferAttribute([0, 0, 0, 0, 0, -rayLength], 3));
|
|
87
|
-
const ray = new Line(geometry, rayMaterial);
|
|
88
|
-
ray.raycast = () => undefined;
|
|
89
|
-
ray.matrixAutoUpdate = false;
|
|
90
|
-
group.add(ray);
|
|
91
|
-
const joints =
|
|
92
|
-
source.hand === undefined
|
|
93
|
-
? undefined
|
|
94
|
-
: new InstancedMesh(jointGeometry, jointMaterial, jointCount);
|
|
95
|
-
if (joints !== undefined) {
|
|
96
|
-
joints.raycast = () => undefined;
|
|
97
|
-
joints.frustumCulled = false;
|
|
98
|
-
group.add(joints);
|
|
99
|
-
}
|
|
100
|
-
return { ray, joints };
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
const disposeVisual = (visual: Visual): void => {
|
|
104
|
-
visual.ray.removeFromParent();
|
|
105
|
-
visual.ray.geometry.dispose();
|
|
106
|
-
visual.joints?.removeFromParent();
|
|
107
|
-
visual.joints?.dispose();
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
const matrix = new Matrix4();
|
|
111
|
-
const up = new Vector3(0, 1, 0);
|
|
112
|
-
const position = new Vector3();
|
|
113
|
-
const turn = new Quaternion();
|
|
114
|
-
const size = new Vector3();
|
|
115
|
-
|
|
116
|
-
/** Places a hand's joint spheres from this frame's joint poses. */
|
|
117
|
-
const placeJoints = (
|
|
118
|
-
joints: InstancedMesh,
|
|
119
|
-
hand: XRHand,
|
|
120
|
-
frame: XRFrame,
|
|
121
|
-
space: XRReferenceSpace,
|
|
122
|
-
): void => {
|
|
123
|
-
let slot = 0;
|
|
124
|
-
for (const jointSpace of hand.values()) {
|
|
125
|
-
const pose = frame.getJointPose?.(jointSpace, space);
|
|
126
|
-
if (pose !== undefined && pose !== null && slot < jointCount) {
|
|
127
|
-
const { x, y, z } = pose.transform.position;
|
|
128
|
-
const o = pose.transform.orientation;
|
|
129
|
-
const radius = pose.radius ?? 0.008;
|
|
130
|
-
matrix.compose(position.set(x, y, z), turn.set(o.x, o.y, o.z, o.w), size.setScalar(radius));
|
|
131
|
-
joints.setMatrixAt(slot, matrix);
|
|
132
|
-
slot++;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
joints.count = slot;
|
|
136
|
-
joints.instanceMatrix.needsUpdate = true;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
type Tracked = {
|
|
140
|
-
readonly id: number;
|
|
141
|
-
visual: Visual | undefined;
|
|
142
|
-
stick: XrStickDirection;
|
|
143
|
-
/** The thumbstick's axes this frame, `[x, y]`. */
|
|
144
|
-
axes: readonly [number, number];
|
|
145
|
-
readonly pressed: Array<boolean>;
|
|
146
|
-
hitTest: XRHitTestSource | undefined;
|
|
147
|
-
/** Where its ray last met a real surface. */
|
|
148
|
-
surface: { readonly point: Vec3; readonly normal: Vec3 } | undefined;
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
/** A hit test result's pose as a point and the surface's normal (the pose's +y). */
|
|
152
|
-
const surfaceOf = (pose: XRPose): { readonly point: Vec3; readonly normal: Vec3 } => {
|
|
153
|
-
const { position: p, orientation: q } = pose.transform;
|
|
154
|
-
// +y rotated by the orientation quaternion.
|
|
155
|
-
return {
|
|
156
|
-
point: [p.x, p.y, p.z],
|
|
157
|
-
normal: [
|
|
158
|
-
2 * (q.x * q.y - q.w * q.z),
|
|
159
|
-
1 - 2 * (q.x * q.x + q.z * q.z),
|
|
160
|
-
2 * (q.y * q.z + q.w * q.x),
|
|
161
|
-
],
|
|
162
|
-
};
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
export type XrInputs = {
|
|
166
|
-
/** Reads every input source in this frame, in `space` (the scene's). */
|
|
167
|
-
readonly frame: (frame: XRFrame, space: XRReferenceSpace) => void;
|
|
168
|
-
/** A source's id, the same for its selects as for its aims. */
|
|
169
|
-
readonly idOf: (source: XRInputSource) => number;
|
|
170
|
-
/** A select ended: in AR with hit tests, report where its ray met a surface. */
|
|
171
|
-
readonly selected: (source: XRInputSource) => void;
|
|
172
|
-
/** Ends tracking: every source is lost, and the visuals go. */
|
|
173
|
-
readonly stop: () => void;
|
|
174
|
-
/** Each hand's thumbstick this frame (`[0, 0]` without one). */
|
|
175
|
-
readonly sticks: () => {
|
|
176
|
-
readonly left: readonly [number, number];
|
|
177
|
-
readonly right: readonly [number, number];
|
|
178
|
-
};
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Tracks `session`'s input sources, sending what they do to `input` and drawing them into
|
|
183
|
-
* `group` (added to the presented scene) while `settings().showInputs`.
|
|
184
|
-
*/
|
|
185
|
-
export const trackInputs = (
|
|
186
|
-
session: XRSession,
|
|
187
|
-
group: Group,
|
|
188
|
-
settings: () => Xr | null,
|
|
189
|
-
input: (input: XrInput) => void,
|
|
190
|
-
): XrInputs => {
|
|
191
|
-
const tracked = new Map<XRInputSource, Tracked>();
|
|
192
|
-
let nextId = 0;
|
|
193
|
-
|
|
194
|
-
const trackedOf = (source: XRInputSource): Tracked => {
|
|
195
|
-
const known = tracked.get(source);
|
|
196
|
-
if (known !== undefined) return known;
|
|
197
|
-
const created: Tracked = {
|
|
198
|
-
id: nextId++,
|
|
199
|
-
visual: undefined,
|
|
200
|
-
stick: 'center',
|
|
201
|
-
axes: [0, 0],
|
|
202
|
-
pressed: [],
|
|
203
|
-
hitTest: undefined,
|
|
204
|
-
surface: undefined,
|
|
205
|
-
};
|
|
206
|
-
tracked.set(source, created);
|
|
207
|
-
if (settings()?.hitTest === true && session.requestHitTestSource !== undefined) {
|
|
208
|
-
// Refused where the session lacks hit testing (it is optional): no surfaces, then.
|
|
209
|
-
void session.requestHitTestSource({ space: source.targetRaySpace })?.then(
|
|
210
|
-
(hitTest) => {
|
|
211
|
-
if (tracked.get(source) === created) created.hitTest = hitTest;
|
|
212
|
-
else hitTest.cancel();
|
|
213
|
-
},
|
|
214
|
-
() => undefined,
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
return created;
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
const forget = (source: XRInputSource, state: Tracked): void => {
|
|
221
|
-
tracked.delete(source);
|
|
222
|
-
if (state.visual !== undefined) disposeVisual(state.visual);
|
|
223
|
-
state.hitTest?.cancel();
|
|
224
|
-
input({ _tag: 'Lost', source: state.id });
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
const gamepad = (source: XRInputSource, state: Tracked): void => {
|
|
228
|
-
const pad = source.gamepad;
|
|
229
|
-
if (pad === undefined || pad === null || pad.mapping !== 'xr-standard') return;
|
|
230
|
-
const hand = handOf(source);
|
|
231
|
-
buttonNames.forEach((button, index) => {
|
|
232
|
-
const pressed = pad.buttons[index]?.pressed ?? false;
|
|
233
|
-
if (pressed !== (state.pressed[index] ?? false)) {
|
|
234
|
-
state.pressed[index] = pressed;
|
|
235
|
-
input({ _tag: 'Button', source: state.id, hand, button, pressed });
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
state.axes = [pad.axes[2] ?? 0, pad.axes[3] ?? 0];
|
|
239
|
-
const direction = stickDirection(state.axes[0], state.axes[1], state.stick);
|
|
240
|
-
if (direction !== state.stick) {
|
|
241
|
-
state.stick = direction;
|
|
242
|
-
input({ _tag: 'Stick', source: state.id, hand, direction });
|
|
243
|
-
}
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
const show = (
|
|
247
|
-
source: XRInputSource,
|
|
248
|
-
state: Tracked,
|
|
249
|
-
ray: Ray,
|
|
250
|
-
frame: XRFrame,
|
|
251
|
-
space: XRReferenceSpace,
|
|
252
|
-
) => {
|
|
253
|
-
if (settings()?.showInputs !== true) {
|
|
254
|
-
if (state.visual !== undefined) disposeVisual(state.visual);
|
|
255
|
-
state.visual = undefined;
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
state.visual ??= makeVisual(source, group);
|
|
259
|
-
const [ox, oy, oz] = ray.origin;
|
|
260
|
-
const [dx, dy, dz] = ray.direction;
|
|
261
|
-
// Turn −z (along which the line runs) to the ray: lookAt points +z from the target.
|
|
262
|
-
matrix.lookAt(position.set(0, 0, 0), size.set(dx, dy, dz), up);
|
|
263
|
-
matrix.setPosition(ox, oy, oz);
|
|
264
|
-
state.visual.ray.matrix.copy(matrix);
|
|
265
|
-
// A tracked hand points from its wrist; its joints show where it is, not a ray.
|
|
266
|
-
state.visual.ray.visible = source.hand === undefined;
|
|
267
|
-
if (state.visual.joints !== undefined && source.hand !== undefined) {
|
|
268
|
-
placeJoints(state.visual.joints, source.hand, frame, space);
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
return {
|
|
273
|
-
frame: (frame, space) => {
|
|
274
|
-
const present = new Set<XRInputSource>();
|
|
275
|
-
for (const source of session.inputSources) {
|
|
276
|
-
present.add(source);
|
|
277
|
-
const state = trackedOf(source);
|
|
278
|
-
const pose = frame.getPose(source.targetRaySpace, space);
|
|
279
|
-
if (pose !== undefined && pose !== null) {
|
|
280
|
-
const ray = rayOf(pose);
|
|
281
|
-
input({ _tag: 'Aim', source: state.id, ray });
|
|
282
|
-
show(source, state, ray, frame, space);
|
|
283
|
-
}
|
|
284
|
-
gamepad(source, state);
|
|
285
|
-
if (state.hitTest !== undefined) {
|
|
286
|
-
const result = frame.getHitTestResults(state.hitTest)[0]?.getPose(space);
|
|
287
|
-
state.surface = result === undefined || result === null ? undefined : surfaceOf(result);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
for (const [source, state] of tracked) if (!present.has(source)) forget(source, state);
|
|
291
|
-
},
|
|
292
|
-
idOf: (source) => trackedOf(source).id,
|
|
293
|
-
selected: (source) => {
|
|
294
|
-
const state = tracked.get(source);
|
|
295
|
-
if (state?.surface === undefined) return;
|
|
296
|
-
input({ _tag: 'Surface', source: state.id, hand: handOf(source), ...state.surface });
|
|
297
|
-
},
|
|
298
|
-
sticks: () => {
|
|
299
|
-
const axesOf = (hand: XrHand): readonly [number, number] => {
|
|
300
|
-
for (const [source, state] of tracked) if (source.handedness === hand) return state.axes;
|
|
301
|
-
return [0, 0];
|
|
302
|
-
};
|
|
303
|
-
return { left: axesOf('left'), right: axesOf('right') };
|
|
304
|
-
},
|
|
305
|
-
stop: () => {
|
|
306
|
-
for (const [source, state] of tracked) forget(source, state);
|
|
307
|
-
},
|
|
308
|
-
};
|
|
309
|
-
};
|