prePassRenderer.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import { MultiRenderTarget } from "../Materials/Textures/multiRenderTarget";
  2. import { Scene } from "../scene";
  3. import { Engine } from "../Engines/engine";
  4. import { Constants } from "../Engines/constants";
  5. import { ImageProcessingPostProcess } from "../PostProcesses/imageProcessingPostProcess";
  6. import { PostProcess } from "../PostProcesses/postProcess";
  7. import { Effect } from "../Materials/effect";
  8. import { _DevTools } from '../Misc/devTools';
  9. import { Color4 } from "../Maths/math.color";
  10. import { SubSurfaceConfiguration } from "./subSurfaceConfiguration";
  11. /**
  12. * Renders a pre pass of the scene
  13. * This means every mesh in the scene will be rendered to a render target texture
  14. * And then this texture will be composited to the rendering canvas with post processes
  15. * It is necessary for effects like subsurface scattering or deferred shading
  16. */
  17. export class PrePassRenderer {
  18. /** @hidden */
  19. public static _SceneComponentInitialization: (scene: Scene) => void = (_) => {
  20. throw _DevTools.WarnImport("PrePassRendererSceneComponent");
  21. }
  22. private _scene: Scene;
  23. private _engine: Engine;
  24. private _isDirty: boolean = false;
  25. /**
  26. * Number of textures in the multi render target texture where the scene is directly rendered
  27. */
  28. public readonly mrtCount: number = 4;
  29. /**
  30. * The render target where the scene is directly rendered
  31. */
  32. public prePassRT: MultiRenderTarget;
  33. private _mrtTypes = [
  34. Constants.TEXTURETYPE_HALF_FLOAT, // Original color
  35. Constants.TEXTURETYPE_HALF_FLOAT, // Irradiance
  36. Constants.TEXTURETYPE_HALF_FLOAT, // Depth (world units)
  37. Constants.TEXTURETYPE_UNSIGNED_INT // Albedo
  38. ];
  39. private _multiRenderAttachments: number[];
  40. private _defaultAttachments: number[];
  41. private _clearAttachments: number[];
  42. private _postProcesses: PostProcess[] = [];
  43. private readonly _clearColor = new Color4(0, 0, 0, 0);
  44. /**
  45. * Image processing post process for composition
  46. */
  47. public imageProcessingPostProcess: ImageProcessingPostProcess;
  48. /**
  49. * Configuration for sub surface scattering post process
  50. */
  51. public subSurfaceConfiguration: SubSurfaceConfiguration;
  52. /**
  53. * Should materials render their geometry on the MRT
  54. */
  55. public materialsShouldRenderGeometry: boolean = false;
  56. /**
  57. * Should materials render the irradiance information on the MRT
  58. */
  59. public materialsShouldRenderIrradiance: boolean = false;
  60. private _enabled: boolean = false;
  61. /**
  62. * Indicates if the prepass is enabled
  63. */
  64. public get enabled() {
  65. return this._enabled;
  66. }
  67. /**
  68. * How many samples are used for MSAA of the scene render target
  69. */
  70. public get samples() {
  71. return this.prePassRT.samples;
  72. }
  73. public set samples(n: number) {
  74. if (!this.imageProcessingPostProcess) {
  75. this._createCompositionEffect();
  76. }
  77. this.prePassRT.samples = n;
  78. }
  79. /**
  80. * Instanciates a prepass renderer
  81. * @param scene The scene
  82. */
  83. constructor(scene: Scene) {
  84. this._scene = scene;
  85. this._engine = scene.getEngine();
  86. PrePassRenderer._SceneComponentInitialization(this._scene);
  87. this.subSurfaceConfiguration = new SubSurfaceConfiguration(this._scene);
  88. }
  89. private _initializeAttachments() {
  90. let gl = this._engine._gl;
  91. this._multiRenderAttachments = [];
  92. this._clearAttachments = [gl.NONE];
  93. this._defaultAttachments = [gl.COLOR_ATTACHMENT0];
  94. for (let i = 0; i < this.mrtCount; i++) {
  95. this._multiRenderAttachments.push((<any>gl)["COLOR_ATTACHMENT" + i]);
  96. if (i > 0) {
  97. this._clearAttachments.push((<any>gl)["COLOR_ATTACHMENT" + i]);
  98. this._defaultAttachments.push(gl.NONE);
  99. }
  100. }
  101. }
  102. private _createCompositionEffect() {
  103. this.prePassRT = new MultiRenderTarget("sceneprePassRT", { width: this._engine.getRenderWidth(), height: this._engine.getRenderHeight() }, this.mrtCount, this._scene,
  104. { generateMipMaps: false, generateDepthTexture: true, defaultType: Constants.TEXTURETYPE_UNSIGNED_INT, types: this._mrtTypes });
  105. this.prePassRT.samples = 1;
  106. this._initializeAttachments();
  107. this.imageProcessingPostProcess = new ImageProcessingPostProcess("sceneCompositionPass", 1, null, undefined, this._engine);
  108. this.imageProcessingPostProcess.autoClear = false;
  109. }
  110. /**
  111. * Indicates if rendering a prepass is supported
  112. */
  113. public get isSupported() {
  114. return this._engine.webGLVersion > 1;
  115. }
  116. /**
  117. * Sets the proper output textures to draw in the engine.
  118. * @param effect The effect that is drawn. It can be or not be compatible with drawing to several output textures.
  119. */
  120. public bindAttachmentsForEffect(effect: Effect) {
  121. if (this.enabled) {
  122. if (effect._multiTarget) {
  123. this._engine.bindAttachments(this._multiRenderAttachments);
  124. } else {
  125. this._engine.bindAttachments(this._defaultAttachments);
  126. }
  127. }
  128. }
  129. /**
  130. * @hidden
  131. */
  132. public _beforeCameraDraw() {
  133. if (this._isDirty) {
  134. this._update();
  135. }
  136. this._bindFrameBuffer();
  137. }
  138. /**
  139. * @hidden
  140. */
  141. public _afterCameraDraw() {
  142. if (this._enabled) {
  143. const firstCameraPP = this._scene.activeCamera && this._scene.activeCamera._getFirstPostProcess();
  144. if (firstCameraPP) {
  145. this._scene.postProcessManager._prepareFrame();
  146. }
  147. this._scene.postProcessManager.directRender(this._postProcesses, firstCameraPP ? firstCameraPP.inputTexture : null);
  148. }
  149. }
  150. private _checkRTSize() {
  151. var requiredWidth = this._engine.getRenderWidth(true);
  152. var requiredHeight = this._engine.getRenderHeight(true);
  153. var width = this.prePassRT.getRenderWidth();
  154. var height = this.prePassRT.getRenderHeight();
  155. if (width !== requiredWidth || height !== requiredHeight) {
  156. this.prePassRT.resize({ width: requiredWidth, height: requiredHeight });
  157. this._bindPostProcessChain();
  158. }
  159. }
  160. private _bindFrameBuffer() {
  161. if (this._enabled) {
  162. this._checkRTSize();
  163. var internalTexture = this.prePassRT.getInternalTexture();
  164. if (internalTexture) {
  165. this._engine.bindFramebuffer(internalTexture);
  166. }
  167. }
  168. }
  169. /**
  170. * Clears the scene render target (in the sense of settings pixels to the scene clear color value)
  171. */
  172. public clear() {
  173. if (this._enabled) {
  174. this._bindFrameBuffer();
  175. // Regular clear color with the scene clear color of the 1st attachment
  176. this._engine.clear(this._scene.clearColor,
  177. this._scene.autoClear || this._scene.forceWireframe || this._scene.forcePointsCloud,
  178. this._scene.autoClearDepthAndStencil,
  179. this._scene.autoClearDepthAndStencil);
  180. // Clearing other attachment with 0 on all other attachments
  181. this._engine.bindAttachments(this._clearAttachments);
  182. this._engine.clear(this._clearColor, true, false, false);
  183. this._engine.bindAttachments(this._multiRenderAttachments);
  184. }
  185. }
  186. private _setState(enabled: boolean) {
  187. this._enabled = enabled;
  188. this._scene.prePass = enabled;
  189. if (this.imageProcessingPostProcess) {
  190. this.imageProcessingPostProcess.imageProcessingConfiguration.applyByPostProcess = enabled;
  191. }
  192. }
  193. private _enable() {
  194. this._resetPostProcessChain();
  195. if (this.subSurfaceConfiguration.enabled) {
  196. if (!this.subSurfaceConfiguration.postProcess) {
  197. this.subSurfaceConfiguration.createPostProcess();
  198. }
  199. this._postProcesses.push(this.subSurfaceConfiguration.postProcess);
  200. }
  201. if (!this.imageProcessingPostProcess) {
  202. this._createCompositionEffect();
  203. }
  204. this._postProcesses.push(this.imageProcessingPostProcess);
  205. this._bindPostProcessChain();
  206. this._setState(true);
  207. }
  208. private _disable() {
  209. this._setState(false);
  210. this.subSurfaceConfiguration.enabled = false;
  211. this.materialsShouldRenderGeometry = false;
  212. this.materialsShouldRenderIrradiance = false;
  213. }
  214. private _resetPostProcessChain() {
  215. this._postProcesses = [];
  216. if (this.imageProcessingPostProcess) {
  217. this.imageProcessingPostProcess.restoreDefaultInputTexture();
  218. }
  219. if (this.subSurfaceConfiguration.postProcess) {
  220. this.subSurfaceConfiguration.postProcess.restoreDefaultInputTexture();
  221. }
  222. }
  223. private _bindPostProcessChain() {
  224. this._postProcesses[0].inputTexture = this.prePassRT.getInternalTexture()!;
  225. }
  226. /**
  227. * Marks the prepass renderer as dirty, triggering a check if the prepass is necessary for the next rendering.
  228. */
  229. public markAsDirty() {
  230. this._isDirty = true;
  231. }
  232. private _update() {
  233. this._disable();
  234. let enablePrePass = false;
  235. // Subsurface scattering
  236. for (let i = 0; i < this._scene.materials.length; i++) {
  237. if (this._scene.materials[i].setPrePassRenderer(this)) {
  238. enablePrePass = true;
  239. }
  240. }
  241. const pipelines = this._scene.postProcessRenderPipelineManager.supportedPipelines;
  242. for (let i = 0; i < pipelines.length; i++) {
  243. if (pipelines[i].setPrePassRenderer(this)) {
  244. enablePrePass = true;
  245. }
  246. }
  247. this._isDirty = false;
  248. if (enablePrePass) {
  249. this._enable();
  250. }
  251. if (!this.enabled) {
  252. this._engine.bindAttachments(this._defaultAttachments);
  253. }
  254. }
  255. /**
  256. * Disposes the prepass renderer.
  257. */
  258. public dispose() {
  259. this.imageProcessingPostProcess.dispose();
  260. this.subSurfaceConfiguration.dispose();
  261. this.prePassRT.dispose();
  262. }
  263. }