OGeek|极客世界-中国程序员成长平台

标题: ios - 如何使用场景文件中的几何图形与多个对象(几何图形)? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:31
标题: ios - 如何使用场景文件中的几何图形与多个对象(几何图形)?

我有一个混合器文件,我已导出为 DAE/collada,然后使用 Xcode 转换为 Scenekit 的场景文件。我在使用场景文件中的几何图形时遇到问题。

场景文件(“model.scn”)非常基本:

当我尝试使用该模型时,我无法将 3 个形状的组合几何体用于与 SCNPhysicsBody 一起使用的 SCNGeometry。

我尝试了各种方法,但都不起作用:

方法 1

let scene = SCNScene(named: "art.scnassets/model.scn")!
let node = scene.rootNode.childNode(withName: "Group", recursively: true)!
guard let geo = node.geometry else { return }
// node.geometry is nil so returns here

let physicsBody = SCNPhysicsBody(type: .kinematic, shape: SCNPhysicsShape(geometry: geo))
// this is what I need the custom/aggregate geometry for

方法2

let scene = SCNScene(named: "art.scnassets/model.scn")!
let geoNode = SCNNode()
let node1 = scene.rootNode.childNode(withName: "Shape1", recursively: true)!
let node2 = scene.rootNode.childNode(withName: "Shape2", recursively: true)!
let node3 = scene.rootNode.childNode(withName: "Shape3", recursively: true)!
geoNode.addChildNode(node1)
geoNode.addChildNode(node2)
geoNode.addChildNode(node3)

// expected geoNode.geometry is not nil, but it is

根据Apple's documentation

A node can have only one geometry attached to it. To combine geometries so they can be controlled or animated together, create a node with no geometry and add other nodes to it.

但它似乎无法正常工作,因为父节点的几何选项仍然为零。

我想做的事情应该很简单,但我做错了。我想使用我的 dae/collada/scene 文件中的几何图形。我不想使用默认值(立方体、圆柱体、金字塔、球体、圆环等)。

我做错了什么?谢谢!



Best Answer-推荐答案


SceneKit 不会自动为您合并 SCNGeometry 实例。为此,您必须先创建新的 SCNGeometrySourceSCNGeometryElement 对象,然后再创建合并的 SCNGeometry

文档可能有点误导。它想说的是,如果您想同时操作多个几何图形,那么将多个节点分组在一个共同的父节点下会使事情变得更容易,因为操作这个单个节点会影响(平移、旋转、缩放)它的所有子节点。

请注意,在您的情况下,您可以使用 init(shapes:transforms显式合并多个 SCNPhysicsShape 实例。首先为每个几何体创建一个形状,然后创建组合形状。 transforms 参数将由 transform 组成分组在其共同父级下的节点数。

关于ios - 如何使用场景文件中的几何图形与多个对象(几何图形)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45778853/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4