followCameraPointersInput.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { __decorate } from "../../tslib.es6.js";
  2. import { serialize } from "../../Misc/decorators.js";
  3. import { CameraInputTypes } from "../../Cameras/cameraInputsManager.js";
  4. import { BaseCameraPointersInput } from "../../Cameras/Inputs/BaseCameraPointersInput.js";
  5. import { Logger } from "../../Misc/logger.js";
  6. /**
  7. * Manage the pointers inputs to control an follow camera.
  8. * @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
  9. */
  10. export class FollowCameraPointersInput extends BaseCameraPointersInput {
  11. constructor() {
  12. super(...arguments);
  13. /**
  14. * Defines the pointer angular sensibility along the X axis or how fast is
  15. * the camera rotating.
  16. * A negative number will reverse the axis direction.
  17. */
  18. this.angularSensibilityX = 1;
  19. /**
  20. * Defines the pointer angular sensibility along the Y axis or how fast is
  21. * the camera rotating.
  22. * A negative number will reverse the axis direction.
  23. */
  24. this.angularSensibilityY = 1;
  25. /**
  26. * Defines the pointer pinch precision or how fast is the camera zooming.
  27. * A negative number will reverse the axis direction.
  28. */
  29. this.pinchPrecision = 10000.0;
  30. /**
  31. * pinchDeltaPercentage will be used instead of pinchPrecision if different
  32. * from 0.
  33. * It defines the percentage of current camera.radius to use as delta when
  34. * pinch zoom is used.
  35. */
  36. this.pinchDeltaPercentage = 0;
  37. /**
  38. * Pointer X axis controls zoom. (X axis modifies camera.radius value.)
  39. */
  40. this.axisXControlRadius = false;
  41. /**
  42. * Pointer X axis controls height. (X axis modifies camera.heightOffset value.)
  43. */
  44. this.axisXControlHeight = false;
  45. /**
  46. * Pointer X axis controls angle. (X axis modifies camera.rotationOffset value.)
  47. */
  48. this.axisXControlRotation = true;
  49. /**
  50. * Pointer Y axis controls zoom. (Y axis modifies camera.radius value.)
  51. */
  52. this.axisYControlRadius = false;
  53. /**
  54. * Pointer Y axis controls height. (Y axis modifies camera.heightOffset value.)
  55. */
  56. this.axisYControlHeight = true;
  57. /**
  58. * Pointer Y axis controls angle. (Y axis modifies camera.rotationOffset value.)
  59. */
  60. this.axisYControlRotation = false;
  61. /**
  62. * Pinch controls zoom. (Pinch modifies camera.radius value.)
  63. */
  64. this.axisPinchControlRadius = true;
  65. /**
  66. * Pinch controls height. (Pinch modifies camera.heightOffset value.)
  67. */
  68. this.axisPinchControlHeight = false;
  69. /**
  70. * Pinch controls angle. (Pinch modifies camera.rotationOffset value.)
  71. */
  72. this.axisPinchControlRotation = false;
  73. /**
  74. * Log error messages if basic misconfiguration has occurred.
  75. */
  76. this.warningEnable = true;
  77. /* Check for obvious misconfiguration. */
  78. this._warningCounter = 0;
  79. }
  80. /**
  81. * Gets the class name of the current input.
  82. * @returns the class name
  83. */
  84. getClassName() {
  85. return "FollowCameraPointersInput";
  86. }
  87. onTouch(pointA, offsetX, offsetY) {
  88. this._warning();
  89. if (this.axisXControlRotation) {
  90. this.camera.rotationOffset += offsetX / this.angularSensibilityX;
  91. }
  92. else if (this.axisYControlRotation) {
  93. this.camera.rotationOffset += offsetY / this.angularSensibilityX;
  94. }
  95. if (this.axisXControlHeight) {
  96. this.camera.heightOffset += offsetX / this.angularSensibilityY;
  97. }
  98. else if (this.axisYControlHeight) {
  99. this.camera.heightOffset += offsetY / this.angularSensibilityY;
  100. }
  101. if (this.axisXControlRadius) {
  102. this.camera.radius -= offsetX / this.angularSensibilityY;
  103. }
  104. else if (this.axisYControlRadius) {
  105. this.camera.radius -= offsetY / this.angularSensibilityY;
  106. }
  107. }
  108. onMultiTouch(pointA, pointB, previousPinchSquaredDistance, pinchSquaredDistance, previousMultiTouchPanPosition, multiTouchPanPosition) {
  109. if (previousPinchSquaredDistance === 0 && previousMultiTouchPanPosition === null) {
  110. // First time this method is called for new pinch.
  111. // Next time this is called there will be a
  112. // previousPinchSquaredDistance and pinchSquaredDistance to compare.
  113. return;
  114. }
  115. if (pinchSquaredDistance === 0 && multiTouchPanPosition === null) {
  116. // Last time this method is called at the end of a pinch.
  117. return;
  118. }
  119. let pinchDelta = (pinchSquaredDistance - previousPinchSquaredDistance) / ((this.pinchPrecision * (this.angularSensibilityX + this.angularSensibilityY)) / 2);
  120. if (this.pinchDeltaPercentage) {
  121. pinchDelta *= 0.01 * this.pinchDeltaPercentage;
  122. if (this.axisPinchControlRotation) {
  123. this.camera.rotationOffset += pinchDelta * this.camera.rotationOffset;
  124. }
  125. if (this.axisPinchControlHeight) {
  126. this.camera.heightOffset += pinchDelta * this.camera.heightOffset;
  127. }
  128. if (this.axisPinchControlRadius) {
  129. this.camera.radius -= pinchDelta * this.camera.radius;
  130. }
  131. }
  132. else {
  133. if (this.axisPinchControlRotation) {
  134. this.camera.rotationOffset += pinchDelta;
  135. }
  136. if (this.axisPinchControlHeight) {
  137. this.camera.heightOffset += pinchDelta;
  138. }
  139. if (this.axisPinchControlRadius) {
  140. this.camera.radius -= pinchDelta;
  141. }
  142. }
  143. }
  144. _warning() {
  145. if (!this.warningEnable || this._warningCounter++ % 100 !== 0) {
  146. return;
  147. }
  148. const warn = "It probably only makes sense to control ONE camera " + "property with each pointer axis. Set 'warningEnable = false' " + "if you are sure. Currently enabled: ";
  149. if (+this.axisXControlRotation + +this.axisXControlHeight + +this.axisXControlRadius <= 1) {
  150. Logger.Warn(warn +
  151. "axisXControlRotation: " +
  152. this.axisXControlRotation +
  153. ", axisXControlHeight: " +
  154. this.axisXControlHeight +
  155. ", axisXControlRadius: " +
  156. this.axisXControlRadius);
  157. }
  158. if (+this.axisYControlRotation + +this.axisYControlHeight + +this.axisYControlRadius <= 1) {
  159. Logger.Warn(warn +
  160. "axisYControlRotation: " +
  161. this.axisYControlRotation +
  162. ", axisYControlHeight: " +
  163. this.axisYControlHeight +
  164. ", axisYControlRadius: " +
  165. this.axisYControlRadius);
  166. }
  167. if (+this.axisPinchControlRotation + +this.axisPinchControlHeight + +this.axisPinchControlRadius <= 1) {
  168. Logger.Warn(warn +
  169. "axisPinchControlRotation: " +
  170. this.axisPinchControlRotation +
  171. ", axisPinchControlHeight: " +
  172. this.axisPinchControlHeight +
  173. ", axisPinchControlRadius: " +
  174. this.axisPinchControlRadius);
  175. }
  176. }
  177. }
  178. __decorate([
  179. serialize()
  180. ], FollowCameraPointersInput.prototype, "angularSensibilityX", void 0);
  181. __decorate([
  182. serialize()
  183. ], FollowCameraPointersInput.prototype, "angularSensibilityY", void 0);
  184. __decorate([
  185. serialize()
  186. ], FollowCameraPointersInput.prototype, "pinchPrecision", void 0);
  187. __decorate([
  188. serialize()
  189. ], FollowCameraPointersInput.prototype, "pinchDeltaPercentage", void 0);
  190. __decorate([
  191. serialize()
  192. ], FollowCameraPointersInput.prototype, "axisXControlRadius", void 0);
  193. __decorate([
  194. serialize()
  195. ], FollowCameraPointersInput.prototype, "axisXControlHeight", void 0);
  196. __decorate([
  197. serialize()
  198. ], FollowCameraPointersInput.prototype, "axisXControlRotation", void 0);
  199. __decorate([
  200. serialize()
  201. ], FollowCameraPointersInput.prototype, "axisYControlRadius", void 0);
  202. __decorate([
  203. serialize()
  204. ], FollowCameraPointersInput.prototype, "axisYControlHeight", void 0);
  205. __decorate([
  206. serialize()
  207. ], FollowCameraPointersInput.prototype, "axisYControlRotation", void 0);
  208. __decorate([
  209. serialize()
  210. ], FollowCameraPointersInput.prototype, "axisPinchControlRadius", void 0);
  211. __decorate([
  212. serialize()
  213. ], FollowCameraPointersInput.prototype, "axisPinchControlHeight", void 0);
  214. __decorate([
  215. serialize()
  216. ], FollowCameraPointersInput.prototype, "axisPinchControlRotation", void 0);
  217. CameraInputTypes["FollowCameraPointersInput"] = FollowCameraPointersInput;
  218. //# sourceMappingURL=followCameraPointersInput.js.map