RealityKit – 在检测到的平面上可视化网格

Kis*_*ala 2 augmented-reality swift arkit arcore realitykit

我想在检测到的平面上绘制网格点,如ARCore 视频链接所示,但我不知道如何实现。

你能帮我实现这个目标吗?提前致谢。

ARG*_*Geo 10

ARKit|中的经典网格可视化RealityKit

在这篇文章中,我想向您展示如何使用 ARKit/RealityKit 框架启用平面检测过程的“经典”可视化。但在这种情况下,我不保证您能够以视觉上令人愉悦的方式可视化网格,因为它是在 Google ARCore 中实现的。

但是,为了可视化检测水平和垂直平面的过程(类似于您在 ARCore 中看到的行为),您应该使用场景重建方法,即使用 LiDAR 扫描仪。但这是一个不同的故事,因为您必须使用金属来实现程序纹理,并为前缘设置软蒙版。

在此输入图像描述

这是一个代码:

import ARKit
import RealityKit

class Grid: Entity, HasModel, HasAnchoring {        
    var planeAnchor: ARPlaneAnchor
    var planeGeometry: MeshResource!
    
    init(planeAnchor: ARPlaneAnchor) {
        self.planeAnchor = planeAnchor
        super.init()
        self.didSetup()
    }
        
    fileprivate func didSetup() {            
        self.planeGeometry = .generatePlane(width: planeAnchor.extent.x,
                                            depth: planeAnchor.extent.z)
        var material = UnlitMaterial()
        material.color = .init(tint: .white.withAlphaComponent(0.999),
                            texture: .init(try! .load(named: "grid.png")))
        let model = ModelEntity(mesh: planeGeometry, materials: [material])
        model.position = [planeAnchor.center.x, 0, planeAnchor.center.z]
        self.addChild(model)
    }
    
    fileprivate func didUpdate(anchor: ARPlaneAnchor) {            
        self.planeGeometry = .generatePlane(width: anchor.extent.x,
                                            depth: anchor.extent.z)
        let pose: SIMD3<Float> = [anchor.center.x, 0, anchor.center.z]
        let model = self.children[0] as! ModelEntity
        model.position = pose
    }        
    required init() { fatalError("Hasn't been implemented yet") }
}
Run Code Online (Sandbox Code Playgroud)

ViewController.swift

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    var grids = [Grid]()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        arView.session.delegate = self
        
        let config = ARWorldTrackingConfiguration()
        config.planeDetection = [.horizontal, .vertical]
        arView.session.run(config)
    }
}
Run Code Online (Sandbox Code Playgroud)

ARSessionDelegate

extension ViewController: ARSessionDelegate {
    
    func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
        
        guard let planeAnchor = anchors.first as? ARPlaneAnchor else { return }
        let grid = Grid(planeAnchor: planeAnchor)
        grid.transform.matrix = planeAnchor.transform
        self.arView.scene.anchors.append(grid)
        self.grids.append(grid)
    }
    
    func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
        
        guard let planeAnchor = anchors[0] as? ARPlaneAnchor else { return }
        let grid: Grid? = grids.filter { grd in
            grd.planeAnchor.identifier == planeAnchor.identifier }[0]
        guard let updatedGrid: Grid = grid else { return }
        updatedGrid.transform.matrix = planeAnchor.transform
        updatedGrid.didUpdate(anchor: planeAnchor)
    }
}
Run Code Online (Sandbox Code Playgroud)

仅可以更新共面检测到的平面。

聚苯乙烯

如果您有兴趣Canonical Face Mask在 RealityKit 中可视化,请查看这篇文章