Level adjustment (custom shader)

From C4 Engine Wiki

Jump to: navigation, search

Level Adjustment (custom shader)

Contents

This shader lets you filter a texture in much the same way the Photoshop Levels tool works. The main use for this, aside from anything else you might think of, would be to have a script controlling the constants, creating things that are supposed to flash, flicker or otherwise change intensity in some way. Doing this with gamma control will look different from doing it with straight emission values.

It takes three input values (plus the image to be filtered):

γ - input gamma
b - black output
w - white output

Input black and input white are not implemented in this version.

The Maths

The general idea is do perform the following sequence of operations for each color channel:

Input = I = image data
O1 = Iγ
O2 = max ( O1, b )
O3 = min ( O2, w )
Output = O3

The Graph


Click to access full resolution version.


The graph is easier to read and build if you consider the fact that the same Power---->Maximum---->Minimum group repeats three times, once for each channel, and the three constants, γ, b and w, are always connected to the same respective node in each such group. So, just build the red channel set first, then copy those nodes twice and connect. Thus, you can easily add support for the alpha channel as well if you need it (just add a fourth group connected to the a-component of the image, and replace the Merge3D with a Merge4D at the end where everything converges).

Examples from the shader tests

Straight curve = original image unchanged
Straight curve = original image unchanged
Input gamma > 1.0 = generally darker, higher contrast
Input gamma > 1.0 = generally darker, higher contrast
Input gamma < 1.0 = brighter midtones
Input gamma < 1.0 = brighter midtones
Insane gamma, output clamped between 0.2 - 0.8  = ugly
Insane gamma, output clamped between 0.2 - 0.8 = ugly
Personal tools