为什么SCNNode.presentation.position相对较慢,并且有解决方法?

Den*_*er9 5 performance ios scenekit scnnode

我在SceneKit应用程序中发现了一个主要的性能瓶颈,原因是该嵌套循环运行了数千次。在该循环中,除了这一行以外,有一堆代码非常愉快地缩放:

var scenePos = presentation.position
Run Code Online (Sandbox Code Playgroud)

这比只要求位置要慢100倍,再加上我在同一循环中所做的数十种其他计算,比较,数组查找和方法调用的组合。令我惊讶的是,似乎没有人对此发表评论。

为什么会这样,除了为每个节点的演示文稿制作副本之外,还有其他解决方法。为每个帧定位自己,这样您就不必一直向演示节点询问它了吗?谢谢。

编辑:presentation.position仅被读取,从未被写入。永远不会编辑boundingBox。我为几个SCNNode使用了动态SCNPhysicsBody,但绝大多数是静态的。

Den*_*er9 1

这是我当前正在使用的解决方法(为此,还有一个单独的问题,即如果从未为该节点运行物理,那么presentation.position 可能为零)。它的速度要快几个数量级,因为几乎所有节点都不是动态的。

// When you're not sure if physics has first run yet
func currentScenePos() -> SCNVector3 {
    if physicsBody?.type == .dynamic {
        var scenePos = presentation.position
        if scenePos.isZero() {
            // Looks like the physics hasn't run on this node yet. Use the regular position.
            // If that's zero too, it must really be at the scene origin.
            scenePos = position
        }
        return scenePos
    } else {
        // Non-dynamic physics. Just use position, it's a lot faster.
        return position
    }
}
Run Code Online (Sandbox Code Playgroud)