babylon.postProcess.ts 6.0 KB

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