swift 如何在RealityKit中使用光线投射方法?

wvyml7n5  于 2022-12-22  发布在  Swift
关注(0)|答案(1)|浏览(195)

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

func raycast(origin: SIMD3<Float>, 
          direction: SIMD3<Float>, 
             length: Float, 
              query: CollisionCastQueryType, 
               mask: CollisionGroup, 
         relativeTo: Entity?) -> [CollisionCastHit]
func raycast(from: SIMD3<Float>, 
               to: SIMD3<Float>, 
            query: CollisionCastQueryType, 
             mask: CollisionGroup, 
       relativeTo: Entity?) -> [CollisionCastHit]
func convexCast(convexShape: ShapeResource, 
               fromPosition: SIMD3<Float>, 
            fromOrientation: simd_quatf, 
                 toPosition: SIMD3<Float>, 
              toOrientation: simd_quatf, 
                      query: CollisionCastQueryType, 
                       mask: CollisionGroup, 
                 relativeTo: Entity?) -> [CollisionCastHit]
xqnpmsa8

xqnpmsa81#

简单的光线投射

如果要了解如何使用光线投射方法将在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的方向无关紧要,重要的是它在世界坐标系中的位置

相关问题