defaultRenderingPipeline.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. import { Nullable } from "../../../types";
  2. import { serialize, SerializationHelper } from "../../../Misc/decorators";
  3. import { Observable, 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. * This is triggered each time the pipeline has been built.
  109. */
  110. public onBuildObservable = new Observable<DefaultRenderingPipeline>();
  111. /**
  112. * Gets active scene
  113. */
  114. public get scene(): Scene {
  115. return this._scene;
  116. }
  117. /**
  118. * Enable or disable the sharpen process from the pipeline
  119. */
  120. public set sharpenEnabled(enabled: boolean) {
  121. if (this._sharpenEnabled === enabled) {
  122. return;
  123. }
  124. this._sharpenEnabled = enabled;
  125. this._buildPipeline();
  126. }
  127. @serialize()
  128. public get sharpenEnabled(): boolean {
  129. return this._sharpenEnabled;
  130. }
  131. private _resizeObserver: Nullable<Observer<Engine>> = null;
  132. private _hardwareScaleLevel = 1.0;
  133. private _bloomKernel: number = 64;
  134. /**
  135. * Specifies the size of the bloom blur kernel, relative to the final output size
  136. */
  137. @serialize()
  138. public get bloomKernel(): number {
  139. return this._bloomKernel;
  140. }
  141. public set bloomKernel(value: number) {
  142. this._bloomKernel = value;
  143. this.bloom.kernel = value / this._hardwareScaleLevel;
  144. }
  145. /**
  146. * Specifies the weight of the bloom in the final rendering
  147. */
  148. @serialize()
  149. private _bloomWeight: number = 0.15;
  150. /**
  151. * Specifies the luma threshold for the area that will be blurred by the bloom
  152. */
  153. @serialize()
  154. private _bloomThreshold: number = 0.9;
  155. @serialize()
  156. private _hdr: boolean;
  157. /**
  158. * The strength of the bloom.
  159. */
  160. public set bloomWeight(value: number) {
  161. if (this._bloomWeight === value) {
  162. return;
  163. }
  164. this.bloom.weight = value;
  165. this._bloomWeight = value;
  166. }
  167. @serialize()
  168. public get bloomWeight(): number {
  169. return this._bloomWeight;
  170. }
  171. /**
  172. * The strength of the bloom.
  173. */
  174. public set bloomThreshold(value: number) {
  175. if (this._bloomThreshold === value) {
  176. return;
  177. }
  178. this.bloom.threshold = value;
  179. this._bloomThreshold = value;
  180. }
  181. @serialize()
  182. public get bloomThreshold(): number {
  183. return this._bloomThreshold;
  184. }
  185. /**
  186. * The scale of the bloom, lower value will provide better performance.
  187. */
  188. public set bloomScale(value: number) {
  189. if (this._bloomScale === value) {
  190. return;
  191. }
  192. this._bloomScale = value;
  193. // recreate bloom and dispose old as this setting is not dynamic
  194. this._rebuildBloom();
  195. this._buildPipeline();
  196. }
  197. @serialize()
  198. public get bloomScale(): number {
  199. return this._bloomScale;
  200. }
  201. /**
  202. * Enable or disable the bloom from the pipeline
  203. */
  204. public set bloomEnabled(enabled: boolean) {
  205. if (this._bloomEnabled === enabled) {
  206. return;
  207. }
  208. this._bloomEnabled = enabled;
  209. this._buildPipeline();
  210. }
  211. @serialize()
  212. public get bloomEnabled(): boolean {
  213. return this._bloomEnabled;
  214. }
  215. private _rebuildBloom() {
  216. // recreate bloom and dispose old as this setting is not dynamic
  217. var oldBloom = this.bloom;
  218. this.bloom = new BloomEffect(this._scene, this.bloomScale, this._bloomWeight, this.bloomKernel, this._defaultPipelineTextureType, false);
  219. this.bloom.threshold = oldBloom.threshold;
  220. for (var i = 0; i < this._cameras.length; i++) {
  221. oldBloom.disposeEffects(this._cameras[i]);
  222. }
  223. }
  224. /**
  225. * If the depth of field is enabled.
  226. */
  227. @serialize()
  228. public get depthOfFieldEnabled(): boolean {
  229. return this._depthOfFieldEnabled;
  230. }
  231. public set depthOfFieldEnabled(enabled: boolean) {
  232. if (this._depthOfFieldEnabled === enabled) {
  233. return;
  234. }
  235. this._depthOfFieldEnabled = enabled;
  236. this._buildPipeline();
  237. }
  238. /**
  239. * Blur level of the depth of field effect. (Higher blur will effect performance)
  240. */
  241. @serialize()
  242. public get depthOfFieldBlurLevel(): DepthOfFieldEffectBlurLevel {
  243. return this._depthOfFieldBlurLevel;
  244. }
  245. public set depthOfFieldBlurLevel(value: DepthOfFieldEffectBlurLevel) {
  246. if (this._depthOfFieldBlurLevel === value) {
  247. return;
  248. }
  249. this._depthOfFieldBlurLevel = value;
  250. // recreate dof and dispose old as this setting is not dynamic
  251. var oldDof = this.depthOfField;
  252. this.depthOfField = new DepthOfFieldEffect(this._scene, null, this._depthOfFieldBlurLevel, this._defaultPipelineTextureType, false);
  253. this.depthOfField.focalLength = oldDof.focalLength;
  254. this.depthOfField.focusDistance = oldDof.focusDistance;
  255. this.depthOfField.fStop = oldDof.fStop;
  256. this.depthOfField.lensSize = oldDof.lensSize;
  257. for (var i = 0; i < this._cameras.length; i++) {
  258. oldDof.disposeEffects(this._cameras[i]);
  259. }
  260. this._buildPipeline();
  261. }
  262. /**
  263. * If the anti aliasing is enabled.
  264. */
  265. public set fxaaEnabled(enabled: boolean) {
  266. if (this._fxaaEnabled === enabled) {
  267. return;
  268. }
  269. this._fxaaEnabled = enabled;
  270. this._buildPipeline();
  271. }
  272. @serialize()
  273. public get fxaaEnabled(): boolean {
  274. return this._fxaaEnabled;
  275. }
  276. private _samples = 1;
  277. /**
  278. * MSAA sample count, setting this to 4 will provide 4x anti aliasing. (default: 1)
  279. */
  280. public set samples(sampleCount: number) {
  281. if (this._samples === sampleCount) {
  282. return;
  283. }
  284. this._samples = sampleCount;
  285. this._buildPipeline();
  286. }
  287. @serialize()
  288. public get samples(): number {
  289. return this._samples;
  290. }
  291. /**
  292. * If image processing is enabled.
  293. */
  294. public set imageProcessingEnabled(enabled: boolean) {
  295. if (this._imageProcessingEnabled === enabled) {
  296. return;
  297. }
  298. this._scene.imageProcessingConfiguration.isEnabled = enabled;
  299. }
  300. @serialize()
  301. public get imageProcessingEnabled(): boolean {
  302. return this._imageProcessingEnabled;
  303. }
  304. /**
  305. * If glow layer is enabled. (Adds a glow effect to emmissive materials)
  306. */
  307. public set glowLayerEnabled(enabled: boolean) {
  308. if (enabled && !this._glowLayer) {
  309. this._glowLayer = new GlowLayer("", this._scene);
  310. } else if (!enabled && this._glowLayer) {
  311. this._glowLayer.dispose();
  312. this._glowLayer = null;
  313. }
  314. }
  315. @serialize()
  316. public get glowLayerEnabled(): boolean {
  317. return this._glowLayer != null;
  318. }
  319. /**
  320. * Gets the glow layer (or null if not defined)
  321. */
  322. public get glowLayer() {
  323. return this._glowLayer;
  324. }
  325. /**
  326. * Enable or disable the chromaticAberration process from the pipeline
  327. */
  328. public set chromaticAberrationEnabled(enabled: boolean) {
  329. if (this._chromaticAberrationEnabled === enabled) {
  330. return;
  331. }
  332. this._chromaticAberrationEnabled = enabled;
  333. this._buildPipeline();
  334. }
  335. @serialize()
  336. public get chromaticAberrationEnabled(): boolean {
  337. return this._chromaticAberrationEnabled;
  338. }
  339. /**
  340. * Enable or disable the grain process from the pipeline
  341. */
  342. public set grainEnabled(enabled: boolean) {
  343. if (this._grainEnabled === enabled) {
  344. return;
  345. }
  346. this._grainEnabled = enabled;
  347. this._buildPipeline();
  348. }
  349. @serialize()
  350. public get grainEnabled(): boolean {
  351. return this._grainEnabled;
  352. }
  353. /**
  354. * @constructor
  355. * @param name - The rendering pipeline name (default: "")
  356. * @param hdr - If high dynamic range textures should be used (default: true)
  357. * @param scene - The scene linked to this pipeline (default: the last created scene)
  358. * @param cameras - The array of cameras that the rendering pipeline will be attached to (default: scene.cameras)
  359. * @param automaticBuild - if false, you will have to manually call prepare() to update the pipeline (default: true)
  360. */
  361. constructor(name: string = "", hdr: boolean = true, scene: Scene = EngineStore.LastCreatedScene!, cameras?: Camera[], automaticBuild = true) {
  362. super(scene.getEngine(), name);
  363. this._cameras = cameras || scene.cameras;
  364. this._cameras = this._cameras.slice();
  365. this._camerasToBeAttached = this._cameras.slice();
  366. this._buildAllowed = automaticBuild;
  367. // Initialize
  368. this._scene = scene;
  369. var caps = this._scene.getEngine().getCaps();
  370. this._hdr = hdr && (caps.textureHalfFloatRender || caps.textureFloatRender);
  371. // Misc
  372. if (this._hdr) {
  373. if (caps.textureHalfFloatRender) {
  374. this._defaultPipelineTextureType = Constants.TEXTURETYPE_HALF_FLOAT;
  375. }
  376. else if (caps.textureFloatRender) {
  377. this._defaultPipelineTextureType = Constants.TEXTURETYPE_FLOAT;
  378. }
  379. } else {
  380. this._defaultPipelineTextureType = Constants.TEXTURETYPE_UNSIGNED_INT;
  381. }
  382. // Attach
  383. scene.postProcessRenderPipelineManager.addPipeline(this);
  384. var engine = this._scene.getEngine();
  385. // Create post processes before hand so they can be modified before enabled.
  386. // Block compilation flag is set to true to avoid compilation prior to use, these will be updated on first use in build pipeline.
  387. this.sharpen = new SharpenPostProcess("sharpen", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType, true);
  388. this._sharpenEffect = new PostProcessRenderEffect(engine, this.SharpenPostProcessId, () => { return this.sharpen; }, true);
  389. this.depthOfField = new DepthOfFieldEffect(this._scene, null, this._depthOfFieldBlurLevel, this._defaultPipelineTextureType, true);
  390. this.bloom = new BloomEffect(this._scene, this._bloomScale, this._bloomWeight, this.bloomKernel, this._defaultPipelineTextureType, true);
  391. this.chromaticAberration = new ChromaticAberrationPostProcess("ChromaticAberration", engine.getRenderWidth(), engine.getRenderHeight(), 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType, true);
  392. this._chromaticAberrationEffect = new PostProcessRenderEffect(engine, this.ChromaticAberrationPostProcessId, () => { return this.chromaticAberration; }, true);
  393. this.grain = new GrainPostProcess("Grain", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType, true);
  394. this._grainEffect = new PostProcessRenderEffect(engine, this.GrainPostProcessId, () => { return this.grain; }, true);
  395. this._resizeObserver = engine.onResizeObservable.add(() => {
  396. this._hardwareScaleLevel = engine.getHardwareScalingLevel();
  397. this.bloomKernel = this.bloomKernel;
  398. });
  399. this._imageProcessingConfigurationObserver = this._scene.imageProcessingConfiguration.onUpdateParameters.add(() => {
  400. this.bloom._downscale._exposure = this._scene.imageProcessingConfiguration.exposure;
  401. if (this.imageProcessingEnabled !== this._scene.imageProcessingConfiguration.isEnabled) {
  402. this._imageProcessingEnabled = this._scene.imageProcessingConfiguration.isEnabled;
  403. this._buildPipeline();
  404. }
  405. });
  406. this._buildPipeline();
  407. }
  408. /**
  409. * Get the class name
  410. * @returns "DefaultRenderingPipeline"
  411. */
  412. public getClassName(): string {
  413. return "DefaultRenderingPipeline";
  414. }
  415. /**
  416. * Force the compilation of the entire pipeline.
  417. */
  418. public prepare(): void {
  419. let previousState = this._buildAllowed;
  420. this._buildAllowed = true;
  421. this._buildPipeline();
  422. this._buildAllowed = previousState;
  423. }
  424. private _hasCleared = false;
  425. private _prevPostProcess: Nullable<PostProcess> = null;
  426. private _prevPrevPostProcess: Nullable<PostProcess> = null;
  427. private _setAutoClearAndTextureSharing(postProcess: PostProcess, skipTextureSharing = false) {
  428. if (this._hasCleared) {
  429. postProcess.autoClear = false;
  430. } else {
  431. postProcess.autoClear = true;
  432. this._scene.autoClear = false;
  433. this._hasCleared = true;
  434. }
  435. if (!skipTextureSharing) {
  436. if (this._prevPrevPostProcess) {
  437. postProcess.shareOutputWith(this._prevPrevPostProcess);
  438. } else {
  439. postProcess.useOwnOutput();
  440. }
  441. if (this._prevPostProcess) {
  442. this._prevPrevPostProcess = this._prevPostProcess;
  443. }
  444. this._prevPostProcess = postProcess;
  445. }
  446. }
  447. private _depthOfFieldSceneObserver: Nullable<Observer<Scene>> = null;
  448. private _buildPipeline() {
  449. if (!this._buildAllowed) {
  450. return;
  451. }
  452. this._scene.autoClear = true;
  453. var engine = this._scene.getEngine();
  454. this._disposePostProcesses();
  455. if (this._cameras !== null) {
  456. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._cameras);
  457. // get back cameras to be used to reattach pipeline
  458. this._cameras = this._camerasToBeAttached.slice();
  459. }
  460. this._reset();
  461. this._prevPostProcess = null;
  462. this._prevPrevPostProcess = null;
  463. this._hasCleared = false;
  464. if (this.depthOfFieldEnabled) {
  465. // Multi camera suport
  466. if (this._cameras.length > 1) {
  467. for (let camera of this._cameras) {
  468. const depthRenderer = this._scene.enableDepthRenderer(camera);
  469. depthRenderer.useOnlyInActiveCamera = true;
  470. }
  471. this._depthOfFieldSceneObserver = this._scene.onAfterRenderTargetsRenderObservable.add((scene) => {
  472. if (this._cameras.indexOf(scene.activeCamera!) > -1) {
  473. this.depthOfField.depthTexture = scene.enableDepthRenderer(scene.activeCamera).getDepthMap();
  474. }
  475. });
  476. }
  477. else {
  478. this._scene.onAfterRenderTargetsRenderObservable.remove(this._depthOfFieldSceneObserver);
  479. const depthRenderer = this._scene.enableDepthRenderer(this._cameras[0]);
  480. this.depthOfField.depthTexture = depthRenderer.getDepthMap();
  481. }
  482. if (!this.depthOfField._isReady()) {
  483. this.depthOfField._updateEffects();
  484. }
  485. this.addEffect(this.depthOfField);
  486. this._setAutoClearAndTextureSharing(this.depthOfField._effects[0], true);
  487. }
  488. else {
  489. this._scene.onAfterRenderTargetsRenderObservable.remove(this._depthOfFieldSceneObserver);
  490. }
  491. if (this.bloomEnabled) {
  492. if (!this.bloom._isReady()) {
  493. this.bloom._updateEffects();
  494. }
  495. this.addEffect(this.bloom);
  496. this._setAutoClearAndTextureSharing(this.bloom._effects[0], true);
  497. }
  498. if (this._imageProcessingEnabled) {
  499. this.imageProcessing = new ImageProcessingPostProcess("imageProcessing", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  500. if (this._hdr) {
  501. this.addEffect(new PostProcessRenderEffect(engine, this.ImageProcessingPostProcessId, () => { return this.imageProcessing; }, true));
  502. this._setAutoClearAndTextureSharing(this.imageProcessing);
  503. } else {
  504. this._scene.imageProcessingConfiguration.applyByPostProcess = false;
  505. }
  506. if (!this.cameras || this.cameras.length === 0) {
  507. this._scene.imageProcessingConfiguration.applyByPostProcess = false;
  508. }
  509. if (!this.imageProcessing.getEffect()) {
  510. this.imageProcessing._updateParameters();
  511. }
  512. }
  513. if (this.sharpenEnabled) {
  514. if (!this.sharpen.isReady()) {
  515. this.sharpen.updateEffect();
  516. }
  517. this.addEffect(this._sharpenEffect);
  518. this._setAutoClearAndTextureSharing(this.sharpen);
  519. }
  520. if (this.grainEnabled) {
  521. if (!this.grain.isReady()) {
  522. this.grain.updateEffect();
  523. }
  524. this.addEffect(this._grainEffect);
  525. this._setAutoClearAndTextureSharing(this.grain);
  526. }
  527. if (this.chromaticAberrationEnabled) {
  528. if (!this.chromaticAberration.isReady()) {
  529. this.chromaticAberration.updateEffect();
  530. }
  531. this.addEffect(this._chromaticAberrationEffect);
  532. this._setAutoClearAndTextureSharing(this.chromaticAberration);
  533. }
  534. if (this.fxaaEnabled) {
  535. this.fxaa = new FxaaPostProcess("fxaa", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  536. this.addEffect(new PostProcessRenderEffect(engine, this.FxaaPostProcessId, () => { return this.fxaa; }, true));
  537. this._setAutoClearAndTextureSharing(this.fxaa, true);
  538. }
  539. if (this._cameras !== null) {
  540. this._scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(this._name, this._cameras);
  541. }
  542. // In multicamera mode, the scene needs to autoclear in between cameras.
  543. if (this._scene.activeCameras && this._scene.activeCameras.length > 1) {
  544. this._scene.autoClear = true;
  545. }
  546. if (!this._enableMSAAOnFirstPostProcess(this.samples) && this.samples > 1) {
  547. Logger.Warn("MSAA failed to enable, MSAA is only supported in browsers that support webGL >= 2.0");
  548. }
  549. this.onBuildObservable.notifyObservers(this);
  550. }
  551. private _disposePostProcesses(disposeNonRecreated = false): void {
  552. for (var i = 0; i < this._cameras.length; i++) {
  553. var camera = this._cameras[i];
  554. if (this.imageProcessing) {
  555. this.imageProcessing.dispose(camera);
  556. }
  557. if (this.fxaa) {
  558. this.fxaa.dispose(camera);
  559. }
  560. // These are created in the constructor and should not be disposed on every pipeline change
  561. if (disposeNonRecreated) {
  562. if (this.sharpen) {
  563. this.sharpen.dispose(camera);
  564. }
  565. if (this.depthOfField) {
  566. this._scene.onAfterRenderTargetsRenderObservable.remove(this._depthOfFieldSceneObserver);
  567. this.depthOfField.disposeEffects(camera);
  568. }
  569. if (this.bloom) {
  570. this.bloom.disposeEffects(camera);
  571. }
  572. if (this.chromaticAberration) {
  573. this.chromaticAberration.dispose(camera);
  574. }
  575. if (this.grain) {
  576. this.grain.dispose(camera);
  577. }
  578. if (this._glowLayer) {
  579. this._glowLayer.dispose();
  580. }
  581. }
  582. }
  583. (<any>this.imageProcessing) = null;
  584. (<any>this.fxaa) = null;
  585. if (disposeNonRecreated) {
  586. (<any>this.sharpen) = null;
  587. (<any>this._sharpenEffect) = null;
  588. (<any>this.depthOfField) = null;
  589. (<any>this.bloom) = null;
  590. (<any>this.chromaticAberration) = null;
  591. (<any>this._chromaticAberrationEffect) = null;
  592. (<any>this.grain) = null;
  593. (<any>this._grainEffect) = null;
  594. this._glowLayer = null;
  595. }
  596. }
  597. /**
  598. * Adds a camera to the pipeline
  599. * @param camera the camera to be added
  600. */
  601. public addCamera(camera: Camera): void {
  602. this._camerasToBeAttached.push(camera);
  603. this._buildPipeline();
  604. }
  605. /**
  606. * Removes a camera from the pipeline
  607. * @param camera the camera to remove
  608. */
  609. public removeCamera(camera: Camera): void {
  610. var index = this._camerasToBeAttached.indexOf(camera);
  611. this._camerasToBeAttached.splice(index, 1);
  612. this._buildPipeline();
  613. }
  614. /**
  615. * Dispose of the pipeline and stop all post processes
  616. */
  617. public dispose(): void {
  618. this.onBuildObservable.clear();
  619. this._disposePostProcesses(true);
  620. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._cameras);
  621. this._scene.autoClear = true;
  622. if (this._resizeObserver) {
  623. this._scene.getEngine().onResizeObservable.remove(this._resizeObserver);
  624. this._resizeObserver = null;
  625. }
  626. this._scene.imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingConfigurationObserver);
  627. super.dispose();
  628. }
  629. /**
  630. * Serialize the rendering pipeline (Used when exporting)
  631. * @returns the serialized object
  632. */
  633. public serialize(): any {
  634. var serializationObject = SerializationHelper.Serialize(this);
  635. serializationObject.customType = "DefaultRenderingPipeline";
  636. return serializationObject;
  637. }
  638. /**
  639. * Parse the serialized pipeline
  640. * @param source Source pipeline.
  641. * @param scene The scene to load the pipeline to.
  642. * @param rootUrl The URL of the serialized pipeline.
  643. * @returns An instantiated pipeline from the serialized object.
  644. */
  645. public static Parse(source: any, scene: Scene, rootUrl: string): DefaultRenderingPipeline {
  646. return SerializationHelper.Parse(() => new DefaultRenderingPipeline(source._name, source._name._hdr, scene), source, scene, rootUrl);
  647. }
  648. }
  649. _TypeStore.RegisteredTypes["BABYLON.DefaultRenderingPipeline"] = DefaultRenderingPipeline;