Transparent clear color

I am new to Metal and am trying to add a metal view on top of another NSView. Within the metal view (MTKView) i want to render a triangle, over clear (transparent) background. However, the background of the MTKView is always a solid color. Here is what i have tried:

I'm setting the NSView's background color to a clear color:


layer?.backgroundColor = NSColor.clearColor().CGColor


I have verified that this view in fact renders clear over the other view (rendering nothing in drawRect).

If i start rendering my triangle in drawRect, it is always over a solid background. I was able to change the background color RGB values, bot not A. It's always solid color.

override func drawRect(dirtyRect: NSRect) { 
    super.drawRect(dirtyRect) 
    ... 
    if let rpd = currentRenderPassDescriptor, drawable = currentDrawable { 
        rpd.colorAttachments[0].loadAction = .Clear 
        rpd.colorAttachments[0].clearColor = MTLClearColorMake(1, 0, 0, 0.5) 
       
        ... 
        command_buffer.presentDrawable(drawable) 
        command_buffer.commit() 
    } 
}

Any suggestions on how to get the texture cleared with a transparent color before rendering any content into it?

Thank you.

Answered by wcm in 137199022

Although

CALayer
s are non-opaque by default,
CAMetalLayer
is opaque by default, so you need to expressly set
layer.opaque
to
false
in order to draw transparent content with Metal.
Accepted Answer

Although

CALayer
s are non-opaque by default,
CAMetalLayer
is opaque by default, so you need to expressly set
layer.opaque
to
false
in order to draw transparent content with Metal.

Thank you!

Transparent clear color
 
 
Q