freeCameraMouseWheelInput.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import { __decorate } from "../../tslib.es6.js";
  2. import { serialize } from "../../Misc/decorators.js";
  3. import { CameraInputTypes } from "../../Cameras/cameraInputsManager.js";
  4. import { BaseCameraMouseWheelInput } from "../../Cameras/Inputs/BaseCameraMouseWheelInput.js";
  5. import { Matrix, Vector3 } from "../../Maths/math.vector.js";
  6. import { Coordinate } from "../../Maths/math.axis.js";
  7. // eslint-disable-next-line @typescript-eslint/naming-convention
  8. var _CameraProperty;
  9. (function (_CameraProperty) {
  10. _CameraProperty[_CameraProperty["MoveRelative"] = 0] = "MoveRelative";
  11. _CameraProperty[_CameraProperty["RotateRelative"] = 1] = "RotateRelative";
  12. _CameraProperty[_CameraProperty["MoveScene"] = 2] = "MoveScene";
  13. })(_CameraProperty || (_CameraProperty = {}));
  14. /**
  15. * Manage the mouse wheel inputs to control a free camera.
  16. * @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
  17. */
  18. export class FreeCameraMouseWheelInput extends BaseCameraMouseWheelInput {
  19. constructor() {
  20. super(...arguments);
  21. this._moveRelative = Vector3.Zero();
  22. this._rotateRelative = Vector3.Zero();
  23. this._moveScene = Vector3.Zero();
  24. /**
  25. * These are set to the desired default behaviour.
  26. */
  27. this._wheelXAction = _CameraProperty.MoveRelative;
  28. this._wheelXActionCoordinate = Coordinate.X;
  29. this._wheelYAction = _CameraProperty.MoveRelative;
  30. this._wheelYActionCoordinate = Coordinate.Z;
  31. this._wheelZAction = null;
  32. this._wheelZActionCoordinate = null;
  33. }
  34. /**
  35. * Gets the class name of the current input.
  36. * @returns the class name
  37. */
  38. getClassName() {
  39. return "FreeCameraMouseWheelInput";
  40. }
  41. /**
  42. * Set which movement axis (relative to camera's orientation) the mouse
  43. * wheel's X axis controls.
  44. * @param axis The axis to be moved. Set null to clear.
  45. */
  46. set wheelXMoveRelative(axis) {
  47. if (axis === null && this._wheelXAction !== _CameraProperty.MoveRelative) {
  48. // Attempting to clear different _wheelXAction.
  49. return;
  50. }
  51. this._wheelXAction = _CameraProperty.MoveRelative;
  52. this._wheelXActionCoordinate = axis;
  53. }
  54. /**
  55. * Get the configured movement axis (relative to camera's orientation) the
  56. * mouse wheel's X axis controls.
  57. * @returns The configured axis or null if none.
  58. */
  59. get wheelXMoveRelative() {
  60. if (this._wheelXAction !== _CameraProperty.MoveRelative) {
  61. return null;
  62. }
  63. return this._wheelXActionCoordinate;
  64. }
  65. /**
  66. * Set which movement axis (relative to camera's orientation) the mouse
  67. * wheel's Y axis controls.
  68. * @param axis The axis to be moved. Set null to clear.
  69. */
  70. set wheelYMoveRelative(axis) {
  71. if (axis === null && this._wheelYAction !== _CameraProperty.MoveRelative) {
  72. // Attempting to clear different _wheelYAction.
  73. return;
  74. }
  75. this._wheelYAction = _CameraProperty.MoveRelative;
  76. this._wheelYActionCoordinate = axis;
  77. }
  78. /**
  79. * Get the configured movement axis (relative to camera's orientation) the
  80. * mouse wheel's Y axis controls.
  81. * @returns The configured axis or null if none.
  82. */
  83. get wheelYMoveRelative() {
  84. if (this._wheelYAction !== _CameraProperty.MoveRelative) {
  85. return null;
  86. }
  87. return this._wheelYActionCoordinate;
  88. }
  89. /**
  90. * Set which movement axis (relative to camera's orientation) the mouse
  91. * wheel's Z axis controls.
  92. * @param axis The axis to be moved. Set null to clear.
  93. */
  94. set wheelZMoveRelative(axis) {
  95. if (axis === null && this._wheelZAction !== _CameraProperty.MoveRelative) {
  96. // Attempting to clear different _wheelZAction.
  97. return;
  98. }
  99. this._wheelZAction = _CameraProperty.MoveRelative;
  100. this._wheelZActionCoordinate = axis;
  101. }
  102. /**
  103. * Get the configured movement axis (relative to camera's orientation) the
  104. * mouse wheel's Z axis controls.
  105. * @returns The configured axis or null if none.
  106. */
  107. get wheelZMoveRelative() {
  108. if (this._wheelZAction !== _CameraProperty.MoveRelative) {
  109. return null;
  110. }
  111. return this._wheelZActionCoordinate;
  112. }
  113. /**
  114. * Set which rotation axis (relative to camera's orientation) the mouse
  115. * wheel's X axis controls.
  116. * @param axis The axis to be moved. Set null to clear.
  117. */
  118. set wheelXRotateRelative(axis) {
  119. if (axis === null && this._wheelXAction !== _CameraProperty.RotateRelative) {
  120. // Attempting to clear different _wheelXAction.
  121. return;
  122. }
  123. this._wheelXAction = _CameraProperty.RotateRelative;
  124. this._wheelXActionCoordinate = axis;
  125. }
  126. /**
  127. * Get the configured rotation axis (relative to camera's orientation) the
  128. * mouse wheel's X axis controls.
  129. * @returns The configured axis or null if none.
  130. */
  131. get wheelXRotateRelative() {
  132. if (this._wheelXAction !== _CameraProperty.RotateRelative) {
  133. return null;
  134. }
  135. return this._wheelXActionCoordinate;
  136. }
  137. /**
  138. * Set which rotation axis (relative to camera's orientation) the mouse
  139. * wheel's Y axis controls.
  140. * @param axis The axis to be moved. Set null to clear.
  141. */
  142. set wheelYRotateRelative(axis) {
  143. if (axis === null && this._wheelYAction !== _CameraProperty.RotateRelative) {
  144. // Attempting to clear different _wheelYAction.
  145. return;
  146. }
  147. this._wheelYAction = _CameraProperty.RotateRelative;
  148. this._wheelYActionCoordinate = axis;
  149. }
  150. /**
  151. * Get the configured rotation axis (relative to camera's orientation) the
  152. * mouse wheel's Y axis controls.
  153. * @returns The configured axis or null if none.
  154. */
  155. get wheelYRotateRelative() {
  156. if (this._wheelYAction !== _CameraProperty.RotateRelative) {
  157. return null;
  158. }
  159. return this._wheelYActionCoordinate;
  160. }
  161. /**
  162. * Set which rotation axis (relative to camera's orientation) the mouse
  163. * wheel's Z axis controls.
  164. * @param axis The axis to be moved. Set null to clear.
  165. */
  166. set wheelZRotateRelative(axis) {
  167. if (axis === null && this._wheelZAction !== _CameraProperty.RotateRelative) {
  168. // Attempting to clear different _wheelZAction.
  169. return;
  170. }
  171. this._wheelZAction = _CameraProperty.RotateRelative;
  172. this._wheelZActionCoordinate = axis;
  173. }
  174. /**
  175. * Get the configured rotation axis (relative to camera's orientation) the
  176. * mouse wheel's Z axis controls.
  177. * @returns The configured axis or null if none.
  178. */
  179. get wheelZRotateRelative() {
  180. if (this._wheelZAction !== _CameraProperty.RotateRelative) {
  181. return null;
  182. }
  183. return this._wheelZActionCoordinate;
  184. }
  185. /**
  186. * Set which movement axis (relative to the scene) the mouse wheel's X axis
  187. * controls.
  188. * @param axis The axis to be moved. Set null to clear.
  189. */
  190. set wheelXMoveScene(axis) {
  191. if (axis === null && this._wheelXAction !== _CameraProperty.MoveScene) {
  192. // Attempting to clear different _wheelXAction.
  193. return;
  194. }
  195. this._wheelXAction = _CameraProperty.MoveScene;
  196. this._wheelXActionCoordinate = axis;
  197. }
  198. /**
  199. * Get the configured movement axis (relative to the scene) the mouse wheel's
  200. * X axis controls.
  201. * @returns The configured axis or null if none.
  202. */
  203. get wheelXMoveScene() {
  204. if (this._wheelXAction !== _CameraProperty.MoveScene) {
  205. return null;
  206. }
  207. return this._wheelXActionCoordinate;
  208. }
  209. /**
  210. * Set which movement axis (relative to the scene) the mouse wheel's Y axis
  211. * controls.
  212. * @param axis The axis to be moved. Set null to clear.
  213. */
  214. set wheelYMoveScene(axis) {
  215. if (axis === null && this._wheelYAction !== _CameraProperty.MoveScene) {
  216. // Attempting to clear different _wheelYAction.
  217. return;
  218. }
  219. this._wheelYAction = _CameraProperty.MoveScene;
  220. this._wheelYActionCoordinate = axis;
  221. }
  222. /**
  223. * Get the configured movement axis (relative to the scene) the mouse wheel's
  224. * Y axis controls.
  225. * @returns The configured axis or null if none.
  226. */
  227. get wheelYMoveScene() {
  228. if (this._wheelYAction !== _CameraProperty.MoveScene) {
  229. return null;
  230. }
  231. return this._wheelYActionCoordinate;
  232. }
  233. /**
  234. * Set which movement axis (relative to the scene) the mouse wheel's Z axis
  235. * controls.
  236. * @param axis The axis to be moved. Set null to clear.
  237. */
  238. set wheelZMoveScene(axis) {
  239. if (axis === null && this._wheelZAction !== _CameraProperty.MoveScene) {
  240. // Attempting to clear different _wheelZAction.
  241. return;
  242. }
  243. this._wheelZAction = _CameraProperty.MoveScene;
  244. this._wheelZActionCoordinate = axis;
  245. }
  246. /**
  247. * Get the configured movement axis (relative to the scene) the mouse wheel's
  248. * Z axis controls.
  249. * @returns The configured axis or null if none.
  250. */
  251. get wheelZMoveScene() {
  252. if (this._wheelZAction !== _CameraProperty.MoveScene) {
  253. return null;
  254. }
  255. return this._wheelZActionCoordinate;
  256. }
  257. /**
  258. * Called for each rendered frame.
  259. */
  260. checkInputs() {
  261. if (this._wheelDeltaX === 0 && this._wheelDeltaY === 0 && this._wheelDeltaZ == 0) {
  262. return;
  263. }
  264. // Clear the camera properties that we might be updating.
  265. this._moveRelative.setAll(0);
  266. this._rotateRelative.setAll(0);
  267. this._moveScene.setAll(0);
  268. // Set the camera properties that are to be updated.
  269. this._updateCamera();
  270. if (this.camera.getScene().useRightHandedSystem) {
  271. // TODO: Does this need done for worldUpdate too?
  272. this._moveRelative.z *= -1;
  273. }
  274. // Convert updates relative to camera to world position update.
  275. const cameraTransformMatrix = Matrix.Zero();
  276. this.camera.getViewMatrix().invertToRef(cameraTransformMatrix);
  277. const transformedDirection = Vector3.Zero();
  278. Vector3.TransformNormalToRef(this._moveRelative, cameraTransformMatrix, transformedDirection);
  279. // Apply updates to camera position.
  280. this.camera.cameraRotation.x += this._rotateRelative.x / 200;
  281. this.camera.cameraRotation.y += this._rotateRelative.y / 200;
  282. this.camera.cameraDirection.addInPlace(transformedDirection);
  283. this.camera.cameraDirection.addInPlace(this._moveScene);
  284. // Call the base class implementation to handle observers and do cleanup.
  285. super.checkInputs();
  286. }
  287. /**
  288. * Update the camera according to any configured properties for the 3
  289. * mouse-wheel axis.
  290. */
  291. _updateCamera() {
  292. // Do the camera updates for each of the 3 touch-wheel axis.
  293. this._updateCameraProperty(this._wheelDeltaX, this._wheelXAction, this._wheelXActionCoordinate);
  294. this._updateCameraProperty(this._wheelDeltaY, this._wheelYAction, this._wheelYActionCoordinate);
  295. this._updateCameraProperty(this._wheelDeltaZ, this._wheelZAction, this._wheelZActionCoordinate);
  296. }
  297. /**
  298. * Update one property of the camera.
  299. * @param value
  300. * @param cameraProperty
  301. * @param coordinate
  302. */
  303. _updateCameraProperty(
  304. /* Mouse-wheel delta. */
  305. value,
  306. /* Camera property to be changed. */
  307. cameraProperty,
  308. /* Axis of Camera property to be changed. */
  309. coordinate) {
  310. if (value === 0) {
  311. // Mouse wheel has not moved.
  312. return;
  313. }
  314. if (cameraProperty === null || coordinate === null) {
  315. // Mouse wheel axis not configured.
  316. return;
  317. }
  318. let action = null;
  319. switch (cameraProperty) {
  320. case _CameraProperty.MoveRelative:
  321. action = this._moveRelative;
  322. break;
  323. case _CameraProperty.RotateRelative:
  324. action = this._rotateRelative;
  325. break;
  326. case _CameraProperty.MoveScene:
  327. action = this._moveScene;
  328. break;
  329. }
  330. switch (coordinate) {
  331. case Coordinate.X:
  332. action.set(value, 0, 0);
  333. break;
  334. case Coordinate.Y:
  335. action.set(0, value, 0);
  336. break;
  337. case Coordinate.Z:
  338. action.set(0, 0, value);
  339. break;
  340. }
  341. }
  342. }
  343. __decorate([
  344. serialize()
  345. ], FreeCameraMouseWheelInput.prototype, "wheelXMoveRelative", null);
  346. __decorate([
  347. serialize()
  348. ], FreeCameraMouseWheelInput.prototype, "wheelYMoveRelative", null);
  349. __decorate([
  350. serialize()
  351. ], FreeCameraMouseWheelInput.prototype, "wheelZMoveRelative", null);
  352. __decorate([
  353. serialize()
  354. ], FreeCameraMouseWheelInput.prototype, "wheelXRotateRelative", null);
  355. __decorate([
  356. serialize()
  357. ], FreeCameraMouseWheelInput.prototype, "wheelYRotateRelative", null);
  358. __decorate([
  359. serialize()
  360. ], FreeCameraMouseWheelInput.prototype, "wheelZRotateRelative", null);
  361. __decorate([
  362. serialize()
  363. ], FreeCameraMouseWheelInput.prototype, "wheelXMoveScene", null);
  364. __decorate([
  365. serialize()
  366. ], FreeCameraMouseWheelInput.prototype, "wheelYMoveScene", null);
  367. __decorate([
  368. serialize()
  369. ], FreeCameraMouseWheelInput.prototype, "wheelZMoveScene", null);
  370. CameraInputTypes["FreeCameraMouseWheelInput"] = FreeCameraMouseWheelInput;
  371. //# sourceMappingURL=freeCameraMouseWheelInput.js.map