camera.ts 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. import { serialize, SerializationHelper, serializeAsVector3 } from "../Misc/decorators";
  2. import { SmartArray } from "../Misc/smartArray";
  3. import { Tools } from "../Misc/tools";
  4. import { Observable } from "../Misc/observable";
  5. import { Nullable } from "../types";
  6. import { CameraInputsManager } from "./cameraInputsManager";
  7. import { Scene } from "../scene";
  8. import { Matrix, Vector3, Quaternion } from "../Maths/math.vector";
  9. import { Node } from "../node";
  10. import { Mesh } from "../Meshes/mesh";
  11. import { AbstractMesh } from "../Meshes/abstractMesh";
  12. import { ICullable } from "../Culling/boundingInfo";
  13. import { Logger } from "../Misc/logger";
  14. import { _TypeStore } from '../Misc/typeStore';
  15. import { _DevTools } from '../Misc/devTools';
  16. import { Viewport } from '../Maths/math.viewport';
  17. import { Frustum } from '../Maths/math.frustum';
  18. import { Plane } from '../Maths/math.plane';
  19. declare type PostProcess = import("../PostProcesses/postProcess").PostProcess;
  20. declare type RenderTargetTexture = import("../Materials/Textures/renderTargetTexture").RenderTargetTexture;
  21. declare type FreeCamera = import("./freeCamera").FreeCamera;
  22. declare type TargetCamera = import("./targetCamera").TargetCamera;
  23. declare type Ray = import("../Culling/ray").Ray;
  24. /**
  25. * This is the base class of all the camera used in the application.
  26. * @see https://doc.babylonjs.com/features/cameras
  27. */
  28. export class Camera extends Node {
  29. /** @hidden */
  30. public static _createDefaultParsedCamera = (name: string, scene: Scene): Camera => {
  31. throw _DevTools.WarnImport("UniversalCamera");
  32. }
  33. /**
  34. * This is the default projection mode used by the cameras.
  35. * It helps recreating a feeling of perspective and better appreciate depth.
  36. * This is the best way to simulate real life cameras.
  37. */
  38. public static readonly PERSPECTIVE_CAMERA = 0;
  39. /**
  40. * This helps creating camera with an orthographic mode.
  41. * Orthographic is commonly used in engineering as a means to produce object specifications that communicate dimensions unambiguously, each line of 1 unit length (cm, meter..whatever) will appear to have the same length everywhere on the drawing. This allows the drafter to dimension only a subset of lines and let the reader know that other lines of that length on the drawing are also that length in reality. Every parallel line in the drawing is also parallel in the object.
  42. */
  43. public static readonly ORTHOGRAPHIC_CAMERA = 1;
  44. /**
  45. * This is the default FOV mode for perspective cameras.
  46. * This setting aligns the upper and lower bounds of the viewport to the upper and lower bounds of the camera frustum.
  47. */
  48. public static readonly FOVMODE_VERTICAL_FIXED = 0;
  49. /**
  50. * This setting aligns the left and right bounds of the viewport to the left and right bounds of the camera frustum.
  51. */
  52. public static readonly FOVMODE_HORIZONTAL_FIXED = 1;
  53. /**
  54. * This specifies ther is no need for a camera rig.
  55. * Basically only one eye is rendered corresponding to the camera.
  56. */
  57. public static readonly RIG_MODE_NONE = 0;
  58. /**
  59. * Simulates a camera Rig with one blue eye and one red eye.
  60. * This can be use with 3d blue and red glasses.
  61. */
  62. public static readonly RIG_MODE_STEREOSCOPIC_ANAGLYPH = 10;
  63. /**
  64. * Defines that both eyes of the camera will be rendered side by side with a parallel target.
  65. */
  66. public static readonly RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL = 11;
  67. /**
  68. * Defines that both eyes of the camera will be rendered side by side with a none parallel target.
  69. */
  70. public static readonly RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED = 12;
  71. /**
  72. * Defines that both eyes of the camera will be rendered over under each other.
  73. */
  74. public static readonly RIG_MODE_STEREOSCOPIC_OVERUNDER = 13;
  75. /**
  76. * Defines that both eyes of the camera will be rendered on successive lines interlaced for passive 3d monitors.
  77. */
  78. public static readonly RIG_MODE_STEREOSCOPIC_INTERLACED = 14;
  79. /**
  80. * Defines that both eyes of the camera should be renderered in a VR mode (carbox).
  81. */
  82. public static readonly RIG_MODE_VR = 20;
  83. /**
  84. * Defines that both eyes of the camera should be renderered in a VR mode (webVR).
  85. */
  86. public static readonly RIG_MODE_WEBVR = 21;
  87. /**
  88. * Custom rig mode allowing rig cameras to be populated manually with any number of cameras
  89. */
  90. public static readonly RIG_MODE_CUSTOM = 22;
  91. /**
  92. * Defines if by default attaching controls should prevent the default javascript event to continue.
  93. */
  94. public static ForceAttachControlToAlwaysPreventDefault = false;
  95. /**
  96. * Define the input manager associated with the camera.
  97. */
  98. public inputs: CameraInputsManager<Camera>;
  99. /** @hidden */
  100. @serializeAsVector3("position")
  101. public _position = Vector3.Zero();
  102. /**
  103. * Define the current local position of the camera in the scene
  104. */
  105. public get position(): Vector3 {
  106. return this._position;
  107. }
  108. public set position(newPosition: Vector3) {
  109. this._position = newPosition;
  110. }
  111. /**
  112. * The vector the camera should consider as up.
  113. * (default is Vector3(0, 1, 0) aka Vector3.Up())
  114. */
  115. @serializeAsVector3()
  116. public upVector = Vector3.Up();
  117. /**
  118. * Define the current limit on the left side for an orthographic camera
  119. * In scene unit
  120. */
  121. @serialize()
  122. public orthoLeft: Nullable<number> = null;
  123. /**
  124. * Define the current limit on the right side for an orthographic camera
  125. * In scene unit
  126. */
  127. @serialize()
  128. public orthoRight: Nullable<number> = null;
  129. /**
  130. * Define the current limit on the bottom side for an orthographic camera
  131. * In scene unit
  132. */
  133. @serialize()
  134. public orthoBottom: Nullable<number> = null;
  135. /**
  136. * Define the current limit on the top side for an orthographic camera
  137. * In scene unit
  138. */
  139. @serialize()
  140. public orthoTop: Nullable<number> = null;
  141. /**
  142. * Field Of View is set in Radians. (default is 0.8)
  143. */
  144. @serialize()
  145. public fov = 0.8;
  146. /**
  147. * Define the minimum distance the camera can see from.
  148. * This is important to note that the depth buffer are not infinite and the closer it starts
  149. * the more your scene might encounter depth fighting issue.
  150. */
  151. @serialize()
  152. public minZ = 1;
  153. /**
  154. * Define the maximum distance the camera can see to.
  155. * This is important to note that the depth buffer are not infinite and the further it end
  156. * the more your scene might encounter depth fighting issue.
  157. */
  158. @serialize()
  159. public maxZ = 10000.0;
  160. /**
  161. * Define the default inertia of the camera.
  162. * This helps giving a smooth feeling to the camera movement.
  163. */
  164. @serialize()
  165. public inertia = 0.9;
  166. /**
  167. * Define the mode of the camera (Camera.PERSPECTIVE_CAMERA or Camera.ORTHOGRAPHIC_CAMERA)
  168. */
  169. @serialize()
  170. public mode = Camera.PERSPECTIVE_CAMERA;
  171. /**
  172. * Define whether the camera is intermediate.
  173. * This is useful to not present the output directly to the screen in case of rig without post process for instance
  174. */
  175. public isIntermediate = false;
  176. /**
  177. * Define the viewport of the camera.
  178. * This correspond to the portion of the screen the camera will render to in normalized 0 to 1 unit.
  179. */
  180. public viewport = new Viewport(0, 0, 1.0, 1.0);
  181. /**
  182. * Restricts the camera to viewing objects with the same layerMask.
  183. * A camera with a layerMask of 1 will render mesh.layerMask & camera.layerMask!== 0
  184. */
  185. @serialize()
  186. public layerMask: number = 0x0FFFFFFF;
  187. /**
  188. * fovMode sets the camera frustum bounds to the viewport bounds. (default is FOVMODE_VERTICAL_FIXED)
  189. */
  190. @serialize()
  191. public fovMode: number = Camera.FOVMODE_VERTICAL_FIXED;
  192. /**
  193. * Rig mode of the camera.
  194. * This is useful to create the camera with two "eyes" instead of one to create VR or stereoscopic scenes.
  195. * This is normally controlled byt the camera themselves as internal use.
  196. */
  197. @serialize()
  198. public cameraRigMode = Camera.RIG_MODE_NONE;
  199. /**
  200. * Defines the distance between both "eyes" in case of a RIG
  201. */
  202. @serialize()
  203. public interaxialDistance: number;
  204. /**
  205. * Defines if stereoscopic rendering is done side by side or over under.
  206. */
  207. @serialize()
  208. public isStereoscopicSideBySide: boolean;
  209. /**
  210. * Defines the list of custom render target which are rendered to and then used as the input to this camera's render. Eg. display another camera view on a TV in the main scene
  211. * This is pretty helpfull if you wish to make a camera render to a texture you could reuse somewhere
  212. * else in the scene. (Eg. security camera)
  213. *
  214. * To change the final output target of the camera, camera.outputRenderTarget should be used instead (eg. webXR renders to a render target corrisponding to an HMD)
  215. */
  216. public customRenderTargets = new Array<RenderTargetTexture>();
  217. /**
  218. * When set, the camera will render to this render target instead of the default canvas
  219. *
  220. * If the desire is to use the output of a camera as a texture in the scene consider using camera.customRenderTargets instead
  221. */
  222. public outputRenderTarget: Nullable<RenderTargetTexture> = null;
  223. /**
  224. * Observable triggered when the camera view matrix has changed.
  225. */
  226. public onViewMatrixChangedObservable = new Observable<Camera>();
  227. /**
  228. * Observable triggered when the camera Projection matrix has changed.
  229. */
  230. public onProjectionMatrixChangedObservable = new Observable<Camera>();
  231. /**
  232. * Observable triggered when the inputs have been processed.
  233. */
  234. public onAfterCheckInputsObservable = new Observable<Camera>();
  235. /**
  236. * Observable triggered when reset has been called and applied to the camera.
  237. */
  238. public onRestoreStateObservable = new Observable<Camera>();
  239. /**
  240. * Is this camera a part of a rig system?
  241. */
  242. public isRigCamera: boolean = false;
  243. /**
  244. * If isRigCamera set to true this will be set with the parent camera.
  245. * The parent camera is not (!) necessarily the .parent of this camera (like in the case of XR)
  246. */
  247. public rigParent?: Camera;
  248. /** @hidden */
  249. public _cameraRigParams: any;
  250. /** @hidden */
  251. public _rigCameras = new Array<Camera>();
  252. /** @hidden */
  253. public _rigPostProcess: Nullable<PostProcess>;
  254. protected _webvrViewMatrix = Matrix.Identity();
  255. /** @hidden */
  256. public _skipRendering = false;
  257. /** @hidden */
  258. public _projectionMatrix = new Matrix();
  259. /** @hidden */
  260. public _postProcesses = new Array<Nullable<PostProcess>>();
  261. /** @hidden */
  262. public _activeMeshes = new SmartArray<AbstractMesh>(256);
  263. protected _globalPosition = Vector3.Zero();
  264. /** @hidden */
  265. public _computedViewMatrix = Matrix.Identity();
  266. private _doNotComputeProjectionMatrix = false;
  267. private _transformMatrix = Matrix.Zero();
  268. private _frustumPlanes: Plane[];
  269. private _refreshFrustumPlanes = true;
  270. private _storedFov: number;
  271. private _stateStored: boolean;
  272. /**
  273. * Instantiates a new camera object.
  274. * This should not be used directly but through the inherited cameras: ArcRotate, Free...
  275. * @see https://doc.babylonjs.com/features/cameras
  276. * @param name Defines the name of the camera in the scene
  277. * @param position Defines the position of the camera
  278. * @param scene Defines the scene the camera belongs too
  279. * @param setActiveOnSceneIfNoneActive Defines if the camera should be set as active after creation if no other camera have been defined in the scene
  280. */
  281. constructor(name: string, position: Vector3, scene: Scene, setActiveOnSceneIfNoneActive = true) {
  282. super(name, scene);
  283. this.getScene().addCamera(this);
  284. if (setActiveOnSceneIfNoneActive && !this.getScene().activeCamera) {
  285. this.getScene().activeCamera = this;
  286. }
  287. this.position = position;
  288. }
  289. /**
  290. * Store current camera state (fov, position, etc..)
  291. * @returns the camera
  292. */
  293. public storeState(): Camera {
  294. this._stateStored = true;
  295. this._storedFov = this.fov;
  296. return this;
  297. }
  298. /**
  299. * Restores the camera state values if it has been stored. You must call storeState() first
  300. */
  301. protected _restoreStateValues(): boolean {
  302. if (!this._stateStored) {
  303. return false;
  304. }
  305. this.fov = this._storedFov;
  306. return true;
  307. }
  308. /**
  309. * Restored camera state. You must call storeState() first.
  310. * @returns true if restored and false otherwise
  311. */
  312. public restoreState(): boolean {
  313. if (this._restoreStateValues()) {
  314. this.onRestoreStateObservable.notifyObservers(this);
  315. return true;
  316. }
  317. return false;
  318. }
  319. /**
  320. * Gets the class name of the camera.
  321. * @returns the class name
  322. */
  323. public getClassName(): string {
  324. return "Camera";
  325. }
  326. /** @hidden */
  327. public readonly _isCamera = true;
  328. /**
  329. * Gets a string representation of the camera useful for debug purpose.
  330. * @param fullDetails Defines that a more verboe level of logging is required
  331. * @returns the string representation
  332. */
  333. public toString(fullDetails?: boolean): string {
  334. var ret = "Name: " + this.name;
  335. ret += ", type: " + this.getClassName();
  336. if (this.animations) {
  337. for (var i = 0; i < this.animations.length; i++) {
  338. ret += ", animation[0]: " + this.animations[i].toString(fullDetails);
  339. }
  340. }
  341. if (fullDetails) {
  342. }
  343. return ret;
  344. }
  345. /**
  346. * Gets the current world space position of the camera.
  347. */
  348. public get globalPosition(): Vector3 {
  349. return this._globalPosition;
  350. }
  351. /**
  352. * Gets the list of active meshes this frame (meshes no culled or excluded by lod s in the frame)
  353. * @returns the active meshe list
  354. */
  355. public getActiveMeshes(): SmartArray<AbstractMesh> {
  356. return this._activeMeshes;
  357. }
  358. /**
  359. * Check whether a mesh is part of the current active mesh list of the camera
  360. * @param mesh Defines the mesh to check
  361. * @returns true if active, false otherwise
  362. */
  363. public isActiveMesh(mesh: Mesh): boolean {
  364. return (this._activeMeshes.indexOf(mesh) !== -1);
  365. }
  366. /**
  367. * Is this camera ready to be used/rendered
  368. * @param completeCheck defines if a complete check (including post processes) has to be done (false by default)
  369. * @return true if the camera is ready
  370. */
  371. public isReady(completeCheck = false): boolean {
  372. if (completeCheck) {
  373. for (var pp of this._postProcesses) {
  374. if (pp && !pp.isReady()) {
  375. return false;
  376. }
  377. }
  378. }
  379. return super.isReady(completeCheck);
  380. }
  381. /** @hidden */
  382. public _initCache() {
  383. super._initCache();
  384. this._cache.position = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  385. this._cache.upVector = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  386. this._cache.mode = undefined;
  387. this._cache.minZ = undefined;
  388. this._cache.maxZ = undefined;
  389. this._cache.fov = undefined;
  390. this._cache.fovMode = undefined;
  391. this._cache.aspectRatio = undefined;
  392. this._cache.orthoLeft = undefined;
  393. this._cache.orthoRight = undefined;
  394. this._cache.orthoBottom = undefined;
  395. this._cache.orthoTop = undefined;
  396. this._cache.renderWidth = undefined;
  397. this._cache.renderHeight = undefined;
  398. }
  399. /** @hidden */
  400. public _updateCache(ignoreParentClass?: boolean): void {
  401. if (!ignoreParentClass) {
  402. super._updateCache();
  403. }
  404. this._cache.position.copyFrom(this.position);
  405. this._cache.upVector.copyFrom(this.upVector);
  406. }
  407. /** @hidden */
  408. public _isSynchronized(): boolean {
  409. return this._isSynchronizedViewMatrix() && this._isSynchronizedProjectionMatrix();
  410. }
  411. /** @hidden */
  412. public _isSynchronizedViewMatrix(): boolean {
  413. if (!super._isSynchronized()) {
  414. return false;
  415. }
  416. return this._cache.position.equals(this.position)
  417. && this._cache.upVector.equals(this.upVector)
  418. && this.isSynchronizedWithParent();
  419. }
  420. /** @hidden */
  421. public _isSynchronizedProjectionMatrix(): boolean {
  422. var check = this._cache.mode === this.mode
  423. && this._cache.minZ === this.minZ
  424. && this._cache.maxZ === this.maxZ;
  425. if (!check) {
  426. return false;
  427. }
  428. var engine = this.getEngine();
  429. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  430. check = this._cache.fov === this.fov
  431. && this._cache.fovMode === this.fovMode
  432. && this._cache.aspectRatio === engine.getAspectRatio(this);
  433. }
  434. else {
  435. check = this._cache.orthoLeft === this.orthoLeft
  436. && this._cache.orthoRight === this.orthoRight
  437. && this._cache.orthoBottom === this.orthoBottom
  438. && this._cache.orthoTop === this.orthoTop
  439. && this._cache.renderWidth === engine.getRenderWidth()
  440. && this._cache.renderHeight === engine.getRenderHeight();
  441. }
  442. return check;
  443. }
  444. /**
  445. * Attach the input controls to a specific dom element to get the input from.
  446. * @param element Defines the element the controls should be listened from
  447. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  448. */
  449. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  450. }
  451. /**
  452. * Detach the current controls from the specified dom element.
  453. * @param element Defines the element to stop listening the inputs from
  454. */
  455. public detachControl(element: HTMLElement): void {
  456. }
  457. /**
  458. * Update the camera state according to the different inputs gathered during the frame.
  459. */
  460. public update(): void {
  461. this._checkInputs();
  462. if (this.cameraRigMode !== Camera.RIG_MODE_NONE) {
  463. this._updateRigCameras();
  464. }
  465. }
  466. /** @hidden */
  467. public _checkInputs(): void {
  468. this.onAfterCheckInputsObservable.notifyObservers(this);
  469. }
  470. /** @hidden */
  471. public get rigCameras(): Camera[] {
  472. return this._rigCameras;
  473. }
  474. /**
  475. * Gets the post process used by the rig cameras
  476. */
  477. public get rigPostProcess(): Nullable<PostProcess> {
  478. return this._rigPostProcess;
  479. }
  480. /**
  481. * Internal, gets the first post proces.
  482. * @returns the first post process to be run on this camera.
  483. */
  484. public _getFirstPostProcess(): Nullable<PostProcess> {
  485. for (var ppIndex = 0; ppIndex < this._postProcesses.length; ppIndex++) {
  486. if (this._postProcesses[ppIndex] !== null) {
  487. return this._postProcesses[ppIndex];
  488. }
  489. }
  490. return null;
  491. }
  492. private _cascadePostProcessesToRigCams(): void {
  493. // invalidate framebuffer
  494. var firstPostProcess = this._getFirstPostProcess();
  495. if (firstPostProcess) {
  496. firstPostProcess.markTextureDirty();
  497. }
  498. // glue the rigPostProcess to the end of the user postprocesses & assign to each sub-camera
  499. for (var i = 0, len = this._rigCameras.length; i < len; i++) {
  500. var cam = this._rigCameras[i];
  501. var rigPostProcess = cam._rigPostProcess;
  502. // for VR rig, there does not have to be a post process
  503. if (rigPostProcess) {
  504. var isPass = rigPostProcess.getEffectName() === "pass";
  505. if (isPass) {
  506. // any rig which has a PassPostProcess for rig[0], cannot be isIntermediate when there are also user postProcesses
  507. cam.isIntermediate = this._postProcesses.length === 0;
  508. }
  509. cam._postProcesses = this._postProcesses.slice(0).concat(rigPostProcess);
  510. rigPostProcess.markTextureDirty();
  511. } else {
  512. cam._postProcesses = this._postProcesses.slice(0);
  513. }
  514. }
  515. }
  516. /**
  517. * Attach a post process to the camera.
  518. * @see https://doc.babylonjs.com/how_to/how_to_use_postprocesses#attach-postprocess
  519. * @param postProcess The post process to attach to the camera
  520. * @param insertAt The position of the post process in case several of them are in use in the scene
  521. * @returns the position the post process has been inserted at
  522. */
  523. public attachPostProcess(postProcess: PostProcess, insertAt: Nullable<number> = null): number {
  524. if (!postProcess.isReusable() && this._postProcesses.indexOf(postProcess) > -1) {
  525. Logger.Error("You're trying to reuse a post process not defined as reusable.");
  526. return 0;
  527. }
  528. if (insertAt == null || insertAt < 0) {
  529. this._postProcesses.push(postProcess);
  530. } else if (this._postProcesses[insertAt] === null) {
  531. this._postProcesses[insertAt] = postProcess;
  532. } else {
  533. this._postProcesses.splice(insertAt, 0, postProcess);
  534. }
  535. this._cascadePostProcessesToRigCams(); // also ensures framebuffer invalidated
  536. return this._postProcesses.indexOf(postProcess);
  537. }
  538. /**
  539. * Detach a post process to the camera.
  540. * @see https://doc.babylonjs.com/how_to/how_to_use_postprocesses#attach-postprocess
  541. * @param postProcess The post process to detach from the camera
  542. */
  543. public detachPostProcess(postProcess: PostProcess): void {
  544. var idx = this._postProcesses.indexOf(postProcess);
  545. if (idx !== -1) {
  546. this._postProcesses[idx] = null;
  547. }
  548. this._cascadePostProcessesToRigCams(); // also ensures framebuffer invalidated
  549. }
  550. /**
  551. * Gets the current world matrix of the camera
  552. */
  553. public getWorldMatrix(): Matrix {
  554. if (this._isSynchronizedViewMatrix()) {
  555. return this._worldMatrix;
  556. }
  557. // Getting the the view matrix will also compute the world matrix.
  558. this.getViewMatrix();
  559. return this._worldMatrix;
  560. }
  561. /** @hidden */
  562. public _getViewMatrix(): Matrix {
  563. return Matrix.Identity();
  564. }
  565. /**
  566. * Gets the current view matrix of the camera.
  567. * @param force forces the camera to recompute the matrix without looking at the cached state
  568. * @returns the view matrix
  569. */
  570. public getViewMatrix(force?: boolean): Matrix {
  571. if (!force && this._isSynchronizedViewMatrix()) {
  572. return this._computedViewMatrix;
  573. }
  574. this.updateCache();
  575. this._computedViewMatrix = this._getViewMatrix();
  576. this._currentRenderId = this.getScene().getRenderId();
  577. this._childUpdateId++;
  578. this._refreshFrustumPlanes = true;
  579. if (this._cameraRigParams && this._cameraRigParams.vrPreViewMatrix) {
  580. this._computedViewMatrix.multiplyToRef(this._cameraRigParams.vrPreViewMatrix, this._computedViewMatrix);
  581. }
  582. // Notify parent camera if rig camera is changed
  583. if (this.parent && (this.parent as Camera).onViewMatrixChangedObservable) {
  584. (this.parent as Camera).onViewMatrixChangedObservable.notifyObservers((this.parent as Camera));
  585. }
  586. this.onViewMatrixChangedObservable.notifyObservers(this);
  587. this._computedViewMatrix.invertToRef(this._worldMatrix);
  588. return this._computedViewMatrix;
  589. }
  590. /**
  591. * Freeze the projection matrix.
  592. * It will prevent the cache check of the camera projection compute and can speed up perf
  593. * if no parameter of the camera are meant to change
  594. * @param projection Defines manually a projection if necessary
  595. */
  596. public freezeProjectionMatrix(projection?: Matrix): void {
  597. this._doNotComputeProjectionMatrix = true;
  598. if (projection !== undefined) {
  599. this._projectionMatrix = projection;
  600. }
  601. }
  602. /**
  603. * Unfreeze the projection matrix if it has previously been freezed by freezeProjectionMatrix.
  604. */
  605. public unfreezeProjectionMatrix(): void {
  606. this._doNotComputeProjectionMatrix = false;
  607. }
  608. /**
  609. * Gets the current projection matrix of the camera.
  610. * @param force forces the camera to recompute the matrix without looking at the cached state
  611. * @returns the projection matrix
  612. */
  613. public getProjectionMatrix(force?: boolean): Matrix {
  614. if (this._doNotComputeProjectionMatrix || (!force && this._isSynchronizedProjectionMatrix())) {
  615. return this._projectionMatrix;
  616. }
  617. // Cache
  618. this._cache.mode = this.mode;
  619. this._cache.minZ = this.minZ;
  620. this._cache.maxZ = this.maxZ;
  621. // Matrix
  622. this._refreshFrustumPlanes = true;
  623. var engine = this.getEngine();
  624. var scene = this.getScene();
  625. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  626. this._cache.fov = this.fov;
  627. this._cache.fovMode = this.fovMode;
  628. this._cache.aspectRatio = engine.getAspectRatio(this);
  629. if (this.minZ <= 0) {
  630. this.minZ = 0.1;
  631. }
  632. const reverseDepth = engine.useReverseDepthBuffer;
  633. let getProjectionMatrix: (fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed: boolean) => void;
  634. if (scene.useRightHandedSystem) {
  635. getProjectionMatrix = reverseDepth ? Matrix.PerspectiveFovReverseRHToRef : Matrix.PerspectiveFovRHToRef;
  636. } else {
  637. getProjectionMatrix = reverseDepth ? Matrix.PerspectiveFovReverseLHToRef : Matrix.PerspectiveFovLHToRef;
  638. }
  639. getProjectionMatrix(this.fov,
  640. engine.getAspectRatio(this),
  641. this.minZ,
  642. this.maxZ,
  643. this._projectionMatrix,
  644. this.fovMode === Camera.FOVMODE_VERTICAL_FIXED);
  645. } else {
  646. var halfWidth = engine.getRenderWidth() / 2.0;
  647. var halfHeight = engine.getRenderHeight() / 2.0;
  648. if (scene.useRightHandedSystem) {
  649. Matrix.OrthoOffCenterRHToRef(this.orthoLeft ?? -halfWidth,
  650. this.orthoRight ?? halfWidth,
  651. this.orthoBottom ?? -halfHeight,
  652. this.orthoTop ?? halfHeight,
  653. this.minZ,
  654. this.maxZ,
  655. this._projectionMatrix);
  656. } else {
  657. Matrix.OrthoOffCenterLHToRef(this.orthoLeft ?? -halfWidth,
  658. this.orthoRight ?? halfWidth,
  659. this.orthoBottom ?? -halfHeight,
  660. this.orthoTop ?? halfHeight,
  661. this.minZ,
  662. this.maxZ,
  663. this._projectionMatrix);
  664. }
  665. this._cache.orthoLeft = this.orthoLeft;
  666. this._cache.orthoRight = this.orthoRight;
  667. this._cache.orthoBottom = this.orthoBottom;
  668. this._cache.orthoTop = this.orthoTop;
  669. this._cache.renderWidth = engine.getRenderWidth();
  670. this._cache.renderHeight = engine.getRenderHeight();
  671. }
  672. this.onProjectionMatrixChangedObservable.notifyObservers(this);
  673. return this._projectionMatrix;
  674. }
  675. /**
  676. * Gets the transformation matrix (ie. the multiplication of view by projection matrices)
  677. * @returns a Matrix
  678. */
  679. public getTransformationMatrix(): Matrix {
  680. this._computedViewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  681. return this._transformMatrix;
  682. }
  683. private _updateFrustumPlanes(): void {
  684. if (!this._refreshFrustumPlanes) {
  685. return;
  686. }
  687. this.getTransformationMatrix();
  688. if (!this._frustumPlanes) {
  689. this._frustumPlanes = Frustum.GetPlanes(this._transformMatrix);
  690. } else {
  691. Frustum.GetPlanesToRef(this._transformMatrix, this._frustumPlanes);
  692. }
  693. this._refreshFrustumPlanes = false;
  694. }
  695. /**
  696. * Checks if a cullable object (mesh...) is in the camera frustum
  697. * This checks the bounding box center. See isCompletelyInFrustum for a full bounding check
  698. * @param target The object to check
  699. * @param checkRigCameras If the rig cameras should be checked (eg. with webVR camera both eyes should be checked) (Default: false)
  700. * @returns true if the object is in frustum otherwise false
  701. */
  702. public isInFrustum(target: ICullable, checkRigCameras = false): boolean {
  703. this._updateFrustumPlanes();
  704. if (checkRigCameras && this.rigCameras.length > 0) {
  705. var result = false;
  706. this.rigCameras.forEach((cam) => {
  707. cam._updateFrustumPlanes();
  708. result = result || target.isInFrustum(cam._frustumPlanes);
  709. });
  710. return result;
  711. } else {
  712. return target.isInFrustum(this._frustumPlanes);
  713. }
  714. }
  715. /**
  716. * Checks if a cullable object (mesh...) is in the camera frustum
  717. * Unlike isInFrustum this cheks the full bounding box
  718. * @param target The object to check
  719. * @returns true if the object is in frustum otherwise false
  720. */
  721. public isCompletelyInFrustum(target: ICullable): boolean {
  722. this._updateFrustumPlanes();
  723. return target.isCompletelyInFrustum(this._frustumPlanes);
  724. }
  725. /**
  726. * Gets a ray in the forward direction from the camera.
  727. * @param length Defines the length of the ray to create
  728. * @param transform Defines the transform to apply to the ray, by default the world matrx is used to create a workd space ray
  729. * @param origin Defines the start point of the ray which defaults to the camera position
  730. * @returns the forward ray
  731. */
  732. public getForwardRay(length = 100, transform?: Matrix, origin?: Vector3): Ray {
  733. throw _DevTools.WarnImport("Ray");
  734. }
  735. /**
  736. * Releases resources associated with this node.
  737. * @param doNotRecurse Set to true to not recurse into each children (recurse into each children by default)
  738. * @param disposeMaterialAndTextures Set to true to also dispose referenced materials and textures (false by default)
  739. */
  740. public dispose(doNotRecurse?: boolean, disposeMaterialAndTextures = false): void {
  741. // Observables
  742. this.onViewMatrixChangedObservable.clear();
  743. this.onProjectionMatrixChangedObservable.clear();
  744. this.onAfterCheckInputsObservable.clear();
  745. this.onRestoreStateObservable.clear();
  746. // Inputs
  747. if (this.inputs) {
  748. this.inputs.clear();
  749. }
  750. // Animations
  751. this.getScene().stopAnimation(this);
  752. // Remove from scene
  753. this.getScene().removeCamera(this);
  754. while (this._rigCameras.length > 0) {
  755. let camera = this._rigCameras.pop();
  756. if (camera) {
  757. camera.dispose();
  758. }
  759. }
  760. // Postprocesses
  761. if (this._rigPostProcess) {
  762. this._rigPostProcess.dispose(this);
  763. this._rigPostProcess = null;
  764. this._postProcesses = [];
  765. }
  766. else if (this.cameraRigMode !== Camera.RIG_MODE_NONE) {
  767. this._rigPostProcess = null;
  768. this._postProcesses = [];
  769. } else {
  770. var i = this._postProcesses.length;
  771. while (--i >= 0) {
  772. var postProcess = this._postProcesses[i];
  773. if (postProcess) {
  774. postProcess.dispose(this);
  775. }
  776. }
  777. }
  778. // Render targets
  779. var i = this.customRenderTargets.length;
  780. while (--i >= 0) {
  781. this.customRenderTargets[i].dispose();
  782. }
  783. this.customRenderTargets = [];
  784. // Active Meshes
  785. this._activeMeshes.dispose();
  786. super.dispose(doNotRecurse, disposeMaterialAndTextures);
  787. }
  788. /** @hidden */
  789. public _isLeftCamera = false;
  790. /**
  791. * Gets the left camera of a rig setup in case of Rigged Camera
  792. */
  793. public get isLeftCamera(): boolean {
  794. return this._isLeftCamera;
  795. }
  796. /** @hidden */
  797. public _isRightCamera = false;
  798. /**
  799. * Gets the right camera of a rig setup in case of Rigged Camera
  800. */
  801. public get isRightCamera(): boolean {
  802. return this._isRightCamera;
  803. }
  804. /**
  805. * Gets the left camera of a rig setup in case of Rigged Camera
  806. */
  807. public get leftCamera(): Nullable<FreeCamera> {
  808. if (this._rigCameras.length < 1) {
  809. return null;
  810. }
  811. return (<FreeCamera>this._rigCameras[0]);
  812. }
  813. /**
  814. * Gets the right camera of a rig setup in case of Rigged Camera
  815. */
  816. public get rightCamera(): Nullable<FreeCamera> {
  817. if (this._rigCameras.length < 2) {
  818. return null;
  819. }
  820. return (<FreeCamera>this._rigCameras[1]);
  821. }
  822. /**
  823. * Gets the left camera target of a rig setup in case of Rigged Camera
  824. * @returns the target position
  825. */
  826. public getLeftTarget(): Nullable<Vector3> {
  827. if (this._rigCameras.length < 1) {
  828. return null;
  829. }
  830. return (<TargetCamera>this._rigCameras[0]).getTarget();
  831. }
  832. /**
  833. * Gets the right camera target of a rig setup in case of Rigged Camera
  834. * @returns the target position
  835. */
  836. public getRightTarget(): Nullable<Vector3> {
  837. if (this._rigCameras.length < 2) {
  838. return null;
  839. }
  840. return (<TargetCamera>this._rigCameras[1]).getTarget();
  841. }
  842. /**
  843. * @hidden
  844. */
  845. public setCameraRigMode(mode: number, rigParams: any): void {
  846. if (this.cameraRigMode === mode) {
  847. return;
  848. }
  849. while (this._rigCameras.length > 0) {
  850. let camera = this._rigCameras.pop();
  851. if (camera) {
  852. camera.dispose();
  853. }
  854. }
  855. this.cameraRigMode = mode;
  856. this._cameraRigParams = {};
  857. //we have to implement stereo camera calcultating left and right viewpoints from interaxialDistance and target,
  858. //not from a given angle as it is now, but until that complete code rewriting provisional stereoHalfAngle value is introduced
  859. this._cameraRigParams.interaxialDistance = rigParams.interaxialDistance || 0.0637;
  860. this._cameraRigParams.stereoHalfAngle = Tools.ToRadians(this._cameraRigParams.interaxialDistance / 0.0637);
  861. // create the rig cameras, unless none
  862. if (this.cameraRigMode !== Camera.RIG_MODE_NONE) {
  863. let leftCamera = this.createRigCamera(this.name + "_L", 0);
  864. if (leftCamera) {
  865. leftCamera._isLeftCamera = true;
  866. }
  867. let rightCamera = this.createRigCamera(this.name + "_R", 1);
  868. if (rightCamera) {
  869. rightCamera._isRightCamera = true;
  870. }
  871. if (leftCamera && rightCamera) {
  872. this._rigCameras.push(leftCamera);
  873. this._rigCameras.push(rightCamera);
  874. }
  875. }
  876. switch (this.cameraRigMode) {
  877. case Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  878. Camera._setStereoscopicAnaglyphRigMode(this);
  879. break;
  880. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  881. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  882. case Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  883. case Camera.RIG_MODE_STEREOSCOPIC_INTERLACED:
  884. Camera._setStereoscopicRigMode(this);
  885. break;
  886. case Camera.RIG_MODE_VR:
  887. Camera._setVRRigMode(this, rigParams);
  888. break;
  889. case Camera.RIG_MODE_WEBVR:
  890. Camera._setWebVRRigMode(this, rigParams);
  891. break;
  892. }
  893. this._cascadePostProcessesToRigCams();
  894. this.update();
  895. }
  896. /** @hidden */
  897. public static _setStereoscopicRigMode(camera: Camera) {
  898. throw "Import Cameras/RigModes/stereoscopicRigMode before using stereoscopic rig mode";
  899. }
  900. /** @hidden */
  901. public static _setStereoscopicAnaglyphRigMode(camera: Camera) {
  902. throw "Import Cameras/RigModes/stereoscopicAnaglyphRigMode before using stereoscopic anaglyph rig mode";
  903. }
  904. /** @hidden */
  905. public static _setVRRigMode(camera: Camera, rigParams: any) {
  906. throw "Import Cameras/RigModes/vrRigMode before using VR rig mode";
  907. }
  908. /** @hidden */
  909. public static _setWebVRRigMode(camera: Camera, rigParams: any) {
  910. throw "Import Cameras/RigModes/WebVRRigMode before using Web VR rig mode";
  911. }
  912. /** @hidden */
  913. public _getVRProjectionMatrix(): Matrix {
  914. Matrix.PerspectiveFovLHToRef(this._cameraRigParams.vrMetrics.aspectRatioFov, this._cameraRigParams.vrMetrics.aspectRatio, this.minZ, this.maxZ, this._cameraRigParams.vrWorkMatrix);
  915. this._cameraRigParams.vrWorkMatrix.multiplyToRef(this._cameraRigParams.vrHMatrix, this._projectionMatrix);
  916. return this._projectionMatrix;
  917. }
  918. protected _updateCameraRotationMatrix() {
  919. //Here for WebVR
  920. }
  921. protected _updateWebVRCameraRotationMatrix() {
  922. //Here for WebVR
  923. }
  924. /**
  925. * This function MUST be overwritten by the different WebVR cameras available.
  926. * The context in which it is running is the RIG camera. So 'this' is the TargetCamera, left or right.
  927. * @hidden
  928. */
  929. public _getWebVRProjectionMatrix(): Matrix {
  930. return Matrix.Identity();
  931. }
  932. /**
  933. * This function MUST be overwritten by the different WebVR cameras available.
  934. * The context in which it is running is the RIG camera. So 'this' is the TargetCamera, left or right.
  935. * @hidden
  936. */
  937. public _getWebVRViewMatrix(): Matrix {
  938. return Matrix.Identity();
  939. }
  940. /** @hidden */
  941. public setCameraRigParameter(name: string, value: any) {
  942. if (!this._cameraRigParams) {
  943. this._cameraRigParams = {};
  944. }
  945. this._cameraRigParams[name] = value;
  946. //provisionnally:
  947. if (name === "interaxialDistance") {
  948. this._cameraRigParams.stereoHalfAngle = Tools.ToRadians(value / 0.0637);
  949. }
  950. }
  951. /**
  952. * needs to be overridden by children so sub has required properties to be copied
  953. * @hidden
  954. */
  955. public createRigCamera(name: string, cameraIndex: number): Nullable<Camera> {
  956. return null;
  957. }
  958. /**
  959. * May need to be overridden by children
  960. * @hidden
  961. */
  962. public _updateRigCameras() {
  963. for (var i = 0; i < this._rigCameras.length; i++) {
  964. this._rigCameras[i].minZ = this.minZ;
  965. this._rigCameras[i].maxZ = this.maxZ;
  966. this._rigCameras[i].fov = this.fov;
  967. this._rigCameras[i].upVector.copyFrom(this.upVector);
  968. }
  969. // only update viewport when ANAGLYPH
  970. if (this.cameraRigMode === Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH) {
  971. this._rigCameras[0].viewport = this._rigCameras[1].viewport = this.viewport;
  972. }
  973. }
  974. /** @hidden */
  975. public _setupInputs() {
  976. }
  977. /**
  978. * Serialiaze the camera setup to a json represention
  979. * @returns the JSON representation
  980. */
  981. public serialize(): any {
  982. var serializationObject = SerializationHelper.Serialize(this);
  983. // Type
  984. serializationObject.type = this.getClassName();
  985. // Parent
  986. if (this.parent) {
  987. serializationObject.parentId = this.parent.id;
  988. }
  989. if (this.inputs) {
  990. this.inputs.serialize(serializationObject);
  991. }
  992. // Animations
  993. SerializationHelper.AppendSerializedAnimations(this, serializationObject);
  994. serializationObject.ranges = this.serializeAnimationRanges();
  995. return serializationObject;
  996. }
  997. /**
  998. * Clones the current camera.
  999. * @param name The cloned camera name
  1000. * @returns the cloned camera
  1001. */
  1002. public clone(name: string): Camera {
  1003. return SerializationHelper.Clone(Camera.GetConstructorFromName(this.getClassName(), name, this.getScene(), this.interaxialDistance, this.isStereoscopicSideBySide), this);
  1004. }
  1005. /**
  1006. * Gets the direction of the camera relative to a given local axis.
  1007. * @param localAxis Defines the reference axis to provide a relative direction.
  1008. * @return the direction
  1009. */
  1010. public getDirection(localAxis: Vector3): Vector3 {
  1011. var result = Vector3.Zero();
  1012. this.getDirectionToRef(localAxis, result);
  1013. return result;
  1014. }
  1015. /**
  1016. * Returns the current camera absolute rotation
  1017. */
  1018. public get absoluteRotation(): Quaternion {
  1019. var result = Quaternion.Zero();
  1020. this.getWorldMatrix().decompose(undefined, result);
  1021. return result;
  1022. }
  1023. /**
  1024. * Gets the direction of the camera relative to a given local axis into a passed vector.
  1025. * @param localAxis Defines the reference axis to provide a relative direction.
  1026. * @param result Defines the vector to store the result in
  1027. */
  1028. public getDirectionToRef(localAxis: Vector3, result: Vector3): void {
  1029. Vector3.TransformNormalToRef(localAxis, this.getWorldMatrix(), result);
  1030. }
  1031. /**
  1032. * Gets a camera constructor for a given camera type
  1033. * @param type The type of the camera to construct (should be equal to one of the camera class name)
  1034. * @param name The name of the camera the result will be able to instantiate
  1035. * @param scene The scene the result will construct the camera in
  1036. * @param interaxial_distance In case of stereoscopic setup, the distance between both eyes
  1037. * @param isStereoscopicSideBySide In case of stereoscopic setup, should the sereo be side b side
  1038. * @returns a factory method to construc the camera
  1039. */
  1040. static GetConstructorFromName(type: string, name: string, scene: Scene, interaxial_distance: number = 0, isStereoscopicSideBySide: boolean = true): () => Camera {
  1041. let constructorFunc = Node.Construct(type, name, scene, {
  1042. interaxial_distance: interaxial_distance,
  1043. isStereoscopicSideBySide: isStereoscopicSideBySide
  1044. });
  1045. if (constructorFunc) {
  1046. return <() => Camera>constructorFunc;
  1047. }
  1048. // Default to universal camera
  1049. return () => Camera._createDefaultParsedCamera(name, scene);
  1050. }
  1051. /**
  1052. * Compute the world matrix of the camera.
  1053. * @returns the camera world matrix
  1054. */
  1055. public computeWorldMatrix(): Matrix {
  1056. return this.getWorldMatrix();
  1057. }
  1058. /**
  1059. * Parse a JSON and creates the camera from the parsed information
  1060. * @param parsedCamera The JSON to parse
  1061. * @param scene The scene to instantiate the camera in
  1062. * @returns the newly constructed camera
  1063. */
  1064. public static Parse(parsedCamera: any, scene: Scene): Camera {
  1065. var type = parsedCamera.type;
  1066. var construct = Camera.GetConstructorFromName(type, parsedCamera.name, scene, parsedCamera.interaxial_distance, parsedCamera.isStereoscopicSideBySide);
  1067. var camera = SerializationHelper.Parse(construct, parsedCamera, scene);
  1068. // Parent
  1069. if (parsedCamera.parentId) {
  1070. camera._waitingParentId = parsedCamera.parentId;
  1071. }
  1072. //If camera has an input manager, let it parse inputs settings
  1073. if (camera.inputs) {
  1074. camera.inputs.parse(parsedCamera);
  1075. camera._setupInputs();
  1076. }
  1077. if ((<any>camera).setPosition) { // need to force position
  1078. camera.position.copyFromFloats(0, 0, 0);
  1079. (<any>camera).setPosition(Vector3.FromArray(parsedCamera.position));
  1080. }
  1081. // Target
  1082. if (parsedCamera.target) {
  1083. if ((<any>camera).setTarget) {
  1084. (<any>camera).setTarget(Vector3.FromArray(parsedCamera.target));
  1085. }
  1086. }
  1087. // Apply 3d rig, when found
  1088. if (parsedCamera.cameraRigMode) {
  1089. var rigParams = (parsedCamera.interaxial_distance) ? { interaxialDistance: parsedCamera.interaxial_distance } : {};
  1090. camera.setCameraRigMode(parsedCamera.cameraRigMode, rigParams);
  1091. }
  1092. // Animations
  1093. if (parsedCamera.animations) {
  1094. for (var animationIndex = 0; animationIndex < parsedCamera.animations.length; animationIndex++) {
  1095. var parsedAnimation = parsedCamera.animations[animationIndex];
  1096. const internalClass = _TypeStore.GetClass("BABYLON.Animation");
  1097. if (internalClass) {
  1098. camera.animations.push(internalClass.Parse(parsedAnimation));
  1099. }
  1100. }
  1101. Node.ParseAnimationRanges(camera, parsedCamera, scene);
  1102. }
  1103. if (parsedCamera.autoAnimate) {
  1104. scene.beginAnimation(camera, parsedCamera.autoAnimateFrom, parsedCamera.autoAnimateTo, parsedCamera.autoAnimateLoop, parsedCamera.autoAnimateSpeed || 1.0);
  1105. }
  1106. return camera;
  1107. }
  1108. }