2024-07-23 20:52:32 +00:00
|
|
|
/* Binary threshold shader
|
|
|
|
* See https://stackoverflow.com/a/56678483
|
|
|
|
*/
|
2024-07-21 20:38:56 +00:00
|
|
|
|
|
|
|
shader_type canvas_item;
|
|
|
|
|
|
|
|
uniform vec4 color_low: source_color = vec4(0.0, 0.0, 0.0, 1.0);
|
|
|
|
uniform vec4 color_hi: source_color = vec4(1.0, 1.0, 1.0, 1.0);
|
|
|
|
|
2024-07-23 20:52:32 +00:00
|
|
|
uniform float threshold: hint_range(0.0, 1.0) = 0.455;
|
2024-07-21 20:38:56 +00:00
|
|
|
uniform float contrast: hint_range(0.0, 1.0) = 1.0;
|
|
|
|
uniform float offset: hint_range(-1.0, 1.0) = 0.0;
|
|
|
|
|
2024-07-23 20:52:32 +00:00
|
|
|
float linearSRGB(float channel) {
|
|
|
|
if (channel <= 0.04045) {
|
|
|
|
return channel / 12.92;
|
|
|
|
} else {
|
|
|
|
return pow(((channel + 0.055) / 1.055), 2.4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float luminance(vec3 color) {
|
|
|
|
return 0.2126 * linearSRGB(color.r) + 0.7152 * linearSRGB(color.g) + 0.0722 * linearSRGB(color.b);
|
|
|
|
}
|
2024-07-21 20:38:56 +00:00
|
|
|
|
|
|
|
void fragment() {
|
|
|
|
vec3 screen_col = texture(TEXTURE, UV).rgb;
|
|
|
|
|
2024-07-23 20:52:32 +00:00
|
|
|
float Y = luminance(screen_col);
|
2024-07-21 20:38:56 +00:00
|
|
|
|
|
|
|
// adjust contrast & offset
|
2024-07-23 20:52:32 +00:00
|
|
|
Y = clamp((Y - 0.5 + offset) * contrast + 0.5, 0.0, 1.0);
|
2024-07-21 20:38:56 +00:00
|
|
|
|
2024-07-23 20:52:32 +00:00
|
|
|
COLOR.rgba = Y > threshold ? color_hi : color_low;
|
2024-07-21 20:38:56 +00:00
|
|
|
}
|