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