babylon.arcRotateCamera.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /// <reference path="babylon.targetCamera.ts" />
  2. /// <reference path="..\Tools\babylon.tools.ts" />
  3. module BABYLON {
  4. export class ArcRotateCamera extends TargetCamera {
  5. @serialize()
  6. public alpha: number;
  7. @serialize()
  8. public beta: number;
  9. @serialize()
  10. public radius: number;
  11. @serializeAsVector3("target")
  12. protected _target: Vector3;
  13. protected _targetHost: AbstractMesh;
  14. public get target(): Vector3 {
  15. return this._target;
  16. }
  17. public set target(value: Vector3) {
  18. this.setTarget(value);
  19. }
  20. @serialize()
  21. public inertialAlphaOffset = 0;
  22. @serialize()
  23. public inertialBetaOffset = 0;
  24. @serialize()
  25. public inertialRadiusOffset = 0;
  26. @serialize()
  27. public lowerAlphaLimit = null;
  28. @serialize()
  29. public upperAlphaLimit = null;
  30. @serialize()
  31. public lowerBetaLimit = 0.01;
  32. @serialize()
  33. public upperBetaLimit = Math.PI;
  34. @serialize()
  35. public lowerRadiusLimit = null;
  36. @serialize()
  37. public upperRadiusLimit = null;
  38. @serialize()
  39. public inertialPanningX: number = 0;
  40. @serialize()
  41. public inertialPanningY: number = 0;
  42. @serialize()
  43. public pinchToPanMaxDistance: number = 3;
  44. @serialize()
  45. public panMaxFingersDistance: number = 100;
  46. @serialize()
  47. public panningDistanceLimit: number = null;
  48. @serializeAsVector3()
  49. public panningOriginTarget: Vector3 = Vector3.Zero();
  50. @serialize()
  51. public panningInertia = 0.9;
  52. //-- begin properties for backward compatibility for inputs
  53. public get angularSensibilityX() {
  54. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  55. if (pointers)
  56. return pointers.angularSensibilityX;
  57. }
  58. public set angularSensibilityX(value) {
  59. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  60. if (pointers) {
  61. pointers.angularSensibilityX = value;
  62. }
  63. }
  64. public get angularSensibilityY() {
  65. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  66. if (pointers)
  67. return pointers.angularSensibilityY;
  68. }
  69. public set angularSensibilityY(value) {
  70. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  71. if (pointers) {
  72. pointers.angularSensibilityY = value;
  73. }
  74. }
  75. public get pinchPrecision() {
  76. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  77. if (pointers)
  78. return pointers.pinchPrecision;
  79. }
  80. public set pinchPrecision(value) {
  81. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  82. if (pointers) {
  83. pointers.pinchPrecision = value;
  84. }
  85. }
  86. public get panningSensibility() {
  87. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  88. if (pointers)
  89. return pointers.panningSensibility;
  90. }
  91. public set panningSensibility(value) {
  92. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  93. if (pointers) {
  94. pointers.panningSensibility = value;
  95. }
  96. }
  97. public get keysUp() {
  98. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  99. if (keyboard)
  100. return keyboard.keysUp;
  101. }
  102. public set keysUp(value) {
  103. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  104. if (keyboard)
  105. keyboard.keysUp = value;
  106. }
  107. public get keysDown() {
  108. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  109. if (keyboard)
  110. return keyboard.keysDown;
  111. }
  112. public set keysDown(value) {
  113. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  114. if (keyboard)
  115. keyboard.keysDown = value;
  116. }
  117. public get keysLeft() {
  118. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  119. if (keyboard)
  120. return keyboard.keysLeft;
  121. }
  122. public set keysLeft(value) {
  123. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  124. if (keyboard)
  125. keyboard.keysLeft = value;
  126. }
  127. public get keysRight() {
  128. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  129. if (keyboard)
  130. return keyboard.keysRight;
  131. }
  132. public set keysRight(value) {
  133. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  134. if (keyboard)
  135. keyboard.keysRight = value;
  136. }
  137. public get wheelPrecision() {
  138. var mousewheel = <ArcRotateCameraMouseWheelInput>this.inputs.attached["mousewheel"];
  139. if (mousewheel)
  140. return mousewheel.wheelPrecision;
  141. }
  142. public set wheelPrecision(value) {
  143. var mousewheel = <ArcRotateCameraMouseWheelInput>this.inputs.attached["mousewheel"];
  144. if (mousewheel)
  145. mousewheel.wheelPrecision = value;
  146. }
  147. //-- end properties for backward compatibility for inputs
  148. @serialize()
  149. public zoomOnFactor = 1;
  150. public targetScreenOffset = Vector2.Zero();
  151. @serialize()
  152. public allowUpsideDown = true;
  153. public _viewMatrix = new Matrix();
  154. public _useCtrlForPanning: boolean;
  155. public _panningMouseButton: number;
  156. public inputs: ArcRotateCameraInputsManager;
  157. public _reset: () => void;
  158. // Panning
  159. public panningAxis: Vector3 = new Vector3(1, 1, 0);
  160. protected _localDirection: Vector3;
  161. protected _transformedDirection: Vector3;
  162. // Behaviors
  163. private _bouncingBehavior: BouncingBehavior;
  164. public get bouncingBehavior(): BouncingBehavior {
  165. return this._bouncingBehavior;
  166. }
  167. public get useBouncingBehavior(): boolean {
  168. return this._bouncingBehavior != null;
  169. }
  170. public set useBouncingBehavior(value: boolean) {
  171. if (value === this.useBouncingBehavior) {
  172. return;
  173. }
  174. if (value) {
  175. this._bouncingBehavior = new BouncingBehavior();
  176. this.addBehavior(this._bouncingBehavior);
  177. } else {
  178. this.removeBehavior(this._bouncingBehavior);
  179. this._bouncingBehavior = null;
  180. }
  181. }
  182. private _framingBehavior: FramingBehavior;
  183. public get framingBehavior(): FramingBehavior {
  184. return this._framingBehavior;
  185. }
  186. public get useFramingBehavior(): boolean {
  187. return this._framingBehavior != null;
  188. }
  189. public set useFramingBehavior(value: boolean) {
  190. if (value === this.useFramingBehavior) {
  191. return;
  192. }
  193. if (value) {
  194. this._framingBehavior = new FramingBehavior();
  195. this.addBehavior(this._framingBehavior);
  196. } else {
  197. this.removeBehavior(this._framingBehavior);
  198. this._framingBehavior = null;
  199. }
  200. }
  201. private _autoRotationBehavior: AutoRotationBehavior;
  202. public get autoRotationBehavior(): AutoRotationBehavior {
  203. return this._autoRotationBehavior;
  204. }
  205. public get useAutoRotationBehavior(): boolean {
  206. return this._autoRotationBehavior != null;
  207. }
  208. public set useAutoRotationBehavior(value: boolean) {
  209. if (value === this.useAutoRotationBehavior) {
  210. return;
  211. }
  212. if (value) {
  213. this._autoRotationBehavior = new AutoRotationBehavior();
  214. this.addBehavior(this._autoRotationBehavior);
  215. } else {
  216. this.removeBehavior(this._autoRotationBehavior);
  217. this._autoRotationBehavior = null;
  218. }
  219. }
  220. public onMeshTargetChangedObservable = new Observable<AbstractMesh>();
  221. // Collisions
  222. public onCollide: (collidedMesh: AbstractMesh) => void;
  223. public checkCollisions = false;
  224. public collisionRadius = new Vector3(0.5, 0.5, 0.5);
  225. protected _collider: Collider;
  226. protected _previousPosition = Vector3.Zero();
  227. protected _collisionVelocity = Vector3.Zero();
  228. protected _newPosition = Vector3.Zero();
  229. protected _previousAlpha: number;
  230. protected _previousBeta: number;
  231. protected _previousRadius: number;
  232. //due to async collision inspection
  233. protected _collisionTriggered: boolean;
  234. protected _targetBoundingCenter: Vector3;
  235. constructor(name: string, alpha: number, beta: number, radius: number, target: Vector3, scene: Scene) {
  236. super(name, Vector3.Zero(), scene);
  237. this._target = Vector3.Zero();
  238. if (target) {
  239. this.setTarget(target);
  240. }
  241. this.alpha = alpha;
  242. this.beta = beta;
  243. this.radius = radius;
  244. this.getViewMatrix();
  245. this.inputs = new ArcRotateCameraInputsManager(this);
  246. this.inputs.addKeyboard().addMouseWheel().addPointers();
  247. }
  248. // Cache
  249. public _initCache(): void {
  250. super._initCache();
  251. this._cache._target = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  252. this._cache.alpha = undefined;
  253. this._cache.beta = undefined;
  254. this._cache.radius = undefined;
  255. this._cache.targetScreenOffset = Vector2.Zero();
  256. }
  257. public _updateCache(ignoreParentClass?: boolean): void {
  258. if (!ignoreParentClass) {
  259. super._updateCache();
  260. }
  261. this._cache._target.copyFrom(this._getTargetPosition());
  262. this._cache.alpha = this.alpha;
  263. this._cache.beta = this.beta;
  264. this._cache.radius = this.radius;
  265. this._cache.targetScreenOffset.copyFrom(this.targetScreenOffset);
  266. }
  267. protected _getTargetPosition(): Vector3 {
  268. if (this._targetHost && this._targetHost.getAbsolutePosition) {
  269. var pos : Vector3 = this._targetHost.getAbsolutePosition();
  270. if (this._targetBoundingCenter) {
  271. pos.addToRef(this._targetBoundingCenter, this._target);
  272. } else {
  273. this._target.copyFrom(pos);
  274. }
  275. }
  276. var lockedTargetPosition = this._getLockedTargetPosition();
  277. if (lockedTargetPosition) {
  278. return lockedTargetPosition;
  279. }
  280. return this._target;
  281. }
  282. // State
  283. /**
  284. * Store current camera state (fov, position, etc..)
  285. */
  286. private _storedAlpha: number;
  287. private _storedBeta: number;
  288. private _storedRadius: number;
  289. private _storedTarget: Vector3;
  290. public storeState(): Camera {
  291. this._storedAlpha = this.alpha;
  292. this._storedBeta = this.beta;
  293. this._storedRadius = this.radius;
  294. this._storedTarget = this._getTargetPosition().clone();
  295. return super.storeState();
  296. }
  297. /**
  298. * Restored camera state. You must call storeState() first
  299. */
  300. public _restoreStateValues(): boolean {
  301. if (!super._restoreStateValues()) {
  302. return false;
  303. }
  304. this.alpha = this._storedAlpha;
  305. this.beta = this._storedBeta;
  306. this.radius = this._storedRadius;
  307. this.setTarget(this._storedTarget);
  308. this.inertialAlphaOffset = 0;
  309. this.inertialBetaOffset = 0;
  310. this.inertialRadiusOffset = 0;
  311. this.inertialPanningX = 0;
  312. this.inertialPanningY = 0;
  313. return true;
  314. }
  315. // Synchronized
  316. public _isSynchronizedViewMatrix(): boolean {
  317. if (!super._isSynchronizedViewMatrix())
  318. return false;
  319. return this._cache._target.equals(this._getTargetPosition())
  320. && this._cache.alpha === this.alpha
  321. && this._cache.beta === this.beta
  322. && this._cache.radius === this.radius
  323. && this._cache.targetScreenOffset.equals(this.targetScreenOffset);
  324. }
  325. // Methods
  326. public attachControl(element: HTMLElement, noPreventDefault?: boolean, useCtrlForPanning: boolean = true, panningMouseButton: number = 2): void {
  327. this._useCtrlForPanning = useCtrlForPanning;
  328. this._panningMouseButton = panningMouseButton;
  329. this.inputs.attachElement(element, noPreventDefault);
  330. this._reset = () => {
  331. this.inertialAlphaOffset = 0;
  332. this.inertialBetaOffset = 0;
  333. this.inertialRadiusOffset = 0;
  334. this.inertialPanningX = 0;
  335. this.inertialPanningY = 0;
  336. };
  337. }
  338. public detachControl(element: HTMLElement): void {
  339. this.inputs.detachElement(element);
  340. if (this._reset) {
  341. this._reset();
  342. }
  343. }
  344. public _checkInputs(): void {
  345. //if (async) collision inspection was triggered, don't update the camera's position - until the collision callback was called.
  346. if (this._collisionTriggered) {
  347. return;
  348. }
  349. this.inputs.checkInputs();
  350. // Inertia
  351. if (this.inertialAlphaOffset !== 0 || this.inertialBetaOffset !== 0 || this.inertialRadiusOffset !== 0) {
  352. if (this.getScene().useRightHandedSystem) {
  353. this.alpha -= this.beta <= 0 ? -this.inertialAlphaOffset : this.inertialAlphaOffset;
  354. } else {
  355. this.alpha += this.beta <= 0 ? -this.inertialAlphaOffset : this.inertialAlphaOffset;
  356. }
  357. this.beta += this.inertialBetaOffset;
  358. this.radius -= this.inertialRadiusOffset;
  359. this.inertialAlphaOffset *= this.inertia;
  360. this.inertialBetaOffset *= this.inertia;
  361. this.inertialRadiusOffset *= this.inertia;
  362. if (Math.abs(this.inertialAlphaOffset) < Epsilon)
  363. this.inertialAlphaOffset = 0;
  364. if (Math.abs(this.inertialBetaOffset) < Epsilon)
  365. this.inertialBetaOffset = 0;
  366. if (Math.abs(this.inertialRadiusOffset) < this.speed * Epsilon)
  367. this.inertialRadiusOffset = 0;
  368. }
  369. // Panning inertia
  370. if (this.inertialPanningX !== 0 || this.inertialPanningY !== 0) {
  371. if (!this._localDirection) {
  372. this._localDirection = Vector3.Zero();
  373. this._transformedDirection = Vector3.Zero();
  374. }
  375. this._localDirection.copyFromFloats(this.inertialPanningX, this.inertialPanningY, this.inertialPanningY);
  376. this._localDirection.multiplyInPlace(this.panningAxis);
  377. this._viewMatrix.invertToRef(this._cameraTransformMatrix);
  378. Vector3.TransformNormalToRef(this._localDirection, this._cameraTransformMatrix, this._transformedDirection);
  379. //Eliminate y if map panning is enabled (panningAxis == 1,0,1)
  380. if (!this.panningAxis.y) {
  381. this._transformedDirection.y = 0;
  382. }
  383. if (!this._targetHost) {
  384. if (this.panningDistanceLimit) {
  385. this._transformedDirection.addInPlace(this._target);
  386. var distanceSquared = Vector3.DistanceSquared(this._transformedDirection, this.panningOriginTarget);
  387. if (distanceSquared <= (this.panningDistanceLimit * this.panningDistanceLimit)) {
  388. this._target.copyFrom(this._transformedDirection);
  389. }
  390. }
  391. else {
  392. this._target.addInPlace(this._transformedDirection);
  393. }
  394. }
  395. this.inertialPanningX *= this.panningInertia;
  396. this.inertialPanningY *= this.panningInertia;
  397. if (Math.abs(this.inertialPanningX) < this.speed * Epsilon)
  398. this.inertialPanningX = 0;
  399. if (Math.abs(this.inertialPanningY) < this.speed * Epsilon)
  400. this.inertialPanningY = 0;
  401. }
  402. // Limits
  403. this._checkLimits();
  404. super._checkInputs();
  405. }
  406. protected _checkLimits() {
  407. if (this.lowerBetaLimit === null || this.lowerBetaLimit === undefined) {
  408. if (this.allowUpsideDown && this.beta > Math.PI) {
  409. this.beta = this.beta - (2 * Math.PI);
  410. }
  411. } else {
  412. if (this.beta < this.lowerBetaLimit) {
  413. this.beta = this.lowerBetaLimit;
  414. }
  415. }
  416. if (this.upperBetaLimit === null || this.upperBetaLimit === undefined) {
  417. if (this.allowUpsideDown && this.beta < -Math.PI) {
  418. this.beta = this.beta + (2 * Math.PI);
  419. }
  420. } else {
  421. if (this.beta > this.upperBetaLimit) {
  422. this.beta = this.upperBetaLimit;
  423. }
  424. }
  425. if (this.lowerAlphaLimit && this.alpha < this.lowerAlphaLimit) {
  426. this.alpha = this.lowerAlphaLimit;
  427. }
  428. if (this.upperAlphaLimit && this.alpha > this.upperAlphaLimit) {
  429. this.alpha = this.upperAlphaLimit;
  430. }
  431. if (this.lowerRadiusLimit && this.radius < this.lowerRadiusLimit) {
  432. this.radius = this.lowerRadiusLimit;
  433. }
  434. if (this.upperRadiusLimit && this.radius > this.upperRadiusLimit) {
  435. this.radius = this.upperRadiusLimit;
  436. }
  437. }
  438. public rebuildAnglesAndRadius() {
  439. var radiusv3 = this.position.subtract(this._getTargetPosition());
  440. this.radius = radiusv3.length();
  441. if (this.radius === 0) {
  442. this.radius = 0.0001; // Just to avoid division by zero
  443. }
  444. // Alpha
  445. this.alpha = Math.acos(radiusv3.x / Math.sqrt(Math.pow(radiusv3.x, 2) + Math.pow(radiusv3.z, 2)));
  446. if (radiusv3.z < 0) {
  447. this.alpha = 2 * Math.PI - this.alpha;
  448. }
  449. // Beta
  450. this.beta = Math.acos(radiusv3.y / this.radius);
  451. this._checkLimits();
  452. }
  453. public setPosition(position: Vector3): void {
  454. if (this.position.equals(position)) {
  455. return;
  456. }
  457. this.position.copyFrom(position);
  458. this.rebuildAnglesAndRadius();
  459. }
  460. public setTarget(target: AbstractMesh | Vector3, toBoundingCenter = false, allowSamePosition = false): void {
  461. if ((<any>target).getBoundingInfo){
  462. if (toBoundingCenter){
  463. this._targetBoundingCenter = (<any>target).getBoundingInfo().boundingBox.centerWorld.clone();
  464. } else {
  465. this._targetBoundingCenter = null;
  466. }
  467. this._targetHost = <AbstractMesh>target;
  468. this._target = this._getTargetPosition();
  469. this.onMeshTargetChangedObservable.notifyObservers(this._targetHost);
  470. } else {
  471. var newTarget = <Vector3>target;
  472. var currentTarget = this._getTargetPosition();
  473. if (currentTarget && !allowSamePosition && currentTarget.equals(newTarget)) {
  474. return;
  475. }
  476. this._targetHost = null;
  477. this._target = newTarget;
  478. this._targetBoundingCenter = null;
  479. this.onMeshTargetChangedObservable.notifyObservers(null);
  480. }
  481. this.rebuildAnglesAndRadius();
  482. }
  483. public _getViewMatrix(): Matrix {
  484. // Compute
  485. var cosa = Math.cos(this.alpha);
  486. var sina = Math.sin(this.alpha);
  487. var cosb = Math.cos(this.beta);
  488. var sinb = Math.sin(this.beta);
  489. if (sinb === 0) {
  490. sinb = 0.0001;
  491. }
  492. var target = this._getTargetPosition();
  493. target.addToRef(new Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this._newPosition);
  494. if (this.getScene().collisionsEnabled && this.checkCollisions) {
  495. if (!this._collider) {
  496. this._collider = new Collider();
  497. }
  498. this._collider.radius = this.collisionRadius;
  499. this._newPosition.subtractToRef(this.position, this._collisionVelocity);
  500. this._collisionTriggered = true;
  501. this.getScene().collisionCoordinator.getNewPosition(this.position, this._collisionVelocity, this._collider, 3, null, this._onCollisionPositionChange, this.uniqueId);
  502. } else {
  503. this.position.copyFrom(this._newPosition);
  504. var up = this.upVector;
  505. if (this.allowUpsideDown && sinb < 0) {
  506. up = up.clone();
  507. up = up.negate();
  508. }
  509. if (this.getScene().useRightHandedSystem) {
  510. Matrix.LookAtRHToRef(this.position, target, up, this._viewMatrix);
  511. } else {
  512. Matrix.LookAtLHToRef(this.position, target, up, this._viewMatrix);
  513. }
  514. this._viewMatrix.m[12] += this.targetScreenOffset.x;
  515. this._viewMatrix.m[13] += this.targetScreenOffset.y;
  516. }
  517. this._currentTarget = target;
  518. return this._viewMatrix;
  519. }
  520. protected _onCollisionPositionChange = (collisionId: number, newPosition: Vector3, collidedMesh: AbstractMesh = null) => {
  521. if (this.getScene().workerCollisions && this.checkCollisions) {
  522. newPosition.multiplyInPlace(this._collider.radius);
  523. }
  524. if (!collidedMesh) {
  525. this._previousPosition.copyFrom(this.position);
  526. } else {
  527. this.setPosition(newPosition);
  528. if (this.onCollide) {
  529. this.onCollide(collidedMesh);
  530. }
  531. }
  532. // Recompute because of constraints
  533. var cosa = Math.cos(this.alpha);
  534. var sina = Math.sin(this.alpha);
  535. var cosb = Math.cos(this.beta);
  536. var sinb = Math.sin(this.beta);
  537. if (sinb === 0) {
  538. sinb = 0.0001;
  539. }
  540. var target = this._getTargetPosition();
  541. target.addToRef(new Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this._newPosition);
  542. this.position.copyFrom(this._newPosition);
  543. var up = this.upVector;
  544. if (this.allowUpsideDown && this.beta < 0) {
  545. up = up.clone();
  546. up = up.negate();
  547. }
  548. Matrix.LookAtLHToRef(this.position, target, up, this._viewMatrix);
  549. this._viewMatrix.m[12] += this.targetScreenOffset.x;
  550. this._viewMatrix.m[13] += this.targetScreenOffset.y;
  551. this._collisionTriggered = false;
  552. }
  553. public zoomOn(meshes?: AbstractMesh[], doNotUpdateMaxZ = false): void {
  554. meshes = meshes || this.getScene().meshes;
  555. var minMaxVector = Mesh.MinMax(meshes);
  556. var distance = Vector3.Distance(minMaxVector.min, minMaxVector.max);
  557. this.radius = distance * this.zoomOnFactor;
  558. this.focusOn({ min: minMaxVector.min, max: minMaxVector.max, distance: distance }, doNotUpdateMaxZ);
  559. }
  560. public focusOn(meshesOrMinMaxVectorAndDistance, doNotUpdateMaxZ = false): void {
  561. var meshesOrMinMaxVector;
  562. var distance;
  563. if (meshesOrMinMaxVectorAndDistance.min === undefined) { // meshes
  564. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance || this.getScene().meshes;
  565. meshesOrMinMaxVector = Mesh.MinMax(meshesOrMinMaxVector);
  566. distance = Vector3.Distance(meshesOrMinMaxVector.min, meshesOrMinMaxVector.max);
  567. }
  568. else { //minMaxVector and distance
  569. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance;
  570. distance = meshesOrMinMaxVectorAndDistance.distance;
  571. }
  572. this._target = Mesh.Center(meshesOrMinMaxVector);
  573. if (!doNotUpdateMaxZ) {
  574. this.maxZ = distance * 2;
  575. }
  576. }
  577. /**
  578. * @override
  579. * Override Camera.createRigCamera
  580. */
  581. public createRigCamera(name: string, cameraIndex: number): Camera {
  582. var alphaShift : number;
  583. switch (this.cameraRigMode) {
  584. case Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  585. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  586. case Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  587. case Camera.RIG_MODE_VR:
  588. alphaShift = this._cameraRigParams.stereoHalfAngle * (cameraIndex === 0 ? 1 : -1);
  589. break;
  590. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  591. alphaShift = this._cameraRigParams.stereoHalfAngle * (cameraIndex === 0 ? -1 : 1);
  592. break;
  593. }
  594. var rigCam = new ArcRotateCamera(name, this.alpha + alphaShift, this.beta, this.radius, this._target, this.getScene());
  595. rigCam._cameraRigParams = {};
  596. return rigCam;
  597. }
  598. /**
  599. * @override
  600. * Override Camera._updateRigCameras
  601. */
  602. public _updateRigCameras() {
  603. var camLeft = <ArcRotateCamera>this._rigCameras[0];
  604. var camRight = <ArcRotateCamera>this._rigCameras[1];
  605. camLeft.beta = camRight.beta = this.beta;
  606. camLeft.radius = camRight.radius = this.radius;
  607. switch (this.cameraRigMode) {
  608. case Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  609. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  610. case Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  611. case Camera.RIG_MODE_VR:
  612. camLeft.alpha = this.alpha - this._cameraRigParams.stereoHalfAngle;
  613. camRight.alpha = this.alpha + this._cameraRigParams.stereoHalfAngle;
  614. break;
  615. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  616. camLeft.alpha = this.alpha + this._cameraRigParams.stereoHalfAngle;
  617. camRight.alpha = this.alpha - this._cameraRigParams.stereoHalfAngle;
  618. break;
  619. }
  620. super._updateRigCameras();
  621. }
  622. public dispose(): void {
  623. this.inputs.clear();
  624. super.dispose();
  625. }
  626. public getClassName(): string {
  627. return "ArcRotateCamera";
  628. }
  629. }
  630. }