babylon.imageProcessingPostProcess.ts 14 KB

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