WebXRHandTracking.d.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import { WebXRAbstractFeature } from "./WebXRAbstractFeature";
  2. import type { WebXRSessionManager } from "../webXRSessionManager";
  3. import type { AbstractMesh } from "../../Meshes/abstractMesh";
  4. import type { Mesh } from "../../Meshes/mesh";
  5. import type { WebXRInput } from "../webXRInput";
  6. import type { WebXRInputSource } from "../webXRInputSource";
  7. import type { Nullable } from "../../types";
  8. import type { IDisposable } from "../../scene";
  9. import { Observable } from "../../Misc/observable";
  10. import type { InstancedMesh } from "../../Meshes/instancedMesh";
  11. import { Color3 } from "../../Maths/math.color";
  12. /**
  13. * Configuration interface for the hand tracking feature
  14. */
  15. export interface IWebXRHandTrackingOptions {
  16. /**
  17. * The xrInput that will be used as source for new hands
  18. */
  19. xrInput: WebXRInput;
  20. /**
  21. * Configuration object for the joint meshes.
  22. */
  23. jointMeshes?: {
  24. /**
  25. * Should the meshes created be invisible (defaults to false).
  26. */
  27. invisible?: boolean;
  28. /**
  29. * A source mesh to be used to create instances. Defaults to an icosphere with two subdivisions and smooth lighting.
  30. * This mesh will be the source for all other (25) meshes.
  31. * It should have the general size of a single unit, as the instances will be scaled according to the provided radius.
  32. */
  33. sourceMesh?: Mesh;
  34. /**
  35. * This function will be called after a mesh was created for a specific joint.
  36. * Using this function you can either manipulate the instance or return a new mesh.
  37. * When returning a new mesh the instance created before will be disposed.
  38. * @param meshInstance An instance of the original joint mesh being used for the joint.
  39. * @param jointId The joint's index, see https://immersive-web.github.io/webxr-hand-input/#skeleton-joints-section for more info.
  40. * @param hand Which hand ("left", "right") the joint will be on.
  41. */
  42. onHandJointMeshGenerated?: (meshInstance: InstancedMesh, jointId: number, hand: XRHandedness) => AbstractMesh | undefined;
  43. /**
  44. * Should the source mesh stay visible (defaults to false).
  45. */
  46. keepOriginalVisible?: boolean;
  47. /**
  48. * Should each instance have its own physics impostor
  49. */
  50. enablePhysics?: boolean;
  51. /**
  52. * If enabled, override default physics properties
  53. */
  54. physicsProps?: {
  55. friction?: number;
  56. restitution?: number;
  57. impostorType?: number;
  58. };
  59. /**
  60. * Scale factor for all joint meshes (defaults to 1)
  61. */
  62. scaleFactor?: number;
  63. };
  64. /**
  65. * Configuration object for the hand meshes.
  66. */
  67. handMeshes?: {
  68. /**
  69. * Should the default hand mesh be disabled. In this case, the spheres will be visible (unless set invisible).
  70. */
  71. disableDefaultMeshes?: boolean;
  72. /**
  73. * Rigged hand meshes that will be tracked to the user's hands. This will override the default hand mesh.
  74. */
  75. customMeshes?: {
  76. right: AbstractMesh;
  77. left: AbstractMesh;
  78. };
  79. /**
  80. * Are the meshes prepared for a left-handed system. Default hand meshes are right-handed.
  81. */
  82. meshesUseLeftHandedCoordinates?: boolean;
  83. /**
  84. * If a hand mesh was provided, this array will define what axis will update which node. This will override the default hand mesh
  85. */
  86. customRigMappings?: {
  87. right: XRHandMeshRigMapping;
  88. left: XRHandMeshRigMapping;
  89. };
  90. /**
  91. * Override the colors of the hand meshes.
  92. */
  93. customColors?: {
  94. base?: Color3;
  95. fresnel?: Color3;
  96. fingerColor?: Color3;
  97. tipFresnel?: Color3;
  98. };
  99. /**
  100. * Define whether or not the hand meshes should be disposed on just invisible when the session ends.
  101. * Not setting, or setting to false, will maintain the hand meshes in the scene after the session ends, which will allow q quicker re-entry into XR.
  102. */
  103. disposeOnSessionEnd?: boolean;
  104. };
  105. }
  106. /**
  107. * Parts of the hands divided to writs and finger names
  108. */
  109. export declare enum HandPart {
  110. /**
  111. * HandPart - Wrist
  112. */
  113. WRIST = "wrist",
  114. /**
  115. * HandPart - The thumb
  116. */
  117. THUMB = "thumb",
  118. /**
  119. * HandPart - Index finger
  120. */
  121. INDEX = "index",
  122. /**
  123. * HandPart - Middle finger
  124. */
  125. MIDDLE = "middle",
  126. /**
  127. * HandPart - Ring finger
  128. */
  129. RING = "ring",
  130. /**
  131. * HandPart - Little finger
  132. */
  133. LITTLE = "little"
  134. }
  135. /**
  136. * Joints of the hand as defined by the WebXR specification.
  137. * https://immersive-web.github.io/webxr-hand-input/#skeleton-joints-section
  138. */
  139. export declare enum WebXRHandJoint {
  140. /** Wrist */
  141. WRIST = "wrist",
  142. /** Thumb near wrist */
  143. THUMB_METACARPAL = "thumb-metacarpal",
  144. /** Thumb first knuckle */
  145. THUMB_PHALANX_PROXIMAL = "thumb-phalanx-proximal",
  146. /** Thumb second knuckle */
  147. THUMB_PHALANX_DISTAL = "thumb-phalanx-distal",
  148. /** Thumb tip */
  149. THUMB_TIP = "thumb-tip",
  150. /** Index finger near wrist */
  151. INDEX_FINGER_METACARPAL = "index-finger-metacarpal",
  152. /** Index finger first knuckle */
  153. INDEX_FINGER_PHALANX_PROXIMAL = "index-finger-phalanx-proximal",
  154. /** Index finger second knuckle */
  155. INDEX_FINGER_PHALANX_INTERMEDIATE = "index-finger-phalanx-intermediate",
  156. /** Index finger third knuckle */
  157. INDEX_FINGER_PHALANX_DISTAL = "index-finger-phalanx-distal",
  158. /** Index finger tip */
  159. INDEX_FINGER_TIP = "index-finger-tip",
  160. /** Middle finger near wrist */
  161. MIDDLE_FINGER_METACARPAL = "middle-finger-metacarpal",
  162. /** Middle finger first knuckle */
  163. MIDDLE_FINGER_PHALANX_PROXIMAL = "middle-finger-phalanx-proximal",
  164. /** Middle finger second knuckle */
  165. MIDDLE_FINGER_PHALANX_INTERMEDIATE = "middle-finger-phalanx-intermediate",
  166. /** Middle finger third knuckle */
  167. MIDDLE_FINGER_PHALANX_DISTAL = "middle-finger-phalanx-distal",
  168. /** Middle finger tip */
  169. MIDDLE_FINGER_TIP = "middle-finger-tip",
  170. /** Ring finger near wrist */
  171. RING_FINGER_METACARPAL = "ring-finger-metacarpal",
  172. /** Ring finger first knuckle */
  173. RING_FINGER_PHALANX_PROXIMAL = "ring-finger-phalanx-proximal",
  174. /** Ring finger second knuckle */
  175. RING_FINGER_PHALANX_INTERMEDIATE = "ring-finger-phalanx-intermediate",
  176. /** Ring finger third knuckle */
  177. RING_FINGER_PHALANX_DISTAL = "ring-finger-phalanx-distal",
  178. /** Ring finger tip */
  179. RING_FINGER_TIP = "ring-finger-tip",
  180. /** Pinky finger near wrist */
  181. PINKY_FINGER_METACARPAL = "pinky-finger-metacarpal",
  182. /** Pinky finger first knuckle */
  183. PINKY_FINGER_PHALANX_PROXIMAL = "pinky-finger-phalanx-proximal",
  184. /** Pinky finger second knuckle */
  185. PINKY_FINGER_PHALANX_INTERMEDIATE = "pinky-finger-phalanx-intermediate",
  186. /** Pinky finger third knuckle */
  187. PINKY_FINGER_PHALANX_DISTAL = "pinky-finger-phalanx-distal",
  188. /** Pinky finger tip */
  189. PINKY_FINGER_TIP = "pinky-finger-tip"
  190. }
  191. /** A type encapsulating a dictionary mapping WebXR joints to bone names in a rigged hand mesh. */
  192. export type XRHandMeshRigMapping = {
  193. [webXRJointName in WebXRHandJoint]: string;
  194. };
  195. /**
  196. * Representing a single hand (with its corresponding native XRHand object)
  197. */
  198. export declare class WebXRHand implements IDisposable {
  199. /** The controller to which the hand correlates. */
  200. readonly xrController: WebXRInputSource;
  201. private readonly _jointMeshes;
  202. private _handMesh;
  203. /** An optional rig mapping for the hand mesh. If not provided (but a hand mesh is provided),
  204. * it will be assumed that the hand mesh's bones are named directly after the WebXR bone names. */
  205. readonly rigMapping: Nullable<XRHandMeshRigMapping>;
  206. private readonly _leftHandedMeshes;
  207. private readonly _jointsInvisible;
  208. private readonly _jointScaleFactor;
  209. /**
  210. * This observable will notify registered observers when the hand object has been set with a new mesh.
  211. * you can get the hand mesh using `webxrHand.handMesh`
  212. */
  213. onHandMeshSetObservable: Observable<WebXRHand>;
  214. private _scene;
  215. /**
  216. * Transform nodes that will directly receive the transforms from the WebXR matrix data.
  217. */
  218. private _jointTransforms;
  219. /**
  220. * The float array that will directly receive the transform matrix data from WebXR.
  221. */
  222. private _jointTransformMatrices;
  223. private _tempJointMatrix;
  224. /**
  225. * The float array that will directly receive the joint radii from WebXR.
  226. */
  227. private _jointRadii;
  228. /**
  229. * Get the hand mesh.
  230. */
  231. get handMesh(): Nullable<AbstractMesh>;
  232. /**
  233. * Get meshes of part of the hand.
  234. * @param part The part of hand to get.
  235. * @returns An array of meshes that correlate to the hand part requested.
  236. */
  237. getHandPartMeshes(part: HandPart): AbstractMesh[];
  238. /**
  239. * Retrieves a mesh linked to a named joint in the hand.
  240. * @param jointName The name of the joint.
  241. * @returns An AbstractMesh whose position corresponds with the joint position.
  242. */
  243. getJointMesh(jointName: WebXRHandJoint): AbstractMesh;
  244. /**
  245. * Construct a new hand object
  246. * @param xrController The controller to which the hand correlates.
  247. * @param _jointMeshes The meshes to be used to track the hand joints.
  248. * @param _handMesh An optional hand mesh.
  249. * @param rigMapping An optional rig mapping for the hand mesh.
  250. * If not provided (but a hand mesh is provided),
  251. * it will be assumed that the hand mesh's bones are named
  252. * directly after the WebXR bone names.
  253. * @param _leftHandedMeshes Are the hand meshes left-handed-system meshes
  254. * @param _jointsInvisible Are the tracked joint meshes visible
  255. * @param _jointScaleFactor Scale factor for all joint meshes
  256. */
  257. constructor(
  258. /** The controller to which the hand correlates. */
  259. xrController: WebXRInputSource, _jointMeshes: AbstractMesh[], _handMesh: Nullable<AbstractMesh>,
  260. /** An optional rig mapping for the hand mesh. If not provided (but a hand mesh is provided),
  261. * it will be assumed that the hand mesh's bones are named directly after the WebXR bone names. */
  262. rigMapping: Nullable<XRHandMeshRigMapping>, _leftHandedMeshes?: boolean, _jointsInvisible?: boolean, _jointScaleFactor?: number);
  263. /**
  264. * Sets the current hand mesh to render for the WebXRHand.
  265. * @param handMesh The rigged hand mesh that will be tracked to the user's hand.
  266. * @param rigMapping The mapping from XRHandJoint to bone names to use with the mesh.
  267. * @param _xrSessionManager The XRSessionManager used to initialize the hand mesh.
  268. */
  269. setHandMesh(handMesh: AbstractMesh, rigMapping: Nullable<XRHandMeshRigMapping>, _xrSessionManager?: WebXRSessionManager): void;
  270. /**
  271. * Update this hand from the latest xr frame.
  272. * @param xrFrame The latest frame received from WebXR.
  273. * @param referenceSpace The current viewer reference space.
  274. */
  275. updateFromXRFrame(xrFrame: XRFrame, referenceSpace: XRReferenceSpace): void;
  276. /**
  277. * Dispose this Hand object
  278. * @param disposeMeshes Should the meshes be disposed as well
  279. */
  280. dispose(disposeMeshes?: boolean): void;
  281. }
  282. /**
  283. * WebXR Hand Joint tracking feature, available for selected browsers and devices
  284. */
  285. export declare class WebXRHandTracking extends WebXRAbstractFeature {
  286. /** Options to use when constructing this feature. */
  287. readonly options: IWebXRHandTrackingOptions;
  288. /**
  289. * The module's name
  290. */
  291. static readonly Name = "xr-hand-tracking";
  292. /**
  293. * The (Babylon) version of this module.
  294. * This is an integer representing the implementation version.
  295. * This number does not correspond to the WebXR specs version
  296. */
  297. static readonly Version = 1;
  298. /** The base URL for the default hand model. */
  299. static DEFAULT_HAND_MODEL_BASE_URL: string;
  300. /** The filename to use for the default right hand model. */
  301. static DEFAULT_HAND_MODEL_RIGHT_FILENAME: string;
  302. /** The filename to use for the default left hand model. */
  303. static DEFAULT_HAND_MODEL_LEFT_FILENAME: string;
  304. /** The URL pointing to the default hand model NodeMaterial shader. */
  305. static DEFAULT_HAND_MODEL_SHADER_URL: string;
  306. private static readonly _ICOSPHERE_PARAMS;
  307. private static _RightHandGLB;
  308. private static _LeftHandGLB;
  309. private static _GenerateTrackedJointMeshes;
  310. private static _GenerateDefaultHandMeshesAsync;
  311. /**
  312. * Generates a mapping from XRHandJoint to bone name for the default hand mesh.
  313. * @param handedness The handedness being mapped for.
  314. * @returns A mapping from XRHandJoint to bone name.
  315. */
  316. private static _GenerateDefaultHandMeshRigMapping;
  317. private _attachedHands;
  318. private _trackingHands;
  319. private _handResources;
  320. private _worldScaleObserver?;
  321. /**
  322. * This observable will notify registered observers when a new hand object was added and initialized
  323. */
  324. onHandAddedObservable: Observable<WebXRHand>;
  325. /**
  326. * This observable will notify its observers right before the hand object is disposed
  327. */
  328. onHandRemovedObservable: Observable<WebXRHand>;
  329. /**
  330. * Check if the needed objects are defined.
  331. * This does not mean that the feature is enabled, but that the objects needed are well defined.
  332. * @returns true if the needed objects for this feature are defined
  333. */
  334. isCompatible(): boolean;
  335. /**
  336. * Get the hand object according to the controller id
  337. * @param controllerId the controller id to which we want to get the hand
  338. * @returns null if not found or the WebXRHand object if found
  339. */
  340. getHandByControllerId(controllerId: string): Nullable<WebXRHand>;
  341. /**
  342. * Get a hand object according to the requested handedness
  343. * @param handedness the handedness to request
  344. * @returns null if not found or the WebXRHand object if found
  345. */
  346. getHandByHandedness(handedness: XRHandedness): Nullable<WebXRHand>;
  347. /**
  348. * Creates a new instance of the XR hand tracking feature.
  349. * @param _xrSessionManager An instance of WebXRSessionManager.
  350. * @param options Options to use when constructing this feature.
  351. */
  352. constructor(_xrSessionManager: WebXRSessionManager,
  353. /** Options to use when constructing this feature. */
  354. options: IWebXRHandTrackingOptions);
  355. /**
  356. * Attach this feature.
  357. * Will usually be called by the features manager.
  358. *
  359. * @returns true if successful.
  360. */
  361. attach(): boolean;
  362. protected _onXRFrame(_xrFrame: XRFrame): void;
  363. private _attachHand;
  364. private _detachHandById;
  365. private _detachHand;
  366. /**
  367. * Detach this feature.
  368. * Will usually be called by the features manager.
  369. *
  370. * @returns true if successful.
  371. */
  372. detach(): boolean;
  373. /**
  374. * Dispose this feature and all of the resources attached.
  375. */
  376. dispose(): void;
  377. }