zoukankan      html  css  js  c++  java
  • Unity3D手游开发日记(8)

    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. }  


    残影+扭曲的效果:

    残影效果:

     
     
  • 相关阅读:
    kotlin实现流读取
    mongo注解详解
    spring 手动注册bean
    mongo 生命周期
    GC类型以及不同类型GC的搭配 1
    GC类型以及不同类型GC的搭配
    Kotlin的高阶函数和常用高阶函数
    通过JVM日志来进行安全点分析
    js中style.display=""无效的解决方法
    Web网页性能管理详解
  • 原文地址:https://www.cnblogs.com/czaoth/p/5785800.html
Copyright © 2011-2022 走看看