我在SceneKit应用程序中追踪了一个主要的性能瓶颈,这个瓶子循环运行了几千次。在那个循环中是一堆代码,除了这一行之外,它非常愉快地放大:
var scenePos = presentation.position
它比仅仅要求位置PLUS的速度慢100多倍。我在同一个循环中进行了数十次其他计算,比较,数组查找和方法调用。我很惊讶没有人似乎对此发表评论但我能找到。
为什么会这样,并且除了制作每个节点的presentation.position自己的每个帧之外,还有一个解决方法,所以你不必继续向表示节点询问它吗?谢谢。
编辑:presentation.position只能被阅读,永远不会被写入。没有编辑任何boundingBox。我正在为一些SCNNodes使用动态SCNPhysicsBody,但绝大多数都是静态的。
这是我正在使用的解决方法(为此,以及如果从未为该节点运行物理,则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
}
}