webXRAbstractMotionController.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import type { IDisposable, Scene } from "../../scene";
  2. import { WebXRControllerComponent } from "./webXRControllerComponent";
  3. import { Observable } from "../../Misc/observable";
  4. import type { AbstractMesh } from "../../Meshes/abstractMesh";
  5. import type { Nullable } from "../../types";
  6. /**
  7. * Handedness type in xrInput profiles. These can be used to define layouts in the Layout Map.
  8. */
  9. export type MotionControllerHandedness = "none" | "left" | "right";
  10. /**
  11. * The type of components available in motion controllers.
  12. * This is not the name of the component.
  13. */
  14. export type MotionControllerComponentType = "trigger" | "squeeze" | "touchpad" | "thumbstick" | "button";
  15. /**
  16. * The state of a controller component
  17. */
  18. export type MotionControllerComponentStateType = "default" | "touched" | "pressed";
  19. /**
  20. * The schema of motion controller layout.
  21. * No object will be initialized using this interface
  22. * This is used just to define the profile.
  23. */
  24. export interface IMotionControllerLayout {
  25. /**
  26. * Path to load the assets. Usually relative to the base path
  27. */
  28. assetPath: string;
  29. /**
  30. * Available components (unsorted)
  31. */
  32. components: {
  33. /**
  34. * A map of component Ids
  35. */
  36. [componentId: string]: {
  37. /**
  38. * The type of input the component outputs
  39. */
  40. type: MotionControllerComponentType;
  41. /**
  42. * The indices of this component in the gamepad object
  43. */
  44. gamepadIndices: {
  45. /**
  46. * Index of button
  47. */
  48. button?: number;
  49. /**
  50. * If available, index of x-axis
  51. */
  52. xAxis?: number;
  53. /**
  54. * If available, index of y-axis
  55. */
  56. yAxis?: number;
  57. };
  58. /**
  59. * The mesh's root node name
  60. */
  61. rootNodeName: string;
  62. /**
  63. * Animation definitions for this model
  64. */
  65. visualResponses: {
  66. [stateKey: string]: {
  67. /**
  68. * What property will be animated
  69. */
  70. componentProperty: "xAxis" | "yAxis" | "button" | "state";
  71. /**
  72. * What states influence this visual response
  73. */
  74. states: MotionControllerComponentStateType[];
  75. /**
  76. * Type of animation - movement or visibility
  77. */
  78. valueNodeProperty: "transform" | "visibility";
  79. /**
  80. * Base node name to move. Its position will be calculated according to the min and max nodes
  81. */
  82. valueNodeName?: string;
  83. /**
  84. * Minimum movement node
  85. */
  86. minNodeName?: string;
  87. /**
  88. * Max movement node
  89. */
  90. maxNodeName?: string;
  91. };
  92. };
  93. /**
  94. * If touch enabled, what is the name of node to display user feedback
  95. */
  96. touchPointNodeName?: string;
  97. };
  98. };
  99. /**
  100. * Is it xr standard mapping or not
  101. */
  102. gamepadMapping: "" | "xr-standard";
  103. /**
  104. * Base root node of this entire model
  105. */
  106. rootNodeName: string;
  107. /**
  108. * Defines the main button component id
  109. */
  110. selectComponentId: string;
  111. }
  112. /**
  113. * A definition for the layout map in the input profile
  114. */
  115. export interface IMotionControllerLayoutMap {
  116. /**
  117. * Layouts with handedness type as a key
  118. */
  119. [handedness: string]: IMotionControllerLayout;
  120. }
  121. /**
  122. * The XR Input profile schema
  123. * Profiles can be found here:
  124. * https://github.com/immersive-web/webxr-input-profiles/tree/master/packages/registry/profiles
  125. */
  126. export interface IMotionControllerProfile {
  127. /**
  128. * fallback profiles for this profileId
  129. */
  130. fallbackProfileIds: string[];
  131. /**
  132. * The layout map, with handedness as key
  133. */
  134. layouts: IMotionControllerLayoutMap;
  135. /**
  136. * The id of this profile
  137. * correlates to the profile(s) in the xrInput.profiles array
  138. */
  139. profileId: string;
  140. }
  141. /**
  142. * A helper-interface for the 3 meshes needed for controller button animation
  143. * The meshes are provided to the _lerpButtonTransform function to calculate the current position of the value mesh
  144. */
  145. export interface IMotionControllerButtonMeshMap {
  146. /**
  147. * the mesh that defines the pressed value mesh position.
  148. * This is used to find the max-position of this button
  149. */
  150. pressedMesh: AbstractMesh;
  151. /**
  152. * the mesh that defines the unpressed value mesh position.
  153. * This is used to find the min (or initial) position of this button
  154. */
  155. unpressedMesh: AbstractMesh;
  156. /**
  157. * The mesh that will be changed when value changes
  158. */
  159. valueMesh: AbstractMesh;
  160. }
  161. /**
  162. * A helper-interface for the 3 meshes needed for controller axis animation.
  163. * This will be expanded when touchpad animations are fully supported
  164. * The meshes are provided to the _lerpAxisTransform function to calculate the current position of the value mesh
  165. */
  166. export interface IMotionControllerMeshMap {
  167. /**
  168. * the mesh that defines the maximum value mesh position.
  169. */
  170. maxMesh?: AbstractMesh;
  171. /**
  172. * the mesh that defines the minimum value mesh position.
  173. */
  174. minMesh?: AbstractMesh;
  175. /**
  176. * The mesh that will be changed when axis value changes
  177. */
  178. valueMesh?: AbstractMesh;
  179. }
  180. /**
  181. * The elements needed for change-detection of the gamepad objects in motion controllers
  182. */
  183. export interface IMinimalMotionControllerObject {
  184. /**
  185. * Available axes of this controller
  186. */
  187. axes: number[];
  188. /**
  189. * An array of available buttons
  190. */
  191. buttons: Array<{
  192. /**
  193. * Value of the button/trigger
  194. */
  195. value: number;
  196. /**
  197. * If the button/trigger is currently touched
  198. */
  199. touched: boolean;
  200. /**
  201. * If the button/trigger is currently pressed
  202. */
  203. pressed: boolean;
  204. }>;
  205. /**
  206. * EXPERIMENTAL haptic support.
  207. */
  208. hapticActuators?: Array<{
  209. pulse: (value: number, duration: number) => Promise<boolean>;
  210. }>;
  211. }
  212. /**
  213. * An Abstract Motion controller
  214. * This class receives an xrInput and a profile layout and uses those to initialize the components
  215. * Each component has an observable to check for changes in value and state
  216. */
  217. export declare abstract class WebXRAbstractMotionController implements IDisposable {
  218. protected scene: Scene;
  219. protected layout: IMotionControllerLayout;
  220. /**
  221. * The gamepad object correlating to this controller
  222. */
  223. gamepadObject: IMinimalMotionControllerObject;
  224. /**
  225. * handedness (left/right/none) of this controller
  226. */
  227. handedness: MotionControllerHandedness;
  228. /**
  229. * @internal
  230. */
  231. _doNotLoadControllerMesh: boolean;
  232. private _controllerCache?;
  233. private _initComponent;
  234. private _modelReady;
  235. /**
  236. * A map of components (WebXRControllerComponent) in this motion controller
  237. * Components have a ComponentType and can also have both button and axis definitions
  238. */
  239. readonly components: {
  240. [id: string]: WebXRControllerComponent;
  241. };
  242. /**
  243. * Disable the model's animation. Can be set at any time.
  244. */
  245. disableAnimation: boolean;
  246. /**
  247. * Observers registered here will be triggered when the model of this controller is done loading
  248. */
  249. onModelLoadedObservable: Observable<WebXRAbstractMotionController>;
  250. /**
  251. * The profile id of this motion controller
  252. */
  253. abstract profileId: string;
  254. /**
  255. * The root mesh of the model. It is null if the model was not yet initialized
  256. */
  257. rootMesh: Nullable<AbstractMesh>;
  258. /**
  259. * constructs a new abstract motion controller
  260. * @param scene the scene to which the model of the controller will be added
  261. * @param layout The profile layout to load
  262. * @param gamepadObject The gamepad object correlating to this controller
  263. * @param handedness handedness (left/right/none) of this controller
  264. * @param _doNotLoadControllerMesh set this flag to ignore the mesh loading
  265. * @param _controllerCache a cache holding controller models already loaded in this session
  266. */
  267. constructor(scene: Scene, layout: IMotionControllerLayout,
  268. /**
  269. * The gamepad object correlating to this controller
  270. */
  271. gamepadObject: IMinimalMotionControllerObject,
  272. /**
  273. * handedness (left/right/none) of this controller
  274. */
  275. handedness: MotionControllerHandedness,
  276. /**
  277. * @internal
  278. */
  279. _doNotLoadControllerMesh?: boolean, _controllerCache?: {
  280. filename: string;
  281. path: string;
  282. meshes: AbstractMesh[];
  283. }[] | undefined);
  284. /**
  285. * Dispose this controller, the model mesh and all its components
  286. */
  287. dispose(): void;
  288. /**
  289. * Returns all components of specific type
  290. * @param type the type to search for
  291. * @returns an array of components with this type
  292. */
  293. getAllComponentsOfType(type: MotionControllerComponentType): WebXRControllerComponent[];
  294. /**
  295. * get a component based an its component id as defined in layout.components
  296. * @param id the id of the component
  297. * @returns the component correlates to the id or undefined if not found
  298. */
  299. getComponent(id: string): WebXRControllerComponent;
  300. /**
  301. * Get the list of components available in this motion controller
  302. * @returns an array of strings correlating to available components
  303. */
  304. getComponentIds(): string[];
  305. /**
  306. * Get the first component of specific type
  307. * @param type type of component to find
  308. * @returns a controller component or null if not found
  309. */
  310. getComponentOfType(type: MotionControllerComponentType): Nullable<WebXRControllerComponent>;
  311. /**
  312. * Get the main (Select) component of this controller as defined in the layout
  313. * @returns the main component of this controller
  314. */
  315. getMainComponent(): WebXRControllerComponent;
  316. /**
  317. * Loads the model correlating to this controller
  318. * When the mesh is loaded, the onModelLoadedObservable will be triggered
  319. * @returns A promise fulfilled with the result of the model loading
  320. */
  321. loadModel(): Promise<boolean>;
  322. /**
  323. * Update this model using the current XRFrame
  324. * @param xrFrame the current xr frame to use and update the model
  325. */
  326. updateFromXRFrame(xrFrame: XRFrame): void;
  327. /**
  328. * Backwards compatibility due to a deeply-integrated typo
  329. */
  330. get handness(): MotionControllerHandedness;
  331. /**
  332. * Pulse (vibrate) this controller
  333. * If the controller does not support pulses, this function will fail silently and return Promise<false> directly after called
  334. * Consecutive calls to this function will cancel the last pulse call
  335. *
  336. * @param value the strength of the pulse in 0.0...1.0 range
  337. * @param duration Duration of the pulse in milliseconds
  338. * @param hapticActuatorIndex optional index of actuator (will usually be 0)
  339. * @returns a promise that will send true when the pulse has ended and false if the device doesn't support pulse or an error accrued
  340. */
  341. pulse(value: number, duration: number, hapticActuatorIndex?: number): Promise<boolean>;
  342. protected _getChildByName(node: AbstractMesh, name: string): AbstractMesh | undefined;
  343. protected _getImmediateChildByName(node: AbstractMesh, name: string): AbstractMesh | undefined;
  344. /**
  345. * Moves the axis on the controller mesh based on its current state
  346. * @param axisMap
  347. * @param axisValue the value of the axis which determines the meshes new position
  348. * @internal
  349. */
  350. protected _lerpTransform(axisMap: IMotionControllerMeshMap, axisValue: number, fixValueCoordinates?: boolean): void;
  351. /**
  352. * Update the model itself with the current frame data
  353. * @param xrFrame the frame to use for updating the model mesh
  354. */
  355. protected updateModel(xrFrame: XRFrame): void;
  356. /**
  357. * Get the filename and path for this controller's model
  358. * @returns a map of filename and path
  359. */
  360. protected abstract _getFilenameAndPath(): {
  361. filename: string;
  362. path: string;
  363. };
  364. /**
  365. * This function is called before the mesh is loaded. It checks for loading constraints.
  366. * For example, this function can check if the GLB loader is available
  367. * If this function returns false, the generic controller will be loaded instead
  368. * @returns Is the client ready to load the mesh
  369. */
  370. protected abstract _getModelLoadingConstraints(): boolean;
  371. /**
  372. * This function will be called after the model was successfully loaded and can be used
  373. * for mesh transformations before it is available for the user
  374. * @param meshes the loaded meshes
  375. */
  376. protected abstract _processLoadedModel(meshes: AbstractMesh[]): void;
  377. /**
  378. * Set the root mesh for this controller. Important for the WebXR controller class
  379. * @param meshes the loaded meshes
  380. */
  381. protected abstract _setRootMesh(meshes: AbstractMesh[]): void;
  382. /**
  383. * A function executed each frame that updates the mesh (if needed)
  384. * @param xrFrame the current xrFrame
  385. */
  386. protected abstract _updateModel(xrFrame: XRFrame): void;
  387. private _getGenericFilenameAndPath;
  388. private _getGenericParentMesh;
  389. }