【发布时间】:2021-06-29 10:05:15
【问题描述】:
我正在尝试编写一个简单的金属着色器以在 Swift 中使用,它会拍摄一张照片并将其从左向右移动。假设我想要图片 x 以 screenwidth - image.size.width 开始并移动 x 直到 image.x = screenwidth.x。我使用了 Metalpetal,我的代码扭曲了图像。
fragment float4 SimplePanFragmentRight(VertexOut vertexIn [[ stage_in ]],
texture2d<float, access::sample> fromTexture [[ texture(0) ]],
texture2d<float, access::sample> toTexture [[ texture(1) ]],
constant float & scale [[ buffer(0) ]],
constant float & rotations [[ buffer(1) ]],
constant float2 & center [[ buffer(2) ]],
constant float4 & backColor [[ buffer(3) ]],
constant float & ratio [[ buffer(4) ]],
constant float & progress [[ buffer(5) ]],
sampler textureSampler [[ sampler(0) ]])
{
float2 uv = vertexIn.textureCoordinate;
uv.y = 1.0 - uv.y;
float _fromR = float(fromTexture.get_width())/float(fromTexture.get_height());
float _toR = float(toTexture.get_width())/float(toTexture.get_height());
float t = 1.0;
float pro = progress / 0.25;
// hence bad code
uv = adjustPos( uv, pro);
// ****
return mix(
getFromColor(uv, fromTexture, ratio, _fromR),
getToColor(uv, toTexture, ratio, _toR),
t);
}
而我操纵 x 位置的“简单函数”是
float2 adjustPos(
float2 uv, float amount) {
uv.x = uv.x * amount;
return uv;
}
如何根据进度比移动线性 x 位置而没有任何图像失真?
【问题讨论】:
-
你不应该加这个数量而不是相乘吗?
uv.x = uv.x + amount