gpt4 book ai didi

swift - 即使我没有点击它,HitTest 也会打印 AR 实体名称

转载 作者:行者123 更新时间:2023-12-01 06:18:21 25 4
gpt4 key购买 nike

我的 Experience.rcproject 有可以通过点击操作触发的动画。两个圆柱体分别命名为“Button 1”和“Button 2”,并启用了 Collide。

我正在使用 Async 方法加载 Experience.Map 场景,并使用 addAnchor 方法将 mapAnchor 添加到 ViewController 中的 ARView。

我尝试在场景中运行 HitTest 以查看应用程序是否正确 react 。

尽管如此,HitTest 结果会打印按钮的实体名称,即使我没有点击按钮而是点击它附近的区域也是如此。

class augmentedReality: UIViewController {

@IBOutlet weak var arView: ARView!

@IBAction func onTap(_ sender: UITapGestureRecognizer) {
let tapLocation = sender.location(in: arView)
// Get the entity at the location we've tapped, if one exists
if let button = arView.entity(at: tapLocation) {
// For testing purposes, print the name of the tapped entity
print(button.name)
}
}
}

下面是我尝试将 AR 场景和点击手势识别器添加到 arView。

class augmentedReality: UIViewController {

arView.scene.addAnchor(mapAnchor)
mapAnchor.notifications.hideAll.post()
mapAnchor.notifications.mapStart.post()

self.arView.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onTap))
self.arView.addGestureRecognizer(tapGesture)
}

问题一

如何实现真正点击而不是靠近按钮时只打印按钮实体名称的目标?

问题二

我真的需要打开 Collide 才能在 HitTest 中检测到两个按钮吗?

问题三

有一个 installGestures 方法。目前没有关于此的在线教程或讨论。我试过了,但我对 (Entity & HasCollision) 感到困惑。这个方法如何实现?

最佳答案

enter image description here

To implement a robust Hit-Testing in RealityKit, all you need is the following code:

import RealityKit

class ViewController: UIViewController {

@IBOutlet var arView: ARView!
let scene = try! Experience.loadScene()

@IBAction func onTap(_ sender: UITapGestureRecognizer) {

let tapLocation: CGPoint = sender.location(in: arView)
let result: [CollisionCastHit] = arView.hitTest(tapLocation)

guard let hitTest: CollisionCastHit = result.first
else { return }

let entity: Entity = hitTest.entity
print(entity.name)
}

override func viewDidLoad() {
super.viewDidLoad()

scene.steelBox!.scale = [2,2,2]
scene.steelCylinder!.scale = [2,2,2]

scene.steelBox!.name = "BOX"
scene.steelCylinder!.name = "CYLINDER"

arView.scene.anchors.append(scene)
}
}

当您点击 ARView 中的实体时,调试区域会打印“BOX”或“CYLINDER”。如果您点击实体以外的任何东西,调试区域只会打印“Ground Plane”。

enter image description here

If you need to implement a Ray-Casting read this post, please.

附言

如果您在 macOS 模拟器上运行此应用程序,它只会打印 Ground Plane 而不是 BOXCYLINDER。所以你需要在 iPhone 上运行这个应用程序。

关于swift - 即使我没有点击它,HitTest 也会打印 AR 实体名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56736645/

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