Scenekit:从外部 Collada 向 SCNNode 添加动画 [英] Scenekit: Add animation to SCNNode from external Collada

查看:42
本文介绍了Scenekit:从外部 Collada 向 SCNNode 添加动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像这样初始化我的场景

I initialize my Scene like this

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

Is it possible to add an animation to characterNode from an external DAE? It is autorigged through Mixamo.

推荐答案

Apple 在他们的 Fox Scenekit 应用程序.

Apple has an example in their Fox Scenekit app.

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

The following function loads an animation from your art.scnassets folder:

- (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:

Which you can then add to your characterNode:

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

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

This should be the equivalent function in Swift, but I haven't had a chance to test it.

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
}

这篇关于Scenekit:从外部 Collada 向 SCNNode 添加动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆