【问题标题】:How to use geometry from scene file with multiple objects (geometries)?如何使用场景文件中的几何图形与多个对象(几何图形)?
【发布时间】:2018-01-28 10:49:18
【问题描述】:

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

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

  • 组(无几何元素)
    • Shape1(几何元素)
    • Shape2(几何元素)
    • Shape3(几何元素)

当我尝试使用该模型时,我无法将 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

一个节点只能附加一个几何图形。要组合几何图形以便一起控制或制作动画,请创建一个没有几何图形的节点并向其添加其他节点。

但它似乎没有工作,因为父节点的几何可选仍然为零。

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

我做错了什么?谢谢!

【问题讨论】:

    标签: ios swift 3d scenekit arkit


    【解决方案1】:

    SceneKit 不会自动为您合并 SCNGeometry 实例。为此,您必须在创建合并的 SCNGeometry 之前创建新的 SCNGeometrySourceSCNGeometryElement 对象。

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

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

    【讨论】:

    【解决方案2】:

    Regular SceneKit 的游戏模板包含ship.scn 文件。要访问节点的几何图形,只需为其子集合下标。为此,您需要使用childNodes[index] 实例属性:

    let scene = SCNScene(named: "art.scnassets/ship.scn")!
    let ship = scene.rootNode.childNode(withName: "ship", recursively: true)!
    
    print(ship.childNodes[0].geometry as Any)
    

    打印出来:

    //  Optional(<SCNGeometry: 0x6000033097c0 'Scrap_MeshShape'>)
    

    但是,如果你运行:

    print(ship.geometry as Any)
    

    打印出来:

    //  nil
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 1970-01-01
      • 1970-01-01
      • 2019-01-18
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      相关资源
      最近更新 更多