USM锐化基本原理
USM锐化利用原图减去高斯模糊后的图,达到提升边缘清晰度的目的,基本公式如下
usm_color = gaussianBlurColor * -usmFactor + originColor * (1.0 + usmFactor)
usmFactor
表示锐化程度,可以从0到1,超过1也是可以的
OpenGL Shader实现
高斯模糊实现
vec4 gaussianBlur(vec2 uv, float size) {
float Pi = 6.28318530718; // Pi*2
// GAUSSIAN BLUR SETTINGS {{{
float Directions = 16.0; // BLUR DIRECTIONS (Default 16.0 - More is better but slower)
float Quality = 4.0; // BLUR QUALITY (Default 4.0 - More is better but slower)
float Size = size; // BLUR SIZE (Radius)
// GAUSSIAN BLUR SETTINGS }}}
vec2 Radius = Size/imageResolution.xy;
// Pixel colour
vec4 Color = texture(diffuseMap, uv);
// Blur calculations
for( float d=0.0; d<Pi; d+=Pi/Directions)
{
for(float i=1.0/Quality; i<=1.0; i+=1.0/Quality)
{
Color += texture( diffuseMap, uv+vec2(cos(d),sin(d))*Radius*i);
}
}
// Output to screen
Color /= Quality * Directions;
return Color;
}
这里需要依赖图片的像素尺寸imageResolution
,需要通过uniform
传进来
USM实现
vec4 usm(vec4 originColor, float factor) {
vec4 gaussianColor = gaussianBlur(vec2(fragUV.x, 1.0 - fragUV.y), 3);
return gaussianColor * -factor + originColor * (1.0 + factor);
}