Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to add a 3D-object properly on a reference image. To add the 3D-object on the image in real world I'm using the imageAnchor.transform property as seen below.

    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

The problem is when I move the device orientation the cup won't stay nicely in the middle on the image. I would also like to have the cup on the same spot even when I remove the image. I don't get the problem because when you add an object using plane detection and hit testing there is also a ARAnchor used for the plane.

The cup when device is moved

Update 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)
    }
}
like image 259
KNV Avatar asked Mar 22 '18 09:03

KNV


1 Answers

I finally found the solution, turns out that the size of the AR Reference Image was not set correctly in the attributes inspector. When the size is not correct, the anchor of the image will be shaky.

attributes inspector

like image 131
KNV Avatar answered Nov 14 '22 23:11

KNV