SceneKit load node with animation from separate scn file

我与影子孤独终老i 提交于 2019-12-04 06:41:32

All you need is retrieving animations:

        [childNode enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop) {
        for(NSString *key in child.animationKeys) {               // for every animation key
            CAAnimation *animation = [child animationForKey:key]; // get the animation
            animation.usesSceneTimeBase = NO;                     // make it system time based
            animation.repeatCount = FLT_MAX;                      // make it repeat forever
            [child addAnimation:animation forKey:key];            // animations are copied upon addition, so we have to replace the previous animation
        }
    }];
3d-indiana-jones

You need to fetch the animation from your scene Serah_Animated.scn, which will be a CAAnimation object. You then add that animation object to the rootNode of your main scene.

let animScene = SCNSceneSource(url:<<URL to your scene file", options:<<Scene Loading Options>>)
let animation:CAAnimation = animScene.entryWithIdentifier(<<animID>>, withClass:CAAnimation.self)

You can find the animID from the .scn file using scene editor in Xcode, as shown below.

Now you can add the animation object to your root node.

scene.rootNode.addAnimation(animation, forKey:<<animID>>)

Note that we are reusing animID, that will allow you to also remove the animation from the node.

scene.rootNode.removeAnimation(forKey:<<animId>>)
  • My solution above assumes your animation is a single animation. If you see a bunch of animations, you need to add all the animation nodes. In my workflow, I have files in Blender which I export to Collada format and then use the Automated Collada Converter to ensure I have single animation nodes.
  • Related SO answer
  • You can also fetch the animID programmatically using entriesWithIdentifiersOfClass(CAAnimation.self), useful when you have a bunch of animations instead of a single animation as above or if you just want to add the animation without bothering about the animID before hand.
  • Apple Sample Code for scene kit animations, note the sample code is in ObjC but translation to Swift should be straight forward.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!