gpt4 book ai didi

ios - 在 ARKIT 中获取聚焦相机的所有 ARAnchors

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:36:16 33 4
gpt4 key购买 nike

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.

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")
}

最佳答案

答案的线索在 if let 语句的开头。

让我们分解一下:

当你说 let anchor = sceneView.session.currentFrame?.anchors.first 时,你正在引用一个可选的 ARAnchor 数组,它自然可以有多个元素。

因为你总是调用 first 例如index [0],您将始终获得添加到数组中的第一个 ARAnchor

既然你现在有 2 个 anchor ,你自然需要最后一个(最新的)元素。因此,您可以先试试这个:

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")
}

更新:由于另一位发帖人对这个问题有不同的解释,因为他们认为问题是我如何检测 ARPlaneAnchor 是否在视野中?让我们换一种方式来解决这个问题。

首先,我们需要考虑到 ARCamera 有一个显示我们内容的 Frostrum:

enter image description here

因此,我们需要确定 ARPlaneAnchor 是否在inViewOfFrostrum

首先我们将创建 2 个变量:

var planesDetected = [ARPlaneAnchor: SCNNode]()
var planeID: Int = 0

第一个用于存储 ARPlaneAnchor 及其关联的 SCNNode,第二个用于为每个平面提供唯一 ID。

ARSCNViewDelegate 中,我们可以可视化一个 ARPlaneAnchor 然后存储它的信息,例如:

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 It A Unique ID
node.addChildNode(planeNode)
planeNode.name = String(planeID)

//5. Store The Anchor & Node
planesDetected[anchor] = planeNode

//6. Increment The Plane ID
planeID += 1

}

现在我们已经存储了检测到的平面,然后我们当然需要确定其中是否有任何平面在 ARCamera 的视野中,例如:

/// 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")
}

}

}
}

最后我们需要访问这个函数,我们可以在下面的 delegate 方法中访问这个函数,例如 renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval):

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

detectPlaneInFrostrumOfCamera()
}

希望这两个都指向正确的方向...

关于ios - 在 ARKIT 中获取聚焦相机的所有 ARAnchors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49363187/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com