/* Binary threshold shader * See https://stackoverflow.com/a/56678483 */ 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); uniform float threshold: hint_range(0.0, 1.0) = 0.455; uniform float contrast: hint_range(0.0, 1.0) = 1.0; uniform float offset: hint_range(-1.0, 1.0) = 0.0; 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); } void fragment() { vec3 screen_col = texture(TEXTURE, UV).rgb; float Y = luminance(screen_col); // adjust contrast & offset Y = clamp((Y - 0.5 + offset) * contrast + 0.5, 0.0, 1.0); COLOR.rgba = Y > threshold ? color_hi : color_low; }