如何在RealityKit中使用Raycast方法?

问题描述 投票:0回答:1

RealityKit
框架中有三种检测交叉点的方法,但我不知道如何在我的项目中使用它。

1.

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

2.

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

3.

func convexCast(convexShape: ShapeResource, 
               fromPosition: SIMD3<Float>, 
            fromOrientation: simd_quatf, 
                 toPosition: SIMD3<Float>, 
              toOrientation: simd_quatf, 
                      query: CollisionCastQueryType, 
                       mask: CollisionGroup, 
                 relativeTo: Entity?) -> [CollisionCastHit]
swift augmented-reality arkit raycasting realitykit
1个回答
10
投票

简单的光线投射

如果您想了解如何使用光线投射方法将 Reality Composer 中制作的模型定位到 RealityKit 场景(具有检测到的水平面)中,请使用以下代码:

import RealityKit
import ARKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    let scene = try! Experience.loadScene()
    
    @IBAction func onTap(_ sender: UITapGestureRecognizer) {
        
        scene.steelBox!.name = "Parcel"
        
        let tapLocation: CGPoint = sender.location(in: arView)
        let estimatedPlane: ARRaycastQuery.Target = .estimatedPlane
        let alignment: ARRaycastQuery.TargetAlignment = .horizontal
                
        let result: [ARRaycastResult] = arView.raycast(from: tapLocation,
                                                   allowing: estimatedPlane,
                                                  alignment: alignment)
        
        guard let rayCast: ARRaycastResult = result.first
        else { return }
        
        let anchor = AnchorEntity(world: rayCast.worldTransform)
        anchor.addChild(scene)
        arView.scene.anchors.append(anchor)
        
        print(rayCast)
    }
}

上课专心听讲

ARRaycastQuery
。这个类来自 ARKit,而不是 RealityKit。

凸射线投射

raycast(from:to:query:mask:relativeTo:)
这样的凸射线投射方法是沿着直线滑动凸形状并在与场景中任何碰撞形状的第一个交叉点处停止的操作。场景
raycast()
方法对场景中具有
collision shapes
的所有实体执行命中测试。 没有碰撞形状的实体将被忽略

您可以使用以下代码从开始位置到结束位置执行凸射线投射:

import RealityKit

let startPosition: SIMD3<Float> = [0, 0, 0]
let endPosition: SIMD3<Float> = [5, 5, 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 }
    
print(rayCast.distance)      /* The distance from the ray origin to the hit */
print(rayCast.entity.name)   /* The entity's name that was hit              */

CollisionCastHit
结构是碰撞投射的命中结果,它存在于 RealityKit 的场景中。

附注

当您使用

raycast(from:to:query:mask:relativeTo:)
方法测量从相机到实体的距离时,ARCamera的方向是什么并不重要,只重要它的位置在世界坐标中的位置。

© www.soinside.com 2019 - 2024. All rights reserved.