swift - 如何在 RealityKit 中使用 Raycast 方法?

标签 swift augmented-reality arkit raycasting realitykit

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

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]

最佳答案

简单的光线转换

If you want to find out how to position a model made in Reality Composer into a RealityKit scene (that has a detected horizontal plane) using Ray-Casting method, use the following code:

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

凸射线转换

A Convex-Ray-Casting methods like raycast(from:to:query:mask:relativeTo:) is the op of swiping a convex shapes along a straight line and stopping at the very first intersection with any of the collision shape in the scene. Scene raycast() method performs a hit-tests against all entities with collision shapes in the scene. Entities without a collision shape are ignored.

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

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 的场景中

附言

When you use raycast(from:to:query:mask:relativeTo:) method for measuring a distance from camera to entity it doesn't matter what an orientation of ARCamera is, it only matters what its position is in world coordinates.

关于swift - 如何在 RealityKit 中使用 Raycast 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60294367/

相关文章:

swift - 获取 NSLayoutConstraint 标识符不适用于 topAnchor

string - 在 Swift 中比较时间字符串

ios - 如何确保 OBJ 模型以正确的方向显示?

swift - .DAE 或 .SCN 文件在 Xcode 中显示不正确

ios - 在添加新节点之前删除节点 Swift

json - swift/Alamofire : Iterating through JSON values to check for bool value?

ios - 苹果 RealityKit : How to draw a polygon plane using ModelEntity?

Android:带有 min3D 框架(或其他 3D 模型/动画加载器/渲染器)的 Qualcomm QCAR (Vuforia) SDK

swift - 如何在 iOS/ARKit 中控制 Apple ProMotion FPS?

ios - 工具栏上 UIBarButtonItem 右侧的图像图标 (iOS)