unity 之 Animation 二 BlendTree
Blend Trees
在游戲動畫中一個常見的任務是將兩個或多個相似的動作混合在一起. 也許最著名的例子就是根據(jù)角色的速度混合行走和跑步的動畫.另一個例子是一個角色在跑步時向左或向右傾斜,就是根據(jù)參數(shù)來覺混合動畫,決定當前播放的是哪個動畫
Transitions and Blend Trees的不同:
-
Transitions是從一個state過渡到另一個state.
-
Blend Trees 通過混合參數(shù)來混和多個動畫. Blend Trees 是動畫狀態(tài)機的一個特殊的state。 Animation State Machine
類似動作的例子可以是各種各樣的walk和run動畫. 為了使過渡效果更好,剪輯中的動作必須在標準時間的相同點上發(fā)生.比如,沒跑步和行走它們在某一時刻的腳應該在同一個位置,這樣混合的就不叫平滑,而不會出現(xiàn)腳在地上滑行的情況
Using Blend Trees
To start working with a new Blend Tree, you need to:
?
1D Blending
Inspector中的第一個選項就是 Blend Type. 按照需要的blend 參數(shù)來分,分為1D和2D,1D就是需要一個混合參數(shù)
設置完Blend Type后,選擇一個 Animation Parameter 作為混合參數(shù),來控制 Blend Tree. 比如這里是direction 表示–1.0 (left) and +1.0 (right)之間的差異.
你可以自定混合參數(shù),通過添加Animation Parameter,但是混合參數(shù)必須是float類型的
檢查器頂部的圖表顯示了當參數(shù)在其最小值和最大值之間變化時,每個動畫的影響.每個運動都顯示為一個小的藍色金字塔(第一個和最后一個只顯示了一半),如果你點擊并按住鼠標左鍵,相應的運動在下面的運動列表中突出顯示. 每個金字塔的頂點都定義了運動具有完全影響力的參數(shù)值,即其動畫權值為1,其他動畫權值為0。這也被稱為運動的閾值. threshold
Parameter Range
參數(shù)的范圍被是圖表中的左右兩個數(shù),這個范圍可以通過點擊左右兩邊的數(shù)字更改它 ,比如上面的是-1到1,可以點擊數(shù)字,更改它的值,其實,最后一個motion的threshold就是它的最大值,第一個motion的threshold就是它的最小值
Thresholds
如果下面的 “Automate Thresholds” 屬性沒有勾選上的話,你可以自定義閾值,就是motion起作用的區(qū)間
如果 “Automate Thresholds” 勾選上,unity默認以平局值得方法,過度每一個motion,比如有五個參數(shù)是 –90 to +90, thresholds 就會從–90, –45, 0, +45 and +90 得順序
The Compute Thresholds 選擇從root motions,也就是從動畫片段里面得參數(shù)里面獲取混合參數(shù)的值
?它包括, velocity x, y, or z, and angular speed in degrees or radians. 如果你的參數(shù)需要和這些相關連,就使用下面的值
| Speed | ?根據(jù)動畫片段的速度來定義threshold值(the magnitude of the velocity). |
| Velocity X | Sets the threshold of each motion according to its velocity.x. |
| Velocity Y | Sets the threshold of each motion according to its velocity.y. |
| Velocity Z | Sets the threshold of each motion according to its velocity.z. |
| Angular Speed (Rad) | Sets the threshold of each motion according to its angular speed in radians per second. |
| Angular Speed (Deg) | Sets the threshold of each motion according to its angular speed in degrees per second. |
比如:你可以看到一個每秒1.5個單位的步行動畫,一個每秒2.3個單位的慢跑動畫,以及一個每秒4個單位的跑步動畫, 從下拉菜單中選擇Speed選項將根據(jù)這些值為三個動畫設置參數(shù)范圍和閾值.因此,如果您將speed參數(shù)設置為3.0,它將混合jog和run,并對jog略有偏向。
2D Blending
?Blend Type 為2D的選項,表示需要兩個參數(shù)
2D Simple Directional: 在同一個方向上只能有一個動畫比如“walk forward”, “walk backward”, “walk left”, and “walk right”,或者 “aim up”, “aim down”, “aim left”, and “aim right”. Optionally a single motion at position (0, 0) can be included, such as “idle” or “aim straight”. In the Simple Directional t不能在同一方向上有多個動畫,比如, such as “walk forward” and “run forward”.
2D Freeform Directional: 在同一方向上可以有多個動畫, for example “walk forward” and “run forward”. In the Freeform Directional type the set of motions should always include a single motion at position (0, 0), such as “idle”.
2D Freeform Cartesian: 不管動畫的方向,都是可以的. With Freeform CartesianX參數(shù)和Y參數(shù)可以代表不同的概念, 比如角速度和線速度. An example would be motions such as “walk forward no turn”, “run forward no turn”, “walk forward turn right”, “run forward turn right” etc.
Direct:這種類型的混合樹允許用戶直接控制每個節(jié)點的權重。適用于面部形狀或隨機空閑混合。
設置完Blend Type為2D后, 選擇兩個 Animation Parameters控制 Blend Tree.比如 velocityX (strafing) and velocityZ (forward speed).
?對應的,第一個參數(shù)控制posx,第二個參數(shù)控制posy,
?每個motion 都用一個棱形表示,拖動紅點,表示當前參數(shù)的值,棱形的外圍有個圓圈,表示紅點在該位置是,該動畫的權重或者說是影響范圍
Direct Blending
?Direct Blend Tree可以直接通過參數(shù)來控制動畫播放的權重,在面部表情豐富的時候,可以使用這個,因為同時有可能做多個動作,比如皺眉,撅嘴.
?
Additional Blend Tree Options
The options below are common to both 1D and 2D blending.
Time Scale
頂部帶有時鐘圖標的列,來更改動畫剪輯的播放速度. The Adjust Time Scale > Homogeneous Speed 按鈕重新調整剪輯的速度,使其與參數(shù)選擇的最小值和最大值相一致,但保持與最初相同的相對速度。
Note? Adjust Time Scale 只有當是 Animation Clips而不是child Blend Trees.才可以使用
Mirroring
可以鏡像人形動畫,節(jié)省內存 humanoid Animation
Animator Override Controllers
?Animator Override Controller 就是復制一份Animator Controller,這樣就可以用同一種結構,比如你的游戲可能有各種各樣的NPC類型,但每種類型(地精、食人魔、精靈等)都有自己獨特的動畫,用于行走、閑蕩、坐著等。它們動畫的結構都是一樣的,比如先行走,在坐下,但是動畫片段不一樣,這樣就可以為每個怪物創(chuàng)建一個Animator Override Controller,使用相同的動畫結構,但是動畫片段不一樣
在Asset面板右鍵創(chuàng)建,它和Animator Controller很像,然后把需要復制的拖到上面
然后,你可以為附加單獨的動畫片段
?它和普通的Animator Controller 一樣使用
Retargeting of Humanoid animations
humanoid animations最重要的特點就是動畫重定向,就是你可以把一樣的動畫設置應用到不同的角色身上
,重定向只對類人型的模型有用 humanoid models,并且配置了 Avatar
Recommended Hierarchy structure
?下面是它的步驟:
?1.創(chuàng)建一個空物體添加上character collider組件
?2.在該物體下放置一個角色模型,掛上animator組件,并由自己的avatar,和animator controller
?3.上面的角色是用的是本身的動畫結構,然后再給空物體放置一個類人型的角色,比如泰迪熊,關閉上一個角色模型,給泰迪熊的animator controller掛上上一個角色模型的animator controller,這樣他倆就用的是一套動畫結構和動畫
?
Performance and optimization:動畫優(yōu)化性能
Animation system Controllers
?Animator 如果沒有 Controller 掛在到它上面,不會消耗性能
如果只包含一個簡單的動畫片段,用animation比用animator好,因為animator需要一個緩存,來緩存blend動畫
scale curves 縮放動畫比移動和旋轉動畫要耗性能 translation and rotation curves.
Note: 但是如果縮放動畫,就是一個常量值,即動畫曲線是一條水平線,他不會消耗性能
Layers
大多數(shù)時候Unity是評估動畫,并保持動畫層和動畫狀態(tài)機的開銷最小. 向狀態(tài)機添加一個新層,不管開沒開啟同步,他消耗的性能屈居于該層播放什么動畫和使用什么blend tree,如果這一層的權重是0,則在更新時跳過這一層.
Humanoid vs. Generic animation types
下面是一些小技巧:
- 在導入人形動畫時, Humanoid animation,使用 Avatar Mask(class-AvatarMask) 去掉 IK Goals 或者 finger
- 在導入通用動畫時Generic, using root motion比較耗性能
Scene-level optimization
編寫腳本時注意:
- 使用hash表來訪問animator的參數(shù)變量
- Implement a small AI Layer to control the Animator. You can make it provide simple callbacks for OnStateChange, OnTransitionBegin, and other events.
- Use State Tags to easily match your AI state machine to the Unity state machine.
- Use additional curves to simulate events.
- Use additional curves to mark up your animations; for example, in conjunction with target matching
?
Runtime Optimizations
設置 animators’s Culling Mode to Based on Renderers, and 關閉the skinned mesh renderer’s Update When Offscreen 屬性.這樣如果角色不在屏幕范圍內,就會不更新動畫
?
Animator component
Properties
| Controller | The animator controller attached to this character. | |
| Avatar | The Avatar for this character. (If the Animator is being used to animate a humanoid character) | |
| Apply Root Motion | Select whether to control the character’s position and rotation from the animation itself or from script. | |
| Update Mode | 這允許您選擇動畫器何時更新,以及應該使用哪個時間刻度。 | |
| ? | Normal | The Animator 和Update 一起更新,動畫的速度和當前的timescale匹配,如果timescale 變小,動畫也會播放的慢 |
| ? | Animate Physics | The animator和 FixedUpdate 同步,也就是物理系統(tǒng),如果動畫需要和物理系統(tǒng)交互,使用這個模式. ,比如角色可以推動 rigidbody物體 |
| ? | Unscaled Time | The animator is updated in-sync with the Update call, but the animator’s speed ignores the current timescale and animates at 100% speed regardless. This is useful for animating a GUI system at normal speed while using modified timescales for special effects or to pause gameplay. |
| Culling Mode | Culling mode you can choose for animations. | |
| ? | Always Animate | 始終播放動畫,即使角色不在屏幕內 |
| ? | Cull Update Transforms | 當這些物體的(重定向Retarget, IK and write of Transforms )render關閉時,不更新 |
| ? | Cull Completely | 當渲染器不可見時,動畫將完全禁用 |
?
Animation transitions
Animation transitions 就是各個動畫片段之間的連線. 就是從一個狀態(tài)轉向另一個狀態(tài). 你可以為它設置條件
例如,你的角色可能有一個“巡邏”動畫和一個“睡眠”動畫。您可以將巡邏和睡眠之間的轉換設置為僅在“alert”參數(shù)值低于某一水平時發(fā)生。
?也可以給轉換命名:
Transition properties
| Has Exit Time | Exit Time 是一個不依賴于condition的特殊的參數(shù). 它代表了動畫的標準化時間,就是表示的是百分比 Exit Time. |
| Settings | Fold-out menu containing detailed transition settings as below. |
| Exit Time | 如果勾選上Has Exit Time ,這個值表示,動畫播放到什么多少的時候開始轉換到下一個動畫. 比如0.75就表示該動畫播放到75%的時候,就開始轉換. 對于循環(huán)動畫,exit time小于 1的效果比較好 如果Exit Time大于1 ,比如 3.5 ,表示進行3圈半的循環(huán). |
| Fixed Duration | 如果打開 Fixed Duration , 轉換時間以秒為單位進行解釋. 如果關閉 Fixed Duration 轉換時間以動畫的絕對時間計算 |
| Transition Duration | 轉換的時間,取決于當前動畫的時間. 在過渡圖中,可以將其可視化為兩個藍色標記之間的部分。 |
| Transition Offset | 在轉換為目標狀態(tài)時開始播放的時間偏移量。例如,0.5的值表示目標狀態(tài)在其自身時間軸的50%處開始播放 |
| Interruption Source | 利用這一點來控制這種轉換可能被中斷的情況 |
| Ordered Interruption | 當前轉換能否被其他轉換打斷,不根據(jù)順序 |
| Conditions | 一個轉換可以有一個條件,多個條件,甚至沒有條件,當沒有條件的時候, Exit Time就會起作用,也就是說沒有條件的時候,當動畫播放到exit time的時候,才會轉換到下一個動畫, 如果有條件,轉換必須在條件達到時才會觸發(fā) ?如果勾選上Has Exit Time且有條件,? Unity Editor 會優(yōu)先考慮exit time ,即當動畫播放到exit time的時候,才會考慮條件是不是滿足,從而決定轉換到下一個動畫 |
Transition graph
To manually adjust the settings listed above, you can either enter numbers directly into the fields or use the transition graph. The transition graph modifies the values above when the visual elements are manipulated.
?這是轉換圖的解釋很清楚了有沒有,恩?baby
?
總結
以上是生活随笔為你收集整理的unity 之 Animation 二 BlendTree的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 挖出api接口的重要性
- 下一篇: 奇数位升序偶数位降序的链表进行排序