babylon.postProcess.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /*
  13. Enable Pixel Perfect mode where texture is not scaled to be power of 2.
  14. Can only be used on a single postprocess or on the last one of a chain.
  15. */
  16. public enablePixelPerfectMode = false;
  17. private _camera: Camera;
  18. private _scene: Scene;
  19. private _engine: Engine;
  20. private _renderRatio: number|any;
  21. private _reusable = false;
  22. private _textureType: number;
  23. public _textures = new SmartArray<WebGLTexture>(2);
  24. public _currentRenderTextureInd = 0;
  25. private _effect: Effect;
  26. private _samplers: string[];
  27. private _fragmentUrl: string;
  28. private _parameters: string[];
  29. private _scaleRatio = new Vector2(1, 1);
  30. 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) {
  31. if (camera != null) {
  32. this._camera = camera;
  33. this._scene = camera.getScene();
  34. camera.attachPostProcess(this);
  35. this._engine = this._scene.getEngine();
  36. }
  37. else {
  38. this._engine = engine;
  39. }
  40. this._renderRatio = ratio;
  41. this.renderTargetSamplingMode = samplingMode ? samplingMode : Texture.NEAREST_SAMPLINGMODE;
  42. this._reusable = reusable || false;
  43. this._textureType = textureType;
  44. this._samplers = samplers || [];
  45. this._samplers.push("textureSampler");
  46. this._fragmentUrl = fragmentUrl;
  47. this._parameters = parameters || [];
  48. this._parameters.push("scale");
  49. this.updateEffect(defines);
  50. }
  51. public updateEffect(defines?: string) {
  52. this._effect = this._engine.createEffect({ vertex: "postprocess", fragment: this._fragmentUrl },
  53. ["position"],
  54. this._parameters,
  55. this._samplers, defines !== undefined ? defines : "");
  56. }
  57. public isReusable(): boolean {
  58. return this._reusable;
  59. }
  60. public activate(camera: Camera, sourceTexture?: WebGLTexture): void {
  61. camera = camera || this._camera;
  62. var scene = camera.getScene();
  63. var maxSize = camera.getEngine().getCaps().maxTextureSize;
  64. var requiredWidth = ((sourceTexture ? sourceTexture._width : this._engine.getRenderingCanvas().width) * this._renderRatio) | 0;
  65. var requiredHeight = ((sourceTexture ? sourceTexture._height : this._engine.getRenderingCanvas().height) * this._renderRatio) | 0;
  66. var desiredWidth = this._renderRatio.width || requiredWidth;
  67. var desiredHeight = this._renderRatio.height || requiredHeight;
  68. if (this.renderTargetSamplingMode !== Texture.NEAREST_SAMPLINGMODE) {
  69. if (!this._renderRatio.width) {
  70. desiredWidth = Tools.GetExponentOfTwo(desiredWidth, maxSize);
  71. }
  72. if (!this._renderRatio.height) {
  73. desiredHeight = Tools.GetExponentOfTwo(desiredHeight, maxSize);
  74. }
  75. }
  76. if (this.width !== desiredWidth || this.height !== desiredHeight) {
  77. if (this._textures.length > 0) {
  78. for (var i = 0; i < this._textures.length; i++) {
  79. this._engine._releaseTexture(this._textures.data[i]);
  80. }
  81. this._textures.reset();
  82. }
  83. this.width = desiredWidth;
  84. this.height = desiredHeight;
  85. 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 }));
  86. if (this._reusable) {
  87. 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 }));
  88. }
  89. if (this.onSizeChanged) {
  90. this.onSizeChanged();
  91. }
  92. }
  93. if (this.enablePixelPerfectMode) {
  94. this._scaleRatio.copyFromFloats(requiredWidth / desiredWidth, requiredHeight / desiredHeight);
  95. this._engine.bindFramebuffer(this._textures.data[this._currentRenderTextureInd], 0, requiredWidth, requiredHeight);
  96. }
  97. else {
  98. this._scaleRatio.copyFromFloats(1, 1);
  99. this._engine.bindFramebuffer(this._textures.data[this._currentRenderTextureInd]);
  100. }
  101. if (this.onActivate) {
  102. this.onActivate(camera);
  103. }
  104. // Clear
  105. if (this.clearColor) {
  106. this._engine.clear(this.clearColor, true, true);
  107. } else {
  108. this._engine.clear(scene.clearColor, scene.autoClear || scene.forceWireframe, true);
  109. }
  110. if (this._reusable) {
  111. this._currentRenderTextureInd = (this._currentRenderTextureInd + 1) % 2;
  112. }
  113. }
  114. public get isSupported(): boolean {
  115. return this._effect.isSupported;
  116. }
  117. public apply(): Effect {
  118. // Check
  119. if (!this._effect.isReady())
  120. return null;
  121. // States
  122. this._engine.enableEffect(this._effect);
  123. this._engine.setState(false);
  124. this._engine.setAlphaMode(Engine.ALPHA_DISABLE);
  125. this._engine.setDepthBuffer(false);
  126. this._engine.setDepthWrite(false);
  127. // Texture
  128. this._effect._bindTexture("textureSampler", this._textures.data[this._currentRenderTextureInd]);
  129. // Parameters
  130. this._effect.setVector2("scale", this._scaleRatio);
  131. if (this.onApply) {
  132. this.onApply(this._effect);
  133. }
  134. return this._effect;
  135. }
  136. public dispose(camera?: Camera): void {
  137. camera = camera || this._camera;
  138. if (this._textures.length > 0) {
  139. for (var i = 0; i < this._textures.length; i++) {
  140. this._engine._releaseTexture(this._textures.data[i]);
  141. }
  142. this._textures.reset();
  143. }
  144. if (!camera) {
  145. return;
  146. }
  147. camera.detachPostProcess(this);
  148. var index = camera._postProcesses.indexOf(this);
  149. if (index === camera._postProcessesTakenIndices[0] && camera._postProcessesTakenIndices.length > 0) {
  150. this._camera._postProcesses[camera._postProcessesTakenIndices[0]].width = -1; // invalidate frameBuffer to hint the postprocess to create a depth buffer
  151. }
  152. }
  153. }
  154. }