ARKit - 从相机到锚点的距离 [英] ARKit - getting distance from camera to anchor

查看:43
本文介绍了ARKit - 从相机到锚点的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个锚点并将其添加到我的 ARSKView 中,距离相机前一定距离,如下所示:

I'm creating an anchor and adding it to my ARSKView at a certain distance in front of the camera like this:

func displayToken(distance: Float) {
        print("token dropped at: \(distance)")
        guard let sceneView = self.view as? ARSKView else {
            return
        }

        // Create anchor using the camera's current position
        if let currentFrame = sceneView.session.currentFrame {
            // Create a transform with a translation of x meters in front of the camera
            var translation = matrix_identity_float4x4
            translation.columns.3.z = -distance
            let transform = simd_mul(currentFrame.camera.transform, translation)

            // Add a new anchor to the session
            let anchor = ARAnchor(transform: transform)
            sceneView.session.add(anchor: anchor)
        }
    }

然后像这样为锚点创建节点:

then the node gets created for the anchor like this:

func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {
        // Create and configure a node for the anchor added to the view's session.
        if let image = tokenImage {
            let texture = SKTexture(image: image)
            let tokenImageNode = SKSpriteNode(texture: texture)
            tokenImageNode.name = "token"
            return tokenImageNode
        } else {
            return nil
        }
    }

这很好用,我看到图像在适当的距离添加.但是,我要做的是计算当您移动时锚点/节点在相机前面的距离.问题是使用 fabs(cameraZ - anchor.transform.columns.3.z) 计算似乎立即关闭.请参阅下面我的代码,该代码在 update() 方法中用于计算相机和对象之间的距离:

This works fine and I see the image get added at the appropriate distance. However, what I'm trying to do is then calculate how far the anchor/node is in front of the camera as you move. The problem is the calculation seems to be off immediately using fabs(cameraZ - anchor.transform.columns.3.z). Please see my code below that is in the update() method to calculate distance between camera and object:

override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
        guard let sceneView = self.view as? ARSKView else {
            return
        }

        if let currentFrame = sceneView.session.currentFrame {
            let cameraZ =  currentFrame.camera.transform.columns.3.z
            for anchor in currentFrame.anchors {
                if let spriteNode = sceneView.node(for: anchor), spriteNode.name == "token", intersects(spriteNode) {
                    // token is within the camera view
                    //print("token is within camera view from update method")
                    print("DISTANCE BETWEEN CAMERA AND TOKEN: \(fabs(cameraZ - anchor.transform.columns.3.z))")
                    print(cameraZ)
                    print(anchor.transform.columns.3.z)
                }
            }
        }
    }

感谢任何帮助以准确获得相机和锚点之间的距离.

Any help is appreciated in order to accurately get distance between camera and the anchor.

推荐答案

一个 4x4 变换矩阵的最后一列是平移向量(或相对于父坐标空间的位置),因此可以得到三个维度之间的距离只需将这些向量相减即可进行两次变换.

The last column of a 4x4 transform matrix is the translation vector (or position relative to a parent coordinate space), so you can get the distance in three dimensions between two transforms by simply subtracting those vectors.

let anchorPosition = anchor.transforms.columns.3
let cameraPosition = camera.transform.columns.3

// here’s a line connecting the two points, which might be useful for other things
let cameraToAnchor = cameraPosition - anchorPosition
// and here’s just the scalar distance
let distance = length(cameraToAnchor)

您所做的工作不正确,因为您要减去每个向量的 z 坐标.如果这两个点的 x、y 和 z 不同,仅减去 z 不会得到距离.

What you’re doing isn’t working right because you’re subtracting the z-coordinates of each vector. If the two points are different in x, y, and z, just subtracting z doesn’t get you distance.

这篇关于ARKit - 从相机到锚点的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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