0
我正在測試一些簡單的SceneKit運行時節點創建飛機(只有大約30架飛機),並且傾向於使用Playground來測試概念。通常遊樂場運行速度相當快,但通過SceneKit,通常需要幾分之一秒的繪圖需要幾分鐘的時間。這裏是我的代碼繪製一個簡單5x5的迷宮改善SceneKit遊樂場速度
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
,請問有什麼我做錯了還是可以做優化SceneKit在操場在畫什麼?
我經歷過同樣的行爲。我最終在我的設備上部署了代碼,這些代碼比那些複雜的遊樂場設備要快。您可以在bugreport.apple.com上提交bugreport。那麼至少有人正在看它。他們不會在沒有錯誤票的情況下做出任何改進...... –
@ Hal's的這個答案是輕描淡寫的。遊樂場在渲染SceneKit時表現不佳。比模擬器更糟糕,這是在說些什麼。 – Confused