-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathinput-profile.ts
More file actions
352 lines (315 loc) · 12.1 KB
/
Copy pathinput-profile.ts
File metadata and controls
352 lines (315 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import {Component, Object3D, Emitter} from '@wonderlandengine/api';
import {HandTracking} from './hand-tracking.js';
import {vec3, quat} from 'gl-matrix';
import {property} from '@wonderlandengine/api/decorators.js';
import {VrModeActiveSwitch} from './vr-mode-active-switch.js';
const _tempVec = vec3.create();
const _tempQuat = quat.create();
const _tempRotation1 = new Float32Array(4);
const _tempRotation2 = new Float32Array(4);
const minTemp = new Float32Array(3);
const maxTemp = new Float32Array(3);
const hands = ['left', 'right'];
interface VisualResponse {
target: Object3D;
min: Object3D;
max: Object3D;
id: number;
}
/**
* Dynamically load and map input profiles for XR controllers.
*/
export class InputProfile extends Component {
static TypeName = 'input-profile';
/**
* A cache to store loaded profiles for reuse.
*/
static Cache: Map<string, any> = new Map();
private _gamepadObjects: Record<string, Object3D> = {};
private _controllerModel: Object3D | null = null;
private _defaultControllerComponents: Component[] | undefined;
private _handedness!: string;
private _profileJSON: any = null;
private _buttons: VisualResponse[] = [];
private _axes: VisualResponse[] = [];
/**
* The XR gamepad associated with the current input source.
*/
gamepad: Gamepad | undefined;
/**
* A reference to the emitter which triggered on model lodaed event.
*/
onModelLoaded: Emitter = new Emitter();
/**
* Returns url of input profile json file
*/
url!: string;
/**
* A set of components to filter during component retrieval.
*/
toFilter: Set<string> = new Set(['vr-mode-active-mode-switch']);
/**
* The index representing the handedness of the controller (0 for left, 1 for right).
*/
@property.enum(hands, 0)
handedness: number = 0;
/**
* The base path where XR input profiles are stored.
*/
@property.string(
'https://cdn.jsdelivr.net/npm/@webxr-input-profiles/assets@latest/dist/profiles/'
)
defaultBasePath!: string;
/**
* An optional folder path for loading custom XR input profiles.
*/
@property.string()
customBasePath!: string;
/**
* The default 3D controller model used when a custom model fails to load.
*/
@property.object()
defaultController!: Object3D;
/**
* The object which has HandTracking component added to it.
*/
@property.object()
trackedHand!: Object3D;
/**
* If true, the input profile will be mapped to the default controller, and no dynamic 3D model of controller will be loaded.
*/
@property.bool(false)
mapToDefaultController!: boolean;
/**
* If true, adds a VR mode switch component to the loaded controller model.
*/
@property.bool(true)
addVrModeSwitch!: boolean;
onActivate() {
this._handedness = hands[this.handedness];
const defaultHandName =
'Hand' + this._handedness.charAt(0).toUpperCase() + this._handedness.slice(1);
this.trackedHand =
this.trackedHand ?? this.object.parent?.findByNameRecursive(defaultHandName)[0];
this.defaultController = this.defaultController || this.object.children[0];
this._defaultControllerComponents = this._getComponents(this.defaultController);
this.engine.onXRSessionStart.add(() => {
this.engine.xr?.session.addEventListener(
'inputsourceschange',
this._onInputSourcesChange
);
});
}
onDeactivate() {
this.engine.xr?.session?.removeEventListener(
'inputsourceschange',
this._onInputSourcesChange
);
}
/**
* Sets newly loaded controllers for the HandTracking component to proper switching.
* @param controllerObject The controller object.
* @hidden
*/
private _setHandTrackingControllers(controllerObject: Object3D) {
const handtrackingComponent: HandTracking | null =
this.trackedHand.getComponent(HandTracking);
if (!handtrackingComponent) return;
handtrackingComponent.controllerToDeactivate = controllerObject;
}
/**
* Retrieves all components from the specified object and its children.
* @param obj The object to retrieve components from.
* @return An array of components.
* @hidden
*/
private _getComponents(obj: Object3D | null) {
const components: Component[] = [];
if (obj == null) return components;
const stack: Object3D[] = [obj];
while (stack.length > 0) {
const currentObj = stack.pop()!;
const comps = currentObj
.getComponents()
.filter((c: Component) => !this.toFilter.has(c.type));
components.push(...comps);
const children = currentObj.children;
// Push children onto the stack in reverse order to maintain the correct order
for (let i = children.length - 1; i >= 0; --i) {
stack.push(children[i]);
}
}
return components;
}
/**
* Activates or deactivates components based on the specified boolean value.
* @param active If true, components are set to active; otherwise, they are set to inactive.
* @hidden
*/
private _setComponentsActive(active: boolean) {
const comps = this._defaultControllerComponents;
if (comps == undefined) return;
for (let i = 0; i < comps.length; ++i) {
comps[i].active = active;
}
}
/**
* Event handler triggered when XR input sources change.
* Detects new XR input sources and initiates the loading of input profiles.
* @param event The XR input source change event.
* @hidden
*/
private _onInputSourcesChange = (event: XRInputSourcesChangeEvent) => {
if (this._isModelLoaded() && !this.mapToDefaultController) {
this._setComponentsActive(false);
}
event.added.forEach((xrInputSource: XRInputSource) => {
if (xrInputSource.hand != null) return;
if (this._handedness != xrInputSource.handedness) return;
this.gamepad = xrInputSource.gamepad;
const profile =
this.customBasePath !== ''
? this.customBasePath
: this.defaultBasePath + xrInputSource.profiles[0];
this.url = profile + '/profile.json';
this._profileJSON = InputProfile.Cache.get(this.url) ?? null;
if (this._profileJSON != null) return;
fetch(this.url)
.then((res) => res.json())
.then((out) => {
this._profileJSON = out;
InputProfile.Cache.set(this.url, this._profileJSON);
if (!this._isModelLoaded()) this._loadAndMapGamepad(profile);
})
.catch((e) => {
console.error(`Failed to load profile from ${this.url}. Reason:`, e);
});
});
};
/**
* Checks if the 3D controller model is loaded.
* @return True if the model is loaded; otherwise, false.
* @hidden
*/
private _isModelLoaded() {
return this._controllerModel !== null;
}
/**
* Loads the 3D controller model and caches the mapping to the gamepad.
* @param profile The path to the input profile.
* @hidden
*/
private async _loadAndMapGamepad(profile: string) {
const assetPath = profile + '/' + this._handedness + '.glb';
this._controllerModel = this.defaultController;
if (!this.mapToDefaultController) {
/** load 3d model in the runtime with profile url */
try {
this._controllerModel = (await this.engine.scene.append(
assetPath
)) as Object3D;
} catch (e) {
console.error(
`Failed to load i-p controller model. Reason:`,
e,
`Continuing with ${this._handedness} default controller.`
);
this._setComponentsActive(true);
}
this._controllerModel.parent = this.object;
this._controllerModel.setPositionLocal([0, 0, 0]);
this._setComponentsActive(false);
if (this.addVrModeSwitch)
this._controllerModel.addComponent(VrModeActiveSwitch);
this.onModelLoaded.notify();
}
this._cacheGamepadObjectsFromProfile(this._profileJSON, this._controllerModel);
this._setHandTrackingControllers(this._controllerModel);
this.update = () => this._mapGamepadInput();
}
/**
* Caches gamepad objects (buttons, axes) from the loaded input profile.
* @hidden
*/
private _cacheGamepadObjectsFromProfile(profile: any, obj: Object3D) {
const components = profile.layouts[this._handedness].components;
if (!components) return;
this._buttons = [];
this._axes = [];
for (const i in components) {
const visualResponses = components[i].visualResponses;
if (components[i].rootNodeName === 'menu') continue;
for (const j in visualResponses) {
// update buttons with new interface of current visual response
const visualResponse = visualResponses[j];
const valueNode = visualResponse.valueNodeName;
const minNode = visualResponse.minNodeName;
const maxNode = visualResponse.maxNodeName;
this._gamepadObjects[valueNode] = obj.findByNameRecursive(valueNode)[0];
this._gamepadObjects[minNode] = obj.findByNameRecursive(minNode)[0];
this._gamepadObjects[maxNode] = obj.findByNameRecursive(maxNode)[0];
const prop = visualResponses[j].componentProperty;
const response: VisualResponse = {
target: this._gamepadObjects[valueNode],
min: this._gamepadObjects[minNode],
max: this._gamepadObjects[maxNode],
id: components[i].gamepadIndices[prop], // Assign a unique ID
};
switch (prop) {
case 'button':
this._buttons.push(response);
break;
case 'xAxis':
case 'yAxis':
this._axes.push(response);
break;
}
}
}
}
/**
* Assigns a transformed position and rotation to the target based on minimum and maximum values and a normalized input value.
* @param target The target object to be transformed.
* @param min The minimum object providing transformation limits.
* @param max The maximum object providing transformation limits.
* @param value The normalized input value.
* @hidden
*/
private _assignTransform(
target: Object3D,
min: Object3D,
max: Object3D,
value: number
) {
vec3.lerp(
_tempVec,
min.getPositionWorld(minTemp),
max.getPositionWorld(maxTemp),
value
);
target.setPositionWorld(_tempVec);
quat.lerp(
_tempQuat,
min.getRotationWorld(_tempRotation1),
max.getRotationWorld(_tempRotation2),
value
);
quat.normalize(_tempQuat, _tempQuat);
target.setRotationWorld(_tempQuat);
}
/**
* Maps input values (buttons, axes) to the 3D controller model.
* @hidden
*/
private _mapGamepadInput() {
for (const button of this._buttons) {
const buttonValue = this.gamepad!.buttons[button.id].value;
this._assignTransform(button.target, button.min, button.max, buttonValue);
}
for (const axis of this._axes) {
const axisValue = this.gamepad!.axes[axis.id];
const normalizedAxisValue = (axisValue + 1) / 2;
this._assignTransform(axis.target, axis.min, axis.max, normalizedAxisValue);
}
}
}