babylon.postProcess.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. module BABYLON {
  2. export class PostProcess {
  3. public onApply = null;
  4. public onSizeChanged = null;
  5. public onActivate = null;
  6. public width = -1;
  7. public height = -1;
  8. public renderTargetSamplingMode: number;
  9. private _onDispose = null;
  10. private _camera: Camera;
  11. private _scene; //ANY
  12. private _engine; //ANY
  13. private _renderRatio: number;
  14. private _reusable = false;
  15. private _textures = new BABYLON.SmartArray(2);
  16. private _currentRenderTextureInd = 0;
  17. private _effect; //ANY
  18. //ANY
  19. constructor(public name: string, fragmentUrl: string, parameters: string[], samplers: string[], ratio: number, camera: Camera, samplingMode: number, engine, reusable?: boolean) {
  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 : 1; //ANY BABYLON.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, "");
  38. }
  39. // Methods
  40. public activate(camera: Camera): void {
  41. camera = camera || this._camera;
  42. var scene = camera.getScene();
  43. var desiredWidth = this._engine._renderingCanvas.width * this._renderRatio;
  44. var desiredHeight = this._engine._renderingCanvas.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(): void {
  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(0); //ANY 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._onDispose) {
  93. this._onDispose();
  94. }
  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. }