imageProcessingPostProcess.ts 14 KB

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