freeCameraVirtualJoystickInput.js 3.7 KB

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