babylon.arcRotateCamera.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. var __extends = 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. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var eventPrefix = BABYLON.Tools.GetPointerPrefix();
  10. var ArcRotateCamera = (function (_super) {
  11. __extends(ArcRotateCamera, _super);
  12. function ArcRotateCamera(name, alpha, beta, radius, target, scene) {
  13. var _this = this;
  14. _super.call(this, name, BABYLON.Vector3.Zero(), scene);
  15. this.alpha = alpha;
  16. this.beta = beta;
  17. this.radius = radius;
  18. this.target = target;
  19. this.inertialAlphaOffset = 0;
  20. this.inertialBetaOffset = 0;
  21. this.inertialRadiusOffset = 0;
  22. this.lowerAlphaLimit = null;
  23. this.upperAlphaLimit = null;
  24. this.lowerBetaLimit = 0.01;
  25. this.upperBetaLimit = Math.PI;
  26. this.lowerRadiusLimit = null;
  27. this.upperRadiusLimit = null;
  28. this.angularSensibility = 1000.0;
  29. this.wheelPrecision = 3.0;
  30. this.pinchPrecision = 2.0;
  31. this.keysUp = [38];
  32. this.keysDown = [40];
  33. this.keysLeft = [37];
  34. this.keysRight = [39];
  35. this.zoomOnFactor = 1;
  36. this.targetScreenOffset = BABYLON.Vector2.Zero();
  37. this.pinchInwards = true;
  38. this._keys = [];
  39. this._viewMatrix = new BABYLON.Matrix();
  40. this.checkCollisions = false;
  41. this.collisionRadius = new BABYLON.Vector3(0.5, 0.5, 0.5);
  42. this._collider = new BABYLON.Collider();
  43. this._previousPosition = BABYLON.Vector3.Zero();
  44. this._collisionVelocity = BABYLON.Vector3.Zero();
  45. this._newPosition = BABYLON.Vector3.Zero();
  46. this._onCollisionPositionChange = function (collisionId, newPosition, collidedMesh) {
  47. if (collidedMesh === void 0) { collidedMesh = null; }
  48. if (_this.getScene().workerCollisions && _this.checkCollisions) {
  49. newPosition.multiplyInPlace(_this._collider.radius);
  50. }
  51. if (!collidedMesh) {
  52. _this._previousPosition.copyFrom(_this.position);
  53. _this.setPosition(_this._newPosition);
  54. _this.position.copyFrom(_this._newPosition);
  55. }
  56. else {
  57. _this.setPosition(_this._previousPosition);
  58. _this.position.copyFrom(_this._previousPosition);
  59. if (_this.onCollide) {
  60. _this.onCollide(collidedMesh);
  61. }
  62. }
  63. BABYLON.Matrix.LookAtLHToRef(_this.position, _this._getTargetPosition(), _this.upVector, _this._viewMatrix);
  64. _this._viewMatrix.m[12] += _this.targetScreenOffset.x;
  65. _this._viewMatrix.m[13] += _this.targetScreenOffset.y;
  66. _this._collisionTriggered = false;
  67. };
  68. if (!this.target) {
  69. this.target = BABYLON.Vector3.Zero();
  70. }
  71. this.getViewMatrix();
  72. }
  73. ArcRotateCamera.prototype._getTargetPosition = function () {
  74. return this.target.position || this.target;
  75. };
  76. // Cache
  77. ArcRotateCamera.prototype._initCache = function () {
  78. _super.prototype._initCache.call(this);
  79. this._cache.target = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  80. this._cache.alpha = undefined;
  81. this._cache.beta = undefined;
  82. this._cache.radius = undefined;
  83. this._cache.targetScreenOffset = undefined;
  84. };
  85. ArcRotateCamera.prototype._updateCache = function (ignoreParentClass) {
  86. if (!ignoreParentClass) {
  87. _super.prototype._updateCache.call(this);
  88. }
  89. this._cache.target.copyFrom(this._getTargetPosition());
  90. this._cache.alpha = this.alpha;
  91. this._cache.beta = this.beta;
  92. this._cache.radius = this.radius;
  93. this._cache.targetScreenOffset = this.targetScreenOffset.clone();
  94. };
  95. // Synchronized
  96. ArcRotateCamera.prototype._isSynchronizedViewMatrix = function () {
  97. if (!_super.prototype._isSynchronizedViewMatrix.call(this))
  98. return false;
  99. return this._cache.target.equals(this._getTargetPosition())
  100. && this._cache.alpha === this.alpha
  101. && this._cache.beta === this.beta
  102. && this._cache.radius === this.radius
  103. && this._cache.targetScreenOffset.equals(this.targetScreenOffset);
  104. };
  105. // Methods
  106. ArcRotateCamera.prototype.attachControl = function (element, noPreventDefault) {
  107. var _this = this;
  108. var cacheSoloPointer; // cache pointer object for better perf on camera rotation
  109. var previousPinchDistance = 0;
  110. var pointers = new BABYLON.SmartCollection();
  111. if (this._attachedElement) {
  112. return;
  113. }
  114. this._attachedElement = element;
  115. var engine = this.getEngine();
  116. if (this._onPointerDown === undefined) {
  117. this._onPointerDown = function (evt) {
  118. pointers.add(evt.pointerId, { x: evt.clientX, y: evt.clientY, type: evt.pointerType });
  119. cacheSoloPointer = pointers.item(evt.pointerId);
  120. if (!noPreventDefault) {
  121. evt.preventDefault();
  122. }
  123. };
  124. this._onPointerUp = function (evt) {
  125. cacheSoloPointer = null;
  126. previousPinchDistance = 0;
  127. pointers.remove(evt.pointerId);
  128. if (!noPreventDefault) {
  129. evt.preventDefault();
  130. }
  131. };
  132. this._onPointerMove = function (evt) {
  133. if (!noPreventDefault) {
  134. evt.preventDefault();
  135. }
  136. switch (pointers.count) {
  137. case 1:
  138. var offsetX = evt.clientX - cacheSoloPointer.x;
  139. var offsetY = evt.clientY - cacheSoloPointer.y;
  140. _this.inertialAlphaOffset -= offsetX / _this.angularSensibility;
  141. _this.inertialBetaOffset -= offsetY / _this.angularSensibility;
  142. cacheSoloPointer.x = evt.clientX;
  143. cacheSoloPointer.y = evt.clientY;
  144. break;
  145. case 2:
  146. //if (noPreventDefault) { evt.preventDefault(); } //if pinch gesture, could be usefull to force preventDefault to avoid html page scroll/zoom in some mobile browsers
  147. pointers.item(evt.pointerId).x = evt.clientX;
  148. pointers.item(evt.pointerId).y = evt.clientY;
  149. var direction = _this.pinchInwards ? 1 : -1;
  150. var distX = pointers.getItemByIndex(0).x - pointers.getItemByIndex(1).x;
  151. var distY = pointers.getItemByIndex(0).y - pointers.getItemByIndex(1).y;
  152. var pinchSquaredDistance = (distX * distX) + (distY * distY);
  153. if (previousPinchDistance === 0) {
  154. previousPinchDistance = pinchSquaredDistance;
  155. return;
  156. }
  157. if (pinchSquaredDistance !== previousPinchDistance) {
  158. _this.inertialRadiusOffset += (pinchSquaredDistance - previousPinchDistance) / (_this.pinchPrecision * _this.wheelPrecision * _this.angularSensibility * direction);
  159. previousPinchDistance = pinchSquaredDistance;
  160. }
  161. break;
  162. default:
  163. if (pointers.item(evt.pointerId)) {
  164. pointers.item(evt.pointerId).x = evt.clientX;
  165. pointers.item(evt.pointerId).y = evt.clientY;
  166. }
  167. }
  168. };
  169. this._onMouseMove = function (evt) {
  170. if (!engine.isPointerLock) {
  171. return;
  172. }
  173. var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  174. var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  175. _this.inertialAlphaOffset -= offsetX / _this.angularSensibility;
  176. _this.inertialBetaOffset -= offsetY / _this.angularSensibility;
  177. if (!noPreventDefault) {
  178. evt.preventDefault();
  179. }
  180. };
  181. this._wheel = function (event) {
  182. var delta = 0;
  183. if (event.wheelDelta) {
  184. delta = event.wheelDelta / (_this.wheelPrecision * 40);
  185. }
  186. else if (event.detail) {
  187. delta = -event.detail / _this.wheelPrecision;
  188. }
  189. if (delta)
  190. _this.inertialRadiusOffset += delta;
  191. if (event.preventDefault) {
  192. if (!noPreventDefault) {
  193. event.preventDefault();
  194. }
  195. }
  196. };
  197. this._onKeyDown = function (evt) {
  198. if (_this.keysUp.indexOf(evt.keyCode) !== -1 ||
  199. _this.keysDown.indexOf(evt.keyCode) !== -1 ||
  200. _this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  201. _this.keysRight.indexOf(evt.keyCode) !== -1) {
  202. var index = _this._keys.indexOf(evt.keyCode);
  203. if (index === -1) {
  204. _this._keys.push(evt.keyCode);
  205. }
  206. if (evt.preventDefault) {
  207. if (!noPreventDefault) {
  208. evt.preventDefault();
  209. }
  210. }
  211. }
  212. };
  213. this._onKeyUp = function (evt) {
  214. if (_this.keysUp.indexOf(evt.keyCode) !== -1 ||
  215. _this.keysDown.indexOf(evt.keyCode) !== -1 ||
  216. _this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  217. _this.keysRight.indexOf(evt.keyCode) !== -1) {
  218. var index = _this._keys.indexOf(evt.keyCode);
  219. if (index >= 0) {
  220. _this._keys.splice(index, 1);
  221. }
  222. if (evt.preventDefault) {
  223. if (!noPreventDefault) {
  224. evt.preventDefault();
  225. }
  226. }
  227. }
  228. };
  229. this._onLostFocus = function () {
  230. _this._keys = [];
  231. pointers.empty();
  232. previousPinchDistance = 0;
  233. cacheSoloPointer = null;
  234. };
  235. this._onGestureStart = function (e) {
  236. if (window.MSGesture === undefined) {
  237. return;
  238. }
  239. if (!_this._MSGestureHandler) {
  240. _this._MSGestureHandler = new MSGesture();
  241. _this._MSGestureHandler.target = element;
  242. }
  243. _this._MSGestureHandler.addPointer(e.pointerId);
  244. };
  245. this._onGesture = function (e) {
  246. _this.radius *= e.scale;
  247. if (e.preventDefault) {
  248. if (!noPreventDefault) {
  249. e.stopPropagation();
  250. e.preventDefault();
  251. }
  252. }
  253. };
  254. this._reset = function () {
  255. _this._keys = [];
  256. _this.inertialAlphaOffset = 0;
  257. _this.inertialBetaOffset = 0;
  258. _this.inertialRadiusOffset = 0;
  259. pointers.empty();
  260. previousPinchDistance = 0;
  261. cacheSoloPointer = null;
  262. };
  263. }
  264. element.addEventListener(eventPrefix + "down", this._onPointerDown, false);
  265. element.addEventListener(eventPrefix + "up", this._onPointerUp, false);
  266. element.addEventListener(eventPrefix + "out", this._onPointerUp, false);
  267. element.addEventListener(eventPrefix + "move", this._onPointerMove, false);
  268. element.addEventListener("mousemove", this._onMouseMove, false);
  269. element.addEventListener("MSPointerDown", this._onGestureStart, false);
  270. element.addEventListener("MSGestureChange", this._onGesture, false);
  271. element.addEventListener('mousewheel', this._wheel, false);
  272. element.addEventListener('DOMMouseScroll', this._wheel, false);
  273. BABYLON.Tools.RegisterTopRootEvents([
  274. { name: "keydown", handler: this._onKeyDown },
  275. { name: "keyup", handler: this._onKeyUp },
  276. { name: "blur", handler: this._onLostFocus }
  277. ]);
  278. };
  279. ArcRotateCamera.prototype.detachControl = function (element) {
  280. if (this._attachedElement !== element) {
  281. return;
  282. }
  283. element.removeEventListener(eventPrefix + "down", this._onPointerDown);
  284. element.removeEventListener(eventPrefix + "up", this._onPointerUp);
  285. element.removeEventListener(eventPrefix + "out", this._onPointerUp);
  286. element.removeEventListener(eventPrefix + "move", this._onPointerMove);
  287. element.removeEventListener("mousemove", this._onMouseMove);
  288. element.removeEventListener("MSPointerDown", this._onGestureStart);
  289. element.removeEventListener("MSGestureChange", this._onGesture);
  290. element.removeEventListener('mousewheel', this._wheel);
  291. element.removeEventListener('DOMMouseScroll', this._wheel);
  292. BABYLON.Tools.UnregisterTopRootEvents([
  293. { name: "keydown", handler: this._onKeyDown },
  294. { name: "keyup", handler: this._onKeyUp },
  295. { name: "blur", handler: this._onLostFocus }
  296. ]);
  297. this._MSGestureHandler = null;
  298. this._attachedElement = null;
  299. if (this._reset) {
  300. this._reset();
  301. }
  302. };
  303. ArcRotateCamera.prototype._checkInputs = function () {
  304. //if (async) collision inspection was triggered, don't update the camera's position - until the collision callback was called.
  305. if (this._collisionTriggered) {
  306. return;
  307. }
  308. // Keyboard
  309. for (var index = 0; index < this._keys.length; index++) {
  310. var keyCode = this._keys[index];
  311. if (this.keysLeft.indexOf(keyCode) !== -1) {
  312. this.inertialAlphaOffset -= 0.01;
  313. }
  314. else if (this.keysUp.indexOf(keyCode) !== -1) {
  315. this.inertialBetaOffset -= 0.01;
  316. }
  317. else if (this.keysRight.indexOf(keyCode) !== -1) {
  318. this.inertialAlphaOffset += 0.01;
  319. }
  320. else if (this.keysDown.indexOf(keyCode) !== -1) {
  321. this.inertialBetaOffset += 0.01;
  322. }
  323. }
  324. // Inertia
  325. if (this.inertialAlphaOffset !== 0 || this.inertialBetaOffset !== 0 || this.inertialRadiusOffset != 0) {
  326. this.alpha += this.beta <= 0 ? -this.inertialAlphaOffset : this.inertialAlphaOffset;
  327. this.beta += this.inertialBetaOffset;
  328. this.radius -= this.inertialRadiusOffset;
  329. this.inertialAlphaOffset *= this.inertia;
  330. this.inertialBetaOffset *= this.inertia;
  331. this.inertialRadiusOffset *= this.inertia;
  332. if (Math.abs(this.inertialAlphaOffset) < BABYLON.Engine.Epsilon)
  333. this.inertialAlphaOffset = 0;
  334. if (Math.abs(this.inertialBetaOffset) < BABYLON.Engine.Epsilon)
  335. this.inertialBetaOffset = 0;
  336. if (Math.abs(this.inertialRadiusOffset) < BABYLON.Engine.Epsilon)
  337. this.inertialRadiusOffset = 0;
  338. }
  339. // Limits
  340. if (this.lowerBetaLimit === null || this.lowerBetaLimit === undefined) {
  341. if (this.beta > Math.PI) {
  342. this.beta = this.beta - (2 * Math.PI);
  343. }
  344. }
  345. else {
  346. if (this.beta < this.lowerBetaLimit) {
  347. this.beta = this.lowerBetaLimit;
  348. }
  349. }
  350. if (this.upperBetaLimit === null || this.upperBetaLimit === undefined) {
  351. if (this.beta < -Math.PI) {
  352. this.beta = this.beta + (2 * Math.PI);
  353. }
  354. }
  355. else {
  356. if (this.beta > this.upperBetaLimit) {
  357. this.beta = this.upperBetaLimit;
  358. }
  359. }
  360. if (this.lowerAlphaLimit && this.alpha < this.lowerAlphaLimit) {
  361. this.alpha = this.lowerAlphaLimit;
  362. }
  363. if (this.upperAlphaLimit && this.alpha > this.upperAlphaLimit) {
  364. this.alpha = this.upperAlphaLimit;
  365. }
  366. if (this.lowerRadiusLimit && this.radius < this.lowerRadiusLimit) {
  367. this.radius = this.lowerRadiusLimit;
  368. }
  369. if (this.upperRadiusLimit && this.radius > this.upperRadiusLimit) {
  370. this.radius = this.upperRadiusLimit;
  371. }
  372. _super.prototype._checkInputs.call(this);
  373. };
  374. ArcRotateCamera.prototype.setPosition = function (position) {
  375. var radiusv3 = position.subtract(this._getTargetPosition());
  376. this.radius = radiusv3.length();
  377. // Alpha
  378. this.alpha = Math.acos(radiusv3.x / Math.sqrt(Math.pow(radiusv3.x, 2) + Math.pow(radiusv3.z, 2)));
  379. if (radiusv3.z < 0) {
  380. this.alpha = 2 * Math.PI - this.alpha;
  381. }
  382. // Beta
  383. this.beta = Math.acos(radiusv3.y / this.radius);
  384. };
  385. ArcRotateCamera.prototype._getViewMatrix = function () {
  386. // Compute
  387. var cosa = Math.cos(this.alpha);
  388. var sina = Math.sin(this.alpha);
  389. var cosb = Math.cos(this.beta);
  390. var sinb = Math.sin(this.beta);
  391. var target = this._getTargetPosition();
  392. target.addToRef(new BABYLON.Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this.position);
  393. if (this.checkCollisions) {
  394. this._collider.radius = this.collisionRadius;
  395. this.position.subtractToRef(this._previousPosition, this._collisionVelocity);
  396. this._collisionTriggered = true;
  397. this.getScene().collisionCoordinator.getNewPosition(this._previousPosition, this._collisionVelocity, this._collider, 3, null, this._onCollisionPositionChange, this.uniqueId);
  398. }
  399. var up = this.upVector.clone();
  400. if (this.beta < 0) {
  401. up = up.negate();
  402. }
  403. BABYLON.Matrix.LookAtLHToRef(this.position, target, up, this._viewMatrix);
  404. this._previousAlpha = this.alpha;
  405. this._previousBeta = this.beta;
  406. this._previousRadius = this.radius;
  407. this._previousPosition.copyFrom(this.position);
  408. this._viewMatrix.m[12] += this.targetScreenOffset.x;
  409. this._viewMatrix.m[13] += this.targetScreenOffset.y;
  410. return this._viewMatrix;
  411. };
  412. ArcRotateCamera.prototype.zoomOn = function (meshes) {
  413. meshes = meshes || this.getScene().meshes;
  414. var minMaxVector = BABYLON.Mesh.MinMax(meshes);
  415. var distance = BABYLON.Vector3.Distance(minMaxVector.min, minMaxVector.max);
  416. this.radius = distance * this.zoomOnFactor;
  417. this.focusOn({ min: minMaxVector.min, max: minMaxVector.max, distance: distance });
  418. };
  419. ArcRotateCamera.prototype.focusOn = function (meshesOrMinMaxVectorAndDistance) {
  420. var meshesOrMinMaxVector;
  421. var distance;
  422. if (meshesOrMinMaxVectorAndDistance.min === undefined) {
  423. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance || this.getScene().meshes;
  424. meshesOrMinMaxVector = BABYLON.Mesh.MinMax(meshesOrMinMaxVector);
  425. distance = BABYLON.Vector3.Distance(meshesOrMinMaxVector.min, meshesOrMinMaxVector.max);
  426. }
  427. else {
  428. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance;
  429. distance = meshesOrMinMaxVectorAndDistance.distance;
  430. }
  431. this.target = BABYLON.Mesh.Center(meshesOrMinMaxVector);
  432. this.maxZ = distance * 2;
  433. };
  434. /**
  435. * @override
  436. * needs to be overridden, so sub has required properties to be copied
  437. */
  438. ArcRotateCamera.prototype.getSubCamera = function (name, isA) {
  439. var alphaSpace = this._subCamHalfSpace * (isA ? -1 : 1);
  440. return new BABYLON.ArcRotateCamera(name, this.alpha + alphaSpace, this.beta, this.radius, this.target, this.getScene());
  441. };
  442. /**
  443. * @override
  444. * needs to be overridden, adding copy of alpha, beta & radius
  445. */
  446. ArcRotateCamera.prototype._updateSubCameras = function () {
  447. var camA = this.subCameras[BABYLON.Camera.SUB_CAMERAID_A];
  448. var camB = this.subCameras[BABYLON.Camera.SUB_CAMERAID_B];
  449. camA.alpha = this.alpha - this._subCamHalfSpace;
  450. camB.alpha = this.alpha + this._subCamHalfSpace;
  451. camA.beta = camB.beta = this.beta;
  452. camA.radius = camB.radius = this.radius;
  453. _super.prototype._updateSubCameras.call(this);
  454. };
  455. return ArcRotateCamera;
  456. })(BABYLON.Camera);
  457. BABYLON.ArcRotateCamera = ArcRotateCamera;
  458. })(BABYLON || (BABYLON = {}));
  459. //# sourceMappingURL=babylon.arcRotateCamera.js.map