ios - 在 ARKIT 中获取所有聚焦相机的 ARAnchors
<p><blockquote>
<p>When application launched first a vertical surface is detected on one wall than camera focus to the second wall, in the second wall another surface is detected. The first wall is now no more visible to the ARCamera but this code is providing me the anchors of the first wall. but I need anchors of the second wall which is right now Visible/focused in camera.</p>
</blockquote>
<pre><code>if let anchor = sceneView.session.currentFrame?.anchors.first {
let node = sceneView.node(for: anchor)
addNode(position: SCNVector3Zero, anchorNode: node)
} else {
debugPrint("anchor node is nil")
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>答案的线索在你的 <code>if let</code> 语句的开始行。</p>
<p>让我们分解一下:</p>
<p>当你说 <code>let anchor = sceneView.session.currentFrame?.anchors.first</code> 时,你是在引用一个可选的 <code>ARAnchor</code> 数组,它自然可以有多个元素。</p>
<p>因为你总是调用 <code>first</code> 例如index ,您将始终获得添加到数组中的第一个 <code>ARAnchor</code>。 </p>
<p>由于您现在有 2 个 anchor ,因此您自然需要最后一个(最新的)元素。因此,您可以尝试以下方法:</p>
<pre><code>if let anchor = sceneView.session.currentFrame?.anchors.last {
let node = sceneView.node(for: anchor)
addNode(position: SCNVector3Zero, anchorNode: node)
} else {
debugPrint("anchor node is nil")
}
</code></pre>
<p><strong>更新:</strong>
由于另一位发帖人对这个问题的解释不同,因为他们认为问题是<em>我如何检测 ARPlaneAnchor 是否在视野中?</em>让我们换一种方式来处理它。</p>
<p>首先我们需要考虑到 ARCamera 有一个 Frostrum 用于显示我们的内容:</p>
<p> <a href="/image/eAPBs.png" rel="noreferrer noopener nofollow"><img src="/image/eAPBs.png" alt="enter image description here"/></a> </p>
<p>因此,我们需要确定 ARPlaneAnchor 是否是<code>inViewOfFrostrum</code>。</p>
<p>首先我们将创建 2 个变量:</p>
<pre><code>var planesDetected = ()
var planeID: Int = 0
</code></pre>
<p>第一个存储 <code>ARPlaneAnchor</code> 及其关联的 <code>SCNNode</code>,第二个用于为每个平面提供唯一 ID。</p>
<p>在 <code>ARSCNViewDelegate</code> 中,我们可以可视化一个 ARPlaneAnchor,然后存储它的信息,例如:</p>
<pre><code>func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
//1. Get The Current ARPlaneAnchor
guard let anchor = anchor as? ARPlaneAnchor else { return }
//2. Create An SCNode & Geometry To Visualize The Plane
let planeNode = SCNNode()
let planeGeometry = SCNPlane(width: CGFloat(anchor.extent.x), height: CGFloat(anchor.extent.z))
planeGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
planeNode.geometry = planeGeometry
//3. Set The Position Based On The Anchors Extent & Rotate It
planeNode.position = SCNVector3(anchor.center.x, anchor.center.y, anchor.center.z)
planeNode.eulerAngles.x = -.pi / 2
//4. Add The PlaneNode To The Node & Give ItA Unique ID
node.addChildNode(planeNode)
planeNode.name = String(planeID)
//5. Store The Anchor & Node
planesDetected = planeNode
//6. Increment The Plane ID
planeID += 1
}
</code></pre>
<p>现在我们已经存储了检测到的平面,那么我们当然需要确定其中是否有任何一个在 ARCamera 的视野中,例如:</p>
<pre><code>/// Detects If An Object Is In View Of The Camera Frostrum
func detectPlaneInFrostrumOfCamera(){
//1. Get The Current Point Of View
if let currentPointOfView = augmentedRealityView.pointOfView{
//2. Loop Through All The Detected Planes
for anchorKey in planesDetected{
let anchor = anchorKey.value
if augmentedRealityView.isNode(anchor, insideFrustumOf: currentPointOfView){
print("ARPlaneAnchor With ID \(anchor.name!) Is In View")
}else{
print("ARPlaneAnchor With ID \(anchor.name!) Is Not In View")
}
}
}
}
</code></pre>
<p>最后我们需要访问这个函数,我们可以在下面的 <code>delegate</code> 方法中执行,例如 <code>renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval)</code>:</p>
<pre><code>func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
detectPlaneInFrostrumOfCamera()
}
</code></pre>
<p>希望这两个都指向正确的方向...</p></p>
<p style="font-size: 20px;">关于ios - 在 ARKIT 中获取所有聚焦相机的 ARAnchors,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/49363187/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/49363187/
</a>
</p>
页:
[1]