簡體   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