【问题标题】:How to add an animation in SceneKit on request如何根据要求在 SceneKit 中添加动画
【发布时间】:2016-02-03 05:17:43
【问题描述】:

如果您能帮我将此动画添加到场景中,或者如果您知道如何将动画添加到场景中,我们将不胜感激。

代码:

let BallScene = SCNScene(named: "art.scnassets/Ball.dae")!
let ManScene = SCNScene(named: "art.scnassets/Genric_Simplifiyed9.dae")!
let Man: SCNNode = ManScene.rootNode.childNodeWithName("Bob_001", recursively: true)!
Man.position = SCNVector3(x: 0, y: 0, z: 5)
BallScene.rootNode.addChildNode(Man)

let Animation: CAAnimation
let AnimationUrl: NSURL = NSBundle.mainBundle().URLForResource("art.scnassets/Genric_Simplifiyed9.dae", withExtension: "dae")!
let sceneSource = SCNSceneSource(URL: AnimationUrl, options: [SCNSceneSourceAnimationImportPolicyKey : SCNSceneSourceAnimationImportPolicyDoNotPlay])

sceneSource.addAnimation(attackAnimation, forKey: "attack”)

【问题讨论】:

  • 运行该代码时会发生什么?你希望发生什么?
  • 出现错误,因为“attackAmination”不存在,因此无法运行。但关键是我不知道如何设置代码行,以便“attackAnimation”实际从 .dae 文件中提取动画并使用它。有没有一种简单的方法可以从 .dae 文件中拉出并运行动画,因为我在搅拌机中制作的胺化已附加到 .dae 文件。就像如果人是主要场景而不是球,那么人的动画会自动播放,但因为人是球的子节点,所以它根本不播放

标签: swift animation scenekit


【解决方案1】:

如果您有一个包含动画的 DAE/COLLADA 文件,您只需单击播放按钮即可在情节提要中播放动画。 那你就可以了

let subScene = SCNScene(named: "art.scnassets/test_ball_2011.dae")!
let childNodes = subScene.rootNode.childNodes
for child in childNodes {
    //child.position = SCNVector3(0, 0, -100)
    rootScene.rootNode.addChildNode(child)
}

动画将在您的 rootScene 中播放

你可以使用

for child in childNodes {
   let keys = child.animationKeys
   child.position = SCNVector3(0, 0, -100)
   for key in keys {
       let animation = child.animationForKey(key)!
       animation.repeatCount = 5
       child.removeAnimationForKey(key) 
       child.addAnimation(animation, forKey: key)
   }
   rootScene.rootNode.addChildNode(child)

设置位置或repeatCount...

类似于您的代码的另一种方式: 你可以使用

let url = NSBundle.mainBundle().URLForResource(/* your DAE file */)
let sceneSource = SCNSceneSource(URL: url, options: [
        SCNSceneSourceAnimationImportPolicyKey : SCNSceneSourceAnimationImportPolicyPlayRepeatedly
])!

要获取源代码并在选项中设置很多东西,只需检查 API 并使用

let node = sceneSource.entryWithIdentifier("/* the node name */", withClass: SCNNode.self)
let animation = sceneSource.entryWithIdentifier("/* the animation name */", withClass: CAAnimation.self)

获取节点和动画。如果这两个文件在不同的文件中,只需制作 2 个来源即可:) 最后一件事是node.addAnimation(animation, forKey: nil) // 检查 API

【讨论】:

  • 我肯定在 .scn 文件中有一个带有特定键的动画,但 entryWithIdentifier 总是返回 nil。为什么?
猜你喜欢
  • 2015-04-03
  • 2017-03-21
  • 2020-08-10
  • 2018-10-25
  • 2016-12-01
  • 1970-01-01
  • 2017-05-12
  • 2018-08-10
  • 2016-01-13
相关资源
最近更新 更多