How to get the advantages of Scenekit's level editor programatically

给你一囗甜甜゛ 提交于 2019-12-08 01:16:06

问题


I've just ran a couple of tests comparing the performance of different ways of loading/creating a scene to see the performance impact. The test was simply rendering a 32x32 grid of cubes and eyeballing the CPU usage, memory, energy and rendering times. Not very scientific but there were some clear results. The four tests consisted of...

  1. Load a .dae, e.g. SCNScene(named: "grid.dae")
  2. Converting a .dae to .scn file in XCode and loadinf that
  3. Building a grid in the Scnenekit editor manually using a reference node
  4. Building a grid programatically using an SCNReference node (see code at bottom of question)

I expected 1 & 2 to be broadly the same and they were.

I expected test 3 to have much better performance than tests 1 & 2, and it did. The CPU load and energy usage was very low. It had half the memory foootprint and the rendering time was a fraction of the rendering times for test 1&2.

I was hoping test 4 would match test 3, but it didn't. It appeared to be the same or worse than tests 1&2.

    // Code for test 4  

    let boxPath = Bundle.main.path(forResource: "box", ofType: "scn")
    let boxUrl = URL(fileURLWithPath: boxPath!)
    let offset: Int = 16

    for xIndex:Int in 0...32 {
      for yIndex:Int in 0...32 {
           let boxReference = SCNReferenceNode(url: boxUrl)
           scene.rootNode.addChildNode(boxReference!)
           boxReference?.position.x = Float(xIndex - offset)
           boxReference?.position.y = Float(yIndex - offset)
           boxReference?.load()
        }
     }

Is the performance advantage that SceneKit's level editor provides available to developers and I'm just going about it wrong, or is Scenekit/XCode doing something bespoke under the hood?

UPDATE

In response to Confused's comment, I tried using the flattenedCone method on SCNNode. Here is a variation on the original code using that technique...

        let boxPath = Bundle.main.path(forResource: "box", ofType: "scn")
        let boxUrl = URL(fileURLWithPath: boxPath!)
        let offset: Int = 16
        let testNode = SCNNode()

        for xIndex:Int in 0...32 {
            for yIndex:Int in 0...32 {
                let boxReference = SCNReferenceNode(url: boxUrl)
                testNode.addChildNode(boxReference!)
                boxReference?.position.x = Float(xIndex - offset)
                boxReference?.position.y = Float(yIndex - offset)
                boxReference?.load()
            }
        }

        let optimizedNode = testNode.flattenedClone()
        scene.rootNode.addChildNode(optimizedNode)

来源:https://stackoverflow.com/questions/39819036/how-to-get-the-advantages-of-scenekits-level-editor-programatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!