4

I'm still very new to SceneKit and SpriteKit and just started working on animating some SCNNodes.

One thing I don't get is when to use SCNAction and when to use CABasicAnimation. Is there any performance difference between SCNAction and CABasicAnimation?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220

2 Answers2

8

For animating SceneKit's content you can use at least four approaches: implicit animations, explicit animations, actions, and dynamics. Let's explore what is what.

Watch WWDC 2012 SceneKit Session 504. Time 28:40.

SCNAction is a simple, reusable animation that changes attributes of any node you attach it to. You can use as many SCNActions per object as you wish. Perceive it as the simplest animation's building block for much complicated animation, containing several SCNActions for one 3D object.

let action = SCNAction.repeatForever(SCNAction.rotate(by: .pi, 
                                                  around: SCNVector3(0, 1, 0), 
                                                duration: 3))
sphereNode.runAction(action)

You use actions most often to change the structure and content of the SCNNode object to which they are attached, but you can also use actions make other changes to the scene. In SceneKit, actions provide an easy way to implement animated behaviors that frequently change in response to user input.

SCNTransaction is a mechanism for creating implicit animations and combining scene graph changes into atomic updates.

@IBAction func fallAndFade(_ sender: Any) {
    SCNTransaction.animationDuration = 3.0
    sphereNode.position.y = -10
    sphereNode.opacity = 0.25
}

or

SCNTransaction.begin()
SCNTransaction.animationDuration = 3
sphereNode.position.y = -10
sphereNode.opacity = 0.25
SCNTransaction.commit()

CAAnimation is an abstract superclass for explicit animation in Core Animation. You can use the properties of the CAAnimation object representing a geometry animation to control its timing, monitor its progress, and attach actions for SceneKit to trigger during the animation.

let animation = CABasicAnimation(keyPath: "geometry.extrusionDepth")
animation.fromValue = 0.0
animation.toValue = 100.0
animation.duration = 1.0
animation.autoreverses = true
animation.repeatCount = .infinity
textNode.addAnimation(animation, forKey: "extrude")

CAAnimation provides the basic support for the CAMediaTiming and CAAction protocols. You do not create instance of CAAnimation: to animate Core Animation layers or SceneKit objects, create instances of the concrete subclasses CABasicAnimation, CAKeyframeAnimation, CAAnimationGroup, or CATransition.

Physics Simulation adds dynamic behaviors to scene elements, detect contacts and collisions, simulate realistic effects like gravity, springs and vehicles.

let node = SCNNode(geometry: SCNSphere(radius: 1.0))
scene.rootNode.addChildNode(node)
node.physicsBody = SCNPhysicsBody.dynamicBody()
scene.physicsWorld.gravity = SCNVector3(x: 0, y: -5, z: 0)
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
0
  • There's no real difference.

  • There is no performance difference.

  • Obviously, if you use individual actions, you can make incredibly complicated animations. Say you have 20 different animations in a group. If you use individual actions, of course they can start at different times, have different lengths, easing, completions, modes, etc etc. If you use a transaction, they simply all run at once in the same way and time.

  • There are some very obscure differences, like, transactions always use shortest-unit-arc for quaternion animations.

In short the only difference between using a transaction, and individual actions, is that with individual actions you can customize every individual anime. (If you're only doing "one anime" in the group, there's really no difference between transaction and individual action.)

Fattie
  • 27,874
  • 70
  • 431
  • 719