prePassRenderer.ts 9.4 KB

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