1

I've set up an SKRenderer to render an SKScene into an MTKView on macOS. However any SKShapeNodes in the scene render like this as though they're ignoring the alpha values: SKRenderer Output

Here is my ViewController where the SKRenderer is set up:

import Cocoa
import SpriteKit
import MetalKit

class ViewController: NSViewController {

    var device: MTLDevice!
    var renderer: SKRenderer!
    var commandQueue: MTLCommandQueue!
    var scene: GameScene!
    var mtkView = MTKView()

    override func viewDidAppear() {
        super.viewDidAppear()

        device = MTLCreateSystemDefaultDevice()
        renderer = SKRenderer(device: device)
        commandQueue = device.makeCommandQueue()

        scene = GameScene(size: view.bounds.size)
        scene.scaleMode = .aspectFill

        renderer.scene = scene

        mtkView.device = device
        mtkView.delegate = self
        mtkView.isPaused = false
        view.addSubview(mtkView)
    }

    override func viewDidLayout() {
        mtkView.frame = view.bounds
    }

}

My MTKViewDelegate where the SKRenderer renders in Metal:

extension ViewController: MTKViewDelegate {

    func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) { }

    func draw(in view: MTKView) {
        let commandBuffer = commandQueue.makeCommandBuffer()!
        let renderPassDescriptor = view.currentRenderPassDescriptor!

        renderer.render(withViewport: view.bounds, commandBuffer: commandBuffer, renderPassDescriptor: renderPassDescriptor)

        commandBuffer.present(view.currentDrawable!)
        commandBuffer.commit()
    }

}

And my basic GameScene:

import SpriteKit

class GameScene: SKScene {

    override init(size: CGSize) {
        super.init(size: size)

        anchorPoint = CGPoint(x: 0.5, y: 0.5)
        let circle = SKShapeNode(ellipseOf: CGSize(width: size.width / 2, height: size.height / 2))
        circle.fillColor = SKColor.green
        circle.strokeColor = SKColor.blue
        addChild(circle)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        anchorPoint = CGPoint(x: 0.5, y: 0.5)
        let circle = SKShapeNode(ellipseOf: CGSize(width: size.width / 2, height: size.height / 2))
        circle.fillColor = SKColor.green
        circle.strokeColor = SKColor.clear
        addChild(circle)
    }
}

I'm pretty new to Metal so I'm sure that I'm just missing something basic.

Doug
  • 2,972
  • 3
  • 22
  • 28
  • Where are you setting alpha values? – El Tomato Nov 30 '18 at 04:04
  • As far as I understand, the alpha values are set automatically by the SKShapeNode? If you present that same GameScene in an SKView, the ellipse has a blue stroke and everything is clear outside of that: https://imgur.com/a/7ZzJ2uC – Doug Nov 30 '18 at 10:29
  • I have a feeling this might be a bug. The same code on iOS renders correctly – Doug Dec 03 '18 at 17:37
  • Did you ever figure this out? I'm having the exact opposite issue. I think it might be an incorrect setting in the pipeline descriptor. – Bill Tudor Jun 30 '21 at 07:10
  • @BillTudor I believe I opened a DTS on this and the response was that it's a bug. I opened a feedback but never saw any activity on it ☹️ – Doug Jul 04 '21 at 14:01
  • @Doug creating a customer SKshader resolved my issue. https://stackoverflow.com/a/28321896/4171592 – Bill Tudor Jul 05 '21 at 17:31

0 Answers0