Metal MTLTexture replaces semi-transparent areas with black when alpha values that aren't 1 or 0

后端 未结 2 622
一生所求
一生所求 2020-12-18 22:06

While using Apple\'s texture importer, or my own, a white soft-edged circle drawn in software (with a transparent bg) or in Photoshop (saved as a PNG) when rendered will hav

相关标签:
2条回答
  • Since you are using a CGContext that is configured to use colors premultiplied with the alpha channel, using the standard RGB = src.rgb * src.a + dst.rgb * (1-src.a) will cause darkened areas because the src.rgb values are premultiplied with src.a already. This means that what you want is src.rgb + dst.rgb * (1-src.a) which is configured like this:

    pipeline.colorAttachments[0].isBlendingEnabled = true
    pipeline.colorAttachments[0].rgbBlendOperation = .add
    pipeline.colorAttachments[0].alphaBlendOperation = .add
    pipeline.colorAttachments[0].sourceRGBBlendFactor = .one
    pipeline.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha
    pipeline.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
    pipeline.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
    

    The .one means to leave RGB as-is. The reason for premultiplied colors is that you only need to multiply the colors once on creation as opposed to every time you blend. Your configuration achieves this too, but indirectly.

    0 讨论(0)
  • 2020-12-18 22:42

    Thanks to Jessy I decided to look through how I was blending my alphas and I've figured it out. My texture still looks darkened in the GPU debugger, but in the actual app everything looks correct. I made my changes to my pipeline state descriptor, which you can see below.

    pipelineStateDescriptor.colorAttachments[0].blendingEnabled = true
    pipelineStateDescriptor.colorAttachments[0].rgbBlendOperation = .Add
    pipelineStateDescriptor.colorAttachments[0].alphaBlendOperation = .Add
    pipelineStateDescriptor.colorAttachments[0].sourceRGBBlendFactor = .DestinationAlpha
    pipelineStateDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .DestinationAlpha
    pipelineStateDescriptor.colorAttachments[0].destinationRGBBlendFactor = .OneMinusSourceAlpha
    pipelineStateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .OneMinusBlendAlpha
    
    0 讨论(0)
提交回复
热议问题