从SceneKit中的SCNNode中提取几何信息

时间:2016-08-12 23:50:48

标签: ios swift scenekit scnnode

我正在尝试为场景中的每个节点提取几何体。我使用.obj文件创建一个场景,它渲染得很完美。但是,我想从每个节点中提取几何体但我被卡住了。我的代码在

之下
    let scn = SCNScene(named: "d.obj")

    for i in scn!.rootNode.childNodes {
        for a in i.childNodes {
            for b in a.childNodes {
                let element = b.geometry!.geometryElementAtIndex(0)
                let source = b.geometry!.geometrySources[0]
                var z: Float = 0
                source.data.getBytes(&z, length: sizeof(Float))
                print(z)
            }
        }

}

我想获得位置和法线,以便将它们存储在数据库中。

1 个答案:

答案 0 :(得分:1)

在您的代码中,您使用let source = b.geometry!.geometrySources[0]

我建议您遍历所有来源:b.geometry!.geometrySources

然后在循环中测试以查看它是什么类型的来源:b.geometry!.geometrySources[i].semantic

.semantic属性可能有几个不同的值:

 NSString *const SCNGeometrySourceSemanticVertex;
 NSString *const SCNGeometrySourceSemanticNormal;
 NSString *const SCNGeometrySourceSemanticColor;
 NSString *const SCNGeometrySourceSemanticTexcoord;
 NSString *const SCNGeometrySourceSemanticVertexCrease;
 NSString *const SCNGeometrySourceSemanticEdgeCrease;
 NSString *const SCNGeometrySourceSemanticBoneWeights;
 NSString *const SCNGeometrySourceSemanticBoneIndices;

然后,根据来源的类型,做任何你想做的事情!