babylon.postProcess.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.PostProcess = function (name, fragmentUrl, parameters, samplers, ratio, camera, samplingMode) {
  5. this.name = name;
  6. this._camera = camera;
  7. this._scene = camera.getScene();
  8. camera.postProcesses.push(this);
  9. this._engine = this._scene.getEngine();
  10. this._renderRatio = ratio;
  11. this.width = -1;
  12. this.height = -1;
  13. this.renderTargetSamplingMode = samplingMode ? samplingMode : BABYLON.Texture.NEAREST_SAMPLINGMODE;
  14. samplers = samplers || [];
  15. samplers.push("textureSampler");
  16. this._effect = this._engine.createEffect({ vertex: "postprocess", fragment: fragmentUrl },
  17. ["position"],
  18. parameters || [],
  19. samplers, "");
  20. };
  21. // Methods
  22. BABYLON.PostProcess.prototype.onApply = null;
  23. BABYLON.PostProcess.prototype._onDispose = null;
  24. BABYLON.PostProcess.prototype.onSizeChanged = null;
  25. BABYLON.PostProcess.prototype.activate = function () {
  26. var desiredWidth = this._engine._renderingCanvas.width * this._renderRatio;
  27. var desiredHeight = this._engine._renderingCanvas.height * this._renderRatio;
  28. if (this.width !== desiredWidth || this.height !== desiredHeight) {
  29. if (this._texture) {
  30. this._engine._releaseTexture(this._texture);
  31. this._texture = null;
  32. }
  33. this.width = desiredWidth;
  34. this.height = desiredHeight;
  35. this._texture = this._engine.createRenderTargetTexture({ width: this.width, height: this.height }, { generateMipMaps: false, generateDepthBuffer: this._camera.postProcesses.indexOf(this) === 0, samplingMode: this.renderTargetSamplingMode });
  36. if (this.onSizeChanged) {
  37. this.onSizeChanged();
  38. }
  39. }
  40. this._engine.bindFramebuffer(this._texture);
  41. // Clear
  42. this._engine.clear(this._scene.clearColor, this._scene.autoClear || this._scene.forceWireframe, true);
  43. };
  44. BABYLON.PostProcess.prototype.apply = function () {
  45. // Check
  46. if (!this._effect.isReady())
  47. return null;
  48. // States
  49. this._engine.enableEffect(this._effect);
  50. this._engine.setState(false);
  51. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  52. this._engine.setDepthBuffer(false);
  53. this._engine.setDepthWrite(false);
  54. // Texture
  55. this._effect._bindTexture("textureSampler", this._texture);
  56. // Parameters
  57. if (this.onApply) {
  58. this.onApply(this._effect);
  59. }
  60. return this._effect;
  61. };
  62. BABYLON.PostProcess.prototype.dispose = function () {
  63. if (this._onDispose) {
  64. this._onDispose();
  65. }
  66. if (this._texture) {
  67. this._engine._releaseTexture(this._texture);
  68. this._texture = null;
  69. }
  70. var index = this._camera.postProcesses.indexOf(this);
  71. this._camera.postProcesses.splice(index, 1);
  72. if (index == 0 && this._camera.postProcesses.length > 0) {
  73. this._camera.postProcesses[0].width = -1; // invalidate frameBuffer to hint the postprocess to create a depth buffer
  74. }
  75. };
  76. })();