本案例的目的是理解如何用Metal实现图像包装效果滤镜,用于图像处理色彩丢失和模糊效果;
// 色彩丢失和模糊效果
let filter = C7ColorPacking.init(horizontalTexel: 2.5, verticalTexel: 5)
// 方案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: "C7ColorPacking")
,参数因子[horizontalTexel, verticalTexel]
对外开放参数
horizontalTexel
: 横向偏移,越大绿色轮廓虚影向右偏移越多;verticalTexel
: 纵向偏移,越大蓝色轮廓虚影向下偏移越多;/// 色彩丢失/模糊效果
public struct C7ColorPacking: C7FilterProtocol {
/// The larger the transverse offset, the more the green contour shadow offset to the right.
public var horizontalTexel: Float
/// The larger the vertical offset, the more the blue contour shadow offset downward.
public var verticalTexel: Float
public var modifier: Modifier {
return .compute(kernel: "C7ColorPacking")
}
public var factors: [Float] {
return [horizontalTexel, verticalTexel]
}
public init(horizontalTexel: Float = 0, verticalTexel: Float = 0) {
self.horizontalTexel = horizontalTexel
self.verticalTexel = verticalTexel
}
}
纹理坐标和偏移均归一化处理,然后获取到上下左右4个纹理偏移坐标,取出对应的纹理红色值,最后得到像素值;
kernel void C7ColorPacking(texture2d outputTexture [[texture(0)]],
texture2d inputTexture [[texture(1)]],
device float *texelWidthPointer [[buffer(0)]],
device float *texelHeightPointer [[buffer(1)]],
uint2 grid [[thread_position_in_grid]]) {
constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);
const float width = outputTexture.get_width();
const float height = outputTexture.get_height();
const float texelWidth = float(*texelWidthPointer / width);
const float texelHeight = float(*texelHeightPointer / height);
const float2 textureCoordinate = float2(float(grid.x) / width, float(grid.y) / height);
const float2 upperLeftTextureCoordinate = textureCoordinate + float2(-texelWidth, -texelHeight);
const float2 upperRightTextureCoordinate = textureCoordinate + float2(texelWidth, -texelHeight);
const float2 lowerLeftTextureCoordinate = textureCoordinate + float2(-texelWidth, texelHeight);
const float2 lowerRightTextureCoordinate = textureCoordinate + float2(texelWidth, texelHeight);
half upperLeftIntensity = inputTexture.sample(quadSampler, upperLeftTextureCoordinate).r;
half upperRightIntensity = inputTexture.sample(quadSampler, upperRightTextureCoordinate).r;
half lowerLeftIntensity = inputTexture.sample(quadSampler, lowerLeftTextureCoordinate).r;
half lowerRightIntensity = inputTexture.sample(quadSampler, lowerRightTextureCoordinate).r;
const half4 outColor = half4(upperLeftIntensity, upperRightIntensity, lowerLeftIntensity, lowerRightIntensity);
outputTexture.write(outColor, grid);
}
横行纵向偏移 | 横向偏移 | 纵向偏移 |
---|---|---|
100+
种滤镜,同时也支持CoreImage混合使用。✌️.