imageProcessingPostProcess.ts 15 KB

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