ARKit:将 3D 对象放置在图像之上

ARKit: Placing a 3D-object on top of image

我正在尝试在参考图像上正确添加 3D 对象。要在现实世界中的图像上添加 3D 对象,我正在使用 imageAnchor.transform 属性,如下所示。

    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

    guard let imageAnchor = anchor as? ARImageAnchor else { return }

    let referenceImage = imageAnchor.referenceImage

    updateQueue.async {  
        // Add a virtual cup at the position of the found image
        self.virtualObject.addVirtualObjectWith(sceneName: "cup.dae",
                                                childNodeName: nil,
                                                position: SCNVector3(x: imageAnchor.transform.columns.3.x,
                                                                     y: imageAnchor.transform.columns.3.y,
                                                                     z: imageAnchor.transform.columns.3.z),
                                                recursively: true,
                                                imageAnchor: imageAnchor)

    }

}

问题是当我移动设备方向时,杯子不会很好地留在图像的中间。即使我删除图像,我也希望将杯子放在同一个位置。我不明白这个问题,因为当您使用平面检测和命中测试添加对象时,还有一个用于平面的 ARAnchor。

2018 年 5 月 14 日更新

 func addVirtualObjectWith(sceneName: String, childNodeName: String, objectName: String, position: SCNVector3, recursively: Bool, node: SCNNode?){
    print("VirtualObject: Added virtual object with scene name: \(sceneName)")

    let scene = SCNScene(named: "art.scnassets/\(sceneName)")!
    var sceneNode = scene.rootNode.childNode(withName: childNodeName, recursively: recursively)!
    sceneNode.name = objectName
    sceneNode.position = position

    add(object: sceneNode, toNode: node)

}

func add(object: SCNNode, toNode: SCNNode?){
    if toNode != nil {
        toNode?.addChildNode(object)
    }
    else {
        sceneView.scene.rootNode.addChildNode(object)
    }
}

我终于找到了解决办法,结果是属性检查器中AR参考图像的大小设置不正确。当尺寸不正确时,图像的锚点会摇晃。