webXRHTCViveMotionController.js 7.0 KB

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