babylon.postProcess.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. module BABYLON {
  2. export type PostProcessOptions = { width: number, height: number };
  3. export class PostProcess
  4. {
  5. public width = -1;
  6. public height = -1;
  7. public renderTargetSamplingMode: number;
  8. public clearColor: Color4;
  9. /*
  10. Enable Pixel Perfect mode where texture is not scaled to be power of 2.
  11. Can only be used on a single postprocess or on the last one of a chain.
  12. */
  13. public enablePixelPerfectMode = false;
  14. public samples = 1;
  15. private _camera: Camera;
  16. private _scene: Scene;
  17. private _engine: Engine;
  18. private _options: number | PostProcessOptions;
  19. private _reusable = false;
  20. private _textureType: number;
  21. public _textures = new SmartArray<WebGLTexture>(2);
  22. public _currentRenderTextureInd = 0;
  23. private _effect: Effect;
  24. private _samplers: string[];
  25. private _fragmentUrl: string;
  26. private _parameters: string[];
  27. private _scaleRatio = new Vector2(1, 1);
  28. // Events
  29. /**
  30. * An event triggered when the postprocess is activated.
  31. * @type {BABYLON.Observable}
  32. */
  33. public onActivateObservable = new Observable<Camera>();
  34. private _onActivateObserver: Observer<Camera>;
  35. public set onActivate(callback: (camera: Camera) => void) {
  36. if (this._onActivateObserver) {
  37. this.onActivateObservable.remove(this._onActivateObserver);
  38. }
  39. this._onActivateObserver = this.onActivateObservable.add(callback);
  40. }
  41. /**
  42. * An event triggered when the postprocess changes its size.
  43. * @type {BABYLON.Observable}
  44. */
  45. public onSizeChangedObservable = new Observable<PostProcess>();
  46. private _onSizeChangedObserver: Observer<PostProcess>;
  47. public set onSizeChanged(callback: (postProcess: PostProcess) => void) {
  48. if (this._onSizeChangedObserver) {
  49. this.onSizeChangedObservable.remove(this._onSizeChangedObserver);
  50. }
  51. this._onSizeChangedObserver = this.onSizeChangedObservable.add(callback);
  52. }
  53. /**
  54. * An event triggered when the postprocess applies its effect.
  55. * @type {BABYLON.Observable}
  56. */
  57. public onApplyObservable = new Observable<Effect>();
  58. private _onApplyObserver: Observer<Effect>;
  59. public set onApply(callback: (effect: Effect) => void) {
  60. if (this._onApplyObserver) {
  61. this.onApplyObservable.remove(this._onApplyObserver);
  62. }
  63. this._onApplyObserver = this.onApplyObservable.add(callback);
  64. }
  65. /**
  66. * An event triggered before rendering the postprocess
  67. * @type {BABYLON.Observable}
  68. */
  69. public onBeforeRenderObservable = new Observable<Effect>();
  70. private _onBeforeRenderObserver: Observer<Effect>;
  71. public set onBeforeRender(callback: (effect: Effect) => void) {
  72. if (this._onBeforeRenderObserver) {
  73. this.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
  74. }
  75. this._onBeforeRenderObserver = this.onBeforeRenderObservable.add(callback);
  76. }
  77. /**
  78. * An event triggered after rendering the postprocess
  79. * @type {BABYLON.Observable}
  80. */
  81. public onAfterRenderObservable = new Observable<Effect>();
  82. private _onAfterRenderObserver: Observer<Effect>;
  83. public set onAfterRender(callback: (efect: Effect) => void) {
  84. if (this._onAfterRenderObserver) {
  85. this.onAfterRenderObservable.remove(this._onAfterRenderObserver);
  86. }
  87. this._onAfterRenderObserver = this.onAfterRenderObservable.add(callback);
  88. }
  89. constructor(public name: string, fragmentUrl: string, parameters: string[], samplers: string[], options: number | PostProcessOptions, camera: Camera, samplingMode: number = Texture.NEAREST_SAMPLINGMODE, engine?: Engine, reusable?: boolean, defines?: string, textureType: number = Engine.TEXTURETYPE_UNSIGNED_INT) {
  90. if (camera != null) {
  91. this._camera = camera;
  92. this._scene = camera.getScene();
  93. camera.attachPostProcess(this);
  94. this._engine = this._scene.getEngine();
  95. }
  96. else {
  97. this._engine = engine;
  98. }
  99. this._options = options;
  100. this.renderTargetSamplingMode = samplingMode ? samplingMode : Texture.NEAREST_SAMPLINGMODE;
  101. this._reusable = reusable || false;
  102. this._textureType = textureType;
  103. this._samplers = samplers || [];
  104. this._samplers.push("textureSampler");
  105. this._fragmentUrl = fragmentUrl;
  106. this._parameters = parameters || [];
  107. this._parameters.push("scale");
  108. this.updateEffect(defines);
  109. }
  110. public updateEffect(defines?: string) {
  111. this._effect = this._engine.createEffect({ vertex: "postprocess", fragment: this._fragmentUrl },
  112. ["position"],
  113. this._parameters,
  114. this._samplers, defines !== undefined ? defines : "");
  115. }
  116. public isReusable(): boolean {
  117. return this._reusable;
  118. }
  119. /** invalidate frameBuffer to hint the postprocess to create a depth buffer */
  120. public markTextureDirty() : void{
  121. this.width = -1;
  122. }
  123. public activate(camera: Camera, sourceTexture?: WebGLTexture): void {
  124. camera = camera || this._camera;
  125. var scene = camera.getScene();
  126. var maxSize = camera.getEngine().getCaps().maxTextureSize;
  127. var requiredWidth = ((sourceTexture ? sourceTexture._width : this._engine.getRenderingCanvas().width) * <number>this._options) | 0;
  128. var requiredHeight = ((sourceTexture ? sourceTexture._height : this._engine.getRenderingCanvas().height) * <number>this._options) | 0;
  129. var desiredWidth = (<PostProcessOptions>this._options).width || requiredWidth;
  130. var desiredHeight = (<PostProcessOptions>this._options).height || requiredHeight;
  131. if (this.renderTargetSamplingMode !== Texture.NEAREST_SAMPLINGMODE) {
  132. if (!(<PostProcessOptions>this._options).width) {
  133. desiredWidth = Tools.GetExponentOfTwo(desiredWidth, maxSize);
  134. }
  135. if (!(<PostProcessOptions>this._options).height) {
  136. desiredHeight = Tools.GetExponentOfTwo(desiredHeight, maxSize);
  137. }
  138. }
  139. if (this.width !== desiredWidth || this.height !== desiredHeight) {
  140. if (this._textures.length > 0) {
  141. for (var i = 0; i < this._textures.length; i++) {
  142. this._engine._releaseTexture(this._textures.data[i]);
  143. }
  144. this._textures.reset();
  145. }
  146. this.width = desiredWidth;
  147. this.height = desiredHeight;
  148. let textureSize = { width: this.width, height: this.height };
  149. let textureOptions = {
  150. generateMipMaps: false,
  151. generateDepthBuffer: camera._postProcesses.indexOf(this) === 0,
  152. generateStencilBuffer: camera._postProcesses.indexOf(this) === 0 && this._engine.isStencilEnable,
  153. samplingMode: this.renderTargetSamplingMode,
  154. type: this._textureType
  155. };
  156. this._textures.push(this._engine.createRenderTargetTexture(textureSize, textureOptions));
  157. if (this._reusable) {
  158. this._textures.push(this._engine.createRenderTargetTexture(textureSize, textureOptions));
  159. }
  160. this.onSizeChangedObservable.notifyObservers(this);
  161. }
  162. this._textures.forEach(texture => {
  163. if (texture.samples !== this.samples) {
  164. this._engine.updateRenderTargetTextureSampleCount(texture, this.samples);
  165. }
  166. });
  167. if (this.enablePixelPerfectMode) {
  168. this._scaleRatio.copyFromFloats(requiredWidth / desiredWidth, requiredHeight / desiredHeight);
  169. this._engine.bindFramebuffer(this._textures.data[this._currentRenderTextureInd], 0, requiredWidth, requiredHeight);
  170. }
  171. else {
  172. this._scaleRatio.copyFromFloats(1, 1);
  173. this._engine.bindFramebuffer(this._textures.data[this._currentRenderTextureInd]);
  174. }
  175. this.onActivateObservable.notifyObservers(camera);
  176. // Clear
  177. if (this.clearColor) {
  178. this._engine.clear(this.clearColor, true, true, true);
  179. } else {
  180. this._engine.clear(scene.clearColor, scene.autoClear || scene.forceWireframe, true, true);
  181. }
  182. if (this._reusable) {
  183. this._currentRenderTextureInd = (this._currentRenderTextureInd + 1) % 2;
  184. }
  185. }
  186. public get isSupported(): boolean {
  187. return this._effect.isSupported;
  188. }
  189. public apply(): Effect {
  190. // Check
  191. if (!this._effect.isReady())
  192. return null;
  193. // States
  194. this._engine.enableEffect(this._effect);
  195. this._engine.setState(false);
  196. this._engine.setAlphaMode(Engine.ALPHA_DISABLE);
  197. this._engine.setDepthBuffer(false);
  198. this._engine.setDepthWrite(false);
  199. // Texture
  200. this._effect._bindTexture("textureSampler", this._textures.data[this._currentRenderTextureInd]);
  201. // Parameters
  202. this._effect.setVector2("scale", this._scaleRatio);
  203. this.onApplyObservable.notifyObservers(this._effect);
  204. return this._effect;
  205. }
  206. public dispose(camera?: Camera): void {
  207. camera = camera || this._camera;
  208. if (this._textures.length > 0) {
  209. for (var i = 0; i < this._textures.length; i++) {
  210. this._engine._releaseTexture(this._textures.data[i]);
  211. }
  212. this._textures.reset();
  213. }
  214. if (!camera) {
  215. return;
  216. }
  217. camera.detachPostProcess(this);
  218. var index = camera._postProcesses.indexOf(this);
  219. if (index === 0 && camera._postProcesses.length > 0) {
  220. this._camera._postProcesses[0].markTextureDirty();
  221. }
  222. this.onActivateObservable.clear();
  223. this.onAfterRenderObservable.clear();
  224. this.onApplyObservable.clear();
  225. this.onBeforeRenderObservable.clear();
  226. this.onSizeChangedObservable.clear();
  227. }
  228. }
  229. }