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

时间:2018-03-22 09:49:41

标签: ios swift augmented-reality arkit

我试图在参考图像上正确添加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)

    }

}

The cup when just added to the scene

问题在于,当我移动设备方向时,杯子不会在图像的中间保持良好状态。即使我删除图像,我也希望将杯子放在同一位置。我没有遇到问题,因为当您使用平面检测和命中测试添加对象时,还会有一个用于飞机的ARAnchor。

The cup when device is moved

更新05/14/2018

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

1 个答案:

答案 0 :(得分:4)

我终于找到了解决方案,结果在属性检查器中未正确设置AR参考图像的大小。当尺寸不正确时,图像的锚点会不稳定。

attributes inspector