首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ARImageAnchor变换矩阵的坐标与ARPlaneAnchor变换矩阵的坐标相差很大。

ARImageAnchor变换矩阵的坐标与ARPlaneAnchor变换矩阵的坐标相差很大。
EN

Stack Overflow用户
提问于 2018-03-13 15:57:30
回答 1查看 1.9K关注 0票数 1

我正在做一件简单的事情:

  1. 垂直平面检测
  2. 垂直平面上的图像识别

图像挂在被探测到的飞机上(在我的墙上)。在这两种情况下,我都从renderer:didAddNode:forAnchor:实现了ARSCNViewDelegate函数。我站在垂直平面检测和图像识别的地方。

代码语言:javascript
复制
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    guard let shipScene = SCNScene(named: "ship.scn"), let shipNode = shipScene.rootNode.childNode(withName: "ship", recursively: false) else { return }
    shipNode.position = SCNVector3(anchor.transform.columns.3.x, anchor.transform.columns.3.y, anchor.transform.columns.3.z)
    sceneView.scene.rootNode.addChildNode(shipNode)
    print(anchor.transform)
}

在垂直平面检测的情况下,anchor将是ARPlaneAnchor。在图像识别的情况下,anchor将是ARImageAnchor

为什么这两个锚的变换矩阵如此不同?我正在打印anchor.transform,我得到了这些结果:

1.

代码语言:javascript
复制
simd_float4x4([
    [0.941312,  0.0,        -0.337538,  0.0)],
    [0.336284,  -0.0861278, 0.937814,   0.0)],
    [-0.0290714,-0.996284,  -0.0810731, 0.0)],
    [0.191099,  0.172432,   -1.14543,   1.0)]
])

2.

代码语言:javascript
复制
simd_float4x4([
    [0.361231,  0.10894,    0.926093,   0.0)],
    [-0.919883, -0.121052,  0.373049,   0.0)],
    [0.152743,  -0.986651,  0.0564843,  0.0)],
    [75.4418,   10.9618,    -14.3788,   1.0)]
])

因此,如果我想在检测到的垂直平面上放置一个3D对象,我可以简单地使用[x = 0.191099, y = 0.172432, z = -1.14543]作为坐标来设置我的节点(myNode)的位置,然后用sceneView.scene.rootNode.addChildNode(myNode)将这个节点添加到场景中,但是如果我想在检测到的图像的锚上放置一个3D对象,我就不能使用[x = 75.4418, y = 10.9618, z = -14.3788]

我应该做些什么来将一个3D物体放置在检测到的图像的锚上?我真的不明白ARImageAnchor的变换矩阵。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-13 23:36:23

下面是我使用func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor)方法的一个示例:

代码语言:javascript
复制
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

    //1. If Out Target Image Has Been Detected Than Get The Corresponding Anchor
    guard let currentImageAnchor = anchor as? ARImageAnchor else { return }

    let x = currentImageAnchor.transform
    print(x.columns.3.x, x.columns.3.y , x.columns.3.z)

    //2. Get The Targets Name
    let name = currentImageAnchor.referenceImage.name!

    //3. Get The Targets Width & Height In Meters
    let width = currentImageAnchor.referenceImage.physicalSize.width
    let height = currentImageAnchor.referenceImage.physicalSize.height

    print("""
    Image Name = \(name)
    Image Width = \(width)
    Image Height = \(height)
    """)

    //4. Create A Plane Geometry To Cover The ARImageAnchor
    let planeNode = SCNNode()
    let planeGeometry = SCNPlane(width: width, height: height)
    planeGeometry.firstMaterial?.diffuse.contents = UIColor.white
    planeNode.opacity = 0.25
    planeNode.geometry = planeGeometry

    //5. Rotate The PlaneNode To Horizontal
    planeNode.eulerAngles.x = -.pi/2

    //The Node Is Centered In The Anchor (0,0,0)
    node.addChildNode(planeNode)


    //6. Create AN SCNBox
    let boxNode = SCNNode()
    let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)

    //7. Create A Different Colour For Each Face
    let faceColours = [UIColor.red, UIColor.green, UIColor.blue, UIColor.cyan, UIColor.yellow, UIColor.gray]
    var faceMaterials = [SCNMaterial]()

    //8. Apply It To Each Face
    for face in 0 ..< 5{
        let material = SCNMaterial()
        material.diffuse.contents = faceColours[face]
        faceMaterials.append(material)
    }
    boxGeometry.materials = faceMaterials
    boxNode.geometry = boxGeometry

    //9. Set The Boxes Position To Be Placed On The Plane (node.x + box.height)
    boxNode.position = SCNVector3(0 , 0.05, 0)

    //10. Add The Box To The Node
    node.addChildNode(boxNode)


}

根据我的理解(我当然可能错了),您会知道您的安置区域是referenceImage.physicalSize的宽度和高度,以米表示:

代码语言:javascript
复制
let width = currentImageAnchor.referenceImage.physicalSize.width
let height = currentImageAnchor.referenceImage.physicalSize.height

因此,您需要在这些边界内缩放您的内容(如果需要的话),前提是您希望它看起来覆盖图像。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49260590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档