babylon.postProcess.js 4.7 KB

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