iwer 2.1.1 → 2.2.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/build/iwer.js +1606 -5
- package/build/iwer.min.js +12 -12
- package/build/iwer.module.js +1599 -6
- package/build/iwer.module.min.js +12 -12
- package/lib/device/XRController.d.ts +20 -0
- package/lib/device/XRController.d.ts.map +1 -1
- package/lib/device/XRController.js +56 -0
- package/lib/device/XRController.js.map +1 -1
- package/lib/device/XRDevice.d.ts +43 -1
- package/lib/device/XRDevice.d.ts.map +1 -1
- package/lib/device/XRDevice.js +88 -0
- package/lib/device/XRDevice.js.map +1 -1
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -0
- package/lib/index.js.map +1 -1
- package/lib/remote/RemoteControlInterface.d.ts +172 -0
- package/lib/remote/RemoteControlInterface.d.ts.map +1 -0
- package/lib/remote/RemoteControlInterface.js +1194 -0
- package/lib/remote/RemoteControlInterface.js.map +1 -0
- package/lib/remote/index.d.ts +9 -0
- package/lib/remote/index.d.ts.map +1 -0
- package/lib/remote/index.js +8 -0
- package/lib/remote/index.js.map +1 -0
- package/lib/remote/types.d.ts +348 -0
- package/lib/remote/types.d.ts.map +1 -0
- package/lib/remote/types.js +8 -0
- package/lib/remote/types.js.map +1 -0
- package/lib/types/state.d.ts +46 -0
- package/lib/types/state.d.ts.map +1 -0
- package/lib/types/state.js +8 -0
- package/lib/types/state.js.map +1 -0
- package/lib/utils/control-math.d.ts +64 -0
- package/lib/utils/control-math.d.ts.map +1 -0
- package/lib/utils/control-math.js +238 -0
- package/lib/utils/control-math.js.map +1 -0
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +10 -5
- package/lib/layers/XRWebGLBinding.d.ts +0 -92
- package/lib/layers/XRWebGLBinding.d.ts.map +0 -1
- package/lib/layers/XRWebGLBinding.js +0 -186
- package/lib/layers/XRWebGLBinding.js.map +0 -1
|
@@ -0,0 +1,1194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import { vec3 } from 'gl-matrix';
|
|
8
|
+
import { P_SESSION, P_SPACE } from '../private.js';
|
|
9
|
+
import { vec3ToObj, quatToObj, quatToEuler, eulerToQuat, directionTo, lookRotation, lookRotationGimbal, waitForCondition, } from '../utils/control-math.js';
|
|
10
|
+
/**
|
|
11
|
+
* Check if an orientation input is euler angles (has any of pitch, yaw, or roll)
|
|
12
|
+
*/
|
|
13
|
+
function isEulerRotation(orientation) {
|
|
14
|
+
return 'pitch' in orientation || 'yaw' in orientation || 'roll' in orientation;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Normalize an orientation input to a quaternion
|
|
18
|
+
*/
|
|
19
|
+
function normalizeOrientation(orientation) {
|
|
20
|
+
if (isEulerRotation(orientation)) {
|
|
21
|
+
return eulerToQuat(orientation);
|
|
22
|
+
}
|
|
23
|
+
return orientation;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Linear interpolation for numbers
|
|
27
|
+
*/
|
|
28
|
+
function lerp(a, b, t) {
|
|
29
|
+
return a + (b - a) * t;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Linear interpolation for Vec3
|
|
33
|
+
*/
|
|
34
|
+
function lerpVec3(a, b, t) {
|
|
35
|
+
return {
|
|
36
|
+
x: lerp(a.x, b.x, t),
|
|
37
|
+
y: lerp(a.y, b.y, t),
|
|
38
|
+
z: lerp(a.z, b.z, t),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Spherical linear interpolation for quaternions
|
|
43
|
+
*/
|
|
44
|
+
function slerpQuat(a, b, t) {
|
|
45
|
+
// Compute dot product
|
|
46
|
+
let dot = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
|
47
|
+
// If dot is negative, negate one quaternion to take shorter path
|
|
48
|
+
let bx = b.x, by = b.y, bz = b.z, bw = b.w;
|
|
49
|
+
if (dot < 0) {
|
|
50
|
+
dot = -dot;
|
|
51
|
+
bx = -bx;
|
|
52
|
+
by = -by;
|
|
53
|
+
bz = -bz;
|
|
54
|
+
bw = -bw;
|
|
55
|
+
}
|
|
56
|
+
// If quaternions are very close, use linear interpolation
|
|
57
|
+
if (dot > 0.9995) {
|
|
58
|
+
const result = {
|
|
59
|
+
x: lerp(a.x, bx, t),
|
|
60
|
+
y: lerp(a.y, by, t),
|
|
61
|
+
z: lerp(a.z, bz, t),
|
|
62
|
+
w: lerp(a.w, bw, t),
|
|
63
|
+
};
|
|
64
|
+
// Normalize
|
|
65
|
+
const len = Math.sqrt(result.x * result.x +
|
|
66
|
+
result.y * result.y +
|
|
67
|
+
result.z * result.z +
|
|
68
|
+
result.w * result.w);
|
|
69
|
+
return {
|
|
70
|
+
x: result.x / len,
|
|
71
|
+
y: result.y / len,
|
|
72
|
+
z: result.z / len,
|
|
73
|
+
w: result.w / len,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
// Standard slerp
|
|
77
|
+
const theta0 = Math.acos(dot);
|
|
78
|
+
const theta = theta0 * t;
|
|
79
|
+
const sinTheta = Math.sin(theta);
|
|
80
|
+
const sinTheta0 = Math.sin(theta0);
|
|
81
|
+
const s0 = Math.cos(theta) - (dot * sinTheta) / sinTheta0;
|
|
82
|
+
const s1 = sinTheta / sinTheta0;
|
|
83
|
+
return {
|
|
84
|
+
x: s0 * a.x + s1 * bx,
|
|
85
|
+
y: s0 * a.y + s1 * by,
|
|
86
|
+
z: s0 * a.z + s1 * bz,
|
|
87
|
+
w: s0 * a.w + s1 * bw,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* RemoteControlInterface provides frame-synchronized programmatic control of an XRDevice.
|
|
92
|
+
*
|
|
93
|
+
* This class implements a command queue that processes actions during each frame update,
|
|
94
|
+
* enabling smooth animations and coordinated control with DevUI.
|
|
95
|
+
*
|
|
96
|
+
* Key features:
|
|
97
|
+
* - Frame-synchronized execution: Commands are queued and processed during frame update
|
|
98
|
+
* - Duration-based actions: Smooth animations via lerp over multiple frames
|
|
99
|
+
* - Automatic capture/release: Captures device on first command, releases 30s after queue empties
|
|
100
|
+
* - Unified device identifiers: 'headset', 'controller-left', 'hand-right', etc.
|
|
101
|
+
*
|
|
102
|
+
* Usage:
|
|
103
|
+
* ```typescript
|
|
104
|
+
* import { XRDevice, metaQuest3 } from 'iwer';
|
|
105
|
+
*
|
|
106
|
+
* const device = new XRDevice(metaQuest3);
|
|
107
|
+
* device.installRuntime();
|
|
108
|
+
*
|
|
109
|
+
* // Get transform
|
|
110
|
+
* const result = await device.remote.dispatch('get_transform', { device: 'headset' });
|
|
111
|
+
*
|
|
112
|
+
* // Animate headset to new position over 1 second
|
|
113
|
+
* await device.remote.dispatch('animate_to', {
|
|
114
|
+
* device: 'headset',
|
|
115
|
+
* position: { x: 0, y: 1.6, z: -1 },
|
|
116
|
+
* duration: 1.0
|
|
117
|
+
* });
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export class RemoteControlInterface {
|
|
121
|
+
constructor(device) {
|
|
122
|
+
this.commandQueue = [];
|
|
123
|
+
this._isCaptured = false;
|
|
124
|
+
this.releaseTimer = null;
|
|
125
|
+
this.actionIdCounter = 0;
|
|
126
|
+
/** Release timeout in milliseconds (default: 30000 = 30 seconds) */
|
|
127
|
+
this.RELEASE_TIMEOUT_MS = 30000;
|
|
128
|
+
this.device = device;
|
|
129
|
+
}
|
|
130
|
+
generateActionId() {
|
|
131
|
+
return `action_${++this.actionIdCounter}`;
|
|
132
|
+
}
|
|
133
|
+
// =============================================================================
|
|
134
|
+
// Public Properties
|
|
135
|
+
// =============================================================================
|
|
136
|
+
/**
|
|
137
|
+
* Whether the device is currently captured for programmatic control.
|
|
138
|
+
* When true, DevUI should go into passive mode (sync FROM device only).
|
|
139
|
+
*/
|
|
140
|
+
get isCaptured() {
|
|
141
|
+
return this._isCaptured;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Number of pending actions in the queue
|
|
145
|
+
*/
|
|
146
|
+
get queueLength() {
|
|
147
|
+
return this.commandQueue.length;
|
|
148
|
+
}
|
|
149
|
+
// =============================================================================
|
|
150
|
+
// Queue Management
|
|
151
|
+
// =============================================================================
|
|
152
|
+
/**
|
|
153
|
+
* Enqueue a discrete action for processing
|
|
154
|
+
*/
|
|
155
|
+
enqueueDiscrete(method, params) {
|
|
156
|
+
return new Promise((resolve, reject) => {
|
|
157
|
+
const action = {
|
|
158
|
+
type: 'discrete',
|
|
159
|
+
id: this.generateActionId(),
|
|
160
|
+
method,
|
|
161
|
+
params,
|
|
162
|
+
resolve,
|
|
163
|
+
reject,
|
|
164
|
+
};
|
|
165
|
+
this.commandQueue.push(action);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Enqueue a duration action for processing
|
|
170
|
+
*/
|
|
171
|
+
enqueueDuration(method, params, durationMs, startState, targetState) {
|
|
172
|
+
return new Promise((resolve, reject) => {
|
|
173
|
+
const action = {
|
|
174
|
+
type: 'duration',
|
|
175
|
+
id: this.generateActionId(),
|
|
176
|
+
method,
|
|
177
|
+
params,
|
|
178
|
+
durationMs,
|
|
179
|
+
elapsedMs: 0,
|
|
180
|
+
startState,
|
|
181
|
+
targetState,
|
|
182
|
+
resolve,
|
|
183
|
+
reject,
|
|
184
|
+
};
|
|
185
|
+
this.commandQueue.push(action);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Update method called each frame by XRDevice.
|
|
190
|
+
* Processes the command queue and handles duration-based animations.
|
|
191
|
+
*
|
|
192
|
+
* @param deltaTimeMs - Time since last frame in milliseconds
|
|
193
|
+
*/
|
|
194
|
+
update(deltaTimeMs) {
|
|
195
|
+
if (this.commandQueue.length === 0) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
// Always cancel pending release while queue is active
|
|
199
|
+
this.cancelReleaseTimer();
|
|
200
|
+
// Activate capture mode
|
|
201
|
+
if (!this._isCaptured) {
|
|
202
|
+
this._isCaptured = true;
|
|
203
|
+
this.device.controlMode = 'programmatic';
|
|
204
|
+
}
|
|
205
|
+
while (this.commandQueue.length > 0) {
|
|
206
|
+
const action = this.commandQueue[0];
|
|
207
|
+
if (action.type === 'discrete') {
|
|
208
|
+
// Execute discrete action immediately
|
|
209
|
+
try {
|
|
210
|
+
const result = this.executeDiscreteAction(action);
|
|
211
|
+
action.resolve(result);
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
action.reject(error);
|
|
215
|
+
}
|
|
216
|
+
this.commandQueue.shift();
|
|
217
|
+
// Continue to next action
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
// Duration action - lerp by delta time
|
|
221
|
+
action.elapsedMs += deltaTimeMs;
|
|
222
|
+
if (action.elapsedMs >= action.durationMs) {
|
|
223
|
+
// Complete - apply final state
|
|
224
|
+
this.applyDurationFinalState(action);
|
|
225
|
+
action.resolve(this.getDurationResult(action));
|
|
226
|
+
this.commandQueue.shift();
|
|
227
|
+
// Continue to next action
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
// In progress - lerp
|
|
231
|
+
const t = action.elapsedMs / action.durationMs;
|
|
232
|
+
this.applyDurationLerpState(action, t);
|
|
233
|
+
// Stop processing - wait for next frame
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
// Notify state change
|
|
239
|
+
this.device.notifyStateChange();
|
|
240
|
+
// Start release timer if queue is empty
|
|
241
|
+
if (this.commandQueue.length === 0) {
|
|
242
|
+
this.startReleaseTimer();
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
startReleaseTimer() {
|
|
246
|
+
this.cancelReleaseTimer();
|
|
247
|
+
this.releaseTimer = setTimeout(() => {
|
|
248
|
+
this._isCaptured = false;
|
|
249
|
+
this.device.controlMode = 'manual';
|
|
250
|
+
this.releaseTimer = null;
|
|
251
|
+
}, this.RELEASE_TIMEOUT_MS);
|
|
252
|
+
}
|
|
253
|
+
cancelReleaseTimer() {
|
|
254
|
+
if (this.releaseTimer !== null) {
|
|
255
|
+
clearTimeout(this.releaseTimer);
|
|
256
|
+
this.releaseTimer = null;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
// =============================================================================
|
|
260
|
+
// Device Resolution
|
|
261
|
+
// =============================================================================
|
|
262
|
+
/**
|
|
263
|
+
* Get the transform (position, quaternion) for a device
|
|
264
|
+
*/
|
|
265
|
+
getDeviceTransform(deviceId) {
|
|
266
|
+
switch (deviceId) {
|
|
267
|
+
case 'headset':
|
|
268
|
+
return {
|
|
269
|
+
position: vec3ToObj(this.device.position),
|
|
270
|
+
orientation: quatToObj(this.device.quaternion),
|
|
271
|
+
};
|
|
272
|
+
case 'controller-left': {
|
|
273
|
+
const controller = this.device.controllers.left;
|
|
274
|
+
if (!controller)
|
|
275
|
+
throw new Error('Left controller not available');
|
|
276
|
+
return {
|
|
277
|
+
position: vec3ToObj(controller.position),
|
|
278
|
+
orientation: quatToObj(controller.quaternion),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
case 'controller-right': {
|
|
282
|
+
const controller = this.device.controllers.right;
|
|
283
|
+
if (!controller)
|
|
284
|
+
throw new Error('Right controller not available');
|
|
285
|
+
return {
|
|
286
|
+
position: vec3ToObj(controller.position),
|
|
287
|
+
orientation: quatToObj(controller.quaternion),
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
case 'hand-left': {
|
|
291
|
+
const hand = this.device.hands.left;
|
|
292
|
+
if (!hand)
|
|
293
|
+
throw new Error('Left hand not available');
|
|
294
|
+
return {
|
|
295
|
+
position: vec3ToObj(hand.position),
|
|
296
|
+
orientation: quatToObj(hand.quaternion),
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
case 'hand-right': {
|
|
300
|
+
const hand = this.device.hands.right;
|
|
301
|
+
if (!hand)
|
|
302
|
+
throw new Error('Right hand not available');
|
|
303
|
+
return {
|
|
304
|
+
position: vec3ToObj(hand.position),
|
|
305
|
+
orientation: quatToObj(hand.quaternion),
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
default:
|
|
309
|
+
throw new Error(`Unknown device: ${deviceId}`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Set the transform for a device
|
|
314
|
+
*/
|
|
315
|
+
setDeviceTransform(deviceId, position, orientation) {
|
|
316
|
+
switch (deviceId) {
|
|
317
|
+
case 'headset':
|
|
318
|
+
if (position) {
|
|
319
|
+
this.device.position.set(position.x, position.y, position.z);
|
|
320
|
+
}
|
|
321
|
+
if (orientation) {
|
|
322
|
+
this.device.quaternion.set(orientation.x, orientation.y, orientation.z, orientation.w);
|
|
323
|
+
}
|
|
324
|
+
break;
|
|
325
|
+
case 'controller-left': {
|
|
326
|
+
const controller = this.device.controllers.left;
|
|
327
|
+
if (!controller)
|
|
328
|
+
throw new Error('Left controller not available');
|
|
329
|
+
if (position) {
|
|
330
|
+
controller.position.set(position.x, position.y, position.z);
|
|
331
|
+
}
|
|
332
|
+
if (orientation) {
|
|
333
|
+
controller.quaternion.set(orientation.x, orientation.y, orientation.z, orientation.w);
|
|
334
|
+
}
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
case 'controller-right': {
|
|
338
|
+
const controller = this.device.controllers.right;
|
|
339
|
+
if (!controller)
|
|
340
|
+
throw new Error('Right controller not available');
|
|
341
|
+
if (position) {
|
|
342
|
+
controller.position.set(position.x, position.y, position.z);
|
|
343
|
+
}
|
|
344
|
+
if (orientation) {
|
|
345
|
+
controller.quaternion.set(orientation.x, orientation.y, orientation.z, orientation.w);
|
|
346
|
+
}
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
case 'hand-left': {
|
|
350
|
+
const hand = this.device.hands.left;
|
|
351
|
+
if (!hand)
|
|
352
|
+
throw new Error('Left hand not available');
|
|
353
|
+
if (position) {
|
|
354
|
+
hand.position.set(position.x, position.y, position.z);
|
|
355
|
+
}
|
|
356
|
+
if (orientation) {
|
|
357
|
+
hand.quaternion.set(orientation.x, orientation.y, orientation.z, orientation.w);
|
|
358
|
+
}
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
case 'hand-right': {
|
|
362
|
+
const hand = this.device.hands.right;
|
|
363
|
+
if (!hand)
|
|
364
|
+
throw new Error('Right hand not available');
|
|
365
|
+
if (position) {
|
|
366
|
+
hand.position.set(position.x, position.y, position.z);
|
|
367
|
+
}
|
|
368
|
+
if (orientation) {
|
|
369
|
+
hand.quaternion.set(orientation.x, orientation.y, orientation.z, orientation.w);
|
|
370
|
+
}
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
default:
|
|
374
|
+
throw new Error(`Unknown device: ${deviceId}`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Transform a position from XR-origin-relative coordinates to GlobalSpace.
|
|
379
|
+
* The XR origin is defined by the first reference space requested by the app.
|
|
380
|
+
* This is necessary because device positions are in GlobalSpace, but positions
|
|
381
|
+
* from get_object_transform are relative to the XR origin.
|
|
382
|
+
*/
|
|
383
|
+
transformXROriginToGlobal(position) {
|
|
384
|
+
var _a, _b;
|
|
385
|
+
const session = this.device.activeSession;
|
|
386
|
+
if (!session) {
|
|
387
|
+
return position;
|
|
388
|
+
}
|
|
389
|
+
const refSpaces = (_a = session[P_SESSION]) === null || _a === void 0 ? void 0 : _a.referenceSpaces;
|
|
390
|
+
if (!refSpaces || refSpaces.length === 0) {
|
|
391
|
+
return position;
|
|
392
|
+
}
|
|
393
|
+
// Use the first reference space (primary one requested by app)
|
|
394
|
+
const primaryRefSpace = refSpaces[0];
|
|
395
|
+
const offsetMatrix = (_b = primaryRefSpace[P_SPACE]) === null || _b === void 0 ? void 0 : _b.offsetMatrix;
|
|
396
|
+
if (!offsetMatrix) {
|
|
397
|
+
return position;
|
|
398
|
+
}
|
|
399
|
+
// Transform position from XR-origin space to GlobalSpace
|
|
400
|
+
const posVec = vec3.fromValues(position.x, position.y, position.z);
|
|
401
|
+
vec3.transformMat4(posVec, posVec, offsetMatrix);
|
|
402
|
+
return {
|
|
403
|
+
x: posVec[0],
|
|
404
|
+
y: posVec[1],
|
|
405
|
+
z: posVec[2],
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Get the select value for an input device (trigger for controller, pinch for hand)
|
|
410
|
+
*/
|
|
411
|
+
getDeviceSelectValue(deviceId) {
|
|
412
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
413
|
+
switch (deviceId) {
|
|
414
|
+
case 'controller-left':
|
|
415
|
+
return (_b = (_a = this.device.controllers.left) === null || _a === void 0 ? void 0 : _a.getButtonValue('trigger')) !== null && _b !== void 0 ? _b : 0;
|
|
416
|
+
case 'controller-right':
|
|
417
|
+
return (_d = (_c = this.device.controllers.right) === null || _c === void 0 ? void 0 : _c.getButtonValue('trigger')) !== null && _d !== void 0 ? _d : 0;
|
|
418
|
+
case 'hand-left':
|
|
419
|
+
return (_f = (_e = this.device.hands.left) === null || _e === void 0 ? void 0 : _e.pinchValue) !== null && _f !== void 0 ? _f : 0;
|
|
420
|
+
case 'hand-right':
|
|
421
|
+
return (_h = (_g = this.device.hands.right) === null || _g === void 0 ? void 0 : _g.pinchValue) !== null && _h !== void 0 ? _h : 0;
|
|
422
|
+
default:
|
|
423
|
+
throw new Error(`Unknown input device: ${deviceId}`);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Set the select value for an input device
|
|
428
|
+
*/
|
|
429
|
+
setDeviceSelectValue(deviceId, value) {
|
|
430
|
+
var _a, _b, _c, _d;
|
|
431
|
+
switch (deviceId) {
|
|
432
|
+
case 'controller-left':
|
|
433
|
+
(_a = this.device.controllers.left) === null || _a === void 0 ? void 0 : _a.updateButtonValue('trigger', value);
|
|
434
|
+
break;
|
|
435
|
+
case 'controller-right':
|
|
436
|
+
(_b = this.device.controllers.right) === null || _b === void 0 ? void 0 : _b.updateButtonValue('trigger', value);
|
|
437
|
+
break;
|
|
438
|
+
case 'hand-left':
|
|
439
|
+
(_c = this.device.hands.left) === null || _c === void 0 ? void 0 : _c.updatePinchValue(value);
|
|
440
|
+
break;
|
|
441
|
+
case 'hand-right':
|
|
442
|
+
(_d = this.device.hands.right) === null || _d === void 0 ? void 0 : _d.updatePinchValue(value);
|
|
443
|
+
break;
|
|
444
|
+
default:
|
|
445
|
+
throw new Error(`Unknown input device: ${deviceId}`);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Set connected state for an input device
|
|
450
|
+
*/
|
|
451
|
+
setDeviceConnected(deviceId, connected) {
|
|
452
|
+
switch (deviceId) {
|
|
453
|
+
case 'controller-left':
|
|
454
|
+
if (this.device.controllers.left) {
|
|
455
|
+
this.device.controllers.left.connected = connected;
|
|
456
|
+
}
|
|
457
|
+
break;
|
|
458
|
+
case 'controller-right':
|
|
459
|
+
if (this.device.controllers.right) {
|
|
460
|
+
this.device.controllers.right.connected = connected;
|
|
461
|
+
}
|
|
462
|
+
break;
|
|
463
|
+
case 'hand-left':
|
|
464
|
+
if (this.device.hands.left) {
|
|
465
|
+
this.device.hands.left.connected = connected;
|
|
466
|
+
}
|
|
467
|
+
break;
|
|
468
|
+
case 'hand-right':
|
|
469
|
+
if (this.device.hands.right) {
|
|
470
|
+
this.device.hands.right.connected = connected;
|
|
471
|
+
}
|
|
472
|
+
break;
|
|
473
|
+
default:
|
|
474
|
+
throw new Error(`Unknown input device: ${deviceId}`);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
// =============================================================================
|
|
478
|
+
// Discrete Action Execution
|
|
479
|
+
// =============================================================================
|
|
480
|
+
executeDiscreteAction(action) {
|
|
481
|
+
const { method, params } = action;
|
|
482
|
+
switch (method) {
|
|
483
|
+
// Session tools
|
|
484
|
+
case 'get_session_status':
|
|
485
|
+
return this.executeGetSessionStatus();
|
|
486
|
+
case 'accept_session':
|
|
487
|
+
return this.executeAcceptSession();
|
|
488
|
+
case 'end_session':
|
|
489
|
+
return this.executeEndSession();
|
|
490
|
+
// Transform tools
|
|
491
|
+
case 'get_transform':
|
|
492
|
+
return this.executeGetTransform(params);
|
|
493
|
+
case 'set_transform':
|
|
494
|
+
return this.executeSetTransform(params);
|
|
495
|
+
case 'look_at':
|
|
496
|
+
return this.executeLookAt(params);
|
|
497
|
+
// Input tools
|
|
498
|
+
case 'set_input_mode':
|
|
499
|
+
return this.executeSetInputMode(params);
|
|
500
|
+
case 'set_connected':
|
|
501
|
+
return this.executeSetConnected(params);
|
|
502
|
+
case 'get_select_value':
|
|
503
|
+
return this.executeGetSelectValue(params);
|
|
504
|
+
case 'set_select_value':
|
|
505
|
+
return this.executeSetSelectValue(params);
|
|
506
|
+
// Gamepad tools
|
|
507
|
+
case 'get_gamepad_state':
|
|
508
|
+
return this.executeGetGamepadState(params);
|
|
509
|
+
case 'set_gamepad_state':
|
|
510
|
+
return this.executeSetGamepadState(params);
|
|
511
|
+
// State tools
|
|
512
|
+
case 'get_device_state':
|
|
513
|
+
return this.executeGetDeviceState();
|
|
514
|
+
case 'set_device_state':
|
|
515
|
+
return this.executeSetDeviceState(params);
|
|
516
|
+
case 'capture_canvas':
|
|
517
|
+
return this.executeCaptureCanvas(params);
|
|
518
|
+
// Internal select sequence actions
|
|
519
|
+
case '_select_press': {
|
|
520
|
+
const deviceId = params.device;
|
|
521
|
+
this.setDeviceSelectValue(deviceId, 1);
|
|
522
|
+
return undefined;
|
|
523
|
+
}
|
|
524
|
+
case '_select_release': {
|
|
525
|
+
const deviceId = params.device;
|
|
526
|
+
this.setDeviceSelectValue(deviceId, 0);
|
|
527
|
+
return undefined;
|
|
528
|
+
}
|
|
529
|
+
default:
|
|
530
|
+
throw new Error(`Unknown method: ${method}`);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
// =============================================================================
|
|
534
|
+
// Session Tool Implementations
|
|
535
|
+
// =============================================================================
|
|
536
|
+
executeGetSessionStatus() {
|
|
537
|
+
const session = this.device.activeSession;
|
|
538
|
+
return {
|
|
539
|
+
deviceName: this.device.name,
|
|
540
|
+
isRuntimeInstalled: true,
|
|
541
|
+
sessionActive: !!session,
|
|
542
|
+
sessionOffered: this.device.sessionOffered,
|
|
543
|
+
sessionMode: session ? session.mode : null,
|
|
544
|
+
enabledFeatures: session
|
|
545
|
+
? Array.from(session.enabledFeatures || [])
|
|
546
|
+
: [],
|
|
547
|
+
visibilityState: this.device.visibilityState,
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
executeAcceptSession() {
|
|
551
|
+
if (!this.device.sessionOffered) {
|
|
552
|
+
throw new Error('No session has been offered');
|
|
553
|
+
}
|
|
554
|
+
this.device.grantOfferedSession();
|
|
555
|
+
// Session activation is async - caller should use get_session_status to poll
|
|
556
|
+
return { success: true };
|
|
557
|
+
}
|
|
558
|
+
executeEndSession() {
|
|
559
|
+
const session = this.device.activeSession;
|
|
560
|
+
if (!session) {
|
|
561
|
+
throw new Error('No active session');
|
|
562
|
+
}
|
|
563
|
+
session.end();
|
|
564
|
+
return { success: true };
|
|
565
|
+
}
|
|
566
|
+
// =============================================================================
|
|
567
|
+
// Transform Tool Implementations
|
|
568
|
+
// =============================================================================
|
|
569
|
+
executeGetTransform(params) {
|
|
570
|
+
const { device: deviceId } = params;
|
|
571
|
+
const transform = this.getDeviceTransform(deviceId);
|
|
572
|
+
return {
|
|
573
|
+
device: deviceId,
|
|
574
|
+
position: transform.position,
|
|
575
|
+
orientation: transform.orientation,
|
|
576
|
+
euler: quatToEuler(transform.orientation),
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
executeSetTransform(params) {
|
|
580
|
+
const { device: deviceId, position, orientation } = params;
|
|
581
|
+
const targetOrientation = orientation
|
|
582
|
+
? normalizeOrientation(orientation)
|
|
583
|
+
: undefined;
|
|
584
|
+
this.setDeviceTransform(deviceId, position, targetOrientation);
|
|
585
|
+
const newTransform = this.getDeviceTransform(deviceId);
|
|
586
|
+
return {
|
|
587
|
+
device: deviceId,
|
|
588
|
+
position: newTransform.position,
|
|
589
|
+
orientation: newTransform.orientation,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
executeLookAt(params) {
|
|
593
|
+
const { device: deviceId, target, moveToDistance } = params;
|
|
594
|
+
const currentTransform = this.getDeviceTransform(deviceId);
|
|
595
|
+
// Transform target from XR-origin-relative to GlobalSpace
|
|
596
|
+
const targetInGlobal = this.transformXROriginToGlobal(target);
|
|
597
|
+
// Calculate direction to target
|
|
598
|
+
const direction = directionTo(currentTransform.position, targetInGlobal);
|
|
599
|
+
// Calculate look rotation
|
|
600
|
+
// Use gimbal rotation for headset (keeps it level, no roll)
|
|
601
|
+
// Use standard lookRotation for controllers/hands (can tilt freely)
|
|
602
|
+
const lookQuat = deviceId === 'headset'
|
|
603
|
+
? lookRotationGimbal(direction)
|
|
604
|
+
: lookRotation(direction);
|
|
605
|
+
// Optionally move to a specific distance from target
|
|
606
|
+
let newPosition;
|
|
607
|
+
if (moveToDistance !== undefined) {
|
|
608
|
+
newPosition = {
|
|
609
|
+
x: targetInGlobal.x - direction.x * moveToDistance,
|
|
610
|
+
y: targetInGlobal.y - direction.y * moveToDistance,
|
|
611
|
+
z: targetInGlobal.z - direction.z * moveToDistance,
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
this.setDeviceTransform(deviceId, newPosition, lookQuat);
|
|
615
|
+
const newTransform = this.getDeviceTransform(deviceId);
|
|
616
|
+
return {
|
|
617
|
+
device: deviceId,
|
|
618
|
+
position: newTransform.position,
|
|
619
|
+
orientation: newTransform.orientation,
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
// =============================================================================
|
|
623
|
+
// Input Tool Implementations
|
|
624
|
+
// =============================================================================
|
|
625
|
+
executeSetInputMode(params) {
|
|
626
|
+
var _a, _b, _c, _d;
|
|
627
|
+
const { mode } = params;
|
|
628
|
+
this.device.primaryInputMode = mode;
|
|
629
|
+
const activeDevices = [];
|
|
630
|
+
if (mode === 'controller') {
|
|
631
|
+
if ((_a = this.device.controllers.left) === null || _a === void 0 ? void 0 : _a.connected) {
|
|
632
|
+
activeDevices.push('controller-left');
|
|
633
|
+
}
|
|
634
|
+
if ((_b = this.device.controllers.right) === null || _b === void 0 ? void 0 : _b.connected) {
|
|
635
|
+
activeDevices.push('controller-right');
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
if ((_c = this.device.hands.left) === null || _c === void 0 ? void 0 : _c.connected) {
|
|
640
|
+
activeDevices.push('hand-left');
|
|
641
|
+
}
|
|
642
|
+
if ((_d = this.device.hands.right) === null || _d === void 0 ? void 0 : _d.connected) {
|
|
643
|
+
activeDevices.push('hand-right');
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return { mode, activeDevices };
|
|
647
|
+
}
|
|
648
|
+
executeSetConnected(params) {
|
|
649
|
+
const { device: deviceId, connected } = params;
|
|
650
|
+
this.setDeviceConnected(deviceId, connected);
|
|
651
|
+
return { device: deviceId, connected };
|
|
652
|
+
}
|
|
653
|
+
executeGetSelectValue(params) {
|
|
654
|
+
const { device: deviceId } = params;
|
|
655
|
+
const value = this.getDeviceSelectValue(deviceId);
|
|
656
|
+
return { device: deviceId, value };
|
|
657
|
+
}
|
|
658
|
+
executeSetSelectValue(params) {
|
|
659
|
+
const { device: deviceId, value } = params;
|
|
660
|
+
this.setDeviceSelectValue(deviceId, value);
|
|
661
|
+
return { device: deviceId, value };
|
|
662
|
+
}
|
|
663
|
+
// =============================================================================
|
|
664
|
+
// Gamepad Tool Implementations
|
|
665
|
+
// =============================================================================
|
|
666
|
+
executeGetGamepadState(params) {
|
|
667
|
+
const { device: deviceId } = params;
|
|
668
|
+
const hand = deviceId === 'controller-left' ? 'left' : 'right';
|
|
669
|
+
const controller = this.device.controllers[hand];
|
|
670
|
+
if (!controller) {
|
|
671
|
+
throw new Error(`Controller ${hand} not available`);
|
|
672
|
+
}
|
|
673
|
+
// Button layout for Meta Quest Touch Plus controllers
|
|
674
|
+
// Use hand-conditional internal names for lookup
|
|
675
|
+
const buttonInternalNames = [
|
|
676
|
+
'trigger',
|
|
677
|
+
'squeeze',
|
|
678
|
+
'thumbstick',
|
|
679
|
+
hand === 'left' ? 'x-button' : 'a-button',
|
|
680
|
+
hand === 'left' ? 'y-button' : 'b-button',
|
|
681
|
+
'thumbrest',
|
|
682
|
+
];
|
|
683
|
+
const buttons = buttonInternalNames.map((name, index) => ({
|
|
684
|
+
index,
|
|
685
|
+
name: name
|
|
686
|
+
.replace('x-button', 'x')
|
|
687
|
+
.replace('y-button', 'y')
|
|
688
|
+
.replace('a-button', 'a')
|
|
689
|
+
.replace('b-button', 'b'),
|
|
690
|
+
value: controller.getButtonValue(name),
|
|
691
|
+
touched: controller.getButtonTouched(name),
|
|
692
|
+
pressed: controller.getButtonValue(name) > 0.5,
|
|
693
|
+
}));
|
|
694
|
+
const axesData = controller.getAxes();
|
|
695
|
+
const axes = [
|
|
696
|
+
{ index: 0, name: 'thumbstick-x', value: axesData.x },
|
|
697
|
+
{ index: 1, name: 'thumbstick-y', value: axesData.y },
|
|
698
|
+
];
|
|
699
|
+
return {
|
|
700
|
+
device: deviceId,
|
|
701
|
+
connected: controller.connected,
|
|
702
|
+
buttons,
|
|
703
|
+
axes,
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
executeSetGamepadState(params) {
|
|
707
|
+
const { device: deviceId, buttons, axes } = params;
|
|
708
|
+
const hand = deviceId === 'controller-left' ? 'left' : 'right';
|
|
709
|
+
const controller = this.device.controllers[hand];
|
|
710
|
+
if (!controller) {
|
|
711
|
+
throw new Error(`Controller ${hand} not available`);
|
|
712
|
+
}
|
|
713
|
+
let buttonsSet = 0;
|
|
714
|
+
let axesSet = 0;
|
|
715
|
+
// Button index to name mapping
|
|
716
|
+
const buttonIndexToName = [
|
|
717
|
+
'trigger',
|
|
718
|
+
'squeeze',
|
|
719
|
+
'thumbstick',
|
|
720
|
+
hand === 'left' ? 'x-button' : 'a-button',
|
|
721
|
+
hand === 'left' ? 'y-button' : 'b-button',
|
|
722
|
+
'thumbrest',
|
|
723
|
+
];
|
|
724
|
+
if (buttons) {
|
|
725
|
+
for (const btn of buttons) {
|
|
726
|
+
const buttonName = buttonIndexToName[btn.index];
|
|
727
|
+
if (buttonName) {
|
|
728
|
+
// Use updateButtonValue for proper event triggering
|
|
729
|
+
controller.updateButtonValue(buttonName, btn.value);
|
|
730
|
+
if (btn.touched !== undefined) {
|
|
731
|
+
controller.updateButtonTouch(buttonName, btn.touched);
|
|
732
|
+
}
|
|
733
|
+
buttonsSet++;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
if (axes) {
|
|
738
|
+
let xValue;
|
|
739
|
+
let yValue;
|
|
740
|
+
for (const axis of axes) {
|
|
741
|
+
if (axis.index === 0) {
|
|
742
|
+
xValue = axis.value;
|
|
743
|
+
axesSet++;
|
|
744
|
+
}
|
|
745
|
+
else if (axis.index === 1) {
|
|
746
|
+
yValue = axis.value;
|
|
747
|
+
axesSet++;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
if (xValue !== undefined || yValue !== undefined) {
|
|
751
|
+
const currentAxes = controller.getAxes();
|
|
752
|
+
controller.updateAxes('thumbstick', xValue !== null && xValue !== void 0 ? xValue : currentAxes.x, yValue !== null && yValue !== void 0 ? yValue : currentAxes.y);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
return { device: deviceId, buttonsSet, axesSet };
|
|
756
|
+
}
|
|
757
|
+
// =============================================================================
|
|
758
|
+
// State Tool Implementations
|
|
759
|
+
// =============================================================================
|
|
760
|
+
executeGetDeviceState() {
|
|
761
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
|
|
762
|
+
return {
|
|
763
|
+
headset: {
|
|
764
|
+
position: vec3ToObj(this.device.position),
|
|
765
|
+
orientation: quatToObj(this.device.quaternion),
|
|
766
|
+
},
|
|
767
|
+
inputMode: this.device.primaryInputMode,
|
|
768
|
+
controllers: {
|
|
769
|
+
left: {
|
|
770
|
+
connected: (_b = (_a = this.device.controllers.left) === null || _a === void 0 ? void 0 : _a.connected) !== null && _b !== void 0 ? _b : false,
|
|
771
|
+
position: vec3ToObj((_d = (_c = this.device.controllers.left) === null || _c === void 0 ? void 0 : _c.position) !== null && _d !== void 0 ? _d : { x: 0, y: 0, z: 0 }),
|
|
772
|
+
orientation: quatToObj((_f = (_e = this.device.controllers.left) === null || _e === void 0 ? void 0 : _e.quaternion) !== null && _f !== void 0 ? _f : {
|
|
773
|
+
x: 0,
|
|
774
|
+
y: 0,
|
|
775
|
+
z: 0,
|
|
776
|
+
w: 1,
|
|
777
|
+
}),
|
|
778
|
+
},
|
|
779
|
+
right: {
|
|
780
|
+
connected: (_h = (_g = this.device.controllers.right) === null || _g === void 0 ? void 0 : _g.connected) !== null && _h !== void 0 ? _h : false,
|
|
781
|
+
position: vec3ToObj((_k = (_j = this.device.controllers.right) === null || _j === void 0 ? void 0 : _j.position) !== null && _k !== void 0 ? _k : { x: 0, y: 0, z: 0 }),
|
|
782
|
+
orientation: quatToObj((_m = (_l = this.device.controllers.right) === null || _l === void 0 ? void 0 : _l.quaternion) !== null && _m !== void 0 ? _m : {
|
|
783
|
+
x: 0,
|
|
784
|
+
y: 0,
|
|
785
|
+
z: 0,
|
|
786
|
+
w: 1,
|
|
787
|
+
}),
|
|
788
|
+
},
|
|
789
|
+
},
|
|
790
|
+
hands: {
|
|
791
|
+
left: {
|
|
792
|
+
connected: (_p = (_o = this.device.hands.left) === null || _o === void 0 ? void 0 : _o.connected) !== null && _p !== void 0 ? _p : false,
|
|
793
|
+
position: vec3ToObj((_r = (_q = this.device.hands.left) === null || _q === void 0 ? void 0 : _q.position) !== null && _r !== void 0 ? _r : { x: 0, y: 0, z: 0 }),
|
|
794
|
+
orientation: quatToObj((_t = (_s = this.device.hands.left) === null || _s === void 0 ? void 0 : _s.quaternion) !== null && _t !== void 0 ? _t : { x: 0, y: 0, z: 0, w: 1 }),
|
|
795
|
+
},
|
|
796
|
+
right: {
|
|
797
|
+
connected: (_v = (_u = this.device.hands.right) === null || _u === void 0 ? void 0 : _u.connected) !== null && _v !== void 0 ? _v : false,
|
|
798
|
+
position: vec3ToObj((_x = (_w = this.device.hands.right) === null || _w === void 0 ? void 0 : _w.position) !== null && _x !== void 0 ? _x : { x: 0, y: 0, z: 0 }),
|
|
799
|
+
orientation: quatToObj((_z = (_y = this.device.hands.right) === null || _y === void 0 ? void 0 : _y.quaternion) !== null && _z !== void 0 ? _z : { x: 0, y: 0, z: 0, w: 1 }),
|
|
800
|
+
},
|
|
801
|
+
},
|
|
802
|
+
stereoEnabled: this.device.stereoEnabled,
|
|
803
|
+
fov: this.device.fovy * (180 / Math.PI), // Convert to degrees
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
executeSetDeviceState(params) {
|
|
807
|
+
const { state } = params;
|
|
808
|
+
if (!state) {
|
|
809
|
+
// Reset to initial state
|
|
810
|
+
this.device.position.set(0, 1.6, 0);
|
|
811
|
+
this.device.quaternion.set(0, 0, 0, 1);
|
|
812
|
+
this.device.primaryInputMode = 'controller';
|
|
813
|
+
this.device.stereoEnabled = false;
|
|
814
|
+
// Reset controllers and hands to default positions
|
|
815
|
+
if (this.device.controllers.left) {
|
|
816
|
+
this.device.controllers.left.position.set(-0.2, 1.4, -0.3);
|
|
817
|
+
this.device.controllers.left.quaternion.set(0, 0, 0, 1);
|
|
818
|
+
this.device.controllers.left.connected = true;
|
|
819
|
+
}
|
|
820
|
+
if (this.device.controllers.right) {
|
|
821
|
+
this.device.controllers.right.position.set(0.2, 1.4, -0.3);
|
|
822
|
+
this.device.controllers.right.quaternion.set(0, 0, 0, 1);
|
|
823
|
+
this.device.controllers.right.connected = true;
|
|
824
|
+
}
|
|
825
|
+
if (this.device.hands.left) {
|
|
826
|
+
this.device.hands.left.position.set(-0.15, 1.3, -0.4);
|
|
827
|
+
this.device.hands.left.quaternion.set(0, 0, 0, 1);
|
|
828
|
+
this.device.hands.left.connected = true;
|
|
829
|
+
}
|
|
830
|
+
if (this.device.hands.right) {
|
|
831
|
+
this.device.hands.right.position.set(0.15, 1.3, -0.4);
|
|
832
|
+
this.device.hands.right.quaternion.set(0, 0, 0, 1);
|
|
833
|
+
this.device.hands.right.connected = true;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
else {
|
|
837
|
+
// Apply partial state
|
|
838
|
+
if (state.headset) {
|
|
839
|
+
if (state.headset.position) {
|
|
840
|
+
this.device.position.set(state.headset.position.x, state.headset.position.y, state.headset.position.z);
|
|
841
|
+
}
|
|
842
|
+
if (state.headset.orientation) {
|
|
843
|
+
this.device.quaternion.set(state.headset.orientation.x, state.headset.orientation.y, state.headset.orientation.z, state.headset.orientation.w);
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
if (state.inputMode !== undefined) {
|
|
847
|
+
this.device.primaryInputMode = state.inputMode;
|
|
848
|
+
}
|
|
849
|
+
if (state.stereoEnabled !== undefined) {
|
|
850
|
+
this.device.stereoEnabled = state.stereoEnabled;
|
|
851
|
+
}
|
|
852
|
+
if (state.fov !== undefined) {
|
|
853
|
+
this.device.fovy = state.fov * (Math.PI / 180); // Convert to radians
|
|
854
|
+
}
|
|
855
|
+
if (state.controllers) {
|
|
856
|
+
this.applyInputState('controller-left', state.controllers.left);
|
|
857
|
+
this.applyInputState('controller-right', state.controllers.right);
|
|
858
|
+
}
|
|
859
|
+
if (state.hands) {
|
|
860
|
+
this.applyInputState('hand-left', state.hands.left);
|
|
861
|
+
this.applyInputState('hand-right', state.hands.right);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
return { state: this.executeGetDeviceState() };
|
|
865
|
+
}
|
|
866
|
+
applyInputState(deviceId, state) {
|
|
867
|
+
if (!state)
|
|
868
|
+
return;
|
|
869
|
+
if (state.connected !== undefined) {
|
|
870
|
+
this.setDeviceConnected(deviceId, state.connected);
|
|
871
|
+
}
|
|
872
|
+
if (state.position || state.orientation) {
|
|
873
|
+
this.setDeviceTransform(deviceId, state.position, state.orientation);
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
executeCaptureCanvas(params) {
|
|
877
|
+
const { maxWidth = 800, format = 'png', quality = 0.92 } = params;
|
|
878
|
+
// Get the app canvas - try device first, then fallback to DOM query
|
|
879
|
+
let canvas = this.device.appCanvas;
|
|
880
|
+
if (!canvas) {
|
|
881
|
+
// No active session - try to find the canvas in the DOM
|
|
882
|
+
// Before XR session, only the app's canvas is in the DOM
|
|
883
|
+
// (IWER's canvases are not added until session starts)
|
|
884
|
+
const canvases = document.querySelectorAll('canvas');
|
|
885
|
+
if (canvases.length === 1) {
|
|
886
|
+
canvas = canvases[0];
|
|
887
|
+
}
|
|
888
|
+
else if (canvases.length > 1) {
|
|
889
|
+
// Multiple canvases - try to find the most likely app canvas
|
|
890
|
+
// Prefer the largest visible canvas
|
|
891
|
+
let bestCanvas = null;
|
|
892
|
+
let bestArea = 0;
|
|
893
|
+
canvases.forEach((c) => {
|
|
894
|
+
const rect = c.getBoundingClientRect();
|
|
895
|
+
const area = rect.width * rect.height;
|
|
896
|
+
if (area > bestArea && rect.width > 0 && rect.height > 0) {
|
|
897
|
+
bestArea = area;
|
|
898
|
+
bestCanvas = c;
|
|
899
|
+
}
|
|
900
|
+
});
|
|
901
|
+
canvas = bestCanvas;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
if (!canvas) {
|
|
905
|
+
throw new Error('No canvas available. Either start an XR session or ensure an app canvas is in the DOM.');
|
|
906
|
+
}
|
|
907
|
+
// Create a temporary canvas for scaling
|
|
908
|
+
const tempCanvas = document.createElement('canvas');
|
|
909
|
+
const ctx = tempCanvas.getContext('2d');
|
|
910
|
+
if (!ctx) {
|
|
911
|
+
throw new Error('Failed to create canvas context');
|
|
912
|
+
}
|
|
913
|
+
// Calculate scaled dimensions
|
|
914
|
+
const aspectRatio = canvas.height / canvas.width;
|
|
915
|
+
const targetWidth = Math.min(canvas.width, maxWidth);
|
|
916
|
+
const targetHeight = Math.round(targetWidth * aspectRatio);
|
|
917
|
+
tempCanvas.width = targetWidth;
|
|
918
|
+
tempCanvas.height = targetHeight;
|
|
919
|
+
// Draw scaled image
|
|
920
|
+
ctx.drawImage(canvas, 0, 0, targetWidth, targetHeight);
|
|
921
|
+
// Convert to base64
|
|
922
|
+
const mimeType = `image/${format}`;
|
|
923
|
+
const dataUrl = tempCanvas.toDataURL(mimeType, quality);
|
|
924
|
+
const imageData = dataUrl.split(',')[1]; // Remove data URL prefix
|
|
925
|
+
return {
|
|
926
|
+
imageData,
|
|
927
|
+
width: targetWidth,
|
|
928
|
+
height: targetHeight,
|
|
929
|
+
format,
|
|
930
|
+
timestamp: Date.now(),
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
// =============================================================================
|
|
934
|
+
// Duration Action Handling
|
|
935
|
+
// =============================================================================
|
|
936
|
+
applyDurationLerpState(action, t) {
|
|
937
|
+
const { startState, targetState, params } = action;
|
|
938
|
+
const deviceId = params.device;
|
|
939
|
+
let newPosition;
|
|
940
|
+
let newOrientation;
|
|
941
|
+
if (startState.position && targetState.position) {
|
|
942
|
+
newPosition = lerpVec3(startState.position, targetState.position, t);
|
|
943
|
+
}
|
|
944
|
+
if (startState.orientation && targetState.orientation) {
|
|
945
|
+
newOrientation = slerpQuat(startState.orientation, targetState.orientation, t);
|
|
946
|
+
}
|
|
947
|
+
this.setDeviceTransform(deviceId, newPosition, newOrientation);
|
|
948
|
+
}
|
|
949
|
+
applyDurationFinalState(action) {
|
|
950
|
+
const { targetState, params } = action;
|
|
951
|
+
const deviceId = params.device;
|
|
952
|
+
this.setDeviceTransform(deviceId, targetState.position, targetState.orientation);
|
|
953
|
+
}
|
|
954
|
+
getDurationResult(action) {
|
|
955
|
+
const { params, elapsedMs } = action;
|
|
956
|
+
const deviceId = params.device;
|
|
957
|
+
const transform = this.getDeviceTransform(deviceId);
|
|
958
|
+
return {
|
|
959
|
+
device: deviceId,
|
|
960
|
+
position: transform.position,
|
|
961
|
+
orientation: transform.orientation,
|
|
962
|
+
actualDuration: elapsedMs / 1000,
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
/**
|
|
966
|
+
* Activate capture mode for programmatic control.
|
|
967
|
+
* Called when active methods are executed.
|
|
968
|
+
*/
|
|
969
|
+
activateCaptureMode() {
|
|
970
|
+
if (!this._isCaptured) {
|
|
971
|
+
this._isCaptured = true;
|
|
972
|
+
this.cancelReleaseTimer();
|
|
973
|
+
this.device.controlMode = 'programmatic';
|
|
974
|
+
}
|
|
975
|
+
// Reset the release timer
|
|
976
|
+
this.startReleaseTimer();
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* Dispatch a method call.
|
|
980
|
+
*
|
|
981
|
+
* Immediate methods (queries, session management) execute synchronously.
|
|
982
|
+
* State-modifying methods require an active session and are queued for frame-synchronized execution.
|
|
983
|
+
*
|
|
984
|
+
* @param method - The method name (e.g., 'get_transform', 'animate_to')
|
|
985
|
+
* @param params - The method parameters
|
|
986
|
+
* @returns Promise that resolves with the method result
|
|
987
|
+
*/
|
|
988
|
+
async dispatch(method, params = {}) {
|
|
989
|
+
var _a;
|
|
990
|
+
// Immediate methods execute synchronously without queue
|
|
991
|
+
if (RemoteControlInterface.IMMEDIATE_METHODS.has(method)) {
|
|
992
|
+
// Active immediate methods trigger capture mode
|
|
993
|
+
if (RemoteControlInterface.ACTIVE_IMMEDIATE_METHODS.has(method)) {
|
|
994
|
+
this.activateCaptureMode();
|
|
995
|
+
}
|
|
996
|
+
return this.executeImmediateMethod(method, params);
|
|
997
|
+
}
|
|
998
|
+
// Methods that modify state require an active session
|
|
999
|
+
if (RemoteControlInterface.SESSION_REQUIRED_METHODS.has(method)) {
|
|
1000
|
+
if (!this.device.activeSession) {
|
|
1001
|
+
throw new Error(`Cannot execute '${method}': No active XR session. ` +
|
|
1002
|
+
`Use 'get_session_status' to check session state, and 'accept_session' to start a session.`);
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
// Handle animate_to specially - it's a duration action
|
|
1006
|
+
if (method === 'animate_to') {
|
|
1007
|
+
const animateParams = params;
|
|
1008
|
+
const currentTransform = this.getDeviceTransform(animateParams.device);
|
|
1009
|
+
const durationMs = ((_a = animateParams.duration) !== null && _a !== void 0 ? _a : 0.5) * 1000;
|
|
1010
|
+
const targetOrientation = animateParams.orientation
|
|
1011
|
+
? normalizeOrientation(animateParams.orientation)
|
|
1012
|
+
: undefined;
|
|
1013
|
+
// Transform target position from XR-origin-relative to GlobalSpace
|
|
1014
|
+
const targetPosition = animateParams.position
|
|
1015
|
+
? this.transformXROriginToGlobal(animateParams.position)
|
|
1016
|
+
: undefined;
|
|
1017
|
+
return this.enqueueDuration(method, params, durationMs, {
|
|
1018
|
+
position: animateParams.position
|
|
1019
|
+
? currentTransform.position
|
|
1020
|
+
: undefined,
|
|
1021
|
+
orientation: targetOrientation
|
|
1022
|
+
? currentTransform.orientation
|
|
1023
|
+
: undefined,
|
|
1024
|
+
}, {
|
|
1025
|
+
position: targetPosition,
|
|
1026
|
+
orientation: targetOrientation,
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
// Handle select specially - it's a discrete action that enqueues multiple sub-actions
|
|
1030
|
+
if (method === 'select') {
|
|
1031
|
+
const selectParams = params;
|
|
1032
|
+
return this.executeSelectSequence(selectParams);
|
|
1033
|
+
}
|
|
1034
|
+
// All other methods are discrete actions that go through the queue
|
|
1035
|
+
return this.enqueueDiscrete(method, params);
|
|
1036
|
+
}
|
|
1037
|
+
/**
|
|
1038
|
+
* Execute an immediate method synchronously (not queued).
|
|
1039
|
+
* Used for queries and session management that must work outside XR frames.
|
|
1040
|
+
*/
|
|
1041
|
+
executeImmediateMethod(method, params) {
|
|
1042
|
+
switch (method) {
|
|
1043
|
+
case 'get_session_status':
|
|
1044
|
+
return this.executeGetSessionStatus();
|
|
1045
|
+
case 'accept_session':
|
|
1046
|
+
return this.executeAcceptSession();
|
|
1047
|
+
case 'end_session':
|
|
1048
|
+
return this.executeEndSession();
|
|
1049
|
+
case 'get_transform':
|
|
1050
|
+
return this.executeGetTransform(params);
|
|
1051
|
+
case 'get_select_value':
|
|
1052
|
+
return this.executeGetSelectValue(params);
|
|
1053
|
+
case 'get_gamepad_state':
|
|
1054
|
+
return this.executeGetGamepadState(params);
|
|
1055
|
+
case 'get_device_state':
|
|
1056
|
+
return this.executeGetDeviceState();
|
|
1057
|
+
case 'capture_canvas':
|
|
1058
|
+
return this.executeCaptureCanvas(params);
|
|
1059
|
+
default:
|
|
1060
|
+
throw new Error(`Unknown immediate method: ${method}`);
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Execute select action - this directly enqueues the three sub-actions without awaiting
|
|
1065
|
+
* The caller's promise resolves when all sub-actions complete
|
|
1066
|
+
*/
|
|
1067
|
+
executeSelectSequence(params) {
|
|
1068
|
+
const { device: deviceId, duration = 0.15 } = params;
|
|
1069
|
+
return new Promise((resolve, reject) => {
|
|
1070
|
+
// Track completion of all three actions
|
|
1071
|
+
let actionsCompleted = 0;
|
|
1072
|
+
const totalActions = 3;
|
|
1073
|
+
const checkComplete = () => {
|
|
1074
|
+
actionsCompleted++;
|
|
1075
|
+
if (actionsCompleted === totalActions) {
|
|
1076
|
+
resolve({
|
|
1077
|
+
device: deviceId,
|
|
1078
|
+
duration,
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
// Enqueue: set value to 1
|
|
1083
|
+
const action1 = {
|
|
1084
|
+
type: 'discrete',
|
|
1085
|
+
id: this.generateActionId(),
|
|
1086
|
+
method: '_select_press',
|
|
1087
|
+
params: { device: deviceId },
|
|
1088
|
+
resolve: checkComplete,
|
|
1089
|
+
reject,
|
|
1090
|
+
};
|
|
1091
|
+
// Enqueue: wait for duration
|
|
1092
|
+
const action2 = {
|
|
1093
|
+
type: 'duration',
|
|
1094
|
+
id: this.generateActionId(),
|
|
1095
|
+
method: '_select_wait',
|
|
1096
|
+
params: { device: deviceId },
|
|
1097
|
+
durationMs: duration * 1000,
|
|
1098
|
+
elapsedMs: 0,
|
|
1099
|
+
startState: {},
|
|
1100
|
+
targetState: {},
|
|
1101
|
+
resolve: checkComplete,
|
|
1102
|
+
reject,
|
|
1103
|
+
};
|
|
1104
|
+
// Enqueue: set value to 0
|
|
1105
|
+
const action3 = {
|
|
1106
|
+
type: 'discrete',
|
|
1107
|
+
id: this.generateActionId(),
|
|
1108
|
+
method: '_select_release',
|
|
1109
|
+
params: { device: deviceId },
|
|
1110
|
+
resolve: checkComplete,
|
|
1111
|
+
reject,
|
|
1112
|
+
};
|
|
1113
|
+
this.commandQueue.push(action1, action2, action3);
|
|
1114
|
+
});
|
|
1115
|
+
}
|
|
1116
|
+
/**
|
|
1117
|
+
* Accept an offered XR session (async wrapper for proper session activation)
|
|
1118
|
+
*/
|
|
1119
|
+
async acceptSession() {
|
|
1120
|
+
if (!this.device.sessionOffered) {
|
|
1121
|
+
throw new Error('No session has been offered');
|
|
1122
|
+
}
|
|
1123
|
+
this.device.grantOfferedSession();
|
|
1124
|
+
// Wait for session to become active
|
|
1125
|
+
await waitForCondition(() => !!this.device.activeSession, 5000);
|
|
1126
|
+
// Just return success - caller can use get_session_status for details
|
|
1127
|
+
return { success: true };
|
|
1128
|
+
}
|
|
1129
|
+
/**
|
|
1130
|
+
* Force release capture mode (for testing/cleanup)
|
|
1131
|
+
*/
|
|
1132
|
+
forceRelease() {
|
|
1133
|
+
this.cancelReleaseTimer();
|
|
1134
|
+
this._isCaptured = false;
|
|
1135
|
+
this.device.controlMode = 'manual';
|
|
1136
|
+
// Clear pending actions
|
|
1137
|
+
for (const action of this.commandQueue) {
|
|
1138
|
+
action.reject(new Error('Capture released'));
|
|
1139
|
+
}
|
|
1140
|
+
this.commandQueue = [];
|
|
1141
|
+
// Reset any stuck select/trigger values
|
|
1142
|
+
for (const hand of ['left', 'right']) {
|
|
1143
|
+
const controller = this.device.controllers[hand];
|
|
1144
|
+
if (controller) {
|
|
1145
|
+
controller.updateButtonValue('trigger', 0);
|
|
1146
|
+
controller.updateButtonValue('squeeze', 0);
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
// =============================================================================
|
|
1152
|
+
// Public API - Dispatch
|
|
1153
|
+
// =============================================================================
|
|
1154
|
+
/**
|
|
1155
|
+
* Set of methods that execute immediately (synchronously) without going through the queue.
|
|
1156
|
+
* These are queries and session management commands that need to work outside of XR frames.
|
|
1157
|
+
*/
|
|
1158
|
+
RemoteControlInterface.IMMEDIATE_METHODS = new Set([
|
|
1159
|
+
// Session management - must work before/after XR session
|
|
1160
|
+
'get_session_status',
|
|
1161
|
+
'accept_session',
|
|
1162
|
+
'end_session',
|
|
1163
|
+
// Pure queries - just read current state
|
|
1164
|
+
'get_transform',
|
|
1165
|
+
'get_select_value',
|
|
1166
|
+
'get_gamepad_state',
|
|
1167
|
+
'get_device_state',
|
|
1168
|
+
// Canvas capture - reads current canvas state
|
|
1169
|
+
'capture_canvas',
|
|
1170
|
+
]);
|
|
1171
|
+
/**
|
|
1172
|
+
* Set of immediate methods that are "active" - they modify state and should trigger capture mode.
|
|
1173
|
+
* Passive methods (queries) should NOT trigger capture mode.
|
|
1174
|
+
*/
|
|
1175
|
+
RemoteControlInterface.ACTIVE_IMMEDIATE_METHODS = new Set([
|
|
1176
|
+
'accept_session',
|
|
1177
|
+
'end_session',
|
|
1178
|
+
]);
|
|
1179
|
+
/**
|
|
1180
|
+
* Set of methods that require an active XR session.
|
|
1181
|
+
* These are state-modifying methods that are processed during frame updates.
|
|
1182
|
+
*/
|
|
1183
|
+
RemoteControlInterface.SESSION_REQUIRED_METHODS = new Set([
|
|
1184
|
+
'set_transform',
|
|
1185
|
+
'look_at',
|
|
1186
|
+
'animate_to',
|
|
1187
|
+
'set_input_mode',
|
|
1188
|
+
'set_connected',
|
|
1189
|
+
'set_select_value',
|
|
1190
|
+
'select',
|
|
1191
|
+
'set_gamepad_state',
|
|
1192
|
+
'set_device_state',
|
|
1193
|
+
]);
|
|
1194
|
+
//# sourceMappingURL=RemoteControlInterface.js.map
|