imageProcessingPostProcess.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import { Nullable } from "../types";
  2. import { Observer } from "../Misc/observable";
  3. import { serialize } from "../Misc/decorators";
  4. import { Color4 } from "../Maths/math";
  5. import { Camera } from "../Cameras/camera";
  6. import { BaseTexture } from "../Materials/Textures/baseTexture";
  7. import { Effect } from "../Materials/effect";
  8. import { ColorCurves } from "../Materials/colorCurves";
  9. import { ImageProcessingConfiguration, IImageProcessingConfigurationDefines } from "../Materials/imageProcessingConfiguration";
  10. import { PostProcess, PostProcessOptions } from "./postProcess";
  11. import { Engine } from "../Engines/engine";
  12. import { EngineStore } from "../Engines/engineStore";
  13. import { Scene } from "../scene";
  14. import { Constants } from "../Engines/constants";
  15. import "../Shaders/imageProcessing.fragment";
  16. import "../Shaders/postprocess.vertex";
  17. /**
  18. * ImageProcessingPostProcess
  19. * @see https://doc.babylonjs.com/how_to/how_to_use_postprocesses#imageprocessing
  20. */
  21. export class ImageProcessingPostProcess extends PostProcess {
  22. /**
  23. * Default configuration related to image processing available in the PBR Material.
  24. */
  25. protected _imageProcessingConfiguration: ImageProcessingConfiguration;
  26. /**
  27. * Gets the image processing configuration used either in this material.
  28. */
  29. public get imageProcessingConfiguration(): ImageProcessingConfiguration {
  30. return this._imageProcessingConfiguration;
  31. }
  32. /**
  33. * Sets the Default image processing configuration used either in the this material.
  34. *
  35. * If sets to null, the scene one is in use.
  36. */
  37. public set imageProcessingConfiguration(value: ImageProcessingConfiguration) {
  38. this._attachImageProcessingConfiguration(value);
  39. }
  40. /**
  41. * Keep track of the image processing observer to allow dispose and replace.
  42. */
  43. private _imageProcessingObserver: Nullable<Observer<ImageProcessingConfiguration>>;
  44. /**
  45. * Attaches a new image processing configuration to the PBR Material.
  46. * @param configuration
  47. */
  48. protected _attachImageProcessingConfiguration(configuration: Nullable<ImageProcessingConfiguration>, doNotBuild = false): void {
  49. if (configuration === this._imageProcessingConfiguration) {
  50. return;
  51. }
  52. // Detaches observer.
  53. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  54. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  55. }
  56. // Pick the scene configuration if needed.
  57. if (!configuration) {
  58. var scene = null;
  59. var engine = this.getEngine();
  60. var camera = this.getCamera();
  61. if (camera) {
  62. scene = camera.getScene();
  63. }
  64. else if (engine && engine.scenes) {
  65. var scenes = engine.scenes;
  66. scene = scenes[scenes.length - 1];
  67. }
  68. else {
  69. scene = EngineStore.LastCreatedScene;
  70. }
  71. this._imageProcessingConfiguration = (<Scene>scene).imageProcessingConfiguration;
  72. }
  73. else {
  74. this._imageProcessingConfiguration = configuration;
  75. }
  76. // Attaches observer.
  77. if (this._imageProcessingConfiguration) {
  78. this._imageProcessingObserver = this._imageProcessingConfiguration.onUpdateParameters.add(() => {
  79. this._updateParameters();
  80. });
  81. }
  82. // Ensure the effect will be rebuilt.
  83. if (!doNotBuild) {
  84. this._updateParameters();
  85. }
  86. }
  87. /**
  88. * Gets Color curves setup used in the effect if colorCurvesEnabled is set to true .
  89. */
  90. public get colorCurves(): Nullable<ColorCurves> {
  91. return this.imageProcessingConfiguration.colorCurves;
  92. }
  93. /**
  94. * Sets Color curves setup used in the effect if colorCurvesEnabled is set to true .
  95. */
  96. public set colorCurves(value: Nullable<ColorCurves>) {
  97. this.imageProcessingConfiguration.colorCurves = value;
  98. }
  99. /**
  100. * Gets wether the color curves effect is enabled.
  101. */
  102. public get colorCurvesEnabled(): boolean {
  103. return this.imageProcessingConfiguration.colorCurvesEnabled;
  104. }
  105. /**
  106. * Sets wether the color curves effect is enabled.
  107. */
  108. public set colorCurvesEnabled(value: boolean) {
  109. this.imageProcessingConfiguration.colorCurvesEnabled = value;
  110. }
  111. /**
  112. * Gets Color grading LUT texture used in the effect if colorGradingEnabled is set to true.
  113. */
  114. public get colorGradingTexture(): Nullable<BaseTexture> {
  115. return this.imageProcessingConfiguration.colorGradingTexture;
  116. }
  117. /**
  118. * Sets Color grading LUT texture used in the effect if colorGradingEnabled is set to true.
  119. */
  120. public set colorGradingTexture(value: Nullable<BaseTexture>) {
  121. this.imageProcessingConfiguration.colorGradingTexture = value;
  122. }
  123. /**
  124. * Gets wether the color grading effect is enabled.
  125. */
  126. public get colorGradingEnabled(): boolean {
  127. return this.imageProcessingConfiguration.colorGradingEnabled;
  128. }
  129. /**
  130. * Gets wether the color grading effect is enabled.
  131. */
  132. public set colorGradingEnabled(value: boolean) {
  133. this.imageProcessingConfiguration.colorGradingEnabled = value;
  134. }
  135. /**
  136. * Gets exposure used in the effect.
  137. */
  138. public get exposure(): number {
  139. return this.imageProcessingConfiguration.exposure;
  140. }
  141. /**
  142. * Sets exposure used in the effect.
  143. */
  144. public set exposure(value: number) {
  145. this.imageProcessingConfiguration.exposure = value;
  146. }
  147. /**
  148. * Gets wether tonemapping is enabled or not.
  149. */
  150. public get toneMappingEnabled(): boolean {
  151. return this._imageProcessingConfiguration.toneMappingEnabled;
  152. }
  153. /**
  154. * Sets wether tonemapping is enabled or not
  155. */
  156. public set toneMappingEnabled(value: boolean) {
  157. this._imageProcessingConfiguration.toneMappingEnabled = value;
  158. }
  159. /**
  160. * Gets the type of tone mapping effect.
  161. */
  162. public get toneMappingType(): number {
  163. return this._imageProcessingConfiguration.toneMappingType;
  164. }
  165. /**
  166. * Sets the type of tone mapping effect.
  167. */
  168. public set toneMappingType(value: number) {
  169. this._imageProcessingConfiguration.toneMappingType = value;
  170. }
  171. /**
  172. * Gets contrast used in the effect.
  173. */
  174. public get contrast(): number {
  175. return this.imageProcessingConfiguration.contrast;
  176. }
  177. /**
  178. * Sets contrast used in the effect.
  179. */
  180. public set contrast(value: number) {
  181. this.imageProcessingConfiguration.contrast = value;
  182. }
  183. /**
  184. * Gets Vignette stretch size.
  185. */
  186. public get vignetteStretch(): number {
  187. return this.imageProcessingConfiguration.vignetteStretch;
  188. }
  189. /**
  190. * Sets Vignette stretch size.
  191. */
  192. public set vignetteStretch(value: number) {
  193. this.imageProcessingConfiguration.vignetteStretch = value;
  194. }
  195. /**
  196. * Gets Vignette centre X Offset.
  197. */
  198. public get vignetteCentreX(): number {
  199. return this.imageProcessingConfiguration.vignetteCentreX;
  200. }
  201. /**
  202. * Sets Vignette centre X Offset.
  203. */
  204. public set vignetteCentreX(value: number) {
  205. this.imageProcessingConfiguration.vignetteCentreX = value;
  206. }
  207. /**
  208. * Gets Vignette centre Y Offset.
  209. */
  210. public get vignetteCentreY(): number {
  211. return this.imageProcessingConfiguration.vignetteCentreY;
  212. }
  213. /**
  214. * Sets Vignette centre Y Offset.
  215. */
  216. public set vignetteCentreY(value: number) {
  217. this.imageProcessingConfiguration.vignetteCentreY = value;
  218. }
  219. /**
  220. * Gets Vignette weight or intensity of the vignette effect.
  221. */
  222. public get vignetteWeight(): number {
  223. return this.imageProcessingConfiguration.vignetteWeight;
  224. }
  225. /**
  226. * Sets Vignette weight or intensity of the vignette effect.
  227. */
  228. public set vignetteWeight(value: number) {
  229. this.imageProcessingConfiguration.vignetteWeight = value;
  230. }
  231. /**
  232. * Gets Color of the vignette applied on the screen through the chosen blend mode (vignetteBlendMode)
  233. * if vignetteEnabled is set to true.
  234. */
  235. public get vignetteColor(): Color4 {
  236. return this.imageProcessingConfiguration.vignetteColor;
  237. }
  238. /**
  239. * Sets Color of the vignette applied on the screen through the chosen blend mode (vignetteBlendMode)
  240. * if vignetteEnabled is set to true.
  241. */
  242. public set vignetteColor(value: Color4) {
  243. this.imageProcessingConfiguration.vignetteColor = value;
  244. }
  245. /**
  246. * Gets Camera field of view used by the Vignette effect.
  247. */
  248. public get vignetteCameraFov(): number {
  249. return this.imageProcessingConfiguration.vignetteCameraFov;
  250. }
  251. /**
  252. * Sets Camera field of view used by the Vignette effect.
  253. */
  254. public set vignetteCameraFov(value: number) {
  255. this.imageProcessingConfiguration.vignetteCameraFov = value;
  256. }
  257. /**
  258. * Gets the vignette blend mode allowing different kind of effect.
  259. */
  260. public get vignetteBlendMode(): number {
  261. return this.imageProcessingConfiguration.vignetteBlendMode;
  262. }
  263. /**
  264. * Sets the vignette blend mode allowing different kind of effect.
  265. */
  266. public set vignetteBlendMode(value: number) {
  267. this.imageProcessingConfiguration.vignetteBlendMode = value;
  268. }
  269. /**
  270. * Gets wether the vignette effect is enabled.
  271. */
  272. public get vignetteEnabled(): boolean {
  273. return this.imageProcessingConfiguration.vignetteEnabled;
  274. }
  275. /**
  276. * Sets wether the vignette effect is enabled.
  277. */
  278. public set vignetteEnabled(value: boolean) {
  279. this.imageProcessingConfiguration.vignetteEnabled = value;
  280. }
  281. @serialize()
  282. private _fromLinearSpace = true;
  283. /**
  284. * Gets wether the input of the processing is in Gamma or Linear Space.
  285. */
  286. public get fromLinearSpace(): boolean {
  287. return this._fromLinearSpace;
  288. }
  289. /**
  290. * Sets wether the input of the processing is in Gamma or Linear Space.
  291. */
  292. public set fromLinearSpace(value: boolean) {
  293. if (this._fromLinearSpace === value) {
  294. return;
  295. }
  296. this._fromLinearSpace = value;
  297. this._updateParameters();
  298. }
  299. /**
  300. * Defines cache preventing GC.
  301. */
  302. private _defines: IImageProcessingConfigurationDefines & { FROMLINEARSPACE: boolean } = {
  303. IMAGEPROCESSING: false,
  304. VIGNETTE: false,
  305. VIGNETTEBLENDMODEMULTIPLY: false,
  306. VIGNETTEBLENDMODEOPAQUE: false,
  307. TONEMAPPING: false,
  308. TONEMAPPING_ACES: false,
  309. CONTRAST: false,
  310. COLORCURVES: false,
  311. COLORGRADING: false,
  312. COLORGRADING3D: false,
  313. FROMLINEARSPACE: false,
  314. SAMPLER3DGREENDEPTH: false,
  315. SAMPLER3DBGRMAP: false,
  316. IMAGEPROCESSINGPOSTPROCESS: false,
  317. EXPOSURE: false,
  318. };
  319. constructor(name: string, options: number | PostProcessOptions, camera: Nullable<Camera> = null, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT, imageProcessingConfiguration?: ImageProcessingConfiguration) {
  320. super(name, "imageProcessing", [], [], options, camera, samplingMode, engine, reusable,
  321. null, textureType, "postprocess", null, true);
  322. // Setup the configuration as forced by the constructor. This would then not force the
  323. // scene materials output in linear space and let untouched the default forward pass.
  324. if (imageProcessingConfiguration) {
  325. imageProcessingConfiguration.applyByPostProcess = true;
  326. this._attachImageProcessingConfiguration(imageProcessingConfiguration, true);
  327. // This will cause the shader to be compiled
  328. this.fromLinearSpace = false;
  329. }
  330. // Setup the default processing configuration to the scene.
  331. else {
  332. this._attachImageProcessingConfiguration(null, true);
  333. this.imageProcessingConfiguration.applyByPostProcess = true;
  334. }
  335. this.onApply = (effect: Effect) => {
  336. this.imageProcessingConfiguration.bind(effect, this.aspectRatio);
  337. };
  338. }
  339. /**
  340. * "ImageProcessingPostProcess"
  341. * @returns "ImageProcessingPostProcess"
  342. */
  343. public getClassName(): string {
  344. return "ImageProcessingPostProcess";
  345. }
  346. protected _updateParameters(): void {
  347. this._defines.FROMLINEARSPACE = this._fromLinearSpace;
  348. this.imageProcessingConfiguration.prepareDefines(this._defines, true);
  349. var defines = "";
  350. for (const define in this._defines) {
  351. if ((<any>this._defines)[define]) {
  352. defines += `#define ${define};\r\n`;
  353. }
  354. }
  355. var samplers = ["textureSampler"];
  356. var uniforms = ["scale"];
  357. if (ImageProcessingConfiguration) {
  358. ImageProcessingConfiguration.PrepareSamplers(samplers, this._defines);
  359. ImageProcessingConfiguration.PrepareUniforms(uniforms, this._defines);
  360. }
  361. this.updateEffect(defines, uniforms, samplers);
  362. }
  363. public dispose(camera?: Camera): void {
  364. super.dispose(camera);
  365. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  366. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  367. }
  368. if (this._imageProcessingConfiguration) {
  369. this.imageProcessingConfiguration.applyByPostProcess = false;
  370. }
  371. }
  372. }