在iOS中找到Submesh的边界框

时间:2019-06-25 09:13:22

标签: ios swift metal

我正在从obj文件绘制如下的3D模型。我需要为该obj文件的每个子网格找到边界框。

 let assetURL = Bundle.main.url(forResource: "train", withExtension: "obj")!
 let allocator = MTKMeshBufferAllocator(device: device)
 let asset = MDLAsset(url: assetURL, vertexDescriptor: vertexDescriptor, bufferAllocator: meshAllocator)
 let mdlMesh = asset.object(at: 0) as! MDLMesh

 mesh = try! MTKMesh(mesh: mdlMesh, device: device)

  for submesh in mesh.submeshes {
//I need to find the bounding box for each Submesh
        }

如何在iOS中实现这一目标。

1 个答案:

答案 0 :(得分:1)

这是一个需要使用MTKMesh并在模型空间中产生MDLAxisAlignedBoundingBox es数组的函数:

enum BoundingBoxError : Error {
    case invalidIndexType(String)
}

func boundingBoxesForSubmeshes(of mtkMesh: MTKMesh, positionAttributeData: MDLVertexAttributeData) throws -> [MDLAxisAlignedBoundingBox]
{
    struct VertexPosition {
        var x, y, z: Float
    }

    var boundingBoxes = [MDLAxisAlignedBoundingBox]()

    var minX = Float.greatestFiniteMagnitude
    var minY = Float.greatestFiniteMagnitude
    var minZ = Float.greatestFiniteMagnitude
    var maxX = -Float.greatestFiniteMagnitude
    var maxY = -Float.greatestFiniteMagnitude
    var maxZ = -Float.greatestFiniteMagnitude

    let positionsPtr = positionAttributeData.dataStart

    for submesh in mtkMesh.submeshes {
        let indexBuffer = submesh.indexBuffer
        let mtlIndexBuffer = indexBuffer.buffer
        let submeshIndicesRaw = mtlIndexBuffer.contents().advanced(by: indexBuffer.offset)

        if submesh.indexType != .uint32 {
            throw BoundingBoxError.invalidIndexType("Expected 32-bit indices")
        }

        let submeshIndicesPtr = submeshIndicesRaw.bindMemory(to: UInt32.self,
                                                             capacity: submesh.indexCount)
        let submeshIndices = UnsafeMutableBufferPointer<UInt32>(start: submeshIndicesPtr,
                                                                count:submesh.indexCount)

        for index in submeshIndices {
            let positionPtr = positionsPtr.advanced(by: Int(index) * positionAttributeData.stride)
            let position = positionPtr.assumingMemoryBound(to: VertexPosition.self).pointee

            if position.x < minX { minX = position.x }
            if position.y < minY { minY = position.y }
            if position.z < minZ { minZ = position.z }
            if position.x > maxX { maxX = position.x }
            if position.y > maxY { maxY = position.y }
            if position.z > maxZ { maxZ = position.z }
        }

        let min = SIMD3<Float>(x: minX, y: minY, z: minZ)
        let max = SIMD3<Float>(x: maxX, y: maxY, z: maxZ)
        let box = MDLAxisAlignedBoundingBox(maxBounds: max, minBounds: min)

        boundingBoxes.append(box)
    }

    return boundingBoxes
}

请注意,此功能希望子网格具有32位索引,尽管它也可以修改为支持16位索引。

要驱动它,您还需要提供一个MDLVertexAttributeData对象,该对象指向包含的网格中的顶点数据。操作方法如下:

let mesh = try! MTKMesh(mesh: mdlMesh, device: device)
let positionAttrData = mdlMesh.vertexAttributeData(forAttributeNamed: MDLVertexAttributePosition, as: .float3)!
let boundingBoxes = try? boundingBoxesForSubmeshes(of: mesh, positionAttributeData: positionAttrData)

我还没有彻底测试过此代码,但是在有限的测试用例集上似乎会产生合理的结果。用调试网格可视化这些框应立即显示它们是否正确。