babylon.postProcess.ts 5.7 KB

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