Gamma Correction in 3D graphics means that the lighting is calculated in more correct way.
The idea is like this: The images you prepare on your computer, and photos you take with your camera, have colors automatically adjusted to look good on a typical monitor. Using these colors directly for the lighting calculation (e.g. as diffuse or base colors) is not entirely correct.
Using gamma correction means that the graphic engine (like Castle Game Engine):
Adjusts the values from the color images
(like textures you provide to X3D
into more correct values, before using them in any calculations.
Calculates the lighting using the correct color values.
At the end, applies the gamma to the final pixel color, to make it look good on your monitor.
In Castle Game Engine you control this using a simple global variable GammaCorrection.
By default it is
which means that gamma correction is performed for materials using PBR (Physically Based Rendering) equations.
This includes standard materials defined in glTF models, and materials defined in X3D 4.0
So we assume that you prepare PBR
materials and their textures taking gamma into account.
You can change it to
gcNone to never do gamma correction.
You can change it to
gcAlways to always do gamma correction.
Another way of explaining it is by looking at the material node (used by each X3D
PhysicalMaterial: Gamma correction is used if
This material type is the standard glTF material type.
You can also use it explicitly by
PhysicalMaterial X3Dv4 node.
Gamma correction is used if
Material is the standard X3D 3.x material type,
practically used by all existing X3D exporters right now.
What you should do?
In most cases, the default does what you expect.
If you use glTF models with PBR, then gammma correction is used.
Otherwise gamma correction is not used. So unlit materials (e.g. with cartoon rendering) have no gamma correction. Older models with Phong lighting have no gamma correction.
If you want gamma correction always then turn
GammaCorrection := gcAlways.
Make sure you prepare your assets (textures) accordingly.
This is also 100% compatible with glTF (that dictates one should use gamma correction always,
for both PBR and unlit materials).
If you want maximum speed, set
GammaCorrection := gcNone.
If you want maximum speed, you should also consider using Phong lighting
(maybe even with Gouraud shading) instead of PBR.
Material instead of
PhysicalMaterial X3D nodes.
And of course use
UnlitMaterial for unrealistic rendering.
But these decisions are independent of the gamma correction, that in principle
makes sense with any lighting model (even unlit).
Note: Right now, gamma correction is only applied to things rendered using
It is not applied to other things.
In particular user-interface elements (like
TCastleImageControl) or low-level 2D APIs (like
TSprite) do not use gamma correction, ever.
Tone Mapping is another feature you can use to adjust the colors of your scene. Contrary to gamma correction, tone mapping is not about realism. It's just about changing all colors using some visually-pleasing equation.
While tone mapping is technically independent of the gamma correction (using one of them is unrelated to using another) the decision about using them is often considered at the same time, as they both affect the general "look of colors" on your scene. That is also why we document tone mapping here.
Simply set the ToneMapping global variable to apply tone mapping to everything you render.
(Just as with gamma correction, it is applied to
It doesn't affect UI rendering.)
Do you want to implement your own color-changing operation? The ToneMapping variable in CGE allows to only choose from built-in operators. But you can trivially define your own color processing using our compositing shaders, by using PLUG_fog_apply to process your colors using GLSL code. See tone_mapping.x3dv (you may want to download it with complete demo models) for demo. You can also use screen effects to apply post-processing in screen space.
Copyright Michalis Kamburelis and other Castle Game Engine developers.
Thank you to Paweł Wojciechowicz from Cat-astrophe Games for various graphics.
This documentation is also open-source and you can even redistribute it on open-source terms.