问题描述
无论出于何种原因,我都遇到金属中Alpha混合的问题.我要绘制一个MTKView,对于我创建的每个管道,请执行以下操作:
For whatever reason I am having issues with alpha blending in metal. I am drawing to a MTKView and for every pipeline that I create I do the following:
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的顶点被绘制为黑色而不是透明的.
However for whatever reason that is not causing alpha testing to happen. You can even check in the frame debugger and you will see vertices with an alpha of 0 that are being drawn black rather than transparent.
我曾经想到的是某些几何图形最终会在完全相同的z平面上结束,因此,如果alpha混合不能在同一z平面上起作用,可能会导致问题.但是我不认为这是一回事.
One thought I had is that some geometry ends up on the exact same z plane so if alpha blending does not work on the same z plane that might cause an issue. But I dont think that is a thing.
为什么Alpha混合不起作用?
Why is alpha blending not working?
我希望将它们融合在一起,就像它们是透明玻璃一样.这样想.
I am hoping to blend as if they were transparent glass. Think like this.
推荐答案
Alpha混合是一种依赖顺序的透明技术.这意味着(半透明)对象不能像(更昂贵的)与顺序无关的透明技术一样以任意顺序呈现.
Alpha blending is an order-dependent transparency technique. This means that the (semi-)transparent objects cannot be rendered in any arbitrary order as is the case for (more expensive) order-independent transparency techniques.
- 请确保您的透明2D对象(例如,圆形,矩形等)具有不同深度值. (通过这种方式,您可以自己定义绘制顺序.否则,绘制顺序取决于排序算法的实现以及排序之前的初始排序.) 根据这些二维对象从后到前的深度值
- 排序.
- 使用alpha混合从后到前(画家的算法)绘制2D对象. (当然,您的2D对象需要一个alpha值
- Make sure your transparent 2D objects (e.g., circle, rectangle, etc.) have different depth values. (This way you can define the draw ordering yourself. Otherwise the draw ordering depends on the implementation of the sorting algorithm and the initial ordering before sorting.)
- Sort these 2D objects based on their depth value from back to front.
- Draw the 2D objects from back to front (painter's algorithm) using alpha blending. (Of course, your 2D objects need an alpha value < 1 to actually see some blending.)
您用于Alpha混合的混合状态是正确的:
Your blend state for alpha blending is correct:
// 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;
这篇关于金属Alpha混合不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!