babylon.postProcess.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. this._samplers = samplers || [];
  27. this._samplers.push("textureSampler");
  28. this._fragmentUrl = fragmentUrl;
  29. this._parameters = parameters || [];
  30. this.updateEffect(defines);
  31. }
  32. PostProcess.prototype.updateEffect = function (defines) {
  33. this._effect = this._engine.createEffect({ vertex: "postprocess", fragment: this._fragmentUrl }, ["position"], this._parameters, this._samplers, defines !== undefined ? defines : "");
  34. };
  35. PostProcess.prototype.isReusable = function () {
  36. return this._reusable;
  37. };
  38. PostProcess.prototype.activate = function (camera, sourceTexture) {
  39. camera = camera || this._camera;
  40. var scene = camera.getScene();
  41. var maxSize = camera.getEngine().getCaps().maxTextureSize;
  42. var desiredWidth = ((sourceTexture ? sourceTexture._width : this._engine.getRenderingCanvas().width) * this._renderRatio) | 0;
  43. var desiredHeight = ((sourceTexture ? sourceTexture._height : this._engine.getRenderingCanvas().height) * this._renderRatio) | 0;
  44. desiredWidth = this._renderRatio.width || BABYLON.Tools.GetExponentOfTwo(desiredWidth, maxSize);
  45. desiredHeight = this._renderRatio.height || BABYLON.Tools.GetExponentOfTwo(desiredHeight, maxSize);
  46. if (this.width !== desiredWidth || this.height !== desiredHeight) {
  47. if (this._textures.length > 0) {
  48. for (var i = 0; i < this._textures.length; i++) {
  49. this._engine._releaseTexture(this._textures.data[i]);
  50. }
  51. this._textures.reset();
  52. }
  53. this.width = desiredWidth;
  54. this.height = desiredHeight;
  55. 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 }));
  56. if (this._reusable) {
  57. 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 }));
  58. }
  59. if (this.onSizeChanged) {
  60. this.onSizeChanged();
  61. }
  62. }
  63. this._engine.bindFramebuffer(this._textures.data[this._currentRenderTextureInd]);
  64. if (this.onActivate) {
  65. this.onActivate(camera);
  66. }
  67. // Clear
  68. if (this.clearColor) {
  69. this._engine.clear(this.clearColor, true, true);
  70. }
  71. else {
  72. this._engine.clear(scene.clearColor, scene.autoClear || scene.forceWireframe, true);
  73. }
  74. if (this._reusable) {
  75. this._currentRenderTextureInd = (this._currentRenderTextureInd + 1) % 2;
  76. }
  77. };
  78. Object.defineProperty(PostProcess.prototype, "isSupported", {
  79. get: function () {
  80. return this._effect.isSupported;
  81. },
  82. enumerable: true,
  83. configurable: true
  84. });
  85. PostProcess.prototype.apply = function () {
  86. // Check
  87. if (!this._effect.isReady())
  88. return null;
  89. // States
  90. this._engine.enableEffect(this._effect);
  91. this._engine.setState(false);
  92. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  93. this._engine.setDepthBuffer(false);
  94. this._engine.setDepthWrite(false);
  95. // Texture
  96. this._effect._bindTexture("textureSampler", this._textures.data[this._currentRenderTextureInd]);
  97. // Parameters
  98. if (this.onApply) {
  99. this.onApply(this._effect);
  100. }
  101. return this._effect;
  102. };
  103. PostProcess.prototype.dispose = function (camera) {
  104. camera = camera || this._camera;
  105. if (this._textures.length > 0) {
  106. for (var i = 0; i < this._textures.length; i++) {
  107. this._engine._releaseTexture(this._textures.data[i]);
  108. }
  109. this._textures.reset();
  110. }
  111. if (!camera) {
  112. return;
  113. }
  114. camera.detachPostProcess(this);
  115. var index = camera._postProcesses.indexOf(this);
  116. if (index === camera._postProcessesTakenIndices[0] && camera._postProcessesTakenIndices.length > 0) {
  117. this._camera._postProcesses[camera._postProcessesTakenIndices[0]].width = -1; // invalidate frameBuffer to hint the postprocess to create a depth buffer
  118. }
  119. };
  120. return PostProcess;
  121. })();
  122. BABYLON.PostProcess = PostProcess;
  123. })(BABYLON || (BABYLON = {}));