babylon.arcRotateCamera.js 20 KB

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