babylon.postProcess.ts 5.4 KB

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