babylon.postProcess.js 5.3 KB

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