babylon.postProcess.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.PostProcess = function (name, fragmentUrl, parameters, samplers, ratio, camera, samplingMode, engine, reusable) {
  5. this.name = name;
  6. if (camera != null) {
  7. console.warn("Adding a camera in a post process is deprecated");
  8. this._camera = camera;
  9. this._scene = camera.getScene();
  10. camera.attachPostProcess(this);
  11. this._engine = this._scene.getEngine();
  12. }
  13. else {
  14. this._engine = engine;
  15. }
  16. this._renderRatio = ratio;
  17. this.width = -1;
  18. this.height = -1;
  19. this.renderTargetSamplingMode = samplingMode ? samplingMode : BABYLON.Texture.NEAREST_SAMPLINGMODE;
  20. this._reusable = reusable || false;
  21. this._textures = new BABYLON.Tools.SmartArray(2);
  22. this._currentRenderTextureInd = 0;
  23. samplers = samplers || [];
  24. samplers.push("textureSampler");
  25. this._effect = this._engine.createEffect({ vertex: "postprocess", fragment: fragmentUrl },
  26. ["position"],
  27. parameters || [],
  28. samplers, "");
  29. };
  30. // Methods
  31. BABYLON.PostProcess.prototype.onApply = null;
  32. BABYLON.PostProcess.prototype._onDispose = null;
  33. BABYLON.PostProcess.prototype.onSizeChanged = null;
  34. BABYLON.PostProcess.prototype.onActivate = null;
  35. BABYLON.PostProcess.prototype.activate = function (camera) {
  36. camera = camera || this._camera;
  37. var scene = camera.getScene();
  38. var desiredWidth = this._engine._renderingCanvas.width * this._renderRatio;
  39. var desiredHeight = this._engine._renderingCanvas.height * this._renderRatio;
  40. if (this.width !== desiredWidth || this.height !== desiredHeight) {
  41. if (this._textures.length > 0) {
  42. for (var i = 0; i < this._textures.length; i++) {
  43. this._engine._releaseTexture(this._textures.data[i]);
  44. }
  45. this._textures.reset();
  46. }
  47. this.width = desiredWidth;
  48. this.height = desiredHeight;
  49. this._textures.push(this._engine.createRenderTargetTexture({ width: this.width, height: this.height }, { generateMipMaps: false, generateDepthBuffer: camera._postProcesses.indexOf(this) === camera._postProcessesTakenIndices[0], samplingMode: this.renderTargetSamplingMode }));
  50. if (this._reusable) {
  51. this._textures.push(this._engine.createRenderTargetTexture({ width: this.width, height: this.height }, { generateMipMaps: false, generateDepthBuffer: camera._postProcesses.indexOf(this) === camera._postProcessesTakenIndices[0], samplingMode: this.renderTargetSamplingMode }));
  52. }
  53. if (this.onSizeChanged) {
  54. this.onSizeChanged();
  55. }
  56. }
  57. this._engine.bindFramebuffer(this._textures.data[this._currentRenderTextureInd]);
  58. if (this.onActivate) {
  59. this.onActivate(camera);
  60. }
  61. // Clear
  62. this._engine.clear(scene.clearColor, scene.autoClear || scene.forceWireframe, true);
  63. if (this._reusable) {
  64. this._currentRenderTextureInd = (this._currentRenderTextureInd + 1) % 2;
  65. }
  66. };
  67. BABYLON.PostProcess.prototype.apply = function () {
  68. // Check
  69. if (!this._effect.isReady())
  70. return null;
  71. // States
  72. this._engine.enableEffect(this._effect);
  73. this._engine.setState(false);
  74. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  75. this._engine.setDepthBuffer(false);
  76. this._engine.setDepthWrite(false);
  77. // Texture
  78. this._effect._bindTexture("textureSampler", this._textures.data[this._currentRenderTextureInd]);
  79. // Parameters
  80. if (this.onApply) {
  81. this.onApply(this._effect);
  82. }
  83. return this._effect;
  84. };
  85. BABYLON.PostProcess.prototype.dispose = function (camera) {
  86. camera = camera || this._camera;
  87. if (this._onDispose) {
  88. this._onDispose();
  89. }
  90. if (this._textures.length > 0) {
  91. for (var i = 0; i < this._textures.length; i++) {
  92. this._engine._releaseTexture(this._textures[i]);
  93. }
  94. this._textures.reset();
  95. }
  96. camera.detachPostProcess(this);
  97. var index = camera._postProcesses.indexOf(this);
  98. if (index === camera._postProcessesTakenIndices[0] && camera._postProcessesTakenIndices.length > 0) {
  99. this._camera._postProcesses[camera._postProcessesTakenIndices[0]].width = -1; // invalidate frameBuffer to hint the postprocess to create a depth buffer
  100. }
  101. };
  102. })();