提高 SceneKit Playground 速度

Posted

技术标签:

【中文标题】提高 SceneKit Playground 速度【英文标题】:Improve SceneKit Playground Speed 【发布时间】:2016-11-20 12:51:16 【问题描述】:

我正在测试一些简单的 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 绘图?

【问题讨论】:

我遇到了同样的行为。我最终在我的设备上部署了代码,这比复杂的操场上的东西要快。您可以在 bugreport.apple.com 提交错误报告。然后至少有人在看它。没有错误票,他们不会做出任何改进...... @Hal 的这个回答是轻描淡写的。 Playgrounds 在渲染 SceneKit 方面非常糟糕。比模拟器更糟糕,这说明了一些事情。 【参考方案1】:

我尽可能在 macOS 中完成我的 SceneKit Playground 工作。 ios Playgrounds 上没有这种性能。

【讨论】:

以上是关于提高 SceneKit Playground 速度的主要内容,如果未能解决你的问题,请参考以下文章

SceneKit 中声明的 func hitTest 导致 Playground 崩溃

如何通过使用不同的类来切换 SceneKit 视图

为什么playground下载好慢

SceneKit 模型的恒定旋转

如何在我的 Scenekit.scene 之上创建 HUD

当新节点变得可见时,SceneKit 应用程序会卡顿