babylon.postProcess.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. module BABYLON {
  2. export type PostProcessOptions = { width: number, height: number };
  3. /**
  4. * PostProcess can be used to apply a shader to a texture after it has been rendered
  5. * See https://doc.babylonjs.com/how_to/how_to_use_postprocesses
  6. */
  7. export class PostProcess {
  8. /**
  9. * Width of the texture to apply the post process on
  10. */
  11. public width = -1;
  12. /**
  13. * Height of the texture to apply the post process on
  14. */
  15. public height = -1;
  16. /**
  17. * Internal, reference to the location where this postprocess was output to. (Typically the texture on the next postprocess in the chain)
  18. */
  19. public _outputTexture: Nullable<InternalTexture> = null;
  20. /**
  21. * Sampling mode used by the shader
  22. * See https://doc.babylonjs.com/classes/3.1/texture
  23. */
  24. public renderTargetSamplingMode: number;
  25. /**
  26. * Clear color to use when screen clearing
  27. */
  28. public clearColor: Color4;
  29. /**
  30. * If the buffer needs to be cleared before applying the post process. (default: true)
  31. * Should be set to false if shader will overwrite all previous pixels.
  32. */
  33. public autoClear = true;
  34. /**
  35. * Type of alpha mode to use when performing the post process (default: Engine.ALPHA_DISABLE)
  36. */
  37. public alphaMode = Engine.ALPHA_DISABLE;
  38. /**
  39. * Sets the setAlphaBlendConstants of the babylon engine
  40. */
  41. public alphaConstants: Color4;
  42. /**
  43. * Animations to be used for the post processing
  44. */
  45. public animations = new Array<Animation>();
  46. /**
  47. * Enable Pixel Perfect mode where texture is not scaled to be power of 2.
  48. * Can only be used on a single postprocess or on the last one of a chain. (default: false)
  49. */
  50. public enablePixelPerfectMode = false;
  51. /**
  52. * Force the postprocess to be applied without taking in account viewport
  53. */
  54. public forceFullscreenViewport = true;
  55. /**
  56. * Scale mode for the post process (default: Engine.SCALEMODE_FLOOR)
  57. *
  58. * | Value | Type | Description |
  59. * | ----- | ----------------------------------- | ----------- |
  60. * | 1 | SCALEMODE_FLOOR | [engine.scalemode_floor](http://doc.babylonjs.com/api/classes/babylon.engine#scalemode_floor) |
  61. * | 2 | SCALEMODE_NEAREST | [engine.scalemode_nearest](http://doc.babylonjs.com/api/classes/babylon.engine#scalemode_nearest) |
  62. * | 3 | SCALEMODE_CEILING | [engine.scalemode_ceiling](http://doc.babylonjs.com/api/classes/babylon.engine#scalemode_ceiling) |
  63. *
  64. */
  65. public scaleMode = Engine.SCALEMODE_FLOOR;
  66. /**
  67. * Force textures to be a power of two (default: false)
  68. */
  69. public alwaysForcePOT = false;
  70. /**
  71. * Number of sample textures (default: 1)
  72. */
  73. public samples = 1;
  74. /**
  75. * Modify the scale of the post process to be the same as the viewport (default: false)
  76. */
  77. public adaptScaleToCurrentViewport = false;
  78. private _camera: Camera;
  79. private _scene: Scene;
  80. private _engine: Engine;
  81. private _options: number | PostProcessOptions;
  82. private _reusable = false;
  83. private _textureType: number;
  84. /**
  85. * Smart array of input and output textures for the post process.
  86. */
  87. public _textures = new SmartArray<InternalTexture>(2);
  88. /**
  89. * The index in _textures that corresponds to the output texture.
  90. */
  91. public _currentRenderTextureInd = 0;
  92. private _effect: Effect;
  93. private _samplers: string[];
  94. private _fragmentUrl: string;
  95. private _vertexUrl: string;
  96. private _parameters: string[];
  97. private _scaleRatio = new Vector2(1, 1);
  98. protected _indexParameters: any;
  99. private _shareOutputWithPostProcess: Nullable<PostProcess>;
  100. private _texelSize = Vector2.Zero();
  101. private _forcedOutputTexture: InternalTexture;
  102. // Events
  103. /**
  104. * An event triggered when the postprocess is activated.
  105. */
  106. public onActivateObservable = new Observable<Camera>();
  107. private _onActivateObserver: Nullable<Observer<Camera>>;
  108. /**
  109. * A function that is added to the onActivateObservable
  110. */
  111. public set onActivate(callback: Nullable<(camera: Camera) => void>) {
  112. if (this._onActivateObserver) {
  113. this.onActivateObservable.remove(this._onActivateObserver);
  114. }
  115. if (callback) {
  116. this._onActivateObserver = this.onActivateObservable.add(callback);
  117. }
  118. }
  119. /**
  120. * An event triggered when the postprocess changes its size.
  121. */
  122. public onSizeChangedObservable = new Observable<PostProcess>();
  123. private _onSizeChangedObserver: Nullable<Observer<PostProcess>>;
  124. /**
  125. * A function that is added to the onSizeChangedObservable
  126. */
  127. public set onSizeChanged(callback: (postProcess: PostProcess) => void) {
  128. if (this._onSizeChangedObserver) {
  129. this.onSizeChangedObservable.remove(this._onSizeChangedObserver);
  130. }
  131. this._onSizeChangedObserver = this.onSizeChangedObservable.add(callback);
  132. }
  133. /**
  134. * An event triggered when the postprocess applies its effect.
  135. */
  136. public onApplyObservable = new Observable<Effect>();
  137. private _onApplyObserver: Nullable<Observer<Effect>>;
  138. /**
  139. * A function that is added to the onApplyObservable
  140. */
  141. public set onApply(callback: (effect: Effect) => void) {
  142. if (this._onApplyObserver) {
  143. this.onApplyObservable.remove(this._onApplyObserver);
  144. }
  145. this._onApplyObserver = this.onApplyObservable.add(callback);
  146. }
  147. /**
  148. * An event triggered before rendering the postprocess
  149. */
  150. public onBeforeRenderObservable = new Observable<Effect>();
  151. private _onBeforeRenderObserver: Nullable<Observer<Effect>>;
  152. /**
  153. * A function that is added to the onBeforeRenderObservable
  154. */
  155. public set onBeforeRender(callback: (effect: Effect) => void) {
  156. if (this._onBeforeRenderObserver) {
  157. this.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
  158. }
  159. this._onBeforeRenderObserver = this.onBeforeRenderObservable.add(callback);
  160. }
  161. /**
  162. * An event triggered after rendering the postprocess
  163. */
  164. public onAfterRenderObservable = new Observable<Effect>();
  165. private _onAfterRenderObserver: Nullable<Observer<Effect>>;
  166. /**
  167. * A function that is added to the onAfterRenderObservable
  168. */
  169. public set onAfterRender(callback: (efect: Effect) => void) {
  170. if (this._onAfterRenderObserver) {
  171. this.onAfterRenderObservable.remove(this._onAfterRenderObserver);
  172. }
  173. this._onAfterRenderObserver = this.onAfterRenderObservable.add(callback);
  174. }
  175. /**
  176. * The input texture for this post process and the output texture of the previous post process. When added to a pipeline the previous post process will
  177. * render it's output into this texture and this texture will be used as textureSampler in the fragment shader of this post process.
  178. */
  179. public get inputTexture(): InternalTexture {
  180. return this._textures.data[this._currentRenderTextureInd];
  181. }
  182. public set inputTexture(value: InternalTexture) {
  183. this._forcedOutputTexture = value;
  184. }
  185. /**
  186. * Gets the camera which post process is applied to.
  187. * @returns The camera the post process is applied to.
  188. */
  189. public getCamera(): Camera {
  190. return this._camera;
  191. }
  192. /**
  193. * Gets the texel size of the postprocess.
  194. * See https://en.wikipedia.org/wiki/Texel_(graphics)
  195. */
  196. public get texelSize(): Vector2 {
  197. if (this._shareOutputWithPostProcess) {
  198. return this._shareOutputWithPostProcess.texelSize;
  199. }
  200. if (this._forcedOutputTexture) {
  201. this._texelSize.copyFromFloats(1.0 / this._forcedOutputTexture.width, 1.0 / this._forcedOutputTexture.height);
  202. }
  203. return this._texelSize;
  204. }
  205. /**
  206. * Creates a new instance PostProcess
  207. * @param name The name of the PostProcess.
  208. * @param fragmentUrl The url of the fragment shader to be used.
  209. * @param parameters Array of the names of uniform non-sampler2D variables that will be passed to the shader.
  210. * @param samplers Array of the names of uniform sampler2D variables that will be passed to the shader.
  211. * @param options The required width/height ratio to downsize to before computing the render pass. (Use 1.0 for full size)
  212. * @param camera The camera to apply the render pass to.
  213. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  214. * @param engine The engine which the post process will be applied. (default: current engine)
  215. * @param reusable If the post process can be reused on the same frame. (default: false)
  216. * @param defines String of defines that will be set when running the fragment shader. (default: null)
  217. * @param textureType Type of textures used when performing the post process. (default: 0)
  218. * @param vertexUrl The url of the vertex shader to be used. (default: "postprocess")
  219. * @param indexParameters The index parameters to be used for babylons include syntax "#include<kernelBlurVaryingDeclaration>[0..varyingCount]". (default: undefined) See usage in babylon.blurPostProcess.ts and kernelBlur.vertex.fx
  220. * @param blockCompilation If the shader should not be compiled imediatly. (default: false)
  221. */
  222. constructor(/** Name of the PostProcess. */public name: string, fragmentUrl: string, parameters: Nullable<string[]>, samplers: Nullable<string[]>, options: number | PostProcessOptions, camera: Nullable<Camera>,
  223. samplingMode: number = Texture.NEAREST_SAMPLINGMODE, engine?: Engine, reusable?: boolean, defines: Nullable<string> = null, textureType: number = Engine.TEXTURETYPE_UNSIGNED_INT, vertexUrl: string = "postprocess", indexParameters?: any, blockCompilation = false) {
  224. if (camera != null) {
  225. this._camera = camera;
  226. this._scene = camera.getScene();
  227. camera.attachPostProcess(this);
  228. this._engine = this._scene.getEngine();
  229. this._scene.postProcesses.push(this);
  230. }
  231. else if (engine) {
  232. this._engine = engine;
  233. this._engine.postProcesses.push(this);
  234. }
  235. this._options = options;
  236. this.renderTargetSamplingMode = samplingMode ? samplingMode : Texture.NEAREST_SAMPLINGMODE;
  237. this._reusable = reusable || false;
  238. this._textureType = textureType;
  239. this._samplers = samplers || [];
  240. this._samplers.push("textureSampler");
  241. this._fragmentUrl = fragmentUrl;
  242. this._vertexUrl = vertexUrl;
  243. this._parameters = parameters || [];
  244. this._parameters.push("scale");
  245. this._indexParameters = indexParameters;
  246. if (!blockCompilation) {
  247. this.updateEffect(defines);
  248. }
  249. }
  250. /**
  251. * Gets the engine which this post process belongs to.
  252. * @returns The engine the post process was enabled with.
  253. */
  254. public getEngine(): Engine {
  255. return this._engine;
  256. }
  257. /**
  258. * The effect that is created when initializing the post process.
  259. * @returns The created effect corrisponding the the postprocess.
  260. */
  261. public getEffect(): Effect {
  262. return this._effect;
  263. }
  264. /**
  265. * To avoid multiple redundant textures for multiple post process, the output the output texture for this post process can be shared with another.
  266. * @param postProcess The post process to share the output with.
  267. * @returns This post process.
  268. */
  269. public shareOutputWith(postProcess: PostProcess): PostProcess {
  270. this._disposeTextures();
  271. this._shareOutputWithPostProcess = postProcess;
  272. return this;
  273. }
  274. /**
  275. * Reverses the effect of calling shareOutputWith and returns the post process back to its original state.
  276. * This should be called if the post process that shares output with this post process is disabled/disposed.
  277. */
  278. public useOwnOutput() {
  279. if(this._textures.length == 0){
  280. this._textures = new SmartArray<InternalTexture>(2);
  281. }
  282. this._shareOutputWithPostProcess = null;
  283. }
  284. /**
  285. * Updates the effect with the current post process compile time values and recompiles the shader.
  286. * @param defines Define statements that should be added at the beginning of the shader. (default: null)
  287. * @param uniforms Set of uniform variables that will be passed to the shader. (default: null)
  288. * @param samplers Set of Texture2D variables that will be passed to the shader. (default: null)
  289. * @param indexParameters The index parameters to be used for babylons include syntax "#include<kernelBlurVaryingDeclaration>[0..varyingCount]". (default: undefined) See usage in babylon.blurPostProcess.ts and kernelBlur.vertex.fx
  290. * @param onCompiled Called when the shader has been compiled.
  291. * @param onError Called if there is an error when compiling a shader.
  292. */
  293. public updateEffect(defines: Nullable<string> = null, uniforms: Nullable<string[]> = null, samplers: Nullable<string[]> = null, indexParameters?: any,
  294. onCompiled?: (effect: Effect) => void, onError?: (effect: Effect, errors: string) => void) {
  295. this._effect = this._engine.createEffect({ vertex: this._vertexUrl, fragment: this._fragmentUrl },
  296. ["position"],
  297. uniforms || this._parameters,
  298. samplers || this._samplers,
  299. defines !== null ? defines : "",
  300. undefined,
  301. onCompiled,
  302. onError,
  303. indexParameters || this._indexParameters
  304. );
  305. }
  306. /**
  307. * The post process is reusable if it can be used multiple times within one frame.
  308. * @returns If the post process is reusable
  309. */
  310. public isReusable(): boolean {
  311. return this._reusable;
  312. }
  313. /** invalidate frameBuffer to hint the postprocess to create a depth buffer */
  314. public markTextureDirty(): void {
  315. this.width = -1;
  316. }
  317. /**
  318. * Activates the post process by intializing the textures to be used when executed. Notifies onActivateObservable.
  319. * When this post process is used in a pipeline, this is call will bind the input texture of this post process to the output of the previous.
  320. * @param camera The camera that will be used in the post process. This camera will be used when calling onActivateObservable.
  321. * @param sourceTexture The source texture to be inspected to get the width and height if not specified in the post process constructor. (default: null)
  322. * @param forceDepthStencil If true, a depth and stencil buffer will be generated. (default: false)
  323. * @returns The target texture that was bound to be written to.
  324. */
  325. public activate(camera: Nullable<Camera>, sourceTexture: Nullable<InternalTexture> = null, forceDepthStencil?: boolean): InternalTexture {
  326. camera = camera || this._camera;
  327. var scene = camera.getScene();
  328. var engine = scene.getEngine();
  329. var maxSize = engine.getCaps().maxTextureSize;
  330. var requiredWidth = ((sourceTexture ? sourceTexture.width : this._engine.getRenderWidth(true)) * <number>this._options) | 0;
  331. var requiredHeight = ((sourceTexture ? sourceTexture.height : this._engine.getRenderHeight(true)) * <number>this._options) | 0;
  332. // If rendering to a webvr camera's left or right eye only half the width should be used to avoid resize when rendered to screen
  333. var webVRCamera = (<WebVRFreeCamera>camera.parent);
  334. if(webVRCamera && (webVRCamera.leftCamera == camera || webVRCamera.rightCamera == camera)){
  335. requiredWidth/=2;
  336. }
  337. var desiredWidth = ((<PostProcessOptions>this._options).width || requiredWidth);
  338. var desiredHeight = (<PostProcessOptions>this._options).height || requiredHeight;
  339. if (!this._shareOutputWithPostProcess && !this._forcedOutputTexture) {
  340. if (this.adaptScaleToCurrentViewport) {
  341. let currentViewport = engine.currentViewport;
  342. if (currentViewport) {
  343. desiredWidth *= currentViewport.width;
  344. desiredHeight *= currentViewport.height;
  345. }
  346. }
  347. if (this.renderTargetSamplingMode === Texture.TRILINEAR_SAMPLINGMODE || this.alwaysForcePOT) {
  348. if (!(<PostProcessOptions>this._options).width) {
  349. desiredWidth = engine.needPOTTextures ? Tools.GetExponentOfTwo(desiredWidth, maxSize, this.scaleMode) : desiredWidth;
  350. }
  351. if (!(<PostProcessOptions>this._options).height) {
  352. desiredHeight = engine.needPOTTextures ? Tools.GetExponentOfTwo(desiredHeight, maxSize, this.scaleMode) : desiredHeight;
  353. }
  354. }
  355. if (this.width !== desiredWidth || this.height !== desiredHeight) {
  356. if (this._textures.length > 0) {
  357. for (var i = 0; i < this._textures.length; i++) {
  358. this._engine._releaseTexture(this._textures.data[i]);
  359. }
  360. this._textures.reset();
  361. }
  362. this.width = desiredWidth;
  363. this.height = desiredHeight;
  364. let textureSize = { width: this.width, height: this.height };
  365. let textureOptions = {
  366. generateMipMaps: false,
  367. generateDepthBuffer: forceDepthStencil || camera._postProcesses.indexOf(this) === 0,
  368. generateStencilBuffer: (forceDepthStencil || camera._postProcesses.indexOf(this) === 0) && this._engine.isStencilEnable,
  369. samplingMode: this.renderTargetSamplingMode,
  370. type: this._textureType
  371. };
  372. this._textures.push(this._engine.createRenderTargetTexture(textureSize, textureOptions));
  373. if (this._reusable) {
  374. this._textures.push(this._engine.createRenderTargetTexture(textureSize, textureOptions));
  375. }
  376. this._texelSize.copyFromFloats(1.0 / this.width, 1.0 / this.height);
  377. this.onSizeChangedObservable.notifyObservers(this);
  378. }
  379. this._textures.forEach(texture => {
  380. if (texture.samples !== this.samples) {
  381. this._engine.updateRenderTargetTextureSampleCount(texture, this.samples);
  382. }
  383. });
  384. }
  385. var target: InternalTexture;
  386. if (this._shareOutputWithPostProcess) {
  387. target = this._shareOutputWithPostProcess.inputTexture;
  388. } else if (this._forcedOutputTexture) {
  389. target = this._forcedOutputTexture;
  390. this.width = this._forcedOutputTexture.width;
  391. this.height = this._forcedOutputTexture.height;
  392. } else {
  393. target = this.inputTexture;
  394. }
  395. // Bind the input of this post process to be used as the output of the previous post process.
  396. if (this.enablePixelPerfectMode) {
  397. this._scaleRatio.copyFromFloats(requiredWidth / desiredWidth, requiredHeight / desiredHeight);
  398. this._engine.bindFramebuffer(target, 0, requiredWidth, requiredHeight, this.forceFullscreenViewport);
  399. }
  400. else {
  401. this._scaleRatio.copyFromFloats(1, 1);
  402. this._engine.bindFramebuffer(target, 0, undefined, undefined, this.forceFullscreenViewport);
  403. }
  404. this.onActivateObservable.notifyObservers(camera);
  405. // Clear
  406. if (scene._allowPostProcessClear && this.autoClear && this.alphaMode === Engine.ALPHA_DISABLE) {
  407. this._engine.clear(this.clearColor ? this.clearColor : scene.clearColor, true, true, true);
  408. }
  409. if (this._reusable) {
  410. this._currentRenderTextureInd = (this._currentRenderTextureInd + 1) % 2;
  411. }
  412. return target;
  413. }
  414. /**
  415. * If the post process is supported.
  416. */
  417. public get isSupported(): boolean {
  418. return this._effect.isSupported;
  419. }
  420. /**
  421. * The aspect ratio of the output texture.
  422. */
  423. public get aspectRatio(): number {
  424. if (this._shareOutputWithPostProcess) {
  425. return this._shareOutputWithPostProcess.aspectRatio;
  426. }
  427. if (this._forcedOutputTexture) {
  428. return this._forcedOutputTexture.width / this._forcedOutputTexture.height;
  429. }
  430. return this.width / this.height;
  431. }
  432. /**
  433. * Get a value indicating if the post-process is ready to be used
  434. * @returns true if the post-process is ready (shader is compiled)
  435. */
  436. public isReady(): boolean {
  437. return this._effect && this._effect.isReady();
  438. }
  439. /**
  440. * Binds all textures and uniforms to the shader, this will be run on every pass.
  441. * @returns the effect corrisponding to this post process. Null if not compiled or not ready.
  442. */
  443. public apply(): Nullable<Effect> {
  444. // Check
  445. if (!this._effect || !this._effect.isReady())
  446. return null;
  447. // States
  448. this._engine.enableEffect(this._effect);
  449. this._engine.setState(false);
  450. this._engine.setDepthBuffer(false);
  451. this._engine.setDepthWrite(false);
  452. // Alpha
  453. this._engine.setAlphaMode(this.alphaMode);
  454. if (this.alphaConstants) {
  455. this.getEngine().setAlphaConstants(this.alphaConstants.r, this.alphaConstants.g, this.alphaConstants.b, this.alphaConstants.a);
  456. }
  457. // Bind the output texture of the preivous post process as the input to this post process.
  458. var source: InternalTexture;
  459. if (this._shareOutputWithPostProcess) {
  460. source = this._shareOutputWithPostProcess.inputTexture;
  461. } else if (this._forcedOutputTexture) {
  462. source = this._forcedOutputTexture;
  463. } else {
  464. source = this.inputTexture;
  465. }
  466. this._effect._bindTexture("textureSampler", source);
  467. // Parameters
  468. this._effect.setVector2("scale", this._scaleRatio);
  469. this.onApplyObservable.notifyObservers(this._effect);
  470. return this._effect;
  471. }
  472. private _disposeTextures() {
  473. if (this._shareOutputWithPostProcess || this._forcedOutputTexture) {
  474. return;
  475. }
  476. if (this._textures.length > 0) {
  477. for (var i = 0; i < this._textures.length; i++) {
  478. this._engine._releaseTexture(this._textures.data[i]);
  479. }
  480. }
  481. this._textures.dispose();
  482. }
  483. /**
  484. * Disposes the post process.
  485. * @param camera The camera to dispose the post process on.
  486. */
  487. public dispose(camera?: Camera): void {
  488. camera = camera || this._camera;
  489. this._disposeTextures();
  490. if (this._scene) {
  491. let index = this._scene.postProcesses.indexOf(this);
  492. if (index !== -1) {
  493. this._scene.postProcesses.splice(index, 1);
  494. }
  495. } else {
  496. let index = this._engine.postProcesses.indexOf(this);
  497. if (index !== -1) {
  498. this._engine.postProcesses.splice(index, 1);
  499. }
  500. }
  501. if (!camera) {
  502. return;
  503. }
  504. camera.detachPostProcess(this);
  505. var index = camera._postProcesses.indexOf(this);
  506. if (index === 0 && camera._postProcesses.length > 0) {
  507. var firstPostProcess = this._camera._getFirstPostProcess();
  508. if(firstPostProcess){
  509. firstPostProcess.markTextureDirty();
  510. }
  511. }
  512. this.onActivateObservable.clear();
  513. this.onAfterRenderObservable.clear();
  514. this.onApplyObservable.clear();
  515. this.onBeforeRenderObservable.clear();
  516. this.onSizeChangedObservable.clear();
  517. }
  518. }
  519. }