babylon.arcRotateCamera.js 22 KB

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