我有下面的着色器(为了长度和清晰度而删除了部分),并且想找到一个更好的方法来做到这一点。我想将一组大小可变的纹理发送到我的金属着色器。我将对顶点位置进行一些计算,然后找出要使用的纹理。
目前我刚刚对东西进行了硬编码并使用了几个 if 语句,但这很丑陋(而且我猜不快)。有什么方法可以计算i
然后i
用作纹理下标(如tex[i].sample
)?
// Current code - its ugly
fragment half4 SimpleTextureFragment(VertextOut inFrag [[stage_in]],
texture2d<half> tex0 [[ texture(0) ]]
texture2d<half> tex1 [[ texture(1) ]]
texture2d<half> tex2 [[ texture(2) ]]
...
texture2d<half> texN [[ texture(N) ]]
)
{
constexpr sampler quad_sampler;
int i = (Compute_Correct_Texture_to_Use);
if(i==0)
{
half4 color = tex0.sample(quad_sampler, inFrag.tex_coord);
}
else if(i==1)
{
half4 color = tex1.sample(quad_sampler, inFrag.tex_coord);
}
...
else if(i==n)
{
half4 color = texN.sample(quad_sampler, inFrag.tex_coord);
}
return color;
}