it seemed to me that the procedure described in chapter 2 for loading an asset required that I know some things about the asset that I might not: namely the vertex descriptor.
On investigation I found that a different asset initialiser MDLAsset(url:) loads the asset and creates a MTLVertexDescriptor.
It seems (though I don’t know why!) that I still need to allocate a mesh buffer, so I then reloaded the asset using the code below, thus avoiding the 10 lines of code in the text needed to construct the vertex descriptor.
It works!! Am I missing something important here?
var mesh: MTKMesh!
func loadMesh() {
guard let assetURL = Bundle.main.url(forResource: "train",
withExtension: "obj")
else { fatalError() }
// make an asset to obtain its Model I/O vertex descriptor
var asset = MDLAsset(url: assetURL)
// remake it with a buffer allocator and the now known vertex descriptor
asset = MDLAsset(url: assetURL,
vertexDescriptor: asset.vertexDescriptor!,
bufferAllocator: MTKMeshBufferAllocator(device: Renderer.device))
let mdlMesh = asset.childObjects(of: MDLMesh.self).first as! MDLMesh
mesh = try MTKMesh(mesh: mdlMesh, device: Renderer.device)
}