「这是我参加2022初次更文应战的第12天,活动详情检查:2022初次更文应战」
本案例的意图是理解如何用Metal完成图画动摇作用滤镜,还可相似涂鸦作用,首要就是对纹路坐标进行正余弦偏移处理;
Demo
- HarbethDemo地址
实操代码
// 动摇作用
let filter = C7Fluctuate.init(extent: 50, amplitude: 0.003, fluctuate: 2.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: "C7Fluctuate")
,参数因子[extent, amplitude, fluctuate]
对外开放参数
-
amplitude
: 操控振幅的巨细,越大图画越夸张; -
extent
: 操控动摇程度,越大越密布; -
fluctuate
: 动摇起伏;
/// 动摇作用,还可相似涂鸦作用
public struct C7Fluctuate: C7FilterProtocol {
/// 操控振幅的巨细,越大图画越夸张
/// Control the size of the amplitude, the larger the image, the more exaggerated the image.
public var amplitude: Float = 0.002
public var extent: Float = 50.0
public var fluctuate: Float = 0.5
public var modifier: Modifier {
return .compute(kernel: "C7Fluctuate")
}
public var factors: [Float] {
return [extent, amplitude, fluctuate]
}
public init(extent: Float = 50.0, amplitude: Float = 0.002, fluctuate: Float = 0.5) {
self.extent = extent
self.amplitude = amplitude
self.fluctuate = fluctuate
}
}
- 着色器
纹路坐标归一化处理,然后获取到偏移正余弦值作为xy,取出纹路像素色彩;
kernel void C7Fluctuate(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::sample> inputTexture [[texture(1)]],
device float *extent [[buffer(0)]],
device float *amplitude [[buffer(1)]],
device float *fluctuate [[buffer(2)]],
uint2 grid [[thread_position_in_grid]]) {
constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);
const float2 textureCoordinate = float2(float(grid.x) / outputTexture.get_width(), float(grid.y) / outputTexture.get_height());
float2 offset = float2(0, 0);
offset.x = sin(grid.x * *extent + *fluctuate) * *amplitude;
offset.y = cos(grid.y * *extent + *fluctuate) * *amplitude;
const float2 tx = textureCoordinate + offset;//mix(textureCoordinate, textureCoordinate+offset, 0.01);
const half4 outColor = inputTexture.sample(quadSampler, tx);
outputTexture.write(outColor, grid);
}
对比图
- 不同参数作用图:
extent: 50, amplitude: 0.004, fluctuate: 5.0 | extent: 40, amplitude: 0.003, fluctuate: 2.0 | extent: 60, amplitude: 0.025, fluctuate: 2.0 |
---|---|---|
更多作用自己去测试玩吧~
Harbeth功用清单
- 支撑ios体系和macOS体系
- 支撑运算符函数式操作
- 支撑多种模式数据源 UIImage, CIImage, CGImage, CMSampleBuffer, CVPixelBuffer.
- 支撑快速规划滤镜
- 支撑兼并多种滤镜作用
- 支撑输出源的快速扩展
- 支撑相机收集特效
- 支撑视频增加滤镜特效
- 支撑矩阵卷积
- 支撑运用体系 MetalPerformanceShaders.
- 支撑兼容 CoreImage.
- 滤镜部分大致分为以下几个模块:
- Blend:图画融合技术
- Blur:含糊作用
- Pixel:图画的根本像素色彩处理
- Effect:作用处理
- Lookup:查找表过滤器
- Matrix: 矩阵卷积滤波器
- Shape:图画形状巨细相关
- Visual: 视觉动态特效
- MPS: 体系 MetalPerformanceShaders.
最后
- 慢慢再弥补其他相关滤镜,喜欢就给我点个星吧。
-
滤镜Demo地址,目前包含
100+
种滤镜,一起也支撑CoreImage混合运用。 - 再附上一个开发加速库KJCategoriesDemo地址
- 再附上一个网络根底库RxNetworksDemo地址
- 喜欢的老板们可以点个星,谢谢各位老板!!!
✌️.