'Implicitly animating SCNNode's transition from one ARImageAnchor to another

I’m trying to make my AR experience more user friendly.

I have this SCNnode (objectNodeToPlace) that I want to place over the node created/update by the renderers whenever an imageReference is detected by the camera:

class ViewController: UIViewController, ARSCNViewDelegate {
var objectNodeToPlace: SCNNode
...

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
                    guard let imageAnchor = anchor as? ARImageAnchor else { return }
          placeObject(object: objectNodeToPlace, at: node, ...)
    }
}

func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
                    guard let imageAnchor = anchor as? ARImageAnchor else { return }
          placeObject(object: objectNodeToPlace, at: node, ...)
    }
}

My placeObject function is pretty simple, I’m just changing the orientation of the “objectNodeToPlace”:

private func placeObject(object objectNodeToPlace: SCNNode, at node: SCNNode, ...) {
    ...
    node.addChildNode(objectNodeToPlace)
}

Everything works well, my object is always placed on the latest detected imageReference.

But when my object is place onto an image and a new one is detected, the object jumps to the newest, it’s not great, very jittery / jerky.

My goal is to make this transition smoother.

What I currently have

Here’s what it looks like rigth now:

What I have now

In red, the imageReference.

What I want

Here’s what I would like:

What I would like

What I’ve found yet

I’ve found this package, SceneKit Bezier Animations, to animate the movement between 2 points but I look a little bit overkill for want I want.

I also read this topic, SceneKit Rotate and Animate a SCNNode, one response suggest to use CABasicAnimation and another one suggest SCNAction.

I feel like SCNAction is the best way to go for the quick, not that precise animation that I want, I’m not sure of what I’m doing so I will be happy to hear from more experienced developers.

Edit

I've found what I'm looking for on the Apple documentation Animating SceneKit Content It's called Implicit animation, and it should work with only one line of code that determines the animation duration of my changes :

SCNTransaction.animationDuration = 1.0

I tried that line just before I change the euler angle (who is "animatable") of my node in my place()function:

SCNTransaction.animationDuration = 1.0
objectNodeToPlace.eulerAngles.x = radian

but that didn't worked. I'm pretty sure that I'm just missing a simple thing but I can't find examples online, even in the documentation of SCNTransaction.

Does someone have an idea ?



Solution 1:[1]

Axel,

SCNTransaction needs a minimum of three statements.

SCNTransaction.start()
SCNTransaction.animationDuration = 1.0
//commands
SCNTransaction.commit()

I written a dozen articles on SceneKit recently you'll find here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user3069232