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.
UPDATE: I seem to have answered my own question, and am updating this post with it so others can find this useful.
The trick seems to be to set the layer to be transparent.
class MetalView: MTKView {
required init(coder: NSCoder) {
super.init(coder: coder)
layer?.opaque = false
}
}