簡體   English   中英

提高SceneKit游樂場速度

[英]Improve SceneKit Playground Speed

我正在測試一些簡單的SceneKit運行時節點創建平面(僅約30個平面),並且希望使用Playground來測試概念。 通常,游樂場的運行速度相當快,但是使用SceneKit,通常只需要幾分之一秒的繪圖就需要幾分鍾。 這是我的代碼,它繪制了一個簡單的5x5迷宮

import UIKit
import SceneKit
import PlaygroundSupport // needed to create the live view

let maze = Maze(mazeSizeX, mazeSizeY)
public let π = M_PI
maze.solveMaze(x: mazeSizeX-1, y: mazeSizeY-1, comingFrom: -1)
maze.displayWithSolution()

let cellSize: CGFloat = 1.0

let scene = SCNScene()


let blueMat = SCNMaterial()
blueMat.diffuse.contents = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
blueMat.lightingModel = SCNMaterial.LightingModel.physicallyBased

let redMat = SCNMaterial()
redMat.diffuse.contents = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
redMat.lightingModel = SCNMaterial.LightingModel.physicallyBased

let greenMat = SCNMaterial()
greenMat.diffuse.contents = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
greenMat.lightingModel = SCNMaterial.LightingModel.physicallyBased

let purpleMat = SCNMaterial()
purpleMat.diffuse.contents = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)
purpleMat.lightingModel = SCNMaterial.LightingModel.physicallyBased


let planeN = SCNPlane(width: cellSize, height: cellSize)
let planeS = SCNPlane(width: cellSize, height: cellSize)
let planeE = SCNPlane(width: cellSize, height: cellSize)
let planeW = SCNPlane(width: cellSize, height: cellSize)

planeN.materials = [blueMat]
planeS.materials = [redMat]
planeE.materials = [greenMat]
planeW.materials = [purpleMat]
//plane.firstMaterial?.isDoubleSided = true


for x in 0 ..< mazeSizeX
{
    let xPos: CGFloat = CGFloat(x)
    for y in 0 ..< mazeSizeY
    {
        let yPos: CGFloat = CGFloat(y)
        if maze.isWallThere(x: Double(x), y: Double(y), side: North)
        {
            let planeNode = SCNNode(geometry: planeN)
            planeNode.rotation = SCNVector4(-1, 0, 0, π/2)
            planeNode.position = SCNVector3(-yPos*cellSize, (xPos-0.5)*cellSize, cellSize/2.0)
            scene.rootNode.addChildNode(planeNode)
        }
        if maze.isWallThere(x: Double(x), y: Double(y), side: South)
        {

            let planeNode = SCNNode(geometry: planeS)
            planeNode.rotation = SCNVector4(1, 0, 0, π/2)
            planeNode.position = SCNVector3(-yPos*cellSize, (xPos+0.5)*cellSize, cellSize/2.0)
            scene.rootNode.addChildNode(planeNode)
        }
        if maze.isWallThere(x: Double(x), y: Double(y), side: East)
        {
            let planeNode = SCNNode(geometry: planeE)
            planeNode.rotation = SCNVector4(0, 1, 0, π/2)
            planeNode.position = SCNVector3((-yPos-0.5)*cellSize, xPos*cellSize, cellSize/2.0)
            scene.rootNode.addChildNode(planeNode)
        }
        if maze.isWallThere(x: Double(x), y: Double(y), side: West)
        {
            let planeNode = SCNNode(geometry: planeW)
            planeNode.rotation = SCNVector4(0, -1, 0, π/2)
            planeNode.position = SCNVector3((-yPos+0.5)*cellSize, xPos*cellSize, cellSize/2.0)
            scene.rootNode.addChildNode(planeNode)
        }
    }
}


let floor = SCNNode(geometry: SCNBox(width: CGFloat(mazeSizeX), height: CGFloat(mazeSizeY), length: 0.1, chamferRadius: 0))
floor.position = SCNVector3(-(CGFloat(mazeSizeX)/2.0)+0.5, (CGFloat(mazeSizeY)/2.0)-0.5, 0)
scene.rootNode.addChildNode(floor)

let light = SCNLight()
light.type = SCNLight.LightType.omni
let lightNode = SCNNode()
lightNode.light = light
lightNode.position = SCNVector3(40,12,15)
scene.rootNode.addChildNode(lightNode)


let light2 = SCNLight()
light2.type = SCNLight.LightType.omni
let lightNode2 = SCNNode()
lightNode2.light = light
lightNode2.position = SCNVector3(-40,-24,-30)
scene.rootNode.addChildNode(lightNode2)

var cameraPosition = SCNVector3Make(0.5, 0.5, 0.5)
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = cameraPosition
cameraNode.rotation = SCNVector4(-1, 0, 0, π/2)
scene.rootNode.addChildNode(cameraNode)

//let view = SCNView() //iPad version
let view = SCNView(frame: CGRect(x: 0, y: 0, width: 400, height: 600))     //Xcode version
view.allowsCameraControl = true
view.autoenablesDefaultLighting = true
view.showsStatistics = true
view.scene = scene
view.backgroundColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
PlaygroundPage.current.liveView = view

我做錯了什么或者可以做些什么來優化Playground中的SceneKit繪圖?

我盡可能在macOS中進行SceneKit Playground的工作。 性能只是iOS Playgrounds上沒有。

暫無
暫無

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

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