babylon.postProcess.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. if (this.clearColor) {
  62. this._engine.clear(this.clearColor, true, true);
  63. }
  64. else {
  65. this._engine.clear(scene.clearColor, scene.autoClear || scene.forceWireframe, true);
  66. }
  67. if (this._reusable) {
  68. this._currentRenderTextureInd = (this._currentRenderTextureInd + 1) % 2;
  69. }
  70. };
  71. PostProcess.prototype.apply = function () {
  72. // Check
  73. if (!this._effect.isReady())
  74. return null;
  75. // States
  76. this._engine.enableEffect(this._effect);
  77. this._engine.setState(false);
  78. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  79. this._engine.setDepthBuffer(false);
  80. this._engine.setDepthWrite(false);
  81. // Texture
  82. this._effect._bindTexture("textureSampler", this._textures.data[this._currentRenderTextureInd]);
  83. // Parameters
  84. if (this.onApply) {
  85. this.onApply(this._effect);
  86. }
  87. return this._effect;
  88. };
  89. PostProcess.prototype.dispose = function (camera) {
  90. camera = camera || this._camera;
  91. if (this._textures.length > 0) {
  92. for (var i = 0; i < this._textures.length; i++) {
  93. this._engine._releaseTexture(this._textures.data[i]);
  94. }
  95. this._textures.reset();
  96. }
  97. if (!camera) {
  98. return;
  99. }
  100. camera.detachPostProcess(this);
  101. var index = camera._postProcesses.indexOf(this);
  102. if (index === camera._postProcessesTakenIndices[0] && camera._postProcessesTakenIndices.length > 0) {
  103. this._camera._postProcesses[camera._postProcessesTakenIndices[0]].width = -1; // invalidate frameBuffer to hint the postprocess to create a depth buffer
  104. }
  105. };
  106. return PostProcess;
  107. })();
  108. BABYLON.PostProcess = PostProcess;
  109. })(BABYLON || (BABYLON = {}));
  110. //# sourceMappingURL=babylon.postProcess.js.map