babylon.freeCameraGamepadInput.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. module BABYLON {
  2. export class FreeCameraGamepadInput implements ICameraInput<FreeCamera> {
  3. camera: FreeCamera;
  4. public gamepad: Gamepad;
  5. private _gamepads: Gamepads<Gamepad>;
  6. @serialize()
  7. public gamepadAngularSensibility = 200;
  8. @serialize()
  9. public gamepadMoveSensibility = 40;
  10. // private members
  11. private _cameraTransform: Matrix = Matrix.Identity();
  12. private _deltaTransform: Vector3 = Vector3.Zero();
  13. private _vector3: Vector3 = Vector3.Zero();
  14. private _vector2: Vector2 = Vector2.Zero();
  15. attachControl(element: HTMLElement, noPreventDefault?: boolean) {
  16. this._gamepads = new Gamepads((gamepad: Gamepad) => { this._onNewGameConnected(gamepad); });
  17. }
  18. detachControl(element: HTMLElement) {
  19. if (this._gamepads) {
  20. this._gamepads.dispose();
  21. }
  22. this.gamepad = null;
  23. }
  24. checkInputs() {
  25. if (this.gamepad && this.gamepad.leftStick) {
  26. var camera = this.camera;
  27. var LSValues = this.gamepad.leftStick;
  28. var normalizedLX = LSValues.x / this.gamepadMoveSensibility;
  29. var normalizedLY = LSValues.y / this.gamepadMoveSensibility;
  30. LSValues.x = Math.abs(normalizedLX) > 0.005 ? 0 + normalizedLX : 0;
  31. LSValues.y = Math.abs(normalizedLY) > 0.005 ? 0 + normalizedLY : 0;
  32. var RSValues = this.gamepad.rightStick;
  33. if (RSValues) {
  34. var normalizedRX = RSValues.x / this.gamepadAngularSensibility;
  35. var normalizedRY = RSValues.y / this.gamepadAngularSensibility;
  36. RSValues.x = Math.abs(normalizedRX) > 0.001 ? 0 + normalizedRX : 0;
  37. RSValues.y = Math.abs(normalizedRY) > 0.001 ? 0 + normalizedRY : 0;
  38. }
  39. else {
  40. RSValues = {x:0, y:0};
  41. }
  42. if (!camera.rotationQuaternion) {
  43. Matrix.RotationYawPitchRollToRef(camera.rotation.y, camera.rotation.x, 0, this._cameraTransform);
  44. } else {
  45. camera.rotationQuaternion.toRotationMatrix(this._cameraTransform);
  46. }
  47. var speed = camera._computeLocalCameraSpeed() * 50.0;
  48. this._vector3.copyFromFloats(LSValues.x * speed, 0, -LSValues.y * speed);
  49. Vector3.TransformCoordinatesToRef(this._vector3, this._cameraTransform, this._deltaTransform);
  50. camera.cameraDirection.addInPlace(this._deltaTransform);
  51. this._vector2.copyFromFloats(RSValues.y, RSValues.x)
  52. camera.cameraRotation.addInPlace(this._vector2);
  53. }
  54. }
  55. private _onNewGameConnected(gamepad: Gamepad) {
  56. // Only the first gamepad found can control the camera
  57. if (gamepad.type !== Gamepad.POSE_ENABLED) {
  58. // prioritize XBOX gamepads.
  59. if (!this.gamepad || gamepad.type === Gamepad.XBOX) {
  60. this.gamepad = gamepad;
  61. }
  62. }
  63. }
  64. getClassName(): string {
  65. return "FreeCameraGamepadInput";
  66. }
  67. getSimpleName() {
  68. return "gamepad";
  69. }
  70. }
  71. CameraInputTypes["FreeCameraGamepadInput"] = FreeCameraGamepadInput;
  72. }