gpt4 book ai didi

swift - RealityKit——在检测到的平面上可视化网格

转载 作者:行者123 更新时间:2023-12-05 09:02:29 26 4
gpt4 key购买 nike

我想在检测到的平面上绘制网格点,如 ARCore video link 中所示我不知道如何实现它。

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

最佳答案

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

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

但是,为了可视化检测水平和垂直平面的过程类似于您在 ARCore 中看到的行为,您应该使用场景重建方法,即使用 LiDAR 扫描仪。但那是另一回事,因为您必须使用 Metal 来实现前边缘带有软 mask 的程序纹理。

enter image description here

代码如下:

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.

如果您有兴趣在 RealityKit 中可视化 Canonical Face Masktake a look at this post .

关于swift - RealityKit——在检测到的平面上可视化网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71347327/

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