defaultRenderingPipeline.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. import { Nullable } from "../../../types";
  2. import { serialize, SerializationHelper } from "../../../Misc/decorators";
  3. import { Observer } from "../../../Misc/observable";
  4. import { IAnimatable } from '../../../Animations/animatable.interface';
  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._scene.imageProcessingConfiguration.isEnabled = enabled;
  295. }
  296. @serialize()
  297. public get imageProcessingEnabled(): boolean {
  298. return this._imageProcessingEnabled;
  299. }
  300. /**
  301. * If glow layer is enabled. (Adds a glow effect to emmissive materials)
  302. */
  303. public set glowLayerEnabled(enabled: boolean) {
  304. if (enabled && !this._glowLayer) {
  305. this._glowLayer = new GlowLayer("", this._scene);
  306. } else if (!enabled && this._glowLayer) {
  307. this._glowLayer.dispose();
  308. this._glowLayer = null;
  309. }
  310. }
  311. @serialize()
  312. public get glowLayerEnabled(): boolean {
  313. return this._glowLayer != null;
  314. }
  315. /**
  316. * Gets the glow layer (or null if not defined)
  317. */
  318. public get glowLayer() {
  319. return this._glowLayer;
  320. }
  321. /**
  322. * Enable or disable the chromaticAberration process from the pipeline
  323. */
  324. public set chromaticAberrationEnabled(enabled: boolean) {
  325. if (this._chromaticAberrationEnabled === enabled) {
  326. return;
  327. }
  328. this._chromaticAberrationEnabled = enabled;
  329. this._buildPipeline();
  330. }
  331. @serialize()
  332. public get chromaticAberrationEnabled(): boolean {
  333. return this._chromaticAberrationEnabled;
  334. }
  335. /**
  336. * Enable or disable the grain process from the pipeline
  337. */
  338. public set grainEnabled(enabled: boolean) {
  339. if (this._grainEnabled === enabled) {
  340. return;
  341. }
  342. this._grainEnabled = enabled;
  343. this._buildPipeline();
  344. }
  345. @serialize()
  346. public get grainEnabled(): boolean {
  347. return this._grainEnabled;
  348. }
  349. /**
  350. * @constructor
  351. * @param name - The rendering pipeline name (default: "")
  352. * @param hdr - If high dynamic range textures should be used (default: true)
  353. * @param scene - The scene linked to this pipeline (default: the last created scene)
  354. * @param cameras - The array of cameras that the rendering pipeline will be attached to (default: scene.cameras)
  355. * @param automaticBuild - if false, you will have to manually call prepare() to update the pipeline (default: true)
  356. */
  357. constructor(name: string = "", hdr: boolean = true, scene: Scene = EngineStore.LastCreatedScene!, cameras?: Camera[], automaticBuild = true) {
  358. super(scene.getEngine(), name);
  359. this._cameras = cameras || scene.cameras;
  360. this._cameras = this._cameras.slice();
  361. this._camerasToBeAttached = this._cameras.slice();
  362. this._buildAllowed = automaticBuild;
  363. // Initialize
  364. this._scene = scene;
  365. var caps = this._scene.getEngine().getCaps();
  366. this._hdr = hdr && (caps.textureHalfFloatRender || caps.textureFloatRender);
  367. // Misc
  368. if (this._hdr) {
  369. if (caps.textureHalfFloatRender) {
  370. this._defaultPipelineTextureType = Constants.TEXTURETYPE_HALF_FLOAT;
  371. }
  372. else if (caps.textureFloatRender) {
  373. this._defaultPipelineTextureType = Constants.TEXTURETYPE_FLOAT;
  374. }
  375. } else {
  376. this._defaultPipelineTextureType = Constants.TEXTURETYPE_UNSIGNED_INT;
  377. }
  378. // Attach
  379. scene.postProcessRenderPipelineManager.addPipeline(this);
  380. var engine = this._scene.getEngine();
  381. // Create post processes before hand so they can be modified before enabled.
  382. // Block compilation flag is set to true to avoid compilation prior to use, these will be updated on first use in build pipeline.
  383. this.sharpen = new SharpenPostProcess("sharpen", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType, true);
  384. this._sharpenEffect = new PostProcessRenderEffect(engine, this.SharpenPostProcessId, () => { return this.sharpen; }, true);
  385. this.depthOfField = new DepthOfFieldEffect(this._scene, null, this._depthOfFieldBlurLevel, this._defaultPipelineTextureType, true);
  386. this.bloom = new BloomEffect(this._scene, this._bloomScale, this._bloomWeight, this.bloomKernel, this._defaultPipelineTextureType, true);
  387. this.chromaticAberration = new ChromaticAberrationPostProcess("ChromaticAberration", engine.getRenderWidth(), engine.getRenderHeight(), 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType, true);
  388. this._chromaticAberrationEffect = new PostProcessRenderEffect(engine, this.ChromaticAberrationPostProcessId, () => { return this.chromaticAberration; }, true);
  389. this.grain = new GrainPostProcess("Grain", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType, true);
  390. this._grainEffect = new PostProcessRenderEffect(engine, this.GrainPostProcessId, () => { return this.grain; }, true);
  391. this._resizeObserver = engine.onResizeObservable.add(() => {
  392. this._hardwareScaleLevel = engine.getHardwareScalingLevel();
  393. this.bloomKernel = this.bloomKernel;
  394. });
  395. this._imageProcessingConfigurationObserver = this._scene.imageProcessingConfiguration.onUpdateParameters.add(() => {
  396. this.bloom._downscale._exposure = this._scene.imageProcessingConfiguration.exposure;
  397. if (this.imageProcessingEnabled !== this._scene.imageProcessingConfiguration.isEnabled) {
  398. this._imageProcessingEnabled = this._scene.imageProcessingConfiguration.isEnabled;
  399. this._buildPipeline();
  400. }
  401. });
  402. this._buildPipeline();
  403. }
  404. /**
  405. * Get the class name
  406. * @returns "DefaultRenderingPipeline"
  407. */
  408. public getClassName(): string {
  409. return "DefaultRenderingPipeline";
  410. }
  411. /**
  412. * Force the compilation of the entire pipeline.
  413. */
  414. public prepare(): void {
  415. let previousState = this._buildAllowed;
  416. this._buildAllowed = true;
  417. this._buildPipeline();
  418. this._buildAllowed = previousState;
  419. }
  420. private _hasCleared = false;
  421. private _prevPostProcess: Nullable<PostProcess> = null;
  422. private _prevPrevPostProcess: Nullable<PostProcess> = null;
  423. private _setAutoClearAndTextureSharing(postProcess: PostProcess, skipTextureSharing = false) {
  424. if (this._hasCleared) {
  425. postProcess.autoClear = false;
  426. } else {
  427. postProcess.autoClear = true;
  428. this._scene.autoClear = false;
  429. this._hasCleared = true;
  430. }
  431. if (!skipTextureSharing) {
  432. if (this._prevPrevPostProcess) {
  433. postProcess.shareOutputWith(this._prevPrevPostProcess);
  434. } else {
  435. postProcess.useOwnOutput();
  436. }
  437. if (this._prevPostProcess) {
  438. this._prevPrevPostProcess = this._prevPostProcess;
  439. }
  440. this._prevPostProcess = postProcess;
  441. }
  442. }
  443. private _depthOfFieldSceneObserver: Nullable<Observer<Scene>> = null;
  444. private _buildPipeline() {
  445. if (!this._buildAllowed) {
  446. return;
  447. }
  448. this._scene.autoClear = true;
  449. var engine = this._scene.getEngine();
  450. this._disposePostProcesses();
  451. if (this._cameras !== null) {
  452. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._cameras);
  453. // get back cameras to be used to reattach pipeline
  454. this._cameras = this._camerasToBeAttached.slice();
  455. }
  456. this._reset();
  457. this._prevPostProcess = null;
  458. this._prevPrevPostProcess = null;
  459. this._hasCleared = false;
  460. if (this.depthOfFieldEnabled) {
  461. // Multi camera suport
  462. if (this._cameras.length > 1) {
  463. for (let camera of this._cameras) {
  464. const depthRenderer = this._scene.enableDepthRenderer(camera);
  465. depthRenderer.useOnlyInActiveCamera = true;
  466. }
  467. this._depthOfFieldSceneObserver = this._scene.onAfterRenderTargetsRenderObservable.add((scene) => {
  468. if (this._cameras.indexOf(scene.activeCamera!) > -1) {
  469. this.depthOfField.depthTexture = scene.enableDepthRenderer(scene.activeCamera).getDepthMap();
  470. }
  471. });
  472. }
  473. else {
  474. this._scene.onAfterRenderTargetsRenderObservable.remove(this._depthOfFieldSceneObserver);
  475. const depthRenderer = this._scene.enableDepthRenderer(this._cameras[0]);
  476. this.depthOfField.depthTexture = depthRenderer.getDepthMap();
  477. }
  478. if (!this.depthOfField._isReady()) {
  479. this.depthOfField._updateEffects();
  480. }
  481. this.addEffect(this.depthOfField);
  482. this._setAutoClearAndTextureSharing(this.depthOfField._effects[0], true);
  483. }
  484. else {
  485. this._scene.onAfterRenderTargetsRenderObservable.remove(this._depthOfFieldSceneObserver);
  486. }
  487. if (this.bloomEnabled) {
  488. if (!this.bloom._isReady()) {
  489. this.bloom._updateEffects();
  490. }
  491. this.addEffect(this.bloom);
  492. this._setAutoClearAndTextureSharing(this.bloom._effects[0], true);
  493. }
  494. if (this._imageProcessingEnabled) {
  495. this.imageProcessing = new ImageProcessingPostProcess("imageProcessing", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  496. if (this._hdr) {
  497. this.addEffect(new PostProcessRenderEffect(engine, this.ImageProcessingPostProcessId, () => { return this.imageProcessing; }, true));
  498. this._setAutoClearAndTextureSharing(this.imageProcessing);
  499. } else {
  500. this._scene.imageProcessingConfiguration.applyByPostProcess = false;
  501. }
  502. if (!this.cameras || this.cameras.length === 0) {
  503. this._scene.imageProcessingConfiguration.applyByPostProcess = false;
  504. }
  505. if (!this.imageProcessing.getEffect()) {
  506. this.imageProcessing._updateParameters();
  507. }
  508. }
  509. if (this.sharpenEnabled) {
  510. if (!this.sharpen.isReady()) {
  511. this.sharpen.updateEffect();
  512. }
  513. this.addEffect(this._sharpenEffect);
  514. this._setAutoClearAndTextureSharing(this.sharpen);
  515. }
  516. if (this.grainEnabled) {
  517. if (!this.grain.isReady()) {
  518. this.grain.updateEffect();
  519. }
  520. this.addEffect(this._grainEffect);
  521. this._setAutoClearAndTextureSharing(this.grain);
  522. }
  523. if (this.chromaticAberrationEnabled) {
  524. if (!this.chromaticAberration.isReady()) {
  525. this.chromaticAberration.updateEffect();
  526. }
  527. this.addEffect(this._chromaticAberrationEffect);
  528. this._setAutoClearAndTextureSharing(this.chromaticAberration);
  529. }
  530. if (this.fxaaEnabled) {
  531. this.fxaa = new FxaaPostProcess("fxaa", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  532. this.addEffect(new PostProcessRenderEffect(engine, this.FxaaPostProcessId, () => { return this.fxaa; }, true));
  533. this._setAutoClearAndTextureSharing(this.fxaa, true);
  534. }
  535. if (this._cameras !== null) {
  536. this._scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(this._name, this._cameras);
  537. }
  538. // In multicamera mode, the scene needs to autoclear in between cameras.
  539. if (this._scene.activeCameras && this._scene.activeCameras.length > 1) {
  540. this._scene.autoClear = true;
  541. }
  542. if (!this._enableMSAAOnFirstPostProcess(this.samples) && this.samples > 1) {
  543. Logger.Warn("MSAA failed to enable, MSAA is only supported in browsers that support webGL >= 2.0");
  544. }
  545. }
  546. private _disposePostProcesses(disposeNonRecreated = false): void {
  547. for (var i = 0; i < this._cameras.length; i++) {
  548. var camera = this._cameras[i];
  549. if (this.imageProcessing) {
  550. this.imageProcessing.dispose(camera);
  551. }
  552. if (this.fxaa) {
  553. this.fxaa.dispose(camera);
  554. }
  555. // These are created in the constructor and should not be disposed on every pipeline change
  556. if (disposeNonRecreated) {
  557. if (this.sharpen) {
  558. this.sharpen.dispose(camera);
  559. }
  560. if (this.depthOfField) {
  561. this._scene.onAfterRenderTargetsRenderObservable.remove(this._depthOfFieldSceneObserver);
  562. this.depthOfField.disposeEffects(camera);
  563. }
  564. if (this.bloom) {
  565. this.bloom.disposeEffects(camera);
  566. }
  567. if (this.chromaticAberration) {
  568. this.chromaticAberration.dispose(camera);
  569. }
  570. if (this.grain) {
  571. this.grain.dispose(camera);
  572. }
  573. if (this._glowLayer) {
  574. this._glowLayer.dispose();
  575. }
  576. }
  577. }
  578. (<any>this.imageProcessing) = null;
  579. (<any>this.fxaa) = null;
  580. if (disposeNonRecreated) {
  581. (<any>this.sharpen) = null;
  582. (<any>this._sharpenEffect) = null;
  583. (<any>this.depthOfField) = null;
  584. (<any>this.bloom) = null;
  585. (<any>this.chromaticAberration) = null;
  586. (<any>this._chromaticAberrationEffect) = null;
  587. (<any>this.grain) = null;
  588. (<any>this._grainEffect) = null;
  589. this._glowLayer = null;
  590. }
  591. }
  592. /**
  593. * Adds a camera to the pipeline
  594. * @param camera the camera to be added
  595. */
  596. public addCamera(camera: Camera): void {
  597. this._camerasToBeAttached.push(camera);
  598. this._buildPipeline();
  599. }
  600. /**
  601. * Removes a camera from the pipeline
  602. * @param camera the camera to remove
  603. */
  604. public removeCamera(camera: Camera): void {
  605. var index = this._camerasToBeAttached.indexOf(camera);
  606. this._camerasToBeAttached.splice(index, 1);
  607. this._buildPipeline();
  608. }
  609. /**
  610. * Dispose of the pipeline and stop all post processes
  611. */
  612. public dispose(): void {
  613. this._disposePostProcesses(true);
  614. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._cameras);
  615. this._scene.autoClear = true;
  616. if (this._resizeObserver) {
  617. this._scene.getEngine().onResizeObservable.remove(this._resizeObserver);
  618. this._resizeObserver = null;
  619. }
  620. this._scene.imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingConfigurationObserver);
  621. super.dispose();
  622. }
  623. /**
  624. * Serialize the rendering pipeline (Used when exporting)
  625. * @returns the serialized object
  626. */
  627. public serialize(): any {
  628. var serializationObject = SerializationHelper.Serialize(this);
  629. serializationObject.customType = "DefaultRenderingPipeline";
  630. return serializationObject;
  631. }
  632. /**
  633. * Parse the serialized pipeline
  634. * @param source Source pipeline.
  635. * @param scene The scene to load the pipeline to.
  636. * @param rootUrl The URL of the serialized pipeline.
  637. * @returns An instantiated pipeline from the serialized object.
  638. */
  639. public static Parse(source: any, scene: Scene, rootUrl: string): DefaultRenderingPipeline {
  640. return SerializationHelper.Parse(() => new DefaultRenderingPipeline(source._name, source._name._hdr, scene), source, scene, rootUrl);
  641. }
  642. }
  643. _TypeStore.RegisteredTypes["BABYLON.DefaultRenderingPipeline"] = DefaultRenderingPipeline;