babylon.postProcess.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. 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<WebGLTexture>(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, defines?: string) {
  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, defines !== undefined ? defines : "");
  37. }
  38. public isReusable(): boolean {
  39. return this._reusable;
  40. }
  41. public activate(camera: Camera, sourceTexture?: WebGLTexture): void {
  42. camera = camera || this._camera;
  43. var scene = camera.getScene();
  44. var maxSize = camera.getEngine().getCaps().maxTextureSize;
  45. var desiredWidth = ((sourceTexture ? sourceTexture._width : this._engine.getRenderingCanvas().width) * this._renderRatio) | 0;
  46. var desiredHeight = ((sourceTexture ? sourceTexture._height : this._engine.getRenderingCanvas().height) * this._renderRatio) | 0;
  47. desiredWidth = Tools.GetExponantOfTwo(desiredWidth, maxSize);
  48. desiredHeight = Tools.GetExponantOfTwo(desiredHeight, maxSize);
  49. if (this.width !== desiredWidth || this.height !== desiredHeight) {
  50. if (this._textures.length > 0) {
  51. for (var i = 0; i < this._textures.length; i++) {
  52. this._engine._releaseTexture(this._textures.data[i]);
  53. }
  54. this._textures.reset();
  55. }
  56. this.width = desiredWidth;
  57. this.height = desiredHeight;
  58. 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 }));
  59. if (this._reusable) {
  60. 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 }));
  61. }
  62. if (this.onSizeChanged) {
  63. this.onSizeChanged();
  64. }
  65. }
  66. this._engine.bindFramebuffer(this._textures.data[this._currentRenderTextureInd]);
  67. if (this.onActivate) {
  68. this.onActivate(camera);
  69. }
  70. // Clear
  71. this._engine.clear(scene.clearColor, scene.autoClear || scene.forceWireframe, true);
  72. if (this._reusable) {
  73. this._currentRenderTextureInd = (this._currentRenderTextureInd + 1) % 2;
  74. }
  75. }
  76. public apply(): Effect {
  77. // Check
  78. if (!this._effect.isReady())
  79. return null;
  80. // States
  81. this._engine.enableEffect(this._effect);
  82. this._engine.setState(false);
  83. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  84. this._engine.setDepthBuffer(false);
  85. this._engine.setDepthWrite(false);
  86. // Texture
  87. this._effect._bindTexture("textureSampler", this._textures.data[this._currentRenderTextureInd]);
  88. // Parameters
  89. if (this.onApply) {
  90. this.onApply(this._effect);
  91. }
  92. return this._effect;
  93. }
  94. public dispose(camera: Camera): void {
  95. camera = camera || this._camera;
  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. }