Import .obj file to Model I/O

I'm trying to import a .obj file to use in Scene Kit using the Model I/O framework. I initially used the simple MDLAsset initWithURL: function, but after transferring the mesh to a SCNGeometry, I realized this function was duplicating vertices such that there were 3 vertices per face, each of which did not belong to another face. In other words there were 3 * numberOfFaces vertices, when in reality there should be fewer vertices than faces, with each vertex belonging to multiple faces. This duplication was causing some major problems with my other functions, so I tried to fix it by instead using the MDLAsset initWithURL:vertexDescriptor:bufferAllocator:preserveTopology function with preserveTopology set to YES. This preserving topology fixed my problem of duplicating vertices but in the process I lost the normals. I first tried setting vertexDescriptor and bufferAllocator to nil to use the defaults, but when looking at the SCNGeometry, I ended up with 2 SCNGeometrySources, one with semantic vertex, and one with semantic normal, but each was identical (each contained the proper vertex position values) and was double the size it should have been, almost as if the normals were stored in the 2nd half of the buffer, although this was not the case.


I looked up some things about .obj files and it appeared that they are stored (assuming not materials) in a v1/n1/v2/n2... format. When i opened my .obj file in a text editor, it appeared to be in a v1/v2/.../n1/n2... format, although as I said, the 2nd half of the vertex buffer did not contain the normal data I was looking for.


So I figured there must be something wrong with the default vertex descriptor and buffer allocator (since I was using nil) and went about trying to create my own that matched these 2 possible data formats. Alas after much trying I was unable to get something that worked.


To be clear, I don't actually care what format the vertex and normal data come out in, I just need to know what it is, since I'm actually copying this data out into my own custom mesh class to run algorithms on it, and then put it back into a new SCNGeometry. I just need to know the format of the SCNGeometrySource(s) that get produced so I can copy the data out properly. I realize this might not be the most efficient way to do this but it did work when using the simple initWithURL: function with the only problem being the duplicated vertices.


Any ideas on how I should do this? How to give MDLAsset the proper vertexDescriptor and bufferAllocator (I feel like nil should be ok here) for importing a .obj file? Thanks

to clarify, when I say that, after setting preserveTopology:Yes, the normals were not stored in the second half of either buffer, I mean that with preserveTopology:NO, BOTH geometry sources (semantic vertex and semantic normal) were v1/n1/v2/n2..., and after setting it to YES, they were both v1/v2/... for the first half of the buffer, and all 0 vectors for the 2nd half. The normal vectors were nowhere to be found in the buffers. I checked the model i/o buffers to make sure the problem was with the init call and not the model i/o to scenekit bridge and in fact there was the same problem of missing normal values.

Import .obj file to Model I/O
 
 
Q