Log inGet started
World

Builtin Library

Branchmain
Branchesmainpublish
Commitcc6939bf11

README.md

colorGrading

The contrast and saturation a grade applies to light, stated once so @builtin::shaders.post.colorGrade, @builtin::shaders.post.color_correction and any grade a world writes share the same response.

Pure math, no bindings:

#include "@builtin::shaderModules.colorGrading"
FunctionWhat it does
zero_grade_contrast(light, contrast, pivot, white)Scales the colour's luminance in log space about pivot: a luminance pivot * 2^s becomes pivot * 2^(s * contrast). 1 leaves the colour, above 1 spreads the stops apart, below 1 draws them together. The pivot holds, every stop moves by the same share, the colour keeps each channel's share of its light (so its hue), and a colour carrying any light keeps some: black stays black and nothing else reaches it. white is where the picture's light peaks, or 0 for light with no peak; under a peak the luminance's headroom to white is graded in stops the same way, so white holds and nothing short of white reaches it, a colour whose strongest channel the new luminance would carry past white moves toward grey until that channel stands at white (keeping its luminance and hue), and light at or above white keeps its luminance.
zero_grade_saturation(light, saturation, white)Scales the colour's distance from the grey of its own luminance: 0 is that grey, 1 the colour as it was, above 1 more colour along the same hue, up to where its weakest channel reaches no light and, under a white above 0, its strongest channel reaches white. A colour already at the edge of the gamut (orange with no blue in it) keeps its chroma rather than turning red.
ZERO_GRADE_LUMAThe Rec. 709 luminance weights both use.

In a post-process effect in light, pivot contrast on zero_middle_grey(): the light the picture shows as middle grey at the exposure in force, so a grade holds middle grey where the picture shows it at every exposure and on every layer. Pass zero_picture_white() as the white: 0 on the scene's light, which has no peak, and the picture's white once the display transform has drawn it, so a grade after __display__ keeps the highlights short of white.

// @domain: post-process
#include "@builtin::shaderModules.colorGrading"

fn fragment(in: PostInput) -> vec4<f32> {
    let light = zero_sample_scene(in.uv);
    let white = zero_picture_white();
    let graded = zero_grade_contrast(light.rgb, material.contrast, zero_middle_grey(), white);
    return vec4<f32>(zero_grade_saturation(graded, material.saturation, white), light.a);
}