SceneKit中的连续变形动画

时间:2015-10-06 10:58:42

标签: ios animation swift2 scenekit cabasicanimation

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

所以从一个几何到另一个几何,这很容易。 在SCNMorpher Class Reference中有一个例子可以为一个变形几何(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)

Output GIF

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

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

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

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

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

1 个答案:

答案 0 :(得分:1)

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

  1. 使用隐式动画(SCNTransaction with a 完成块将触发下一个动画)。
  2. 将removedOnCompletion设置为NO并将fillMode设置为FillForward。
  3. 可选#2:您可以为动画设置委托,并在将modelValue同步到presentationValue后删除animationDidStop中的动画。