babylon.arcRotateCamera.js 24 KB

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