我正在开发一个着色器来渲染带有胶片框架效果的图像.我制作了以下着色器:
extern "C" {
namespace coreimage {
float4 reassemble(sampler source, sampler frame, float imgWidth, float perFrameSize, float epsilon, destination dest) {
float2 coord = dest.coord();
float4 srcExtent = source.extent();
float4 pixel = float4(0, 1, 0, 1);
float sideFrameSize = epsilon + (imgWidth - srcExtent.z)/2 - perFrameSize;
float offset = sideFrameSize + perFrameSize - epsilon;
if (coord.x < sideFrameSize) {
pixel = source.sample(source.transform(float2(coord.x + srcExtent.x + srcExtent.z - sideFrameSize, coord.y)));
}
else if (coord.x >= offset && coord.x <= srcExtent.z + offset) {
pixel = source.sample(source.transform(float2(coord.x + srcExtent.x - offset, coord.y)));
}
else if (coord.x >= srcExtent.z + offset + perFrameSize - epsilon) {
pixel = source.sample(source.transform(float2(coord.x + srcExtent.x - srcExtent.z - offset - perFrameSize + epsilon, coord.y)));
}
float4 framePixel = frame.sample(frame.transform(coord));
return mix(pixel, framePixel, framePixel.a);
}
}
}
它的运作与预期不谋而合,yields 为the following image辆.
当试图将种植整合到管道中时,就会出现这个问题.当我在调用内核函数之前添加裁剪时,一切都按预期运行.但是,如果我裁剪内核函数的输出图像,似乎首先裁剪了源图像,然后着色器在裁剪后的图像上运行.
if let source = self.kernel.apply(extent: frameImage.extent,
roiCallback: { _, rect -> CGRect in
return rect
},
arguments: [scaled.transformed(by: .init(translationX: 0, y: perFrameWidth - vOffset)), frameImage, image.extent.width, perFrameWidth, hOffset]) {
return source.cropped(to: frameImage.extent.insetBy(dx: 80, dy: 200))
}
The resulting image由上面的代码生成.
虽然这可能是一个优化,但它是相当意想不到的.如何修改此行为以确保修剪着色器的输出图像,而不是原始图像?您的真知灼见将不胜感激.
我想更详细地解释这个问题.着色器的第一个参数是图像,重要的是要注意,我不会直接裁剪它.相反,裁剪发生在输出图像上.奇怪的是,我传递给着色器的图像似乎也被裁剪了.更重要的是,我try 在调用着色器之后进行裁剪,这很奇怪,着色器接收到裁剪后的图像.