[UPBGE] HDR Eye Adaption Filter

I decided to make a quick eye adaption filter using my knowledge of how it works. It turned out to work very well. It is using the latest UPBGE features which include full HDR(to prevent color banding) aswell as mipmaps for efficient average brightness detection.

In case if you see big exprossure diferences at diferent parts of screen, you may increase the mip map level and hope that it won’t mess it up at lower resolutions. Feel free to play around with it:)

DOWNLOAD: http://pasteall.org/blend/index.php?id=45930

Textures from textures.com

The code:


uniform sampler2D bgl_RenderedTexture;
uniform sampler2D bgl_LuminanceTexture;
uniform float bgl_RenderedTextureWidth;
uniform float bgl_RenderedTextureHeight;


float prevmult = 1.0;


void main() {


    float exposure = 1.0; // Overall brightness
    float maxMult = 2.5; // Maximum enchantion
    float minMult = 0.5; // Maximum absorbtion
    float lumInfluence = 1.0; // How much eyes adapt to light
    float smoothness = 0.4; // Makes eye adaption smoother


    float level = 9.0;
    vec4 mipcolor = textureLod(bgl_RenderedTexture, gl_TexCoord[0].st, level);
    float lum = mipcolor.r * 0.3 + mipcolor.g * 0.6 + mipcolor.b * 0.1;    
    
    float mult = exposure / max(lum, 0.001);
    mult = clamp(mult, minMult, maxMult);
    mult = mix(exposure, mult, lumInfluence);
    
    mult = mix(mult, prevmult, smoothness);
    
    gl_FragColor = texture(bgl_RenderedTexture, gl_TexCoord[0].st) * mult;
    
    prevmult = mult;
    
}

SETUP:

Requirements:
>UPBGE 0.1.4 or newer

Settings:
>Render properties
~System
*HDR - full (this will prevent color banding)

Logic:
[Always (false pulse triggering)] --> [And] --> [Filter 2D (Custom Filter | Enable Mipmaps)]

Those are the most important settings.

With this filter you can now realistically alter brightness of different lights(e.g. you make sun 10x brighter than bulb, but if you’re in basement with bulb only, it seems almost as light as sun. This way you can realistically represent transitions between different brightness environments(outdoor -> indoor; day -> night; etc.

SOME TIPS:
>If your scene gets too dark or too bright(overally not occasionally), than I suggest adjusting exposure value till it fits your needs.
>If scene is too dark or too bright at certain places(e.g. too dark when entering basement from street) adjust the minMult and maxMult. The smaller minMult and bigger maxMult, the wider dynamic range will be aviable. However, don’t exceed this unless you want the scene to become bright grey when looking at a black wall :wink:
>Avoid using directional lights with intensity above 1. Instead reduce intensity of other lights proportionally. Think of 1.0 being max cap of brightness, the brightest thing you could get. You can, for example, use 1.0 for sun and lower values for other lamps. If you want a bright point/spot light, however, you can use higher values(yet not reccomended).

That looks very nice. It would be very cool if you could make the transition slower, kind of like how a child object moves with a parent offset.

Edit: whoops :stuck_out_tongue: I didn’t see the smoothness variable in the code. Sorry

Yea… The smoothness must be improved yet, though, as it doesn’t feel right yet. Great that you like it:)

P.S. Added the shader code to the main post :wink:

Its nice. You could use a py component for the settings. :slight_smile:

I guess I gotta learn them first:D

P.S. Updated the download link with a bit more realistically tweaked scene. Also added another light source to show off how you can simply transit between 2 brightness environments realistically. :wink:

Nice Filter.

So far I can see the smoothness variable control only the influence. I also miss a adption speed variable that control the delay of the transition.

Smoothness is supposed to be the adaption speed delayer(for smooth adaption). However, it is not working quite correctly yet. I must study this deeper.