Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom animatable properties in SceneKit

I know it's possible to create custom animatable properties in Core Animation, but what about in OS X 10.8's SceneKit framework? SCNAnimatable doesn't seem to expose the same APIs that CALayer does for making properties animatable.

In my application, I've got a SCNNode subclass called Starfield, which I've ported from an old OpenGL application by using a SCNNodeRendererDelegate. Starfields expose a GLfloat property called warpFactor:

@interface Starfield : SCNNode<SCNNodeRendererDelegate> {
    // other stuff that's not really important for this question
    GLfloat warpFactor;
}

@property(nonatomic) GLfloat warpFactor;
@end

But when I try to add an animation to it, like so:

CABasicAnimation *warp = [CABasicAnimation animationWithKeyPath:@"warpFactor"];
warp.toValue = @1.0;
warp.duration = 5.0;
[starfield addAnimation:warp forKey:@"warp"];

I get the following in the console:

[SCNKit ERROR] warpFactor is not an animatable path (from <unnamed SCNNode 0x1016473c0, no children>)

I know SceneKit is brand new, but does anyone happen to know how to make this work?

like image 204
Nat Budin Avatar asked Jul 31 '12 14:07

Nat Budin


1 Answers

A workaround/hack:

  • create a geometry-less node and animate some animatable property on that node, for example the opacity
  • use a SCNSceneRendererDelegate to hook into the appropriate stage of the rendering process and manually update your custom property with the value of the geometry-less node's animated property (make sure to read the animated property from the node's presentationNode)

This solution is ugly, but it allows you to synchronize the animation of the custom property with other animations via SCNTransaction or CAAnimationGroup

like image 59
Kevin Tonon Avatar answered Oct 17 '22 07:10

Kevin Tonon