babylon.postProcess.js 5.7 KB

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