webXRGenericMotionController.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {
  2. WebXRAbstractMotionController,
  3. IMinimalMotionControllerObject,
  4. MotionControllerHandness,
  5. IMotionControllerLayoutMap
  6. } from "./webXRAbstractMotionController";
  7. import { AbstractMesh } from '../../Meshes/abstractMesh';
  8. import { Scene } from '../../scene';
  9. import { Mesh } from '../../Meshes/mesh';
  10. import { Quaternion } from '../../Maths/math.vector';
  11. /**
  12. * A generic trigger-only motion controller for WebXR
  13. */
  14. export class WebXRGenericTriggerMotionController extends WebXRAbstractMotionController {
  15. /**
  16. * Static version of the profile id of this controller
  17. */
  18. public static ProfileId = "generic-trigger";
  19. public profileId = WebXRGenericTriggerMotionController.ProfileId;
  20. constructor(scene: Scene, gamepadObject: IMinimalMotionControllerObject, handness: MotionControllerHandness) {
  21. super(scene, GenericTriggerLayout[handness], gamepadObject, handness);
  22. }
  23. protected _processLoadedModel(meshes: AbstractMesh[]): void {
  24. // nothing to do
  25. }
  26. protected _updateModel(): void {
  27. // no-op
  28. }
  29. protected _getFilenameAndPath(): { filename: string; path: string; } {
  30. return {
  31. filename: "generic.babylon",
  32. path: "https://controllers.babylonjs.com/generic/"
  33. };
  34. }
  35. protected _setRootMesh(meshes: AbstractMesh[]): void {
  36. this.rootMesh = new Mesh(this.profileId + " " + this.handness, this.scene);
  37. meshes.forEach((mesh) => {
  38. mesh.isPickable = false;
  39. if (!mesh.parent) {
  40. mesh.setParent(this.rootMesh);
  41. }
  42. });
  43. this.rootMesh.rotationQuaternion = Quaternion.FromEulerAngles(0, Math.PI, 0);
  44. }
  45. protected _getModelLoadingConstraints(): boolean {
  46. return true;
  47. }
  48. }
  49. // https://github.com/immersive-web/webxr-input-profiles/blob/master/packages/registry/profiles/generic/generic-trigger-touchpad-thumbstick.json
  50. const GenericTriggerLayout: IMotionControllerLayoutMap = {
  51. "left": {
  52. "selectComponentId": "xr-standard-trigger",
  53. "components": {
  54. "xr-standard-trigger": {
  55. "type": "trigger",
  56. "gamepadIndices": {
  57. "button": 0
  58. },
  59. "rootNodeName": "xr_standard_trigger",
  60. "visualResponses": {}
  61. }
  62. },
  63. "gamepadMapping": "xr-standard",
  64. "rootNodeName": "generic-trigger-left",
  65. "assetPath": "left.glb"
  66. },
  67. "right": {
  68. "selectComponentId": "xr-standard-trigger",
  69. "components": {
  70. "xr-standard-trigger": {
  71. "type": "trigger",
  72. "gamepadIndices": {
  73. "button": 0
  74. },
  75. "rootNodeName": "xr_standard_trigger",
  76. "visualResponses": {}
  77. }
  78. },
  79. "gamepadMapping": "xr-standard",
  80. "rootNodeName": "generic-trigger-right",
  81. "assetPath": "right.glb"
  82. },
  83. "none": {
  84. "selectComponentId": "xr-standard-trigger",
  85. "components": {
  86. "xr-standard-trigger": {
  87. "type": "trigger",
  88. "gamepadIndices": {
  89. "button": 0
  90. },
  91. "rootNodeName": "xr_standard_trigger",
  92. "visualResponses": {}
  93. }
  94. },
  95. "gamepadMapping": "xr-standard",
  96. "rootNodeName": "generic-trigger-none",
  97. "assetPath": "none.glb"
  98. }
  99. };