检查 ARReferenceImage 是否在相机视图中不再可见 [英] Check whether the ARReferenceImage is no longer visible in the camera's view

查看:28
本文介绍了检查 ARReferenceImage 是否在相机视图中不再可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查

解决方案

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

//从会话中删除锚点以重新激活图像识别sceneView.session.remove(锚点:锚点)

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

//scnNodeBarn 变量将是找到谷仓图像时要添加的节点.当您有另一个图像时添加另一个 scnNode.var scnNodeBarn: SCNNode = SCNNode()//此变量保存当前添加的 scnNode(在本例中为找到谷仓图像时的 scnNodeBarn)var currentNode: SCNNode?= 零//该变量保存了找到的用于添加 scnNode 的 Image Anchor 的 UUIDvar currentARImageAnchorIdentifier:UUID?//该变量用于在 0.6 秒内没有添加新锚点时调用函数var 计时器:计时器!

完整的代码和下面的注释.

///- 标签:ARImageAnchor-Visualizingfunc renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {守卫让 imageAnchor = 锚定为?ARImageAnchor else { return }让 referenceImage = imageAnchor.referenceImage//以下计时器在 0.6 秒后触发,但每次找到锚点时,计时器都会停止.//所以当没有找到 ARImageAnchor 时,定时器将完成并删除当前场景节点并将变量设置为 nilDispatchQueue.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)}//根据ARImageAnchorIdentifier检查是否有新图片,找到后删除当前场景节点并将变量设置为nilif(self.currentARImageAnchorIdentifier != imageAnchor.identifier &&self.currentARImageAnchorIdentifier != nil&&self.currentNode != nil){//找到新图片self.currentNode!.removeFromParentNode()self.currentNode = nil}updateQueue.async {//如果currentNode为nil,则当前没有场景节点if(self.currentNode == nil){切换referenceImage.name {案例谷仓":self.scnNodeBarn.transform = node.transformself.sceneView.scene.rootNode.addChildNode(self.scnNodeBarn)self.currentNode = self.scnNodeBarn默认值:中断}}self.currentARImageAnchorIdentifier = imageAnchor.identifier//从会话中删除锚点以重新激活图像识别self.sceneView.session.remove(锚点:锚点)}}

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

@objcfunc imageLost(_发件人:定时器){self.currentNode!.removeFromParentNode()self.currentNode = nil}

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

很遗憾,这个方案没有解决图片的定位问题,原因如下:

<块引用>

ARKit 不会跟踪每个检测到的图像的位置或方向的变化.

I would like to check whether the ARReferenceImage is no longer visible in the camera's view. At the moment I can check if the image's node is in the camera's view, but this node is still visible in the camera's view when the ARReferenceImage is covered with another image or when the image is removed.

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

So I need to check if the image is no longer visible instead of the image's node visibility. But I can't find out if this is possible. The first screenshot shows three boxes that are added when the image beneath is found. When the found image is covered (see screenshot 2) I would like to remove the boxes.

解决方案

I managed to fix the problem! Used a little bit of Maybe1's code and his concept to solving the problem, but in a different way. The following line of code is still used to reactivate the image recognition.

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

Let me explain. First we need to add some variables.

// 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!

The complete code with comments below.

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

}

Delete the node when the timer is finished indicating that there was no new ARImageAnchor found.

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

In this way the currently added scnNode wil be deleted when the image is covered or when there is found a new image.

This solution does unfortunately not solve the positioning problem of images because of the following:

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

这篇关于检查 ARReferenceImage 是否在相机视图中不再可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆