babylon.postProcess.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.PostProcess = function (name, fragmentUrl, parameters, samplers, ratio, scene) {
  4. this.name = name;
  5. this._scene = scene;
  6. this._manager = scene.postProcessManager;
  7. this._manager.postProcesses.push(this);
  8. this._engine = scene.getEngine();
  9. this.width = this._engine._renderingCanvas.width * ratio;
  10. this.height = this._engine._renderingCanvas.height * ratio;
  11. this._texture = this._engine.createRenderTargetTexture({ width: this.width, height: this.height }, false);
  12. samplers = samplers || [];
  13. samplers.push("textureSampler");
  14. this._effect = this._engine.createEffect({ vertex: "postprocess", fragment: fragmentUrl },
  15. ["position"],
  16. parameters || [],
  17. samplers, "");
  18. };
  19. // Methods
  20. BABYLON.PostProcess.prototype.onApply = null;
  21. BABYLON.PostProcess.prototype.activate = function() {
  22. this._engine.bindFramebuffer(this._texture);
  23. };
  24. BABYLON.PostProcess.prototype.apply = function () {
  25. // Check
  26. if (!this._effect.isReady())
  27. return null;
  28. // Render
  29. this._engine.enableEffect(this._effect);
  30. this._engine.setState(false);
  31. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  32. // Texture
  33. this._effect._bindTexture("textureSampler", this._texture);
  34. // Parameters
  35. if (this.onApply) {
  36. this.onApply(this._effect);
  37. }
  38. return this._effect;
  39. };
  40. BABYLON.PostProcess.prototype.dispose = function() {
  41. this._engine._releaseTexture(this._texture);
  42. var index = this._manager.postProcesses.indexOf(this);
  43. this._manager.postProcesses.splice(index, 1);
  44. };
  45. })();