–使用世界射线而不是屏幕点进行光线投射

–使用世界射线而不是屏幕点进行光线投射

本文介绍了ARKit –使用世界射线而不是屏幕点进行光线投射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现类似 ARCore的光线投射方法,它可以在世界空间坐标中获取任意光线,而不是屏幕空间点:

I want to achieve something similar like ARCore's raycast method which takes an arbitrary ray in world space coordinates instead of a screen-space point:

List<HitResult> hitTest (float[] origin3, int originOffset, float[] direction3, int directionOffset)

我看到ARKit本身没有那样的方法,但是无论如何有人可能有一个主意!

I see ARKit itself has not that method like that, but in any way maybe someone has an idea!

谢谢.

推荐答案

在Apple RealityKit 2.0和ARKit 4.0框架中,您可以找到三种主要类型的 Raycast 方法: ARView Raycast ARSession Raycast Scene Raycast (或World Raycast).所有用Swift编写的方法:

In Apple RealityKit 2.0 and ARKit 4.0 frameworks you can find three main types of Raycast methods: ARView Raycast, ARSession Raycast and Scene Raycast (or World Raycast). All methods written in Swift:


此实例方法执行射线投射,其中将射线从相机中心通过视图中的点投射到场景中,并立即返回结果.您可以在ARKit中使用这种类型的raycast.

This instance method performs a ray cast, where a ray is cast into the scene from the center of the camera through a point in the view, and the results are immediately returned. You can use this type of raycast in ARKit.

func raycast(from point: CGPoint,
        allowing target: ARRaycastQuery.Target,
              alignment: ARRaycastQuery.TargetAlignment) -> [ARRaycastResult]


此实例方法针对给定的原点,方向和长度的射线对场景中的所有几何体执行凸射线投射.

This instance method performs a convex ray cast against all the geometry in the scene for a ray of a given origin, direction, and length.

func raycast(origin: SIMD3<Float>,
          direction: SIMD3<Float>,
              query: CollisionCastQueryType,
               mask: CollisionGroup,
         relativeTo: Entity) -> [CollisionCastHit]


此实例方法会随着时间的推移重复射线投射查询,以通知您物理环境中的更新曲面.您可以在ARKit 3.5中使用这种类型的raycast.

This instance method repeats a ray-cast query over time to notify you of updated surfaces in the physical environment. You can use this type of raycast in ARKit 3.5.

func trackedRaycast(_ query: ARRaycastQuery,
              updateHandler: @escaping ([ARRaycastResult]) -> Void) -> ARTrackedRaycast?


此RealityKit的实例方法还执行跟踪的光线投射,但是这里是从相机的中心到视图中的点将光线投射到场景中.

This RealityKit's instance method also performs a tracked ray cast, but here a ray is cast into the scene from the center of the camera through a point in the view.

func trackedRaycast(from point: CGPoint,
               allowing target: ARRaycastQuery.Target,
                     alignment: ARRaycastQuery.TargetAlignment,
                 updateHandler: @escaping ([ARRaycastResult]) -> Void) -> ARTrackedRaycast?


import RealityKit

let startPosition: SIMD3<Float> = [3,-2,0]
let endPosition: SIMD3<Float> = [10,7,-5]
let query: CollisionCastQueryType = .all
let mask: CollisionGroup = .all

let raycasts: [CollisionCastHit] = arView.scene.raycast(from: startPosition,
                                                          to: endPosition,
                                                       query: query,
                                                        mask: mask,
                                                  relativeTo: nil)

guard let rayCast: CollisionCastHit = raycasts.first
else {
    return
}

代码段02:

import ARKit

let query = arView.raycastQuery(from: screenCenter,
                            allowing: .estimatedPlane,
                           alignment: .any)

let raycast = session.trackedRaycast(query) { results in

    if let result = results.first {
        object.transform = result.transform
    }
}

raycast.stop()

这篇关于ARKit –使用世界射线而不是屏幕点进行光线投射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:31