webXRHTCViveMotionController.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import {
  2. IMotionControllerLayoutMap,
  3. IMinimalMotionControllerObject,
  4. MotionControllerHandness,
  5. WebXRAbstractMotionController
  6. } from "./webXRAbstractController";
  7. import { Scene } from '../../../scene';
  8. import { AbstractMesh } from '../../../Meshes/abstractMesh';
  9. import { Mesh } from '../../../Meshes/mesh';
  10. import { Quaternion } from '../../../Maths/math.vector';
  11. import { WebXRMotionControllerManager } from './webXRMotionControllerManager';
  12. const HTCViveLayout: IMotionControllerLayoutMap = {
  13. "left": {
  14. "selectComponentId": "xr-standard-trigger",
  15. "components": {
  16. "xr-standard-trigger": {
  17. "type": "trigger",
  18. "gamepadIndices": {
  19. "button": 0
  20. },
  21. "rootNodeName": "xr_standard_trigger",
  22. "visualResponses": {
  23. }
  24. },
  25. "xr-standard-squeeze": {
  26. "type": "squeeze",
  27. "gamepadIndices": {
  28. "button": 1
  29. },
  30. "rootNodeName": "xr_standard_squeeze",
  31. "visualResponses": {
  32. }
  33. },
  34. "xr-standard-touchpad": {
  35. "type": "touchpad",
  36. "gamepadIndices": {
  37. "button": 2,
  38. "xAxis": 0,
  39. "yAxis": 1
  40. },
  41. "rootNodeName": "xr_standard_touchpad",
  42. "visualResponses": {
  43. },
  44. },
  45. "menu": {
  46. "type": "button",
  47. "gamepadIndices": {
  48. "button": 4
  49. },
  50. "rootNodeName": "menu",
  51. "visualResponses": {
  52. }
  53. }
  54. },
  55. "gamepadMapping": "xr-standard",
  56. "rootNodeName": "htc_vive_none",
  57. "assetPath": "none.glb"
  58. },
  59. "right": {
  60. "selectComponentId": "xr-standard-trigger",
  61. "components": {
  62. "xr-standard-trigger": {
  63. "type": "trigger",
  64. "gamepadIndices": {
  65. "button": 0
  66. },
  67. "rootNodeName": "xr_standard_trigger",
  68. "visualResponses": {
  69. }
  70. },
  71. "xr-standard-squeeze": {
  72. "type": "squeeze",
  73. "gamepadIndices": {
  74. "button": 1
  75. },
  76. "rootNodeName": "xr_standard_squeeze",
  77. "visualResponses": {
  78. }
  79. },
  80. "xr-standard-touchpad": {
  81. "type": "touchpad",
  82. "gamepadIndices": {
  83. "button": 2,
  84. "xAxis": 0,
  85. "yAxis": 1
  86. },
  87. "rootNodeName": "xr_standard_touchpad",
  88. "visualResponses": {
  89. },
  90. },
  91. "menu": {
  92. "type": "button",
  93. "gamepadIndices": {
  94. "button": 4
  95. },
  96. "rootNodeName": "menu",
  97. "visualResponses": {
  98. }
  99. }
  100. },
  101. "gamepadMapping": "xr-standard",
  102. "rootNodeName": "htc_vive_none",
  103. "assetPath": "none.glb"
  104. },
  105. "none": {
  106. "selectComponentId": "xr-standard-trigger",
  107. "components": {
  108. "xr-standard-trigger": {
  109. "type": "trigger",
  110. "gamepadIndices": {
  111. "button": 0
  112. },
  113. "rootNodeName": "xr_standard_trigger",
  114. "visualResponses": {
  115. }
  116. },
  117. "xr-standard-squeeze": {
  118. "type": "squeeze",
  119. "gamepadIndices": {
  120. "button": 1
  121. },
  122. "rootNodeName": "xr_standard_squeeze",
  123. "visualResponses": {
  124. }
  125. },
  126. "xr-standard-touchpad": {
  127. "type": "touchpad",
  128. "gamepadIndices": {
  129. "button": 2,
  130. "xAxis": 0,
  131. "yAxis": 1
  132. },
  133. "rootNodeName": "xr_standard_touchpad",
  134. "visualResponses": {
  135. },
  136. },
  137. "menu": {
  138. "type": "button",
  139. "gamepadIndices": {
  140. "button": 4
  141. },
  142. "rootNodeName": "menu",
  143. "visualResponses": {
  144. }
  145. }
  146. },
  147. "gamepadMapping": "xr-standard",
  148. "rootNodeName": "htc-vive-none",
  149. "assetPath": "none.glb"
  150. }
  151. };
  152. // const HTCViveLegacyLayout: IMotionControllerLayoutMap = {
  153. // "left-right-none": {
  154. // "selectComponentId": "xr-standard-trigger",
  155. // "components": {
  156. // "xr-standard-trigger": { "type": "trigger" },
  157. // "xr-standard-squeeze": { "type": "squeeze" },
  158. // "xr-standard-touchpad": { "type": "touchpad" }
  159. // },
  160. // "gamepad": {
  161. // "mapping": "",
  162. // "buttons": [
  163. // "xr-standard-touchpad",
  164. // "xr-standard-trigger",
  165. // "xr-standard-squeeze"
  166. // ],
  167. // "axes": [
  168. // { "componentId": "xr-standard-touchpad", "axis": "x-axis" },
  169. // { "componentId": "xr-standard-touchpad", "axis": "y-axis" }
  170. // ]
  171. // }
  172. // }
  173. // };
  174. /**
  175. * The motion controller class for the standard HTC-Vive controllers
  176. */
  177. export class WebXRHTCViveMotionController extends WebXRAbstractMotionController {
  178. /**
  179. * The base url used to load the left and right controller models
  180. */
  181. public static MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/vive/';
  182. /**
  183. * File name for the controller model.
  184. */
  185. public static MODEL_FILENAME: string = 'wand.babylon';
  186. public profileId = "htc-vive";
  187. private _modelRootNode: AbstractMesh;
  188. constructor(scene: Scene,
  189. gamepadObject: IMinimalMotionControllerObject,
  190. handness: MotionControllerHandness/*,
  191. private _legacyMapping: boolean = false*/) {
  192. super(scene, HTCViveLayout[handness], gamepadObject, handness);
  193. }
  194. protected _processLoadedModel(_meshes: AbstractMesh[]): void {
  195. // this.layout.gamepad!.buttons.forEach((buttonName) => {
  196. // const comp = buttonName && this.getComponent(buttonName);
  197. // if (comp) {
  198. // comp.onButtonStateChanged.add((component) => {
  199. // if (!this.rootMesh) { return; }
  200. // switch (buttonName) {
  201. // case "xr-standard-trigger":
  202. // (<AbstractMesh>(this._modelRootNode.getChildren()[6])).rotation.x = -component.value * 0.15;
  203. // return;
  204. // case "xr-standard-touchpad":
  205. // return;
  206. // case "xr-standard-squeeze":
  207. // return;
  208. // }
  209. // }, undefined, true);
  210. // }
  211. // });
  212. }
  213. protected _getFilenameAndPath(): { filename: string; path: string; } {
  214. let filename = WebXRHTCViveMotionController.MODEL_FILENAME;
  215. let path = WebXRHTCViveMotionController.MODEL_BASE_URL;
  216. return {
  217. filename,
  218. path
  219. };
  220. }
  221. protected _updateModel(): void {
  222. // no-op. model is updated using observables.
  223. }
  224. protected _getModelLoadingConstraints(): boolean {
  225. return true;
  226. }
  227. protected _setRootMesh(meshes: AbstractMesh[]): void {
  228. this.rootMesh = new Mesh(this.profileId + " " + this.handness, this.scene);
  229. meshes.forEach((mesh) => { mesh.isPickable = false; });
  230. this._modelRootNode = meshes[1];
  231. this._modelRootNode.parent = this.rootMesh;
  232. this.rootMesh.rotationQuaternion = Quaternion.FromEulerAngles(0, Math.PI, 0);
  233. }
  234. }
  235. // register the profile
  236. WebXRMotionControllerManager.RegisterController("htc-vive", (xrInput: XRInputSource, scene: Scene) => {
  237. return new WebXRHTCViveMotionController(scene, <any>(xrInput.gamepad), xrInput.handedness);
  238. });
  239. // WebXRMotionControllerManager.RegisterController("htc-vive-legacy", (xrInput: XRInputSource, scene: Scene) => {
  240. // return new WebXRHTCViveMotionController(scene, <any>(xrInput.gamepad), xrInput.handedness, true);
  241. // });