如何根据要求在 SceneKit 中添加动画
How to add an animation in SceneKit on request
如果你能帮我把这个动画添加到场景中,或者如果你知道如何将动画添加到场景中,将不胜感激。
代码:
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”)
如果您有一个包含动画的 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)
设置位置或重复次数...
另一种方式类似于您的代码:
你可以使用
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
如果你能帮我把这个动画添加到场景中,或者如果你知道如何将动画添加到场景中,将不胜感激。
代码:
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”)
如果您有一个包含动画的 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)
设置位置或重复次数...
另一种方式类似于您的代码: 你可以使用
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