问题
What is a ray-casting in ARKit and RealityKit for?
And when I need to use a makeRaycastQuery instance method:
func makeRaycastQuery(from point: CGPoint,
allowing target: ARRaycastQuery.Target,
alignment: ARRaycastQuery.TargetAlignment) -> ARRaycastQuery?
Any help appreciated.
回答1:
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 want to know how to use a
Hit-Testing
method in RealityKit, read THIS POST, please.
来源:https://stackoverflow.com/questions/56466773/what-is-the-real-benefit-of-using-raycast-in-arkit-and-realitykit