在哪种情况下,ARSCNView.raycastQuery返回nil? [英] In which cases ARSCNView.raycastQuery returns nil?
问题描述
在渲染器委托中,我从视图中心创建一个raycast查询,以跟踪估计的平面并显示跟随raycast结果的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值.我了解raycast结果可能为空,但是为什么不创建查询?
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
}
}
}
}
推荐答案
可返回的 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
?
这是您的代码外观:
@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)
}
}
这是获取模型的方法:
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屋!