2015-10-06 123 views
1

我在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」,但似乎沒有工作。

有沒有人提供線索或解決方案?

我可以發佈一個鏈接,到整個代碼,如果這是必要的 - 不要以爲這是要求在這裏。

回答

1

這是CAAnimation的一個常見問題 - 不只是使用SceneKit:顯式動畫在默認情況下完成時被移除,並且表示值被重置爲模型值(永遠不會被顯式動畫修改)。 有該2級的解決方案:

  1. 使用代替的隱式動畫(具有 完成塊,將觸發下一個動畫SCNTransaction)。
  2. 將removedOnCompletion設置爲NO並將fillMode設置爲FillForward。

可選地爲#2:您可以爲您的動畫設置一個委託,並在將modelValue同步到presentationValue後刪除animationDidStop中的動畫。