babylon.postProcess.ts 5.1 KB

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