babylon.postProcess.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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(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): void {
  41. camera = camera || this._camera;
  42. var scene = camera.getScene();
  43. var desiredWidth = this._engine.getRenderingCanvas().width * this._renderRatio;
  44. var desiredHeight = this._engine.getRenderingCanvas().height * this._renderRatio;
  45. if (this.width !== desiredWidth || this.height !== desiredHeight) {
  46. if (this._textures.length > 0) {
  47. for (var i = 0; i < this._textures.length; i++) {
  48. this._engine._releaseTexture(this._textures.data[i]);
  49. }
  50. this._textures.reset();
  51. }
  52. this.width = desiredWidth;
  53. this.height = desiredHeight;
  54. 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 }));
  55. if (this._reusable) {
  56. 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 }));
  57. }
  58. if (this.onSizeChanged) {
  59. this.onSizeChanged();
  60. }
  61. }
  62. this._engine.bindFramebuffer(this._textures.data[this._currentRenderTextureInd]);
  63. if (this.onActivate) {
  64. this.onActivate(camera);
  65. }
  66. // Clear
  67. this._engine.clear(scene.clearColor, scene.autoClear || scene.forceWireframe, true);
  68. if (this._reusable) {
  69. this._currentRenderTextureInd = (this._currentRenderTextureInd + 1) % 2;
  70. }
  71. }
  72. public apply(): Effect {
  73. // Check
  74. if (!this._effect.isReady())
  75. return null;
  76. // States
  77. this._engine.enableEffect(this._effect);
  78. this._engine.setState(false);
  79. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  80. this._engine.setDepthBuffer(false);
  81. this._engine.setDepthWrite(false);
  82. // Texture
  83. this._effect._bindTexture("textureSampler", this._textures.data[this._currentRenderTextureInd]);
  84. // Parameters
  85. if (this.onApply) {
  86. this.onApply(this._effect);
  87. }
  88. return this._effect;
  89. }
  90. public dispose(camera: Camera): void {
  91. camera = camera || this._camera;
  92. if (this._textures.length > 0) {
  93. for (var i = 0; i < this._textures.length; i++) {
  94. this._engine._releaseTexture(this._textures.data[i]);
  95. }
  96. this._textures.reset();
  97. }
  98. camera.detachPostProcess(this);
  99. var index = camera._postProcesses.indexOf(this);
  100. if (index === camera._postProcessesTakenIndices[0] && camera._postProcessesTakenIndices.length > 0) {
  101. this._camera._postProcesses[camera._postProcessesTakenIndices[0]].width = -1; // invalidate frameBuffer to hint the postprocess to create a depth buffer
  102. }
  103. }
  104. }
  105. }