babylon.defaultRenderingPipeline.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. module BABYLON {
  2. export class DefaultRenderingPipeline extends PostProcessRenderPipeline implements IDisposable, IAnimatable {
  3. private _scene: Scene;
  4. readonly PassPostProcessId: string = "PassPostProcessEffect";
  5. readonly HighLightsPostProcessId: string = "HighLightsPostProcessEffect";
  6. readonly BlurXPostProcessId: string = "BlurXPostProcessEffect";
  7. readonly BlurYPostProcessId: string = "BlurYPostProcessEffect";
  8. readonly CopyBackPostProcessId: string = "CopyBackPostProcessEffect";
  9. readonly ImageProcessingPostProcessId: string = "ImageProcessingPostProcessEffect";
  10. readonly FxaaPostProcessId: string = "FxaaPostProcessEffect";
  11. readonly FinalMergePostProcessId: string = "FinalMergePostProcessEffect";
  12. // Post-processes
  13. public pass: BABYLON.PassPostProcess;
  14. public highlights: BABYLON.HighlightsPostProcess;
  15. public blurX: BABYLON.BlurPostProcess;
  16. public blurY: BABYLON.BlurPostProcess;
  17. public copyBack: BABYLON.PassPostProcess;
  18. public fxaa: FxaaPostProcess;
  19. public imageProcessing: ImageProcessingPostProcess;
  20. public finalMerge: BABYLON.PassPostProcess;
  21. // IAnimatable
  22. public animations: Animation[] = [];
  23. // Values
  24. private _bloomEnabled: boolean = false;
  25. private _fxaaEnabled: boolean = false;
  26. private _imageProcessingEnabled: boolean = true;
  27. private _defaultPipelineTextureType: number;
  28. private _bloomScale: number = 0.6;
  29. private _buildAllowed = true;
  30. /**
  31. * Specifies the size of the bloom blur kernel, relative to the final output size
  32. */
  33. @serialize()
  34. public bloomKernel: number = 64;
  35. /**
  36. * Specifies the weight of the bloom in the final rendering
  37. */
  38. @serialize()
  39. private _bloomWeight: number = 0.15;
  40. @serialize()
  41. private _hdr: boolean;
  42. public set bloomWeight(value: number) {
  43. if (this._bloomWeight === value) {
  44. return;
  45. }
  46. this._bloomWeight = value;
  47. if (this._hdr && this.copyBack) {
  48. this.copyBack.alphaConstants = new BABYLON.Color4(value, value, value, value);
  49. }
  50. }
  51. @serialize()
  52. public get bloomWeight(): number {
  53. return this._bloomWeight;
  54. }
  55. public set bloomScale(value: number) {
  56. if (this._bloomScale === value) {
  57. return;
  58. }
  59. this._bloomScale = value;
  60. this._buildPipeline();
  61. }
  62. @serialize()
  63. public get bloomScale(): number {
  64. return this._bloomScale;
  65. }
  66. public set bloomEnabled(enabled: boolean) {
  67. if (this._bloomEnabled === enabled) {
  68. return;
  69. }
  70. this._bloomEnabled = enabled;
  71. this._buildPipeline();
  72. }
  73. @serialize()
  74. public get bloomEnabled(): boolean {
  75. return this._bloomEnabled;
  76. }
  77. public set fxaaEnabled(enabled: boolean) {
  78. if (this._fxaaEnabled === enabled) {
  79. return;
  80. }
  81. this._fxaaEnabled = enabled;
  82. this._buildPipeline();
  83. }
  84. @serialize()
  85. public get fxaaEnabled(): boolean {
  86. return this._fxaaEnabled;
  87. }
  88. public set imageProcessingEnabled(enabled: boolean) {
  89. if (this._imageProcessingEnabled === enabled) {
  90. return;
  91. }
  92. this._imageProcessingEnabled = enabled;
  93. this._buildPipeline();
  94. }
  95. @serialize()
  96. public get imageProcessingEnabled(): boolean {
  97. return this._imageProcessingEnabled;
  98. }
  99. /**
  100. * @constructor
  101. * @param {string} name - The rendering pipeline name
  102. * @param {BABYLON.Scene} scene - The scene linked to this pipeline
  103. * @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)
  104. * @param {BABYLON.Camera[]} cameras - The array of cameras that the rendering pipeline will be attached to
  105. * @param {boolean} automaticBuild - if false, you will have to manually call prepare() to update the pipeline
  106. */
  107. constructor(name: string, hdr: boolean, scene: Scene, cameras?: Camera[], automaticBuild = true) {
  108. super(scene.getEngine(), name);
  109. this._cameras = cameras || [];
  110. this._buildAllowed = automaticBuild;
  111. // Initialize
  112. this._scene = scene;
  113. var caps = this._scene.getEngine().getCaps();
  114. this._hdr = hdr && (caps.textureHalfFloatRender || caps.textureFloatRender);
  115. // Misc
  116. if (this._hdr) {
  117. if (caps.textureHalfFloatRender) {
  118. this._defaultPipelineTextureType = Engine.TEXTURETYPE_HALF_FLOAT;
  119. }
  120. else if (caps.textureFloatRender) {
  121. this._defaultPipelineTextureType = Engine.TEXTURETYPE_FLOAT;
  122. }
  123. } else {
  124. this._defaultPipelineTextureType = Engine.TEXTURETYPE_UNSIGNED_INT;
  125. }
  126. // Attach
  127. scene.postProcessRenderPipelineManager.addPipeline(this);
  128. this._buildPipeline();
  129. }
  130. /**
  131. * Force the compilation of the entire pipeline.
  132. */
  133. public prepare(): void {
  134. let previousState = this._buildAllowed;
  135. this._buildAllowed = true;
  136. this._buildPipeline();
  137. this._buildAllowed = previousState;
  138. }
  139. private _buildPipeline() {
  140. if (!this._buildAllowed) {
  141. return;
  142. }
  143. var engine = this._scene.getEngine();
  144. this._disposePostProcesses();
  145. this._reset();
  146. if (this.bloomEnabled) {
  147. this.pass = new BABYLON.PassPostProcess("sceneRenderTarget", 1.0, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  148. this.addEffect(new PostProcessRenderEffect(engine, this.PassPostProcessId, () => { return this.pass; }, true));
  149. if (!this._hdr) { // Need to enhance highlights if not using float rendering
  150. this.highlights = new BABYLON.HighlightsPostProcess("highlights", this.bloomScale, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  151. this.addEffect(new PostProcessRenderEffect(engine, this.HighLightsPostProcessId, () => { return this.highlights; }, true));
  152. this.highlights.autoClear = false;
  153. this.highlights.alwaysForcePOT = true;
  154. }
  155. this.blurX = new BABYLON.BlurPostProcess("horizontal blur", new BABYLON.Vector2(1.0, 0), 10.0, this.bloomScale, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  156. this.addEffect(new PostProcessRenderEffect(engine, this.BlurXPostProcessId, () => { return this.blurX; }, true));
  157. this.blurX.alwaysForcePOT = true;
  158. this.blurX.autoClear = false;
  159. this.blurX.onActivateObservable.add(() => {
  160. let dw = this.blurX.width / engine.getRenderingCanvas().width;
  161. this.blurX.kernel = this.bloomKernel * dw;
  162. });
  163. this.blurY = new BABYLON.BlurPostProcess("vertical blur", new BABYLON.Vector2(0, 1.0), 10.0, this.bloomScale, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  164. this.addEffect(new PostProcessRenderEffect(engine, this.BlurYPostProcessId, () => { return this.blurY; }, true));
  165. this.blurY.alwaysForcePOT = true;
  166. this.blurY.autoClear = false;
  167. this.blurY.onActivateObservable.add(() => {
  168. let dh = this.blurY.height / engine.getRenderingCanvas().height;
  169. this.blurY.kernel = this.bloomKernel * dh;
  170. });
  171. this.copyBack = new BABYLON.PassPostProcess("bloomBlendBlit", this.bloomScale, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  172. this.addEffect(new PostProcessRenderEffect(engine, this.CopyBackPostProcessId, () => { return this.copyBack; }, true));
  173. this.copyBack.alwaysForcePOT = true;
  174. if (this._hdr) {
  175. this.copyBack.alphaMode = BABYLON.Engine.ALPHA_INTERPOLATE;
  176. let w = this.bloomWeight;
  177. this.copyBack.alphaConstants = new BABYLON.Color4(w, w, w, w);
  178. } else {
  179. this.copyBack.alphaMode = BABYLON.Engine.ALPHA_SCREENMODE;
  180. }
  181. this.copyBack.autoClear = false;
  182. }
  183. if (this._imageProcessingEnabled) {
  184. this.imageProcessing = new BABYLON.ImageProcessingPostProcess("imageProcessing", 1.0, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  185. if (this._hdr) {
  186. this.addEffect(new PostProcessRenderEffect(engine, this.ImageProcessingPostProcessId, () => { return this.imageProcessing; }, true));
  187. } else {
  188. this._scene.imageProcessingConfiguration.applyByPostProcess = false;
  189. }
  190. }
  191. if (this.fxaaEnabled) {
  192. this.fxaa = new FxaaPostProcess("fxaa", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  193. this.addEffect(new PostProcessRenderEffect(engine, this.FxaaPostProcessId, () => { return this.fxaa; }, true));
  194. this.fxaa.autoClear = !this.bloomEnabled && (!this._hdr || !this.imageProcessing);
  195. } else {
  196. this.finalMerge = new BABYLON.PassPostProcess("finalMerge", 1.0, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType);
  197. this.addEffect(new PostProcessRenderEffect(engine, this.FinalMergePostProcessId, () => { return this.finalMerge; }, true));
  198. this.finalMerge.autoClear = !this.bloomEnabled && (!this._hdr || !this.imageProcessing);
  199. }
  200. if (this.bloomEnabled) {
  201. if (this._hdr) { // Share render targets to save memory
  202. this.copyBack.shareOutputWith(this.blurX);
  203. if (this.imageProcessing) {
  204. this.imageProcessing.shareOutputWith(this.pass);
  205. this.imageProcessing.autoClear = false;
  206. } else if (this.fxaa) {
  207. this.fxaa.shareOutputWith(this.pass);
  208. } else {
  209. this.finalMerge.shareOutputWith(this.pass);
  210. }
  211. } else {
  212. if (this.fxaa) {
  213. this.fxaa.shareOutputWith(this.pass);
  214. } else {
  215. this.finalMerge.shareOutputWith(this.pass);
  216. }
  217. }
  218. }
  219. if (this._cameras !== null) {
  220. this._scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(this._name, this._cameras);
  221. }
  222. }
  223. private _disposePostProcesses(): void {
  224. for (var i = 0; i < this._cameras.length; i++) {
  225. var camera = this._cameras[i];
  226. if (this.pass) {
  227. this.pass.dispose(camera);
  228. }
  229. if (this.highlights) {
  230. this.highlights.dispose(camera);
  231. }
  232. if (this.blurX) {
  233. this.blurX.dispose(camera);
  234. }
  235. if (this.blurY) {
  236. this.blurY.dispose(camera);
  237. }
  238. if (this.copyBack) {
  239. this.copyBack.dispose(camera);
  240. }
  241. if (this.imageProcessing) {
  242. this.imageProcessing.dispose(camera);
  243. }
  244. if (this.fxaa) {
  245. this.fxaa.dispose(camera);
  246. }
  247. if (this.finalMerge) {
  248. this.finalMerge.dispose(camera);
  249. }
  250. }
  251. this.pass = null;
  252. this.highlights = null;
  253. this.blurX = null;
  254. this.blurY = null;
  255. this.copyBack = null;
  256. this.imageProcessing = null;
  257. this.fxaa = null;
  258. this.finalMerge = null;
  259. }
  260. // Dispose
  261. public dispose(): void {
  262. this._disposePostProcesses();
  263. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._cameras);
  264. super.dispose();
  265. }
  266. // Serialize rendering pipeline
  267. public serialize(): any {
  268. var serializationObject = SerializationHelper.Serialize(this);
  269. serializationObject.customType = "DefaultRenderingPipeline";
  270. return serializationObject;
  271. }
  272. // Parse serialized pipeline
  273. public static Parse(source: any, scene: Scene, rootUrl: string): DefaultRenderingPipeline {
  274. return SerializationHelper.Parse(() => new DefaultRenderingPipeline(source._name, source._name._hdr, scene), source, scene, rootUrl);
  275. }
  276. }
  277. }