【问题标题】:Scenekit: Add animation to SCNNode from external ColladaScenekit:从外部 Collada 向 SCNNode 添加动画
【发布时间】:2016-12-01 19:56:35
【问题描述】:

我像这样初始化我的场景

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

【问题讨论】:

    标签: scenekit


    【解决方案1】:

    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
    }
    

    【讨论】:

    • 感谢您的回答!我使用 Swift(我从未说过)。所以我在link 中找到了相应的函数(animationWithSceneNamed)但是,我在输入此代码时收到错误 Value of type 'SCNNode' has no member 'enumerateChildNodes'
    • 我认为示例代码现在已经很老了,我已经将 Objective-c 函数转换为 Swift,看看是否适合你。
    • 谢谢!有没有办法将动画添加到所有键?我的对象有“Body、Bottoms、Top”等。不知道“Key”到底是什么意思。
    • 好的,看来动画正在运行。但是,整个模型/对象正在移动。手臂,腿等作为一个整体。 Link to gif
    • 您可能将动画附加到错误的节点。我发现它需要大量的试验和错误才能让动画正常工作。 Scenekit 对 dae 格式非常特别,您可能需要使用不同的设置导出它。这可能会有所帮助forums.developer.apple.com/thread/27744
    猜你喜欢
    • 2015-08-30
    • 2014-10-12
    • 2017-03-21
    • 2018-03-28
    • 2015-06-23
    • 2018-05-15
    • 2013-08-03
    • 2015-06-21
    • 1970-01-01
    相关资源
    最近更新 更多