babylon.postProcess.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /*
  11. Enable Pixel Perfect mode where texture is not scaled to be power of 2.
  12. Can only be used on a single postprocess or on the last one of a chain.
  13. */
  14. this.enablePixelPerfectMode = false;
  15. this._reusable = false;
  16. this._textures = new BABYLON.SmartArray(2);
  17. this._currentRenderTextureInd = 0;
  18. this._scaleRatio = new BABYLON.Vector2(1, 1);
  19. if (camera != null) {
  20. this._camera = camera;
  21. this._scene = camera.getScene();
  22. camera.attachPostProcess(this);
  23. this._engine = this._scene.getEngine();
  24. }
  25. else {
  26. this._engine = engine;
  27. }
  28. this._renderRatio = ratio;
  29. this.renderTargetSamplingMode = samplingMode ? samplingMode : BABYLON.Texture.NEAREST_SAMPLINGMODE;
  30. this._reusable = reusable || false;
  31. this._textureType = textureType;
  32. this._samplers = samplers || [];
  33. this._samplers.push("textureSampler");
  34. this._fragmentUrl = fragmentUrl;
  35. this._parameters = parameters || [];
  36. this._parameters.push("scale");
  37. this.updateEffect(defines);
  38. }
  39. PostProcess.prototype.updateEffect = function (defines) {
  40. this._effect = this._engine.createEffect({ vertex: "postprocess", fragment: this._fragmentUrl }, ["position"], this._parameters, this._samplers, defines !== undefined ? defines : "");
  41. };
  42. PostProcess.prototype.isReusable = function () {
  43. return this._reusable;
  44. };
  45. PostProcess.prototype.activate = function (camera, sourceTexture) {
  46. camera = camera || this._camera;
  47. var scene = camera.getScene();
  48. var maxSize = camera.getEngine().getCaps().maxTextureSize;
  49. var requiredWidth = ((sourceTexture ? sourceTexture._width : this._engine.getRenderingCanvas().width) * this._renderRatio) | 0;
  50. var requiredHeight = ((sourceTexture ? sourceTexture._height : this._engine.getRenderingCanvas().height) * this._renderRatio) | 0;
  51. var desiredWidth = this._renderRatio.width || requiredWidth;
  52. var desiredHeight = this._renderRatio.height || requiredHeight;
  53. if (this.renderTargetSamplingMode !== BABYLON.Texture.NEAREST_SAMPLINGMODE) {
  54. if (!this._renderRatio.width) {
  55. desiredWidth = BABYLON.Tools.GetExponentOfTwo(desiredWidth, maxSize);
  56. }
  57. if (!this._renderRatio.height) {
  58. desiredHeight = BABYLON.Tools.GetExponentOfTwo(desiredHeight, maxSize);
  59. }
  60. }
  61. if (this.width !== desiredWidth || this.height !== desiredHeight) {
  62. if (this._textures.length > 0) {
  63. for (var i = 0; i < this._textures.length; i++) {
  64. this._engine._releaseTexture(this._textures.data[i]);
  65. }
  66. this._textures.reset();
  67. }
  68. this.width = desiredWidth;
  69. this.height = desiredHeight;
  70. 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 }));
  71. if (this._reusable) {
  72. 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 }));
  73. }
  74. if (this.onSizeChanged) {
  75. this.onSizeChanged();
  76. }
  77. }
  78. if (this.enablePixelPerfectMode) {
  79. this._scaleRatio.copyFromFloats(requiredWidth / desiredWidth, requiredHeight / desiredHeight);
  80. this._engine.bindFramebuffer(this._textures.data[this._currentRenderTextureInd], 0, requiredWidth, requiredHeight);
  81. }
  82. else {
  83. this._scaleRatio.copyFromFloats(1, 1);
  84. this._engine.bindFramebuffer(this._textures.data[this._currentRenderTextureInd]);
  85. }
  86. if (this.onActivate) {
  87. this.onActivate(camera);
  88. }
  89. // Clear
  90. if (this.clearColor) {
  91. this._engine.clear(this.clearColor, true, true);
  92. }
  93. else {
  94. this._engine.clear(scene.clearColor, scene.autoClear || scene.forceWireframe, true);
  95. }
  96. if (this._reusable) {
  97. this._currentRenderTextureInd = (this._currentRenderTextureInd + 1) % 2;
  98. }
  99. };
  100. Object.defineProperty(PostProcess.prototype, "isSupported", {
  101. get: function () {
  102. return this._effect.isSupported;
  103. },
  104. enumerable: true,
  105. configurable: true
  106. });
  107. PostProcess.prototype.apply = function () {
  108. // Check
  109. if (!this._effect.isReady())
  110. return null;
  111. // States
  112. this._engine.enableEffect(this._effect);
  113. this._engine.setState(false);
  114. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  115. this._engine.setDepthBuffer(false);
  116. this._engine.setDepthWrite(false);
  117. // Texture
  118. this._effect._bindTexture("textureSampler", this._textures.data[this._currentRenderTextureInd]);
  119. // Parameters
  120. this._effect.setVector2("scale", this._scaleRatio);
  121. if (this.onApply) {
  122. this.onApply(this._effect);
  123. }
  124. return this._effect;
  125. };
  126. PostProcess.prototype.dispose = function (camera) {
  127. camera = camera || this._camera;
  128. if (this._textures.length > 0) {
  129. for (var i = 0; i < this._textures.length; i++) {
  130. this._engine._releaseTexture(this._textures.data[i]);
  131. }
  132. this._textures.reset();
  133. }
  134. if (!camera) {
  135. return;
  136. }
  137. camera.detachPostProcess(this);
  138. var index = camera._postProcesses.indexOf(this);
  139. if (index === camera._postProcessesTakenIndices[0] && camera._postProcessesTakenIndices.length > 0) {
  140. this._camera._postProcesses[camera._postProcessesTakenIndices[0]].width = -1; // invalidate frameBuffer to hint the postprocess to create a depth buffer
  141. }
  142. };
  143. return PostProcess;
  144. })();
  145. BABYLON.PostProcess = PostProcess;
  146. })(BABYLON || (BABYLON = {}));