2D游戏的残影很简单,美术做序列帧图片就行了,那么3D游戏的残影美术做不了,得靠程序员动态创建模型来处理.

 

实现原理也很简单:

 

1.间隔一定时间创建一个残影模型

 

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. GameObject go = GameObject.Instantiate(origin, pos, dir) as GameObject;  

 

2.对残影模型采用特殊的shader,要简单高效

 

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public class MotionGhost  
  2. {  
  3.     public GameObject m_GameObject;  
  4.     public List<Material> m_Materials;  
  5.     public float m_DurationTime;  
  6.     public float m_FadeTime;  
  7.     public float m_Time;  
  8.   
  9.     public MotionGhost(GameObject go, Color color, float durationTime, float fadeTime)  
  10.     {  
  11.         m_GameObject = go;  
  12.         m_DurationTime = durationTime;  
  13.         m_FadeTime = fadeTime;  
  14.         m_Time = durationTime;  
  15.   
  16.         if (MotionGhostMgr.Instance.m_MaterialMotionGhost == null)  
  17.             MotionGhostMgr.Instance.m_MaterialMotionGhost = Resources.Load("Material/MotionGhost");  
  18.   
  19.         m_Materials = new List<Material>();  
  20.         foreach (Renderer renderer in go.GetComponentsInChildren<Renderer>())  
  21.         {  
  22.             if (renderer as MeshRenderer || renderer as SkinnedMeshRenderer)  
  23.             {  
  24.                 // 身体和武器  
  25.                 Material[] newMaterials = new Material[1];  
  26.                 newMaterials[0] = GameObject.Instantiate(MotionGhostMgr.Instance.m_MaterialMotionGhost) as Material;                 
  27.                 if (newMaterials[0].HasProperty("_RimColor"))  
  28.                     newMaterials[0].SetColor("_RimColor", color);  
  29.                 renderer.materials = newMaterials;  
  30.   
  31.                 m_Materials.Add(renderer.material);  
  32.             }  
  33.             else  
  34.             {  
  35.                 // 隐藏粒子  
  36.                 renderer.enabled = false;  
  37.             }  
  38.         }  
  39.     }  
  40. }  


 

3.残影淡入淡出的处理

 

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public void Tick()  
  2. {  
  3.     for (int i = m_MotionGhosts.Count - 1; i >= 0; --i)  
  4.     {  
  5.         m_MotionGhosts[i].m_Time -= Time.deltaTime;  
  6.   
  7.         // 时间到了删除掉  
  8.         if (m_MotionGhosts[i].m_Time <= 0)  
  9.         {  
  10.             GameObject.Destroy(m_MotionGhosts[i].m_GameObject);  
  11.             m_MotionGhosts.RemoveAt(i);  
  12.             --m_Counter;  
  13.             continue;  
  14.         }  
  15.   
  16.         // 开始材质渐变  
  17.         if (m_MotionGhosts[i].m_Time < m_MotionGhosts[i].m_FadeTime)  
  18.         {  
  19.             float alpha = m_MotionGhosts[i].m_Time / m_MotionGhosts[i].m_FadeTime;  
  20.             foreach (Material material in m_MotionGhosts[i].m_Materials)  
  21.             {  
  22.                 if (material.HasProperty("_RimColor"))  
  23.                 {  
  24.                     Color color = material.GetColor("_RimColor");  
  25.                     color *= alpha;  
  26.                     material.SetColor("_RimColor", color);  
  27.                 }  
  28.             }  
  29.         }               
  30.     }  
  31. }  


 

 

 

残影+扭曲的效果:

 

残影效果:

 

 
 
posted on 2016-08-18 22:39  chenzhao  阅读(1303)  评论(0编辑  收藏  举报