【发布时间】:2019-06-11 11:03:24
【问题描述】:
fragment half4 fragmen_shader_test(WaterColorCloudOut params[[stage_in]],
texture2d<float , access::sample>cloud1 [[texture(0)]],
texture2d<half, access::sample> cloud2 [[texture(1)]],
texture2d<half, access::sample> cloud3 [[texture(2)]]
)
{
constexpr sampler defaultSampler;
float4 color1;
if(params.index == 0){
color1= float4(cloud1.sample(defaultSampler, float2(params.textureCoordinates))) * params.color ;
}
else if(params.index == 1){
color1= float4(cloud2.sample(defaultSampler, float2(params.textureCoordinates))) * params.color ;
} else{
color1= float4(cloud3.sample(defaultSampler, float2(params.textureCoordinates))) * params.color ;
}
return half4(color1);
}
这里我使用了三个纹理,因为 If-else 条件会随着时间的推移而下降。我觉得如果我将纹理数组发送到着色器,则无需执行 if else 语句。在 CPU 中,我有三个 MTLTexture。如何将三个纹理绑定到一个数组并传递给它着色器。
在 CPU 方面我创建了三个纹理并创建了一个 MTLTexture 数组
var textureArray:[MTLTexture] = []
然后我将纹理附加到该数组。 在 MTLRenderCommandEncoder
let myRange: CountableRange = 0..<2
commandEncoder.setFragmentTextures(textureArray, range: myRange)
在着色器中
texture2d_array<float , access::sample> texture [[ texture(0) ]]
在着色器中采样时
float4 color = texture.sample(defaultSampler, float2(params.textureCoordinates),0) * float4(1,0,0,1.0);
我目前正在这样做但我无法获得正确的纹理输出
【问题讨论】: