imageProcessingConfiguration.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. import { serialize, Observable, Tools, SerializationHelper, serializeAsTexture, serializeAsColorCurves, serializeAsColor4 } from "Tools";
  2. import { Nullable } from "types";
  3. import { Color4 } from "Math";
  4. import { Effect, BaseTexture, IImageProcessingConfigurationDefines, ColorCurves, MaterialDefines } from "Materials";
  5. /**
  6. * Interface to follow in your material defines to integrate easily the
  7. * Image proccessing functions.
  8. * @hidden
  9. */
  10. export interface IImageProcessingConfigurationDefines {
  11. IMAGEPROCESSING: boolean;
  12. VIGNETTE: boolean;
  13. VIGNETTEBLENDMODEMULTIPLY: boolean;
  14. VIGNETTEBLENDMODEOPAQUE: boolean;
  15. TONEMAPPING: boolean;
  16. TONEMAPPING_ACES: boolean;
  17. CONTRAST: boolean;
  18. EXPOSURE: boolean;
  19. COLORCURVES: boolean;
  20. COLORGRADING: boolean;
  21. COLORGRADING3D: boolean;
  22. SAMPLER3DGREENDEPTH: boolean;
  23. SAMPLER3DBGRMAP: boolean;
  24. IMAGEPROCESSINGPOSTPROCESS: boolean;
  25. }
  26. /**
  27. * @hidden
  28. */
  29. export class ImageProcessingConfigurationDefines extends MaterialDefines implements IImageProcessingConfigurationDefines {
  30. public IMAGEPROCESSING = false;
  31. public VIGNETTE = false;
  32. public VIGNETTEBLENDMODEMULTIPLY = false;
  33. public VIGNETTEBLENDMODEOPAQUE = false;
  34. public TONEMAPPING = false;
  35. public TONEMAPPING_ACES = false;
  36. public CONTRAST = false;
  37. public COLORCURVES = false;
  38. public COLORGRADING = false;
  39. public COLORGRADING3D = false;
  40. public SAMPLER3DGREENDEPTH = false;
  41. public SAMPLER3DBGRMAP = false;
  42. public IMAGEPROCESSINGPOSTPROCESS = false;
  43. public EXPOSURE = false;
  44. constructor() {
  45. super();
  46. this.rebuild();
  47. }
  48. }
  49. /**
  50. * This groups together the common properties used for image processing either in direct forward pass
  51. * or through post processing effect depending on the use of the image processing pipeline in your scene
  52. * or not.
  53. */
  54. export class ImageProcessingConfiguration {
  55. /**
  56. * Default tone mapping applied in BabylonJS.
  57. */
  58. public static readonly TONEMAPPING_STANDARD = 0;
  59. /**
  60. * ACES Tone mapping (used by default in unreal and unity). This can help getting closer
  61. * to other engines rendering to increase portability.
  62. */
  63. public static readonly TONEMAPPING_ACES = 1;
  64. /**
  65. * Color curves setup used in the effect if colorCurvesEnabled is set to true
  66. */
  67. @serializeAsColorCurves()
  68. public colorCurves: Nullable<ColorCurves> = new ColorCurves();
  69. @serialize()
  70. private _colorCurvesEnabled = false;
  71. /**
  72. * Gets wether the color curves effect is enabled.
  73. */
  74. public get colorCurvesEnabled(): boolean {
  75. return this._colorCurvesEnabled;
  76. }
  77. /**
  78. * Sets wether the color curves effect is enabled.
  79. */
  80. public set colorCurvesEnabled(value: boolean) {
  81. if (this._colorCurvesEnabled === value) {
  82. return;
  83. }
  84. this._colorCurvesEnabled = value;
  85. this._updateParameters();
  86. }
  87. @serializeAsTexture("colorGradingTexture")
  88. private _colorGradingTexture: Nullable<BaseTexture>;
  89. /**
  90. * Color grading LUT texture used in the effect if colorGradingEnabled is set to true
  91. */
  92. public get colorGradingTexture(): Nullable<BaseTexture> {
  93. return this._colorGradingTexture;
  94. }
  95. /**
  96. * Color grading LUT texture used in the effect if colorGradingEnabled is set to true
  97. */
  98. public set colorGradingTexture(value: Nullable<BaseTexture>) {
  99. if (this._colorGradingTexture === value) {
  100. return;
  101. }
  102. this._colorGradingTexture = value;
  103. this._updateParameters();
  104. }
  105. @serialize()
  106. private _colorGradingEnabled = false;
  107. /**
  108. * Gets wether the color grading effect is enabled.
  109. */
  110. public get colorGradingEnabled(): boolean {
  111. return this._colorGradingEnabled;
  112. }
  113. /**
  114. * Sets wether the color grading effect is enabled.
  115. */
  116. public set colorGradingEnabled(value: boolean) {
  117. if (this._colorGradingEnabled === value) {
  118. return;
  119. }
  120. this._colorGradingEnabled = value;
  121. this._updateParameters();
  122. }
  123. @serialize()
  124. private _colorGradingWithGreenDepth = true;
  125. /**
  126. * Gets wether the color grading effect is using a green depth for the 3d Texture.
  127. */
  128. public get colorGradingWithGreenDepth(): boolean {
  129. return this._colorGradingWithGreenDepth;
  130. }
  131. /**
  132. * Sets wether the color grading effect is using a green depth for the 3d Texture.
  133. */
  134. public set colorGradingWithGreenDepth(value: boolean) {
  135. if (this._colorGradingWithGreenDepth === value) {
  136. return;
  137. }
  138. this._colorGradingWithGreenDepth = value;
  139. this._updateParameters();
  140. }
  141. @serialize()
  142. private _colorGradingBGR = true;
  143. /**
  144. * Gets wether the color grading texture contains BGR values.
  145. */
  146. public get colorGradingBGR(): boolean {
  147. return this._colorGradingBGR;
  148. }
  149. /**
  150. * Sets wether the color grading texture contains BGR values.
  151. */
  152. public set colorGradingBGR(value: boolean) {
  153. if (this._colorGradingBGR === value) {
  154. return;
  155. }
  156. this._colorGradingBGR = value;
  157. this._updateParameters();
  158. }
  159. /** @hidden */
  160. @serialize()
  161. public _exposure = 1.0;
  162. /**
  163. * Gets the Exposure used in the effect.
  164. */
  165. public get exposure(): number {
  166. return this._exposure;
  167. }
  168. /**
  169. * Sets the Exposure used in the effect.
  170. */
  171. public set exposure(value: number) {
  172. if (this._exposure === value) {
  173. return;
  174. }
  175. this._exposure = value;
  176. this._updateParameters();
  177. }
  178. @serialize()
  179. private _toneMappingEnabled = false;
  180. /**
  181. * Gets wether the tone mapping effect is enabled.
  182. */
  183. public get toneMappingEnabled(): boolean {
  184. return this._toneMappingEnabled;
  185. }
  186. /**
  187. * Sets wether the tone mapping effect is enabled.
  188. */
  189. public set toneMappingEnabled(value: boolean) {
  190. if (this._toneMappingEnabled === value) {
  191. return;
  192. }
  193. this._toneMappingEnabled = value;
  194. this._updateParameters();
  195. }
  196. @serialize()
  197. private _toneMappingType = ImageProcessingConfiguration.TONEMAPPING_STANDARD;
  198. /**
  199. * Gets the type of tone mapping effect.
  200. */
  201. public get toneMappingType(): number {
  202. return this._toneMappingType;
  203. }
  204. /**
  205. * Sets the type of tone mapping effect used in BabylonJS.
  206. */
  207. public set toneMappingType(value: number) {
  208. if (this._toneMappingType === value) {
  209. return;
  210. }
  211. this._toneMappingType = value;
  212. this._updateParameters();
  213. }
  214. @serialize()
  215. protected _contrast = 1.0;
  216. /**
  217. * Gets the contrast used in the effect.
  218. */
  219. public get contrast(): number {
  220. return this._contrast;
  221. }
  222. /**
  223. * Sets the contrast used in the effect.
  224. */
  225. public set contrast(value: number) {
  226. if (this._contrast === value) {
  227. return;
  228. }
  229. this._contrast = value;
  230. this._updateParameters();
  231. }
  232. /**
  233. * Vignette stretch size.
  234. */
  235. @serialize()
  236. public vignetteStretch = 0;
  237. /**
  238. * Vignette centre X Offset.
  239. */
  240. @serialize()
  241. public vignetteCentreX = 0;
  242. /**
  243. * Vignette centre Y Offset.
  244. */
  245. @serialize()
  246. public vignetteCentreY = 0;
  247. /**
  248. * Vignette weight or intensity of the vignette effect.
  249. */
  250. @serialize()
  251. public vignetteWeight = 1.5;
  252. /**
  253. * Color of the vignette applied on the screen through the chosen blend mode (vignetteBlendMode)
  254. * if vignetteEnabled is set to true.
  255. */
  256. @serializeAsColor4()
  257. public vignetteColor: Color4 = new Color4(0, 0, 0, 0);
  258. /**
  259. * Camera field of view used by the Vignette effect.
  260. */
  261. @serialize()
  262. public vignetteCameraFov = 0.5;
  263. @serialize()
  264. private _vignetteBlendMode = ImageProcessingConfiguration.VIGNETTEMODE_MULTIPLY;
  265. /**
  266. * Gets the vignette blend mode allowing different kind of effect.
  267. */
  268. public get vignetteBlendMode(): number {
  269. return this._vignetteBlendMode;
  270. }
  271. /**
  272. * Sets the vignette blend mode allowing different kind of effect.
  273. */
  274. public set vignetteBlendMode(value: number) {
  275. if (this._vignetteBlendMode === value) {
  276. return;
  277. }
  278. this._vignetteBlendMode = value;
  279. this._updateParameters();
  280. }
  281. @serialize()
  282. private _vignetteEnabled = false;
  283. /**
  284. * Gets wether the vignette effect is enabled.
  285. */
  286. public get vignetteEnabled(): boolean {
  287. return this._vignetteEnabled;
  288. }
  289. /**
  290. * Sets wether the vignette effect is enabled.
  291. */
  292. public set vignetteEnabled(value: boolean) {
  293. if (this._vignetteEnabled === value) {
  294. return;
  295. }
  296. this._vignetteEnabled = value;
  297. this._updateParameters();
  298. }
  299. @serialize()
  300. private _applyByPostProcess = false;
  301. /**
  302. * Gets wether the image processing is applied through a post process or not.
  303. */
  304. public get applyByPostProcess(): boolean {
  305. return this._applyByPostProcess;
  306. }
  307. /**
  308. * Sets wether the image processing is applied through a post process or not.
  309. */
  310. public set applyByPostProcess(value: boolean) {
  311. if (this._applyByPostProcess === value) {
  312. return;
  313. }
  314. this._applyByPostProcess = value;
  315. this._updateParameters();
  316. }
  317. @serialize()
  318. private _isEnabled = true;
  319. /**
  320. * Gets wether the image processing is enabled or not.
  321. */
  322. public get isEnabled(): boolean {
  323. return this._isEnabled;
  324. }
  325. /**
  326. * Sets wether the image processing is enabled or not.
  327. */
  328. public set isEnabled(value: boolean) {
  329. if (this._isEnabled === value) {
  330. return;
  331. }
  332. this._isEnabled = value;
  333. this._updateParameters();
  334. }
  335. /**
  336. * An event triggered when the configuration changes and requires Shader to Update some parameters.
  337. */
  338. public onUpdateParameters = new Observable<ImageProcessingConfiguration>();
  339. /**
  340. * Method called each time the image processing information changes requires to recompile the effect.
  341. */
  342. protected _updateParameters(): void {
  343. this.onUpdateParameters.notifyObservers(this);
  344. }
  345. /**
  346. * Gets the current class name.
  347. * @return "ImageProcessingConfiguration"
  348. */
  349. public getClassName(): string {
  350. return "ImageProcessingConfiguration";
  351. }
  352. /**
  353. * Prepare the list of uniforms associated with the Image Processing effects.
  354. * @param uniforms The list of uniforms used in the effect
  355. * @param defines the list of defines currently in use
  356. */
  357. public static PrepareUniforms(uniforms: string[], defines: IImageProcessingConfigurationDefines): void {
  358. if (defines.EXPOSURE) {
  359. uniforms.push("exposureLinear");
  360. }
  361. if (defines.CONTRAST) {
  362. uniforms.push("contrast");
  363. }
  364. if (defines.COLORGRADING) {
  365. uniforms.push("colorTransformSettings");
  366. }
  367. if (defines.VIGNETTE) {
  368. uniforms.push("vInverseScreenSize");
  369. uniforms.push("vignetteSettings1");
  370. uniforms.push("vignetteSettings2");
  371. }
  372. if (defines.COLORCURVES) {
  373. ColorCurves.PrepareUniforms(uniforms);
  374. }
  375. }
  376. /**
  377. * Prepare the list of samplers associated with the Image Processing effects.
  378. * @param samplersList The list of uniforms used in the effect
  379. * @param defines the list of defines currently in use
  380. */
  381. public static PrepareSamplers(samplersList: string[], defines: IImageProcessingConfigurationDefines): void {
  382. if (defines.COLORGRADING) {
  383. samplersList.push("txColorTransform");
  384. }
  385. }
  386. /**
  387. * Prepare the list of defines associated to the shader.
  388. * @param defines the list of defines to complete
  389. * @param forPostProcess Define if we are currently in post process mode or not
  390. */
  391. public prepareDefines(defines: IImageProcessingConfigurationDefines, forPostProcess: boolean = false): void {
  392. if (forPostProcess !== this.applyByPostProcess || !this._isEnabled) {
  393. defines.VIGNETTE = false;
  394. defines.TONEMAPPING = false;
  395. defines.TONEMAPPING_ACES = false;
  396. defines.CONTRAST = false;
  397. defines.EXPOSURE = false;
  398. defines.COLORCURVES = false;
  399. defines.COLORGRADING = false;
  400. defines.COLORGRADING3D = false;
  401. defines.IMAGEPROCESSING = false;
  402. defines.IMAGEPROCESSINGPOSTPROCESS = this.applyByPostProcess && this._isEnabled;
  403. return;
  404. }
  405. defines.VIGNETTE = this.vignetteEnabled;
  406. defines.VIGNETTEBLENDMODEMULTIPLY = (this.vignetteBlendMode === ImageProcessingConfiguration._VIGNETTEMODE_MULTIPLY);
  407. defines.VIGNETTEBLENDMODEOPAQUE = !defines.VIGNETTEBLENDMODEMULTIPLY;
  408. defines.TONEMAPPING = this.toneMappingEnabled;
  409. switch (this._toneMappingType) {
  410. case ImageProcessingConfiguration.TONEMAPPING_ACES:
  411. defines.TONEMAPPING_ACES = true;
  412. break;
  413. }
  414. defines.CONTRAST = (this.contrast !== 1.0);
  415. defines.EXPOSURE = (this.exposure !== 1.0);
  416. defines.COLORCURVES = (this.colorCurvesEnabled && !!this.colorCurves);
  417. defines.COLORGRADING = (this.colorGradingEnabled && !!this.colorGradingTexture);
  418. if (defines.COLORGRADING) {
  419. defines.COLORGRADING3D = this.colorGradingTexture!.is3D;
  420. } else {
  421. defines.COLORGRADING3D = false;
  422. }
  423. defines.SAMPLER3DGREENDEPTH = this.colorGradingWithGreenDepth;
  424. defines.SAMPLER3DBGRMAP = this.colorGradingBGR;
  425. defines.IMAGEPROCESSINGPOSTPROCESS = this.applyByPostProcess;
  426. defines.IMAGEPROCESSING = defines.VIGNETTE || defines.TONEMAPPING || defines.CONTRAST || defines.EXPOSURE || defines.COLORCURVES || defines.COLORGRADING;
  427. }
  428. /**
  429. * Returns true if all the image processing information are ready.
  430. * @returns True if ready, otherwise, false
  431. */
  432. public isReady() {
  433. // Color Grading texure can not be none blocking.
  434. return !this.colorGradingEnabled || !this.colorGradingTexture || this.colorGradingTexture.isReady();
  435. }
  436. /**
  437. * Binds the image processing to the shader.
  438. * @param effect The effect to bind to
  439. * @param aspectRatio Define the current aspect ratio of the effect
  440. */
  441. public bind(effect: Effect, aspectRatio = 1): void {
  442. // Color Curves
  443. if (this._colorCurvesEnabled && this.colorCurves) {
  444. ColorCurves.Bind(this.colorCurves, effect);
  445. }
  446. // Vignette
  447. if (this._vignetteEnabled) {
  448. var inverseWidth = 1 / effect.getEngine().getRenderWidth();
  449. var inverseHeight = 1 / effect.getEngine().getRenderHeight();
  450. effect.setFloat2("vInverseScreenSize", inverseWidth, inverseHeight);
  451. let vignetteScaleY = Math.tan(this.vignetteCameraFov * 0.5);
  452. let vignetteScaleX = vignetteScaleY * aspectRatio;
  453. let vignetteScaleGeometricMean = Math.sqrt(vignetteScaleX * vignetteScaleY);
  454. vignetteScaleX = Tools.Mix(vignetteScaleX, vignetteScaleGeometricMean, this.vignetteStretch);
  455. vignetteScaleY = Tools.Mix(vignetteScaleY, vignetteScaleGeometricMean, this.vignetteStretch);
  456. effect.setFloat4("vignetteSettings1", vignetteScaleX, vignetteScaleY, -vignetteScaleX * this.vignetteCentreX, -vignetteScaleY * this.vignetteCentreY);
  457. let vignettePower = -2.0 * this.vignetteWeight;
  458. effect.setFloat4("vignetteSettings2", this.vignetteColor.r, this.vignetteColor.g, this.vignetteColor.b, vignettePower);
  459. }
  460. // Exposure
  461. effect.setFloat("exposureLinear", this.exposure);
  462. // Contrast
  463. effect.setFloat("contrast", this.contrast);
  464. // Color transform settings
  465. if (this.colorGradingTexture) {
  466. effect.setTexture("txColorTransform", this.colorGradingTexture);
  467. let textureSize = this.colorGradingTexture.getSize().height;
  468. effect.setFloat4("colorTransformSettings",
  469. (textureSize - 1) / textureSize, // textureScale
  470. 0.5 / textureSize, // textureOffset
  471. textureSize, // textureSize
  472. this.colorGradingTexture.level // weight
  473. );
  474. }
  475. }
  476. /**
  477. * Clones the current image processing instance.
  478. * @return The cloned image processing
  479. */
  480. public clone(): ImageProcessingConfiguration {
  481. return SerializationHelper.Clone(() => new ImageProcessingConfiguration(), this);
  482. }
  483. /**
  484. * Serializes the current image processing instance to a json representation.
  485. * @return a JSON representation
  486. */
  487. public serialize(): any {
  488. return SerializationHelper.Serialize(this);
  489. }
  490. /**
  491. * Parses the image processing from a json representation.
  492. * @param source the JSON source to parse
  493. * @return The parsed image processing
  494. */
  495. public static Parse(source: any): ImageProcessingConfiguration {
  496. return SerializationHelper.Parse(() => new ImageProcessingConfiguration(), source, null, null);
  497. }
  498. // Static constants associated to the image processing.
  499. private static _VIGNETTEMODE_MULTIPLY = 0;
  500. private static _VIGNETTEMODE_OPAQUE = 1;
  501. /**
  502. * Used to apply the vignette as a mix with the pixel color.
  503. */
  504. public static get VIGNETTEMODE_MULTIPLY(): number {
  505. return this._VIGNETTEMODE_MULTIPLY;
  506. }
  507. /**
  508. * Used to apply the vignette as a replacement of the pixel color.
  509. */
  510. public static get VIGNETTEMODE_OPAQUE(): number {
  511. return this._VIGNETTEMODE_OPAQUE;
  512. }
  513. }