3

I am trying to extract the geometry for each node in my scene. I create a scene using a .obj file and it renders perfectly. I would, however, like to extract the geometry from each node but I am stuck. My code is below

    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)
            }
        }

}

I would like to get the positions and normals so I can store them in a database.

1 Answers1

2

In your code you use let source = b.geometry!.geometrySources[0].

I suggest that you loop through all the sources: b.geometry!.geometrySources

Then in the loop you test to see what kind of source it is: b.geometry!.geometrySources[i].semantic

This .semantic property could be few different values:

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

Then, depending on the type of source, do whatever you want!

Pro Blaster
  • 491
  • 3
  • 9