[英]iOS - Metal not rendering color with low saturation and alpha
我正在使用MTKView
渲染一些三角形。 一切正常,直到我降低顏色的飽和度和不透明度值,使三角形完全透明。 創建具有相同值的 SwiftUI 的Color
正確顯示。 這只發生在具有“低”飽和度的 colors 上,如果顏色具有 100% 飽和度(如#FF0000
),即使只有 1% 的不透明度,它仍然可以很好地渲染。
我注意到如果我更改colorPixelFormat
的MTKView
,結果將會改變。 所以不確定我是否只需要更改colorPixelFormat
來解決這個問題,在那種情況下,我也不知道是哪一個,因為我對圖形的了解有限。 這是顏色#FF8888
的示例:
bgra8Unorm
:最小 55% 的不透明度來渲染bgra8Unorm_srgb
:最小 77% 的不透明度用於渲染,顏色比應有的顏色淺很多。 在 Swift 中,我將 colors 存儲為[Float]
,在 MSL 中,它將轉換為float4*
。 頂點和片段函數沒什么特別的,只是返回輸入。 這不太可能是問題所在,因為其他 colors 正在工作。
一些代碼來顯示我的設置:
// MTKView's etup
clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 0)
isOpaque = false
layer.magnificationFilter = .nearest
layer.minificationFilter = .nearest
// State setup
let pipelineDescriptor = MTLRenderPipelineDescriptor()
pipelineDescriptor.vertexFunction = vertexFunction
pipelineDescriptor.fragmentFunction = fragmentFunction
pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
pipelineState = try? device.makeRenderPipelineState(descriptor: pipelineDescriptor)
// draw method setup
guard let vertexBuffer = vertexBuffer,
let indexBuffer = indexBuffer,
let indexCount = indexCount,
let colorBuffer = colorBuffer,
let pipelineState = pipelineState,
let discriptor = view.currentRenderPassDescriptor,
let commandBuffer = commandQueue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeRenderCommandEncoder(
descriptor: discriptor
),
let drawable = view.currentDrawable else {
return
}
commandEncoder.setRenderPipelineState(pipelineState)
commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
commandEncoder.setVertexBuffer(colorBuffer, offset: 0, index: 1)
commandEncoder.drawIndexedPrimitives(
type: .triangle,
indexCount: indexCount,
indexType: .uint32,
indexBuffer: indexBuffer,
indexBufferOffset: 0
)
commandEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
找到這個答案: https://stackoverflow.com/a/51414692/8355412
所以基本上,我只需要將 alpha 預乘到我的每個 rgb 組件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.