babylon.postProcess.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.PostProcess = function (name, fragmentUrl, parameters, samplers, ratio, camera) {
  4. this.name = name;
  5. this._camera = camera;
  6. this._scene = camera.getScene();
  7. camera.postProcesses.push(this);
  8. this._engine = this._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._onDispose = null;
  22. BABYLON.PostProcess.prototype.activate = function() {
  23. this._engine.bindFramebuffer(this._texture);
  24. };
  25. BABYLON.PostProcess.prototype.apply = function () {
  26. // Check
  27. if (!this._effect.isReady())
  28. return null;
  29. // Render
  30. this._engine.enableEffect(this._effect);
  31. this._engine.setState(false);
  32. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  33. // Texture
  34. this._effect._bindTexture("textureSampler", this._texture);
  35. // Parameters
  36. if (this.onApply) {
  37. this.onApply(this._effect);
  38. }
  39. return this._effect;
  40. };
  41. BABYLON.PostProcess.prototype.dispose = function () {
  42. if (this._onDispose) {
  43. this._onDispose();
  44. }
  45. this._engine._releaseTexture(this._texture);
  46. var index = this._camera.postProcesses.indexOf(this);
  47. this._camera.postProcesses.splice(index, 1);
  48. };
  49. })();