freeCameraGamepadInput.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. */
  53. public attachControl(): void {
  54. let manager = this.camera.getScene().gamepadManager;
  55. this._onGamepadConnectedObserver = manager.onGamepadConnectedObservable.add((gamepad) => {
  56. if (gamepad.type !== Gamepad.POSE_ENABLED) {
  57. // prioritize XBOX gamepads.
  58. if (!this.gamepad || gamepad.type === Gamepad.XBOX) {
  59. this.gamepad = gamepad;
  60. }
  61. }
  62. });
  63. this._onGamepadDisconnectedObserver = manager.onGamepadDisconnectedObservable.add((gamepad) => {
  64. if (this.gamepad === gamepad) {
  65. this.gamepad = null;
  66. }
  67. });
  68. // check if there are already other controllers connected
  69. this.gamepad = manager.getGamepadByType(Gamepad.XBOX);
  70. // if no xbox controller was found, but there are gamepad controllers, take the first one
  71. if (!this.gamepad && manager.gamepads.length) {
  72. this.gamepad = manager.gamepads[0];
  73. }
  74. }
  75. /**
  76. * Detach the current controls from the specified dom element.
  77. */
  78. public detachControl(): void {
  79. this.camera.getScene().gamepadManager.onGamepadConnectedObservable.remove(this._onGamepadConnectedObserver);
  80. this.camera.getScene().gamepadManager.onGamepadDisconnectedObservable.remove(this._onGamepadDisconnectedObserver);
  81. this.gamepad = null;
  82. }
  83. /**
  84. * Update the current camera state depending on the inputs that have been used this frame.
  85. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  86. */
  87. public checkInputs(): void {
  88. if (this.gamepad && this.gamepad.leftStick) {
  89. var camera = this.camera;
  90. var LSValues = this.gamepad.leftStick;
  91. var normalizedLX = LSValues.x / this.gamepadMoveSensibility;
  92. var normalizedLY = LSValues.y / this.gamepadMoveSensibility;
  93. LSValues.x = Math.abs(normalizedLX) > 0.005 ? 0 + normalizedLX : 0;
  94. LSValues.y = Math.abs(normalizedLY) > 0.005 ? 0 + normalizedLY : 0;
  95. var RSValues = this.gamepad.rightStick;
  96. if (RSValues) {
  97. var normalizedRX = RSValues.x / this.gamepadAngularSensibility;
  98. var normalizedRY = (RSValues.y / this.gamepadAngularSensibility) * this._yAxisScale;
  99. RSValues.x = Math.abs(normalizedRX) > 0.001 ? 0 + normalizedRX : 0;
  100. RSValues.y = Math.abs(normalizedRY) > 0.001 ? 0 + normalizedRY : 0;
  101. } else {
  102. RSValues = { x: 0, y: 0 };
  103. }
  104. if (!camera.rotationQuaternion) {
  105. Matrix.RotationYawPitchRollToRef(camera.rotation.y, camera.rotation.x, 0, this._cameraTransform);
  106. } else {
  107. camera.rotationQuaternion.toRotationMatrix(this._cameraTransform);
  108. }
  109. var speed = camera._computeLocalCameraSpeed() * 50.0;
  110. this._vector3.copyFromFloats(LSValues.x * speed, 0, -LSValues.y * speed);
  111. Vector3.TransformCoordinatesToRef(this._vector3, this._cameraTransform, this._deltaTransform);
  112. camera.cameraDirection.addInPlace(this._deltaTransform);
  113. this._vector2.copyFromFloats(RSValues.y, RSValues.x);
  114. camera.cameraRotation.addInPlace(this._vector2);
  115. }
  116. }
  117. /**
  118. * Gets the class name of the current intput.
  119. * @returns the class name
  120. */
  121. public getClassName(): string {
  122. return "FreeCameraGamepadInput";
  123. }
  124. /**
  125. * Get the friendly name associated with the input class.
  126. * @returns the input friendly name
  127. */
  128. public getSimpleName(): string {
  129. return "gamepad";
  130. }
  131. }
  132. (<any>CameraInputTypes)["FreeCameraGamepadInput"] = FreeCameraGamepadInput;