WebXRHandTracking.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. import { WebXRAbstractFeature } from "./WebXRAbstractFeature";
  2. import { WebXRSessionManager } from "../webXRSessionManager";
  3. import { WebXRFeatureName } from "../webXRFeaturesManager";
  4. import { AbstractMesh } from "../../Meshes/abstractMesh";
  5. import { Mesh } from "../../Meshes/mesh";
  6. import { SphereBuilder } from "../../Meshes/Builders/sphereBuilder";
  7. import { WebXRInput } from "../webXRInput";
  8. import { WebXRInputSource } from "../webXRInputSource";
  9. import { Quaternion } from "../../Maths/math.vector";
  10. import { Nullable } from "../../types";
  11. import { PhysicsImpostor } from "../../Physics/physicsImpostor";
  12. import { WebXRFeaturesManager } from "../webXRFeaturesManager";
  13. import { IDisposable, Scene } from "../../scene";
  14. import { Observable } from "../../Misc/observable";
  15. import { InstancedMesh } from "../../Meshes/instancedMesh";
  16. import { SceneLoader } from "../../Loading/sceneLoader";
  17. import { Color3 } from "../../Maths/math.color";
  18. import { NodeMaterial } from "../../Materials/Node/nodeMaterial";
  19. import { InputBlock } from "../../Materials/Node/Blocks/Input/inputBlock";
  20. import { Material } from "../../Materials/material";
  21. import { Engine } from "../../Engines/engine";
  22. import { Tools } from "../../Misc/tools";
  23. import { Axis } from "../../Maths/math.axis";
  24. import { TransformNode } from "../../Meshes/transformNode";
  25. import { Tags } from "../../Misc/tags";
  26. declare const XRHand: XRHand;
  27. /**
  28. * Configuration interface for the hand tracking feature
  29. */
  30. export interface IWebXRHandTrackingOptions {
  31. /**
  32. * The xrInput that will be used as source for new hands
  33. */
  34. xrInput: WebXRInput;
  35. /**
  36. * Configuration object for the joint meshes
  37. */
  38. jointMeshes?: {
  39. /**
  40. * Should the meshes created be invisible (defaults to false)
  41. */
  42. invisible?: boolean;
  43. /**
  44. * A source mesh to be used to create instances. Defaults to a sphere.
  45. * This mesh will be the source for all other (25) meshes.
  46. * It should have the general size of a single unit, as the instances will be scaled according to the provided radius
  47. */
  48. sourceMesh?: Mesh;
  49. /**
  50. * This function will be called after a mesh was created for a specific joint.
  51. * Using this function you can either manipulate the instance or return a new mesh.
  52. * When returning a new mesh the instance created before will be disposed
  53. */
  54. onHandJointMeshGenerated?: (meshInstance: InstancedMesh, jointId: number, controllerId: string) => Mesh | undefined;
  55. /**
  56. * Should the source mesh stay visible. Defaults to false
  57. */
  58. keepOriginalVisible?: boolean;
  59. /**
  60. * Scale factor for all instances (defaults to 2)
  61. */
  62. scaleFactor?: number;
  63. /**
  64. * Should each instance have its own physics impostor
  65. */
  66. enablePhysics?: boolean;
  67. /**
  68. * If enabled, override default physics properties
  69. */
  70. physicsProps?: { friction?: number; restitution?: number; impostorType?: number };
  71. /**
  72. * Should the default hand mesh be disabled. In this case, the spheres will be visible (unless set invisible).
  73. */
  74. disableDefaultHandMesh?: boolean;
  75. /**
  76. * a rigged hand-mesh that will be updated according to the XRHand data provided. This will override the default hand mesh
  77. */
  78. handMeshes?: {
  79. right: AbstractMesh;
  80. left: AbstractMesh;
  81. };
  82. /**
  83. * If a hand mesh was provided, this array will define what axis will update which node. This will override the default hand mesh
  84. */
  85. rigMapping?: {
  86. right: string[];
  87. left: string[];
  88. };
  89. /**
  90. * The utilityLayer scene that contains the 3D UI elements. Passing this in turns on near interactions with the index finger tip
  91. */
  92. sceneForNearInteraction?: Scene;
  93. };
  94. }
  95. /**
  96. * Parts of the hands divided to writs and finger names
  97. */
  98. export const enum HandPart {
  99. /**
  100. * HandPart - Wrist
  101. */
  102. WRIST = "wrist",
  103. /**
  104. * HandPart - The thumb
  105. */
  106. THUMB = "thumb",
  107. /**
  108. * HandPart - Index finger
  109. */
  110. INDEX = "index",
  111. /**
  112. * HandPart - Middle finger
  113. */
  114. MIDDLE = "middle",
  115. /**
  116. * HandPart - Ring finger
  117. */
  118. RING = "ring",
  119. /**
  120. * HandPart - Little finger
  121. */
  122. LITTLE = "little",
  123. }
  124. /**
  125. * Representing a single hand (with its corresponding native XRHand object)
  126. */
  127. export class WebXRHand implements IDisposable {
  128. private _scene: Scene;
  129. private _defaultHandMesh: boolean = false;
  130. private _transformNodeMapping: TransformNode[] = [];
  131. /**
  132. * Hand-parts definition (key is HandPart)
  133. */
  134. public handPartsDefinition: { [key: string]: number[] };
  135. /**
  136. * Observers will be triggered when the mesh for this hand was initialized.
  137. */
  138. public onHandMeshReadyObservable: Observable<WebXRHand> = new Observable();
  139. /**
  140. * Populate the HandPartsDefinition object.
  141. * This is called as a side effect since certain browsers don't have XRHand defined.
  142. */
  143. private generateHandPartsDefinition(hand: XRHand) {
  144. return {
  145. [HandPart.WRIST]: [hand.WRIST],
  146. [HandPart.THUMB]: [hand.THUMB_METACARPAL, hand.THUMB_PHALANX_PROXIMAL, hand.THUMB_PHALANX_DISTAL, hand.THUMB_PHALANX_TIP],
  147. [HandPart.INDEX]: [hand.INDEX_METACARPAL, hand.INDEX_PHALANX_PROXIMAL, hand.INDEX_PHALANX_INTERMEDIATE, hand.INDEX_PHALANX_DISTAL, hand.INDEX_PHALANX_TIP],
  148. [HandPart.MIDDLE]: [hand.MIDDLE_METACARPAL, hand.MIDDLE_PHALANX_PROXIMAL, hand.MIDDLE_PHALANX_INTERMEDIATE, hand.MIDDLE_PHALANX_DISTAL, hand.MIDDLE_PHALANX_TIP],
  149. [HandPart.RING]: [hand.RING_METACARPAL, hand.RING_PHALANX_PROXIMAL, hand.RING_PHALANX_INTERMEDIATE, hand.RING_PHALANX_DISTAL, hand.RING_PHALANX_TIP],
  150. [HandPart.LITTLE]: [hand.LITTLE_METACARPAL, hand.LITTLE_PHALANX_PROXIMAL, hand.LITTLE_PHALANX_INTERMEDIATE, hand.LITTLE_PHALANX_DISTAL, hand.LITTLE_PHALANX_TIP],
  151. };
  152. }
  153. /**
  154. * Construct a new hand object
  155. * @param xrController the controller to which the hand correlates
  156. * @param trackedMeshes the meshes to be used to track the hand joints
  157. * @param _handMesh an optional hand mesh. if not provided, ours will be used
  158. * @param _rigMapping an optional rig mapping for the hand mesh. if not provided, ours will be used
  159. * @param disableDefaultHandMesh should the default mesh creation be disabled
  160. * @param _nearInteractionMesh as optional mesh used for near interaction collision checking
  161. */
  162. constructor(
  163. /** the controller to which the hand correlates */
  164. public readonly xrController: WebXRInputSource,
  165. /** the meshes to be used to track the hand joints */
  166. public readonly trackedMeshes: AbstractMesh[],
  167. private _handMesh?: AbstractMesh,
  168. private _rigMapping?: string[],
  169. disableDefaultHandMesh?: boolean,
  170. private _nearInteractionMesh?: Nullable<AbstractMesh>
  171. ) {
  172. this.handPartsDefinition = this.generateHandPartsDefinition(xrController.inputSource.hand!);
  173. this._scene = trackedMeshes[0].getScene();
  174. if (this._handMesh && this._rigMapping) {
  175. this._defaultHandMesh = false;
  176. this.onHandMeshReadyObservable.notifyObservers(this);
  177. } else {
  178. if (!disableDefaultHandMesh) {
  179. this._generateDefaultHandMesh();
  180. }
  181. }
  182. // hide the motion controller, if available/loaded
  183. if (this.xrController.motionController) {
  184. if (this.xrController.motionController.rootMesh) {
  185. this.xrController.motionController.rootMesh.setEnabled(false);
  186. } else {
  187. this.xrController.motionController.onModelLoadedObservable.add((controller) => {
  188. if (controller.rootMesh) {
  189. controller.rootMesh.setEnabled(false);
  190. }
  191. });
  192. }
  193. }
  194. this.xrController.onMotionControllerInitObservable.add((motionController) => {
  195. motionController.onModelLoadedObservable.add((controller) => {
  196. if (controller.rootMesh) {
  197. controller.rootMesh.setEnabled(false);
  198. }
  199. });
  200. if (motionController.rootMesh) {
  201. motionController.rootMesh.setEnabled(false);
  202. }
  203. });
  204. }
  205. /**
  206. * Get the hand mesh. It is possible that the hand mesh is not yet ready!
  207. */
  208. public get handMesh() {
  209. return this._handMesh;
  210. }
  211. /**
  212. * Update this hand from the latest xr frame
  213. * @param xrFrame xrFrame to update from
  214. * @param referenceSpace The current viewer reference space
  215. * @param scaleFactor optional scale factor for the meshes
  216. */
  217. public updateFromXRFrame(xrFrame: XRFrame, referenceSpace: XRReferenceSpace, scaleFactor: number = 2) {
  218. const hand = this.xrController.inputSource.hand;
  219. if (!hand) {
  220. return;
  221. }
  222. this.trackedMeshes.forEach((mesh, idx) => {
  223. const xrJoint = hand[idx];
  224. if (xrJoint) {
  225. let pose = xrFrame.getJointPose!(xrJoint, referenceSpace);
  226. if (!pose || !pose.transform) {
  227. return;
  228. }
  229. // get the transformation. can be done with matrix decomposition as well
  230. const pos = pose.transform.position;
  231. const orientation = pose.transform.orientation;
  232. mesh.position.set(pos.x, pos.y, pos.z);
  233. mesh.rotationQuaternion!.set(orientation.x, orientation.y, orientation.z, orientation.w);
  234. // left handed system conversion
  235. // get the radius of the joint. In general it is static, but just in case it does change we update it on each frame.
  236. const radius = (pose.radius || 0.008) * scaleFactor;
  237. mesh.scaling.set(radius, radius, radius);
  238. // now check for the hand mesh
  239. if (this._handMesh && this._rigMapping) {
  240. if (this._rigMapping[idx]) {
  241. this._transformNodeMapping[idx] = this._transformNodeMapping[idx] || this._scene.getTransformNodeByName(this._rigMapping[idx]);
  242. if (this._transformNodeMapping[idx]) {
  243. this._transformNodeMapping[idx].position.copyFrom(mesh.position);
  244. this._transformNodeMapping[idx].rotationQuaternion!.copyFrom(mesh.rotationQuaternion!);
  245. // no scaling at the moment
  246. // this._transformNodeMapping[idx].scaling.copyFrom(mesh.scaling).scaleInPlace(20);
  247. mesh.isVisible = false;
  248. }
  249. }
  250. }
  251. if (!mesh.getScene().useRightHandedSystem) {
  252. mesh.position.z *= -1;
  253. mesh.rotationQuaternion!.z *= -1;
  254. mesh.rotationQuaternion!.w *= -1;
  255. }
  256. }
  257. });
  258. // Update the invisible fingertip collidable
  259. if (this._nearInteractionMesh) {
  260. const indexTipPose = this.trackedMeshes[hand.INDEX_PHALANX_TIP].position;
  261. this._nearInteractionMesh.position.set(indexTipPose.x, indexTipPose.y, indexTipPose.z);
  262. }
  263. }
  264. /**
  265. * Get meshes of part of the hand
  266. * @param part the part of hand to get
  267. * @returns An array of meshes that correlate to the hand part requested
  268. */
  269. public getHandPartMeshes(part: HandPart): AbstractMesh[] {
  270. return this.handPartsDefinition[part].map((idx) => this.trackedMeshes[idx]);
  271. }
  272. /**
  273. * Dispose this Hand object
  274. */
  275. public dispose() {
  276. this.trackedMeshes.forEach((mesh) => mesh.dispose());
  277. this._nearInteractionMesh?.dispose();
  278. this.onHandMeshReadyObservable.clear();
  279. // dispose the hand mesh, if it is the default one
  280. if (this._defaultHandMesh && this._handMesh) {
  281. this._handMesh.dispose();
  282. }
  283. }
  284. private async _generateDefaultHandMesh() {
  285. try {
  286. const handedness = this.xrController.inputSource.handedness === "right" ? "right" : "left";
  287. const filename = `${handedness === "right" ? "r" : "l"}_hand_${this._scene.useRightHandedSystem ? "r" : "l"}hs.glb`;
  288. const loaded = await SceneLoader.ImportMeshAsync("", "https://assets.babylonjs.com/meshes/HandMeshes/", filename, this._scene);
  289. // shader
  290. const handColors = {
  291. base: Color3.FromInts(116, 63, 203),
  292. fresnel: Color3.FromInts(149, 102, 229),
  293. fingerColor: Color3.FromInts(177, 130, 255),
  294. tipFresnel: Color3.FromInts(220, 200, 255),
  295. };
  296. const handShader = new NodeMaterial("leftHandShader", this._scene, { emitComments: false });
  297. await handShader.loadAsync("https://assets.babylonjs.com/meshes/HandMeshes/handsShader.json");
  298. // build node materials
  299. handShader.build(false);
  300. // depth prepass and alpha mode
  301. handShader.needDepthPrePass = true;
  302. handShader.transparencyMode = Material.MATERIAL_ALPHABLEND;
  303. handShader.alphaMode = Engine.ALPHA_COMBINE;
  304. const handNodes = {
  305. base: handShader.getBlockByName("baseColor") as InputBlock,
  306. fresnel: handShader.getBlockByName("fresnelColor") as InputBlock,
  307. fingerColor: handShader.getBlockByName("fingerColor") as InputBlock,
  308. tipFresnel: handShader.getBlockByName("tipFresnelColor") as InputBlock,
  309. };
  310. handNodes.base.value = handColors.base;
  311. handNodes.fresnel.value = handColors.fresnel;
  312. handNodes.fingerColor.value = handColors.fingerColor;
  313. handNodes.tipFresnel.value = handColors.tipFresnel;
  314. loaded.meshes[1].material = handShader;
  315. loaded.meshes[1].alwaysSelectAsActiveMesh = true;
  316. this._defaultHandMesh = true;
  317. this._handMesh = loaded.meshes[0];
  318. this._rigMapping = [
  319. "wrist_",
  320. "thumb_metacarpal_",
  321. "thumb_proxPhalanx_",
  322. "thumb_distPhalanx_",
  323. "thumb_tip_",
  324. "index_metacarpal_",
  325. "index_proxPhalanx_",
  326. "index_intPhalanx_",
  327. "index_distPhalanx_",
  328. "index_tip_",
  329. "middle_metacarpal_",
  330. "middle_proxPhalanx_",
  331. "middle_intPhalanx_",
  332. "middle_distPhalanx_",
  333. "middle_tip_",
  334. "ring_metacarpal_",
  335. "ring_proxPhalanx_",
  336. "ring_intPhalanx_",
  337. "ring_distPhalanx_",
  338. "ring_tip_",
  339. "little_metacarpal_",
  340. "little_proxPhalanx_",
  341. "little_intPhalanx_",
  342. "little_distPhalanx_",
  343. "little_tip_",
  344. ].map((joint) => `${joint}${handedness === "right" ? "R" : "L"}`);
  345. // single change for left handed systems
  346. const tm = this._scene.getTransformNodeByName(this._rigMapping[0]);
  347. if (!tm) {
  348. throw new Error("could not find the wrist node");
  349. } else {
  350. tm.parent && (tm.parent as AbstractMesh).rotate(Axis.Y, Math.PI);
  351. }
  352. this.onHandMeshReadyObservable.notifyObservers(this);
  353. } catch (e) {
  354. Tools.Error("error loading hand mesh");
  355. console.log(e);
  356. }
  357. }
  358. }
  359. /**
  360. * WebXR Hand Joint tracking feature, available for selected browsers and devices
  361. */
  362. export class WebXRHandTracking extends WebXRAbstractFeature {
  363. private static _idCounter = 0;
  364. /**
  365. * The module's name
  366. */
  367. public static readonly Name = WebXRFeatureName.HAND_TRACKING;
  368. /**
  369. * The (Babylon) version of this module.
  370. * This is an integer representing the implementation version.
  371. * This number does not correspond to the WebXR specs version
  372. */
  373. public static readonly Version = 1;
  374. /**
  375. * This observable will notify registered observers when a new hand object was added and initialized
  376. */
  377. public onHandAddedObservable: Observable<WebXRHand> = new Observable();
  378. /**
  379. * This observable will notify its observers right before the hand object is disposed
  380. */
  381. public onHandRemovedObservable: Observable<WebXRHand> = new Observable();
  382. private _hands: {
  383. [controllerId: string]: {
  384. id: number;
  385. handObject: WebXRHand;
  386. };
  387. } = {};
  388. /**
  389. * Creates a new instance of the hit test feature
  390. * @param _xrSessionManager an instance of WebXRSessionManager
  391. * @param options options to use when constructing this feature
  392. */
  393. constructor(
  394. _xrSessionManager: WebXRSessionManager,
  395. /**
  396. * options to use when constructing this feature
  397. */
  398. public readonly options: IWebXRHandTrackingOptions
  399. ) {
  400. super(_xrSessionManager);
  401. this.xrNativeFeatureName = "hand-tracking";
  402. }
  403. /**
  404. * Check if the needed objects are defined.
  405. * This does not mean that the feature is enabled, but that the objects needed are well defined.
  406. */
  407. public isCompatible(): boolean {
  408. return typeof XRHand !== "undefined";
  409. }
  410. /**
  411. * attach this feature
  412. * Will usually be called by the features manager
  413. *
  414. * @returns true if successful.
  415. */
  416. public attach(): boolean {
  417. if (!super.attach()) {
  418. return false;
  419. }
  420. this.options.xrInput.controllers.forEach(this._attachHand);
  421. this._addNewAttachObserver(this.options.xrInput.onControllerAddedObservable, this._attachHand);
  422. this._addNewAttachObserver(this.options.xrInput.onControllerRemovedObservable, (controller) => {
  423. // REMOVE the controller
  424. this._detachHand(controller.uniqueId);
  425. });
  426. return true;
  427. }
  428. /**
  429. * detach this feature.
  430. * Will usually be called by the features manager
  431. *
  432. * @returns true if successful.
  433. */
  434. public detach(): boolean {
  435. if (!super.detach()) {
  436. return false;
  437. }
  438. Object.keys(this._hands).forEach((controllerId) => {
  439. this._detachHand(controllerId);
  440. });
  441. return true;
  442. }
  443. /**
  444. * Dispose this feature and all of the resources attached
  445. */
  446. public dispose(): void {
  447. super.dispose();
  448. this.onHandAddedObservable.clear();
  449. }
  450. /**
  451. * Get the hand object according to the controller id
  452. * @param controllerId the controller id to which we want to get the hand
  453. * @returns null if not found or the WebXRHand object if found
  454. */
  455. public getHandByControllerId(controllerId: string): Nullable<WebXRHand> {
  456. return this._hands[controllerId]?.handObject || null;
  457. }
  458. /**
  459. * Get a hand object according to the requested handedness
  460. * @param handedness the handedness to request
  461. * @returns null if not found or the WebXRHand object if found
  462. */
  463. public getHandByHandedness(handedness: XRHandedness): Nullable<WebXRHand> {
  464. const handednesses = Object.keys(this._hands).map((key) => this._hands[key].handObject.xrController.inputSource.handedness);
  465. const found = handednesses.indexOf(handedness);
  466. if (found !== -1) {
  467. return this._hands[found].handObject;
  468. }
  469. return null;
  470. }
  471. protected _onXRFrame(_xrFrame: XRFrame): void {
  472. // iterate over the hands object
  473. Object.keys(this._hands).forEach((id) => {
  474. this._hands[id].handObject.updateFromXRFrame(_xrFrame, this._xrSessionManager.referenceSpace, this.options.jointMeshes?.scaleFactor);
  475. });
  476. }
  477. private _attachHand = (xrController: WebXRInputSource) => {
  478. if (!xrController.inputSource.hand || this._hands[xrController.uniqueId]) {
  479. // already attached
  480. return;
  481. }
  482. const hand = xrController.inputSource.hand;
  483. const trackedMeshes: AbstractMesh[] = [];
  484. const originalMesh = this.options.jointMeshes?.sourceMesh || SphereBuilder.CreateSphere("jointParent", { diameter: 1 });
  485. originalMesh.scaling.set(0.01, 0.01, 0.01);
  486. originalMesh.isVisible = !!this.options.jointMeshes?.keepOriginalVisible;
  487. for (let i = 0; i < hand.length; ++i) {
  488. let newInstance: AbstractMesh = originalMesh.createInstance(`${xrController.uniqueId}-handJoint-${i}`);
  489. if (this.options.jointMeshes?.onHandJointMeshGenerated) {
  490. const returnedMesh = this.options.jointMeshes.onHandJointMeshGenerated(newInstance as InstancedMesh, i, xrController.uniqueId);
  491. if (returnedMesh) {
  492. if (returnedMesh !== newInstance) {
  493. newInstance.dispose();
  494. newInstance = returnedMesh;
  495. }
  496. }
  497. }
  498. newInstance.isPickable = false;
  499. if (this.options.jointMeshes?.enablePhysics) {
  500. const props = this.options.jointMeshes.physicsProps || {};
  501. const type = props.impostorType !== undefined ? props.impostorType : PhysicsImpostor.SphereImpostor;
  502. newInstance.physicsImpostor = new PhysicsImpostor(newInstance, type, { mass: 0, ...props });
  503. }
  504. newInstance.rotationQuaternion = new Quaternion();
  505. if (this.options.jointMeshes?.invisible) {
  506. newInstance.isVisible = false;
  507. }
  508. trackedMeshes.push(newInstance);
  509. }
  510. let touchMesh: Nullable<AbstractMesh> = null;
  511. if (this.options.jointMeshes?.sceneForNearInteraction) {
  512. touchMesh = SphereBuilder.CreateSphere(`${xrController.uniqueId}-handJoint-indexCollidable`, {}, this.options.jointMeshes.sceneForNearInteraction);
  513. touchMesh.isVisible = false;
  514. Tags.AddTagsTo(touchMesh, "touchEnabled");
  515. }
  516. const handedness = xrController.inputSource.handedness === "right" ? "right" : "left";
  517. const handMesh = this.options.jointMeshes?.handMeshes && this.options.jointMeshes?.handMeshes[handedness];
  518. const rigMapping = this.options.jointMeshes?.rigMapping && this.options.jointMeshes?.rigMapping[handedness];
  519. const webxrHand = new WebXRHand(xrController, trackedMeshes, handMesh, rigMapping, this.options.jointMeshes?.disableDefaultHandMesh, touchMesh);
  520. // get two new meshes
  521. this._hands[xrController.uniqueId] = {
  522. handObject: webxrHand,
  523. id: WebXRHandTracking._idCounter++,
  524. };
  525. this.onHandAddedObservable.notifyObservers(webxrHand);
  526. };
  527. private _detachHand(controllerId: string) {
  528. if (this._hands[controllerId]) {
  529. this.onHandRemovedObservable.notifyObservers(this._hands[controllerId].handObject);
  530. this._hands[controllerId].handObject.dispose();
  531. }
  532. }
  533. }
  534. //register the plugin
  535. WebXRFeaturesManager.AddWebXRFeature(
  536. WebXRHandTracking.Name,
  537. (xrSessionManager, options) => {
  538. return () => new WebXRHandTracking(xrSessionManager, options);
  539. },
  540. WebXRHandTracking.Version,
  541. false
  542. );