babylon.viveController.ts 3.2 KB

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