无论出于何种原因,我都遇到金属中Alpha混合的问题。我要绘制一个MTKView,对于我创建的每个管道,请执行以下操作:
descriptor.colorAttachments[0].blendingEnabled = YES;
descriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
descriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;
descriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
descriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorSourceAlpha;
descriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
descriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
但是,无论出于何种原因,都不会导致进行Alpha测试。您甚至可以检入帧调试器,并且会看到alpha为0的顶点被绘制为黑色而不是透明的。
我曾经想到的是某些几何图形最终会在完全相同的z平面上结束,因此,如果alpha混合在同一z平面上不起作用,可能会导致问题。但是我不认为这是一回事。
为什么Alpha混合不起作用?
我希望将它们融合在一起,就像它们是透明玻璃一样。这样想。
答案 0 :(得分:1)
Alpha混合是一种与订单有关的透明技术。这意味着(半透明)对象不能像(更昂贵的)与顺序无关的透明技术一样以任意顺序呈现。
您用于Alpha混合的混合状态是正确的:
// The blend formula is defined as:
// (source.rgb * sourceRGBBlendFactor ) rgbBlendOperation (destination.rgb * destinationRGBBlendFactor )
// (source.a * sourceAlphaBlendFactor) alphaBlendOperation (destination.a * destinationAlphaBlendFactor)
// <=>
// (source.rgba * source.a) + (destination.rgba * (1-source.a))
descriptor.colorAttachments[0].blendingEnabled = YES;
descriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
descriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;
descriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
descriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorSourceAlpha;
descriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
descriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;