In am iOS app I am trying to load a SceneKit
.scn
file with a single quad in it and generate an MTKMesh
from it. No joy.
I load the SCNScene
file:
guard let scene = SCNScene(named:"scenes.scnassets/quad.scn") else {
fatalError("Error: Can not create SCNScene with \(sceneName)")
}
I traverse the scene graph to locate the quad:
guard let sceneNode = scene.rootNode.childNode(withName:"quadIdentity", recursively:true) else {
fatalError("Error: Can not create sceneNode")
}
I extract the quad geometry:
guard let sceneGeometry = sceneNode.geometry else {
fatalError("Error: Can not create sceneGeometry")
}
I create a Model I/O
mesh from the geometry:
let modelIOMesh = MDLMesh(scnGeometry:sceneGeometry, bufferAllocator:MTKMeshBufferAllocator(device:device))
I force the vertex description to be Metal
format:
let metalVertexDescriptor = MTLVertexDescriptor.xyz_n_st_vertexDescriptor()
let modelIOVertexDescriptor = MTKModelIOVertexDescriptorFromMetal(metalVertexDescriptor)
(modelIOVertexDescriptor.attributes[ 0 ] as! MDLVertexAttribute).name = MDLVertexAttributePosition
(modelIOVertexDescriptor.attributes[ 1 ] as! MDLVertexAttribute).name = MDLVertexAttributeNormal
(modelIOVertexDescriptor.attributes[ 2 ] as! MDLVertexAttribute).name = MDLVertexAttributeTextureCoordinate
modelIOMesh.vertexDescriptor = modelIOVertexDescriptor
This always fails with a fatal error
:
do {
mesh = try MTKMesh(mesh:modelIOMesh, device:device)
} catch {
fatalError("Error: Can not create Metal mesh")
}
What am I missing?
UPDATE 0
I changes the try/catch to print the error:
do {
mesh = try MTKMesh(mesh:modelIOMesh, device:device)
} catch let error as NSError {
print(error.localizedDescription)
}
The error is:
fatal error: unexpectedly found nil while unwrapping an Optional value