激光雷达 | Swift |如何使用 LiDAR 扫描仪导出真实纹理 3d 模型

LiDAR | Swift | How to Export real texture 3d model using LiDAR Scanner

如何导出具有真实世界纹理的相同模型?

我们使用了许多演示和组合逻辑来导出具有真实纹理的 3d 模型 (usdz)。但是,我们仅在导出空白 (white/gray) 模型时获得成功。

我们使用以下代码导出 3d 模型。

func exportModel() {
        
        guard let camera = sceneView.session.currentFrame?.camera else {return}

        
        func convertToAsset(meshAnchors: [ARMeshAnchor]) -> MDLAsset? {
             
                guard let device = MTLCreateSystemDefaultDevice() else {return nil}
                
                let asset = MDLAsset()

            
            guard let frame = sceneView.session.currentFrame else { return nil }
            guard let cameraImage = captureCamera() else { return nil }

            for anchor in meshAnchors {
                
                
                // Below commented code help in showing preview model in sceneView
//                guard let node = sceneView.node(for: anchor) else { continue }
//                let geometry = scanGeometory(frame: frame, anchor: anchor, node: node, needTexture: true, cameraImage: cameraImage)
//                node.geometry = geometry
                
                
                let mdlMesh = anchor.geometry.toMDLMesh(device: device, camera: camera, modelMatrix: anchor.transform)
                asset.add(mdlMesh)
            }

            return asset
        }
        func export(asset: MDLAsset) throws -> URL {
            let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
            let url = directory.appendingPathComponent("scaned.usdc")

            try asset.export(to: url)

            return url
        }
        func share(url: URL) {
            let vc = UIActivityViewController(activityItems: [url],applicationActivities: nil)
            // vc.popoverPresentationController?.sourceView = sender
            self.present(vc, animated: true, completion: nil)
        }
        if let meshAnchors = sceneView.session.currentFrame?.anchors.compactMap({ [=11=] as? ARMeshAnchor }),
           let asset = convertToAsset(meshAnchors: meshAnchors) {
            do {
                let url = try export(asset: asset)
                share(url: url)
            } catch {
                print("export error")
            }
        }
    }

试试下面的代码:

let documentDirURL = try! FileManager.default.url(for: .documentDirectory,
                                                         in: .userDomainMask,
                                                         appropriateFor: nil,
                                                         create: true)

let filename = documentDirURL.appendingPathComponent("3d_Model.usdz")

self.sceneView.scene.write(to: filename, options: nil, delegate: nil, progressHandler: nil)

let activityController = UIActivityViewController(activityItems: [filename], applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)