I have tried all APIs for loading a SceneKit .scn file to create an MTKMesh. All generate an error and do not work at all. Can someone at Apple please give us status on when these will work properly?
The key API that refuses to work is: MTKMesh(mesh:,device:)
When loaded with a valid asset and device it generates the following error:
unexpectedly found nil while unwrapping an Optional value
I have used two paths to create the MDLMesh that feeds this method:
1)
let asset = MDLAsset(scnScene:scene, bufferAllocator:MTKMeshBufferAllocator(device:device))
let kids = asset.childObjects(of:MDLMesh.self)
let modelIOMesh:MDLMesh = kids[ 0 ] as! MDLMesh2)
guard let sceneNode = scene.rootNode.childNode(withName:nodeName, recursively:true) else {
fatalError("Error: Can not create sceneNode")
}
guard let sceneGeometry = sceneNode.geometry else {
fatalError("Error: Can not create sceneGeometry")
}
let modelIOMesh = MDLMesh(scnGeometry:sceneGeometry, bufferAllocator:MTKMeshBufferAllocator(device:device))In each cast the resultant MDLMesh is fed to MTKMesh(mesh:,device:) and generates an error.
All data is valid and inspected in the debugger.
1) The buffer backing an
MTKMeshBuffer is an MTLBuffer, so you would create an MTLBuffer of the appropriate size, call map() on the MDLMeshBuffer to get a pointer to its contents, and copy those bytes into the MTLBuffer.2) In the case of the primitive type and index type, those are the same types used in Metal (
MTLVertexType and MTLIndexType), but you'll need to map the corresponding constants from Model I/O (e.g., MDLIndexBitDepth -> MTLIndexType). For the index buffer, you can use the approach described in (1) above.