babylon.postProcessManager.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.PostProcessManager = function (scene) {
  5. this._scene = scene;
  6. // VBO
  7. var vertices = [];
  8. vertices.push(1, 1);
  9. vertices.push(-1, 1);
  10. vertices.push(-1, -1);
  11. vertices.push(1, -1);
  12. this._vertexDeclaration = [2];
  13. this._vertexStrideSize = 2 * 4;
  14. this._vertexBuffer = scene.getEngine().createVertexBuffer(vertices);
  15. // Indices
  16. var indices = [];
  17. indices.push(0);
  18. indices.push(1);
  19. indices.push(2);
  20. indices.push(0);
  21. indices.push(2);
  22. indices.push(3);
  23. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  24. };
  25. // Methods
  26. BABYLON.PostProcessManager.prototype._prepareFrame = function () {
  27. var postProcesses = this._scene.activeCamera._postProcesses;
  28. if (postProcesses.length === 0 || !this._scene.postProcessesEnabled) {
  29. return;
  30. }
  31. postProcesses[this._scene.activeCamera._postProcessesTakenIndices[0]].activate(this._scene.activeCamera);
  32. };
  33. BABYLON.PostProcessManager.prototype._finalizeFrame = function () {
  34. var postProcesses = this._scene.activeCamera._postProcesses;
  35. var postProcessesTakenIndices = this._scene.activeCamera._postProcessesTakenIndices;
  36. if (postProcesses.length === 0 || !this._scene.postProcessesEnabled) {
  37. return;
  38. }
  39. var engine = this._scene.getEngine();
  40. for (var index = 0; index < postProcessesTakenIndices.length; index++) {
  41. if (postProcessesTakenIndices[index] < postProcessesTakenIndices.length - 1) {
  42. postProcesses[postProcessesTakenIndices[index + 1]].activate(this._scene.activeCamera);
  43. } else {
  44. engine.restoreDefaultFramebuffer();
  45. }
  46. var effect = postProcesses[postProcessesTakenIndices[index]].apply();
  47. if (effect) {
  48. // VBOs
  49. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, effect);
  50. // Draw order
  51. engine.draw(true, 0, 6);
  52. }
  53. }
  54. // Restore depth buffer
  55. engine.setDepthBuffer(true);
  56. engine.setDepthWrite(true);
  57. };
  58. BABYLON.PostProcessManager.prototype.dispose = function () {
  59. if (this._vertexBuffer) {
  60. this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  61. this._vertexBuffer = null;
  62. }
  63. if (this._indexBuffer) {
  64. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  65. this._indexBuffer = null;
  66. }
  67. };
  68. })();