@driftengine/xr 3.61.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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Drift Technologies
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,9 @@
1
+ DriftEngine
2
+ Copyright 2026 Drift Technologies
3
+
4
+ This product includes software developed at Drift Technologies
5
+ (https://github.com/drftrun/driftengine).
6
+
7
+ Licensed under the Apache License, Version 2.0. Section 4(d) of that licence
8
+ requires this NOTICE to be reproduced in any derivative work you distribute,
9
+ in the source, the documentation, or a display generated by the work.
package/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # @driftengine/xr
2
+
3
+ WebXR sessions, stereo views, controller input and hand joints, over both backends.
4
+
5
+ **A package and not part of core**, so a game that never enters a session carries none of it. It
6
+ supplies views to a camera; nothing in core reaches back for a session.
7
+
8
+ ```ts
9
+ import { startLoop } from '@driftengine/core';
10
+ import { aimCameraAtEye, enterXr, eyeViews, probeXrSupport } from '@driftengine/xr';
11
+
12
+ // From a click. requestSession needs a user gesture and no wrapper can change that.
13
+ const entered = await enterXr({ sources: { gl }, optionalFeatures: ['hand-tracking'] });
14
+ if (!entered.ok) {
15
+ showTheUser(entered.reason);
16
+ return;
17
+ }
18
+
19
+ const { run } = entered;
20
+ const stop = startLoop(
21
+ {
22
+ simulate: (dt, tick) => world.step(dt, tick),
23
+ render: (alpha, dt, wallDt, frame) => {
24
+ const pose = (frame as XrFrame).getViewerPose(run.referenceSpace);
25
+ if (pose === null) return; // Tracking lost. Draw nothing rather than draw it wrong.
26
+ for (const eye of eyeViews(pose, run.layer.baseLayer)) {
27
+ aimCameraAtEye(camera, eye);
28
+ renderer.setViewport(eye.viewport);
29
+ renderer.draw(scene, camera);
30
+ }
31
+ },
32
+ },
33
+ { frameSource: run.frameSource },
34
+ );
35
+
36
+ run.onEnd(stop);
37
+ ```
38
+
39
+ ## The camera is supplied, not configured
40
+
41
+ `Camera` derives its view from yaw and pitch and its projection from `mat4.perspective`, and it
42
+ cannot describe an XR eye. Its own note says why: _only a centred crop of a symmetric frustum is
43
+ itself a symmetric frustum; an off-centre one is sheared, which `perspective` cannot produce._ A
44
+ headset's eye projection is exactly that shear, off-axis by the distance from pupil to display
45
+ centre.
46
+
47
+ So `Camera.adoptView` takes the matrices the runtime computed. It recomputes the combined and
48
+ inverse matrices, so culling, picking and the sky keep working, and it decomposes `position` and
49
+ `forward` back out, so a listener placed at `camera.position` follows the head rather than staying
50
+ where the last flat frame left it. It leaves `yaw`, `pitch` and `roll` alone, because a supplied
51
+ rotation may be one no Euler triple describes.
52
+
53
+ ## The loop runs on the session's clock
54
+
55
+ A session produces frames at whatever rate its display runs, through its own
56
+ `requestAnimationFrame`, and the callback carries the frame every pose is read from. Hand
57
+ `run.frameSource` to `startLoop` and the engine follows it. Outside a session the window drives, as
58
+ it always did.
59
+
60
+ ## Nothing fails silently
61
+
62
+ Every path that does not enter a session answers with a whole sentence, because a consumer whose
63
+ button did nothing is owed the difference between a browser with no WebXR, a machine with no
64
+ headset, a context that would not become XR compatible, and a reference space the runtime declined.
65
+ Those are four different things to do next.
66
+
67
+ `probeXrSupport` asks by **doing**. `isSessionSupported` is a promise about a mode and says nothing
68
+ about whether a context can be made XR compatible, which is the step that actually fails: on a
69
+ machine with no headset, `inline` reports supported, the session starts, the reference space is
70
+ granted, and `makeXRCompatible` throws. Without it there is no base layer and therefore no frame at
71
+ all.
72
+
73
+ ## Both backends
74
+
75
+ `XRGPUBinding` with a projection layer where the runtime has one, `XRWebGLLayer` otherwise, chosen
76
+ from what the session offers rather than from a preference. A WebGPU layer that fails to build falls
77
+ through to WebGL2 and says so, because a game running on WebGPU in a browser whose WebXR is
78
+ WebGL-only should enter a session rather than refuse.
79
+
80
+ WebGPU in WebXR is experimental. In Chrome it is behind `--enable-experimental-web-platform-features`
81
+ and its own note calls it available for developer testing on Windows and Android. This package does
82
+ not ask a consumer to ship that flag; it uses the binding when a browser already offers one.
83
+
84
+ ## From a script
85
+
86
+ `drift/xr` binds twelve capabilities: whether a session is presenting, where the head is, what each
87
+ hand's trigger and grip are doing, where a named joint is, and whether a hand is pinching. **None of
88
+ them is deterministic**, so a `@deterministic` system cannot ask where a head is, which is the
89
+ property the annotation exists for. **None of them is a matrix**: drawing the two eyes is the host's,
90
+ because the host owns the renderer.
91
+
92
+ ## What has been measured, and what has not
93
+
94
+ The machine this was written on has no headset. `immersive-vr` reports false and `makeXRCompatible`
95
+ throws, so no `XRFrame` can be produced there at all.
96
+
97
+ - **Measured** by `npm run check:xr` against a real browser: capability detection, a real `inline`
98
+ session requested and ended, a real reference space, the WebGPU binding constructing under the
99
+ flag, and every refusal path.
100
+ - **Measured** by the unit tests against a synthetic runtime: two views with different off-axis
101
+ projections, the camera adopting each, controller buttons and poses, twenty-five hand joints, and
102
+ a joint that stops being tracked keeping its last pose.
103
+ - **Not measured**: a real immersive session, stereo on hardware, controller input from a device,
104
+ hand tracking, device performance, and the compositor's reprojection.
105
+
106
+ That last line is an open row, unmeasured rather than claimed. Nothing here says otherwise.
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Twenty-five joints a hand, read into flat arrays.
3
+ *
4
+ * **The joint names are the specification's and their order is this file's**, fixed once as
5
+ * `HAND_JOINTS` so an index means the same thing in every frame, on every device and in a recording
6
+ * somebody kept. Reading them into a `Float32Array` rather than handing back objects is the same
7
+ * decision every other per-frame reader in this engine makes: a hand is twenty-five poses, twice,
8
+ * at ninety hertz, and an object per joint is four and a half thousand allocations a second on the
9
+ * device this arrangement exists for.
10
+ *
11
+ * **A joint that is not tracked is not zeroed**, and that is the interesting decision here. Hand
12
+ * tracking loses joints constantly, one at a time, as fingers occlude each other from the camera's
13
+ * view. Writing an identity matrix into an untracked joint would collapse that finger to the wrist
14
+ * for one frame and snap it back on the next, which reads as a violent twitch. So the last known
15
+ * pose stays, `tracked` says it is stale, and a consumer decides whether to hold, fade or hide.
16
+ * That is a policy a game owns, and this package refuses to make it for them.
17
+ */
18
+ import type { XrFrame, XrInputSource } from './types.ts';
19
+ /**
20
+ * Every joint WebXR defines, in the order this engine indexes them.
21
+ *
22
+ * Wrist first, then each finger from its base outward, which is the order the specification lists
23
+ * them in and the order a reader expects. **Never reordered**: an index is written into recordings
24
+ * and compared across runs, so moving one would change what a stored hand means.
25
+ */
26
+ export declare const HAND_JOINTS: readonly ['wrist', 'thumb-metacarpal', 'thumb-phalanx-proximal', 'thumb-phalanx-distal', 'thumb-tip', 'index-finger-metacarpal', 'index-finger-phalanx-proximal', 'index-finger-phalanx-intermediate', 'index-finger-phalanx-distal', 'index-finger-tip', 'middle-finger-metacarpal', 'middle-finger-phalanx-proximal', 'middle-finger-phalanx-intermediate', 'middle-finger-phalanx-distal', 'middle-finger-tip', 'ring-finger-metacarpal', 'ring-finger-phalanx-proximal', 'ring-finger-phalanx-intermediate', 'ring-finger-phalanx-distal', 'ring-finger-tip', 'pinky-finger-metacarpal', 'pinky-finger-phalanx-proximal', 'pinky-finger-phalanx-intermediate', 'pinky-finger-phalanx-distal', 'pinky-finger-tip'];
27
+ export type HandJoint = (typeof HAND_JOINTS)[number];
28
+ export declare const JOINT_COUNT: 25;
29
+ /** The index of a named joint, so a consumer never counts. */
30
+ export declare const JOINT_INDEX: Readonly<Record<string, number>>;
31
+ /**
32
+ * One hand's joints, allocated once and written into every frame.
33
+ *
34
+ * Sixteen floats a joint because a joint carries a full transform: a fingertip has an orientation
35
+ * as well as a position, and a consumer drawing a mesh needs both.
36
+ */
37
+ export declare class HandSkeleton {
38
+ /** Row-major transforms, `JOINT_COUNT * 16`, in world space. */
39
+ readonly matrices: Float32Array<ArrayBuffer>;
40
+ /** Joint radii in metres, or 0 where the runtime offered none. */
41
+ readonly radii: Float32Array<ArrayBuffer>;
42
+ /** Whether each joint had a pose in the last frame that was read. */
43
+ readonly tracked: Uint8Array<ArrayBuffer>;
44
+ /** How many joints were tracked in that frame. Zero means the hand is not visible. */
45
+ trackedCount: number;
46
+ /** Whether anything at all was seen, which is what a consumer branches on before drawing. */
47
+ get visible(): boolean;
48
+ /**
49
+ * Forget everything, which is what a source going away means.
50
+ *
51
+ * Separate from a frame where every joint is merely untracked: a hand that has left has no last
52
+ * known pose worth holding, and a consumer that fades on loss needs the two told apart.
53
+ */
54
+ clear(): void;
55
+ }
56
+ /**
57
+ * Read one hand into a skeleton.
58
+ *
59
+ * Returns whether anything was tracked. `false` with the skeleton left holding its last poses is
60
+ * the ordinary case for a hand that has moved out of the cameras' view, and it is not an error.
61
+ */
62
+ export declare function readHand(source: XrInputSource, frame: XrFrame, referenceSpace: unknown, into: HandSkeleton): boolean;
63
+ /** A joint's position, read out of the flat storage without allocating. */
64
+ export declare function jointPosition(skeleton: HandSkeleton, joint: HandJoint | number, out: Float32Array): boolean;
package/dist/hands.js ADDED
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Twenty-five joints a hand, read into flat arrays.
3
+ *
4
+ * **The joint names are the specification's and their order is this file's**, fixed once as
5
+ * `HAND_JOINTS` so an index means the same thing in every frame, on every device and in a recording
6
+ * somebody kept. Reading them into a `Float32Array` rather than handing back objects is the same
7
+ * decision every other per-frame reader in this engine makes: a hand is twenty-five poses, twice,
8
+ * at ninety hertz, and an object per joint is four and a half thousand allocations a second on the
9
+ * device this arrangement exists for.
10
+ *
11
+ * **A joint that is not tracked is not zeroed**, and that is the interesting decision here. Hand
12
+ * tracking loses joints constantly, one at a time, as fingers occlude each other from the camera's
13
+ * view. Writing an identity matrix into an untracked joint would collapse that finger to the wrist
14
+ * for one frame and snap it back on the next, which reads as a violent twitch. So the last known
15
+ * pose stays, `tracked` says it is stale, and a consumer decides whether to hold, fade or hide.
16
+ * That is a policy a game owns, and this package refuses to make it for them.
17
+ */
18
+ /**
19
+ * Every joint WebXR defines, in the order this engine indexes them.
20
+ *
21
+ * Wrist first, then each finger from its base outward, which is the order the specification lists
22
+ * them in and the order a reader expects. **Never reordered**: an index is written into recordings
23
+ * and compared across runs, so moving one would change what a stored hand means.
24
+ */
25
+ export const HAND_JOINTS = [
26
+ 'wrist',
27
+ 'thumb-metacarpal',
28
+ 'thumb-phalanx-proximal',
29
+ 'thumb-phalanx-distal',
30
+ 'thumb-tip',
31
+ 'index-finger-metacarpal',
32
+ 'index-finger-phalanx-proximal',
33
+ 'index-finger-phalanx-intermediate',
34
+ 'index-finger-phalanx-distal',
35
+ 'index-finger-tip',
36
+ 'middle-finger-metacarpal',
37
+ 'middle-finger-phalanx-proximal',
38
+ 'middle-finger-phalanx-intermediate',
39
+ 'middle-finger-phalanx-distal',
40
+ 'middle-finger-tip',
41
+ 'ring-finger-metacarpal',
42
+ 'ring-finger-phalanx-proximal',
43
+ 'ring-finger-phalanx-intermediate',
44
+ 'ring-finger-phalanx-distal',
45
+ 'ring-finger-tip',
46
+ 'pinky-finger-metacarpal',
47
+ 'pinky-finger-phalanx-proximal',
48
+ 'pinky-finger-phalanx-intermediate',
49
+ 'pinky-finger-phalanx-distal',
50
+ 'pinky-finger-tip',
51
+ ];
52
+ export const JOINT_COUNT = HAND_JOINTS.length;
53
+ /** The index of a named joint, so a consumer never counts. */
54
+ export const JOINT_INDEX = Object.freeze(Object.fromEntries(HAND_JOINTS.map((name, index) => [name, index])));
55
+ /**
56
+ * One hand's joints, allocated once and written into every frame.
57
+ *
58
+ * Sixteen floats a joint because a joint carries a full transform: a fingertip has an orientation
59
+ * as well as a position, and a consumer drawing a mesh needs both.
60
+ */
61
+ export class HandSkeleton {
62
+ /** Row-major transforms, `JOINT_COUNT * 16`, in world space. */
63
+ matrices = new Float32Array(JOINT_COUNT * 16);
64
+ /** Joint radii in metres, or 0 where the runtime offered none. */
65
+ radii = new Float32Array(JOINT_COUNT);
66
+ /** Whether each joint had a pose in the last frame that was read. */
67
+ tracked = new Uint8Array(JOINT_COUNT);
68
+ /** How many joints were tracked in that frame. Zero means the hand is not visible. */
69
+ trackedCount = 0;
70
+ /** Whether anything at all was seen, which is what a consumer branches on before drawing. */
71
+ get visible() {
72
+ return this.trackedCount > 0;
73
+ }
74
+ /**
75
+ * Forget everything, which is what a source going away means.
76
+ *
77
+ * Separate from a frame where every joint is merely untracked: a hand that has left has no last
78
+ * known pose worth holding, and a consumer that fades on loss needs the two told apart.
79
+ */
80
+ clear() {
81
+ this.matrices.fill(0);
82
+ this.radii.fill(0);
83
+ this.tracked.fill(0);
84
+ this.trackedCount = 0;
85
+ }
86
+ }
87
+ /**
88
+ * Read one hand into a skeleton.
89
+ *
90
+ * Returns whether anything was tracked. `false` with the skeleton left holding its last poses is
91
+ * the ordinary case for a hand that has moved out of the cameras' view, and it is not an error.
92
+ */
93
+ export function readHand(source, frame, referenceSpace, into) {
94
+ const hand = source.hand;
95
+ if (hand === undefined || frame.getJointPose === undefined) {
96
+ into.trackedCount = 0;
97
+ into.tracked.fill(0);
98
+ return false;
99
+ }
100
+ let tracked = 0;
101
+ for (let i = 0; i < JOINT_COUNT; i++) {
102
+ const space = hand.get(HAND_JOINTS[i]);
103
+ const pose = space === undefined || space === null
104
+ ? null
105
+ : (frame.getJointPose(space, referenceSpace) ?? null);
106
+ if (pose === null) {
107
+ /*
108
+ * Left as it was. See the header: zeroing an occluded joint collapses that finger onto the
109
+ * wrist for one frame and snaps it back on the next, and a hand tracker loses joints
110
+ * constantly. The flag says the pose is stale; what to do about it is the consumer's.
111
+ */
112
+ into.tracked[i] = 0;
113
+ continue;
114
+ }
115
+ into.matrices.set(pose.transform.matrix, i * 16);
116
+ into.radii[i] = pose.radius ?? 0;
117
+ into.tracked[i] = 1;
118
+ tracked++;
119
+ }
120
+ into.trackedCount = tracked;
121
+ return tracked > 0;
122
+ }
123
+ /** A joint's position, read out of the flat storage without allocating. */
124
+ export function jointPosition(skeleton, joint, out) {
125
+ const index = typeof joint === 'number' ? joint : (JOINT_INDEX[joint] ?? -1);
126
+ if (index < 0 || index >= JOINT_COUNT)
127
+ return false;
128
+ /* Column-major, as every matrix in this engine is, so the translation is elements 12 to 14. */
129
+ const at = index * 16;
130
+ out[0] = skeleton.matrices[at + 12];
131
+ out[1] = skeleton.matrices[at + 13];
132
+ out[2] = skeleton.matrices[at + 14];
133
+ return skeleton.tracked[index] === 1;
134
+ }
@@ -0,0 +1,36 @@
1
+ /*! DriftEngine | Copyright 2026 Drift Technologies | Apache-2.0 | https://github.com/drftrun/driftengine */
2
+ /**
3
+ * WebXR: sessions, stereo views, controller input and hand joints.
4
+ *
5
+ * **A package and not part of core**, so a game that never enters a session carries none of it.
6
+ * `size-gate.test.mjs` keeps that a measurement rather than an intention.
7
+ *
8
+ * **It supplies views to a camera and core never reaches back for a session.** That is the whole
9
+ * shape of the dependency: `Camera.adoptView` lives in core because the camera is core's, and the
10
+ * reason a camera cannot derive an XR eye's matrices is written beside it. Everything about
11
+ * sessions, layers, poses and hands is here.
12
+ *
13
+ * ## What is measured, and what is written down instead
14
+ *
15
+ * A real immersive session has never been run against this code. The machine it was written on has
16
+ * no headset: `immersive-vr` reports false, and `makeXRCompatible` throws, so no `XRFrame` can be
17
+ * produced there at all. What that machine does reach is asserted by `scripts/xr-check.mjs`, and
18
+ * everything past the first frame is exercised against a synthetic `XRSystem` in the unit tests.
19
+ *
20
+ * So **stereo on hardware, controller input, hand tracking, device performance and the compositor's
21
+ * reprojection are unmeasured**, and are tracked as such rather than claimed. Nothing
22
+ * here says otherwise.
23
+ */
24
+ export type { XrFrame, XrGamepad, XrHand, XrInputSource, XrJointPose, XrMode, XrRigidTransform, XrSession, XrSystem, XrView, XrViewerPose, XrViewport, XrWebGlLayer, } from './types.ts';
25
+ export type { XrCompatibleContext, XrSupport } from './support.ts';
26
+ export { bestMode, probeXrSupport } from './support.ts';
27
+ export type { LayerBackend, LayerSources, XrLayer } from './layers.ts';
28
+ export { chooseLayer } from './layers.ts';
29
+ export type { EnterXrOptions, EnterXrResult, XrRun } from './session.ts';
30
+ export { enterXr } from './session.ts';
31
+ export type { EyeView } from './views.ts';
32
+ export { aimCameraAtEye, eyeViews } from './views.ts';
33
+ export type { ControllerState, Handedness, XrButton } from './input.ts';
34
+ export { XR_BUTTON, buttonPressed, buttonValue, controllerFor, readController, readControllers, } from './input.ts';
35
+ export type { HandJoint } from './hands.ts';
36
+ export { HAND_JOINTS, HandSkeleton, JOINT_COUNT, JOINT_INDEX, jointPosition, readHand, } from './hands.ts';
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ /*! DriftEngine | Copyright 2026 Drift Technologies | Apache-2.0 | https://github.com/drftrun/driftengine */
2
+ /**
3
+ * WebXR: sessions, stereo views, controller input and hand joints.
4
+ *
5
+ * **A package and not part of core**, so a game that never enters a session carries none of it.
6
+ * `size-gate.test.mjs` keeps that a measurement rather than an intention.
7
+ *
8
+ * **It supplies views to a camera and core never reaches back for a session.** That is the whole
9
+ * shape of the dependency: `Camera.adoptView` lives in core because the camera is core's, and the
10
+ * reason a camera cannot derive an XR eye's matrices is written beside it. Everything about
11
+ * sessions, layers, poses and hands is here.
12
+ *
13
+ * ## What is measured, and what is written down instead
14
+ *
15
+ * A real immersive session has never been run against this code. The machine it was written on has
16
+ * no headset: `immersive-vr` reports false, and `makeXRCompatible` throws, so no `XRFrame` can be
17
+ * produced there at all. What that machine does reach is asserted by `scripts/xr-check.mjs`, and
18
+ * everything past the first frame is exercised against a synthetic `XRSystem` in the unit tests.
19
+ *
20
+ * So **stereo on hardware, controller input, hand tracking, device performance and the compositor's
21
+ * reprojection are unmeasured**, and are tracked as such rather than claimed. Nothing
22
+ * here says otherwise.
23
+ */
24
+ export { bestMode, probeXrSupport } from './support.js';
25
+ export { chooseLayer } from './layers.js';
26
+ export { enterXr } from './session.js';
27
+ export { aimCameraAtEye, eyeViews } from './views.js';
28
+ export { XR_BUTTON, buttonPressed, buttonValue, controllerFor, readController, readControllers, } from './input.js';
29
+ export { HAND_JOINTS, HandSkeleton, JOINT_COUNT, JOINT_INDEX, jointPosition, readHand, } from './hands.js';
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Controllers, read the way the rest of this engine reads input.
3
+ *
4
+ * **A controller is a gamepad with a pose, and the specification says so**: an `XRInputSource`
5
+ * carries an ordinary `Gamepad` whose button order is fixed by the WebXR gamepad profile. So the
6
+ * buttons are named here rather than indexed at every call site, and the names are the profile's
7
+ * rather than any device's marketing: `trigger`, `squeeze`, `touchpad`, `thumbstick`.
8
+ *
9
+ * **What is deliberately absent is a binding layer.** This engine has `ActionMap`, and a second one
10
+ * that only XR could reach would be a second answer to what a button means. A consumer maps a
11
+ * trigger to an action the same way they map a key, which is why this reads state and never
12
+ * dispatches an intent.
13
+ *
14
+ * **Nothing here is deterministic and none of it belongs in a fixed step.** A controller pose
15
+ * arrives with a frame, from a device, at a rate the runtime chooses. `drift/ui` already settled
16
+ * that a pointer position is `input.read` and outside `DETERMINISTIC_EFFECTS`, and a hand is the
17
+ * same kind of fact.
18
+ */
19
+ import type { XrFrame, XrInputSource, XrSession } from './types.ts';
20
+ /** Which hand, as WebXR reports it. `none` is a source with no handedness, like a gaze pointer. */
21
+ export type Handedness = 'left' | 'right' | 'none';
22
+ /**
23
+ * The buttons the WebXR gamepad profile fixes, in its order.
24
+ *
25
+ * Indexes and not a guess: the profile pins 0 to the trigger and 1 to the squeeze for every device
26
+ * that reports `xr-standard`, which is what makes reading them by name safe. A device offering
27
+ * fewer buttons simply has none at the higher indexes, which reads as zero rather than as an error.
28
+ */
29
+ export declare const XR_BUTTON: {
30
+ readonly trigger: 0;
31
+ readonly squeeze: 1;
32
+ readonly touchpad: 2;
33
+ readonly thumbstick: 3;
34
+ };
35
+ export type XrButton = keyof typeof XR_BUTTON;
36
+ /** One controller, as much of it as this engine reads. */
37
+ export interface ControllerState {
38
+ readonly handedness: Handedness;
39
+ /** How the source is aimed: `tracked-pointer`, `gaze` or `screen`. */
40
+ readonly targetRayMode: string;
41
+ /** Whether a pose was available this frame. False while a controller is out of view. */
42
+ readonly tracked: boolean;
43
+ /** The grip transform's matrix, valid only while `tracked`. */
44
+ readonly gripMatrix: Float32Array | null;
45
+ /** Where the source is aiming, which is not where it is held. */
46
+ readonly rayMatrix: Float32Array | null;
47
+ /** Analogue values in the profile's order, and zero for a button the device does not have. */
48
+ readonly buttons: readonly number[];
49
+ readonly pressed: readonly boolean[];
50
+ readonly axes: readonly number[];
51
+ }
52
+ /**
53
+ * Read every input source a session currently reports.
54
+ *
55
+ * `out` is filled and returned, so a frame loop allocates nothing after the first call. Sources
56
+ * come and go as controllers wake and sleep, which is why the list is read from the session every
57
+ * frame rather than cached at `inputsourceschange`: the event exists for a consumer that wants to
58
+ * react, and this is the polling half.
59
+ */
60
+ export declare function readControllers(session: XrSession, frame: XrFrame, referenceSpace: unknown, out?: ControllerState[]): ControllerState[];
61
+ export declare function readController(source: XrInputSource, frame: XrFrame, referenceSpace: unknown): ControllerState;
62
+ /** One named button's analogue value, or zero where the device has no such button. */
63
+ export declare function buttonValue(state: ControllerState, button: XrButton): number;
64
+ /** Whether a named button is pressed, false where the device has no such button. */
65
+ export declare function buttonPressed(state: ControllerState, button: XrButton): boolean;
66
+ /** The first source for a hand, or null. Two sources may share a handedness and rarely do. */
67
+ export declare function controllerFor(states: readonly ControllerState[], handedness: Handedness): ControllerState | null;