babylon.arcRotateCamera.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  7. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  8. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  9. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  10. return c > 3 && r && Object.defineProperty(target, key, r), r;
  11. };
  12. var BABYLON;
  13. (function (BABYLON) {
  14. var eventPrefix = BABYLON.Tools.GetPointerPrefix();
  15. var ArcRotateCamera = (function (_super) {
  16. __extends(ArcRotateCamera, _super);
  17. function ArcRotateCamera(name, alpha, beta, radius, target, scene) {
  18. var _this = this;
  19. _super.call(this, name, BABYLON.Vector3.Zero(), scene);
  20. this.inertialAlphaOffset = 0;
  21. this.inertialBetaOffset = 0;
  22. this.inertialRadiusOffset = 0;
  23. this.lowerAlphaLimit = null;
  24. this.upperAlphaLimit = null;
  25. this.lowerBetaLimit = 0.01;
  26. this.upperBetaLimit = Math.PI;
  27. this.lowerRadiusLimit = null;
  28. this.upperRadiusLimit = null;
  29. this.angularSensibilityX = 1000.0;
  30. this.angularSensibilityY = 1000.0;
  31. this.wheelPrecision = 3.0;
  32. this.pinchPrecision = 2.0;
  33. this.panningSensibility = 50.0;
  34. this.inertialPanningX = 0;
  35. this.inertialPanningY = 0;
  36. this.keysUp = [38];
  37. this.keysDown = [40];
  38. this.keysLeft = [37];
  39. this.keysRight = [39];
  40. this.zoomOnFactor = 1;
  41. this.targetScreenOffset = BABYLON.Vector2.Zero();
  42. this.pinchInwards = true;
  43. this.allowUpsideDown = true;
  44. this._keys = [];
  45. this._viewMatrix = new BABYLON.Matrix();
  46. // Panning
  47. this.panningAxis = new BABYLON.Vector3(1, 1, 0);
  48. this._isRightClick = false;
  49. this._isCtrlPushed = false;
  50. this.checkCollisions = false;
  51. this.collisionRadius = new BABYLON.Vector3(0.5, 0.5, 0.5);
  52. this._collider = new BABYLON.Collider();
  53. this._previousPosition = BABYLON.Vector3.Zero();
  54. this._collisionVelocity = BABYLON.Vector3.Zero();
  55. this._newPosition = BABYLON.Vector3.Zero();
  56. this._onCollisionPositionChange = function (collisionId, newPosition, collidedMesh) {
  57. if (collidedMesh === void 0) { collidedMesh = null; }
  58. if (_this.getScene().workerCollisions && _this.checkCollisions) {
  59. newPosition.multiplyInPlace(_this._collider.radius);
  60. }
  61. if (!collidedMesh) {
  62. _this._previousPosition.copyFrom(_this.position);
  63. }
  64. else {
  65. _this.setPosition(newPosition);
  66. if (_this.onCollide) {
  67. _this.onCollide(collidedMesh);
  68. }
  69. }
  70. // Recompute because of constraints
  71. var cosa = Math.cos(_this.alpha);
  72. var sina = Math.sin(_this.alpha);
  73. var cosb = Math.cos(_this.beta);
  74. var sinb = Math.sin(_this.beta);
  75. if (sinb === 0) {
  76. sinb = 0.0001;
  77. }
  78. var target = _this.target;
  79. target.addToRef(new BABYLON.Vector3(_this.radius * cosa * sinb, _this.radius * cosb, _this.radius * sina * sinb), _this._newPosition);
  80. _this.position.copyFrom(_this._newPosition);
  81. var up = _this.upVector;
  82. if (_this.allowUpsideDown && _this.beta < 0) {
  83. up = up.clone();
  84. up = up.negate();
  85. }
  86. BABYLON.Matrix.LookAtLHToRef(_this.position, target, up, _this._viewMatrix);
  87. _this._viewMatrix.m[12] += _this.targetScreenOffset.x;
  88. _this._viewMatrix.m[13] += _this.targetScreenOffset.y;
  89. _this._collisionTriggered = false;
  90. };
  91. if (!this.target) {
  92. this.target = BABYLON.Vector3.Zero();
  93. }
  94. else {
  95. this.target = target;
  96. }
  97. this.alpha = alpha;
  98. this.beta = beta;
  99. this.radius = radius;
  100. this.getViewMatrix();
  101. }
  102. Object.defineProperty(ArcRotateCamera.prototype, "angularSensibility", {
  103. //deprecated angularSensibility support
  104. get: function () {
  105. BABYLON.Tools.Warn("Warning: angularSensibility is deprecated, use angularSensibilityX and angularSensibilityY instead.");
  106. return Math.max(this.angularSensibilityX, this.angularSensibilityY);
  107. },
  108. //deprecated angularSensibility support
  109. set: function (value) {
  110. BABYLON.Tools.Warn("Warning: angularSensibility is deprecated, use angularSensibilityX and angularSensibilityY instead.");
  111. this.angularSensibilityX = value;
  112. this.angularSensibilityY = value;
  113. },
  114. enumerable: true,
  115. configurable: true
  116. });
  117. // Cache
  118. ArcRotateCamera.prototype._initCache = function () {
  119. _super.prototype._initCache.call(this);
  120. this._cache.target = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  121. this._cache.alpha = undefined;
  122. this._cache.beta = undefined;
  123. this._cache.radius = undefined;
  124. this._cache.targetScreenOffset = BABYLON.Vector2.Zero();
  125. };
  126. ArcRotateCamera.prototype._updateCache = function (ignoreParentClass) {
  127. if (!ignoreParentClass) {
  128. _super.prototype._updateCache.call(this);
  129. }
  130. this._cache.target.copyFrom(this.target);
  131. this._cache.alpha = this.alpha;
  132. this._cache.beta = this.beta;
  133. this._cache.radius = this.radius;
  134. this._cache.targetScreenOffset.copyFrom(this.targetScreenOffset);
  135. };
  136. // Synchronized
  137. ArcRotateCamera.prototype._isSynchronizedViewMatrix = function () {
  138. if (!_super.prototype._isSynchronizedViewMatrix.call(this))
  139. return false;
  140. return this._cache.target.equals(this.target)
  141. && this._cache.alpha === this.alpha
  142. && this._cache.beta === this.beta
  143. && this._cache.radius === this.radius
  144. && this._cache.targetScreenOffset.equals(this.targetScreenOffset);
  145. };
  146. // Methods
  147. ArcRotateCamera.prototype.attachControl = function (element, noPreventDefault, useCtrlForPanning) {
  148. var _this = this;
  149. if (useCtrlForPanning === void 0) { useCtrlForPanning = true; }
  150. var cacheSoloPointer; // cache pointer object for better perf on camera rotation
  151. var previousPinchDistance = 0;
  152. var pointers = new BABYLON.SmartCollection();
  153. if (this._attachedElement) {
  154. return;
  155. }
  156. this._attachedElement = element;
  157. var engine = this.getEngine();
  158. if (this._onPointerDown === undefined) {
  159. this._onPointerDown = function (evt) {
  160. // Manage panning with right click
  161. _this._isRightClick = evt.button === 2;
  162. // manage pointers
  163. pointers.add(evt.pointerId, { x: evt.clientX, y: evt.clientY, type: evt.pointerType });
  164. cacheSoloPointer = pointers.item(evt.pointerId);
  165. if (!noPreventDefault) {
  166. evt.preventDefault();
  167. }
  168. };
  169. this._onPointerUp = function (evt) {
  170. cacheSoloPointer = null;
  171. previousPinchDistance = 0;
  172. //would be better to use pointers.remove(evt.pointerId) for multitouch gestures,
  173. //but emptying completly pointers collection is required to fix a bug on iPhone :
  174. //when changing orientation while pinching camera, one pointer stay pressed forever if we don't release all pointers
  175. //will be ok to put back pointers.remove(evt.pointerId); when iPhone bug corrected
  176. pointers.empty();
  177. if (!noPreventDefault) {
  178. evt.preventDefault();
  179. }
  180. };
  181. this._onContextMenu = function (evt) {
  182. evt.preventDefault();
  183. };
  184. this._onPointerMove = function (evt) {
  185. if (!noPreventDefault) {
  186. evt.preventDefault();
  187. }
  188. switch (pointers.count) {
  189. case 1:
  190. if (_this.panningSensibility !== 0 && ((_this._isCtrlPushed && useCtrlForPanning) || (!useCtrlForPanning && _this._isRightClick))) {
  191. _this.inertialPanningX += -(evt.clientX - cacheSoloPointer.x) / _this.panningSensibility;
  192. _this.inertialPanningY += (evt.clientY - cacheSoloPointer.y) / _this.panningSensibility;
  193. }
  194. else {
  195. var offsetX = evt.clientX - cacheSoloPointer.x;
  196. var offsetY = evt.clientY - cacheSoloPointer.y;
  197. _this.inertialAlphaOffset -= offsetX / _this.angularSensibilityX;
  198. _this.inertialBetaOffset -= offsetY / _this.angularSensibilityY;
  199. }
  200. cacheSoloPointer.x = evt.clientX;
  201. cacheSoloPointer.y = evt.clientY;
  202. break;
  203. case 2:
  204. //if (noPreventDefault) { evt.preventDefault(); } //if pinch gesture, could be usefull to force preventDefault to avoid html page scroll/zoom in some mobile browsers
  205. pointers.item(evt.pointerId).x = evt.clientX;
  206. pointers.item(evt.pointerId).y = evt.clientY;
  207. var direction = _this.pinchInwards ? 1 : -1;
  208. var distX = pointers.getItemByIndex(0).x - pointers.getItemByIndex(1).x;
  209. var distY = pointers.getItemByIndex(0).y - pointers.getItemByIndex(1).y;
  210. var pinchSquaredDistance = (distX * distX) + (distY * distY);
  211. if (previousPinchDistance === 0) {
  212. previousPinchDistance = pinchSquaredDistance;
  213. return;
  214. }
  215. if (pinchSquaredDistance !== previousPinchDistance) {
  216. _this.inertialRadiusOffset += (pinchSquaredDistance - previousPinchDistance) / (_this.pinchPrecision * _this.wheelPrecision * ((_this.angularSensibilityX + _this.angularSensibilityY) / 2) * direction);
  217. previousPinchDistance = pinchSquaredDistance;
  218. }
  219. break;
  220. default:
  221. if (pointers.item(evt.pointerId)) {
  222. pointers.item(evt.pointerId).x = evt.clientX;
  223. pointers.item(evt.pointerId).y = evt.clientY;
  224. }
  225. }
  226. };
  227. this._onMouseMove = function (evt) {
  228. if (!engine.isPointerLock) {
  229. return;
  230. }
  231. var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  232. var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  233. _this.inertialAlphaOffset -= offsetX / _this.angularSensibilityX;
  234. _this.inertialBetaOffset -= offsetY / _this.angularSensibilityY;
  235. if (!noPreventDefault) {
  236. evt.preventDefault();
  237. }
  238. };
  239. this._wheel = function (event) {
  240. var delta = 0;
  241. if (event.wheelDelta) {
  242. delta = event.wheelDelta / (_this.wheelPrecision * 40);
  243. }
  244. else if (event.detail) {
  245. delta = -event.detail / _this.wheelPrecision;
  246. }
  247. if (delta)
  248. _this.inertialRadiusOffset += delta;
  249. if (event.preventDefault) {
  250. if (!noPreventDefault) {
  251. event.preventDefault();
  252. }
  253. }
  254. };
  255. this._onKeyDown = function (evt) {
  256. _this._isCtrlPushed = evt.ctrlKey;
  257. if (_this.keysUp.indexOf(evt.keyCode) !== -1 ||
  258. _this.keysDown.indexOf(evt.keyCode) !== -1 ||
  259. _this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  260. _this.keysRight.indexOf(evt.keyCode) !== -1) {
  261. var index = _this._keys.indexOf(evt.keyCode);
  262. if (index === -1) {
  263. _this._keys.push(evt.keyCode);
  264. }
  265. if (evt.preventDefault) {
  266. if (!noPreventDefault) {
  267. evt.preventDefault();
  268. }
  269. }
  270. }
  271. };
  272. this._onKeyUp = function (evt) {
  273. _this._isCtrlPushed = evt.ctrlKey;
  274. if (_this.keysUp.indexOf(evt.keyCode) !== -1 ||
  275. _this.keysDown.indexOf(evt.keyCode) !== -1 ||
  276. _this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  277. _this.keysRight.indexOf(evt.keyCode) !== -1) {
  278. var index = _this._keys.indexOf(evt.keyCode);
  279. if (index >= 0) {
  280. _this._keys.splice(index, 1);
  281. }
  282. if (evt.preventDefault) {
  283. if (!noPreventDefault) {
  284. evt.preventDefault();
  285. }
  286. }
  287. }
  288. };
  289. this._onLostFocus = function () {
  290. _this._keys = [];
  291. pointers.empty();
  292. previousPinchDistance = 0;
  293. cacheSoloPointer = null;
  294. };
  295. this._onGestureStart = function (e) {
  296. if (window.MSGesture === undefined) {
  297. return;
  298. }
  299. if (!_this._MSGestureHandler) {
  300. _this._MSGestureHandler = new MSGesture();
  301. _this._MSGestureHandler.target = element;
  302. }
  303. _this._MSGestureHandler.addPointer(e.pointerId);
  304. };
  305. this._onGesture = function (e) {
  306. _this.radius *= e.scale;
  307. if (e.preventDefault) {
  308. if (!noPreventDefault) {
  309. e.stopPropagation();
  310. e.preventDefault();
  311. }
  312. }
  313. };
  314. this._reset = function () {
  315. _this._keys = [];
  316. _this.inertialAlphaOffset = 0;
  317. _this.inertialBetaOffset = 0;
  318. _this.inertialRadiusOffset = 0;
  319. pointers.empty();
  320. previousPinchDistance = 0;
  321. cacheSoloPointer = null;
  322. };
  323. }
  324. if (!useCtrlForPanning) {
  325. element.addEventListener("contextmenu", this._onContextMenu, false);
  326. }
  327. element.addEventListener(eventPrefix + "down", this._onPointerDown, false);
  328. element.addEventListener(eventPrefix + "up", this._onPointerUp, false);
  329. element.addEventListener(eventPrefix + "out", this._onPointerUp, false);
  330. element.addEventListener(eventPrefix + "move", this._onPointerMove, false);
  331. element.addEventListener("mousemove", this._onMouseMove, false);
  332. element.addEventListener("MSPointerDown", this._onGestureStart, false);
  333. element.addEventListener("MSGestureChange", this._onGesture, false);
  334. element.addEventListener('mousewheel', this._wheel, false);
  335. element.addEventListener('DOMMouseScroll', this._wheel, false);
  336. BABYLON.Tools.RegisterTopRootEvents([
  337. { name: "keydown", handler: this._onKeyDown },
  338. { name: "keyup", handler: this._onKeyUp },
  339. { name: "blur", handler: this._onLostFocus }
  340. ]);
  341. };
  342. ArcRotateCamera.prototype.detachControl = function (element) {
  343. if (this._attachedElement !== element) {
  344. return;
  345. }
  346. element.removeEventListener("contextmenu", this._onContextMenu);
  347. element.removeEventListener(eventPrefix + "down", this._onPointerDown);
  348. element.removeEventListener(eventPrefix + "up", this._onPointerUp);
  349. element.removeEventListener(eventPrefix + "out", this._onPointerUp);
  350. element.removeEventListener(eventPrefix + "move", this._onPointerMove);
  351. element.removeEventListener("mousemove", this._onMouseMove);
  352. element.removeEventListener("MSPointerDown", this._onGestureStart);
  353. element.removeEventListener("MSGestureChange", this._onGesture);
  354. element.removeEventListener('mousewheel', this._wheel);
  355. element.removeEventListener('DOMMouseScroll', this._wheel);
  356. BABYLON.Tools.UnregisterTopRootEvents([
  357. { name: "keydown", handler: this._onKeyDown },
  358. { name: "keyup", handler: this._onKeyUp },
  359. { name: "blur", handler: this._onLostFocus }
  360. ]);
  361. this._MSGestureHandler = null;
  362. this._attachedElement = null;
  363. if (this._reset) {
  364. this._reset();
  365. }
  366. };
  367. ArcRotateCamera.prototype._checkInputs = function () {
  368. //if (async) collision inspection was triggered, don't update the camera's position - until the collision callback was called.
  369. if (this._collisionTriggered) {
  370. return;
  371. }
  372. // Keyboard
  373. for (var index = 0; index < this._keys.length; index++) {
  374. var keyCode = this._keys[index];
  375. if (this.keysLeft.indexOf(keyCode) !== -1) {
  376. this.inertialAlphaOffset -= 0.01;
  377. }
  378. else if (this.keysUp.indexOf(keyCode) !== -1) {
  379. this.inertialBetaOffset -= 0.01;
  380. }
  381. else if (this.keysRight.indexOf(keyCode) !== -1) {
  382. this.inertialAlphaOffset += 0.01;
  383. }
  384. else if (this.keysDown.indexOf(keyCode) !== -1) {
  385. this.inertialBetaOffset += 0.01;
  386. }
  387. }
  388. // Inertia
  389. if (this.inertialAlphaOffset !== 0 || this.inertialBetaOffset !== 0 || this.inertialRadiusOffset !== 0) {
  390. this.alpha += this.beta <= 0 ? -this.inertialAlphaOffset : this.inertialAlphaOffset;
  391. this.beta += this.inertialBetaOffset;
  392. this.radius -= this.inertialRadiusOffset;
  393. this.inertialAlphaOffset *= this.inertia;
  394. this.inertialBetaOffset *= this.inertia;
  395. this.inertialRadiusOffset *= this.inertia;
  396. if (Math.abs(this.inertialAlphaOffset) < BABYLON.Engine.Epsilon)
  397. this.inertialAlphaOffset = 0;
  398. if (Math.abs(this.inertialBetaOffset) < BABYLON.Engine.Epsilon)
  399. this.inertialBetaOffset = 0;
  400. if (Math.abs(this.inertialRadiusOffset) < BABYLON.Engine.Epsilon)
  401. this.inertialRadiusOffset = 0;
  402. }
  403. // Panning inertia
  404. if (this.inertialPanningX !== 0 || this.inertialPanningY !== 0) {
  405. if (!this._localDirection) {
  406. this._localDirection = BABYLON.Vector3.Zero();
  407. this._transformedDirection = BABYLON.Vector3.Zero();
  408. }
  409. this.inertialPanningX *= this.inertia;
  410. this.inertialPanningY *= this.inertia;
  411. if (Math.abs(this.inertialPanningX) < BABYLON.Engine.Epsilon)
  412. this.inertialPanningX = 0;
  413. if (Math.abs(this.inertialPanningY) < BABYLON.Engine.Epsilon)
  414. this.inertialPanningY = 0;
  415. this._localDirection.copyFromFloats(this.inertialPanningX, this.inertialPanningY, this.inertialPanningY);
  416. this._localDirection.multiplyInPlace(this.panningAxis);
  417. this._viewMatrix.invertToRef(this._cameraTransformMatrix);
  418. BABYLON.Vector3.TransformNormalToRef(this._localDirection, this._cameraTransformMatrix, this._transformedDirection);
  419. //Eliminate y if map panning is enabled (panningAxis == 1,0,1)
  420. if (!this.panningAxis.y) {
  421. this._transformedDirection.y = 0;
  422. }
  423. this.target.addInPlace(this._transformedDirection);
  424. }
  425. // Limits
  426. this._checkLimits();
  427. _super.prototype._checkInputs.call(this);
  428. };
  429. ArcRotateCamera.prototype._checkLimits = function () {
  430. if (this.lowerBetaLimit === null || this.lowerBetaLimit === undefined) {
  431. if (this.allowUpsideDown && this.beta > Math.PI) {
  432. this.beta = this.beta - (2 * Math.PI);
  433. }
  434. }
  435. else {
  436. if (this.beta < this.lowerBetaLimit) {
  437. this.beta = this.lowerBetaLimit;
  438. }
  439. }
  440. if (this.upperBetaLimit === null || this.upperBetaLimit === undefined) {
  441. if (this.allowUpsideDown && this.beta < -Math.PI) {
  442. this.beta = this.beta + (2 * Math.PI);
  443. }
  444. }
  445. else {
  446. if (this.beta > this.upperBetaLimit) {
  447. this.beta = this.upperBetaLimit;
  448. }
  449. }
  450. if (this.lowerAlphaLimit && this.alpha < this.lowerAlphaLimit) {
  451. this.alpha = this.lowerAlphaLimit;
  452. }
  453. if (this.upperAlphaLimit && this.alpha > this.upperAlphaLimit) {
  454. this.alpha = this.upperAlphaLimit;
  455. }
  456. if (this.lowerRadiusLimit && this.radius < this.lowerRadiusLimit) {
  457. this.radius = this.lowerRadiusLimit;
  458. }
  459. if (this.upperRadiusLimit && this.radius > this.upperRadiusLimit) {
  460. this.radius = this.upperRadiusLimit;
  461. }
  462. };
  463. ArcRotateCamera.prototype.rebuildAnglesAndRadius = function () {
  464. var radiusv3 = this.position.subtract(this.target);
  465. this.radius = radiusv3.length();
  466. // Alpha
  467. this.alpha = Math.acos(radiusv3.x / Math.sqrt(Math.pow(radiusv3.x, 2) + Math.pow(radiusv3.z, 2)));
  468. if (radiusv3.z < 0) {
  469. this.alpha = 2 * Math.PI - this.alpha;
  470. }
  471. // Beta
  472. this.beta = Math.acos(radiusv3.y / this.radius);
  473. this._checkLimits();
  474. };
  475. ArcRotateCamera.prototype.setPosition = function (position) {
  476. if (this.position.equals(position)) {
  477. return;
  478. }
  479. this.position = position;
  480. this.rebuildAnglesAndRadius();
  481. };
  482. ArcRotateCamera.prototype.setTarget = function (target) {
  483. if (this.target.equals(target)) {
  484. return;
  485. }
  486. this.target = target;
  487. this.rebuildAnglesAndRadius();
  488. };
  489. ArcRotateCamera.prototype._getViewMatrix = function () {
  490. // Compute
  491. var cosa = Math.cos(this.alpha);
  492. var sina = Math.sin(this.alpha);
  493. var cosb = Math.cos(this.beta);
  494. var sinb = Math.sin(this.beta);
  495. if (sinb === 0) {
  496. sinb = 0.0001;
  497. }
  498. var target = this.target;
  499. target.addToRef(new BABYLON.Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this._newPosition);
  500. if (this.getScene().collisionsEnabled && this.checkCollisions) {
  501. this._collider.radius = this.collisionRadius;
  502. this._newPosition.subtractToRef(this.position, this._collisionVelocity);
  503. this._collisionTriggered = true;
  504. this.getScene().collisionCoordinator.getNewPosition(this.position, this._collisionVelocity, this._collider, 3, null, this._onCollisionPositionChange, this.uniqueId);
  505. }
  506. else {
  507. this.position.copyFrom(this._newPosition);
  508. var up = this.upVector;
  509. if (this.allowUpsideDown && this.beta < 0) {
  510. up = up.clone();
  511. up = up.negate();
  512. }
  513. BABYLON.Matrix.LookAtLHToRef(this.position, target, up, this._viewMatrix);
  514. this._viewMatrix.m[12] += this.targetScreenOffset.x;
  515. this._viewMatrix.m[13] += this.targetScreenOffset.y;
  516. }
  517. return this._viewMatrix;
  518. };
  519. ArcRotateCamera.prototype.zoomOn = function (meshes, doNotUpdateMaxZ) {
  520. if (doNotUpdateMaxZ === void 0) { doNotUpdateMaxZ = false; }
  521. meshes = meshes || this.getScene().meshes;
  522. var minMaxVector = BABYLON.Mesh.MinMax(meshes);
  523. var distance = BABYLON.Vector3.Distance(minMaxVector.min, minMaxVector.max);
  524. this.radius = distance * this.zoomOnFactor;
  525. this.focusOn({ min: minMaxVector.min, max: minMaxVector.max, distance: distance }, doNotUpdateMaxZ);
  526. };
  527. ArcRotateCamera.prototype.focusOn = function (meshesOrMinMaxVectorAndDistance, doNotUpdateMaxZ) {
  528. if (doNotUpdateMaxZ === void 0) { doNotUpdateMaxZ = false; }
  529. var meshesOrMinMaxVector;
  530. var distance;
  531. if (meshesOrMinMaxVectorAndDistance.min === undefined) {
  532. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance || this.getScene().meshes;
  533. meshesOrMinMaxVector = BABYLON.Mesh.MinMax(meshesOrMinMaxVector);
  534. distance = BABYLON.Vector3.Distance(meshesOrMinMaxVector.min, meshesOrMinMaxVector.max);
  535. }
  536. else {
  537. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance;
  538. distance = meshesOrMinMaxVectorAndDistance.distance;
  539. }
  540. this.target = BABYLON.Mesh.Center(meshesOrMinMaxVector);
  541. if (!doNotUpdateMaxZ) {
  542. this.maxZ = distance * 2;
  543. }
  544. };
  545. /**
  546. * @override
  547. * Override Camera.createRigCamera
  548. */
  549. ArcRotateCamera.prototype.createRigCamera = function (name, cameraIndex) {
  550. switch (this.cameraRigMode) {
  551. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  552. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  553. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  554. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  555. case BABYLON.Camera.RIG_MODE_VR:
  556. var alphaShift = this._cameraRigParams.stereoHalfAngle * (cameraIndex === 0 ? 1 : -1);
  557. return new ArcRotateCamera(name, this.alpha + alphaShift, this.beta, this.radius, this.target, this.getScene());
  558. }
  559. return null;
  560. };
  561. /**
  562. * @override
  563. * Override Camera._updateRigCameras
  564. */
  565. ArcRotateCamera.prototype._updateRigCameras = function () {
  566. switch (this.cameraRigMode) {
  567. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  568. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  569. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  570. case BABYLON.Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  571. case BABYLON.Camera.RIG_MODE_VR:
  572. var camLeft = this._rigCameras[0];
  573. var camRight = this._rigCameras[1];
  574. camLeft.alpha = this.alpha - this._cameraRigParams.stereoHalfAngle;
  575. camRight.alpha = this.alpha + this._cameraRigParams.stereoHalfAngle;
  576. camLeft.beta = camRight.beta = this.beta;
  577. camLeft.radius = camRight.radius = this.radius;
  578. break;
  579. }
  580. _super.prototype._updateRigCameras.call(this);
  581. };
  582. ArcRotateCamera.prototype.getTypeName = function () {
  583. return "ArcRotateCamera";
  584. };
  585. __decorate([
  586. BABYLON.serialize()
  587. ], ArcRotateCamera.prototype, "alpha", void 0);
  588. __decorate([
  589. BABYLON.serialize()
  590. ], ArcRotateCamera.prototype, "beta", void 0);
  591. __decorate([
  592. BABYLON.serialize()
  593. ], ArcRotateCamera.prototype, "radius", void 0);
  594. __decorate([
  595. BABYLON.serializeAsVector3()
  596. ], ArcRotateCamera.prototype, "target", void 0);
  597. __decorate([
  598. BABYLON.serialize()
  599. ], ArcRotateCamera.prototype, "inertialAlphaOffset", void 0);
  600. __decorate([
  601. BABYLON.serialize()
  602. ], ArcRotateCamera.prototype, "inertialBetaOffset", void 0);
  603. __decorate([
  604. BABYLON.serialize()
  605. ], ArcRotateCamera.prototype, "inertialRadiusOffset", void 0);
  606. __decorate([
  607. BABYLON.serialize()
  608. ], ArcRotateCamera.prototype, "lowerAlphaLimit", void 0);
  609. __decorate([
  610. BABYLON.serialize()
  611. ], ArcRotateCamera.prototype, "upperAlphaLimit", void 0);
  612. __decorate([
  613. BABYLON.serialize()
  614. ], ArcRotateCamera.prototype, "lowerBetaLimit", void 0);
  615. __decorate([
  616. BABYLON.serialize()
  617. ], ArcRotateCamera.prototype, "upperBetaLimit", void 0);
  618. __decorate([
  619. BABYLON.serialize()
  620. ], ArcRotateCamera.prototype, "lowerRadiusLimit", void 0);
  621. __decorate([
  622. BABYLON.serialize()
  623. ], ArcRotateCamera.prototype, "upperRadiusLimit", void 0);
  624. __decorate([
  625. BABYLON.serialize()
  626. ], ArcRotateCamera.prototype, "angularSensibilityX", void 0);
  627. __decorate([
  628. BABYLON.serialize()
  629. ], ArcRotateCamera.prototype, "angularSensibilityY", void 0);
  630. __decorate([
  631. BABYLON.serialize()
  632. ], ArcRotateCamera.prototype, "wheelPrecision", void 0);
  633. __decorate([
  634. BABYLON.serialize()
  635. ], ArcRotateCamera.prototype, "pinchPrecision", void 0);
  636. __decorate([
  637. BABYLON.serialize()
  638. ], ArcRotateCamera.prototype, "panningSensibility", void 0);
  639. __decorate([
  640. BABYLON.serialize()
  641. ], ArcRotateCamera.prototype, "inertialPanningX", void 0);
  642. __decorate([
  643. BABYLON.serialize()
  644. ], ArcRotateCamera.prototype, "inertialPanningY", void 0);
  645. __decorate([
  646. BABYLON.serialize()
  647. ], ArcRotateCamera.prototype, "keysUp", void 0);
  648. __decorate([
  649. BABYLON.serialize()
  650. ], ArcRotateCamera.prototype, "keysDown", void 0);
  651. __decorate([
  652. BABYLON.serialize()
  653. ], ArcRotateCamera.prototype, "keysLeft", void 0);
  654. __decorate([
  655. BABYLON.serialize()
  656. ], ArcRotateCamera.prototype, "keysRight", void 0);
  657. __decorate([
  658. BABYLON.serialize()
  659. ], ArcRotateCamera.prototype, "zoomOnFactor", void 0);
  660. __decorate([
  661. BABYLON.serialize()
  662. ], ArcRotateCamera.prototype, "allowUpsideDown", void 0);
  663. return ArcRotateCamera;
  664. })(BABYLON.TargetCamera);
  665. BABYLON.ArcRotateCamera = ArcRotateCamera;
  666. })(BABYLON || (BABYLON = {}));