postProcess.ts 26 KB

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