ssaoRenderingPipeline.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import { Vector2, Vector3, TmpVectors } from "../../../Maths/math.vector";
  2. import { Camera } from "../../../Cameras/camera";
  3. import { Effect } from "../../../Materials/effect";
  4. import { Texture } from "../../../Materials/Textures/texture";
  5. import { DynamicTexture } from "../../../Materials/Textures/dynamicTexture";
  6. import { RenderTargetTexture } from "../../../Materials/Textures/renderTargetTexture";
  7. import { PostProcess } from "../../../PostProcesses/postProcess";
  8. import { PostProcessRenderPipeline } from "../../../PostProcesses/RenderPipeline/postProcessRenderPipeline";
  9. import { PostProcessRenderEffect } from "../../../PostProcesses/RenderPipeline/postProcessRenderEffect";
  10. import { PassPostProcess } from "../../../PostProcesses/passPostProcess";
  11. import { BlurPostProcess } from "../../../PostProcesses/blurPostProcess";
  12. import { Constants } from "../../../Engines/constants";
  13. import { serialize } from "../../../Misc/decorators";
  14. import { Scene } from "../../../scene";
  15. import "../../../PostProcesses/RenderPipeline/postProcessRenderPipelineManagerSceneComponent";
  16. import "../../../Shaders/ssao.fragment";
  17. import "../../../Shaders/ssaoCombine.fragment";
  18. /**
  19. * Render pipeline to produce ssao effect
  20. */
  21. export class SSAORenderingPipeline extends PostProcessRenderPipeline {
  22. // Members
  23. /**
  24. * @ignore
  25. * The PassPostProcess id in the pipeline that contains the original scene color
  26. */
  27. public SSAOOriginalSceneColorEffect: string = "SSAOOriginalSceneColorEffect";
  28. /**
  29. * @ignore
  30. * The SSAO PostProcess id in the pipeline
  31. */
  32. public SSAORenderEffect: string = "SSAORenderEffect";
  33. /**
  34. * @ignore
  35. * The horizontal blur PostProcess id in the pipeline
  36. */
  37. public SSAOBlurHRenderEffect: string = "SSAOBlurHRenderEffect";
  38. /**
  39. * @ignore
  40. * The vertical blur PostProcess id in the pipeline
  41. */
  42. public SSAOBlurVRenderEffect: string = "SSAOBlurVRenderEffect";
  43. /**
  44. * @ignore
  45. * The PostProcess id in the pipeline that combines the SSAO-Blur output with the original scene color (SSAOOriginalSceneColorEffect)
  46. */
  47. public SSAOCombineRenderEffect: string = "SSAOCombineRenderEffect";
  48. /**
  49. * The output strength of the SSAO post-process. Default value is 1.0.
  50. */
  51. @serialize()
  52. public totalStrength: number = 1.0;
  53. /**
  54. * The radius around the analyzed pixel used by the SSAO post-process. Default value is 0.0006
  55. */
  56. @serialize()
  57. public radius: number = 0.0001;
  58. /**
  59. * Related to fallOff, used to interpolate SSAO samples (first interpolate function input) based on the occlusion difference of each pixel
  60. * Must not be equal to fallOff and superior to fallOff.
  61. * Default value is 0.0075
  62. */
  63. @serialize()
  64. public area: number = 0.0075;
  65. /**
  66. * Related to area, used to interpolate SSAO samples (second interpolate function input) based on the occlusion difference of each pixel
  67. * Must not be equal to area and inferior to area.
  68. * Default value is 0.000001
  69. */
  70. @serialize()
  71. public fallOff: number = 0.000001;
  72. /**
  73. * The base color of the SSAO post-process
  74. * The final result is "base + ssao" between [0, 1]
  75. */
  76. @serialize()
  77. public base: number = 0.5;
  78. private _scene: Scene;
  79. private _depthTexture: RenderTargetTexture;
  80. private _randomTexture: DynamicTexture;
  81. private _originalColorPostProcess: PassPostProcess;
  82. private _ssaoPostProcess: PostProcess;
  83. private _blurHPostProcess: BlurPostProcess;
  84. private _blurVPostProcess: BlurPostProcess;
  85. private _ssaoCombinePostProcess: PostProcess;
  86. private _firstUpdate: boolean = true;
  87. /**
  88. * Gets active scene
  89. */
  90. public get scene(): Scene {
  91. return this._scene;
  92. }
  93. /**
  94. * @constructor
  95. * @param name - The rendering pipeline name
  96. * @param scene - The scene linked to this pipeline
  97. * @param ratio - The size of the postprocesses. Can be a number shared between passes or an object for more precision: { ssaoRatio: 0.5, combineRatio: 1.0 }
  98. * @param cameras - The array of cameras that the rendering pipeline will be attached to
  99. */
  100. constructor(name: string, scene: Scene, ratio: any, cameras?: Camera[]) {
  101. super(scene.getEngine(), name);
  102. this._scene = scene;
  103. // Set up assets
  104. this._createRandomTexture();
  105. this._depthTexture = scene.enableDepthRenderer().getDepthMap(); // Force depth renderer "on"
  106. var ssaoRatio = ratio.ssaoRatio || ratio;
  107. var combineRatio = ratio.combineRatio || ratio;
  108. this._originalColorPostProcess = new PassPostProcess("SSAOOriginalSceneColor", combineRatio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  109. this._createSSAOPostProcess(ssaoRatio);
  110. this._createBlurPostProcess(ssaoRatio);
  111. this._createSSAOCombinePostProcess(combineRatio);
  112. // Set up pipeline
  113. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOOriginalSceneColorEffect, () => { return this._originalColorPostProcess; }, true));
  114. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAORenderEffect, () => { return this._ssaoPostProcess; }, true));
  115. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurHRenderEffect, () => { return this._blurHPostProcess; }, true));
  116. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurVRenderEffect, () => { return this._blurVPostProcess; }, true));
  117. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOCombineRenderEffect, () => { return this._ssaoCombinePostProcess; }, true));
  118. // Finish
  119. scene.postProcessRenderPipelineManager.addPipeline(this);
  120. if (cameras) {
  121. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(name, cameras);
  122. }
  123. }
  124. // Public Methods
  125. /**
  126. * Get the class name
  127. * @returns "SSAORenderingPipeline"
  128. */
  129. public getClassName(): string {
  130. return "SSAORenderingPipeline";
  131. }
  132. /**
  133. * Removes the internal pipeline assets and detatches the pipeline from the scene cameras
  134. */
  135. public dispose(disableDepthRender: boolean = false): void {
  136. for (var i = 0; i < this._scene.cameras.length; i++) {
  137. var camera = this._scene.cameras[i];
  138. this._originalColorPostProcess.dispose(camera);
  139. this._ssaoPostProcess.dispose(camera);
  140. this._blurHPostProcess.dispose(camera);
  141. this._blurVPostProcess.dispose(camera);
  142. this._ssaoCombinePostProcess.dispose(camera);
  143. }
  144. this._randomTexture.dispose();
  145. if (disableDepthRender) {
  146. this._scene.disableDepthRenderer();
  147. }
  148. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
  149. super.dispose();
  150. }
  151. // Private Methods
  152. private _createBlurPostProcess(ratio: number): void {
  153. var size = 16;
  154. this._blurHPostProcess = new BlurPostProcess("BlurH", new Vector2(1, 0), size, ratio, null, Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, Constants.TEXTURETYPE_UNSIGNED_INT);
  155. this._blurVPostProcess = new BlurPostProcess("BlurV", new Vector2(0, 1), size, ratio, null, Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, Constants.TEXTURETYPE_UNSIGNED_INT);
  156. this._blurHPostProcess.onActivateObservable.add(() => {
  157. let dw = this._blurHPostProcess.width / this._scene.getEngine().getRenderWidth();
  158. this._blurHPostProcess.kernel = size * dw;
  159. });
  160. this._blurVPostProcess.onActivateObservable.add(() => {
  161. let dw = this._blurVPostProcess.height / this._scene.getEngine().getRenderHeight();
  162. this._blurVPostProcess.kernel = size * dw;
  163. });
  164. }
  165. /** @hidden */
  166. public _rebuild() {
  167. this._firstUpdate = true;
  168. super._rebuild();
  169. }
  170. private _createSSAOPostProcess(ratio: number): void {
  171. var numSamples = 16;
  172. var sampleSphere = [
  173. 0.5381, 0.1856, -0.4319,
  174. 0.1379, 0.2486, 0.4430,
  175. 0.3371, 0.5679, -0.0057,
  176. -0.6999, -0.0451, -0.0019,
  177. 0.0689, -0.1598, -0.8547,
  178. 0.0560, 0.0069, -0.1843,
  179. -0.0146, 0.1402, 0.0762,
  180. 0.0100, -0.1924, -0.0344,
  181. -0.3577, -0.5301, -0.4358,
  182. -0.3169, 0.1063, 0.0158,
  183. 0.0103, -0.5869, 0.0046,
  184. -0.0897, -0.4940, 0.3287,
  185. 0.7119, -0.0154, -0.0918,
  186. -0.0533, 0.0596, -0.5411,
  187. 0.0352, -0.0631, 0.5460,
  188. -0.4776, 0.2847, -0.0271
  189. ];
  190. var samplesFactor = 1.0 / numSamples;
  191. this._ssaoPostProcess = new PostProcess("ssao", "ssao",
  192. [
  193. "sampleSphere", "samplesFactor", "randTextureTiles", "totalStrength", "radius",
  194. "area", "fallOff", "base", "range", "viewport"
  195. ],
  196. ["randomSampler"],
  197. ratio, null, Texture.BILINEAR_SAMPLINGMODE,
  198. this._scene.getEngine(), false,
  199. "#define SAMPLES " + numSamples + "\n#define SSAO");
  200. this._ssaoPostProcess.onApply = (effect: Effect) => {
  201. if (this._firstUpdate) {
  202. effect.setArray3("sampleSphere", sampleSphere);
  203. effect.setFloat("samplesFactor", samplesFactor);
  204. effect.setFloat("randTextureTiles", 4.0);
  205. }
  206. effect.setFloat("totalStrength", this.totalStrength);
  207. effect.setFloat("radius", this.radius);
  208. effect.setFloat("area", this.area);
  209. effect.setFloat("fallOff", this.fallOff);
  210. effect.setFloat("base", this.base);
  211. effect.setTexture("textureSampler", this._depthTexture);
  212. effect.setTexture("randomSampler", this._randomTexture);
  213. };
  214. }
  215. private _createSSAOCombinePostProcess(ratio: number): void {
  216. this._ssaoCombinePostProcess = new PostProcess("ssaoCombine", "ssaoCombine", [], ["originalColor", "viewport"],
  217. ratio, null, Texture.BILINEAR_SAMPLINGMODE,
  218. this._scene.getEngine(), false);
  219. this._ssaoCombinePostProcess.onApply = (effect: Effect) => {
  220. effect.setVector4("viewport", TmpVectors.Vector4[0].copyFromFloats(0, 0, 1.0, 1.0));
  221. effect.setTextureFromPostProcess("originalColor", this._originalColorPostProcess);
  222. };
  223. }
  224. private _createRandomTexture(): void {
  225. var size = 512;
  226. this._randomTexture = new DynamicTexture("SSAORandomTexture", size, this._scene, false, Texture.TRILINEAR_SAMPLINGMODE);
  227. this._randomTexture.wrapU = Texture.WRAP_ADDRESSMODE;
  228. this._randomTexture.wrapV = Texture.WRAP_ADDRESSMODE;
  229. var context = this._randomTexture.getContext();
  230. var rand = (min: number, max: number) => {
  231. return Math.random() * (max - min) + min;
  232. };
  233. var randVector = Vector3.Zero();
  234. for (var x = 0; x < size; x++) {
  235. for (var y = 0; y < size; y++) {
  236. randVector.x = Math.floor(rand(-1.0, 1.0) * 255);
  237. randVector.y = Math.floor(rand(-1.0, 1.0) * 255);
  238. randVector.z = Math.floor(rand(-1.0, 1.0) * 255);
  239. context.fillStyle = 'rgb(' + randVector.x + ', ' + randVector.y + ', ' + randVector.z + ')';
  240. context.fillRect(x, y, 1, 1);
  241. }
  242. }
  243. this._randomTexture.update(false);
  244. }
  245. }