'SceneKit – What's the difference between SCNAction and CABasicAnimation?
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
?
Solution 1:[1]
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.setAnimationDuration(3.0)
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)
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 |