babylon.postProcess.js 5.2 KB

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