imageProcessingConfiguration.ts 19 KB

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