babylon.postProcessManager.ts 8.0 KB

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