[英]Is there a way to enable blending and depth at the same time in Metal
我有一个显示一些纹理四边形的金属视图。 纹理是从 PNG 加载的,因此是预乘的。 一些纹理具有透明像素。
当我启用混合并以正确的顺序绘制时,透明度会起作用,您可以通过纹理的透明部分看到其他四边形下方的四边形。 但是,我必须通过排序来计算正确的绘制顺序,这很昂贵并且会大大减慢我的渲染速度。
当我尝试使用深度模板并以任何顺序绘制时,我可以使用 z 位置使顺序正确工作,但随后混合停止工作。 纹理的透明部分显示金属场景的背景颜色,而不是下面的四边形。
我究竟做错了什么? 有没有办法让它工作,有人可以提供一些示例代码吗?
我看到的另一个选择是尝试在 GPU 上进行排序,这很好,因为 GPU 帧时间明显小于 CPU 帧时间。 但是,我也不确定该怎么做。
任何帮助将不胜感激。 :)
Alpha 混合是一种依赖顺序的透明技术。 这意味着(半)透明对象不能像(更昂贵的)与顺序无关的透明技术那样以任意顺序呈现。
你需要正确设置 pipelineStateDescriptor :
// To have depth buffer.
pipelineStateDescriptor.depthAttachmentPixelFormat = .depth32Float
// To use transparency.
pipelineStateDescriptor.colorAttachments[0].isBlendingEnabled = true
pipelineStateDescriptor.colorAttachments[0].rgbBlendOperation = .add
pipelineStateDescriptor.colorAttachments[0].alphaBlendOperation = .add
pipelineStateDescriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
pipelineStateDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha
pipelineStateDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
pipelineStateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
希望这可以帮助。 从这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.