postProcessManager.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { Nullable } from "../types";
  2. import { Material } from "../Materials/material";
  3. import { InternalTexture } from "../Materials/Textures/internalTexture";
  4. import { PostProcess } from "./postProcess";
  5. import { VertexBuffer } from "../Meshes/buffer";
  6. import { Constants } from "../Engines/constants";
  7. import { DataBuffer } from '../Meshes/dataBuffer';
  8. declare type Scene = import("../scene").Scene;
  9. /**
  10. * PostProcessManager is used to manage one or more post processes or post process pipelines
  11. * See https://doc.babylonjs.com/how_to/how_to_use_postprocesses
  12. */
  13. export class PostProcessManager {
  14. private _scene: Scene;
  15. private _indexBuffer: Nullable<DataBuffer>;
  16. private _vertexBuffers: { [key: string]: Nullable<VertexBuffer> } = {};
  17. /**
  18. * Creates a new instance PostProcess
  19. * @param scene The scene that the post process is associated with.
  20. */
  21. constructor(scene: Scene) {
  22. this._scene = scene;
  23. }
  24. private _prepareBuffers(): void {
  25. if (this._vertexBuffers[VertexBuffer.PositionKind]) {
  26. return;
  27. }
  28. // VBO
  29. var vertices = [];
  30. vertices.push(1, 1);
  31. vertices.push(-1, 1);
  32. vertices.push(-1, -1);
  33. vertices.push(1, -1);
  34. this._vertexBuffers[VertexBuffer.PositionKind] = new VertexBuffer(this._scene.getEngine(), vertices, VertexBuffer.PositionKind, false, false, 2);
  35. this._buildIndexBuffer();
  36. }
  37. private _buildIndexBuffer(): void {
  38. // Indices
  39. var indices = [];
  40. indices.push(0);
  41. indices.push(1);
  42. indices.push(2);
  43. indices.push(0);
  44. indices.push(2);
  45. indices.push(3);
  46. this._indexBuffer = this._scene.getEngine().createIndexBuffer(indices);
  47. }
  48. /**
  49. * Rebuilds the vertex buffers of the manager.
  50. * @hidden
  51. */
  52. public _rebuild(): void {
  53. let vb = this._vertexBuffers[VertexBuffer.PositionKind];
  54. if (!vb) {
  55. return;
  56. }
  57. vb._rebuild();
  58. this._buildIndexBuffer();
  59. }
  60. // Methods
  61. /**
  62. * Prepares a frame to be run through a post process.
  63. * @param sourceTexture The input texture to the post procesess. (default: null)
  64. * @param postProcesses An array of post processes to be run. (default: null)
  65. * @returns True if the post processes were able to be run.
  66. * @hidden
  67. */
  68. public _prepareFrame(sourceTexture: Nullable<InternalTexture> = null, postProcesses: Nullable<PostProcess[]> = null): boolean {
  69. let camera = this._scene.activeCamera;
  70. if (!camera) {
  71. return false;
  72. }
  73. postProcesses = postProcesses || (<Nullable<PostProcess[]>>camera._postProcesses.filter((pp) => { return pp != null; }));
  74. if (!postProcesses || postProcesses.length === 0 || !this._scene.postProcessesEnabled) {
  75. return false;
  76. }
  77. postProcesses[0].activate(camera, sourceTexture, postProcesses !== null && postProcesses !== undefined);
  78. return true;
  79. }
  80. /**
  81. * Manually render a set of post processes to a texture.
  82. * Please note, the frame buffer won't be unbound after the call in case you have more render to do.
  83. * @param postProcesses An array of post processes to be run.
  84. * @param targetTexture The target texture to render to.
  85. * @param forceFullscreenViewport force gl.viewport to be full screen eg. 0,0,textureWidth,textureHeight
  86. * @param faceIndex defines the face to render to if a cubemap is defined as the target
  87. * @param lodLevel defines which lod of the texture to render to
  88. */
  89. public directRender(postProcesses: PostProcess[], targetTexture: Nullable<InternalTexture> = null, forceFullscreenViewport = false, faceIndex = 0, lodLevel = 0): void {
  90. var engine = this._scene.getEngine();
  91. for (var index = 0; index < postProcesses.length; index++) {
  92. if (index < postProcesses.length - 1) {
  93. postProcesses[index + 1].activate(this._scene.activeCamera, targetTexture);
  94. } else {
  95. if (targetTexture) {
  96. engine.bindFramebuffer(targetTexture, faceIndex, undefined, undefined, forceFullscreenViewport, lodLevel);
  97. } else {
  98. engine.restoreDefaultFramebuffer();
  99. }
  100. }
  101. var pp = postProcesses[index];
  102. var effect = pp.apply();
  103. if (effect) {
  104. pp.onBeforeRenderObservable.notifyObservers(effect);
  105. // VBOs
  106. this._prepareBuffers();
  107. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);
  108. // Draw order
  109. engine.drawElementsType(Material.TriangleFillMode, 0, 6);
  110. pp.onAfterRenderObservable.notifyObservers(effect);
  111. }
  112. }
  113. // Restore depth buffer
  114. engine.setDepthBuffer(true);
  115. engine.setDepthWrite(true);
  116. }
  117. /**
  118. * Finalize the result of the output of the postprocesses.
  119. * @param doNotPresent If true the result will not be displayed to the screen.
  120. * @param targetTexture The target texture to render to.
  121. * @param faceIndex The index of the face to bind the target texture to.
  122. * @param postProcesses The array of post processes to render.
  123. * @param forceFullscreenViewport force gl.viewport to be full screen eg. 0,0,textureWidth,textureHeight (default: false)
  124. * @hidden
  125. */
  126. public _finalizeFrame(doNotPresent?: boolean, targetTexture?: InternalTexture, faceIndex?: number, postProcesses?: Array<PostProcess>, forceFullscreenViewport = false): void {
  127. let camera = this._scene.activeCamera;
  128. if (!camera) {
  129. return;
  130. }
  131. postProcesses = postProcesses || <Array<PostProcess>>camera._postProcesses.filter((pp) => { return pp != null; });
  132. if (postProcesses.length === 0 || !this._scene.postProcessesEnabled) {
  133. return;
  134. }
  135. var engine = this._scene.getEngine();
  136. for (var index = 0, len = postProcesses.length; index < len; index++) {
  137. var pp = postProcesses[index];
  138. if (index < len - 1) {
  139. pp._outputTexture = postProcesses[index + 1].activate(camera, targetTexture);
  140. } else {
  141. if (targetTexture) {
  142. engine.bindFramebuffer(targetTexture, faceIndex, undefined, undefined, forceFullscreenViewport);
  143. pp._outputTexture = targetTexture;
  144. } else {
  145. engine.restoreDefaultFramebuffer();
  146. pp._outputTexture = null;
  147. }
  148. }
  149. if (doNotPresent) {
  150. break;
  151. }
  152. var effect = pp.apply();
  153. if (effect) {
  154. pp.onBeforeRenderObservable.notifyObservers(effect);
  155. // VBOs
  156. this._prepareBuffers();
  157. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);
  158. // Draw order
  159. engine.drawElementsType(Material.TriangleFillMode, 0, 6);
  160. pp.onAfterRenderObservable.notifyObservers(effect);
  161. }
  162. }
  163. // Restore states
  164. engine.setDepthBuffer(true);
  165. engine.setDepthWrite(true);
  166. engine.setAlphaMode(Constants.ALPHA_DISABLE);
  167. }
  168. /**
  169. * Disposes of the post process manager.
  170. */
  171. public dispose(): void {
  172. var buffer = this._vertexBuffers[VertexBuffer.PositionKind];
  173. if (buffer) {
  174. buffer.dispose();
  175. this._vertexBuffers[VertexBuffer.PositionKind] = null;
  176. }
  177. if (this._indexBuffer) {
  178. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  179. this._indexBuffer = null;
  180. }
  181. }
  182. }