defaultRenderingPipeline.ts 27 KB

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