簡體   English   中英

使用 Metal/SpriteKit 渲染矢量圖形

[英]Rendering Vector graphics using Metal/SpriteKit

我希望使用 Metal 將矢量形狀 (CGPath) 渲染到屏幕外紋理。 然而,正如我所見,使用處理抗鋸齒和其他問題的 Metal 繪制粗線非常困難。 因此,使用 Metal 繪制任意 CGPath 將涉及大量工作。 我在想是否可以使用 SpriteKit(基於 Metal)來實現同樣的事情。 目前尚不清楚如何使用 SKRenderer class 將任意 CGPath 渲染到金屬紋理。

以下代碼將使用MTLTextureCGPath創建SKScene

import SpriteKit
import MetalKit

class PathScene: SKScene {
    
    let device = MTLCreateSystemDefaultDevice()!
    
    override func didMove(to view: SKView) {
        
        //This is not needed if you supply your own CGPath
        let bezPath = UIBezierPath(ovalIn: self.frame.insetBy(dx: 50, dy: 50))
        
        let lineNode = SKShapeNode()
        lineNode.path = bezPath.cgPath //Use existing CGPath here
        lineNode.lineWidth = 8.0
        lineNode.strokeColor = UIColor.blue
        lineNode.name = "line"
        
        self.addChild(lineNode)
        
        guard let scene = self.scene else {return}
        
        if let skTexture = self.view?.texture(from: scene) {
            let cgImage = skTexture.cgImage()
            let loader = MTKTextureLoader(device: device)
            let mtlTexture = try! loader.newTexture(cgImage: cgImage, options: nil)
        }
        
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM