在我的 iOS 应用程序中,我试图加载一个SceneKit
.scn
包含单个四边形的文件并MTKMesh
从中生成一个。没有喜悦。
我加载SCNScene
文件:
guard let scene = SCNScene(named:"scenes.scnassets/quad.scn") else {
fatalError("Error: Can not create SCNScene with \(sceneName)")
}
我遍历场景图来定位四边形:
guard let sceneNode = scene.rootNode.childNode(withName:"quadIdentity", recursively:true) else {
fatalError("Error: Can not create sceneNode")
}
我提取四边形几何:
guard let sceneGeometry = sceneNode.geometry else {
fatalError("Error: Can not create sceneGeometry")
}
Model I/O
我从几何创建一个网格:
let modelIOMesh = MDLMesh(scnGeometry:sceneGeometry, bufferAllocator:MTKMeshBufferAllocator(device:device))
我强制顶点描述为Metal
格式:
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
这总是失败并显示fatal error
:
do {
mesh = try MTKMesh(mesh:modelIOMesh, device:device)
} catch {
fatalError("Error: Can not create Metal mesh")
}
我错过了什么?
更新 0
我更改了 try/catch 以打印错误:
do {
mesh = try MTKMesh(mesh:modelIOMesh, device:device)
} catch let error as NSError {
print(error.localizedDescription)
}
错误是:
致命错误:在展开可选值时意外发现 nil