问题描述
我想实现类似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 中使用这种类型的光线投射.
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 中使用这种类型的光线投射.
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 – 使用世界射线而不是屏幕点进行光线投射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!