在 ARKit 和 RealityKit 中使用 Raycast 的真正好处是什么? [英] What is the real benefit of using Raycast in ARKit and RealityKit?

查看:26
本文介绍了在 ARKit 和 RealityKit 中使用 Raycast 的真正好处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ARKit 和 RealityKit 中的光线投射是什么?

当我需要使用 makeRaycastQuery 实例方法时:

func makeRaycastQuery(from point: CGPoint,允许目标:ARRaycastQuery.Target,对齐:ARRaycastQuery.TargetAlignment)->ARRaycastQuery?

感谢任何帮助.

解决方案

Simple Ray-Casting,与 Hit-Testing 相同,有助于在通过将假想的光线从屏幕点投射到检测到的平面上来获得真实世界的表面.在 Apple 文档 (2019) 中,光线投射有以下定义:

<块引用>

光线投射是在现实环境中寻找表面位置的首选方法,但为了兼容性,命中测试功能仍然存在.通过跟踪光线投射,ARKit 和 RealityKit 会继续优化结果,以提高您使用光线投射放置的虚拟内容的位置准确性.

当用户想要将虚拟内容放置在检测到的表面上时,最好为此提供提示.许多 AR 应用程序会绘制一个焦点圆或正方形,让用户可以直观地确认 ARKit 所知道的表面的形状和对齐方式.因此,要找出在现实世界中放置焦点圆或正方形的位置,您可以使用 ARRaycastQuery 询问 ARKit 在现实世界中的任何表面.

这是一个示例,您可以在其中了解如何实现光线投射方法 makeRaycastQuery()raycast():

导入 UIKit导入 RealityKit类视图控制器:UIViewController {@IBOutlet var arView:ARView!让模型=尝试!Entity.loadModel(命名为:usdzModel")覆盖 func touchesBegan(_ touches: Set, with event: UIEvent?) {self.raycasting()}fileprivate func raycasting() {守卫让查询 = arView.makeRaycastQuery(from: arView.center,允许:.estimatedPlane,对齐:.水平)否则{返回}守卫让结果 = arView.session.raycast(query).first否则{返回}让 raycastAnchor = AnchorEntity(raycastResult: result)raycastAnchor.addChild(模型)arView.scene.anchors.append(raycastAnchor)}}

<块引用>

如果您想知道如何在 RealityKit 中使用 Convex-Ray-Casting,请阅读 这篇文章.

<块引用>

如果您想知道如何在 RealityKit 中使用 Hit-Testing,请阅读 这篇文章.

What is a ray-casting in ARKit and RealityKit for?

And when I need to use a makeRaycastQuery instance method:

func makeRaycastQuery(from point: CGPoint, 
                 allowing target: ARRaycastQuery.Target, 
                       alignment: ARRaycastQuery.TargetAlignment) -> ARRaycastQuery?

Any help appreciated.

解决方案

Simple Ray-Casting, the same way as Hit-Testing, helps find a 3D position on a real-world surface by projecting an imaginary ray from a screen point onto detected plane. In Apple documentation (2019) there's a following definition of ray-casting:

Ray-casting is the preferred method for finding positions on surfaces in the real-world environment, but the hit-testing functions remain present for compatibility. With tracked ray-casting, ARKit and RealityKit continues to refine the results to increase the position accuracy of virtual content you place with a ray-cast.

When the user wants to place a virtual content onto detected surface, it's a good idea to have a tip for this. Many AR apps draw a focus circle or square that give the user visual confirmation of the shape and alignment of the surfaces that ARKit is aware of. So, to find out where to put a focus circle or a square in the real world, you may use an ARRaycastQuery to ask ARKit where any surfaces exist in the real world.

Here's an example where you could see how to implement ray-casting methods makeRaycastQuery() and raycast():

import UIKit
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    let model = try! Entity.loadModel(named: "usdzModel")
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.raycasting()
    }

    fileprivate func raycasting() {
            
        guard let query = arView.makeRaycastQuery(from: arView.center,
                                              allowing: .estimatedPlane,
                                             alignment: .horizontal)
        else { return }

        guard let result = arView.session.raycast(query).first
        else { return }

        let raycastAnchor = AnchorEntity(raycastResult: result)
        raycastAnchor.addChild(model)
        arView.scene.anchors.append(raycastAnchor)
    }
}

If you wanna know how to use a Convex-Ray-Casting in RealityKit, read THIS POST.

If you wanna know how to use Hit-Testing in RealityKit, read THIS POST.

这篇关于在 ARKit 和 RealityKit 中使用 Raycast 的真正好处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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