Scenekit:从外部Collada向SCNNode添加动画

时间:2016-07-27 17:03:37

标签: scenekit

我像这样初始化我的场景

// Load COLLADA Character
let myScene = SCNScene(named: "Characters.scnassets/Police/Police.dae")

// Recurse through all the child nodes in the Character and add to characterNode
for node in myScene!.rootNode.childNodes as [SCNNode]
{
    characterNode.addChildNode(node)
}

// Add characterNode to scene
self.rootNode.addChildNode(characterNode)

是否可以从外部DAE向 characterNode 添加动画?它通过Mixamo自动播放。

1 个答案:

答案 0 :(得分:2)

Apple在Fox Scenekit app.

中有一个例子

以下函数从art.scnassets文件夹加载动画:

- (CAAnimation *)animationFromSceneNamed:(NSString *)path {
    SCNScene *scene = [SCNScene sceneNamed:path];
    __block CAAnimation *animation = nil;

    [scene.rootNode enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop) {
        if (child.animationKeys.count > 0) {
            animation = [child animationForKey:child.animationKeys[0]];
            *stop = YES;
        }
    }];

    return animation;
}

然后您可以将其添加到characterNode:

CAAnimation *animation = [self animationFromSceneNamed:@"art.scnassets/characterAnim.scn"];
[characterNode addAnimation:animation forKey:@"characterAnim"];

这应该是Swift中的等效函数,但我没有机会测试它。

func animationFromSceneNamed(path: String) -> CAAnimation? {
    let scene  = SCNScene(named: path)
    var animation:CAAnimation?
    scene?.rootNode.enumerateChildNodes({ child, stop in
        if let animKey = child.animationKeys.first {
            animation = child.animation(forKey: animKey)
            stop.pointee = true
        }
    })
    return animation
}