MTLRenderPassColorAttachmentDescriptor.clearColor does not support alpha value

I am new to Metal and trying to add a metallic look on top of another NSView. In the metal view (MTKView) I want to display a triangle, a transparent (transparent) background. However, the MTKView background always has a solid color. Here is what I tried:

I set the NSView background color to a clear color:

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

I checked that this view actually displays another view (without displaying anything in drawRect).

If I start rendering my triangle in drawRect, it will always be on a solid background. I was able to change the RGB values ​​of the background color, the bot is not A. It is always a 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 clear the texture with a transparent color before transferring any content to it?

Thank.

enter image description here

: , , , .

, .

class MetalView: MTKView {
    required init(coder: NSCoder) {
        super.init(coder: coder)

        layer?.opaque = false
    }
}
+4

All Articles