2023-01-09 03:14:20 +01:00
|
|
|
//
|
|
|
|
// Simple passthrough fragment shader
|
|
|
|
//
|
|
|
|
varying vec2 v_vTexcoord;
|
|
|
|
varying vec4 v_vColour;
|
|
|
|
|
2023-07-18 17:51:40 +02:00
|
|
|
uniform int useMask;
|
|
|
|
uniform sampler2D mask;
|
|
|
|
|
2023-01-09 03:14:20 +01:00
|
|
|
uniform vec4 colorFrom[32];
|
|
|
|
uniform int colorFrom_amo;
|
|
|
|
uniform vec4 colorTo[32];
|
|
|
|
uniform int colorTo_amo;
|
|
|
|
|
|
|
|
uniform int inverted;
|
|
|
|
uniform int alphacmp;
|
|
|
|
uniform int hardReplace;
|
|
|
|
uniform float treshold;
|
|
|
|
|
2024-01-08 08:10:50 +01:00
|
|
|
vec3 rgb2xyz( vec3 c ) { #region
|
2023-01-09 03:14:20 +01:00
|
|
|
vec3 tmp;
|
|
|
|
tmp.x = ( c.r > 0.04045 ) ? pow( ( c.r + 0.055 ) / 1.055, 2.4 ) : c.r / 12.92;
|
|
|
|
tmp.y = ( c.g > 0.04045 ) ? pow( ( c.g + 0.055 ) / 1.055, 2.4 ) : c.g / 12.92,
|
|
|
|
tmp.z = ( c.b > 0.04045 ) ? pow( ( c.b + 0.055 ) / 1.055, 2.4 ) : c.b / 12.92;
|
|
|
|
return 100.0 * tmp *
|
|
|
|
mat3( 0.4124, 0.3576, 0.1805,
|
|
|
|
0.2126, 0.7152, 0.0722,
|
|
|
|
0.0193, 0.1192, 0.9505 );
|
2024-01-08 08:10:50 +01:00
|
|
|
} #endregion
|
2023-01-09 03:14:20 +01:00
|
|
|
|
2024-01-08 08:10:50 +01:00
|
|
|
vec3 xyz2lab( vec3 c ) { #region
|
2023-01-09 03:14:20 +01:00
|
|
|
vec3 n = c / vec3( 95.047, 100, 108.883 );
|
|
|
|
vec3 v;
|
|
|
|
v.x = ( n.x > 0.008856 ) ? pow( n.x, 1.0 / 3.0 ) : ( 7.787 * n.x ) + ( 16.0 / 116.0 );
|
|
|
|
v.y = ( n.y > 0.008856 ) ? pow( n.y, 1.0 / 3.0 ) : ( 7.787 * n.y ) + ( 16.0 / 116.0 );
|
|
|
|
v.z = ( n.z > 0.008856 ) ? pow( n.z, 1.0 / 3.0 ) : ( 7.787 * n.z ) + ( 16.0 / 116.0 );
|
|
|
|
return vec3(( 116.0 * v.y ) - 16.0, 500.0 * ( v.x - v.y ), 200.0 * ( v.y - v.z ));
|
2024-01-08 08:10:50 +01:00
|
|
|
} #endregion
|
2023-01-09 03:14:20 +01:00
|
|
|
|
2024-01-08 08:10:50 +01:00
|
|
|
vec3 rgb2lab(vec3 c) { #region
|
2023-01-09 03:14:20 +01:00
|
|
|
vec3 lab = xyz2lab( rgb2xyz( c ) );
|
|
|
|
return vec3( lab.x / 100.0, 0.5 + 0.5 * ( lab.y / 127.0 ), 0.5 + 0.5 * ( lab.z / 127.0 ));
|
2024-01-08 08:10:50 +01:00
|
|
|
} #endregion
|
2023-01-09 03:14:20 +01:00
|
|
|
|
2024-01-08 08:10:50 +01:00
|
|
|
vec3 rgb2hsv(vec3 c) { #region
|
2023-01-09 03:14:20 +01:00
|
|
|
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 = 0.0000000001;
|
|
|
|
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
|
2024-01-08 08:10:50 +01:00
|
|
|
} #endregion
|
2023-01-09 03:14:20 +01:00
|
|
|
|
2024-01-08 08:10:50 +01:00
|
|
|
vec3 hsv2rgb(vec3 c) { #region
|
2023-01-09 03:14:20 +01:00
|
|
|
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);
|
2024-01-08 08:10:50 +01:00
|
|
|
} #endregion
|
2023-01-09 03:14:20 +01:00
|
|
|
|
2024-01-08 08:10:50 +01:00
|
|
|
float round(float val) { return fract(val) >= 0.5? ceil(val) : floor(val); }
|
2023-01-09 03:14:20 +01:00
|
|
|
|
|
|
|
void main() {
|
2023-07-18 17:51:40 +02:00
|
|
|
vec4 col = texture2D( gm_BaseTexture, v_vTexcoord );
|
2023-01-09 03:14:20 +01:00
|
|
|
vec4 baseColor;
|
|
|
|
if(inverted == 0)
|
|
|
|
baseColor = col;
|
2023-07-18 17:51:40 +02:00
|
|
|
else if(inverted == 1) {
|
2023-01-09 03:14:20 +01:00
|
|
|
baseColor = vec4(vec3(0.), 1.);
|
2023-07-18 17:51:40 +02:00
|
|
|
|
|
|
|
if(useMask == 1) {
|
|
|
|
vec4 m = texture2D( mask, v_vTexcoord );
|
|
|
|
if((m.r + m.g + m.b) * m.a < .5) {
|
|
|
|
gl_FragColor = baseColor;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-09 03:14:20 +01:00
|
|
|
|
|
|
|
vec3 hsv = rgb2hsv(col.rgb);
|
|
|
|
if(alphacmp == 1) hsv *= col.a;
|
|
|
|
|
|
|
|
float min_df = treshold;
|
|
|
|
int min_index = 0;
|
|
|
|
|
|
|
|
for(int i = 0; i < colorFrom_amo; i++) {
|
|
|
|
vec3 hsvFrom = rgb2hsv(colorFrom[i].rgb);
|
|
|
|
|
|
|
|
float df = length(hsv - hsvFrom);
|
|
|
|
if(df < min_df) {
|
|
|
|
min_df = df;
|
|
|
|
min_index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vec4 clr = colorTo[int(round(float(min_index) / float(colorFrom_amo - 1) * float(colorTo_amo - 1)))];
|
|
|
|
|
|
|
|
if(min_df < treshold) {
|
|
|
|
if(hardReplace == 0) {
|
|
|
|
float rat = min_df / treshold;
|
|
|
|
gl_FragColor = baseColor * (rat) + clr * (1. - rat);
|
|
|
|
} else
|
|
|
|
gl_FragColor = clr;
|
|
|
|
} else
|
|
|
|
gl_FragColor = baseColor;
|
|
|
|
|
|
|
|
if(inverted == 0)
|
|
|
|
gl_FragColor.a = col.a;
|
|
|
|
}
|