universalCamera.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { TouchCamera } from "./touchCamera";
  2. import { Node } from "../node";
  3. import { FreeCameraGamepadInput } from "../Cameras/Inputs/freeCameraGamepadInput";
  4. import { Scene } from "../scene";
  5. import { Vector3 } from "../Maths/math.vector";
  6. import { Camera } from "./camera";
  7. import "../Gamepads/gamepadSceneComponent";
  8. Node.AddNodeConstructor("FreeCamera", (name, scene) => {
  9. // Forcing to use the Universal camera
  10. return () => new UniversalCamera(name, Vector3.Zero(), scene);
  11. });
  12. /**
  13. * The Universal Camera is the one to choose for first person shooter type games, and works with all the keyboard, mouse, touch and gamepads. This replaces the earlier Free Camera,
  14. * which still works and will still be found in many Playgrounds.
  15. * @see https://doc.babylonjs.com/features/cameras#universal-camera
  16. */
  17. export class UniversalCamera extends TouchCamera {
  18. /**
  19. * Defines the gamepad rotation sensiblity.
  20. * This is the threshold from when rotation starts to be accounted for to prevent jittering.
  21. */
  22. public get gamepadAngularSensibility(): number {
  23. var gamepad = <FreeCameraGamepadInput>this.inputs.attached["gamepad"];
  24. if (gamepad) {
  25. return gamepad.gamepadAngularSensibility;
  26. }
  27. return 0;
  28. }
  29. public set gamepadAngularSensibility(value: number) {
  30. var gamepad = <FreeCameraGamepadInput>this.inputs.attached["gamepad"];
  31. if (gamepad) {
  32. gamepad.gamepadAngularSensibility = value;
  33. }
  34. }
  35. /**
  36. * Defines the gamepad move sensiblity.
  37. * This is the threshold from when moving starts to be accounted for for to prevent jittering.
  38. */
  39. public get gamepadMoveSensibility(): number {
  40. var gamepad = <FreeCameraGamepadInput>this.inputs.attached["gamepad"];
  41. if (gamepad) {
  42. return gamepad.gamepadMoveSensibility;
  43. }
  44. return 0;
  45. }
  46. public set gamepadMoveSensibility(value: number) {
  47. var gamepad = <FreeCameraGamepadInput>this.inputs.attached["gamepad"];
  48. if (gamepad) {
  49. gamepad.gamepadMoveSensibility = value;
  50. }
  51. }
  52. /**
  53. * The Universal Camera is the one to choose for first person shooter type games, and works with all the keyboard, mouse, touch and gamepads. This replaces the earlier Free Camera,
  54. * which still works and will still be found in many Playgrounds.
  55. * @see https://doc.babylonjs.com/features/cameras#universal-camera
  56. * @param name Define the name of the camera in the scene
  57. * @param position Define the start position of the camera in the scene
  58. * @param scene Define the scene the camera belongs to
  59. */
  60. constructor(name: string, position: Vector3, scene: Scene) {
  61. super(name, position, scene);
  62. this.inputs.addGamepad();
  63. }
  64. /**
  65. * Gets the current object class name.
  66. * @return the class name
  67. */
  68. public getClassName(): string {
  69. return "UniversalCamera";
  70. }
  71. }
  72. Camera._createDefaultParsedCamera = (name: string, scene: Scene) => {
  73. return new UniversalCamera(name, Vector3.Zero(), scene);
  74. };