SceneKit中的连续变形动画

SceneKit中的连续变形动画

问题描述:

我在SceneKit中通过不同的变形目标挣扎动画。 我有一个我想要遍历的变形目标数组,每个变形目标之间都有一个动画。SceneKit中的连续变形动画

所以从一个几何到另一个几何,这很容易。 在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]” 'to'morpher。权重“,并将from-和toValues设置为像数组一样。包括改变valueFunction。

  • 因为动画发生在表现层上,所以它必须是一种将已经动画的属性保存为最终值的方法。它试图“removedOnCompletion”,但似乎没有工作。

有没有人提供线索或解决方案?

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

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

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

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