[Solved] Enabling gamma breaks GUI colors


There is a need to use gamma in the application, and gamma value should be set up by user, so it is not always 2.2. Application uses Lemur. Following the documentation the next 3 steps were made:

1. Enable Gamma Correction global app setting.
2. Disable rendered output correction : renderer.setMainFrameBufferSrgb(false); (for example in the simpleInit method of your SimpleApplication).
3. Use the GammaCorrectionFilter in a FilterPostProcessor, and set the proper gamma value on it (default is 2.2).

Gamma correction works fine with the exception of GUI: textures in GUI became with linear colors, and because of this, are very dark :frowning: Reason for this is because FilterPostProcessor is not run for guiViewPort and renderer does not use linear to sRGB conversion, because we disabled it with renderer.setMainFrameBufferSrgb(false);.

I tried to create own FilterPostProcessor for guiViewPort using such code:

FilterPostProcessor guiFilterPostProcessor = new FilterPostProcessor(assetManager);
guiFilterPostProcessor.addFilter(new GammaCorrectionFilter(1.8f));

It applies correct gamma correction for textures in GUI, but unfortunately main viewport is fully black. Looks like it is impossible to use FilterPostProcessor for multiple viewports. Or is there a way?

There is a logical idea to make such ugly code:


which fixes gamma for GUI textures, but code smells and GUI gets all the post processor filters, not just GammaCorrectionFilter

Besides, if GammaCorrectionFilter is run for GUI, texture color looks correct, but text color becomes brighter. It can be fixed with changing text color using new ColorRGBA().setAsSrgb(), but such change should be done everywhere for GUI text, and it is not very convenient to use it.

Is it possible to set up gamma correction such way, so it is applied for main viewport, but does not break colors for textures and text in GUI? Maybe is it possible to work with GUI like it was before enabling gamma? Any advice is welcome.

I’m not sure why you do all this… Just enabling the checkbox in the settings windows does everything that you need.
Also the GammaCorrection filrter is kind of deprecated… We had this before proper gamma correction implementation

I use filter, because it allows to change gamma value. Just enabled checkbox uses some fixed gamma curve. Or is it possible to change the value?

Or maybe I follow the misleading path, thinking that it is cool to allow user to set up a gamma in the settings.

Yes it’s a fixed gamma curve, but it’s applied consistently, and almost for free.
stacking filter post processors in the main viewport and the gui viewport will not work indeed.
A mid ground solution if you want extra gamma correction could be to enable it, then add the gamma correction filter on the main viewport with a low value (0.2, -0.2… i don’t even knwo if negative values woudl work).
This way you could tweak the gamma correction for the main viewport and still have the fixed curve gamma correction for the GUI…
Seems a bit hackish to me though…
Is that so important? I mean, can’t you just keep the fixed curve correction? Because over correcting will give you wrong results anyway.

1 Like

Additional small correction only for the main viewport! I like such approach.
It should work, because (c^a)^b = c^(a * b). Just not 0.2, -0.2, but 0.8, 1.2. Thank you, @nehon!

I would like to give a user a way to control brightness, so some very dark areas won’t look very dark. Gamma will be used 2.2 by default, and probably dark areas will look bright enough on most monitors. But maybe some monitors will show picture very dark, and user will want to increase brightness a little bit. That is why I want to allow user to change gamma. I know, that gamma and brightness are different things. But gamma will mostly affect dark colors and will not make very bright colors become white, like real brightness would do.

Ha yes :smiley:
Well I’m interested in your findings if it works as you want in the end :wink:

So, following the @nehon’s idea, gamma correction for the application was set up this way:

  1. Gamma Correction global app settings was enabled.
  2. renderer.setMainFrameBufferSrgb(false) is not called directly (it is called inside JME with true value, because gamma is enabled).
  3. GammaCorrectionFilter is used for the main viewport. Gamma value passed to this filter is an additional correction for only main viewport. 1.0f means, that no additional correction is used, and final gamma will be 2.2. 0.8f means, that gamma will be 2.2 * 0.8 = 1.76.

Main viewport uses gamma 2.2, which is additionally corrected with the filter. Picture in the main viewport looks as expected.

GUI viewport always uses gamma 2.2. Textures colors in GUI look correct (sRGB, not linear), which is good.

Text colors in GUI look brighter, than the given color. Reason for this is because text colors are passed in linear format. For example, in Lemur: color = color(0.5f, 0.5f, 0.5f, 1f) will show color (0.72f, 0.72f, 0.72f, 1f) in GUI. If to use color = new ColorRGBA().setAsSrgb(0.5f, 0.5f, 0.5f, 1f), then final GUI color will be (0.5f, 0.5f, 0.5f, 1f), which is correct. It is valid to use setAsSrgb for GUI colors, because GUI always uses gamma 2.2, and setAsSrgb uses 2.2 gamma value as well.

I am satisfied with the results. Thank you, @nehon I will mark thread solved.


Cool, thanks for the summary :wink: