Unity通过BlendShape实现面部表情过渡切换Animation教程

2022-07-21,,,,

一、前言

点关注不迷路,持续输出unity干货文章。
嗨,大家好,我是新发。
unity项目中,我们可能需要实现3d角色表情过渡切换,比如像这样子:

unity中如何做这个功能呢?今天就来跟大家讲一下。

二、blendshape(动画师、maya)

maya中,动画师可以使用blendshape绑定好表情,动画师只需要制作好a表情b表情c表情等,通过blendshape就可以很方便地进行表情之间的插值过渡了。
注意blendshape与骨骼动画不同,骨骼动画需要先将网格绑定骨骼,由骨骼来驱动网格;而blendshape不需要骨骼,blendshape很适合用作面部表情的实现。
动画师做好blendshape后,unity客户端程序员将模型导入unity工程中,在unity中勾选模型的import blendshapes

这样,在unity中我们就可以通过blendshape滑块来调节表情的过渡啦。

效果如下:

三、unity控制blendshapes

上面我们是手动调节blendshape的滑块,我们如何通过代码来控制blendshape呢?
有两种方式:

  • 1、直接通过skinnedmeshrenderer组件访问blendshape
  • 2、制作成animation,在animation中控制blendshape,然后由代码来控制animation

1、skinnedmeshrenderer控制blendshape

接口:

// skinnedmeshrenderer.cs
public void setblendshapeweight(int index, float value);

参数index就是blendshape的索引,从0开始,参数value是滑块的值,范围:0~100

2、animation控制blendshape

动画师将表情制作成animation,特定组合的blendshape其实就是一个表情,比如眉毛上抬,眼睛睁大,嘴巴张开这三个组合起来是一个表情,这样做成nanimation动画。

把这些动画组织到animator状态机中。

关于animator的使用,可以参见我之前写的这篇文章:关于unity动画状态机animator使用教程

接着,我们就可以通过播放动画来实现表情的过渡了。

// animator.cs
public void crossfade(string statename, float normalizedtransitionduration);

参数statename是动画状态名称,比如smile,参数normalizedtransitionduration是动画融合归一化时间。
我们可以测试下normalizedtransitionduration分别为00.3f1三种情况的融合效果:
为0的情况:

为0.3f的情况:

为1的情况:

四、结束语

好了。
喜欢unity的同学,不要忘记点击关注,如果有什么unity相关的技术难题,也欢迎留言或私信~
感谢阅读,我们下次再见。

以上就是unity通过blendshape实现面部表情过渡切换animation教程的详细内容,更多关于unity通过blendshape实现面部表情过渡切换animation的资料请关注其它相关文章!

《Unity通过BlendShape实现面部表情过渡切换Animation教程.doc》

下载本文的Word格式文档,以方便收藏与打印。