Metal每日分享,灵魂出窍滤镜效果

2,284 阅读2分钟

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

本案例的目的是理解如何用Metal实现灵魂出窍滤镜,灵魂出窍效果实现原理是通过两个纹理叠加,根据时间上层纹理做缩放并且不断变化其不透明度来逐渐显现。之前在缓动函数介绍中已经知道如何实现缩放效果,灵魂出窍效果就是在其基础之上再多个纹理对象叠加就能够实现;


Demo

动态效果

实操代码

// 灵魂出窍滤镜
let filter = C7SoulOut.init(soul: 0.7)

// 方案1:
let dest = BoxxIO.init(element: originImage, filter: filter)
ImageView.image = try? dest.output()

dest.filters.forEach {
    NSLog("%@", "\($0.parameterDescription)")
}

// 方案2:
ImageView.image = try? originImage.make(filter: filter)

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

实现原理

  • 过滤器

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

对外开放参数:

  • soul: 调整后的灵魂,从0.0到1.0,默认值为0.5
  • maxScale: 最大灵魂比例
  • maxAlpha: 灵魂最大透明度
/// 灵魂出窍效果
public struct C7SoulOut: C7FilterProtocol {

    public static let soulRange: ParameterRange<Float, Self> = .init(min: 0.0, max: 1.0, value: 0.5)
    
    /// The adjusted soul, from 0.0 to 1.0, with a default of 0.5
    public var soul: Float = soulRange.value
    public var maxScale: Float = 1.5
    public var maxAlpha: Float = 0.5

    public var modifier: Modifier {
        return .compute(kernel: "C7SoulOut")
    }
    
    public var factors: [Float] {
        return [soul, maxScale, maxAlpha]
    }

    public init(soul: Float = soulRange.value, maxScale: Float = 1.5, maxAlpha: Float = 0.5) {
        self.soul = soul
        self.maxScale = maxScale
        self.maxAlpha = maxAlpha
    }
}
  • 着色器

对坐标点归一化处理,然后采用最终色 = 基色 * (1 - a) + 混合色 * a,最后取对应点(soulX, soulY)像素与原像素叠加产生;

kernel void C7SoulOut(texture2d<half, access::write> outputTexture [[texture(0)]],
                      texture2d<half, access::sample> inputTexture [[texture(1)]],
                      constant float *soulPointer [[buffer(0)]],
                      constant float *maxScalePointer [[buffer(1)]],
                      constant float *maxAlphaPointer [[buffer(2)]],
                      uint2 grid [[thread_position_in_grid]]) {
    constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);
    const half4 inColor = inputTexture.read(grid);
    const float x = float(grid.x) / outputTexture.get_width();
    const float y = float(grid.y) / outputTexture.get_height();

    const half soul = half(*soulPointer);
    const half maxScale = half(*maxScalePointer);
    const half maxAlpha = half(*maxAlphaPointer);

    const half alpha = maxAlpha * (1.0h - soul);
    const half scale = 1.0h + (maxScale - 1.0h) * soul;

    const half soulX = 0.5h + (x - 0.5h) / scale;
    const half soulY = 0.5h + (y - 0.5h) / scale;

    // 最终色 = 基色 * (1 - a) + 混合色 * a
    const half4 soulMask = inputTexture.sample(quadSampler, float2(soulX, soulY));
    const half4 outColor = inColor * (1.0h - alpha) + soulMask * alpha;

    outputTexture.write(outColor, grid);
}

最后

  • 关于灵魂出窍滤镜介绍与设计到此为止吧。
  • 慢慢再补充其他相关滤镜,喜欢就给我点个星🌟吧。

✌️.