gpt4 book ai didi

swift - 检查 ARReferenceImage 在相机 View 中是否不再可见

转载 作者:IT王子 更新时间:2023-10-29 05:17:10 28 4
gpt4 key购买 nike

我想检查 ARReferenceImage在相机的视野中不再可见。目前我可以检查图像的节点是否在相机的 View 中,但是当 ARReferenceImage 时,该节点在相机的 View 中仍然可见。被另一个图像覆盖或图像被删除时。

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
guard let node = self.currentImageNode else { return }

if let pointOfView = sceneView.pointOfView {
let isVisible = sceneView.isNode(node, insideFrustumOf: pointOfView)
print("Is node visible: \(isVisible)")
}
}

所以我需要检查图像是否不再可见而不是图像的节点可见性。但我不知道这是否可能。第一个屏幕截图显示了找到下面的图像时添加的三个框。当找到的图像被覆盖时(见屏幕截图 2),我想删除框。

Found image with boxes

Convered image with boxes

最佳答案

我设法解决了这个问题!使用了一点 Maybe1 的代码和他的概念来解决问题,但方式不同。以下代码行仍然用于重新激活图像识别。

// Delete anchor from the session to reactivate the image recognition
sceneView.session.remove(anchor: anchor)

让我解释一下。首先我们需要添加一些变量。

// The scnNodeBarn variable will be the node to be added when the barn image is found. Add another scnNode when you have another image.    
var scnNodeBarn: SCNNode = SCNNode()
// This variable holds the currently added scnNode (in this case scnNodeBarn when the barn image is found)
var currentNode: SCNNode? = nil
// This variable holds the UUID of the found Image Anchor that is used to add a scnNode
var currentARImageAnchorIdentifier: UUID?
// This variable is used to call a function when there is no new anchor added for 0.6 seconds
var timer: Timer!

完整的代码,下面有注释。

/// - Tag: ARImageAnchor-Visualizing
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let imageAnchor = anchor as? ARImageAnchor else { return }

let referenceImage = imageAnchor.referenceImage

// The following timer fires after 0.6 seconds, but everytime when there found an anchor the timer is stopped.
// So when there is no ARImageAnchor found the timer will be completed and the current scene node will be deleted and the variable will set to nil
DispatchQueue.main.async {
if(self.timer != nil){
self.timer.invalidate()
}
self.timer = Timer.scheduledTimer(timeInterval: 0.6 , target: self, selector: #selector(self.imageLost(_:)), userInfo: nil, repeats: false)
}

// Check if there is found a new image on the basis of the ARImageAnchorIdentifier, when found delete the current scene node and set the variable to nil
if(self.currentARImageAnchorIdentifier != imageAnchor.identifier &&
self.currentARImageAnchorIdentifier != nil
&& self.currentNode != nil){
//found new image
self.currentNode!.removeFromParentNode()
self.currentNode = nil
}

updateQueue.async {

//If currentNode is nil, there is currently no scene node
if(self.currentNode == nil){

switch referenceImage.name {
case "barn":
self.scnNodeBarn.transform = node.transform
self.sceneView.scene.rootNode.addChildNode(self.scnNodeBarn)
self.currentNode = self.scnNodeBarn
default: break
}

}

self.currentARImageAnchorIdentifier = imageAnchor.identifier

// Delete anchor from the session to reactivate the image recognition
self.sceneView.session.remove(anchor: anchor)
}

}

当计时器结束表示没有找到新的 ARImageAnchor 时删除节点。

@objc
func imageLost(_ sender:Timer){
self.currentNode!.removeFromParentNode()
self.currentNode = nil
}

这样当图片被覆盖或者发现新的图片时,会删除当前添加的scnNode。

enter image description here

不幸的是,由于以下原因,该解决方案没有解决图像的定位问题:

ARKit doesn’t track changes to the position or orientation of each detected image.

关于swift - 检查 ARReferenceImage 在相机 View 中是否不再可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49997025/

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