camera.ts 47 KB

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