freeCameraVirtualJoystickInput.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { VirtualJoystick, JoystickAxis } from "../../Misc/virtualJoystick";
  2. import { Nullable } from "../../types";
  3. import { ICameraInput, CameraInputTypes } from "../../Cameras/cameraInputsManager";
  4. import { FreeCamera } from "../../Cameras/freeCamera";
  5. import { Matrix, Vector3 } from "../../Maths/math.vector";
  6. import { FreeCameraInputsManager } from "../../Cameras/freeCameraInputsManager";
  7. // Module augmentation to abstract virtual joystick from camera.
  8. declare module "../../Cameras/freeCameraInputsManager" {
  9. export interface FreeCameraInputsManager {
  10. /**
  11. * Add virtual joystick input support to the input manager.
  12. * @returns the current input manager
  13. */
  14. addVirtualJoystick(): FreeCameraInputsManager;
  15. }
  16. }
  17. /**
  18. * Add virtual joystick input support to the input manager.
  19. * @returns the current input manager
  20. */
  21. FreeCameraInputsManager.prototype.addVirtualJoystick = function(): FreeCameraInputsManager {
  22. this.add(new FreeCameraVirtualJoystickInput());
  23. return this;
  24. };
  25. /**
  26. * Manage the Virtual Joystick inputs to control the movement of a free camera.
  27. * @see https://doc.babylonjs.com/how_to/customizing_camera_inputs
  28. */
  29. export class FreeCameraVirtualJoystickInput implements ICameraInput<FreeCamera> {
  30. /**
  31. * Defines the camera the input is attached to.
  32. */
  33. public camera: FreeCamera;
  34. private _leftjoystick: VirtualJoystick;
  35. private _rightjoystick: VirtualJoystick;
  36. /**
  37. * Gets the left stick of the virtual joystick.
  38. * @returns The virtual Joystick
  39. */
  40. public getLeftJoystick(): VirtualJoystick {
  41. return this._leftjoystick;
  42. }
  43. /**
  44. * Gets the right stick of the virtual joystick.
  45. * @returns The virtual Joystick
  46. */
  47. public getRightJoystick(): VirtualJoystick {
  48. return this._rightjoystick;
  49. }
  50. /**
  51. * Update the current camera state depending on the inputs that have been used this frame.
  52. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  53. */
  54. public checkInputs() {
  55. if (this._leftjoystick) {
  56. var camera = this.camera;
  57. var speed = camera._computeLocalCameraSpeed() * 50;
  58. var cameraTransform = Matrix.RotationYawPitchRoll(camera.rotation.y, camera.rotation.x, 0);
  59. var deltaTransform = Vector3.TransformCoordinates(new Vector3(this._leftjoystick.deltaPosition.x * speed, this._leftjoystick.deltaPosition.y * speed, this._leftjoystick.deltaPosition.z * speed), cameraTransform);
  60. camera.cameraDirection = camera.cameraDirection.add(deltaTransform);
  61. camera.cameraRotation = camera.cameraRotation.addVector3(this._rightjoystick.deltaPosition);
  62. if (!this._leftjoystick.pressed) {
  63. this._leftjoystick.deltaPosition = this._leftjoystick.deltaPosition.scale(0.9);
  64. }
  65. if (!this._rightjoystick.pressed) {
  66. this._rightjoystick.deltaPosition = this._rightjoystick.deltaPosition.scale(0.9);
  67. }
  68. }
  69. }
  70. /**
  71. * Attach the input controls to a specific dom element to get the input from.
  72. * @param element Defines the element the controls should be listened from
  73. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  74. */
  75. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  76. this._leftjoystick = new VirtualJoystick(true);
  77. this._leftjoystick.setAxisForUpDown(JoystickAxis.Z);
  78. this._leftjoystick.setAxisForLeftRight(JoystickAxis.X);
  79. this._leftjoystick.setJoystickSensibility(0.15);
  80. this._rightjoystick = new VirtualJoystick(false);
  81. this._rightjoystick.setAxisForUpDown(JoystickAxis.X);
  82. this._rightjoystick.setAxisForLeftRight(JoystickAxis.Y);
  83. this._rightjoystick.reverseUpDown = true;
  84. this._rightjoystick.setJoystickSensibility(0.05);
  85. this._rightjoystick.setJoystickColor("yellow");
  86. }
  87. /**
  88. * Detach the current controls from the specified dom element.
  89. * @param element Defines the element to stop listening the inputs from
  90. */
  91. public detachControl(element: Nullable<HTMLElement>): void {
  92. this._leftjoystick.releaseCanvas();
  93. this._rightjoystick.releaseCanvas();
  94. }
  95. /**
  96. * Gets the class name of the current intput.
  97. * @returns the class name
  98. */
  99. public getClassName(): string {
  100. return "FreeCameraVirtualJoystickInput";
  101. }
  102. /**
  103. * Get the friendly name associated with the input class.
  104. * @returns the input friendly name
  105. */
  106. public getSimpleName(): string {
  107. return "virtualJoystick";
  108. }
  109. }
  110. (<any>CameraInputTypes)["FreeCameraVirtualJoystickInput"] = FreeCameraVirtualJoystickInput;