Posts

Post not yet marked as solved
1 Replies
665 Views
I've been playing around with the MDLVoxelArray functionality in iOS, to create a mesh algorithmically via voxels. Currently I create the voxel array like this: 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&lt;MDLVoxelIndex&gt;.size * width * height * depth), boundingBox: boundingBox, voxelExtent: 1.0) Then I populate it with indices: var index = MDLVoxelIndex(x: 0, y: 0, z: 0, w: 0) for i in 0 ..< height { &#9;&#9;for j in 0 ..< width { &#9;&#9;&#9;&#9;index.x = Int32(j) &#9;&#9;&#9;&#9;index.y = Int32(i) &#9;&#9;&#9;&#9;let depth = Int32(heightMap[j][i]) &#9;&#9;&#9;&#9;let midPoint = depth / 2 &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;for k in 0 ... depth { &#9;&#9;&#9;&#9;&#9;&#9;// w should be 0 at the surface and increase in magnitude towards the middle &#9;&#9;&#9;&#9;&#9;&#9;let w = abs(k - midPoint) - midPoint &#9;&#9;&#9;&#9;&#9;&#9;index.z = k &#9;&#9;&#9;&#9;&#9;&#9;index.w = w &#9;&#9;&#9;&#9;&#9;&#9;voxels.setVoxelAtIndex(index) &#9;&#9;&#9;&#9;} &#9;&#9;} } Later I then convert the MDLVoxelArray to a mesh and save it out to a USDZ file. It works (and produces pretty nice results, actually) but it's really slow and gets progressively slower and slower as it iterates further and further. I'm finding very few examples / little documentation about MDLVoxelArrays online, so my question is: Is this the right way to go about creating and populating the voxel array? I was just guessing at how to give it an initial capacity when populating the Data object, but am imagining that might not be correct. Any tips would also be welcome on how to go about profiling why it's slow. I'm not sure what tools are available for diagnosing performance bottlenecks when they're inside Apple's internal APIs like this?
Posted
by andygeers.
Last updated
.