babylon.imageProcessingPostProcess.ts 15 KB

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