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

Scenekit: Add animation to SCNNode from external Collada

我这样初始化我的场景

// 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 自动装配的。

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;
}

然后您可以将其添加到您的角色节点:

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
}