freeCameraVirtualJoystickInput.ts 4.5 KB

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