postProcessManager.ts 8.4 KB

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