GAMESH

游戏美术动画unity资源搬运工!

剧情党,完美控!
桐崎千棘
当前位置:首页 > 学习 > 正文内容

unity使用json三维模型的变形动画

admin3年前 (2020-04-20)学习34260

有时我们只需要网格中顶点的子集即可动画,而无需完整的骨架,例如用于面部动画的一组嘴形或脸部顶点。一种简单的方法是使用“变形目标动画”。在此动画中,我们混合顶点而不是骨骼,这就是为什么它也被称为逐顶点动画的原因。动画存储为原始网格顶点的一系列变形版本。变形版本称为“变形目标”,而原始版本称为“基础”。我们可以使用权重数组在基本目标和变形目标之间进行混合:

v e r t e x = b a s e + i = 1 n ( w i ( t a r g e t i b a s e ) )

通过使用jd文件格式,我们可以在pos0和pos1,normal0和normal1之间进行插值

来自Unity Wiki的以下示例仅使用Lerp在2个网格之间混合顶点:

using UnityEngine;
 /// REALLY IMPORTANT NOTE./// When using the mesh morpher you should absolutely make sure that you turn/// off generate normals automatically in the importer, or set the normal angle to 180 degrees./// When importing a mesh Unity automatically splits vertices based on normal creases./// However the mesh morpher requires that you use the same amount of vertices for each mesh and that/// those vertices are laid out in the exact same way. Thus it wont work if unity autosplits vertices based on normals.[RequireComponent(typeof(MeshFilter))]public class MeshMorpher : MonoBehaviour{   public Mesh[] m_Meshes;
   public bool m_AnimateAutomatically = true;
   public float m_OneLoopLength = 1.0F; /// The time it takes for one loop to complete   public WrapMode m_WrapMode = WrapMode.Loop;
   private float m_AutomaticTime = 0;
 
   private int m_SrcMesh = -1;
   private int m_DstMesh = -1;
   private float m_Weight = -1;
   private Mesh m_Mesh;
 
   /// Set the current morph in       public void SetComplexMorph(int srcIndex, int dstIndex, float t)   {      if (m_SrcMesh == srcIndex && m_DstMesh == dstIndex && Mathf.Approximately(m_Weight, t))         return;
 
      Vector3[] v0 = m_Meshes[srcIndex].vertices;
      Vector3[] v1 = m_Meshes[dstIndex].vertices;
      Vector3[] vdst = new Vector3[m_Mesh.vertexCount];
      for (int i = 0; i < vdst.Length; i++)         vdst[i] = Vector3.Lerp(v0[i], v1[i], t);
 
      m_Mesh.vertices = vdst;
      m_Mesh.RecalculateBounds();
   } 
   /// t is between 0 and m_Meshes.Length - 1.   /// 0 means the first mesh, m_Meshes.Length - 1 means the last mesh.   /// 0.5 means half of the first mesh and half of the second mesh.   public void SetMorph(float t)   {      int floor = (int)t;
      floor = Mathf.Clamp(floor, 0, m_Meshes.Length - 2);
      float fraction = t - floor;
      fraction = Mathf.Clamp(t - floor, 0.0F, 1.0F);
      SetComplexMorph(floor, floor + 1, fraction);
   } 
   void Awake()   {      enabled = m_AnimateAutomatically;
      MeshFilter filter = GetComponent(typeof(MeshFilter)) as MeshFilter;
 
      // Make sure all meshes are assigned!      for (int i = 0; i < m_Meshes.Length; i++)      {         if (m_Meshes[i] == null)         {            Debug.Log("MeshMorpher mesh  " + i + " has not been setup correctly");
            m_AnimateAutomatically = false;
            return;
         }      } 
      //  At least two meshes      if (m_Meshes.Length < 2)      {         Debug.Log("The mesh morpher needs at least 2 source meshes");
         m_AnimateAutomatically = false;
         return;
      } 
      filter.sharedMesh = m_Meshes[0];
      m_Mesh = filter.mesh;
      int vertexCount = m_Mesh.vertexCount;
      for (int i = 0; i < m_Meshes.Length; i++)      {         if (m_Meshes[i].vertexCount != vertexCount)         {            Debug.Log("Mesh " + i + " doesn't have the same number of vertices as the first mesh");
            m_AnimateAutomatically = false;
            return;
         }      }   } 
   void Update()   {      if (m_AnimateAutomatically)      {         float deltaTime = Time.deltaTime * (m_Meshes.Length - 1) / m_OneLoopLength;
         m_AutomaticTime += deltaTime;
         float time;
         if (m_WrapMode == WrapMode.Loop)            time = Mathf.Repeat(m_AutomaticTime, m_Meshes.Length - 1);
         else if (m_WrapMode == WrapMode.PingPong)            time = Mathf.PingPong(m_AutomaticTime, m_Meshes.Length - 1);
         else            time = Mathf.Clamp(m_AutomaticTime, 0, m_Meshes.Length - 1);
 
         SetMorph(time);
      }   }}

转载http://www.cgdev.net/blog/476.html

扫描二维码推送至手机访问。

本サイト上に掲載の文章、画像、写真などを無断で複製することは法律で禁じられています。全ての著作権はGAMESHに帰属します。

本文链接:https://pylblog.com/post/178.html

标签: 3dmaxunity3d
分享给朋友:

相关文章

模型收集

模型收集

cgjoy大神免费放出的模型,收集起来说不定哪天用得上呢!链接:https://pan.baidu.com/s/1ykhTcJswn94HfJYndFRVXA 提取码:vp99 ...

3dmax射击类游戏动作库

3dmax射击类游戏动作库

淘宝买的一款射击类动作源文件,3dmax的biped骨骼绑定,基本覆盖了常用的射击类游戏动作!包含射击动画,换枪动画,换子弹动画,待机动画、翻墙动画、跳跃动画、下蹲动画、各方向走和各个方向跑以及各种死...

3dmax快速对齐插件

3dmax快速对齐插件

平时在工作中会遇到需要把两个物体对齐的时候,如果没有动画好说,max自带的功能足以!但有时候几百帧动画需要对齐那就尴尬了!所以我写了一个插件方便大家快速对齐动画,模型、虚拟体、相机和骨骼都可以!相对论...

3dmax bone骨骼无法k帧

3dmax bone骨骼无法k帧

今天在做文件时遇到一个小问题,就是有一根bone始终无法记录关键帧,查找了半天愣是没法解决。正当要放弃的时候我在菜单栏动画栏发现了一个地方不对劲,凭借多年动画制作经验发现有个地方多了一个勾,原来是我把...

福利:精灵剑士pbr游戏模型

福利:精灵剑士pbr游戏模型

3种版本:Base,Half Nude,Full Nude。 全装甲:57.406,半裸:50.668,全裸:36.924武器:4.258 模型高度:178厘米 FBX文件夹包含...

3dmax动画技巧-将鱼线浮出水面的部分固定(浮漂)

3dmax动画技巧-将鱼线浮出水面的部分固定(浮漂)

这周做一个钓鱼的项目时策划要求待机过程中鱼线露出水面的部分需要固定做一个浮漂,但是我的鱼线是用飘带插件解算的,下面始终在来回摆动。这就不得不需要我想办法去固定住露出水面的那一根骨骼了!首先我尝试使用i...

发表评论

访客

看不清,换一张

QQ登录
◎欢迎参与讨论,请在这里发表您的看法和观点。