babylon.postProcessManager.js 2.7 KB

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