babylon.postProcess.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.TextureSamplingModes.NEAREST;
  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. // Render
  46. this._engine.enableEffect(this._effect);
  47. this._engine.setState(false);
  48. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  49. // Texture
  50. this._effect._bindTexture("textureSampler", this._texture);
  51. // Parameters
  52. if (this.onApply) {
  53. this.onApply(this._effect);
  54. }
  55. return this._effect;
  56. };
  57. BABYLON.PostProcess.prototype.dispose = function () {
  58. if (this._onDispose) {
  59. this._onDispose();
  60. }
  61. if (this._texture) {
  62. this._engine._releaseTexture(this._texture);
  63. this._texture = null;
  64. }
  65. var index = this._camera.postProcesses.indexOf(this);
  66. this._camera.postProcesses.splice(index, 1);
  67. if (index == 0 && this._camera.postProcesses.length > 0) {
  68. this._camera.postProcesses[0].width = -1; // invalidate frameBuffer to hint the postprocess to create a depth buffer
  69. }
  70. };
  71. })();