metal layer transparent background

Hello, I want to render metal with a transparent background on top of a gradient layer.

I have set the clearColor alpha to 0 renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(1.0, 1.0, 1.0, 0.0)

My metal render displays as I expect but the background is white, not transparent.

I tried setting the metal layer view opaque = false, background still white.


What am I missing??


Thanks.

Answered by JohnWeeks in 91158022

Fritzt, Thanks for your reply.


In the process of trying out your suggestion I found a bug in my code that resolved my issue.

Blending is not necessary, loadAction = .Clear and a clearColor with alpha 0.0 creates a transparant background.


FYI my graident layer background is screen size and my Metal layer is smaller so the transparent background is simple to do. Calculating the graident in the Metal layer to match the graident background would be... well I'm really not sure how I would do it.

did you enable blending? e.g.


pipelineStateDescriptor.colorAttachments[0].pixelFormat = .BGRA8Unorm
pipelineStateDescriptor.colorAttachments[0].blendingEnabled = true
pipelineStateDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperation.Add
pipelineStateDescriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperation.Add
pipelineStateDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactor.One
pipelineStateDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactor.One
pipelineStateDescriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactor.OneMinusSourceAlpha
pipelineStateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactor.OneMinusSourceAlpha


I also have a gradient background but 'am drawing it using Metal as well, it's probably cheaper.

Accepted Answer

Fritzt, Thanks for your reply.


In the process of trying out your suggestion I found a bug in my code that resolved my issue.

Blending is not necessary, loadAction = .Clear and a clearColor with alpha 0.0 creates a transparant background.


FYI my graident layer background is screen size and my Metal layer is smaller so the transparent background is simple to do. Calculating the graident in the Metal layer to match the graident background would be... well I'm really not sure how I would do it.

metal layer transparent background
 
 
Q