freeCameraGamepadInput.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { serialize } from "../../Misc/decorators";
  2. import { Observer } from "../../Misc/observable";
  3. import { Nullable } from "../../types";
  4. import { ICameraInput, CameraInputTypes } from "../../Cameras/cameraInputsManager";
  5. import { FreeCamera } from "../../Cameras/freeCamera";
  6. import { Matrix, Vector3, Vector2 } from "../../Maths/math";
  7. import { Gamepad } from "../../Gamepads/gamepad";
  8. /**
  9. * Manage the gamepad inputs to control a free camera.
  10. * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
  11. */
  12. export class FreeCameraGamepadInput implements ICameraInput<FreeCamera> {
  13. /**
  14. * Define the camera the input is attached to.
  15. */
  16. public camera: FreeCamera;
  17. /**
  18. * Define the Gamepad controlling the input
  19. */
  20. public gamepad: Nullable<Gamepad>;
  21. /**
  22. * Defines the gamepad rotation sensiblity.
  23. * This is the threshold from when rotation starts to be accounted for to prevent jittering.
  24. */
  25. @serialize()
  26. public gamepadAngularSensibility = 200;
  27. /**
  28. * Defines the gamepad move sensiblity.
  29. * This is the threshold from when moving starts to be accounted for for to prevent jittering.
  30. */
  31. @serialize()
  32. public gamepadMoveSensibility = 40;
  33. // private members
  34. private _onGamepadConnectedObserver: Nullable<Observer<Gamepad>>;
  35. private _onGamepadDisconnectedObserver: Nullable<Observer<Gamepad>>;
  36. private _cameraTransform: Matrix = Matrix.Identity();
  37. private _deltaTransform: Vector3 = Vector3.Zero();
  38. private _vector3: Vector3 = Vector3.Zero();
  39. private _vector2: Vector2 = Vector2.Zero();
  40. /**
  41. * Attach the input controls to a specific dom element to get the input from.
  42. * @param element Defines the element the controls should be listened from
  43. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  44. */
  45. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  46. let manager = this.camera.getScene().gamepadManager;
  47. this._onGamepadConnectedObserver = manager.onGamepadConnectedObservable.add((gamepad) => {
  48. if (gamepad.type !== Gamepad.POSE_ENABLED) {
  49. // prioritize XBOX gamepads.
  50. if (!this.gamepad || gamepad.type === Gamepad.XBOX) {
  51. this.gamepad = gamepad;
  52. }
  53. }
  54. });
  55. this._onGamepadDisconnectedObserver = manager.onGamepadDisconnectedObservable.add((gamepad) => {
  56. if (this.gamepad === gamepad) {
  57. this.gamepad = null;
  58. }
  59. });
  60. this.gamepad = manager.getGamepadByType(Gamepad.XBOX);
  61. }
  62. /**
  63. * Detach the current controls from the specified dom element.
  64. * @param element Defines the element to stop listening the inputs from
  65. */
  66. public detachControl(element: Nullable<HTMLElement>): void {
  67. this.camera.getScene().gamepadManager.onGamepadConnectedObservable.remove(this._onGamepadConnectedObserver);
  68. this.camera.getScene().gamepadManager.onGamepadDisconnectedObservable.remove(this._onGamepadDisconnectedObserver);
  69. this.gamepad = null;
  70. }
  71. /**
  72. * Update the current camera state depending on the inputs that have been used this frame.
  73. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  74. */
  75. public checkInputs(): void {
  76. if (this.gamepad && this.gamepad.leftStick) {
  77. var camera = this.camera;
  78. var LSValues = this.gamepad.leftStick;
  79. var normalizedLX = LSValues.x / this.gamepadMoveSensibility;
  80. var normalizedLY = LSValues.y / this.gamepadMoveSensibility;
  81. LSValues.x = Math.abs(normalizedLX) > 0.005 ? 0 + normalizedLX : 0;
  82. LSValues.y = Math.abs(normalizedLY) > 0.005 ? 0 + normalizedLY : 0;
  83. var RSValues = this.gamepad.rightStick;
  84. if (RSValues) {
  85. var normalizedRX = RSValues.x / this.gamepadAngularSensibility;
  86. var normalizedRY = RSValues.y / this.gamepadAngularSensibility;
  87. RSValues.x = Math.abs(normalizedRX) > 0.001 ? 0 + normalizedRX : 0;
  88. RSValues.y = Math.abs(normalizedRY) > 0.001 ? 0 + normalizedRY : 0;
  89. }
  90. else {
  91. RSValues = { x: 0, y: 0 };
  92. }
  93. if (!camera.rotationQuaternion) {
  94. Matrix.RotationYawPitchRollToRef(camera.rotation.y, camera.rotation.x, 0, this._cameraTransform);
  95. } else {
  96. camera.rotationQuaternion.toRotationMatrix(this._cameraTransform);
  97. }
  98. var speed = camera._computeLocalCameraSpeed() * 50.0;
  99. this._vector3.copyFromFloats(LSValues.x * speed, 0, -LSValues.y * speed);
  100. Vector3.TransformCoordinatesToRef(this._vector3, this._cameraTransform, this._deltaTransform);
  101. camera.cameraDirection.addInPlace(this._deltaTransform);
  102. this._vector2.copyFromFloats(RSValues.y, RSValues.x);
  103. camera.cameraRotation.addInPlace(this._vector2);
  104. }
  105. }
  106. /**
  107. * Gets the class name of the current intput.
  108. * @returns the class name
  109. */
  110. public getClassName(): string {
  111. return "FreeCameraGamepadInput";
  112. }
  113. /**
  114. * Get the friendly name associated with the input class.
  115. * @returns the input friendly name
  116. */
  117. public getSimpleName(): string {
  118. return "gamepad";
  119. }
  120. }
  121. (<any>CameraInputTypes)["FreeCameraGamepadInput"] = FreeCameraGamepadInput;