babylon.viveController.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. module BABYLON {
  2. export class ViveController extends WebVRController {
  3. public static MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/vive/';
  4. public static MODEL_FILENAME: string = 'wand.babylon';
  5. constructor(vrGamepad: any) {
  6. super(vrGamepad);
  7. this.controllerType = PoseEnabledControllerType.VIVE;
  8. this._invertLeftStickY = true;
  9. }
  10. public initControllerMesh(scene: Scene, meshLoaded?: (mesh: AbstractMesh) => void) {
  11. SceneLoader.ImportMesh("", ViveController.MODEL_BASE_URL, ViveController.MODEL_FILENAME, scene, (newMeshes) => {
  12. /*
  13. Parent Mesh name: ViveWand
  14. - body
  15. - r_gripper
  16. - l_gripper
  17. - menu_button
  18. - system_button
  19. - trackpad
  20. - trigger
  21. - LED
  22. */
  23. this._defaultModel = newMeshes[1];
  24. this.attachToMesh(this._defaultModel);
  25. if (meshLoaded) {
  26. meshLoaded(this._defaultModel);
  27. }
  28. });
  29. }
  30. public get onLeftButtonStateChangedObservable() {
  31. return this.onMainButtonStateChangedObservable;
  32. }
  33. public get onRightButtonStateChangedObservable() {
  34. return this.onMainButtonStateChangedObservable;
  35. }
  36. public get onMenuButtonStateChangedObservable() {
  37. return this.onSecondaryButtonStateChangedObservable;
  38. }
  39. /**
  40. * Vive mapping:
  41. * 0: touchpad
  42. * 1: trigger
  43. * 2: left AND right buttons
  44. * 3: menu button
  45. */
  46. protected _handleButtonChange(buttonIdx: number, state: ExtendedGamepadButton, changes: GamepadButtonChanges) {
  47. let notifyObject = state; //{ state: state, changes: changes };
  48. switch (buttonIdx) {
  49. case 0:
  50. this.onPadStateChangedObservable.notifyObservers(notifyObject);
  51. return;
  52. case 1: // index trigger
  53. if (this._defaultModel) {
  54. (<AbstractMesh>(this._defaultModel.getChildren()[6])).rotation.x = -notifyObject.value * 0.15;
  55. }
  56. this.onTriggerStateChangedObservable.notifyObservers(notifyObject);
  57. return;
  58. case 2: // left AND right button
  59. this.onMainButtonStateChangedObservable.notifyObservers(notifyObject);
  60. return;
  61. case 3:
  62. if (this._defaultModel) {
  63. if (notifyObject.pressed) {
  64. (<AbstractMesh>(this._defaultModel.getChildren()[2])).position.y = -0.001;
  65. }
  66. else {
  67. (<AbstractMesh>(this._defaultModel.getChildren()[2])).position.y = 0;
  68. }
  69. }
  70. this.onSecondaryButtonStateChangedObservable.notifyObservers(notifyObject);
  71. return;
  72. }
  73. }
  74. }
  75. }