经典网格可视化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") }
}
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)
}
}
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)
}
}
仅可以更新共面检测到的平面。
P. S.
如果您有兴趣可视化Canonical Face Mask
在现实工具包中,看看这个帖子.