[英]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.