gpt4 book ai didi

animation - 如何 “crop” SCNAnimationPlayer 到特定的开始和结束时间,iOS 11

转载 作者:行者123 更新时间:2023-12-04 14:39:41 25 4
gpt4 key购买 nike

我有一个带有长动画的 .dae 模型。动画包括走、跑、打、死等片段,我知道每个片段开始和结束的帧数。我也知道每秒帧数。因此,获取每个段的开始和结束时间非常容易。

我可以获得完整的动画作为 SCNAnimationPlayer 对象。我一直在尝试制作完整动画的副本,然后设置动画的时间偏移和持续时间。

let walkPlayer = fullPlayer.copy() as! SCNAnimationPlayer
walkPlayer.stop()
walkPlayer.animation.timeOffset = walk.offset
walkPlayer.animation.duration = walk.duration

然后我将 walkPlayer 添加回 Bip01 节点(我从那里获得完整动画)。

我可以通过调用 animationPlayer(forKey:"walk")?.play() 轻松地播放步行

我可以很容易地改变动画的持续时间和其他方面。但是动画总是从第 0 帧开始。无论我在 .timeOffset 中输入什么值,它都会被忽略。

如何从 SCNAnimationPlayer 中找到的 SCNAnimation 的开始帧播放到结束帧?

最佳答案

关键是要找到

CAAnimation(scnAnimation: animation)


SCNAnimation(caAnimation: animation)

一旦找到这些,我就可以使用 CAAnimationGroup 来“裁剪”完整的动画。

这是我正在开发的 Troll.swift。当然,还有很多事情要做,但现在我至少可以让这头可怜的野兽走路和死去。
class Troll: SCNNode {
var body:SCNNode!

static func timeRange(forStartingAtFrame start:Int, endingAtFrame end:Int, fps:Double = 30) -> (offset:TimeInterval, duration:TimeInterval) {
let startTime = self.time(atFrame: start, fps: fps) //TimeInterval(start) / fps
let endTime = self.time(atFrame: end, fps: fps) //TimeInterval(end) / fps
return (offset:startTime, duration:endTime - startTime)
}

static func time(atFrame frame:Int, fps:Double = 30) -> TimeInterval {
return TimeInterval(frame) / fps
}

static func animation(from full:CAAnimation, startingAtFrame start:Int, endingAtFrame end:Int, fps:Double = 30) -> CAAnimation {
let range = self.timeRange(forStartingAtFrame: start, endingAtFrame: end, fps: fps)
let animation = CAAnimationGroup()
let sub = full.copy() as! CAAnimation
sub.timeOffset = range.offset
animation.animations = [sub]
animation.duration = range.duration
return animation
}


func load() {

guard let trollScene = SCNScene(named: "Models.scnassets/troll/troll.dae") else {
fatalError("Can't load the scene")
}

guard let troll_body = trollScene.rootNode.childNode(withName: "troll", recursively: true) else {
fatalError( "found no troll")
}

guard let troll_weapon = trollScene.rootNode.childNode(withName: "troll_weapon", recursively: true) else {
fatalError( "found no troll_weapon")
}

guard let troll_bracelet = trollScene.rootNode.childNode(withName: "troll_bracelet", recursively: true) else {
fatalError( "found no troll_bracelet")
}

guard let bips = trollScene.rootNode.childNode(withName: "Bip01", recursively: true) else {
fatalError( "found no Bip01")
}

guard let fullKey = bips.animationKeys.first else {
fatalError( "Bip01 got no animation")
}

guard let fullPlayer = bips.animationPlayer(forKey: fullKey) else {
fatalError( "Bip01 got no player for \(fullKey)")
}
let fullAnimation = CAAnimation(scnAnimation: fullPlayer.animation)

self.addChildNode(troll_body)
self.addChildNode(troll_weapon)
self.addChildNode(troll_bracelet)
self.addChildNode(bips)

self.body = bips
self.body.removeAllAnimations()

let walkAnimation = Troll.animation(from: fullAnimation, startingAtFrame: 10, endingAtFrame: 60)
walkAnimation.repeatCount = .greatestFiniteMagnitude
walkAnimation.fadeInDuration = 0.3
walkAnimation.fadeOutDuration = 0.3
let walkPlayer = SCNAnimationPlayer(animation: SCNAnimation(caAnimation: walkAnimation))
self.body.addAnimationPlayer(walkPlayer, forKey: "walk")

let deathAnimation = Troll.animation(from: fullAnimation, startingAtFrame: 1810, endingAtFrame: 1850)
deathAnimation.isRemovedOnCompletion = false
deathAnimation.fadeInDuration = 0.3
deathAnimation.fadeOutDuration = 0.3
let deathPlayer = SCNAnimationPlayer(animation: SCNAnimation(caAnimation: deathAnimation))
self.body.addAnimationPlayer(deathPlayer, forKey: "death")

self.scale = SCNVector3(0.1,0.1,0.1)

}

func walk() {
print( "+++ walk +++" )
self.body.animationPlayer(forKey: "walk")?.play()
}

func death() {
print( "+++ death +++" )
self.body.animationPlayer(forKey: "walk")?.stop(withBlendOutDuration: 0.3)
self.body.animationPlayer(forKey: "death")?.play()
}
}

关于animation - 如何 “crop” SCNAnimationPlayer 到特定的开始和结束时间,iOS 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46109391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com