babylon.viveController.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. module BABYLON {
  2. /**
  3. * Vive Controller
  4. */
  5. export class ViveController extends WebVRController {
  6. /**
  7. * Base Url for the controller model.
  8. */
  9. public static MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/vive/';
  10. /**
  11. * File name for the controller model.
  12. */
  13. public static MODEL_FILENAME: string = 'wand.babylon';
  14. /**
  15. * Creates a new ViveController from a gamepad
  16. * @param vrGamepad the gamepad that the controller should be created from
  17. */
  18. constructor(vrGamepad: any) {
  19. super(vrGamepad);
  20. this.controllerType = PoseEnabledControllerType.VIVE;
  21. this._invertLeftStickY = true;
  22. }
  23. /**
  24. * Implements abstract method on WebVRController class, loading controller meshes and calling this.attachToMesh if successful.
  25. * @param scene scene in which to add meshes
  26. * @param meshLoaded optional callback function that will be called if the mesh loads successfully.
  27. */
  28. public initControllerMesh(scene: Scene, meshLoaded?: (mesh: AbstractMesh) => void) {
  29. SceneLoader.ImportMesh("", ViveController.MODEL_BASE_URL, ViveController.MODEL_FILENAME, scene, (newMeshes) => {
  30. /*
  31. Parent Mesh name: ViveWand
  32. - body
  33. - r_gripper
  34. - l_gripper
  35. - menu_button
  36. - system_button
  37. - trackpad
  38. - trigger
  39. - LED
  40. */
  41. this._defaultModel = newMeshes[1];
  42. this.attachToMesh(this._defaultModel);
  43. if (meshLoaded) {
  44. meshLoaded(this._defaultModel);
  45. }
  46. });
  47. }
  48. /**
  49. * Fired when the left button on this controller is modified
  50. */
  51. public get onLeftButtonStateChangedObservable() {
  52. return this.onMainButtonStateChangedObservable;
  53. }
  54. /**
  55. * Fired when the right button on this controller is modified
  56. */
  57. public get onRightButtonStateChangedObservable() {
  58. return this.onMainButtonStateChangedObservable;
  59. }
  60. /**
  61. * Fired when the menu button on this controller is modified
  62. */
  63. public get onMenuButtonStateChangedObservable() {
  64. return this.onSecondaryButtonStateChangedObservable;
  65. }
  66. /**
  67. * Called once for each button that changed state since the last frame
  68. * Vive mapping:
  69. * 0: touchpad
  70. * 1: trigger
  71. * 2: left AND right buttons
  72. * 3: menu button
  73. * @param buttonIdx Which button index changed
  74. * @param state New state of the button
  75. * @param changes Which properties on the state changed since last frame
  76. */
  77. protected _handleButtonChange(buttonIdx: number, state: ExtendedGamepadButton, changes: GamepadButtonChanges) {
  78. let notifyObject = state; //{ state: state, changes: changes };
  79. switch (buttonIdx) {
  80. case 0:
  81. this.onPadStateChangedObservable.notifyObservers(notifyObject);
  82. return;
  83. case 1: // index trigger
  84. if (this._defaultModel) {
  85. (<AbstractMesh>(this._defaultModel.getChildren()[6])).rotation.x = -notifyObject.value * 0.15;
  86. }
  87. this.onTriggerStateChangedObservable.notifyObservers(notifyObject);
  88. return;
  89. case 2: // left AND right button
  90. this.onMainButtonStateChangedObservable.notifyObservers(notifyObject);
  91. return;
  92. case 3:
  93. if (this._defaultModel) {
  94. if (notifyObject.pressed) {
  95. (<AbstractMesh>(this._defaultModel.getChildren()[2])).position.y = -0.001;
  96. }
  97. else {
  98. (<AbstractMesh>(this._defaultModel.getChildren()[2])).position.y = 0;
  99. }
  100. }
  101. this.onSecondaryButtonStateChangedObservable.notifyObservers(notifyObject);
  102. return;
  103. }
  104. }
  105. }
  106. }