babylon.defaultRenderingPipeline.ts 15 KB

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