babylon.arcRotateCamera.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. module BABYLON {
  2. var eventPrefix = Tools.GetPointerPrefix();
  3. export class ArcRotateCamera extends Camera {
  4. public inertialAlphaOffset = 0;
  5. public inertialBetaOffset = 0;
  6. public inertialRadiusOffset = 0;
  7. public lowerAlphaLimit = null;
  8. public upperAlphaLimit = null;
  9. public lowerBetaLimit = 0.01;
  10. public upperBetaLimit = Math.PI;
  11. public lowerRadiusLimit = null;
  12. public upperRadiusLimit = null;
  13. public angularSensibility = 1000.0;
  14. public wheelPrecision = 3.0;
  15. public keysUp = [38];
  16. public keysDown = [40];
  17. public keysLeft = [37];
  18. public keysRight = [39];
  19. public zoomOnFactor = 1;
  20. private _keys = [];
  21. private _viewMatrix = new BABYLON.Matrix();
  22. private _attachedElement: HTMLElement;
  23. private _onPointerDown: (e: PointerEvent) => void;
  24. private _onPointerUp: (e: PointerEvent) => void;
  25. private _onPointerMove: (e: PointerEvent) => void;
  26. private _wheel: (e: MouseWheelEvent) => void;
  27. private _onMouseMove: (e: MouseEvent) => any;
  28. private _onKeyDown: (e: KeyboardEvent) => any;
  29. private _onKeyUp: (e: KeyboardEvent) => any;
  30. private _onLostFocus: (e: FocusEvent) => any;
  31. private _reset: () => void;
  32. private _onGestureStart: (e: PointerEvent) => void;
  33. private _onGesture: (e: MSGestureEvent) => void;
  34. private _MSGestureHandler: MSGesture;
  35. constructor(name: string, public alpha: number, public beta: number, public radius: number, public target: any, scene: Scene) {
  36. super(name, BABYLON.Vector3.Zero(), scene);
  37. this.getViewMatrix();
  38. }
  39. public _getTargetPosition(): Vector3 {
  40. return this.target.position || this.target;
  41. }
  42. // Cache
  43. public _initCache(): void {
  44. super._initCache();
  45. this._cache.target = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  46. this._cache.alpha = undefined;
  47. this._cache.beta = undefined;
  48. this._cache.radius = undefined;
  49. }
  50. public _updateCache(ignoreParentClass?: boolean): void {
  51. if (!ignoreParentClass) {
  52. super._updateCache();
  53. }
  54. this._cache.target.copyFrom(this._getTargetPosition());
  55. this._cache.alpha = this.alpha;
  56. this._cache.beta = this.beta;
  57. this._cache.radius = this.radius;
  58. }
  59. // Synchronized
  60. public _isSynchronizedViewMatrix(): boolean {
  61. if (!super._isSynchronizedViewMatrix())
  62. return false;
  63. return this._cache.target.equals(this._getTargetPosition())
  64. && this._cache.alpha === this.alpha
  65. && this._cache.beta === this.beta
  66. && this._cache.radius === this.radius;
  67. }
  68. // Methods
  69. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  70. var previousPosition;
  71. var pointerId;
  72. if (this._attachedElement) {
  73. return;
  74. }
  75. this._attachedElement = element;
  76. var engine = this.getEngine();
  77. if (this._onPointerDown === undefined) {
  78. this._onPointerDown = evt => {
  79. if (pointerId) {
  80. return;
  81. }
  82. pointerId = evt.pointerId;
  83. previousPosition = {
  84. x: evt.clientX,
  85. y: evt.clientY
  86. };
  87. if (!noPreventDefault) {
  88. evt.preventDefault();
  89. }
  90. };
  91. this._onPointerUp = evt => {
  92. previousPosition = null;
  93. pointerId = null;
  94. if (!noPreventDefault) {
  95. evt.preventDefault();
  96. }
  97. };
  98. this._onPointerMove = evt => {
  99. if (!previousPosition) {
  100. return;
  101. }
  102. if (pointerId !== evt.pointerId) {
  103. return;
  104. }
  105. var offsetX = evt.clientX - previousPosition.x;
  106. var offsetY = evt.clientY - previousPosition.y;
  107. this.inertialAlphaOffset -= offsetX / this.angularSensibility;
  108. this.inertialBetaOffset -= offsetY / this.angularSensibility;
  109. previousPosition = {
  110. x: evt.clientX,
  111. y: evt.clientY
  112. };
  113. if (!noPreventDefault) {
  114. evt.preventDefault();
  115. }
  116. };
  117. this._onMouseMove = evt => {
  118. if (!engine.isPointerLock) {
  119. return;
  120. }
  121. var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  122. var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  123. this.inertialAlphaOffset -= offsetX / this.angularSensibility;
  124. this.inertialBetaOffset -= offsetY / this.angularSensibility;
  125. if (!noPreventDefault) {
  126. evt.preventDefault();
  127. }
  128. };
  129. this._wheel = event => {
  130. var delta = 0;
  131. if (event.wheelDelta) {
  132. delta = event.wheelDelta / (this.wheelPrecision * 40);
  133. } else if (event.detail) {
  134. delta = -event.detail / this.wheelPrecision;
  135. }
  136. if (delta)
  137. this.inertialRadiusOffset += delta;
  138. if (event.preventDefault) {
  139. if (!noPreventDefault) {
  140. event.preventDefault();
  141. }
  142. }
  143. };
  144. this._onKeyDown = evt => {
  145. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  146. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  147. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  148. this.keysRight.indexOf(evt.keyCode) !== -1) {
  149. var index = this._keys.indexOf(evt.keyCode);
  150. if (index === -1) {
  151. this._keys.push(evt.keyCode);
  152. }
  153. if (evt.preventDefault) {
  154. if (!noPreventDefault) {
  155. evt.preventDefault();
  156. }
  157. }
  158. }
  159. };
  160. this._onKeyUp = evt => {
  161. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  162. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  163. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  164. this.keysRight.indexOf(evt.keyCode) !== -1) {
  165. var index = this._keys.indexOf(evt.keyCode);
  166. if (index >= 0) {
  167. this._keys.splice(index, 1);
  168. }
  169. if (evt.preventDefault) {
  170. if (!noPreventDefault) {
  171. evt.preventDefault();
  172. }
  173. }
  174. }
  175. };
  176. this._onLostFocus = () => {
  177. this._keys = [];
  178. pointerId = null;
  179. };
  180. this._onGestureStart = e => {
  181. if (window.MSGesture === undefined) {
  182. return;
  183. }
  184. if (!this._MSGestureHandler) {
  185. this._MSGestureHandler = new MSGesture();
  186. this._MSGestureHandler.target = element;
  187. }
  188. this._MSGestureHandler.addPointer(e.pointerId);
  189. };
  190. this._onGesture = e => {
  191. this.radius *= e.scale;
  192. if (e.preventDefault) {
  193. if (!noPreventDefault) {
  194. e.stopPropagation();
  195. e.preventDefault();
  196. }
  197. }
  198. };
  199. this._reset = () => {
  200. this._keys = [];
  201. this.inertialAlphaOffset = 0;
  202. this.inertialBetaOffset = 0;
  203. this.inertialRadiusOffset = 0;
  204. previousPosition = null;
  205. pointerId = null;
  206. };
  207. }
  208. element.addEventListener(eventPrefix + "down", this._onPointerDown, false);
  209. element.addEventListener(eventPrefix + "up", this._onPointerUp, false);
  210. element.addEventListener(eventPrefix + "out", this._onPointerUp, false);
  211. element.addEventListener(eventPrefix + "move", this._onPointerMove, false);
  212. element.addEventListener("mousemove", this._onMouseMove, false);
  213. element.addEventListener("MSPointerDown", this._onGestureStart, false);
  214. element.addEventListener("MSGestureChange", this._onGesture, false);
  215. element.addEventListener('mousewheel', this._wheel, false);
  216. element.addEventListener('DOMMouseScroll', this._wheel, false);
  217. Tools.RegisterTopRootEvents([
  218. { name: "keydown", handler: this._onKeyDown },
  219. { name: "keyup", handler: this._onKeyUp },
  220. { name: "blur", handler: this._onLostFocus }
  221. ]);
  222. }
  223. public detachControl(element: HTMLElement): void {
  224. if (this._attachedElement != element) {
  225. return;
  226. }
  227. element.removeEventListener(eventPrefix + "down", this._onPointerDown);
  228. element.removeEventListener(eventPrefix + "up", this._onPointerUp);
  229. element.removeEventListener(eventPrefix + "out", this._onPointerUp);
  230. element.removeEventListener(eventPrefix + "move", this._onPointerMove);
  231. element.removeEventListener("mousemove", this._onMouseMove);
  232. element.removeEventListener("MSPointerDown", this._onGestureStart);
  233. element.removeEventListener("MSGestureChange", this._onGesture);
  234. element.removeEventListener('mousewheel', this._wheel);
  235. element.removeEventListener('DOMMouseScroll', this._wheel);
  236. Tools.UnregisterTopRootEvents([
  237. { name: "keydown", handler: this._onKeyDown },
  238. { name: "keyup", handler: this._onKeyUp },
  239. { name: "blur", handler: this._onLostFocus }
  240. ]);
  241. this._MSGestureHandler = null;
  242. this._attachedElement = null;
  243. if (this._reset) {
  244. this._reset();
  245. }
  246. }
  247. public _update(): void {
  248. // Keyboard
  249. for (var index = 0; index < this._keys.length; index++) {
  250. var keyCode = this._keys[index];
  251. if (this.keysLeft.indexOf(keyCode) !== -1) {
  252. this.inertialAlphaOffset -= 0.01;
  253. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  254. this.inertialBetaOffset -= 0.01;
  255. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  256. this.inertialAlphaOffset += 0.01;
  257. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  258. this.inertialBetaOffset += 0.01;
  259. }
  260. }
  261. // Inertia
  262. if (this.inertialAlphaOffset != 0 || this.inertialBetaOffset != 0 || this.inertialRadiusOffset != 0) {
  263. this.alpha += this.inertialAlphaOffset;
  264. this.beta += this.inertialBetaOffset;
  265. this.radius -= this.inertialRadiusOffset;
  266. this.inertialAlphaOffset *= this.inertia;
  267. this.inertialBetaOffset *= this.inertia;
  268. this.inertialRadiusOffset *= this.inertia;
  269. if (Math.abs(this.inertialAlphaOffset) < BABYLON.Engine.Epsilon)
  270. this.inertialAlphaOffset = 0;
  271. if (Math.abs(this.inertialBetaOffset) < BABYLON.Engine.Epsilon)
  272. this.inertialBetaOffset = 0;
  273. if (Math.abs(this.inertialRadiusOffset) < BABYLON.Engine.Epsilon)
  274. this.inertialRadiusOffset = 0;
  275. }
  276. // Limits
  277. if (this.lowerAlphaLimit && this.alpha < this.lowerAlphaLimit) {
  278. this.alpha = this.lowerAlphaLimit;
  279. }
  280. if (this.upperAlphaLimit && this.alpha > this.upperAlphaLimit) {
  281. this.alpha = this.upperAlphaLimit;
  282. }
  283. if (this.lowerBetaLimit && this.beta < this.lowerBetaLimit) {
  284. this.beta = this.lowerBetaLimit;
  285. }
  286. if (this.upperBetaLimit && this.beta > this.upperBetaLimit) {
  287. this.beta = this.upperBetaLimit;
  288. }
  289. if (this.lowerRadiusLimit && this.radius < this.lowerRadiusLimit) {
  290. this.radius = this.lowerRadiusLimit;
  291. }
  292. if (this.upperRadiusLimit && this.radius > this.upperRadiusLimit) {
  293. this.radius = this.upperRadiusLimit;
  294. }
  295. }
  296. public setPosition(position: Vector3): void {
  297. var radiusv3 = position.subtract(this._getTargetPosition());
  298. this.radius = radiusv3.length();
  299. // Alpha
  300. this.alpha = Math.acos(radiusv3.x / Math.sqrt(Math.pow(radiusv3.x, 2) + Math.pow(radiusv3.z, 2)));
  301. if (radiusv3.z < 0) {
  302. this.alpha = 2 * Math.PI - this.alpha;
  303. }
  304. // Beta
  305. this.beta = Math.acos(radiusv3.y / this.radius);
  306. }
  307. public _getViewMatrix(): Matrix {
  308. // Compute
  309. var cosa = Math.cos(this.alpha);
  310. var sina = Math.sin(this.alpha);
  311. var cosb = Math.cos(this.beta);
  312. var sinb = Math.sin(this.beta);
  313. var target = this._getTargetPosition();
  314. target.addToRef(new BABYLON.Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this.position);
  315. Matrix.LookAtLHToRef(this.position, target, this.upVector, this._viewMatrix);
  316. return this._viewMatrix;
  317. }
  318. public zoomOn(meshes?: AbstractMesh[]): void {
  319. meshes = meshes || this.getScene().meshes;
  320. var minMaxVector = BABYLON.Mesh.MinMax(meshes);
  321. var distance = BABYLON.Vector3.Distance(minMaxVector.min, minMaxVector.max);
  322. this.radius = distance * this.zoomOnFactor;
  323. this.focusOn({ min: minMaxVector.min, max: minMaxVector.max, distance: distance });
  324. }
  325. public focusOn(meshesOrMinMaxVectorAndDistance): void {
  326. var meshesOrMinMaxVector;
  327. var distance;
  328. if (meshesOrMinMaxVectorAndDistance.min === undefined) { // meshes
  329. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance || this.getScene().meshes;
  330. meshesOrMinMaxVector = BABYLON.Mesh.MinMax(meshesOrMinMaxVector);
  331. distance = BABYLON.Vector3.Distance(meshesOrMinMaxVector.min, meshesOrMinMaxVector.max);
  332. }
  333. else { //minMaxVector and distance
  334. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance;
  335. distance = meshesOrMinMaxVectorAndDistance.distance;
  336. }
  337. this.target = Mesh.Center(meshesOrMinMaxVector);
  338. this.maxZ = distance * 2;
  339. }
  340. }
  341. }