postProcess.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. import { __decorate } from "../tslib.es6.js";
  2. import { SmartArray } from "../Misc/smartArray.js";
  3. import { Observable } from "../Misc/observable.js";
  4. import { Vector2 } from "../Maths/math.vector.js";
  5. import "../Shaders/postprocess.vertex.js";
  6. import "../Engines/Extensions/engine.renderTarget.js";
  7. import { serialize, serializeAsColor4 } from "../Misc/decorators.js";
  8. import { SerializationHelper } from "../Misc/decorators.serialization.js";
  9. import { GetClass, RegisterClass } from "../Misc/typeStore.js";
  10. import { DrawWrapper } from "../Materials/drawWrapper.js";
  11. import { ShaderLanguage } from "../Materials/shaderLanguage.js";
  12. import { GetExponentOfTwo } from "../Misc/tools.functions.js";
  13. /**
  14. * PostProcess can be used to apply a shader to a texture after it has been rendered
  15. * See https://doc.babylonjs.com/features/featuresDeepDive/postProcesses/usePostProcesses
  16. */
  17. export class PostProcess {
  18. /**
  19. * Registers a shader code processing with a post process name.
  20. * @param postProcessName name of the post process. Use null for the fallback shader code processing. This is the shader code processing that will be used in case no specific shader code processing has been associated to a post process name
  21. * @param customShaderCodeProcessing shader code processing to associate to the post process name
  22. */
  23. static RegisterShaderCodeProcessing(postProcessName, customShaderCodeProcessing) {
  24. if (!customShaderCodeProcessing) {
  25. delete PostProcess._CustomShaderCodeProcessing[postProcessName ?? ""];
  26. return;
  27. }
  28. PostProcess._CustomShaderCodeProcessing[postProcessName ?? ""] = customShaderCodeProcessing;
  29. }
  30. static _GetShaderCodeProcessing(postProcessName) {
  31. return PostProcess._CustomShaderCodeProcessing[postProcessName] ?? PostProcess._CustomShaderCodeProcessing[""];
  32. }
  33. /**
  34. * Number of sample textures (default: 1)
  35. */
  36. get samples() {
  37. return this._samples;
  38. }
  39. set samples(n) {
  40. this._samples = Math.min(n, this._engine.getCaps().maxMSAASamples);
  41. this._textures.forEach((texture) => {
  42. texture.setSamples(this._samples);
  43. });
  44. }
  45. /**
  46. * Returns the fragment url or shader name used in the post process.
  47. * @returns the fragment url or name in the shader store.
  48. */
  49. getEffectName() {
  50. return this._fragmentUrl;
  51. }
  52. /**
  53. * A function that is added to the onActivateObservable
  54. */
  55. set onActivate(callback) {
  56. if (this._onActivateObserver) {
  57. this.onActivateObservable.remove(this._onActivateObserver);
  58. }
  59. if (callback) {
  60. this._onActivateObserver = this.onActivateObservable.add(callback);
  61. }
  62. }
  63. /**
  64. * A function that is added to the onSizeChangedObservable
  65. */
  66. set onSizeChanged(callback) {
  67. if (this._onSizeChangedObserver) {
  68. this.onSizeChangedObservable.remove(this._onSizeChangedObserver);
  69. }
  70. this._onSizeChangedObserver = this.onSizeChangedObservable.add(callback);
  71. }
  72. /**
  73. * A function that is added to the onApplyObservable
  74. */
  75. set onApply(callback) {
  76. if (this._onApplyObserver) {
  77. this.onApplyObservable.remove(this._onApplyObserver);
  78. }
  79. this._onApplyObserver = this.onApplyObservable.add(callback);
  80. }
  81. /**
  82. * A function that is added to the onBeforeRenderObservable
  83. */
  84. set onBeforeRender(callback) {
  85. if (this._onBeforeRenderObserver) {
  86. this.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
  87. }
  88. this._onBeforeRenderObserver = this.onBeforeRenderObservable.add(callback);
  89. }
  90. /**
  91. * A function that is added to the onAfterRenderObservable
  92. */
  93. set onAfterRender(callback) {
  94. if (this._onAfterRenderObserver) {
  95. this.onAfterRenderObservable.remove(this._onAfterRenderObserver);
  96. }
  97. this._onAfterRenderObserver = this.onAfterRenderObservable.add(callback);
  98. }
  99. /**
  100. * 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
  101. * render it's output into this texture and this texture will be used as textureSampler in the fragment shader of this post process.
  102. */
  103. get inputTexture() {
  104. return this._textures.data[this._currentRenderTextureInd];
  105. }
  106. set inputTexture(value) {
  107. this._forcedOutputTexture = value;
  108. }
  109. /**
  110. * Since inputTexture should always be defined, if we previously manually set `inputTexture`,
  111. * the only way to unset it is to use this function to restore its internal state
  112. */
  113. restoreDefaultInputTexture() {
  114. if (this._forcedOutputTexture) {
  115. this._forcedOutputTexture = null;
  116. this.markTextureDirty();
  117. }
  118. }
  119. /**
  120. * Gets the camera which post process is applied to.
  121. * @returns The camera the post process is applied to.
  122. */
  123. getCamera() {
  124. return this._camera;
  125. }
  126. /**
  127. * Gets the texel size of the postprocess.
  128. * See https://en.wikipedia.org/wiki/Texel_(graphics)
  129. */
  130. get texelSize() {
  131. if (this._shareOutputWithPostProcess) {
  132. return this._shareOutputWithPostProcess.texelSize;
  133. }
  134. if (this._forcedOutputTexture) {
  135. this._texelSize.copyFromFloats(1.0 / this._forcedOutputTexture.width, 1.0 / this._forcedOutputTexture.height);
  136. }
  137. return this._texelSize;
  138. }
  139. /** @internal */
  140. constructor(name, fragmentUrl, parameters, samplers, _size, camera, samplingMode = 1, engine, reusable, defines = null, textureType = 0, vertexUrl = "postprocess", indexParameters, blockCompilation = false, textureFormat = 5, shaderLanguage = ShaderLanguage.GLSL) {
  141. /** @internal */
  142. this._parentContainer = null;
  143. /**
  144. * Width of the texture to apply the post process on
  145. */
  146. this.width = -1;
  147. /**
  148. * Height of the texture to apply the post process on
  149. */
  150. this.height = -1;
  151. /**
  152. * Gets the node material used to create this postprocess (null if the postprocess was manually created)
  153. */
  154. this.nodeMaterialSource = null;
  155. /**
  156. * Internal, reference to the location where this postprocess was output to. (Typically the texture on the next postprocess in the chain)
  157. * @internal
  158. */
  159. this._outputTexture = null;
  160. /**
  161. * If the buffer needs to be cleared before applying the post process. (default: true)
  162. * Should be set to false if shader will overwrite all previous pixels.
  163. */
  164. this.autoClear = true;
  165. /**
  166. * If clearing the buffer should be forced in autoClear mode, even when alpha mode is enabled (default: false).
  167. * By default, the buffer will only be cleared if alpha mode is disabled (and autoClear is true).
  168. */
  169. this.forceAutoClearInAlphaMode = false;
  170. /**
  171. * Type of alpha mode to use when performing the post process (default: Engine.ALPHA_DISABLE)
  172. */
  173. this.alphaMode = 0;
  174. /**
  175. * Animations to be used for the post processing
  176. */
  177. this.animations = [];
  178. /**
  179. * Enable Pixel Perfect mode where texture is not scaled to be power of 2.
  180. * Can only be used on a single postprocess or on the last one of a chain. (default: false)
  181. */
  182. this.enablePixelPerfectMode = false;
  183. /**
  184. * Force the postprocess to be applied without taking in account viewport
  185. */
  186. this.forceFullscreenViewport = true;
  187. /**
  188. * Scale mode for the post process (default: Engine.SCALEMODE_FLOOR)
  189. *
  190. * | Value | Type | Description |
  191. * | ----- | ----------------------------------- | ----------- |
  192. * | 1 | SCALEMODE_FLOOR | [engine.scalemode_floor](https://doc.babylonjs.com/api/classes/babylon.engine#scalemode_floor) |
  193. * | 2 | SCALEMODE_NEAREST | [engine.scalemode_nearest](https://doc.babylonjs.com/api/classes/babylon.engine#scalemode_nearest) |
  194. * | 3 | SCALEMODE_CEILING | [engine.scalemode_ceiling](https://doc.babylonjs.com/api/classes/babylon.engine#scalemode_ceiling) |
  195. *
  196. */
  197. this.scaleMode = 1;
  198. /**
  199. * Force textures to be a power of two (default: false)
  200. */
  201. this.alwaysForcePOT = false;
  202. this._samples = 1;
  203. /**
  204. * Modify the scale of the post process to be the same as the viewport (default: false)
  205. */
  206. this.adaptScaleToCurrentViewport = false;
  207. this._reusable = false;
  208. this._renderId = 0;
  209. /**
  210. * if externalTextureSamplerBinding is true, the "apply" method won't bind the textureSampler texture, it is expected to be done by the "outside" (by the onApplyObservable observer most probably).
  211. * counter-productive in some cases because if the texture bound by "apply" is different from the currently texture bound, (the one set by the onApplyObservable observer, for eg) some
  212. * internal structures (materialContext) will be dirtified, which may impact performances
  213. */
  214. this.externalTextureSamplerBinding = false;
  215. /**
  216. * Smart array of input and output textures for the post process.
  217. * @internal
  218. */
  219. this._textures = new SmartArray(2);
  220. /**
  221. * Smart array of input and output textures for the post process.
  222. * @internal
  223. */
  224. this._textureCache = [];
  225. /**
  226. * The index in _textures that corresponds to the output texture.
  227. * @internal
  228. */
  229. this._currentRenderTextureInd = 0;
  230. this._scaleRatio = new Vector2(1, 1);
  231. this._texelSize = Vector2.Zero();
  232. // Events
  233. /**
  234. * An event triggered when the postprocess is activated.
  235. */
  236. this.onActivateObservable = new Observable();
  237. /**
  238. * An event triggered when the postprocess changes its size.
  239. */
  240. this.onSizeChangedObservable = new Observable();
  241. /**
  242. * An event triggered when the postprocess applies its effect.
  243. */
  244. this.onApplyObservable = new Observable();
  245. /**
  246. * An event triggered before rendering the postprocess
  247. */
  248. this.onBeforeRenderObservable = new Observable();
  249. /**
  250. * An event triggered after rendering the postprocess
  251. */
  252. this.onAfterRenderObservable = new Observable();
  253. this.name = name;
  254. let size = 1;
  255. let uniformBuffers = null;
  256. if (parameters && !Array.isArray(parameters)) {
  257. const options = parameters;
  258. parameters = options.uniforms ?? null;
  259. samplers = options.samplers ?? null;
  260. size = options.size ?? 1;
  261. camera = options.camera ?? null;
  262. samplingMode = options.samplingMode ?? 1;
  263. engine = options.engine;
  264. reusable = options.reusable;
  265. defines = options.defines ?? null;
  266. textureType = options.textureType ?? 0;
  267. vertexUrl = options.vertexUrl ?? "postprocess";
  268. indexParameters = options.indexParameters;
  269. blockCompilation = options.blockCompilation ?? false;
  270. textureFormat = options.textureFormat ?? 5;
  271. shaderLanguage = options.shaderLanguage ?? ShaderLanguage.GLSL;
  272. uniformBuffers = options.uniformBuffers ?? null;
  273. }
  274. else if (_size) {
  275. if (typeof _size === "number") {
  276. size = _size;
  277. }
  278. else {
  279. size = { width: _size.width, height: _size.height };
  280. }
  281. }
  282. if (camera != null) {
  283. this._camera = camera;
  284. this._scene = camera.getScene();
  285. camera.attachPostProcess(this);
  286. this._engine = this._scene.getEngine();
  287. this._scene.postProcesses.push(this);
  288. this.uniqueId = this._scene.getUniqueId();
  289. }
  290. else if (engine) {
  291. this._engine = engine;
  292. this._engine.postProcesses.push(this);
  293. }
  294. this._options = size;
  295. this.renderTargetSamplingMode = samplingMode ? samplingMode : 1;
  296. this._reusable = reusable || false;
  297. this._textureType = textureType;
  298. this._textureFormat = textureFormat;
  299. this._shaderLanguage = shaderLanguage;
  300. this._samplers = samplers || [];
  301. this._samplers.push("textureSampler");
  302. this._fragmentUrl = fragmentUrl;
  303. this._vertexUrl = vertexUrl;
  304. this._parameters = parameters || [];
  305. this._parameters.push("scale");
  306. this._uniformBuffers = uniformBuffers || [];
  307. this._indexParameters = indexParameters;
  308. this._drawWrapper = new DrawWrapper(this._engine);
  309. if (!blockCompilation) {
  310. this.updateEffect(defines);
  311. }
  312. }
  313. /**
  314. * Gets a string identifying the name of the class
  315. * @returns "PostProcess" string
  316. */
  317. getClassName() {
  318. return "PostProcess";
  319. }
  320. /**
  321. * Gets the engine which this post process belongs to.
  322. * @returns The engine the post process was enabled with.
  323. */
  324. getEngine() {
  325. return this._engine;
  326. }
  327. /**
  328. * The effect that is created when initializing the post process.
  329. * @returns The created effect corresponding the postprocess.
  330. */
  331. getEffect() {
  332. return this._drawWrapper.effect;
  333. }
  334. /**
  335. * To avoid multiple redundant textures for multiple post process, the output the output texture for this post process can be shared with another.
  336. * @param postProcess The post process to share the output with.
  337. * @returns This post process.
  338. */
  339. shareOutputWith(postProcess) {
  340. this._disposeTextures();
  341. this._shareOutputWithPostProcess = postProcess;
  342. return this;
  343. }
  344. /**
  345. * Reverses the effect of calling shareOutputWith and returns the post process back to its original state.
  346. * This should be called if the post process that shares output with this post process is disabled/disposed.
  347. */
  348. useOwnOutput() {
  349. if (this._textures.length == 0) {
  350. this._textures = new SmartArray(2);
  351. }
  352. this._shareOutputWithPostProcess = null;
  353. }
  354. /**
  355. * Updates the effect with the current post process compile time values and recompiles the shader.
  356. * @param defines Define statements that should be added at the beginning of the shader. (default: null)
  357. * @param uniforms Set of uniform variables that will be passed to the shader. (default: null)
  358. * @param samplers Set of Texture2D variables that will be passed to the shader. (default: null)
  359. * @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
  360. * @param onCompiled Called when the shader has been compiled.
  361. * @param onError Called if there is an error when compiling a shader.
  362. * @param vertexUrl The url of the vertex shader to be used (default: the one given at construction time)
  363. * @param fragmentUrl The url of the fragment shader to be used (default: the one given at construction time)
  364. */
  365. updateEffect(defines = null, uniforms = null, samplers = null, indexParameters, onCompiled, onError, vertexUrl, fragmentUrl) {
  366. const customShaderCodeProcessing = PostProcess._GetShaderCodeProcessing(this.name);
  367. if (customShaderCodeProcessing?.defineCustomBindings) {
  368. const newUniforms = uniforms?.slice() ?? [];
  369. newUniforms.push(...this._parameters);
  370. const newSamplers = samplers?.slice() ?? [];
  371. newSamplers.push(...this._samplers);
  372. defines = customShaderCodeProcessing.defineCustomBindings(this.name, defines, newUniforms, newSamplers);
  373. uniforms = newUniforms;
  374. samplers = newSamplers;
  375. }
  376. this._postProcessDefines = defines;
  377. this._drawWrapper.effect = this._engine.createEffect({ vertex: vertexUrl ?? this._vertexUrl, fragment: fragmentUrl ?? this._fragmentUrl }, {
  378. attributes: ["position"],
  379. uniformsNames: uniforms || this._parameters,
  380. uniformBuffersNames: this._uniformBuffers,
  381. samplers: samplers || this._samplers,
  382. defines: defines !== null ? defines : "",
  383. fallbacks: null,
  384. onCompiled: onCompiled ?? null,
  385. onError: onError ?? null,
  386. indexParameters: indexParameters || this._indexParameters,
  387. processCodeAfterIncludes: customShaderCodeProcessing?.processCodeAfterIncludes
  388. ? (shaderType, code) => customShaderCodeProcessing.processCodeAfterIncludes(this.name, shaderType, code)
  389. : null,
  390. processFinalCode: customShaderCodeProcessing?.processFinalCode
  391. ? (shaderType, code) => customShaderCodeProcessing.processFinalCode(this.name, shaderType, code)
  392. : null,
  393. shaderLanguage: this._shaderLanguage,
  394. }, this._engine);
  395. }
  396. /**
  397. * The post process is reusable if it can be used multiple times within one frame.
  398. * @returns If the post process is reusable
  399. */
  400. isReusable() {
  401. return this._reusable;
  402. }
  403. /** invalidate frameBuffer to hint the postprocess to create a depth buffer */
  404. markTextureDirty() {
  405. this.width = -1;
  406. }
  407. _createRenderTargetTexture(textureSize, textureOptions, channel = 0) {
  408. for (let i = 0; i < this._textureCache.length; i++) {
  409. if (this._textureCache[i].texture.width === textureSize.width &&
  410. this._textureCache[i].texture.height === textureSize.height &&
  411. this._textureCache[i].postProcessChannel === channel &&
  412. this._textureCache[i].texture._generateDepthBuffer === textureOptions.generateDepthBuffer &&
  413. this._textureCache[i].texture.samples === textureOptions.samples) {
  414. return this._textureCache[i].texture;
  415. }
  416. }
  417. const tex = this._engine.createRenderTargetTexture(textureSize, textureOptions);
  418. this._textureCache.push({ texture: tex, postProcessChannel: channel, lastUsedRenderId: -1 });
  419. return tex;
  420. }
  421. _flushTextureCache() {
  422. const currentRenderId = this._renderId;
  423. for (let i = this._textureCache.length - 1; i >= 0; i--) {
  424. if (currentRenderId - this._textureCache[i].lastUsedRenderId > 100) {
  425. let currentlyUsed = false;
  426. for (let j = 0; j < this._textures.length; j++) {
  427. if (this._textures.data[j] === this._textureCache[i].texture) {
  428. currentlyUsed = true;
  429. break;
  430. }
  431. }
  432. if (!currentlyUsed) {
  433. this._textureCache[i].texture.dispose();
  434. this._textureCache.splice(i, 1);
  435. }
  436. }
  437. }
  438. }
  439. /**
  440. * Resizes the post-process texture
  441. * @param width Width of the texture
  442. * @param height Height of the texture
  443. * @param camera The camera this post-process is applied to. Pass null if the post-process is used outside the context of a camera post-process chain (default: null)
  444. * @param needMipMaps True if mip maps need to be generated after render (default: false)
  445. * @param forceDepthStencil True to force post-process texture creation with stencil depth and buffer (default: false)
  446. */
  447. resize(width, height, camera = null, needMipMaps = false, forceDepthStencil = false) {
  448. if (this._textures.length > 0) {
  449. this._textures.reset();
  450. }
  451. this.width = width;
  452. this.height = height;
  453. let firstPP = null;
  454. if (camera) {
  455. for (let i = 0; i < camera._postProcesses.length; i++) {
  456. if (camera._postProcesses[i] !== null) {
  457. firstPP = camera._postProcesses[i];
  458. break;
  459. }
  460. }
  461. }
  462. const textureSize = { width: this.width, height: this.height };
  463. const textureOptions = {
  464. generateMipMaps: needMipMaps,
  465. generateDepthBuffer: forceDepthStencil || firstPP === this,
  466. generateStencilBuffer: (forceDepthStencil || firstPP === this) && this._engine.isStencilEnable,
  467. samplingMode: this.renderTargetSamplingMode,
  468. type: this._textureType,
  469. format: this._textureFormat,
  470. samples: this._samples,
  471. label: "PostProcessRTT-" + this.name,
  472. };
  473. this._textures.push(this._createRenderTargetTexture(textureSize, textureOptions, 0));
  474. if (this._reusable) {
  475. this._textures.push(this._createRenderTargetTexture(textureSize, textureOptions, 1));
  476. }
  477. this._texelSize.copyFromFloats(1.0 / this.width, 1.0 / this.height);
  478. this.onSizeChangedObservable.notifyObservers(this);
  479. }
  480. _getTarget() {
  481. let target;
  482. if (this._shareOutputWithPostProcess) {
  483. target = this._shareOutputWithPostProcess.inputTexture;
  484. }
  485. else if (this._forcedOutputTexture) {
  486. target = this._forcedOutputTexture;
  487. this.width = this._forcedOutputTexture.width;
  488. this.height = this._forcedOutputTexture.height;
  489. }
  490. else {
  491. target = this.inputTexture;
  492. let cache;
  493. for (let i = 0; i < this._textureCache.length; i++) {
  494. if (this._textureCache[i].texture === target) {
  495. cache = this._textureCache[i];
  496. break;
  497. }
  498. }
  499. if (cache) {
  500. cache.lastUsedRenderId = this._renderId;
  501. }
  502. }
  503. return target;
  504. }
  505. /**
  506. * Activates the post process by intializing the textures to be used when executed. Notifies onActivateObservable.
  507. * 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.
  508. * @param camera The camera that will be used in the post process. This camera will be used when calling onActivateObservable.
  509. * @param sourceTexture The source texture to be inspected to get the width and height if not specified in the post process constructor. (default: null)
  510. * @param forceDepthStencil If true, a depth and stencil buffer will be generated. (default: false)
  511. * @returns The render target wrapper that was bound to be written to.
  512. */
  513. activate(camera, sourceTexture = null, forceDepthStencil) {
  514. camera = camera || this._camera;
  515. const scene = camera.getScene();
  516. const engine = scene.getEngine();
  517. const maxSize = engine.getCaps().maxTextureSize;
  518. const requiredWidth = ((sourceTexture ? sourceTexture.width : this._engine.getRenderWidth(true)) * this._options) | 0;
  519. const requiredHeight = ((sourceTexture ? sourceTexture.height : this._engine.getRenderHeight(true)) * this._options) | 0;
  520. let desiredWidth = this._options.width || requiredWidth;
  521. let desiredHeight = this._options.height || requiredHeight;
  522. const needMipMaps = this.renderTargetSamplingMode !== 7 &&
  523. this.renderTargetSamplingMode !== 1 &&
  524. this.renderTargetSamplingMode !== 2;
  525. let target = null;
  526. if (!this._shareOutputWithPostProcess && !this._forcedOutputTexture) {
  527. if (this.adaptScaleToCurrentViewport) {
  528. const currentViewport = engine.currentViewport;
  529. if (currentViewport) {
  530. desiredWidth *= currentViewport.width;
  531. desiredHeight *= currentViewport.height;
  532. }
  533. }
  534. if (needMipMaps || this.alwaysForcePOT) {
  535. if (!this._options.width) {
  536. desiredWidth = engine.needPOTTextures ? GetExponentOfTwo(desiredWidth, maxSize, this.scaleMode) : desiredWidth;
  537. }
  538. if (!this._options.height) {
  539. desiredHeight = engine.needPOTTextures ? GetExponentOfTwo(desiredHeight, maxSize, this.scaleMode) : desiredHeight;
  540. }
  541. }
  542. if (this.width !== desiredWidth || this.height !== desiredHeight || !(target = this._getTarget())) {
  543. this.resize(desiredWidth, desiredHeight, camera, needMipMaps, forceDepthStencil);
  544. }
  545. this._textures.forEach((texture) => {
  546. if (texture.samples !== this.samples) {
  547. this._engine.updateRenderTargetTextureSampleCount(texture, this.samples);
  548. }
  549. });
  550. this._flushTextureCache();
  551. this._renderId++;
  552. }
  553. if (!target) {
  554. target = this._getTarget();
  555. }
  556. // Bind the input of this post process to be used as the output of the previous post process.
  557. if (this.enablePixelPerfectMode) {
  558. this._scaleRatio.copyFromFloats(requiredWidth / desiredWidth, requiredHeight / desiredHeight);
  559. this._engine.bindFramebuffer(target, 0, requiredWidth, requiredHeight, this.forceFullscreenViewport);
  560. }
  561. else {
  562. this._scaleRatio.copyFromFloats(1, 1);
  563. this._engine.bindFramebuffer(target, 0, undefined, undefined, this.forceFullscreenViewport);
  564. }
  565. this._engine._debugInsertMarker?.(`post process ${this.name} input`);
  566. this.onActivateObservable.notifyObservers(camera);
  567. // Clear
  568. if (this.autoClear && (this.alphaMode === 0 || this.forceAutoClearInAlphaMode)) {
  569. this._engine.clear(this.clearColor ? this.clearColor : scene.clearColor, scene._allowPostProcessClearColor, true, true);
  570. }
  571. if (this._reusable) {
  572. this._currentRenderTextureInd = (this._currentRenderTextureInd + 1) % 2;
  573. }
  574. return target;
  575. }
  576. /**
  577. * If the post process is supported.
  578. */
  579. get isSupported() {
  580. return this._drawWrapper.effect.isSupported;
  581. }
  582. /**
  583. * The aspect ratio of the output texture.
  584. */
  585. get aspectRatio() {
  586. if (this._shareOutputWithPostProcess) {
  587. return this._shareOutputWithPostProcess.aspectRatio;
  588. }
  589. if (this._forcedOutputTexture) {
  590. return this._forcedOutputTexture.width / this._forcedOutputTexture.height;
  591. }
  592. return this.width / this.height;
  593. }
  594. /**
  595. * Get a value indicating if the post-process is ready to be used
  596. * @returns true if the post-process is ready (shader is compiled)
  597. */
  598. isReady() {
  599. return this._drawWrapper.effect?.isReady() ?? false;
  600. }
  601. /**
  602. * Binds all textures and uniforms to the shader, this will be run on every pass.
  603. * @returns the effect corresponding to this post process. Null if not compiled or not ready.
  604. */
  605. apply() {
  606. // Check
  607. if (!this._drawWrapper.effect?.isReady()) {
  608. return null;
  609. }
  610. // States
  611. this._engine.enableEffect(this._drawWrapper);
  612. this._engine.setState(false);
  613. this._engine.setDepthBuffer(false);
  614. this._engine.setDepthWrite(false);
  615. // Alpha
  616. this._engine.setAlphaMode(this.alphaMode);
  617. if (this.alphaConstants) {
  618. this.getEngine().setAlphaConstants(this.alphaConstants.r, this.alphaConstants.g, this.alphaConstants.b, this.alphaConstants.a);
  619. }
  620. // Bind the output texture of the preivous post process as the input to this post process.
  621. let source;
  622. if (this._shareOutputWithPostProcess) {
  623. source = this._shareOutputWithPostProcess.inputTexture;
  624. }
  625. else if (this._forcedOutputTexture) {
  626. source = this._forcedOutputTexture;
  627. }
  628. else {
  629. source = this.inputTexture;
  630. }
  631. if (!this.externalTextureSamplerBinding) {
  632. this._drawWrapper.effect._bindTexture("textureSampler", source?.texture);
  633. }
  634. // Parameters
  635. this._drawWrapper.effect.setVector2("scale", this._scaleRatio);
  636. this.onApplyObservable.notifyObservers(this._drawWrapper.effect);
  637. PostProcess._GetShaderCodeProcessing(this.name)?.bindCustomBindings?.(this.name, this._drawWrapper.effect);
  638. return this._drawWrapper.effect;
  639. }
  640. _disposeTextures() {
  641. if (this._shareOutputWithPostProcess || this._forcedOutputTexture) {
  642. this._disposeTextureCache();
  643. return;
  644. }
  645. this._disposeTextureCache();
  646. this._textures.dispose();
  647. }
  648. _disposeTextureCache() {
  649. for (let i = this._textureCache.length - 1; i >= 0; i--) {
  650. this._textureCache[i].texture.dispose();
  651. }
  652. this._textureCache.length = 0;
  653. }
  654. /**
  655. * Sets the required values to the prepass renderer.
  656. * @param prePassRenderer defines the prepass renderer to setup.
  657. * @returns true if the pre pass is needed.
  658. */
  659. setPrePassRenderer(prePassRenderer) {
  660. if (this._prePassEffectConfiguration) {
  661. this._prePassEffectConfiguration = prePassRenderer.addEffectConfiguration(this._prePassEffectConfiguration);
  662. this._prePassEffectConfiguration.enabled = true;
  663. return true;
  664. }
  665. return false;
  666. }
  667. /**
  668. * Disposes the post process.
  669. * @param camera The camera to dispose the post process on.
  670. */
  671. dispose(camera) {
  672. camera = camera || this._camera;
  673. this._disposeTextures();
  674. let index;
  675. if (this._scene) {
  676. index = this._scene.postProcesses.indexOf(this);
  677. if (index !== -1) {
  678. this._scene.postProcesses.splice(index, 1);
  679. }
  680. }
  681. if (this._parentContainer) {
  682. const index = this._parentContainer.postProcesses.indexOf(this);
  683. if (index > -1) {
  684. this._parentContainer.postProcesses.splice(index, 1);
  685. }
  686. this._parentContainer = null;
  687. }
  688. index = this._engine.postProcesses.indexOf(this);
  689. if (index !== -1) {
  690. this._engine.postProcesses.splice(index, 1);
  691. }
  692. if (!camera) {
  693. return;
  694. }
  695. camera.detachPostProcess(this);
  696. index = camera._postProcesses.indexOf(this);
  697. if (index === 0 && camera._postProcesses.length > 0) {
  698. const firstPostProcess = this._camera._getFirstPostProcess();
  699. if (firstPostProcess) {
  700. firstPostProcess.markTextureDirty();
  701. }
  702. }
  703. this.onActivateObservable.clear();
  704. this.onAfterRenderObservable.clear();
  705. this.onApplyObservable.clear();
  706. this.onBeforeRenderObservable.clear();
  707. this.onSizeChangedObservable.clear();
  708. }
  709. /**
  710. * Serializes the post process to a JSON object
  711. * @returns the JSON object
  712. */
  713. serialize() {
  714. const serializationObject = SerializationHelper.Serialize(this);
  715. const camera = this.getCamera() || (this._scene && this._scene.activeCamera);
  716. serializationObject.customType = "BABYLON." + this.getClassName();
  717. serializationObject.cameraId = camera ? camera.id : null;
  718. serializationObject.reusable = this._reusable;
  719. serializationObject.textureType = this._textureType;
  720. serializationObject.fragmentUrl = this._fragmentUrl;
  721. serializationObject.parameters = this._parameters;
  722. serializationObject.samplers = this._samplers;
  723. serializationObject.options = this._options;
  724. serializationObject.defines = this._postProcessDefines;
  725. serializationObject.textureFormat = this._textureFormat;
  726. serializationObject.vertexUrl = this._vertexUrl;
  727. serializationObject.indexParameters = this._indexParameters;
  728. return serializationObject;
  729. }
  730. /**
  731. * Clones this post process
  732. * @returns a new post process similar to this one
  733. */
  734. clone() {
  735. const serializationObject = this.serialize();
  736. serializationObject._engine = this._engine;
  737. serializationObject.cameraId = null;
  738. const result = PostProcess.Parse(serializationObject, this._scene, "");
  739. if (!result) {
  740. return null;
  741. }
  742. result.onActivateObservable = this.onActivateObservable.clone();
  743. result.onSizeChangedObservable = this.onSizeChangedObservable.clone();
  744. result.onApplyObservable = this.onApplyObservable.clone();
  745. result.onBeforeRenderObservable = this.onBeforeRenderObservable.clone();
  746. result.onAfterRenderObservable = this.onAfterRenderObservable.clone();
  747. result._prePassEffectConfiguration = this._prePassEffectConfiguration;
  748. return result;
  749. }
  750. /**
  751. * Creates a material from parsed material data
  752. * @param parsedPostProcess defines parsed post process data
  753. * @param scene defines the hosting scene
  754. * @param rootUrl defines the root URL to use to load textures
  755. * @returns a new post process
  756. */
  757. static Parse(parsedPostProcess, scene, rootUrl) {
  758. const postProcessType = GetClass(parsedPostProcess.customType);
  759. if (!postProcessType || !postProcessType._Parse) {
  760. return null;
  761. }
  762. const camera = scene ? scene.getCameraById(parsedPostProcess.cameraId) : null;
  763. return postProcessType._Parse(parsedPostProcess, camera, scene, rootUrl);
  764. }
  765. /**
  766. * @internal
  767. */
  768. static _Parse(parsedPostProcess, targetCamera, scene, rootUrl) {
  769. return SerializationHelper.Parse(() => {
  770. return new PostProcess(parsedPostProcess.name, parsedPostProcess.fragmentUrl, parsedPostProcess.parameters, parsedPostProcess.samplers, parsedPostProcess.options, targetCamera, parsedPostProcess.renderTargetSamplingMode, parsedPostProcess._engine, parsedPostProcess.reusable, parsedPostProcess.defines, parsedPostProcess.textureType, parsedPostProcess.vertexUrl, parsedPostProcess.indexParameters, false, parsedPostProcess.textureFormat);
  771. }, parsedPostProcess, scene, rootUrl);
  772. }
  773. }
  774. PostProcess._CustomShaderCodeProcessing = {};
  775. __decorate([
  776. serialize()
  777. ], PostProcess.prototype, "uniqueId", void 0);
  778. __decorate([
  779. serialize()
  780. ], PostProcess.prototype, "name", void 0);
  781. __decorate([
  782. serialize()
  783. ], PostProcess.prototype, "width", void 0);
  784. __decorate([
  785. serialize()
  786. ], PostProcess.prototype, "height", void 0);
  787. __decorate([
  788. serialize()
  789. ], PostProcess.prototype, "renderTargetSamplingMode", void 0);
  790. __decorate([
  791. serializeAsColor4()
  792. ], PostProcess.prototype, "clearColor", void 0);
  793. __decorate([
  794. serialize()
  795. ], PostProcess.prototype, "autoClear", void 0);
  796. __decorate([
  797. serialize()
  798. ], PostProcess.prototype, "forceAutoClearInAlphaMode", void 0);
  799. __decorate([
  800. serialize()
  801. ], PostProcess.prototype, "alphaMode", void 0);
  802. __decorate([
  803. serialize()
  804. ], PostProcess.prototype, "alphaConstants", void 0);
  805. __decorate([
  806. serialize()
  807. ], PostProcess.prototype, "enablePixelPerfectMode", void 0);
  808. __decorate([
  809. serialize()
  810. ], PostProcess.prototype, "forceFullscreenViewport", void 0);
  811. __decorate([
  812. serialize()
  813. ], PostProcess.prototype, "scaleMode", void 0);
  814. __decorate([
  815. serialize()
  816. ], PostProcess.prototype, "alwaysForcePOT", void 0);
  817. __decorate([
  818. serialize("samples")
  819. ], PostProcess.prototype, "_samples", void 0);
  820. __decorate([
  821. serialize()
  822. ], PostProcess.prototype, "adaptScaleToCurrentViewport", void 0);
  823. RegisterClass("BABYLON.PostProcess", PostProcess);
  824. //# sourceMappingURL=postProcess.js.map