babylon.gearVRController.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. module BABYLON {
  2. /**
  3. * Gear VR Controller
  4. */
  5. export class GearVRController extends WebVRController {
  6. /**
  7. * Base Url for the controller model.
  8. */
  9. public static MODEL_BASE_URL:string = 'https://controllers.babylonjs.com/generic/';
  10. /**
  11. * File name for the controller model.
  12. */
  13. public static MODEL_FILENAME:string = 'generic.babylon';
  14. /**
  15. * Gamepad Id prefix used to identify this controller.
  16. */
  17. public static readonly GAMEPAD_ID_PREFIX: string = 'Gear VR'; // id is 'Gear VR Controller'
  18. private readonly _buttonIndexToObservableNameMap = [
  19. 'onTrackpadChangedObservable', // Trackpad
  20. 'onTriggerStateChangedObservable' // Trigger
  21. ]
  22. /**
  23. * Creates a new GearVRController from a gamepad
  24. * @param vrGamepad the gamepad that the controller should be created from
  25. */
  26. constructor(vrGamepad: any) {
  27. super(vrGamepad);
  28. this.controllerType = PoseEnabledControllerType.GEAR_VR;
  29. // Initial starting position defaults to where hand would be (incase of only 3dof controller)
  30. this._calculatedPosition = new Vector3(this.hand == "left" ? -0.15 : 0.15,-0.5, 0.4)
  31. }
  32. /**
  33. * Implements abstract method on WebVRController class, loading controller meshes and calling this.attachToMesh if successful.
  34. * @param scene scene in which to add meshes
  35. * @param meshLoaded optional callback function that will be called if the mesh loads successfully.
  36. */
  37. public initControllerMesh(scene: Scene, meshLoaded?: (mesh: AbstractMesh) => void) {
  38. SceneLoader.ImportMesh("", GearVRController.MODEL_BASE_URL, GearVRController.MODEL_FILENAME, scene, (newMeshes) => {
  39. this._defaultModel = newMeshes[1];
  40. this.attachToMesh(this._defaultModel);
  41. if (meshLoaded) {
  42. meshLoaded(this._defaultModel);
  43. }
  44. });
  45. }
  46. /**
  47. * Called once for each button that changed state since the last frame
  48. * @param buttonIdx Which button index changed
  49. * @param state New state of the button
  50. * @param changes Which properties on the state changed since last frame
  51. */
  52. protected _handleButtonChange(buttonIdx: number, state: ExtendedGamepadButton, changes: GamepadButtonChanges) {
  53. if (buttonIdx < this._buttonIndexToObservableNameMap.length) {
  54. const observableName : string = this._buttonIndexToObservableNameMap[buttonIdx];
  55. // Only emit events for buttons that we know how to map from index to observable
  56. let observable = (<any>this)[observableName];
  57. if (observable) {
  58. observable.notifyObservers(state);
  59. }
  60. }
  61. }
  62. }
  63. }