postProcessManager.ts 8.3 KB

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