babylon.arcRotateCamera.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. _super.call(this, name, BABYLON.Vector3.Zero(), scene);
  14. this.alpha = alpha;
  15. this.beta = beta;
  16. this.radius = radius;
  17. this.target = target;
  18. this.inertialAlphaOffset = 0;
  19. this.inertialBetaOffset = 0;
  20. this.inertialRadiusOffset = 0;
  21. this.lowerAlphaLimit = null;
  22. this.upperAlphaLimit = null;
  23. this.lowerBetaLimit = 0.01;
  24. this.upperBetaLimit = Math.PI;
  25. this.lowerRadiusLimit = null;
  26. this.upperRadiusLimit = null;
  27. this.angularSensibility = 1000.0;
  28. this.wheelPrecision = 3.0;
  29. this.keysUp = [38];
  30. this.keysDown = [40];
  31. this.keysLeft = [37];
  32. this.keysRight = [39];
  33. this.zoomOnFactor = 1;
  34. this.targetScreenOffset = BABYLON.Vector2.Zero();
  35. this._keys = [];
  36. this._viewMatrix = new BABYLON.Matrix();
  37. this.checkCollisions = false;
  38. this.collisionRadius = new BABYLON.Vector3(0.5, 0.5, 0.5);
  39. this._collider = new BABYLON.Collider();
  40. this._previousPosition = BABYLON.Vector3.Zero();
  41. this._collisionVelocity = BABYLON.Vector3.Zero();
  42. this._newPosition = BABYLON.Vector3.Zero();
  43. // Pinch
  44. // value for pinch step scaling
  45. // set to 20 by default
  46. this.pinchPrecision = 20;
  47. this.getViewMatrix();
  48. }
  49. ArcRotateCamera.prototype._getTargetPosition = function () {
  50. return this.target.position || this.target;
  51. };
  52. // Cache
  53. ArcRotateCamera.prototype._initCache = function () {
  54. _super.prototype._initCache.call(this);
  55. this._cache.target = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  56. this._cache.alpha = undefined;
  57. this._cache.beta = undefined;
  58. this._cache.radius = undefined;
  59. this._cache.targetScreenOffset = undefined;
  60. };
  61. ArcRotateCamera.prototype._updateCache = function (ignoreParentClass) {
  62. if (!ignoreParentClass) {
  63. _super.prototype._updateCache.call(this);
  64. }
  65. this._cache.target.copyFrom(this._getTargetPosition());
  66. this._cache.alpha = this.alpha;
  67. this._cache.beta = this.beta;
  68. this._cache.radius = this.radius;
  69. this._cache.targetScreenOffset = this.targetScreenOffset.clone();
  70. };
  71. // Synchronized
  72. ArcRotateCamera.prototype._isSynchronizedViewMatrix = function () {
  73. if (!_super.prototype._isSynchronizedViewMatrix.call(this))
  74. return false;
  75. 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);
  76. };
  77. // Methods
  78. ArcRotateCamera.prototype.attachControl = function (element, noPreventDefault) {
  79. var _this = this;
  80. var previousPosition;
  81. var pointerId;
  82. // to know if pinch started
  83. var pinchStarted = false;
  84. // two pinch point on X
  85. // that will use for find if user action is pinch open or pinch close
  86. var pinchPointX1, pinchPointX2;
  87. if (this._attachedElement) {
  88. return;
  89. }
  90. this._attachedElement = element;
  91. var engine = this.getEngine();
  92. if (this._onPointerDown === undefined) {
  93. this._onPointerDown = function (evt) {
  94. if (pointerId) {
  95. return;
  96. }
  97. pointerId = evt.pointerId;
  98. previousPosition = {
  99. x: evt.clientX,
  100. y: evt.clientY
  101. };
  102. if (!noPreventDefault) {
  103. evt.preventDefault();
  104. }
  105. };
  106. this._onPointerUp = function (evt) {
  107. previousPosition = null;
  108. pointerId = null;
  109. if (!noPreventDefault) {
  110. evt.preventDefault();
  111. }
  112. };
  113. this._onPointerMove = function (evt) {
  114. if (!previousPosition) {
  115. return;
  116. }
  117. if (pointerId !== evt.pointerId) {
  118. return;
  119. }
  120. // return pinch is started
  121. if (pinchStarted) {
  122. return;
  123. }
  124. var offsetX = evt.clientX - previousPosition.x;
  125. var offsetY = evt.clientY - previousPosition.y;
  126. _this.inertialAlphaOffset -= offsetX / _this.angularSensibility;
  127. _this.inertialBetaOffset -= offsetY / _this.angularSensibility;
  128. previousPosition = {
  129. x: evt.clientX,
  130. y: evt.clientY
  131. };
  132. if (!noPreventDefault) {
  133. evt.preventDefault();
  134. }
  135. };
  136. this._onMouseMove = function (evt) {
  137. if (!engine.isPointerLock) {
  138. return;
  139. }
  140. // return pinch is started
  141. if (pinchStarted) {
  142. return;
  143. }
  144. var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  145. var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  146. _this.inertialAlphaOffset -= offsetX / _this.angularSensibility;
  147. _this.inertialBetaOffset -= offsetY / _this.angularSensibility;
  148. if (!noPreventDefault) {
  149. evt.preventDefault();
  150. }
  151. };
  152. this._wheel = function (event) {
  153. var delta = 0;
  154. if (event.wheelDelta) {
  155. delta = event.wheelDelta / (_this.wheelPrecision * 40);
  156. }
  157. else if (event.detail) {
  158. delta = -event.detail / _this.wheelPrecision;
  159. }
  160. if (delta)
  161. _this.inertialRadiusOffset += delta;
  162. if (event.preventDefault) {
  163. if (!noPreventDefault) {
  164. event.preventDefault();
  165. }
  166. }
  167. };
  168. this._onKeyDown = function (evt) {
  169. 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) {
  170. var index = _this._keys.indexOf(evt.keyCode);
  171. if (index === -1) {
  172. _this._keys.push(evt.keyCode);
  173. }
  174. if (evt.preventDefault) {
  175. if (!noPreventDefault) {
  176. evt.preventDefault();
  177. }
  178. }
  179. }
  180. };
  181. this._onKeyUp = function (evt) {
  182. 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) {
  183. var index = _this._keys.indexOf(evt.keyCode);
  184. if (index >= 0) {
  185. _this._keys.splice(index, 1);
  186. }
  187. if (evt.preventDefault) {
  188. if (!noPreventDefault) {
  189. evt.preventDefault();
  190. }
  191. }
  192. }
  193. };
  194. this._onLostFocus = function () {
  195. _this._keys = [];
  196. pointerId = null;
  197. };
  198. this._onGestureStart = function (e) {
  199. if (window.MSGesture === undefined) {
  200. return;
  201. }
  202. if (!_this._MSGestureHandler) {
  203. _this._MSGestureHandler = new MSGesture();
  204. _this._MSGestureHandler.target = element;
  205. }
  206. _this._MSGestureHandler.addPointer(e.pointerId);
  207. };
  208. this._onGesture = function (e) {
  209. _this.radius *= e.scale;
  210. if (e.preventDefault) {
  211. if (!noPreventDefault) {
  212. e.stopPropagation();
  213. e.preventDefault();
  214. }
  215. }
  216. };
  217. this._reset = function () {
  218. _this._keys = [];
  219. _this.inertialAlphaOffset = 0;
  220. _this.inertialBetaOffset = 0;
  221. _this.inertialRadiusOffset = 0;
  222. previousPosition = null;
  223. pointerId = null;
  224. };
  225. this._touchStart = function (event) {
  226. if (event.touches.length === 2) {
  227. //-- start pinch if two fingers on the screen
  228. pinchStarted = true;
  229. _this._pinchStart(event);
  230. }
  231. };
  232. this._touchMove = function (event) {
  233. if (pinchStarted) {
  234. //-- make scaling
  235. _this._pinchMove(event);
  236. }
  237. };
  238. this._touchEnd = function (event) {
  239. if (pinchStarted) {
  240. //-- end of pinch
  241. _this._pinchEnd(event);
  242. }
  243. };
  244. this._pinchStart = function (event) {
  245. // save origin touch point
  246. pinchPointX1 = event.touches[0].clientX;
  247. pinchPointX2 = event.touches[1].clientX;
  248. // block the camera
  249. // if not it rotate around target during pinch
  250. pinchStarted = true;
  251. };
  252. this._pinchMove = function (event) {
  253. // variable for new camera's radius
  254. var delta = 0;
  255. // variables to know if pinch open or pinch close
  256. var direction = 1;
  257. var distanceXOrigine, distanceXNow;
  258. if (event.touches.length !== 2)
  259. return;
  260. // calculate absolute distances of the two fingers
  261. distanceXOrigine = Math.abs(pinchPointX1 - pinchPointX2);
  262. distanceXNow = Math.abs(event.touches[0].clientX - event.touches[1].clientX);
  263. // if distanceXNow < distanceXOrigine -> pinch close so direction = -1
  264. if (distanceXNow < distanceXOrigine) {
  265. direction = -1;
  266. }
  267. // calculate new radius
  268. delta = (_this.pinchPrecision / (_this.wheelPrecision * 40)) * direction;
  269. // set new radius
  270. _this.inertialRadiusOffset -= delta;
  271. // save origin touch point
  272. pinchPointX1 = event.touches[0].clientX;
  273. pinchPointX2 = event.touches[1].clientX;
  274. };
  275. this._pinchEnd = function (event) {
  276. // cancel pinch and deblock camera rotation
  277. pinchStarted = false;
  278. };
  279. }
  280. element.addEventListener(eventPrefix + "down", this._onPointerDown, false);
  281. element.addEventListener(eventPrefix + "up", this._onPointerUp, false);
  282. element.addEventListener(eventPrefix + "out", this._onPointerUp, false);
  283. element.addEventListener(eventPrefix + "move", this._onPointerMove, false);
  284. element.addEventListener("mousemove", this._onMouseMove, false);
  285. element.addEventListener("MSPointerDown", this._onGestureStart, false);
  286. element.addEventListener("MSGestureChange", this._onGesture, false);
  287. element.addEventListener('mousewheel', this._wheel, false);
  288. element.addEventListener('DOMMouseScroll', this._wheel, false);
  289. // pinch
  290. element.addEventListener('touchstart', this._touchStart, false);
  291. element.addEventListener('touchmove', this._touchMove, false);
  292. element.addEventListener('touchend', this._touchEnd, false);
  293. BABYLON.Tools.RegisterTopRootEvents([
  294. { name: "keydown", handler: this._onKeyDown },
  295. { name: "keyup", handler: this._onKeyUp },
  296. { name: "blur", handler: this._onLostFocus }
  297. ]);
  298. };
  299. ArcRotateCamera.prototype.detachControl = function (element) {
  300. if (this._attachedElement != element) {
  301. return;
  302. }
  303. element.removeEventListener(eventPrefix + "down", this._onPointerDown);
  304. element.removeEventListener(eventPrefix + "up", this._onPointerUp);
  305. element.removeEventListener(eventPrefix + "out", this._onPointerUp);
  306. element.removeEventListener(eventPrefix + "move", this._onPointerMove);
  307. element.removeEventListener("mousemove", this._onMouseMove);
  308. element.removeEventListener("MSPointerDown", this._onGestureStart);
  309. element.removeEventListener("MSGestureChange", this._onGesture);
  310. element.removeEventListener('mousewheel', this._wheel);
  311. element.removeEventListener('DOMMouseScroll', this._wheel);
  312. // pinch
  313. element.removeEventListener('touchstart', this._touchStart);
  314. element.removeEventListener('touchmove', this._touchMove);
  315. element.removeEventListener('touchend', this._touchEnd);
  316. BABYLON.Tools.UnregisterTopRootEvents([
  317. { name: "keydown", handler: this._onKeyDown },
  318. { name: "keyup", handler: this._onKeyUp },
  319. { name: "blur", handler: this._onLostFocus }
  320. ]);
  321. this._MSGestureHandler = null;
  322. this._attachedElement = null;
  323. if (this._reset) {
  324. this._reset();
  325. }
  326. };
  327. ArcRotateCamera.prototype._update = function () {
  328. for (var index = 0; index < this._keys.length; index++) {
  329. var keyCode = this._keys[index];
  330. if (this.keysLeft.indexOf(keyCode) !== -1) {
  331. this.inertialAlphaOffset -= 0.01;
  332. }
  333. else if (this.keysUp.indexOf(keyCode) !== -1) {
  334. this.inertialBetaOffset -= 0.01;
  335. }
  336. else if (this.keysRight.indexOf(keyCode) !== -1) {
  337. this.inertialAlphaOffset += 0.01;
  338. }
  339. else if (this.keysDown.indexOf(keyCode) !== -1) {
  340. this.inertialBetaOffset += 0.01;
  341. }
  342. }
  343. // Inertia
  344. if (this.inertialAlphaOffset != 0 || this.inertialBetaOffset != 0 || this.inertialRadiusOffset != 0) {
  345. this.alpha += this.inertialAlphaOffset;
  346. this.beta += this.inertialBetaOffset;
  347. this.radius -= this.inertialRadiusOffset;
  348. this.inertialAlphaOffset *= this.inertia;
  349. this.inertialBetaOffset *= this.inertia;
  350. this.inertialRadiusOffset *= this.inertia;
  351. if (Math.abs(this.inertialAlphaOffset) < BABYLON.Engine.Epsilon)
  352. this.inertialAlphaOffset = 0;
  353. if (Math.abs(this.inertialBetaOffset) < BABYLON.Engine.Epsilon)
  354. this.inertialBetaOffset = 0;
  355. if (Math.abs(this.inertialRadiusOffset) < BABYLON.Engine.Epsilon)
  356. this.inertialRadiusOffset = 0;
  357. }
  358. // Limits
  359. if (this.lowerAlphaLimit && this.alpha < this.lowerAlphaLimit) {
  360. this.alpha = this.lowerAlphaLimit;
  361. }
  362. if (this.upperAlphaLimit && this.alpha > this.upperAlphaLimit) {
  363. this.alpha = this.upperAlphaLimit;
  364. }
  365. if (this.lowerBetaLimit && this.beta < this.lowerBetaLimit) {
  366. this.beta = this.lowerBetaLimit;
  367. }
  368. if (this.upperBetaLimit && this.beta > this.upperBetaLimit) {
  369. this.beta = this.upperBetaLimit;
  370. }
  371. if (this.lowerRadiusLimit && this.radius < this.lowerRadiusLimit) {
  372. this.radius = this.lowerRadiusLimit;
  373. }
  374. if (this.upperRadiusLimit && this.radius > this.upperRadiusLimit) {
  375. this.radius = this.upperRadiusLimit;
  376. }
  377. };
  378. ArcRotateCamera.prototype.setPosition = function (position) {
  379. var radiusv3 = position.subtract(this._getTargetPosition());
  380. this.radius = radiusv3.length();
  381. // Alpha
  382. this.alpha = Math.acos(radiusv3.x / Math.sqrt(Math.pow(radiusv3.x, 2) + Math.pow(radiusv3.z, 2)));
  383. if (radiusv3.z < 0) {
  384. this.alpha = 2 * Math.PI - this.alpha;
  385. }
  386. // Beta
  387. this.beta = Math.acos(radiusv3.y / this.radius);
  388. };
  389. ArcRotateCamera.prototype._getViewMatrix = function () {
  390. // Compute
  391. var cosa = Math.cos(this.alpha);
  392. var sina = Math.sin(this.alpha);
  393. var cosb = Math.cos(this.beta);
  394. var sinb = Math.sin(this.beta);
  395. var target = this._getTargetPosition();
  396. target.addToRef(new BABYLON.Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this.position);
  397. if (this.checkCollisions) {
  398. this._collider.radius = this.collisionRadius;
  399. this.position.subtractToRef(this._previousPosition, this._collisionVelocity);
  400. this.getScene()._getNewPosition(this._previousPosition, this._collisionVelocity, this._collider, 3, this._newPosition);
  401. if (!this._newPosition.equalsWithEpsilon(this.position)) {
  402. this.position.copyFrom(this._previousPosition);
  403. this.alpha = this._previousAlpha;
  404. this.beta = this._previousBeta;
  405. this.radius = this._previousRadius;
  406. if (this.onCollide) {
  407. this.onCollide(this._collider.collidedMesh);
  408. }
  409. }
  410. }
  411. BABYLON.Matrix.LookAtLHToRef(this.position, target, this.upVector, this._viewMatrix);
  412. this._previousAlpha = this.alpha;
  413. this._previousBeta = this.beta;
  414. this._previousRadius = this.radius;
  415. this._previousPosition.copyFrom(this.position);
  416. this._viewMatrix.m[12] += this.targetScreenOffset.x;
  417. this._viewMatrix.m[13] += this.targetScreenOffset.y;
  418. return this._viewMatrix;
  419. };
  420. ArcRotateCamera.prototype.zoomOn = function (meshes) {
  421. meshes = meshes || this.getScene().meshes;
  422. var minMaxVector = BABYLON.Mesh.MinMax(meshes);
  423. var distance = BABYLON.Vector3.Distance(minMaxVector.min, minMaxVector.max);
  424. this.radius = distance * this.zoomOnFactor;
  425. this.focusOn({ min: minMaxVector.min, max: minMaxVector.max, distance: distance });
  426. };
  427. ArcRotateCamera.prototype.focusOn = function (meshesOrMinMaxVectorAndDistance) {
  428. var meshesOrMinMaxVector;
  429. var distance;
  430. if (meshesOrMinMaxVectorAndDistance.min === undefined) {
  431. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance || this.getScene().meshes;
  432. meshesOrMinMaxVector = BABYLON.Mesh.MinMax(meshesOrMinMaxVector);
  433. distance = BABYLON.Vector3.Distance(meshesOrMinMaxVector.min, meshesOrMinMaxVector.max);
  434. }
  435. else {
  436. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance;
  437. distance = meshesOrMinMaxVectorAndDistance.distance;
  438. }
  439. this.target = BABYLON.Mesh.Center(meshesOrMinMaxVector);
  440. this.maxZ = distance * 2;
  441. };
  442. return ArcRotateCamera;
  443. })(BABYLON.Camera);
  444. BABYLON.ArcRotateCamera = ArcRotateCamera;
  445. })(BABYLON || (BABYLON = {}));
  446. //# sourceMappingURL=babylon.arcRotateCamera.js.map