Skip to content

Instantly share code, notes, and snippets.

@tommyettinger
Created April 25, 2019 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommyettinger/95d3bff59ef2ecaeefca8ff8f7eb4990 to your computer and use it in GitHub Desktop.
Save tommyettinger/95d3bff59ef2ecaeefca8ff8f7eb4990 to your computer and use it in GitHub Desktop.
Fragment Shader for YCwCm warm-and-mild color tweaks
// this is a libGDX ShaderBatch fragment shader; it may need some tweaks to fit in other frameworks.
// You can use this to desaturate colors by setting `u_mul` to `vec3(1.0, 0.5, 0.5)` or any other small fractions for Cw and Cm. You
// can make colors warmer by setting `u_add` to `vec3(0.0, 0.6, 0.0)`; while warmth is added, randomly setting the added Cm to a
// value between -0.5 and 0.5 can simulate a fiery color effect over the screen. You can make an icy effect by setting `u_add` to
// `vec3(0.3, -0.4, 0.0)`. You can simulate the desaturation and yellowing that happens to old paintings by setting `u_mul` to
// `vec3(0.9, 0.7, 0.75)` and `u_add` to `vec3(0.05, 0.14, 0.16)`. Many other effects are possible by changing warmth and/or mild over
// multiple frames, usually smoothly and usually applying the change to the additive uniform.
varying vec2 v_texCoords;
varying vec4 v_color;
uniform sampler2D u_texture;
uniform vec3 u_mul; // luma, warm, mild (multiplied first). should default to 1,1,1 and can go above 1 or (rarely) below 0
uniform vec3 u_add; // luma, warm, mild (added second). should default to 0,0,0 and should be between -2 and 2
void main()
{
vec4 tgt = texture2D( u_texture, v_texCoords );
// xyz is used when a color is using luma, warm, mild instead of red, green, blue channels
tgt.xyz = u_add + u_mul * vec3(dot(tgt.rgb, vec3(0.375, 0.5, 0.125)), tgt.r - tgt.b, tgt.g - tgt.b);
gl_FragColor.rgb = v_color.rgb * clamp(vec3(
dot(tgt.xyz, vec3(1.0, 0.625, -0.5)),
dot(tgt.xyz, vec3(1.0, -0.375, 0.5)),
dot(tgt.xyz, vec3(1.0, -0.375, -0.5))),
0.0, 1.0);
gl_FragColor.a = v_color.a * tgt.a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment