Skip to content

Instantly share code, notes, and snippets.

@shakesoda
Created October 26, 2014 23:57
Show Gist options
  • Save shakesoda/efc5fca130675a2a2b0e to your computer and use it in GitHub Desktop.
Save shakesoda/efc5fca130675a2a2b0e to your computer and use it in GitHub Desktop.
color correction shader (gutted out of a larger post processing shader)
// For textureSize2D, etc.
#extension GL_EXT_gpu_shader4 : enable
uniform int u_color_correct = 0;
// Sort of a hack: LOVE doesn't expose 3D textures, so we have to sample them
// as 2D and interpolate the samples ourselves.
uniform sampler2D u_lut_primary;
uniform sampler2D u_lut_secondary;
uniform float u_factor = 0.0;
// Thanks Google.
vec4 _texture3D(sampler2D tex, vec3 texCoord, float size) {
float sliceSize = 1.0 / size; // space of 1 slice
float slicePixelSize = sliceSize / size; // space of 1 pixel
float sliceInnerSize = slicePixelSize * (size - 1.0); // space of size pixels
float zSlice0 = min(floor(texCoord.z * size), size - 1.0);
float zSlice1 = min(zSlice0 + 1.0, size - 1.0);
float xOffset = slicePixelSize * 0.5 + texCoord.x * sliceInnerSize;
float s0 = xOffset + (zSlice0 * sliceSize);
float s1 = xOffset + (zSlice1 * sliceSize);
vec4 slice0Color = texture2D(tex, vec2(s0, texCoord.y));
vec4 slice1Color = texture2D(tex, vec2(s1, texCoord.y));
float zOffset = mod(texCoord.z * size, 1.0);
return mix(slice0Color, slice1Color, zOffset);
}
vec3 ColorCorrect(in vec3 raw_color, in vec2 coords) {
float threshold = 1.0/255.0;
vec4 color;
float lut_size = textureSize2D(u_lut_primary, 0).y;
// correct for clamping from the texture lookup, from GPU Gems 2 ch24
// http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter24.html
float scale = (lut_size - 1.0) / lut_size;
float offset = 1.0 / (lut_size * 2.0);
raw_color *= scale;
raw_color += offset;
if (u_factor <= threshold) {
color = _texture3D(u_lut_primary, raw_color.rgb, lut_size);
} else if (u_factor >= 1.0 - threshold) {
color = _texture3D(u_lut_secondary, raw_color.rgb, lut_size);
} else {
color = mix(
_texture3D(u_lut_primary, raw_color.rgb, lut_size),
_texture3D(u_lut_secondary, raw_color.rgb, lut_size),
u_factor
);
}
return color.rgb;
}
vec4 effect(vec4 color, sampler2D texture, vec2 texture_coords, vec2 screen_coords) {
vec4 out_color = vec4(1.0);
vec2 coords = texture_coords * vec2(1.0, -1.0) + vec2(0.0, 1.0);
out_color = texture2D(texture, coords);
if (u_color_correct == 1) {
out_color.rgb = ColorCorrect(out_color.rgb, texture_coords);
}
return out_color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment