postProcess.ts 25 KB

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