改善SceneKit游乐场速度

时间:2016-11-20 12:51:17

标签: ios swift scenekit swift-playground scnnode

我正在测试一些简单的SceneKit运行时节点创建平面(只有大约30个平面),并且更喜欢使用Playground来测试概念。通常情况下,Playgrounds运行速度相当快,但使用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绘图?

1 个答案:

答案 0 :(得分:0)

我尽可能在macOS中使用SceneKit Playground。 iOS Playgrounds上的表现并不存在。