babylon.postProcess.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // Clear
  41. this._engine.clear(this._scene.clearColor, this._scene.autoClear || this._scene.forceWireframe, true);
  42. };
  43. BABYLON.PostProcess.prototype.apply = function () {
  44. // Check
  45. if (!this._effect.isReady())
  46. return null;
  47. // States
  48. this._engine.enableEffect(this._effect);
  49. this._engine.setState(false);
  50. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  51. this._engine.setDepthBuffer(false);
  52. this._engine.setDepthWrite(false);
  53. // Texture
  54. this._effect._bindTexture("textureSampler", this._texture);
  55. // Parameters
  56. if (this.onApply) {
  57. this.onApply(this._effect);
  58. }
  59. return this._effect;
  60. };
  61. BABYLON.PostProcess.prototype.dispose = function () {
  62. if (this._onDispose) {
  63. this._onDispose();
  64. }
  65. if (this._texture) {
  66. this._engine._releaseTexture(this._texture);
  67. this._texture = null;
  68. }
  69. var index = this._camera.postProcesses.indexOf(this);
  70. this._camera.postProcesses.splice(index, 1);
  71. if (index == 0 && this._camera.postProcesses.length > 0) {
  72. this._camera.postProcesses[0].width = -1; // invalidate frameBuffer to hint the postprocess to create a depth buffer
  73. }
  74. };
  75. })();