babylon.arcRotateCamera.js 20 KB

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