I'm trying to generate UV map for a mesh using Model/IO. The code runs on the simulator and generates a UV map for the input mesh but when I run it on a device it crashes on
mdlMesh.addUnwrappedTextureCoordinates(forAttributeNamed: MDLVertexAttributeTextureCoordinate)
with this error displayed multiple times on the console:
Can't choose for edge creation
.
The fatal error that terminates the app is:
libc++abi: terminating with uncaught exception of type std::out_of_range: unordered_map::at: key not found
The code:
let asset = MDLAsset()
let allocator = MTKMeshBufferAllocator(device: device)
let zoneSize = MemoryLayout<Float>.size * 3 * mesh.vertices.count + MemoryLayout<UInt32>.size * indexCount
let zone = allocator.newZone(zoneSize)
let data = Data.init(bytes: vertexBuffer.contents(), count: MemoryLayout<Float>.size * 3 * mesh.vertices.count)
let vBuffer = allocator.newBuffer(from: zone, data: data, type: .vertex)!
let indexData = Data.init(bytes: indexBuffer.contents(), count: MemoryLayout<UInt32>.size * indexCount)
let iBuffer = allocator.newBuffer(from: zone, data: indexData, type: .index)!
let submesh = MDLSubmesh(indexBuffer: iBuffer,
indexCount: indexCount,
indexType: .uint32,
geometryType: .triangles,
material: nil)
let vDescriptor = MDLVertexDescriptor()
// Vertex Positions
vDescriptor.attributes[0] = MDLVertexAttribute(name: MDLVertexAttributePosition,
format: .float3,
offset: 0,
bufferIndex: 0)
vDescriptor.layouts[0] = MDLVertexBufferLayout(stride: MemoryLayout<Float>.size * 3)
let mdlMesh = MDLMesh(vertexBuffer: vBuffer,
vertexCount: mesh.vertices.count,
descriptor: vDescriptor,
submeshes: [submesh])
mdlMesh.addAttribute(withName: MDLVertexAttributeTextureCoordinate, format: .float2)
mdlMesh.addUnwrappedTextureCoordinates(forAttributeNamed: MDLVertexAttributeTextureCoordinate)
asset.add(mdlMesh)