Metal每日分享,UV去雾滤镜效果

4,007 阅读1分钟

「这是我参与2022首次更文挑战的第20天,活动详情查看:2022首次更文挑战

本案例的目的是理解如何用Metal实现去雾效果滤镜,类似于UV过滤器;


Demo

实操代码

// 去雾效果滤镜
let filter = C7Haze.init(distance: 0.5, slope: 0.5)

// 方案1:
ImageView.image = try? BoxxIO(element: originImage, filters: [filter, filter2, filter3]).output()

// 方案2:
ImageView.image = originImage.filtering(filter, filter2, filter3)

// 方案3:
ImageView.image = originImage ->> filter ->> filter2 ->> filter3

效果对比图

  • 不同参数下效果
distance: 0.25, slope: 0.25distance: 0.25, slope: 0.5distance: 0.4, slope: 0.5
WX20221209-154656.pngWX20221209-154718.pngWX20221209-154743.png

实现原理

  • 过滤器

这款滤镜采用并行计算编码器设计.compute(kernel: "C7Haze"),参数因子[distance, slope]

对外开放参数

  • distance: 应用颜色的强度;
  • slope: 颜色变化量;
/// 去雾,类似于UV过滤器
public struct C7Haze: C7FilterProtocol {
    
    /// Strength of the color applied.
    public var distance: Float = 0
    /// Amount of color change.
    public var slope: Float = 0
    
    public var modifier: Modifier {
        return .compute(kernel: "C7Haze")
    }
    
    public var factors: [Float] {
        return [distance, slope]
    }
    
    public init(distance: Float = 0, slope: Float = 0) {
        self.distance = distance
        self.slope = slope
    }
}
  • 着色器

归一化y乘以颜色变化量,加上强度,得到像素颜色(inColor - dd * white) / (1.0h - dd)

kernel void C7Haze(texture2d<half, access::write> outputTexture [[texture(0)]],
                   texture2d<half, access::read> inputTexture [[texture(1)]],
                   constant float *hazeDistance [[buffer(0)]],
                   constant float *slope [[buffer(1)]],
                   uint2 grid [[thread_position_in_grid]]) {
    const half4 inColor = inputTexture.read(grid);
    
    const half4 white = half4(1.0h);
    const half dd = half(grid.y) / half(inputTexture.get_height()) * half(*slope) + half(*hazeDistance);
    const half4 outColor = half4((inColor - dd * white) / (1.0h - dd));
    
    outputTexture.write(outColor, grid);
}

最后

  • 慢慢再补充其他相关滤镜,喜欢就给我点个星🌟吧。

✌️.