繁体   English   中英

SceneKit中的连续变形动画

[英]Successive Morphing Animation in SceneKit

我在SceneKit中通过不同的Morph-Targets进行动画制作。 我有一个变形目标数组,我希望通过它们之间的动画来展示它们之间的动画。

所以从一个几何到另一个几何,这很容易。 SCNMorpher类参考中,有一个示例为一个变形几何(morpher.weights [0])执行此操作:

let animation = CABasicAnimation(keyPath: "morpher.weights[0]")
animation.fromValue = 0.0;
animation.toValue = 1.0;
animation.autoreverses = true;
animation.repeatCount = Float.infinity;
animation.duration = 5;

我想做的是,连续动画变形。 我尝试为每个目标设置几个动画。 但是在完成第一个动画后,第一个目标的重量再次设置为0。

override func viewDidLoad() {
    super.viewDidLoad()

    // create a new scene
    let scene = SCNScene()

    let vbasic_geom: [Vertex] = [
        [ [0, 0, 0],      [+1.0, +0.0, +0.0] ],
        [ [0, 2, 0],      [+1.0, +0.0, +0.0] ],
        [ [2, 2, 0],      [+1.0, +0.0, +0.0] ]
    ]

    let vmorph1: [Vertex] = [
        [ [0, 0, 0],      [+1.0, +0.0, +0.0] ],
        [ [0, 4, 0],      [+1.0, +0.0, +0.0] ],
        [ [2, 2, 0],      [+1.0, +0.0, +0.0] ]
    ]

    let vmorph2: [Vertex] = [
        [ [0, 0, 0],      [+1.0, +0.0, +0.0] ],
        [ [0, 2, 0],      [+1.0, +0.0, +0.0] ],
        [ [4, 2, 0],      [+1.0, +0.0, +0.0] ]
    ]


    let basic_geom = gen_geometry(vbasic_geom)

    let morph1 = gen_geometry(vmorph1)

    let morph2 = gen_geometry(vmorph2)


    let morpher = SCNMorpher()
    morpher.targets = [morph1, morph2]

    let object = SCNNode(geometry: basic_geom)
    object.morpher = morpher

    //needed for model-layer refresh; implicit animation
    morpher.setWeight(1.0, forTargetAtIndex: 0)
    morpher.setWeight(1.0, forTargetAtIndex: 1)

    //set up animations
    let animation = CABasicAnimation(keyPath: "morpher.weights[0]")
    animation.fromValue = 0.0
    animation.toValue = 1.0
    animation.duration = 5
    animation.beginTime = 0.0
    animation.removedOnCompletion = false

    let animation2 = CABasicAnimation(keyPath: "morpher.weights[1]")
    animation2.fromValue = 0.0;
    animation2.toValue = 1.0
    animation2.duration = 5
    animation2.beginTime = 5.0
    animation.removedOnCompletion = false

    let animationgroup = CAAnimationGroup()
    animationgroup.duration = 10.0
    animationgroup.autoreverses = false
    animationgroup.repeatCount = 10000;

    animationgroup.animations = [animation, animation2]

    object.addAnimation(animationgroup, forKey: "morpher.weights")

    scene.rootNode.addChildNode(object)

输出 GIF

尝试了几个解决方案,但我从未成功:

  • 也许可以改变'keyPath:'morpher.weights [0]“'到'morphher.weights'并将from-和toValues设置为类似于数组的东西。 包括更改valueFunction。

  • 因为动画发生在表示层上,所以它必须是将已经动画的属性保存到完成值的方法。 它尝试了“removedOnCompletion”,但似乎没有用。

有人有线索或解决方案吗?

如果有必要,我可以发布链接到整个代码 - 不要认为这里有要求。

这是CAAnimation的常见问题 - 不仅仅是使用SceneKit:默认情况下,在完成时删除显式动画,并将显示值重置为模型值(永远不会被显式动画修改)。 有两个解决方案:

  1. 使用隐式动画(具有将触发下一个动画的完成块的SCNTransaction)。
  2. 将removedOnCompletion设置为NO并将fillMode设置为FillForward。

(可选)#2:您可以为动画设置委托,并在将modelValue同步到presentationValue后删除animationDidStop中的动画。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM