babylon.arcRotateCamera.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. module BABYLON {
  2. var eventPrefix = Tools.GetPointerPrefix();
  3. export class ArcRotateCamera extends Camera {
  4. public inertialAlphaOffset = 0;
  5. public inertialBetaOffset = 0;
  6. public inertialRadiusOffset = 0;
  7. public lowerAlphaLimit = null;
  8. public upperAlphaLimit = null;
  9. public lowerBetaLimit = 0.01;
  10. public upperBetaLimit = Math.PI;
  11. public lowerRadiusLimit = null;
  12. public upperRadiusLimit = null;
  13. public angularSensibility = 1000.0;
  14. public wheelPrecision = 3.0;
  15. public keysUp = [38];
  16. public keysDown = [40];
  17. public keysLeft = [37];
  18. public keysRight = [39];
  19. public zoomOnFactor = 1;
  20. private _keys = [];
  21. private _viewMatrix = new BABYLON.Matrix();
  22. private _attachedElement: HTMLElement;
  23. private _onPointerDown: (e: PointerEvent) => void;
  24. private _onPointerUp: (e: PointerEvent) => void;
  25. private _onPointerMove: (e: PointerEvent) => void;
  26. private _wheel: (e: MouseWheelEvent) => void;
  27. private _onMouseMove: (e: MouseEvent) => any;
  28. private _onKeyDown: (e: KeyboardEvent) => any;
  29. private _onKeyUp: (e: KeyboardEvent) => any;
  30. private _onLostFocus: (e: FocusEvent) => any;
  31. private _reset: () => void;
  32. private _onGestureStart: (e: PointerEvent) => void;
  33. private _onGesture: (e: MSGestureEvent) => void;
  34. private _MSGestureHandler: MSGesture;
  35. // Collisions
  36. public onCollide: (collidedMesh: AbstractMesh) => void;
  37. public checkCollisions = false;
  38. public collisionRadius = new Vector3(0.5, 0.5, 0.5);
  39. private _collider = new Collider();
  40. private _previousPosition = Vector3.Zero();
  41. private _collisionVelocity = Vector3.Zero();
  42. private _newPosition = Vector3.Zero();
  43. private _previousAlpha: number;
  44. private _previousBeta: number;
  45. private _previousRadius: number;
  46. constructor(name: string, public alpha: number, public beta: number, public radius: number, public target: any, scene: Scene) {
  47. super(name, BABYLON.Vector3.Zero(), scene);
  48. this.getViewMatrix();
  49. }
  50. public _getTargetPosition(): Vector3 {
  51. return this.target.position || this.target;
  52. }
  53. // Cache
  54. public _initCache(): void {
  55. super._initCache();
  56. this._cache.target = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  57. this._cache.alpha = undefined;
  58. this._cache.beta = undefined;
  59. this._cache.radius = undefined;
  60. }
  61. public _updateCache(ignoreParentClass?: boolean): void {
  62. if (!ignoreParentClass) {
  63. super._updateCache();
  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. }
  70. // Synchronized
  71. public _isSynchronizedViewMatrix(): boolean {
  72. if (!super._isSynchronizedViewMatrix())
  73. return false;
  74. return this._cache.target.equals(this._getTargetPosition())
  75. && this._cache.alpha === this.alpha
  76. && this._cache.beta === this.beta
  77. && this._cache.radius === this.radius;
  78. }
  79. // Methods
  80. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  81. var previousPosition;
  82. var pointerId;
  83. if (this._attachedElement) {
  84. return;
  85. }
  86. this._attachedElement = element;
  87. var engine = this.getEngine();
  88. if (this._onPointerDown === undefined) {
  89. this._onPointerDown = evt => {
  90. if (pointerId) {
  91. return;
  92. }
  93. pointerId = evt.pointerId;
  94. previousPosition = {
  95. x: evt.clientX,
  96. y: evt.clientY
  97. };
  98. if (!noPreventDefault) {
  99. evt.preventDefault();
  100. }
  101. };
  102. this._onPointerUp = evt => {
  103. previousPosition = null;
  104. pointerId = null;
  105. if (!noPreventDefault) {
  106. evt.preventDefault();
  107. }
  108. };
  109. this._onPointerMove = evt => {
  110. if (!previousPosition) {
  111. return;
  112. }
  113. if (pointerId !== evt.pointerId) {
  114. return;
  115. }
  116. var offsetX = evt.clientX - previousPosition.x;
  117. var offsetY = evt.clientY - previousPosition.y;
  118. this.inertialAlphaOffset -= offsetX / this.angularSensibility;
  119. this.inertialBetaOffset -= offsetY / this.angularSensibility;
  120. previousPosition = {
  121. x: evt.clientX,
  122. y: evt.clientY
  123. };
  124. if (!noPreventDefault) {
  125. evt.preventDefault();
  126. }
  127. };
  128. this._onMouseMove = evt => {
  129. if (!engine.isPointerLock) {
  130. return;
  131. }
  132. var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  133. var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  134. this.inertialAlphaOffset -= offsetX / this.angularSensibility;
  135. this.inertialBetaOffset -= offsetY / this.angularSensibility;
  136. if (!noPreventDefault) {
  137. evt.preventDefault();
  138. }
  139. };
  140. this._wheel = event => {
  141. var delta = 0;
  142. if (event.wheelDelta) {
  143. delta = event.wheelDelta / (this.wheelPrecision * 40);
  144. } else if (event.detail) {
  145. delta = -event.detail / this.wheelPrecision;
  146. }
  147. if (delta)
  148. this.inertialRadiusOffset += delta;
  149. if (event.preventDefault) {
  150. if (!noPreventDefault) {
  151. event.preventDefault();
  152. }
  153. }
  154. };
  155. this._onKeyDown = evt => {
  156. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  157. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  158. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  159. this.keysRight.indexOf(evt.keyCode) !== -1) {
  160. var index = this._keys.indexOf(evt.keyCode);
  161. if (index === -1) {
  162. this._keys.push(evt.keyCode);
  163. }
  164. if (evt.preventDefault) {
  165. if (!noPreventDefault) {
  166. evt.preventDefault();
  167. }
  168. }
  169. }
  170. };
  171. this._onKeyUp = evt => {
  172. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  173. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  174. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  175. this.keysRight.indexOf(evt.keyCode) !== -1) {
  176. var index = this._keys.indexOf(evt.keyCode);
  177. if (index >= 0) {
  178. this._keys.splice(index, 1);
  179. }
  180. if (evt.preventDefault) {
  181. if (!noPreventDefault) {
  182. evt.preventDefault();
  183. }
  184. }
  185. }
  186. };
  187. this._onLostFocus = () => {
  188. this._keys = [];
  189. pointerId = null;
  190. };
  191. this._onGestureStart = e => {
  192. if (window.MSGesture === undefined) {
  193. return;
  194. }
  195. if (!this._MSGestureHandler) {
  196. this._MSGestureHandler = new MSGesture();
  197. this._MSGestureHandler.target = element;
  198. }
  199. this._MSGestureHandler.addPointer(e.pointerId);
  200. };
  201. this._onGesture = e => {
  202. this.radius *= e.scale;
  203. if (e.preventDefault) {
  204. if (!noPreventDefault) {
  205. e.stopPropagation();
  206. e.preventDefault();
  207. }
  208. }
  209. };
  210. this._reset = () => {
  211. this._keys = [];
  212. this.inertialAlphaOffset = 0;
  213. this.inertialBetaOffset = 0;
  214. this.inertialRadiusOffset = 0;
  215. previousPosition = null;
  216. pointerId = null;
  217. };
  218. }
  219. element.addEventListener(eventPrefix + "down", this._onPointerDown, false);
  220. element.addEventListener(eventPrefix + "up", this._onPointerUp, false);
  221. element.addEventListener(eventPrefix + "out", this._onPointerUp, false);
  222. element.addEventListener(eventPrefix + "move", this._onPointerMove, false);
  223. element.addEventListener("mousemove", this._onMouseMove, false);
  224. element.addEventListener("MSPointerDown", this._onGestureStart, false);
  225. element.addEventListener("MSGestureChange", this._onGesture, false);
  226. element.addEventListener('mousewheel', this._wheel, false);
  227. element.addEventListener('DOMMouseScroll', this._wheel, false);
  228. Tools.RegisterTopRootEvents([
  229. { name: "keydown", handler: this._onKeyDown },
  230. { name: "keyup", handler: this._onKeyUp },
  231. { name: "blur", handler: this._onLostFocus }
  232. ]);
  233. }
  234. public detachControl(element: HTMLElement): void {
  235. if (this._attachedElement != element) {
  236. return;
  237. }
  238. element.removeEventListener(eventPrefix + "down", this._onPointerDown);
  239. element.removeEventListener(eventPrefix + "up", this._onPointerUp);
  240. element.removeEventListener(eventPrefix + "out", this._onPointerUp);
  241. element.removeEventListener(eventPrefix + "move", this._onPointerMove);
  242. element.removeEventListener("mousemove", this._onMouseMove);
  243. element.removeEventListener("MSPointerDown", this._onGestureStart);
  244. element.removeEventListener("MSGestureChange", this._onGesture);
  245. element.removeEventListener('mousewheel', this._wheel);
  246. element.removeEventListener('DOMMouseScroll', this._wheel);
  247. Tools.UnregisterTopRootEvents([
  248. { name: "keydown", handler: this._onKeyDown },
  249. { name: "keyup", handler: this._onKeyUp },
  250. { name: "blur", handler: this._onLostFocus }
  251. ]);
  252. this._MSGestureHandler = null;
  253. this._attachedElement = null;
  254. if (this._reset) {
  255. this._reset();
  256. }
  257. }
  258. public _update(): void {
  259. // Keyboard
  260. for (var index = 0; index < this._keys.length; index++) {
  261. var keyCode = this._keys[index];
  262. if (this.keysLeft.indexOf(keyCode) !== -1) {
  263. this.inertialAlphaOffset -= 0.01;
  264. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  265. this.inertialBetaOffset -= 0.01;
  266. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  267. this.inertialAlphaOffset += 0.01;
  268. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  269. this.inertialBetaOffset += 0.01;
  270. }
  271. }
  272. // Inertia
  273. if (this.inertialAlphaOffset != 0 || this.inertialBetaOffset != 0 || this.inertialRadiusOffset != 0) {
  274. this.alpha += this.inertialAlphaOffset;
  275. this.beta += this.inertialBetaOffset;
  276. this.radius -= this.inertialRadiusOffset;
  277. this.inertialAlphaOffset *= this.inertia;
  278. this.inertialBetaOffset *= this.inertia;
  279. this.inertialRadiusOffset *= this.inertia;
  280. if (Math.abs(this.inertialAlphaOffset) < BABYLON.Engine.Epsilon)
  281. this.inertialAlphaOffset = 0;
  282. if (Math.abs(this.inertialBetaOffset) < BABYLON.Engine.Epsilon)
  283. this.inertialBetaOffset = 0;
  284. if (Math.abs(this.inertialRadiusOffset) < BABYLON.Engine.Epsilon)
  285. this.inertialRadiusOffset = 0;
  286. }
  287. // Limits
  288. if (this.lowerAlphaLimit && this.alpha < this.lowerAlphaLimit) {
  289. this.alpha = this.lowerAlphaLimit;
  290. }
  291. if (this.upperAlphaLimit && this.alpha > this.upperAlphaLimit) {
  292. this.alpha = this.upperAlphaLimit;
  293. }
  294. if (this.lowerBetaLimit && this.beta < this.lowerBetaLimit) {
  295. this.beta = this.lowerBetaLimit;
  296. }
  297. if (this.upperBetaLimit && this.beta > this.upperBetaLimit) {
  298. this.beta = this.upperBetaLimit;
  299. }
  300. if (this.lowerRadiusLimit && this.radius < this.lowerRadiusLimit) {
  301. this.radius = this.lowerRadiusLimit;
  302. }
  303. if (this.upperRadiusLimit && this.radius > this.upperRadiusLimit) {
  304. this.radius = this.upperRadiusLimit;
  305. }
  306. }
  307. public setPosition(position: Vector3): void {
  308. var radiusv3 = position.subtract(this._getTargetPosition());
  309. this.radius = radiusv3.length();
  310. // Alpha
  311. this.alpha = Math.acos(radiusv3.x / Math.sqrt(Math.pow(radiusv3.x, 2) + Math.pow(radiusv3.z, 2)));
  312. if (radiusv3.z < 0) {
  313. this.alpha = 2 * Math.PI - this.alpha;
  314. }
  315. // Beta
  316. this.beta = Math.acos(radiusv3.y / this.radius);
  317. }
  318. public _getViewMatrix(): Matrix {
  319. // Compute
  320. var cosa = Math.cos(this.alpha);
  321. var sina = Math.sin(this.alpha);
  322. var cosb = Math.cos(this.beta);
  323. var sinb = Math.sin(this.beta);
  324. var target = this._getTargetPosition();
  325. target.addToRef(new BABYLON.Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this.position);
  326. if (this.checkCollisions) {
  327. this._collider.radius = this.collisionRadius;
  328. this.position.subtractToRef(this._previousPosition, this._collisionVelocity);
  329. this.getScene()._getNewPosition(this._previousPosition, this._collisionVelocity, this._collider, 3, this._newPosition);
  330. if (!this._newPosition.equalsWithEpsilon(this.position)) {
  331. this.position.copyFrom(this._previousPosition);
  332. this.alpha = this._previousAlpha;
  333. this.beta = this._previousBeta;
  334. this.radius = this._previousRadius;
  335. if (this.onCollide) {
  336. this.onCollide(this._collider.collidedMesh);
  337. }
  338. }
  339. }
  340. Matrix.LookAtLHToRef(this.position, target, this.upVector, this._viewMatrix);
  341. this._previousAlpha = this.alpha;
  342. this._previousBeta = this.beta;
  343. this._previousRadius = this.radius;
  344. this._previousPosition.copyFrom(this.position);
  345. return this._viewMatrix;
  346. }
  347. public zoomOn(meshes?: AbstractMesh[]): void {
  348. meshes = meshes || this.getScene().meshes;
  349. var minMaxVector = BABYLON.Mesh.MinMax(meshes);
  350. var distance = BABYLON.Vector3.Distance(minMaxVector.min, minMaxVector.max);
  351. this.radius = distance * this.zoomOnFactor;
  352. this.focusOn({ min: minMaxVector.min, max: minMaxVector.max, distance: distance });
  353. }
  354. public focusOn(meshesOrMinMaxVectorAndDistance): void {
  355. var meshesOrMinMaxVector;
  356. var distance;
  357. if (meshesOrMinMaxVectorAndDistance.min === undefined) { // meshes
  358. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance || this.getScene().meshes;
  359. meshesOrMinMaxVector = BABYLON.Mesh.MinMax(meshesOrMinMaxVector);
  360. distance = BABYLON.Vector3.Distance(meshesOrMinMaxVector.min, meshesOrMinMaxVector.max);
  361. }
  362. else { //minMaxVector and distance
  363. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance;
  364. distance = meshesOrMinMaxVectorAndDistance.distance;
  365. }
  366. this.target = Mesh.Center(meshesOrMinMaxVector);
  367. this.maxZ = distance * 2;
  368. }
  369. }
  370. }