Lookup table post processing effect

Use lookup table textures to apply color transforms in a shader.

December 26, 2017 - 2 minute read -
opengl cpp mac

I have been working on a personal rendering engine these days. Find the previous tease here. This demo is for LUT post processing effect test. Here I used some lookup tables from Unreal Engine to apply color transforms in a shader.

clut preview

Spec

3D textures are used, and the size of lookup table is (16x16)x16.

clut default rgb

channels:

clut default r

clut default g

clut default b

Shader

Apply the lookup table after the final combination of all the other passes. Since the output value of ‘combine pass’ is followed by an tone mapping processing, none of the RGB values will reach 1.0. And it is better to use some precomputed numbers. (etc. 0.03125 is 1/32)

out vec4 lutPass;
in vec2 uv;

uniform sampler2D combinePass;
uniform sampler2D lut;

void main() {
	vec3 combineColor = texture(combinePass, uv).rgb;

	float blueIndex = floor(combineColor.b * 16.0); // 0 ~ 15

	vec2 uvMapping;
	uvMapping.x = (blueIndex + 0.03125 + 0.9375 * combineColor.r) * 0.0625;
	uvMapping.y = 0.03125 + 0.9375 * combineColor.g;

	lutPass = vec4(texture(lut, uvMapping).rgb, 1);
}

And mipmap mode needed to be disabled for a lookup table image. Use GL_LINEAR for a better looking.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

Demo

For test, Here is a LutController Component attached to gameControl GameObject, which can switch lookup tables by pushing left or right key.

Assets

Bibliography