babylon.postProcess.js 5.3 KB

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