在哪些情况下 ARSCNView.raycastQuery 返回 nil? [英] In which cases ARSCNView.raycastQuery returns nil?
问题描述
在我的渲染器委托中,我从视图中心创建了一个光线投射查询,以跟踪估计平面并显示跟随光线投射结果的 3D 指针.
In my renderer delegate I create a raycast query from the center of the view to track estimated plane and display a 3D pointer that follows the raycast result.
它是通过 view.raycastQuery(from:allowing:alignment:)
完成的,但返回 nil.
It is done via view.raycastQuery(from:allowing:alignment:)
but is returns nil.
我的问题是为什么?没有文档说明为什么这个函数会返回一个 nil 值.我知道光线投射结果可能为空,但为什么不创建查询?
My question is why ? There is no documentation that says why this function would returns a nil value. I understand the raycast results could be empty, but why a query would not be created ?
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
guard view.session.currentFrame?.camera.trackingState == .normal else {
return
}
DispatchQueue.main.async {
let center = view.center
DispatchQueue.global(qos: .userInitiated).async {
if let query = view.raycastQuery(from: center, allowing: .estimatedPlane, alignment: .any) {
let results = view.session.raycast(query)
...
}
else {
// sometimes it gets here
}
}
}
}
推荐答案
Returnable ARRaycastQuery?
最初是可选的.
根据 Apple 文档:
raycastQuery(from:allowing:alignment:)
实例方法创建一条射线,该射线从参数屏幕空间点沿正 z 方向延伸,以确定是否有任何参数目标存在于沿射线的任何物理环境中.如果是,ARKit 会返回光线与目标相交的 3D 位置.
raycastQuery(from:allowing:alignment:)
instance method creates a ray that extends in the positive z-direction from the argument screen space point, to determine if any of the argument targets exist in the physical environment anywhere along the ray. If so, ARKit returns a 3D position where the ray intersects the target.
如果射线不与目标相交,则没有 ARRaycastQuery?
对象.所以有 nil
.
If a ray doesn't intersect a target, there's no ARRaycastQuery?
object. So there's nil
.
func raycastQuery(from point: CGPoint,
allowing target: ARRaycastQuery.Target,
alignment: ARRaycastQuery.TargetAlignment) -> ARRaycastQuery?
返回nil
的两个可能原因是什么?
- 没有检测到射线击中的平面
- 代码逻辑错误
- There's no detected plane where ray hits
- Code's logic is wrong
What are two possible reasons of returning nil
?
您的代码可能如下所示:
Here's how you code may look like:
@IBOutlet var sceneView: ARSCNView!
@IBAction func onTap(_ sender: UIGestureRecognizer) {
let tapLocation: CGPoint = sender.location(in: sceneView)
let estimatedPlane: ARRaycastQuery.Target = .estimatedPlane
let alignment: ARRaycastQuery.TargetAlignment = .any
let query: ARRaycastQuery? = sceneView.raycastQuery(from: tapLocation,
allowing: estimatedPlane,
alignment: alignment)
if let nonOptQuery: ARRaycastQuery = query {
let result: [ARRaycastResult] = sceneView.session.raycast(nonOptQuery)
guard let rayCast: ARRaycastResult = result.first
else { return }
self.loadGeometry(rayCast)
}
}
这是一种获取模型的方法:
And here's a method for getting a model:
func loadGeometry(_ result: ARRaycastResult) {
let scene = SCNScene(named: "art.scnassets/myScene.scn")!
let node: SCNNode? = scene.rootNode.childNode(withName: "model",
recursively: true)
node?.position = SCNVector3(result.worldTransform.columns.3.x,
result.worldTransform.columns.3.y,
result.worldTransform.columns.3.z)
self.sceneView.scene.rootNode.addChildNode(node!)
}
这篇关于在哪些情况下 ARSCNView.raycastQuery 返回 nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!