SKRenderer无法在MTKView中渲染SKShapeNode的Alpha通道

时间:2018-11-29 23:41:22

标签: swift macos sprite-kit metal metalkit

我已经设置了SKRenderer以便在macOS上将SKScene渲染到MTKView中。但是,场景中的任何SKShapeNode都像这样忽略它们的alpha值进行渲染: SKRenderer Output

这是我的ViewController,在其中设置了SKRenderer:

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
    }

}

我的MTKViewDelegate,其中SKRenderer在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()
    }

}

还有我的基本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)
    }
}

我是Metal的新手,所以我确定我只是缺少一些基本知识。

0 个答案:

没有答案