gpt4 book ai didi

swift - ARKit 3.5 – 如何使用 LiDAR 从新 iPad Pro 导出 OBJ?

转载 作者:行者123 更新时间:2023-12-03 09:18:56 25 4
gpt4 key购买 nike

如何导出 ARMeshGeometry新生成的SceneReconstruction最新 iPad Pro 上的 API .obj文件?

这是SceneReconstruction文档。

最佳答案

从苹果的 Visualising Scene Scemantics 开始示例应用程序,您可以检索 ARMeshGeometry来自框架中第一个 anchor 的对象。

导出数据的最简单方法是首先将其转换为 MDLMesh:

extension ARMeshGeometry {
func toMDLMesh(device: MTLDevice) -> MDLMesh {
let allocator = MTKMeshBufferAllocator(device: device);

let data = Data.init(bytes: vertices.buffer.contents(), count: vertices.stride * vertices.count);
let vertexBuffer = allocator.newBuffer(with: data, type: .vertex);

let indexData = Data.init(bytes: faces.buffer.contents(), count: faces.bytesPerIndex * faces.count * faces.indexCountPerPrimitive);
let indexBuffer = allocator.newBuffer(with: indexData, type: .index);

let submesh = MDLSubmesh(indexBuffer: indexBuffer,
indexCount: faces.count * faces.indexCountPerPrimitive,
indexType: .uInt32,
geometryType: .triangles,
material: nil);

let vertexDescriptor = MDLVertexDescriptor();
vertexDescriptor.attributes[0] = MDLVertexAttribute(name: MDLVertexAttributePosition,
format: .float3,
offset: 0,
bufferIndex: 0);
vertexDescriptor.layouts[0] = MDLVertexBufferLayout(stride: vertices.stride);

return MDLMesh(vertexBuffer: vertexBuffer,
vertexCount: vertices.count,
descriptor: vertexDescriptor,
submeshes: [submesh]);
}
}


一旦你有了 MDLMesh,导出到 OBJ 文件就变得轻而易举了:
    @IBAction func exportMesh(_ button: UIButton) {
let meshAnchors = arView.session.currentFrame?.anchors.compactMap({ $0 as? ARMeshAnchor });

DispatchQueue.global().async {

let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
let filename = directory.appendingPathComponent("MyFirstMesh.obj");

guard let device = MTLCreateSystemDefaultDevice() else {
print("metal device could not be created");
return;
};

let asset = MDLAsset();

for anchor in meshAnchors! {
let mdlMesh = anchor.geometry.toMDLMesh(device: device);
asset.add(mdlMesh);
}

do {
try asset.export(to: filename);
} catch {
print("failed to write to file");
}
}
}

关于swift - ARKit 3.5 – 如何使用 LiDAR 从新 iPad Pro 导出 OBJ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61063571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com