shader_type canvas_item; uniform float line_number = 6; uniform vec4 line_color : source_color = vec4(1.); uniform float line_thickness : hint_range(0.0, 0.01) = 0.001; uniform bool inverse_result = false; const mat3 sobel_x = mat3( vec3( 1.0, 2.0, 1.0), vec3( 0.0, 0.0, 0.0), vec3(-1.0, -2.0, -1.0) ); const mat3 sobel_y = mat3( vec3(1.0, 0.0, -1.0), vec3(2.0, 0.0, -2.0), vec3(1.0, 0.0, -1.0) ); float sample_quantized(sampler2D text, vec2 uv) { return ceil(texture(text, uv).r * line_number) / line_number; } void fragment() { float gx = 0.0; float gy = 0.0; for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { vec2 offset = vec2(float(x) - 1.0, float(y) - 1.0) * line_thickness; float s = sample_quantized(TEXTURE, UV + offset); gx += s * sobel_x[x][y]; gy += s * sobel_y[x][y]; } } float edge = length(vec2(gx, gy)); float line = step(0.0001, edge); float alpha = line * line_color.a * COLOR.r; if (inverse_result) alpha = 1. - line * line_color.a; COLOR = vec4(line_color.rgb, alpha); }