babylon.postProcess.js 3.2 KB

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