postProcessManager.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * @param postProcesses An array of post processes to be run.
  83. * @param targetTexture The target texture to render to.
  84. * @param forceFullscreenViewport force gl.viewport to be full screen eg. 0,0,textureWidth,textureHeight
  85. * @param faceIndex defines the face to render to if a cubemap is defined as the target
  86. * @param lodLevel defines which lod of the texture to render to
  87. */
  88. public directRender(postProcesses: PostProcess[], targetTexture: Nullable<InternalTexture> = null, forceFullscreenViewport = false, faceIndex = 0, lodLevel = 0): void {
  89. var engine = this._scene.getEngine();
  90. for (var index = 0; index < postProcesses.length; index++) {
  91. if (index < postProcesses.length - 1) {
  92. postProcesses[index + 1].activate(this._scene.activeCamera, targetTexture);
  93. } else {
  94. if (targetTexture) {
  95. engine.bindFramebuffer(targetTexture, faceIndex, undefined, undefined, forceFullscreenViewport, undefined, lodLevel);
  96. } else {
  97. engine.restoreDefaultFramebuffer();
  98. }
  99. }
  100. var pp = postProcesses[index];
  101. var effect = pp.apply();
  102. if (effect) {
  103. pp.onBeforeRenderObservable.notifyObservers(effect);
  104. // VBOs
  105. this._prepareBuffers();
  106. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);
  107. // Draw order
  108. engine.drawElementsType(Material.TriangleFillMode, 0, 6);
  109. pp.onAfterRenderObservable.notifyObservers(effect);
  110. }
  111. }
  112. // Restore depth buffer
  113. engine.setDepthBuffer(true);
  114. engine.setDepthWrite(true);
  115. }
  116. /**
  117. * Finalize the result of the output of the postprocesses.
  118. * @param doNotPresent If true the result will not be displayed to the screen.
  119. * @param targetTexture The target texture to render to.
  120. * @param faceIndex The index of the face to bind the target texture to.
  121. * @param postProcesses The array of post processes to render.
  122. * @param forceFullscreenViewport force gl.viewport to be full screen eg. 0,0,textureWidth,textureHeight (default: false)
  123. * @hidden
  124. */
  125. public _finalizeFrame(doNotPresent?: boolean, targetTexture?: InternalTexture, faceIndex?: number, postProcesses?: Array<PostProcess>, forceFullscreenViewport = false): void {
  126. let camera = this._scene.activeCamera;
  127. if (!camera) {
  128. return;
  129. }
  130. postProcesses = postProcesses || <Array<PostProcess>>camera._postProcesses.filter((pp) => { return pp != null; });
  131. if (postProcesses.length === 0 || !this._scene.postProcessesEnabled) {
  132. return;
  133. }
  134. var engine = this._scene.getEngine();
  135. for (var index = 0, len = postProcesses.length; index < len; index++) {
  136. var pp = postProcesses[index];
  137. if (index < len - 1) {
  138. pp._outputTexture = postProcesses[index + 1].activate(camera, targetTexture);
  139. } else {
  140. if (targetTexture) {
  141. engine.bindFramebuffer(targetTexture, faceIndex, undefined, undefined, forceFullscreenViewport);
  142. pp._outputTexture = targetTexture;
  143. } else {
  144. engine.restoreDefaultFramebuffer();
  145. pp._outputTexture = null;
  146. }
  147. }
  148. if (doNotPresent) {
  149. break;
  150. }
  151. var effect = pp.apply();
  152. if (effect) {
  153. pp.onBeforeRenderObservable.notifyObservers(effect);
  154. // VBOs
  155. this._prepareBuffers();
  156. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);
  157. // Draw order
  158. engine.drawElementsType(Material.TriangleFillMode, 0, 6);
  159. pp.onAfterRenderObservable.notifyObservers(effect);
  160. }
  161. }
  162. // Restore states
  163. engine.setDepthBuffer(true);
  164. engine.setDepthWrite(true);
  165. engine.setAlphaMode(Constants.ALPHA_DISABLE);
  166. }
  167. /**
  168. * Disposes of the post process manager.
  169. */
  170. public dispose(): void {
  171. var buffer = this._vertexBuffers[VertexBuffer.PositionKind];
  172. if (buffer) {
  173. buffer.dispose();
  174. this._vertexBuffers[VertexBuffer.PositionKind] = null;
  175. }
  176. if (this._indexBuffer) {
  177. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  178. this._indexBuffer = null;
  179. }
  180. }
  181. }