目录
1. Blend Trees
1.1 Using Blend Trees
1.2 1D Blending
1.2.1 Parameter Range
1.2.2 Thresholds
1.2 2D Blending
1.2.1 Direct Blending
1.3 Additional Blend Tree Options
1.3.1 Time Scale
1.3.2 Mirroring
1.4 Animator Override Controllers
2. Retargeting of Humanoid animations
2.1 Recommended Hierarchy structure
3. 动画性能优化
3.1 Animation system
3.2 Humanoid vs. Generic animation types
3.3 Scene-level optimization
3.4 Runtime Optimizations
4. Animator component
4.1 Animation curve information
5.1 Animation transitions
5.1 Transition properties
5.1.1 Transition interruption
5.2 Transition graph
5.2.1 Transitions between Blend Tree states
5.3.2 Conditions
1. Blend Trees
在游戏中一个常见的场景是将两个或多个相似的动作混合在一起. 比如:根据角色的速度混合行走和跑步的动画、角色在跑步时向左或向右倾斜
Transitions 和 Blend Trees的不同:
- Transitions:从一个state过渡到另一个state.
- Blend Trees:通过混合参数来混和多个动画
为了使过渡效果更好,clip中的动作必须在相同时间点上同时发生,比如:跑步和行走它们在某一时刻的脚应该在同一个位置。
1.1 Using Blend Trees
创建:
- 右键 Create State > From New Blend Tree
- 双击 Blend Tree 进入 Blend Tree 视图,在.Blend Tree 视图双击空白处,回到base视图
- 通过选中Blend Tree 在属性面板上点击+号添加动画片段或者右键Blend Tree 添加动画片段
blend tree 并不会改变root position,它是以差值的方式,让动画过渡
1.2 1D Blending
即:用一个参数,控制动画混合
可以自定混合参数,通过添加Animation Parameter,但是混合参数必须是float类型的
该图表显示了当参数在最小值和最大值之间变化时,对每个动画的影响,每个motion都显示为一个小的蓝色金字塔(第一个和最后一个只显示了一半),如果点击并按住鼠标左键,相应的motion在下面的列表中突出显示. 金字塔的顶点表示其动画权值为1,其他动画权值为0。这也被称为运动的阈值.(threshold)
1.2.1 Parameter Range
参数的范围被是图表中的左右两个数,可以通过点击左右两边的数字更改它 ,比如上面的是-1到1,可以点击数字,更改它的值,其实,第一个motion的threshold就是它的最小值,最后一个motion的threshold就是它的最大值,
1.2.2 Thresholds
如果 “Automate Thresholds” 属性没有勾选上的话,可以自定义阈值
如果 “Automate Thresholds” 勾选上,Unity默认以平均值的方法,过度每一个motion,比如有五个motion,范围是[ –90,90], thresholds 就会从–90, –45, 0, +45 and +90 的顺序
Compute Thresholds:表示从clip里面获取混合参数的值,它包括, velocity x, y, or z,以度或弧度为单位的角速度. 如果参数需要和这些相关连,就使用下面的值
Property: | Function: |
Speed | 根据动画片段的速度来定义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.5m/s的步行动画,一个2.3m/s的慢跑动画,以及一个4m/s的跑步动画, 从下拉菜单中选择Speed选项将根据这些值为三个动画设置参数范围和阈值.因此,如果您将speed参数设置为3.0,它将混合jog和run,并对jog略有偏向。
1.2 2D Blending
即:需要两个参数混合动画,不同的类型,在混合方式上不同
- 2D Simple Directional: 在同一个方向上只能有一个动画,比如:“walk forward”, “walk backward”, “walk left”, and “walk right”,或者 “aim up”, “aim down”, “aim left”, and “aim right”,不能在同一方向上有多个动画,比如, “walk forward” and “run forward”.在Postion(0,0)的位置,可选的包含一个动画,比如idle
- 2D Freeform Directional: 在同一方向上可以有多个动画, 比如: “walk forward” and “run forward”. 在Postion(0,0)的位置,必须包含一个动画,比如idle
- 2D Freeform Cartesian: 不管动画的方向,都是可以的. With Freeform CartesianX参数和Y参数可以代表不同的概念, 比如角速度和线速度.
- Direct:多个参数控制动画。
选择两个 Animation Parameters控制 Blend Tree.比如 velocityX (strafing) and velocityZ (forward speed).
对应的,第一个参数控制posx,第二个参数控制posy,
每个motion 都用一个棱形表示,红点表示当前参数的值,棱形的外围有个圆圈,表示红点在该位置时的影响范围
1.2.1 Direct Blending
使用多个参数控制动画的混合
1.3 Additional Blend Tree Options
下面的选项对于1D和2D混合都是通用的
1.3.1 Time Scale
顶部带有时钟图标的列,来更改动画剪辑的播放速度. The Adjust Time Scale > Homogeneous Speed 按钮重新调整剪辑的速度,使其与参数选择的最小值和最大值相一致,但保持与最初相同的相对速度。Adjust Time Scale 只有当所有的Clips不是Blend Trees类型时才可以使用
1.3.2 Mirroring
可以镜像人形动画,节省内存
1.4 Animator Override Controllers
Animator Override Controller 就是复制一份Animator Controller,这样就可以用同一种结构,比如有各种各样的NPC,但每种类型(地精、食人魔、精灵等)都有自己独特的动画,用于行走、闲荡、坐着等。它们动画的结构都是一样的,比如先行走,在坐下,但是动画片段不一样,这样就可以为每个怪物创建一个Animator Override Controller,使用相同的动画结构,但是动画片段不一样
在Asset面板右键创建,它和Animator Controller很像,然后把需要复制的拖到上面
然后,你可以为附加单独的动画片段
它和普通的Animator Controller 一样使用
2. Retargeting of Humanoid animations
humanoid animations最重要的特点就是动画重定向,就是你可以把一样的动画设置应用到不同的角色身上,重定向只对配置了 Avatar的humanoid models有用 ,因为Avtar确定了动画的骨骼信息
2.1 Recommended Hierarchy structure
下面是它的步骤:
- 创建一个空物体添加上character collider组件
- 在该物体下放置一个角色模型,挂上animator组件,赋值avatar、aniator controller
- 上面的角色是用的是本身的动画结构,然后再给空物体放置一个泰迪熊,关闭上一个角色模型,给泰迪熊的animator controller挂上上一个角色模型的animator controller,这样它俩就用的是一套动画结构和动画
3. 动画性能优化
3.1 Animation system
- Animator 组件如果没有 Controller ,不会消耗性能
- 如果只包含一个简单的动画片段,用animation比用animator好,因为animator需要一个缓存,来缓存blend动画
- scale curves 缩放动画比移动和旋转动画要耗性能,但是如果缩放动画,就是一个常量值,即动画曲线是一条水平线,他不会消耗性能
- Layers消耗的性能取决于该层播放什么动画和使用什么blend tree,如果这一层的权重是0,则在更新时跳过这一层.
3.2 Humanoid vs. Generic animation types
- 在导入Humanoid animation时,使用Avatar Mask(class-AvatarMask) 去掉 IK Goals 或者 finger动画,如果不需要
- 在导入Generic动画时,尽量不使用root motion
3.3 Scene-level optimization
编写脚本时注意:
- 使用Animator.StringToHash表来访问参数变量
- 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
3.4 Runtime Optimizations
设置 animators的Culling Mode 为Based on Renderers, 关闭skinned mesh renderer’s Update When Offscreen 属性.这样如果角色不在屏幕范围内,就会不更新动画
4. Animator component
Property: | Function: | |
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 同步,也就是物理系统,如果动画需要和物理系统交互,使用这个模式. ,比如角色可以推动 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 | 当渲染器不可见时,动画将完全禁用 |
4.1 Animation curve information
Animator组件底部的信息框提供了Animator控制器使用的所有clip中使用的数据。
animation clip 包含有 “curves”组成的数据,表示值的变化
下表解释了每项数据的含义:
Label | Description |
Clip Count | clip的总数量 |
Curves (Pos, Rot & Scale) | Unity用于动画化游戏对象的位置、旋转或比例的曲线总数。这些是用于游戏对象,不是人形动画, When animating a humanoid avatar, these curves would show up a count for extra non-muscle bones such as a tail, flowing cloth or a dangling pendant. 如果有一个humanoid animation 但是有一个该 animation curves,说明有不必要的动画 |
Muscles | The number of muscle animation curves used for humanoid animation by this Animator. These are the curves used to animate the standard humanoid avatar muscles. As well as the standard muscle movements for all the humanoid bones in Unity’s standard avatar, this also includes two “muscle curves” which store the root motion position and rotation animation. |
Generic | 其它属性动画的曲线数量,比如颜色东海 |
PPtr | The total count of sprite animation curves (used by Unity’s 2d system) |
Curves Count | 动画曲线的总组合数 |
Constant | 常量动画曲线的数量. |
Dense | The number of animation curves that are optimized using the “dense” method of storing data (discrete values which are interpolated between linearly). This method uses significantly less memory than the “stream” method. |
Stream | The number of animation curves using the “stream” method of storing data (values with time and tangent data for curved interpolation). This data occupies significantly more memory than the “dense” method. |
如果animation clips导入的时候设置了 “Anim Compression”为 “Optimal” ,Unity将会决定是否用dense 或者 stream方法来存储数据
5.1 Animation transitions
Animation transitions 就是各个动画片段之间的连线. 就是从一个状态转向另一个状态. 可以为它设置条件
例如,可能有一个“巡逻”动画和一个“睡眠”动画。可以将巡逻和睡眠之间的转换设置为仅在“alert”参数值低于某一水平时发生。
也可以给转换命名:
5.1 Transition properties
Property | Function |
Has Exit Time | Exit Time 是一个不依赖于condition的特殊的参数. 它代表了动画的标准化时间,就是表示的是百分比 Exit Time. |
Settings | |
Exit Time |
|
Fixed Duration | 如果打开 Fixed Duration , 转换时间以秒为单位进行播放. 如果关闭 Fixed Duration 转换时间以动画的绝对时间计算 |
Transition Duration | 转换的过渡时间 |
Transition Offset | 在转换为目标状态时,开始播放的时间偏移量。例如,0.5的值表示目标状态在其自身时间轴的50%处开始播放 |
Interruption Source | 利用这一点来控制转换可能被中断的情况 |
Ordered Interruption | 当前转换能否被其他转换打断,不根据顺序 |
Conditions | 一个转换可以有一个条件,多个条件,甚至没有条件,当没有条件的时候, Exit Time就会起作用,也就是说没有条件的时候,当动画播放到exit time的时候,才会转换到下一个动画, 如果有条件,转换必须在条件达到时才会触发 |
5.1.1 Transition interruption
Interruption Source 和 Ordered Interruption 属性来控制被其他clip 打断时的操作
从概念上讲,中断的原理就是将转换排队,然后解析从插入的第一个转换到最后一个转换
AnyState 总是首先添加到队列中, 其它的state根据 Interruption Source的值来添加:
Value | Function |
None | Don’t add any more transitions. |
Current State | Queue the transitions from the current state. |
Next State | Queue the transitions from the next state. |
Current State then Next State | Queue the transitions from the current state, then queue the ones from the next state. |
Next State then Current State | Queue the transitions from the next state, then queue the ones from the current state. |
Ordered Interruption property
它改变了state 执行的顺序
下面的选项表示,执行state队列到什么时候结束
Value | Ends when |
Checked | A valid transition or the current transition has been found. |
Unchecked | A valid transition has been found. |
AnyState transition可以被自己打断。
5.2 Transition graph
5.2.1 Transitions between Blend Tree states
如果当前或者下一个state 是 Blend Tree 类型, Blend Tree 中的参数,会出现在属性面板中,调整这些值可以预览实时的动画混合效果.
5.3.2 Conditions
如果Has Exit Time开启,且有多个条件,转换只会在到达exit time 时,判断条件成立,才会发生