gearVRController.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Gear VR Controller
  3. */
  4. export class GearVRController extends WebVRController {
  5. /**
  6. * Base Url for the controller model.
  7. */
  8. public static MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/generic/';
  9. /**
  10. * File name for the controller model.
  11. */
  12. public static MODEL_FILENAME: string = 'generic.babylon';
  13. /**
  14. * Gamepad Id prefix used to identify this controller.
  15. */
  16. public static readonly GAMEPAD_ID_PREFIX: string = 'Gear VR'; // id is 'Gear VR Controller'
  17. private readonly _buttonIndexToObservableNameMap = [
  18. 'onTrackpadChangedObservable', // Trackpad
  19. 'onTriggerStateChangedObservable' // Trigger
  20. ];
  21. /**
  22. * Creates a new GearVRController from a gamepad
  23. * @param vrGamepad the gamepad that the controller should be created from
  24. */
  25. constructor(vrGamepad: any) {
  26. super(vrGamepad);
  27. this.controllerType = PoseEnabledControllerType.GEAR_VR;
  28. // Initial starting position defaults to where hand would be (incase of only 3dof controller)
  29. this._calculatedPosition = new Vector3(this.hand == "left" ? -0.15 : 0.15, -0.5, 0.25);
  30. this._disableTrackPosition(this._calculatedPosition);
  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. // Offset the controller so it will rotate around the users wrist
  40. var mesh = new BABYLON.Mesh("", scene);
  41. newMeshes[1].parent = mesh;
  42. newMeshes[1].position.z = -0.15;
  43. this._defaultModel = mesh;
  44. this.attachToMesh(this._defaultModel);
  45. if (meshLoaded) {
  46. meshLoaded(this._defaultModel);
  47. }
  48. });
  49. }
  50. /**
  51. * Called once for each button that changed state since the last frame
  52. * @param buttonIdx Which button index changed
  53. * @param state New state of the button
  54. * @param changes Which properties on the state changed since last frame
  55. */
  56. protected _handleButtonChange(buttonIdx: number, state: ExtendedGamepadButton, changes: GamepadButtonChanges) {
  57. if (buttonIdx < this._buttonIndexToObservableNameMap.length) {
  58. const observableName : string = this._buttonIndexToObservableNameMap[buttonIdx];
  59. // Only emit events for buttons that we know how to map from index to observable
  60. let observable = (<any>this)[observableName];
  61. if (observable) {
  62. observable.notifyObservers(state);
  63. }
  64. }
  65. }
  66. }