我一直在玩 iOS 中的 MDLVoxelArray 功能,通过体素算法创建网格。
目前我像这样创建体素数组:
let minBounds = vector_float3(x: 0.0, y: 0.0, z: 0.0)
let maxBounds = vector_float3(x: Float(width), y: Float(height), z: Float(maxDepth))
let boundingBox = MDLAxisAlignedBoundingBox(maxBounds: maxBounds, minBounds: minBounds)
let voxels = MDLVoxelArray(data: Data(capacity: MemoryLayout<MDLVoxelIndex>.size * width * height * depth), boundingBox: boundingBox, voxelExtent: 1.0)
然后我用索引填充它:
var index = MDLVoxelIndex(x: 0, y: 0, z: 0, w: 0)
for i in 0 ..< height {
for j in 0 ..< width {
index.x = Int32(j)
index.y = Int32(i)
let depth = Int32(heightMap[j][i])
let midPoint = depth / 2
for k in 0 ... depth {
// w should be 0 at the surface and increase in magnitude towards the middle
let w = abs(k - midPoint) - midPoint
index.z = k
index.w = w
voxels.setVoxelAtIndex(index)
}
}
}
稍后我将 MDLVoxelArray 转换为网格并将其保存到 USDZ 文件中。它可以工作(实际上会产生非常好的结果),但它真的很慢,并且随着迭代的越来越远越来越慢。
我在网上找到的关于 MDLVoxelArrays 的示例/文档很少,所以我的问题是:
这是创建和填充体素数组的正确方法吗?我只是在猜测如何在填充Data
对象时给它一个初始容量,但我想这可能不正确。
任何关于如何分析它为什么慢的提示也将受到欢迎。我不确定当它们位于 Apple 的内部 API 中时,有哪些工具可用于诊断性能瓶颈?