defaultRenderingPipeline.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. import { Nullable } from "../../../types";
  2. import { serialize, SerializationHelper } from "../../../Misc/decorators";
  3. import { Observer } from "../../../Misc/observable";
  4. import { IAnimatable } from "../../../Misc/tools";
  5. import { Logger } from "../../../Misc/logger";
  6. import { Camera } from "../../../Cameras/camera";
  7. import { ImageProcessingConfiguration } from "../../../Materials/imageProcessingConfiguration";
  8. import { Texture } from "../../../Materials/Textures/texture";
  9. import { Engine } from "../../../Engines/engine";
  10. import { Constants } from "../../../Engines/constants";
  11. import { IDisposable } from "../../../scene";
  12. import { GlowLayer } from "../../../Layers/glowLayer";
  13. import { Scene } from "../../../scene";
  14. import { PostProcess } from "../../../PostProcesses/postProcess";
  15. import { SharpenPostProcess } from "../../../PostProcesses/sharpenPostProcess";
  16. import { ImageProcessingPostProcess } from "../../../PostProcesses/imageProcessingPostProcess";
  17. import { ChromaticAberrationPostProcess } from "../../../PostProcesses/chromaticAberrationPostProcess";
  18. import { GrainPostProcess } from "../../../PostProcesses/grainPostProcess";
  19. import { FxaaPostProcess } from "../../../PostProcesses/fxaaPostProcess";
  20. import { PostProcessRenderPipeline } from "../../../PostProcesses/RenderPipeline/postProcessRenderPipeline";
  21. import { PostProcessRenderEffect } from "../../../PostProcesses/RenderPipeline/postProcessRenderEffect";
  22. import { DepthOfFieldEffect, DepthOfFieldEffectBlurLevel } from "../../../PostProcesses/depthOfFieldEffect";
  23. import { BloomEffect } from "../../../PostProcesses/bloomEffect";
  24. import { _TypeStore } from '../../../Misc/typeStore';
  25. import { EngineStore } from "../../../Engines/engineStore";
  26. import "../../../PostProcesses/RenderPipeline/postProcessRenderPipelineManagerSceneComponent";
  27. declare type Animation = import("../../../Animations/animation").Animation;
  28. /**
  29. * The default rendering pipeline can be added to a scene to apply common post processing effects such as anti-aliasing or depth of field.
  30. * See https://doc.babylonjs.com/how_to/using_default_rendering_pipeline
  31. */
  32. export class DefaultRenderingPipeline extends PostProcessRenderPipeline implements IDisposable, IAnimatable {
  33. private _scene: Scene;
  34. private _camerasToBeAttached: Array<Camera> = [];
  35. /**
  36. * ID of the sharpen post process,
  37. */
  38. private readonly SharpenPostProcessId: string = "SharpenPostProcessEffect";
  39. /**
  40. * @ignore
  41. * ID of the image processing post process;
  42. */
  43. readonly ImageProcessingPostProcessId: string = "ImageProcessingPostProcessEffect";
  44. /**
  45. * @ignore
  46. * ID of the Fast Approximate Anti-Aliasing post process;
  47. */
  48. readonly FxaaPostProcessId: string = "FxaaPostProcessEffect";
  49. /**
  50. * ID of the chromatic aberration post process,
  51. */
  52. private readonly ChromaticAberrationPostProcessId: string = "ChromaticAberrationPostProcessEffect";
  53. /**
  54. * ID of the grain post process
  55. */
  56. private readonly GrainPostProcessId: string = "GrainPostProcessEffect";
  57. // Post-processes
  58. /**
  59. * Sharpen post process which will apply a sharpen convolution to enhance edges
  60. */
  61. public sharpen: SharpenPostProcess;
  62. private _sharpenEffect: PostProcessRenderEffect;
  63. private bloom: BloomEffect;
  64. /**
  65. * Depth of field effect, applies a blur based on how far away objects are from the focus distance.
  66. */
  67. public depthOfField: DepthOfFieldEffect;
  68. /**
  69. * The Fast Approximate Anti-Aliasing post process which attemps to remove aliasing from an image.
  70. */
  71. public fxaa: FxaaPostProcess;
  72. /**
  73. * Image post processing pass used to perform operations such as tone mapping or color grading.
  74. */
  75. public imageProcessing: ImageProcessingPostProcess;
  76. /**
  77. * Chromatic aberration post process which will shift rgb colors in the image
  78. */
  79. public chromaticAberration: ChromaticAberrationPostProcess;
  80. private _chromaticAberrationEffect: PostProcessRenderEffect;
  81. /**
  82. * Grain post process which add noise to the image
  83. */
  84. public grain: GrainPostProcess;
  85. private _grainEffect: PostProcessRenderEffect;
  86. /**
  87. * Glow post process which adds a glow to emissive areas of the image
  88. */
  89. private _glowLayer: Nullable<GlowLayer> = null;
  90. /**
  91. * Animations which can be used to tweak settings over a period of time
  92. */
  93. public animations: Animation[] = [];
  94. private _imageProcessingConfigurationObserver: Nullable<Observer<ImageProcessingConfiguration>> = null;
  95. // Values
  96. private _sharpenEnabled: boolean = false;
  97. private _bloomEnabled: boolean = false;
  98. private _depthOfFieldEnabled: boolean = false;
  99. private _depthOfFieldBlurLevel = DepthOfFieldEffectBlurLevel.Low;
  100. private _fxaaEnabled: boolean = false;
  101. private _imageProcessingEnabled: boolean = true;
  102. private _defaultPipelineTextureType: number;
  103. private _bloomScale: number = 0.5;
  104. private _chromaticAberrationEnabled: boolean = false;
  105. private _grainEnabled: boolean = false;
  106. private _buildAllowed = true;
  107. /**
  108. * Gets active scene
  109. */
  110. public get scene(): Scene {
  111. return this._scene;
  112. }
  113. /**
  114. * Enable or disable the sharpen process from the pipeline
  115. */
  116. public set sharpenEnabled(enabled: boolean) {
  117. if (this._sharpenEnabled === enabled) {
  118. return;
  119. }
  120. this._sharpenEnabled = enabled;
  121. this._buildPipeline();
  122. }
  123. @serialize()
  124. public get sharpenEnabled(): boolean {
  125. return this._sharpenEnabled;
  126. }
  127. private _resizeObserver: Nullable<Observer<Engine>> = null;
  128. private _hardwareScaleLevel = 1.0;
  129. private _bloomKernel: number = 64;
  130. /**
  131. * Specifies the size of the bloom blur kernel, relative to the final output size
  132. */
  133. @serialize()
  134. public get bloomKernel(): number {
  135. return this._bloomKernel;
  136. }
  137. public set bloomKernel(value: number) {
  138. this._bloomKernel = value;
  139. this.bloom.kernel = value / this._hardwareScaleLevel;
  140. }
  141. /**
  142. * Specifies the weight of the bloom in the final rendering
  143. */
  144. @serialize()
  145. private _bloomWeight: number = 0.15;
  146. /**
  147. * Specifies the luma threshold for the area that will be blurred by the bloom
  148. */
  149. @serialize()
  150. private _bloomThreshold: number = 0.9;
  151. @serialize()
  152. private _hdr: boolean;
  153. /**
  154. * The strength of the bloom.
  155. */
  156. public set bloomWeight(value: number) {
  157. if (this._bloomWeight === value) {
  158. return;
  159. }
  160. this.bloom.weight = value;
  161. this._bloomWeight = value;
  162. }
  163. @serialize()
  164. public get bloomWeight(): number {
  165. return this._bloomWeight;
  166. }
  167. /**
  168. * The strength of the bloom.
  169. */
  170. public set bloomThreshold(value: number) {
  171. if (this._bloomThreshold === value) {
  172. return;
  173. }
  174. this.bloom.threshold = value;
  175. this._bloomThreshold = value;
  176. }
  177. @serialize()
  178. public get bloomThreshold(): number {
  179. return this._bloomThreshold;
  180. }
  181. /**
  182. * The scale of the bloom, lower value will provide better performance.
  183. */
  184. public set bloomScale(value: number) {
  185. if (this._bloomScale === value) {
  186. return;
  187. }
  188. this._bloomScale = value;
  189. // recreate bloom and dispose old as this setting is not dynamic
  190. this._rebuildBloom();
  191. this._buildPipeline();
  192. }
  193. @serialize()
  194. public get bloomScale(): number {
  195. return this._bloomScale;
  196. }
  197. /**
  198. * Enable or disable the bloom from the pipeline
  199. */
  200. public set bloomEnabled(enabled: boolean) {
  201. if (this._bloomEnabled === enabled) {
  202. return;
  203. }
  204. this._bloomEnabled = enabled;
  205. this._buildPipeline();
  206. }
  207. @serialize()
  208. public get bloomEnabled(): boolean {
  209. return this._bloomEnabled;
  210. }
  211. private _rebuildBloom() {
  212. // recreate bloom and dispose old as this setting is not dynamic
  213. var oldBloom = this.bloom;
  214. this.bloom = new BloomEffect(this._scene, this.bloomScale, this._bloomWeight, this.bloomKernel, this._defaultPipelineTextureType, false);
  215. this.bloom.threshold = oldBloom.threshold;
  216. for (var i = 0; i < this._cameras.length; i++) {
  217. oldBloom.disposeEffects(this._cameras[i]);
  218. }
  219. }
  220. /**
  221. * If the depth of field is enabled.
  222. */
  223. @serialize()
  224. public get depthOfFieldEnabled(): boolean {
  225. return this._depthOfFieldEnabled;
  226. }
  227. public set depthOfFieldEnabled(enabled: boolean) {
  228. if (this._depthOfFieldEnabled === enabled) {
  229. return;
  230. }
  231. this._depthOfFieldEnabled = enabled;
  232. this._buildPipeline();
  233. }
  234. /**
  235. * Blur level of the depth of field effect. (Higher blur will effect performance)
  236. */
  237. @serialize()
  238. public get depthOfFieldBlurLevel(): DepthOfFieldEffectBlurLevel {
  239. return this._depthOfFieldBlurLevel;
  240. }
  241. public set depthOfFieldBlurLevel(value: DepthOfFieldEffectBlurLevel) {
  242. if (this._depthOfFieldBlurLevel === value) {
  243. return;
  244. }
  245. this._depthOfFieldBlurLevel = value;
  246. // recreate dof and dispose old as this setting is not dynamic
  247. var oldDof = this.depthOfField;
  248. this.depthOfField = new DepthOfFieldEffect(this._scene, null, this._depthOfFieldBlurLevel, this._defaultPipelineTextureType, false);
  249. this.depthOfField.focalLength = oldDof.focalLength;
  250. this.depthOfField.focusDistance = oldDof.focusDistance;
  251. this.depthOfField.fStop = oldDof.fStop;
  252. this.depthOfField.lensSize = oldDof.lensSize;
  253. for (var i = 0; i < this._cameras.length; i++) {
  254. oldDof.disposeEffects(this._cameras[i]);
  255. }
  256. this._buildPipeline();
  257. }
  258. /**
  259. * If the anti aliasing is enabled.
  260. */
  261. public set fxaaEnabled(enabled: boolean) {
  262. if (this._fxaaEnabled === enabled) {
  263. return;
  264. }
  265. this._fxaaEnabled = enabled;
  266. this._buildPipeline();
  267. }
  268. @serialize()
  269. public get fxaaEnabled(): boolean {
  270. return this._fxaaEnabled;
  271. }
  272. private _samples = 1;
  273. /**
  274. * MSAA sample count, setting this to 4 will provide 4x anti aliasing. (default: 1)
  275. */
  276. public set samples(sampleCount: number) {
  277. if (this._samples === sampleCount) {
  278. return;
  279. }
  280. this._samples = sampleCount;
  281. this._buildPipeline();
  282. }
  283. @serialize()
  284. public get samples(): number {
  285. return this._samples;
  286. }
  287. /**
  288. * If image processing is enabled.
  289. */
  290. public set imageProcessingEnabled(enabled: boolean) {
  291. if (this._imageProcessingEnabled === enabled) {
  292. return;
  293. }
  294. this._imageProcessingEnabled = enabled;
  295. this._buildPipeline();
  296. }
  297. @serialize()
  298. public get imageProcessingEnabled(): boolean {
  299. return this._imageProcessingEnabled;
  300. }
  301. /**
  302. * If glow layer is enabled. (Adds a glow effect to emmissive materials)
  303. */
  304. public set glowLayerEnabled(enabled: boolean) {
  305. if (enabled && !this._glowLayer) {
  306. this._glowLayer = new GlowLayer("", this._scene);
  307. } else if (!enabled && this._glowLayer) {
  308. this._glowLayer.dispose();
  309. this._glowLayer = null;
  310. }
  311. }
  312. @serialize()
  313. public get glowLayerEnabled(): boolean {
  314. return this._glowLayer != null;
  315. }
  316. /**
  317. * Gets the glow layer (or null if not defined)
  318. */
  319. public get glowLayer() {
  320. return this._glowLayer;
  321. }
  322. /**
  323. * Enable or disable the chromaticAberration process from the pipeline
  324. */
  325. public set chromaticAberrationEnabled(enabled: boolean) {
  326. if (this._chromaticAberrationEnabled === enabled) {
  327. return;
  328. }
  329. this._chromaticAberrationEnabled = enabled;
  330. this._buildPipeline();
  331. }
  332. @serialize()
  333. public get chromaticAberrationEnabled(): boolean {
  334. return this._chromaticAberrationEnabled;
  335. }
  336. /**
  337. * Enable or disable the grain process from the pipeline
  338. */
  339. public set grainEnabled(enabled: boolean) {
  340. if (this._grainEnabled === enabled) {
  341. return;
  342. }
  343. this._grainEnabled = enabled;
  344. this._buildPipeline();
  345. }
  346. @serialize()
  347. public get grainEnabled(): boolean {
  348. return this._grainEnabled;
  349. }
  350. /**
  351. * @constructor
  352. * @param name - The rendering pipeline name (default: "")
  353. * @param hdr - If high dynamic range textures should be used (default: true)
  354. * @param scene - The scene linked to this pipeline (default: the last created scene)
  355. * @param cameras - The array of cameras that the rendering pipeline will be attached to (default: scene.cameras)
  356. * @param automaticBuild - if false, you will have to manually call prepare() to update the pipeline (default: true)
  357. */
  358. constructor(name: string = "", hdr: boolean = true, scene: Scene = EngineStore.LastCreatedScene!, cameras?: Camera[], automaticBuild = true) {
  359. super(scene.getEngine(), name);
  360. this._cameras = cameras || scene.cameras;
  361. this._cameras = this._cameras.slice();
  362. this._camerasToBeAttached = this._cameras.slice();
  363. this._buildAllowed = automaticBuild;
  364. // Initialize
  365. this._scene = scene;
  366. var caps = this._scene.getEngine().getCaps();
  367. this._hdr = hdr && (caps.textureHalfFloatRender || caps.textureFloatRender);
  368. // Misc
  369. if (this._hdr) {
  370. if (caps.textureHalfFloatRender) {
  371. this._defaultPipelineTextureType = Constants.TEXTURETYPE_HALF_FLOAT;
  372. }
  373. else if (caps.textureFloatRender) {
  374. this._defaultPipelineTextureType = Constants.TEXTURETYPE_FLOAT;
  375. }
  376. } else {
  377. this._defaultPipelineTextureType = Constants.TEXTURETYPE_UNSIGNED_INT;
  378. }
  379. // Attach
  380. scene.postProcessRenderPipelineManager.addPipeline(this);
  381. var engine = this._scene.getEngine();
  382. // Create post processes before hand so they can be modified before enabled.
  383. // Block compilation flag is set to true to avoid compilation prior to use, these will be updated on first use in build pipeline.
  384. this.sharpen = new SharpenPostProcess("sharpen", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType, true);
  385. this._sharpenEffect = new PostProcessRenderEffect(engine, this.SharpenPostProcessId, () => { return this.sharpen; }, true);
  386. this.depthOfField = new DepthOfFieldEffect(this._scene, null, this._depthOfFieldBlurLevel, this._defaultPipelineTextureType, true);
  387. this.bloom = new BloomEffect(this._scene, this._bloomScale, this._bloomWeight, this.bloomKernel, this._defaultPipelineTextureType, true);
  388. this.chromaticAberration = new ChromaticAberrationPostProcess("ChromaticAberration", engine.getRenderWidth(), engine.getRenderHeight(), 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType, true);
  389. this._chromaticAberrationEffect = new PostProcessRenderEffect(engine, this.ChromaticAberrationPostProcessId, () => { return this.chromaticAberration; }, true);
  390. this.grain = new GrainPostProcess("Grain", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType, true);
  391. this._grainEffect = new PostProcessRenderEffect(engine, this.GrainPostProcessId, () => { return this.grain; }, true);
  392. this._resizeObserver = engine.onResizeObservable.add(() => {
  393. this._hardwareScaleLevel = engine.getHardwareScalingLevel();
  394. this.bloomKernel = this.bloomKernel;
  395. });
  396. this._imageProcessingConfigurationObserver = this._scene.imageProcessingConfiguration.onUpdateParameters.add(() => {
  397. this.bloom._downscale._exposure = this._scene.imageProcessingConfiguration.exposure;
  398. });
  399. this._buildPipeline();
  400. }
  401. /**
  402. * Get the class name
  403. * @returns "DefaultRenderingPipeline"
  404. */
  405. public getClassName(): string {
  406. return "DefaultRenderingPipeline";
  407. }
  408. /**
  409. * Force the compilation of the entire pipeline.
  410. */
  411. public prepare(): void {
  412. let previousState = this._buildAllowed;
  413. this._buildAllowed = true;
  414. this._buildPipeline();
  415. this._buildAllowed = previousState;
  416. }
  417. private _hasCleared = false;
  418. private _prevPostProcess: Nullable<PostProcess> = null;
  419. private _prevPrevPostProcess: Nullable<PostProcess> = null;
  420. private _setAutoClearAndTextureSharing(postProcess: PostProcess, skipTextureSharing = false) {
  421. if (this._hasCleared) {
  422. postProcess.autoClear = false;
  423. } else {
  424. postProcess.autoClear = true;
  425. this._scene.autoClear = false;
  426. this._hasCleared = true;
  427. }
  428. if (!skipTextureSharing) {
  429. if (this._prevPrevPostProcess) {
  430. postProcess.shareOutputWith(this._prevPrevPostProcess);
  431. } else {
  432. postProcess.useOwnOutput();
  433. }
  434. if (this._prevPostProcess) {
  435. this._prevPrevPostProcess = this._prevPostProcess;
  436. }
  437. this._prevPostProcess = postProcess;
  438. }
  439. }
  440. private _depthOfFieldSceneObserver: Nullable<Observer<Scene>> = null;
  441. private _buildPipeline() {
  442. if (!this._buildAllowed) {
  443. return;
  444. }
  445. this._scene.autoClear = true;
  446. var engine = this._scene.getEngine();
  447. this._disposePostProcesses();
  448. if (this._cameras !== null) {
  449. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._cameras);
  450. // get back cameras to be used to reattach pipeline
  451. this._cameras = this._camerasToBeAttached.slice();
  452. }
  453. this._reset();
  454. this._prevPostProcess = null;
  455. this._prevPrevPostProcess = null;
  456. this._hasCleared = false;
  457. if (this.depthOfFieldEnabled) {
  458. // Multi camera suport
  459. if (this._cameras.length > 1) {
  460. for (let camera of this._cameras) {
  461. const depthRenderer = this._scene.enableDepthRenderer(camera);
  462. depthRenderer.useOnlyInActiveCamera = true;
  463. }
  464. this._depthOfFieldSceneObserver = this._scene.onAfterRenderTargetsRenderObservable.add((scene) => {
  465. if (this._cameras.indexOf(scene.activeCamera!) > -1) {
  466. this.depthOfField.depthTexture = scene.enableDepthRenderer(scene.activeCamera).getDepthMap();
  467. }
  468. });
  469. }
  470. else {
  471. this._scene.onAfterRenderTargetsRenderObservable.remove(this._depthOfFieldSceneObserver);
  472. const depthRenderer = this._scene.enableDepthRenderer(this._cameras[0]);
  473. this.depthOfField.depthTexture = depthRenderer.getDepthMap();
  474. }
  475. if (!this.depthOfField._isReady()) {
  476. this.depthOfField._updateEffects();
  477. }
  478. this.addEffect(this.depthOfField);
  479. this._setAutoClearAndTextureSharing(this.depthOfField._effects[0], true);
  480. }
  481. else {
  482. this._scene.onAfterRenderTargetsRenderObservable.remove(this._depthOfFieldSceneObserver);
  483. }
  484. if (this.bloomEnabled) {
  485. if (!this.bloom._isReady()) {
  486. this.bloom._updateEffects();
  487. }
  488. this.addEffect(this.bloom);
  489. this._setAutoClearAndTextureSharing(this.bloom._effects[0], true);
  490. }
  491. if (this._imageProcessingEnabled) {
  492. this.imageProcessing = new ImageProcessingPostProcess("imageProcessing", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  493. if (this._hdr) {
  494. this.addEffect(new PostProcessRenderEffect(engine, this.ImageProcessingPostProcessId, () => { return this.imageProcessing; }, true));
  495. this._setAutoClearAndTextureSharing(this.imageProcessing);
  496. } else {
  497. this._scene.imageProcessingConfiguration.applyByPostProcess = false;
  498. }
  499. }
  500. if (this.sharpenEnabled) {
  501. if (!this.sharpen.isReady()) {
  502. this.sharpen.updateEffect();
  503. }
  504. this.addEffect(this._sharpenEffect);
  505. this._setAutoClearAndTextureSharing(this.sharpen);
  506. }
  507. if (this.grainEnabled) {
  508. if (!this.grain.isReady()) {
  509. this.grain.updateEffect();
  510. }
  511. this.addEffect(this._grainEffect);
  512. this._setAutoClearAndTextureSharing(this.grain);
  513. }
  514. if (this.chromaticAberrationEnabled) {
  515. if (!this.chromaticAberration.isReady()) {
  516. this.chromaticAberration.updateEffect();
  517. }
  518. this.addEffect(this._chromaticAberrationEffect);
  519. this._setAutoClearAndTextureSharing(this.chromaticAberration);
  520. }
  521. if (this.fxaaEnabled) {
  522. this.fxaa = new FxaaPostProcess("fxaa", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  523. this.addEffect(new PostProcessRenderEffect(engine, this.FxaaPostProcessId, () => { return this.fxaa; }, true));
  524. this._setAutoClearAndTextureSharing(this.fxaa, true);
  525. }
  526. if (this._cameras !== null) {
  527. this._scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(this._name, this._cameras);
  528. }
  529. // In multicamera mode, the scene needs to autoclear in between cameras.
  530. if (this._scene.activeCameras && this._scene.activeCameras.length > 1) {
  531. this._scene.autoClear = true;
  532. }
  533. if (!this._enableMSAAOnFirstPostProcess(this.samples) && this.samples > 1) {
  534. Logger.Warn("MSAA failed to enable, MSAA is only supported in browsers that support webGL >= 2.0");
  535. }
  536. }
  537. private _disposePostProcesses(disposeNonRecreated = false): void {
  538. for (var i = 0; i < this._cameras.length; i++) {
  539. var camera = this._cameras[i];
  540. if (this.imageProcessing) {
  541. this.imageProcessing.dispose(camera);
  542. }
  543. if (this.fxaa) {
  544. this.fxaa.dispose(camera);
  545. }
  546. // These are created in the constructor and should not be disposed on every pipeline change
  547. if (disposeNonRecreated) {
  548. if (this.sharpen) {
  549. this.sharpen.dispose(camera);
  550. }
  551. if (this.depthOfField) {
  552. this._scene.onAfterRenderTargetsRenderObservable.remove(this._depthOfFieldSceneObserver);
  553. this.depthOfField.disposeEffects(camera);
  554. }
  555. if (this.bloom) {
  556. this.bloom.disposeEffects(camera);
  557. }
  558. if (this.chromaticAberration) {
  559. this.chromaticAberration.dispose(camera);
  560. }
  561. if (this.grain) {
  562. this.grain.dispose(camera);
  563. }
  564. if (this._glowLayer) {
  565. this._glowLayer.dispose();
  566. }
  567. }
  568. }
  569. (<any>this.imageProcessing) = null;
  570. (<any>this.fxaa) = null;
  571. if (disposeNonRecreated) {
  572. (<any>this.sharpen) = null;
  573. (<any>this._sharpenEffect) = null;
  574. (<any>this.depthOfField) = null;
  575. (<any>this.bloom) = null;
  576. (<any>this.chromaticAberration) = null;
  577. (<any>this._chromaticAberrationEffect) = null;
  578. (<any>this.grain) = null;
  579. (<any>this._grainEffect) = null;
  580. this._glowLayer = null;
  581. }
  582. }
  583. /**
  584. * Adds a camera to the pipeline
  585. * @param camera the camera to be added
  586. */
  587. public addCamera(camera: Camera): void {
  588. this._camerasToBeAttached.push(camera);
  589. this._buildPipeline();
  590. }
  591. /**
  592. * Removes a camera from the pipeline
  593. * @param camera the camera to remove
  594. */
  595. public removeCamera(camera: Camera): void {
  596. var index = this._camerasToBeAttached.indexOf(camera);
  597. this._camerasToBeAttached.splice(index, 1);
  598. this._buildPipeline();
  599. }
  600. /**
  601. * Dispose of the pipeline and stop all post processes
  602. */
  603. public dispose(): void {
  604. this._disposePostProcesses(true);
  605. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._cameras);
  606. this._scene.autoClear = true;
  607. if (this._resizeObserver) {
  608. this._scene.getEngine().onResizeObservable.remove(this._resizeObserver);
  609. this._resizeObserver = null;
  610. }
  611. this._scene.imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingConfigurationObserver);
  612. super.dispose();
  613. }
  614. /**
  615. * Serialize the rendering pipeline (Used when exporting)
  616. * @returns the serialized object
  617. */
  618. public serialize(): any {
  619. var serializationObject = SerializationHelper.Serialize(this);
  620. serializationObject.customType = "DefaultRenderingPipeline";
  621. return serializationObject;
  622. }
  623. /**
  624. * Parse the serialized pipeline
  625. * @param source Source pipeline.
  626. * @param scene The scene to load the pipeline to.
  627. * @param rootUrl The URL of the serialized pipeline.
  628. * @returns An instantiated pipeline from the serialized object.
  629. */
  630. public static Parse(source: any, scene: Scene, rootUrl: string): DefaultRenderingPipeline {
  631. return SerializationHelper.Parse(() => new DefaultRenderingPipeline(source._name, source._name._hdr, scene), source, scene, rootUrl);
  632. }
  633. }
  634. _TypeStore.RegisteredTypes["BABYLON.DefaultRenderingPipeline"] = DefaultRenderingPipeline;