ComfyUI/blueprints/.glsl/Unsharp_Mask_26.frag
2026-01-31 13:48:59 -08:00

69 lines
2.1 KiB
GLSL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#version 300 es
precision highp float;
uniform sampler2D u_image0;
uniform vec2 u_resolution;
uniform float u_float0; // amount [0.0 3.0] typical: 0.51.5
uniform float u_float1; // radius [0.5 10.0] blur radius in pixels
uniform float u_float2; // threshold [0.0 0.1] min difference to sharpen
in vec2 v_texCoord;
layout(location = 0) out vec4 fragColor0;
float gaussian(float x, float sigma) {
return exp(-(x * x) / (2.0 * sigma * sigma));
}
float getLuminance(vec3 color) {
return dot(color, vec3(0.2126, 0.7152, 0.0722));
}
void main() {
vec2 texel = 1.0 / u_resolution;
float radius = max(u_float1, 0.5);
float amount = u_float0;
float threshold = u_float2;
vec4 original = texture(u_image0, v_texCoord);
// Gaussian blur for the "unsharp" mask
int samples = int(ceil(radius));
float sigma = radius / 2.0;
vec4 blurred = vec4(0.0);
float totalWeight = 0.0;
for (int x = -samples; x <= samples; x++) {
for (int y = -samples; y <= samples; y++) {
vec2 offset = vec2(float(x), float(y)) * texel;
vec2 sampleCoord = v_texCoord + offset;
// Boundary check - skip out-of-bounds samples
if (sampleCoord.x < 0.0 || sampleCoord.x > 1.0 ||
sampleCoord.y < 0.0 || sampleCoord.y > 1.0) {
continue;
}
float dist = length(vec2(float(x), float(y)));
float weight = gaussian(dist, sigma);
blurred += texture(u_image0, sampleCoord) * weight;
totalWeight += weight;
}
}
blurred /= totalWeight;
// Unsharp mask = original - blurred
vec3 mask = original.rgb - blurred.rgb;
// Luminance-based threshold (Photoshop-style)
float lumaDelta = abs(getLuminance(original.rgb) - getLuminance(blurred.rgb));
if (lumaDelta < threshold) {
mask = vec3(0.0);
}
// Sharpen: original + mask * amount
vec3 sharpened = original.rgb + mask * amount;
fragColor0 = vec4(clamp(sharpened, 0.0, 1.0), original.a);
}