babylon.postProcessManager.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.PostProcessManager = function (scene) {
  4. this.postProcesses = [];
  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. if (this.postProcesses.length === 0 || !this._scene.postProcessesEnabled) {
  28. return;
  29. }
  30. this.postProcesses[0].activate();
  31. };
  32. BABYLON.PostProcessManager.prototype._finalizeFrame = function () {
  33. if (this.postProcesses.length === 0 || !this._scene.postProcessesEnabled) {
  34. return;
  35. }
  36. var engine = this._scene.getEngine();
  37. for (var index = 0; index < this.postProcesses.length; index++) {
  38. if (index < this.postProcesses.length - 1) {
  39. this.postProcesses[index + 1].activate();
  40. } else {
  41. engine.restoreDefaultFramebuffer();
  42. }
  43. var effect = this.postProcesses[index].apply();
  44. if (effect) {
  45. // VBOs
  46. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, effect);
  47. // Draw order
  48. engine.draw(true, 0, 6);
  49. }
  50. }
  51. };
  52. BABYLON.PostProcessManager.prototype.dispose = function () {
  53. if (this._vertexBuffer) {
  54. this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  55. this._vertexBuffer = null;
  56. }
  57. if (this._indexBuffer) {
  58. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  59. this._indexBuffer = null;
  60. }
  61. while (this.postProcesses.length) {
  62. this.postProcesses[0].dispose();
  63. }
  64. };
  65. })();