顯示具有 Spine 標籤的文章。 顯示所有文章
顯示具有 Spine 標籤的文章。 顯示所有文章

2014年12月13日 星期六

[Spine] Spine匯入Unity,使Spine中的Root motion在Unity中實現

Spine進Unity,是不會像FBX一樣可以把FBX中的Root motion直接當作Unity中Game object的Root motion,因此需要額外自己寫script實現,在Spine Forum中有一篇討論串有討論到這個問題:
Applying Root Motion
Mitch 提供了一個解決方案
Had to modify SkeletonAnimation and add a callback between Update and Apply because the Time and LastTime are set to be equivalent by the time UpdateBones is called.
修改了SkeletonAnimation.cs以及SkeletonRootMotion.cs兩個檔案,實現X軸的Root motion。但是我的需求是X及Y軸都需要實現,因此對SkeletonRootMotion.cs做了一些修改,修改後的結果post在Forum中,在這邊補充一下,我不是Programmer所以Code可能不盡完善、正確...
http://esotericsoftware.com/forum/viewtopic.php?f=7&t=1364&start=26
也可以在我的GitHub上查看,檔案內直接保留了Mitch原始的code,可以直接做比對:
https://github.com/januswow/TAG/blob/master/TouchActionGame/Assets/spine-unity/Assets/spine-unity/SkeletonRootMotion.cs
主要的重點是Spine的Frames Data是以[time, x, y, ... time, x, y]的格式寫入,因此在GetXYAtTime()時,frames變數(timeline.frames)的取用需要注意:
float lastFrameX = frames[frameIndex - 2];
float lastFrameY = frames[frameIndex - 1];
另外一個題外話,由於Spine輸出的資訊完全不同於FBX,因此尚無法搭配Mecanim系統,不過nicloay有寫一個Importer,使Spine能夠在Mecanim中運作,可惜的是無法支援全部的資訊,如FFD等...
http://esotericsoftware.com/forum/viewtopic.php?f=11&t=2012

動作流程控制參考:

2014年11月2日 星期日

[Spine] AnimationState Events

在Spine的Runtimes中,AnimationState有幾個Events可以使用。

  • Start
  • Complete
  • End
  • Event
這幾個事件的觸發時機是:
  • Start
    當一個動作開始播放時(在此推測應該是開始Mixing的時候,尚未驗證…)
  • Complete
    當動作(Animation)完成完整的播放時,若是Looping的Animation則會在每個Loop結束時觸發一次。
    • 每個循環結束時觸發
    • 當動作完整的播放完畢時
  • End
    當動作結束時,其他與Complete相同
    • 每個循環結束時觸發
    • 當動作完整的播放完畢時
    • 當動作結束時(例如在動作播放途中被切換成其他動作)
    • 動作被終止或者清除掉時
  • Event
    當Event Track有設置Event時(在此指Spine Editor中設置的Event)(尚未確認)
在使用End事件時需要注意,不可在End事件中做SetAnimation的動作,因為SetAnimation後動作被打斷又會再觸發End事件,造成無限迴圈的狀況。

Complete與End是Pharan在Spine Forum中所做的說明,來源如下

2014年10月29日 星期三

[Spine] 動作進入Runtimes時,發生動作互相干擾的問題

使用Spine應該都會遇到一個狀況,就是A動作結束之後進入B動作,卻發現B動作的某些骨頭的動態有問題,看起來是受到A動作干擾,這是因為Spine的設計是當該Track不帶有Keyframes時,就會延續上一個動作的資訊!所以解決方式就是在動作的起始幀對每一個Track上Key。

詳見官網文件:
http://esotericsoftware.com/spine-using-runtimes#Animation-changes
An animation only affects the bones and slots for which it has keyframes.
When animations are applied in sequence, a previous animation may have made changes to bones or slots that a subsequent animation does not have keyframes for.
This can be solved be keying everything at the start of the second animation that the first animation affects.
文件有提到對每個Track打上key是會造成額外的運算負擔,但若是增加的幅度很小,其實也可以無視了。
With many animations this quickly leads to everything being keyed at the start of every animation. This is suboptimal because each property that is keyed adds a small amount of overhead when the animation is applied each frame.
文件也提出第二種解決方式,就是歸回SetupPose,但造成的負面影響是動作可能會發生不順暢、無法crossfading的狀況。 
Another solution is to call setToSetupPose, setBonesToSetupPose, or setSlotsToSetupPose on the skeleton when the current animation is changed (or otherwise write code that changes the slots and bones as needed).
However, this will cause bones that aren't keyed in the second animation to be instantly set to the setup pose state. AnimationState won't be able to do crossfading for those bones.

[Spine] 熱鍵