babylon.defaultRenderingPipeline.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. module BABYLON {
  2. /**
  3. * The default rendering pipeline can be added to a scene to apply common post processing effects such as anti-aliasing or depth of field.
  4. * See https://doc.babylonjs.com/how_to/using_default_rendering_pipeline
  5. */
  6. export class DefaultRenderingPipeline extends PostProcessRenderPipeline implements IDisposable, IAnimatable {
  7. private _scene: Scene;
  8. /**
  9. * ID of the sharpen post process,
  10. */
  11. readonly SharpenPostProcessId: string = "SharpenPostProcessEffect";
  12. /**
  13. * ID of the pass post process used for bloom,
  14. */
  15. readonly PassPostProcessId: string = "PassPostProcessEffect";
  16. /**
  17. * ID of the highlight post process used for bloom,
  18. */
  19. readonly HighLightsPostProcessId: string = "HighLightsPostProcessEffect";
  20. /**
  21. * ID of the blurX post process used for bloom,
  22. */
  23. readonly BlurXPostProcessId: string = "BlurXPostProcessEffect";
  24. /**
  25. * ID of the blurY post process used for bloom,
  26. */
  27. readonly BlurYPostProcessId: string = "BlurYPostProcessEffect";
  28. /**
  29. * ID of the copy back post process used for bloom,
  30. */
  31. readonly CopyBackPostProcessId: string = "CopyBackPostProcessEffect";
  32. /**
  33. * ID of the image processing post process;
  34. */
  35. readonly ImageProcessingPostProcessId: string = "ImageProcessingPostProcessEffect";
  36. /**
  37. * ID of the Fast Approximate Anti-Aliasing post process;
  38. */
  39. readonly FxaaPostProcessId: string = "FxaaPostProcessEffect";
  40. /**
  41. * ID of the final merge post process;
  42. */
  43. readonly FinalMergePostProcessId: string = "FinalMergePostProcessEffect";
  44. // Post-processes
  45. /**
  46. * Sharpen post process which will apply a sharpen convolution to enhance edges
  47. */
  48. public sharpen: SharpenPostProcess;
  49. /**
  50. * First pass of bloom to capture the original image texture for later use.
  51. */
  52. public pass: PassPostProcess;
  53. /**
  54. * Second pass of bloom used to brighten bright portions of the image.
  55. */
  56. public highlights: HighlightsPostProcess;
  57. /**
  58. * BlurX post process used in coordination with blurY to guassian blur the highlighted image.
  59. */
  60. public blurX: BlurPostProcess;
  61. /**
  62. * BlurY post process used in coordination with blurX to guassian blur the highlighted image.
  63. */
  64. public blurY: BlurPostProcess;
  65. /**
  66. * Final pass run for bloom to copy the resulting bloom texture back to screen.
  67. */
  68. public copyBack: PassPostProcess;
  69. /**
  70. * Depth of field effect, applies a blur based on how far away objects are from the focus distance.
  71. */
  72. public depthOfField: DepthOfFieldEffect;
  73. /**
  74. * The Fast Approximate Anti-Aliasing post process which attemps to remove aliasing from an image.
  75. */
  76. public fxaa: FxaaPostProcess;
  77. /**
  78. * Image post processing pass used to perform operations such as tone mapping or color grading.
  79. */
  80. public imageProcessing: ImageProcessingPostProcess;
  81. /**
  82. * Final post process to merge results of all previous passes
  83. */
  84. public finalMerge: PassPostProcess;
  85. /**
  86. * Animations which can be used to tweak settings over a period of time
  87. */
  88. public animations: Animation[] = [];
  89. // Values
  90. private _sharpenEnabled:boolean = false;
  91. private _bloomEnabled: boolean = false;
  92. private _depthOfFieldEnabled: boolean = false;
  93. private _depthOfFieldBlurLevel = DepthOfFieldEffectBlurLevel.Low;
  94. private _fxaaEnabled: boolean = false;
  95. private _msaaEnabled: boolean = false;
  96. private _imageProcessingEnabled: boolean = true;
  97. private _defaultPipelineTextureType: number;
  98. private _bloomScale: number = 0.6;
  99. private _buildAllowed = true;
  100. /**
  101. * Enable or disable the sharpen process from the pipeline
  102. */
  103. public set sharpenEnabled(enabled: boolean) {
  104. if (this._sharpenEnabled === enabled) {
  105. return;
  106. }
  107. this._sharpenEnabled = enabled;
  108. this._buildPipeline();
  109. }
  110. @serialize()
  111. public get sharpenEnabled(): boolean {
  112. return this._sharpenEnabled;
  113. }
  114. /**
  115. * Specifies the size of the bloom blur kernel, relative to the final output size
  116. */
  117. @serialize()
  118. public bloomKernel: number = 64;
  119. /**
  120. * Specifies the weight of the bloom in the final rendering
  121. */
  122. @serialize()
  123. private _bloomWeight: number = 0.15;
  124. @serialize()
  125. private _hdr: boolean;
  126. /**
  127. * The strength of the bloom.
  128. */
  129. public set bloomWeight(value: number) {
  130. if (this._bloomWeight === value) {
  131. return;
  132. }
  133. this._bloomWeight = value;
  134. if (this._hdr && this.copyBack) {
  135. this.copyBack.alphaConstants = new Color4(value, value, value, value);
  136. }
  137. }
  138. @serialize()
  139. public get bloomWeight(): number {
  140. return this._bloomWeight;
  141. }
  142. /**
  143. * The scale of the bloom, lower value will provide better performance.
  144. */
  145. public set bloomScale(value: number) {
  146. if (this._bloomScale === value) {
  147. return;
  148. }
  149. this._bloomScale = value;
  150. this._buildPipeline();
  151. }
  152. @serialize()
  153. public get bloomScale(): number {
  154. return this._bloomScale;
  155. }
  156. /**
  157. * Enable or disable the bloom from the pipeline
  158. */
  159. public set bloomEnabled(enabled: boolean) {
  160. if (this._bloomEnabled === enabled) {
  161. return;
  162. }
  163. this._bloomEnabled = enabled;
  164. this._buildPipeline();
  165. }
  166. @serialize()
  167. public get bloomEnabled(): boolean {
  168. return this._bloomEnabled;
  169. }
  170. /**
  171. * If the depth of field is enabled.
  172. */
  173. @serialize()
  174. public get depthOfFieldEnabled(): boolean {
  175. return this._depthOfFieldEnabled;
  176. }
  177. public set depthOfFieldEnabled(enabled: boolean) {
  178. if (this._depthOfFieldEnabled === enabled) {
  179. return;
  180. }
  181. this._depthOfFieldEnabled = enabled;
  182. this._buildPipeline();
  183. }
  184. /**
  185. * Blur level of the depth of field effect. (Higher blur will effect performance)
  186. */
  187. @serialize()
  188. public get depthOfFieldBlurLevel(): DepthOfFieldEffectBlurLevel {
  189. return this._depthOfFieldBlurLevel;
  190. }
  191. public set depthOfFieldBlurLevel(value: DepthOfFieldEffectBlurLevel) {
  192. if (this._depthOfFieldBlurLevel === value) {
  193. return;
  194. }
  195. this._depthOfFieldBlurLevel = value;
  196. this._buildPipeline();
  197. }
  198. /**
  199. * If the anti aliasing is enabled.
  200. */
  201. public set fxaaEnabled(enabled: boolean) {
  202. if (this._fxaaEnabled === enabled) {
  203. return;
  204. }
  205. this._fxaaEnabled = enabled;
  206. this._buildPipeline();
  207. }
  208. @serialize()
  209. public get fxaaEnabled(): boolean {
  210. return this._fxaaEnabled;
  211. }
  212. /**
  213. * If the multisample anti-aliasing is enabled.
  214. */
  215. public set msaaEnabled(enabled: boolean) {
  216. if (this._msaaEnabled === enabled) {
  217. return;
  218. }
  219. this._msaaEnabled = enabled;
  220. this._buildPipeline();
  221. }
  222. @serialize()
  223. public get msaaEnabled(): boolean {
  224. return this._msaaEnabled;
  225. }
  226. /**
  227. * If image processing is enabled.
  228. */
  229. public set imageProcessingEnabled(enabled: boolean) {
  230. if (this._imageProcessingEnabled === enabled) {
  231. return;
  232. }
  233. this._imageProcessingEnabled = enabled;
  234. this._buildPipeline();
  235. }
  236. @serialize()
  237. public get imageProcessingEnabled(): boolean {
  238. return this._imageProcessingEnabled;
  239. }
  240. /**
  241. * @constructor
  242. * @param {string} name - The rendering pipeline name
  243. * @param {BABYLON.Scene} scene - The scene linked to this pipeline
  244. * @param {any} ratio - The size of the postprocesses (0.5 means that your postprocess will have a width = canvas.width 0.5 and a height = canvas.height 0.5)
  245. * @param {BABYLON.Camera[]} cameras - The array of cameras that the rendering pipeline will be attached to
  246. * @param {boolean} automaticBuild - if false, you will have to manually call prepare() to update the pipeline
  247. */
  248. constructor(name: string, hdr: boolean, scene: Scene, cameras?: Camera[], automaticBuild = true) {
  249. super(scene.getEngine(), name);
  250. this._cameras = cameras ||  [];
  251. this._buildAllowed = automaticBuild;
  252. // Initialize
  253. this._scene = scene;
  254. var caps = this._scene.getEngine().getCaps();
  255. this._hdr = hdr && (caps.textureHalfFloatRender || caps.textureFloatRender);
  256. // Misc
  257. if (this._hdr) {
  258. if (caps.textureHalfFloatRender) {
  259. this._defaultPipelineTextureType = Engine.TEXTURETYPE_HALF_FLOAT;
  260. }
  261. else if (caps.textureFloatRender) {
  262. this._defaultPipelineTextureType = Engine.TEXTURETYPE_FLOAT;
  263. }
  264. } else {
  265. this._defaultPipelineTextureType = Engine.TEXTURETYPE_UNSIGNED_INT;
  266. }
  267. // Attach
  268. scene.postProcessRenderPipelineManager.addPipeline(this);
  269. this._buildPipeline();
  270. }
  271. /**
  272. * Force the compilation of the entire pipeline.
  273. */
  274. public prepare(): void {
  275. let previousState = this._buildAllowed;
  276. this._buildAllowed = true;
  277. this._buildPipeline();
  278. this._buildAllowed = previousState;
  279. }
  280. private _buildPipeline() {
  281. if (!this._buildAllowed) {
  282. return;
  283. }
  284. var engine = this._scene.getEngine();
  285. this._disposePostProcesses();
  286. this._reset();
  287. if (this.sharpenEnabled) {
  288. this.sharpen = new SharpenPostProcess("sharpen", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  289. this.addEffect(new PostProcessRenderEffect(engine, this.SharpenPostProcessId, () => { return this.sharpen; }, true));
  290. }
  291. if(this.depthOfFieldEnabled){
  292. // Enable and get current depth map
  293. var depthTexture = this._scene.enableDepthRenderer(this._cameras[0]).getDepthMap();
  294. this.depthOfField = new DepthOfFieldEffect(this._scene, depthTexture, this._depthOfFieldBlurLevel, this._defaultPipelineTextureType);
  295. this.addEffect(this.depthOfField);
  296. }
  297. if (this.bloomEnabled) {
  298. this.pass = new PassPostProcess("sceneRenderTarget", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  299. this.addEffect(new PostProcessRenderEffect(engine, this.PassPostProcessId, () => { return this.pass; }, true));
  300. if (!this._hdr) { // Need to enhance highlights if not using float rendering
  301. this.highlights = new HighlightsPostProcess("highlights", this.bloomScale, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  302. this.addEffect(new PostProcessRenderEffect(engine, this.HighLightsPostProcessId, () => { return this.highlights; }, true));
  303. this.highlights.autoClear = false;
  304. this.highlights.alwaysForcePOT = true;
  305. }
  306. this.blurX = new BlurPostProcess("horizontal blur", new Vector2(1.0, 0), 10.0, this.bloomScale, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  307. this.addEffect(new PostProcessRenderEffect(engine, this.BlurXPostProcessId, () => { return this.blurX; }, true));
  308. this.blurX.alwaysForcePOT = true;
  309. this.blurX.autoClear = false;
  310. this.blurX.onActivateObservable.add(() => {
  311. let dw = this.blurX.width / engine.getRenderWidth(true);
  312. this.blurX.kernel = this.bloomKernel * dw;
  313. });
  314. this.blurY = new BlurPostProcess("vertical blur", new Vector2(0, 1.0), 10.0, this.bloomScale, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  315. this.addEffect(new PostProcessRenderEffect(engine, this.BlurYPostProcessId, () => { return this.blurY; }, true));
  316. this.blurY.alwaysForcePOT = true;
  317. this.blurY.autoClear = false;
  318. this.blurY.onActivateObservable.add(() => {
  319. let dh = this.blurY.height / engine.getRenderHeight(true);
  320. this.blurY.kernel = this.bloomKernel * dh;
  321. });
  322. this.copyBack = new PassPostProcess("bloomBlendBlit", this.bloomScale, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  323. this.addEffect(new PostProcessRenderEffect(engine, this.CopyBackPostProcessId, () => { return this.copyBack; }, true));
  324. this.copyBack.alwaysForcePOT = true;
  325. if (this._hdr) {
  326. this.copyBack.alphaMode = Engine.ALPHA_INTERPOLATE;
  327. let w = this.bloomWeight;
  328. this.copyBack.alphaConstants = new Color4(w, w, w, w);
  329. } else {
  330. this.copyBack.alphaMode = Engine.ALPHA_SCREENMODE;
  331. }
  332. this.copyBack.autoClear = false;
  333. }
  334. if (this._imageProcessingEnabled) {
  335. this.imageProcessing = new ImageProcessingPostProcess("imageProcessing", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  336. if (this._hdr) {
  337. this.addEffect(new PostProcessRenderEffect(engine, this.ImageProcessingPostProcessId, () => { return this.imageProcessing; }, true));
  338. } else {
  339. this._scene.imageProcessingConfiguration.applyByPostProcess = false;
  340. }
  341. }
  342. if (this.fxaaEnabled) {
  343. this.fxaa = new FxaaPostProcess("fxaa", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  344. this.addEffect(new PostProcessRenderEffect(engine, this.FxaaPostProcessId, () => { return this.fxaa; }, true));
  345. this.fxaa.autoClear = !this.bloomEnabled && (!this._hdr || !this.imageProcessing);
  346. } else if (this._hdr && this.imageProcessing) {
  347. this.finalMerge = this.imageProcessing;
  348. }
  349. else {
  350. this.finalMerge = new PassPostProcess("finalMerge", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  351. this.addEffect(new PostProcessRenderEffect(engine, this.FinalMergePostProcessId, () => { return this.finalMerge; }, true));
  352. this.finalMerge.autoClear = !this.bloomEnabled && (!this._hdr || !this.imageProcessing);
  353. }
  354. if (this.bloomEnabled) {
  355. if (this._hdr) { // Share render targets to save memory
  356. this.copyBack.shareOutputWith(this.blurX);
  357. if (this.imageProcessing) {
  358. this.imageProcessing.shareOutputWith(this.pass);
  359. this.imageProcessing.autoClear = false;
  360. } else if (this.fxaa) {
  361. this.fxaa.shareOutputWith(this.pass);
  362. } else {
  363. this.finalMerge.shareOutputWith(this.pass);
  364. }
  365. } else {
  366. if (this.fxaa) {
  367. this.fxaa.shareOutputWith(this.pass);
  368. } else {
  369. this.finalMerge.shareOutputWith(this.pass);
  370. }
  371. }
  372. }
  373. if (this._cameras !== null) {
  374. this._scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(this._name, this._cameras);
  375. }
  376. if(this.msaaEnabled){
  377. if(!this._enableMSAAOnFirstPostProcess()){
  378. BABYLON.Tools.Warn("MSAA failed to enable, MSAA is only supported in browsers that support webGL >= 2.0");
  379. }
  380. }
  381. }
  382. private _disposePostProcesses(): void {
  383. for (var i = 0; i < this._cameras.length; i++) {
  384. var camera = this._cameras[i];
  385. if (this.sharpen) {
  386. this.sharpen.dispose(camera);
  387. }
  388. if (this.pass) {
  389. this.pass.dispose(camera);
  390. }
  391. if (this.highlights) {
  392. this.highlights.dispose(camera);
  393. }
  394. if (this.blurX) {
  395. this.blurX.dispose(camera);
  396. }
  397. if (this.blurY) {
  398. this.blurY.dispose(camera);
  399. }
  400. if (this.copyBack) {
  401. this.copyBack.dispose(camera);
  402. }
  403. if (this.imageProcessing) {
  404. this.imageProcessing.dispose(camera);
  405. }
  406. if (this.fxaa) {
  407. this.fxaa.dispose(camera);
  408. }
  409. if (this.finalMerge) {
  410. this.finalMerge.dispose(camera);
  411. }
  412. if(this.depthOfField){
  413. this.depthOfField.disposeEffects(camera);
  414. }
  415. }
  416. (<any>this.sharpen) = null;
  417. (<any>this.pass) = null;
  418. (<any>this.highlights) = null;
  419. (<any>this.blurX) = null;
  420. (<any>this.blurY) = null;
  421. (<any>this.copyBack) = null;
  422. (<any>this.imageProcessing) = null;
  423. (<any>this.fxaa) = null;
  424. (<any>this.finalMerge) = null;
  425. (<any>this.depthOfField) = null;
  426. }
  427. /**
  428. * Dispose of the pipeline and stop all post processes
  429. */
  430. public dispose(): void {
  431. this._disposePostProcesses();
  432. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._cameras);
  433. super.dispose();
  434. }
  435. /**
  436. * Serialize the rendering pipeline (Used when exporting)
  437. * @returns the serialized object
  438. */
  439. public serialize(): any {
  440. var serializationObject = SerializationHelper.Serialize(this);
  441. serializationObject.customType = "DefaultRenderingPipeline";
  442. return serializationObject;
  443. }
  444. /**
  445. * Parse the serialized pipeline
  446. * @param source Source pipeline.
  447. * @param scene The scene to load the pipeline to.
  448. * @param rootUrl The URL of the serialized pipeline.
  449. * @returns An instantiated pipeline from the serialized object.
  450. */
  451. public static Parse(source: any, scene: Scene, rootUrl: string): DefaultRenderingPipeline {
  452. return SerializationHelper.Parse(() => new DefaultRenderingPipeline(source._name, source._name._hdr, scene), source, scene, rootUrl);
  453. }
  454. }
  455. }