Skip to content

Instantly share code, notes, and snippets.

/shader.lua
Created Oct 9, 2017

Embed
What would you like to do?
shaders = {}
local colorconv = [[
vec3 rgb2hsv(vec3 c) {
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
float lum(vec3 c) {
return sqrt(pow(c.r, 2) * .299 + pow(c.g, 2) * .587 + pow(c.b, 2) * .114);
}
]]
shaders.colorize = lg.newShader(colorconv .. [[
vec4 effect(vec4 clr, Image tx, vec2 tc, vec2 sc) {
vec4 txp = Texel(tx, tc);
float txp_lum = lum(txp.rgb);
vec3 hsv_txp = rgb2hsv(txp.rgb);
vec3 hsv_clr = rgb2hsv(clr.rgb);
float out_h = hsv_clr.r;
float out_s = clamp((1 - hsv_txp.b) * 2, 0, 1);
float out_v = clamp(hsv_txp.b * 2, 0, 1);
vec3 rgb_out = hsv2rgb(vec3(out_h, out_s, out_v));
vec4 final = txp * clr;
final.rgb = rgb_out;
return final;
}
]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.