daydreamController.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Google Daydream controller
  3. */
  4. export class DaydreamController 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 Daydream Controller.
  15. */
  16. public static readonly GAMEPAD_ID_PREFIX: string = 'Daydream'; // id is 'Daydream Controller'
  17. /**
  18. * Creates a new DaydreamController from a gamepad
  19. * @param vrGamepad the gamepad that the controller should be created from
  20. */
  21. constructor(vrGamepad: any) {
  22. super(vrGamepad);
  23. this.controllerType = PoseEnabledControllerType.DAYDREAM;
  24. }
  25. /**
  26. * Implements abstract method on WebVRController class, loading controller meshes and calling this.attachToMesh if successful.
  27. * @param scene scene in which to add meshes
  28. * @param meshLoaded optional callback function that will be called if the mesh loads successfully.
  29. */
  30. public initControllerMesh(scene: Scene, meshLoaded?: (mesh: AbstractMesh) => void) {
  31. SceneLoader.ImportMesh("", DaydreamController.MODEL_BASE_URL, DaydreamController.MODEL_FILENAME, scene, (newMeshes) => {
  32. this._defaultModel = newMeshes[1];
  33. this.attachToMesh(this._defaultModel);
  34. if (meshLoaded) {
  35. meshLoaded(this._defaultModel);
  36. }
  37. });
  38. }
  39. /**
  40. * Called once for each button that changed state since the last frame
  41. * @param buttonIdx Which button index changed
  42. * @param state New state of the button
  43. * @param changes Which properties on the state changed since last frame
  44. */
  45. protected _handleButtonChange(buttonIdx: number, state: ExtendedGamepadButton, changes: GamepadButtonChanges) {
  46. // Daydream controller only has 1 GamepadButton (on the trackpad).
  47. if (buttonIdx === 0) {
  48. let observable = this.onTriggerStateChangedObservable;
  49. if (observable) {
  50. observable.notifyObservers(state);
  51. }
  52. } else {
  53. // If the app or home buttons are ever made available
  54. Tools.Warn(`Unrecognized Daydream button index: ${buttonIdx}`);
  55. }
  56. }
  57. }