即使没有点击,HitTest也会打印AR实体名称 [英] HitTest prints AR Entity name even when I am not tapping on it

查看:190
本文介绍了即使没有点击,HitTest也会打印AR实体名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Experience.rcproject具有可以通过点击操作触发的动画. 两个液压缸分别命名为按钮1"和按钮2",并且已打开碰撞.

My Experience.rcproject has animations that can be triggered by tap action. Two cylinders are named "Button 1" and "Button 2" and have Collide turned on.

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

I am using Async method to load Experience.Map scene and addAnchor method to add mapAnchor to ARView in a ViewController.

我试图在现场运行HitTest,以查看应用程序是否反应正常.

I tried to run HitTest on the scene to see if the app reacts properly.

尽管如此,即使我没有点击按钮但在按钮附近的区域,HitTest结果仍会打印按钮的实体名称.

Nonetheless, the HitTest result prints the entity name of a button even when I am not tapping on it but area near it.

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)
        }
    }
}

下面是我尝试向ARView添加AR场景并点击手势识别器.

Below is my attempt to add the AR scene and tap gesture recogniser to 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

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

How can I achieve the goal of only having the entity name of a button printed when I am really tapping on it instead of close to it?

问题2

我是否真的需要打开碰撞"功能才能在HitTest中检测到两个按钮?

Do I actually need to turn Collide on to have both buttons able to be detected in the HitTest?

问题3

有一个installGestures方法.目前尚无与此相关的在线教程或讨论.我尝试过,但对(Entity& HasCollision)感到困惑.该方法如何实现?

There’s an installGestures method. There’s no online tutorials or discussions about this at the moment. I tried but I am confused by (Entity & HasCollision). How can this method be implemented?

推荐答案

要在RealityKit中实现健壮的Hit-Testing,您所需要做的只是以下代码:

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 ".而且,如果您点击除实体以外的任何内容,则调试区域"仅打印"地平面".

When you tap on entities in ARView a Debug Area prints "BOX" or "CYLINDER". And if you tap anything but entities, a Debug Area prints just "Ground Plane".

如果您需要实施光线投射,请阅读 PS

如果您在macOS Simulator上运行此应用程序,它将仅打印Ground Plane而不是BOXCYLINDER.因此,您需要在iPhone上运行此应用.

In case you run this app on macOS Simulator, it prints just Ground Plane instead of BOX and CYLINDER. So you need to run this app on iPhone.

这篇关于即使没有点击,HitTest也会打印AR实体名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆