babylon.freecamera.input.gamepad.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var FreeCameraGamepadInput = (function () {
  10. function FreeCameraGamepadInput() {
  11. this.gamepadAngularSensibility = 200;
  12. this.gamepadMoveSensibility = 40;
  13. }
  14. FreeCameraGamepadInput.prototype.attachControl = function (element, noPreventDefault) {
  15. var _this = this;
  16. this._gamepads = new BABYLON.Gamepads(function (gamepad) { _this._onNewGameConnected(gamepad); });
  17. };
  18. FreeCameraGamepadInput.prototype.detachControl = function (element) {
  19. if (this._gamepads) {
  20. this._gamepads.dispose();
  21. }
  22. this.gamepad = null;
  23. };
  24. FreeCameraGamepadInput.prototype.checkInputs = function () {
  25. if (this.gamepad) {
  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. var normalizedRX = RSValues.x / this.gamepadAngularSensibility;
  34. var normalizedRY = RSValues.y / this.gamepadAngularSensibility;
  35. RSValues.x = Math.abs(normalizedRX) > 0.001 ? 0 + normalizedRX : 0;
  36. RSValues.y = Math.abs(normalizedRY) > 0.001 ? 0 + normalizedRY : 0;
  37. var cameraTransform = BABYLON.Matrix.RotationYawPitchRoll(camera.rotation.y, camera.rotation.x, 0);
  38. var speed = camera._computeLocalCameraSpeed() * 50.0;
  39. var deltaTransform = BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(LSValues.x * speed, 0, -LSValues.y * speed), cameraTransform);
  40. camera.cameraDirection = camera.cameraDirection.add(deltaTransform);
  41. camera.cameraRotation = camera.cameraRotation.add(new BABYLON.Vector2(RSValues.y, RSValues.x));
  42. }
  43. };
  44. FreeCameraGamepadInput.prototype._onNewGameConnected = function (gamepad) {
  45. // Only the first gamepad can control the camera
  46. if (gamepad.index === 0) {
  47. this.gamepad = gamepad;
  48. }
  49. };
  50. FreeCameraGamepadInput.prototype.getTypeName = function () {
  51. return "FreeCameraGamepadInput";
  52. };
  53. FreeCameraGamepadInput.prototype.getSimpleName = function () {
  54. return "gamepad";
  55. };
  56. __decorate([
  57. BABYLON.serialize()
  58. ], FreeCameraGamepadInput.prototype, "gamepadAngularSensibility", void 0);
  59. __decorate([
  60. BABYLON.serialize()
  61. ], FreeCameraGamepadInput.prototype, "gamepadMoveSensibility", void 0);
  62. return FreeCameraGamepadInput;
  63. }());
  64. BABYLON.FreeCameraGamepadInput = FreeCameraGamepadInput;
  65. BABYLON.CameraInputTypes["FreeCameraGamepadInput"] = FreeCameraGamepadInput;
  66. })(BABYLON || (BABYLON = {}));