What is the real benefit of using Raycast in ARKit and RealityKit?

前端 未结 1 2047
庸人自扰
庸人自扰 2020-12-21 06:40

What is a ray-casting in ARKit and RealityKit for?

And when I need to use a makeRaycastQuery instance method:

func makeR         


        
相关标签:
1条回答
  • 2020-12-21 07:03

    Simple Ray-Casting, the same way as Hit-Testing, helps find a 3D position on a real-world surface by projecting an imaginary ray from a screen point. I've found a following definition of ray-casting in Apple documentation:

    Ray-casting is the preferred method for finding positions on surfaces in the real-world environment, but the hit-testing functions remain present for compatibility. With tracked ray-casting, ARKit continues to refine the results to increase the position accuracy of virtual content you place with a ray-cast.

    When the user wants to place a virtual content onto some surface, it's a good idea to have a tip for this. Many AR apps draw a focus circle or square that give the user visual confirmation of the shape and alignment of the surfaces that ARKit is aware of. So, to find out where to put a focus circle or a square in the real world, you may use an ARRaycastQuery to ask ARKit where any surfaces exist in the real world.

    Here's some abstract example where you could see ray-casting method makeRaycastQuery():

    import RealityKit
    
    class ViewController: UIViewController {
        
        @IBOutlet var arView: ARView!
        let model = try! Entity.load(named: "car")
    
        func rayCastingMethod() {
            
            // target iOS 13.0+, Xcode 11.0+
    
            guard let query = arView.makeRaycastQuery(from: arView.center,
                                                  allowing: .estimatedPlane,
                                                 alignment: .vertical) 
            else {
                return
            }
    
            guard let result = arView.session.raycast(query).first 
            
            else {
                return
            }
    
            let transform = Transform(matrix: result.worldTransform)
            model.transform = transform
    
            let raycastAnchor = AnchorEntity(raycastResult: result)
            raycastAnchor.addChild(model)
            arView.scene.anchors.append(raycastAnchor)
        }
    }
    

    If you wanna know how to use a Convex-Ray-Casting in RealityKit, read THIS POST.

    If you wanna know how to use Hit-Testing in RealityKit, read THIS POST.

    0 讨论(0)
提交回复
热议问题