babylon.freeCameraGamepadInput.ts 5.8 KB

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