freeCameraGamepadInput.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.vector";
  7. import { Gamepad } from "../../Gamepads/gamepad";
  8. /**
  9. * Manage the gamepad inputs to control a free camera.
  10. * @see https://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 _yAxisScale = 1.0;
  34. /**
  35. * Gets or sets a boolean indicating that Yaxis (for right stick) should be inverted
  36. */
  37. public get invertYAxis() {
  38. return this._yAxisScale !== 1.0;
  39. }
  40. public set invertYAxis(value: boolean) {
  41. this._yAxisScale = value ? -1.0 : 1.0;
  42. }
  43. // private members
  44. private _onGamepadConnectedObserver: Nullable<Observer<Gamepad>>;
  45. private _onGamepadDisconnectedObserver: Nullable<Observer<Gamepad>>;
  46. private _cameraTransform: Matrix = Matrix.Identity();
  47. private _deltaTransform: Vector3 = Vector3.Zero();
  48. private _vector3: Vector3 = Vector3.Zero();
  49. private _vector2: Vector2 = Vector2.Zero();
  50. /**
  51. * Attach the input controls to a specific dom element to get the input from.
  52. * @param element Defines the element the controls should be listened from
  53. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  54. */
  55. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  56. let manager = this.camera.getScene().gamepadManager;
  57. this._onGamepadConnectedObserver = manager.onGamepadConnectedObservable.add((gamepad) => {
  58. if (gamepad.type !== Gamepad.POSE_ENABLED) {
  59. // prioritize XBOX gamepads.
  60. if (!this.gamepad || gamepad.type === Gamepad.XBOX) {
  61. this.gamepad = gamepad;
  62. }
  63. }
  64. });
  65. this._onGamepadDisconnectedObserver = manager.onGamepadDisconnectedObservable.add((gamepad) => {
  66. if (this.gamepad === gamepad) {
  67. this.gamepad = null;
  68. }
  69. });
  70. this.gamepad = manager.getGamepadByType(Gamepad.XBOX);
  71. }
  72. /**
  73. * Detach the current controls from the specified dom element.
  74. * @param element Defines the element to stop listening the inputs from
  75. */
  76. public detachControl(element: Nullable<HTMLElement>): void {
  77. this.camera.getScene().gamepadManager.onGamepadConnectedObservable.remove(this._onGamepadConnectedObserver);
  78. this.camera.getScene().gamepadManager.onGamepadDisconnectedObservable.remove(this._onGamepadDisconnectedObserver);
  79. this.gamepad = null;
  80. }
  81. /**
  82. * Update the current camera state depending on the inputs that have been used this frame.
  83. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  84. */
  85. public checkInputs(): void {
  86. if (this.gamepad && this.gamepad.leftStick) {
  87. var camera = this.camera;
  88. var LSValues = this.gamepad.leftStick;
  89. var normalizedLX = LSValues.x / this.gamepadMoveSensibility;
  90. var normalizedLY = LSValues.y / this.gamepadMoveSensibility;
  91. LSValues.x = Math.abs(normalizedLX) > 0.005 ? 0 + normalizedLX : 0;
  92. LSValues.y = Math.abs(normalizedLY) > 0.005 ? 0 + normalizedLY : 0;
  93. var RSValues = this.gamepad.rightStick;
  94. if (RSValues) {
  95. var normalizedRX = RSValues.x / this.gamepadAngularSensibility;
  96. var normalizedRY = (RSValues.y / this.gamepadAngularSensibility) * this._yAxisScale;
  97. RSValues.x = Math.abs(normalizedRX) > 0.001 ? 0 + normalizedRX : 0;
  98. RSValues.y = Math.abs(normalizedRY) > 0.001 ? 0 + normalizedRY : 0;
  99. }
  100. else {
  101. RSValues = { x: 0, y: 0 };
  102. }
  103. if (!camera.rotationQuaternion) {
  104. Matrix.RotationYawPitchRollToRef(camera.rotation.y, camera.rotation.x, 0, this._cameraTransform);
  105. } else {
  106. camera.rotationQuaternion.toRotationMatrix(this._cameraTransform);
  107. }
  108. var speed = camera._computeLocalCameraSpeed() * 50.0;
  109. this._vector3.copyFromFloats(LSValues.x * speed, 0, -LSValues.y * speed);
  110. Vector3.TransformCoordinatesToRef(this._vector3, this._cameraTransform, this._deltaTransform);
  111. camera.cameraDirection.addInPlace(this._deltaTransform);
  112. this._vector2.copyFromFloats(RSValues.y, RSValues.x);
  113. camera.cameraRotation.addInPlace(this._vector2);
  114. }
  115. }
  116. /**
  117. * Gets the class name of the current intput.
  118. * @returns the class name
  119. */
  120. public getClassName(): string {
  121. return "FreeCameraGamepadInput";
  122. }
  123. /**
  124. * Get the friendly name associated with the input class.
  125. * @returns the input friendly name
  126. */
  127. public getSimpleName(): string {
  128. return "gamepad";
  129. }
  130. }
  131. (<any>CameraInputTypes)["FreeCameraGamepadInput"] = FreeCameraGamepadInput;