adding a lot of custom nodes to scene takes a long time to load(将大量自定义节点添加到场景需要很长时间才能加载)
问题描述
我正在构建一个scenekit
应用程序,但我遇到了一些更精细的细节问题。我创建了一个scene
,我正在向它添加自定义几何体,它工作得很好,直到节点数量达到大约100个。向场景中添加大量节点是理想的,是否有更干净的方法?
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)
}
我有大量的指数、法线和位置。
推荐答案
您可以做的一件事是使用Grand Central Dispatch和dispatch_apply
(分发计算)和一个串行调度队列(使用每个新cubeNode
更新emptyNode
)来拆分负载。可以找到此方法的详细解释(使用不同的问题空间,但仍在尝试加快代价高昂的操作)in this answer。
这篇关于将大量自定义节点添加到场景需要很长时间才能加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!