Swift HitTest打印AR实体名称,即使我没有点击它
我的Swift HitTest打印AR实体名称,即使我没有点击它,swift,augmented-reality,arkit,uitapgesturerecognizer,realitykit,Swift,Augmented Reality,Arkit,Uitapgesturerecognizer,Realitykit,我的体验.rcproject具有可通过点击操作触发的动画。 两个圆柱体分别命名为“按钮1”和“按钮2”,并已启用“碰撞” 我正在使用Async方法加载体验。映射场景和addAnchor方法将mapAnchor添加到ViewController中的ARView 我试着在现场运行HitTest,看看应用程序是否反应正常 尽管如此,HitTest结果打印按钮的实体名称,即使我没有点击按钮,而是点击按钮附近的区域 class augmentedReality: UIViewController {
体验.rcproject
具有可通过点击操作触发的动画。
两个圆柱体分别命名为“按钮1”和“按钮2”,并已启用“碰撞”
我正在使用Async
方法加载体验。映射场景和addAnchor
方法将mapAnchor添加到ViewController中的ARView
我试着在现场运行HitTest,看看应用程序是否反应正常
尽管如此,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)
}
问题1
当我真的点击按钮而不是靠近按钮时,如何实现只打印按钮的实体名称的目标
问题2
我真的需要打开Collide才能在HitTest中检测到这两个按钮吗
问题3
有一种方法。目前还没有关于这方面的在线教程或讨论。我试过了,但我被(实体和冲突)弄糊涂了。如何实现此方法?
要在RealityKit中实现健壮的命中测试
,您只需要以下代码:
点击ARView中的实体时,调试区域会打印“框”或“圆柱体”。如果点击除实体以外的任何内容,调试区域只打印“地平面”
如果需要实现光线投射读取,请
p.S.
如果您在macOS模拟器上运行此应用程序,它只打印地平面
,而不是框
和圆柱体
。所以你需要在iPhone上运行这个应用程序
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)
}
}