问题
I am building a scenekit
app however I am having issues with some of the finer details. I created a scene
and I am adding custom geometry to it and it works fine until the number of nodes gets to about 100. It is ideal to add a large number of nodes to a scene and is there a cleaner way to do it?
for i in 0..<modelArr!.count {
let model = modelArr![i]
let pos: [SCNVector3] = Parser.loadPosition(model)
let norm:[SCNVector3] = Parser.loadNormals(model)
let ind:[CInt] = Parser.loadIndices(model)
let src = SCNGeometrySource(vertices: pos, count: pos.count)
let norSrc = SCNGeometrySource(normals: norm, count: norm.count)
//let indexData = NSData(bytes: ind, length: sizeof(CInt) * ind.count)
let indexData = NSData(bytes: Array<CInt>(0..<CInt(ind.count)),
length: ind.count * sizeof(Float))
let element = SCNGeometryElement(data: indexData,
primitiveType: .Triangles,
primitiveCount: pos.count / 3,
bytesPerIndex: sizeof(Float))
let geo = SCNGeometry(sources: [src, norSrc], elements: [element])
let material = SCNMaterial()
material.diffuse.contents = UIColor.redColor()
material.specular.contents = UIColor.whiteColor()
let cubeNode = SCNNode(geometry: geo)
cubeNode.geometry?.firstMaterial = material
emptyNode.addChildNode(cubeNode)
}
scn.rootNode.addChildNode(emptyNode)
}
I have a large number of indices, normals, and positions.
回答1:
One thing you can do is split up the load using Grand Central Dispatch, and dispatch_apply
(to distribute the computation) and a serial dispatch queue (to update emptyNode
with each new cubeNode
. A thorough explanation of this approach (using a different problem space, but still trying to speed up an expensive operation) can be found in this answer.
What will help a lot is to do the loading in a separate utility program. After you've created your scene, archive it. Embed that scene in your user-facing program. Unarchiving the scene is much faster than creating it from scratch.
来源:https://stackoverflow.com/questions/38941570/adding-a-lot-of-custom-nodes-to-scene-takes-a-long-time-to-load