Jush KillaB
Jush KillaB

Reputation: 151

3D Model is shaky with ARKit in Xcode

I am using ARKits image detection to place a 3D object when a certain image is detected. Everything works fine except for the 3D model that is being created. It is shaking like crazy. I double checked and the reference image has the right measures.

I call addModel() when the image is detected. Here is how my code looks like.

Finding reference Image:

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
    let node = SCNNode()

    if let imageAnchor = anchor as? ARImageAnchor{
        let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)

        let planeNode = SCNNode(geometry: plane)

        addModel(addTo: planeNode)
        node.addChildNode(planeNode)
    }
    return node
}

The addModel() functions looks like this:

func addModel(addTo: SCNNode){
    let testScene = SCNScene(named: "art.scnassets/testModel.scn")

    let testNode = testScene?.rootNode.childNode(withName: "test", recursively: true)
    let testMaterial = SCNMaterial()
    testMaterial.diffuse.contents = UIImage(named: "art.scnassets/bricks")

    testNode?.geometry?.materials = [testMaterial]

    testNode!.position = SCNVector3Zero
    testNode!.position.x = -0.3
    testNode!.position.z = 0.3

    addTo.addChildNode(testNode!)
}

Upvotes: 2

Views: 474

Answers (1)

AntonyCatcher
AntonyCatcher

Reputation: 129

Did your try to delete: testNode!.position.x = -0.3 testNode!.position.z = 0.3 ?

Because when you type testNode!.position = SCNVector3Zero it is says, that x = 0.0, y = 0.0, z = 0.0 and after that you type another coordinates.

Upvotes: 1

Related Questions