babylon.postProcess.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var PostProcess = (function () {
  4. //ANY
  5. function PostProcess(name, fragmentUrl, parameters, samplers, ratio, camera, samplingMode, engine, reusable) {
  6. this.name = name;
  7. this.onApply = null;
  8. this.onSizeChanged = null;
  9. this.onActivate = null;
  10. this.width = -1;
  11. this.height = -1;
  12. this._onDispose = null;
  13. this._reusable = false;
  14. this._textures = new BABYLON.SmartArray(2);
  15. this._currentRenderTextureInd = 0;
  16. if (camera != null) {
  17. this._camera = camera;
  18. this._scene = camera.getScene();
  19. camera.attachPostProcess(this);
  20. this._engine = this._scene.getEngine();
  21. } else {
  22. this._engine = engine;
  23. }
  24. this._renderRatio = ratio;
  25. this.renderTargetSamplingMode = samplingMode ? samplingMode : 1; //ANY BABYLON.Texture.NEAREST_SAMPLINGMODE;
  26. this._reusable = reusable || false;
  27. samplers = samplers || [];
  28. samplers.push("textureSampler");
  29. this._effect = this._engine.createEffect({ vertex: "postprocess", fragment: fragmentUrl }, ["position"], parameters || [], samplers, "");
  30. }
  31. // Methods
  32. PostProcess.prototype.activate = function (camera) {
  33. camera = camera || this._camera;
  34. var scene = camera.getScene();
  35. var desiredWidth = this._engine._renderingCanvas.width * this._renderRatio;
  36. var desiredHeight = this._engine._renderingCanvas.height * this._renderRatio;
  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(0); //ANY 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._onDispose) {
  85. this._onDispose();
  86. }
  87. if (this._textures.length > 0) {
  88. for (var i = 0; i < this._textures.length; i++) {
  89. this._engine._releaseTexture(this._textures.data[i]);
  90. }
  91. this._textures.reset();
  92. }
  93. camera.detachPostProcess(this);
  94. var index = camera._postProcesses.indexOf(this);
  95. if (index === camera._postProcessesTakenIndices[0] && camera._postProcessesTakenIndices.length > 0) {
  96. this._camera._postProcesses[camera._postProcessesTakenIndices[0]].width = -1; // invalidate frameBuffer to hint the postprocess to create a depth buffer
  97. }
  98. };
  99. return PostProcess;
  100. })();
  101. BABYLON.PostProcess = PostProcess;
  102. })(BABYLON || (BABYLON = {}));
  103. //# sourceMappingURL=babylon.postProcess.js.map