最近学Shader有点心得,做了一个SLG画线模块

最近学Shader有点心得,做了一个SLG画线模块。

性能有能再提高的地方,欢迎大佬指点。大笑

 

1.介绍

 

  • 画线模块,可用于SLG游戏行军线。
  • 多个不同速度、颜色、长度的线在一个DrawCall中实现。
  • 材质中ShowLength控制两端显示的长度,AlphaLength控制渐隐长度,AllShowLength是一个临界值,长度小于它的线全部显示,长度大于它的线出现中间渐隐消失效果。(ShowLength、AlphaLength控制这两个效果)
  • 只有一个C#脚本文件和一个Shader,100多行代码。

2.使用demo

 

  • 画线:int handle=LineManager.Instance.DrawLine(Vector3 start,Vector3 end,Color color,float speed);
  • 回收线:LineManager.Instance.BackLine(int handle);

多次调用画线效果:

 

  • 线中间不显示,两端渐隐

image

 

  • 线全部显示 

 

3.C#代码部分

[csharp] view plain  copy
 
  1. using System.Collections.Generic;  
  2. using UnityEngine;  
  3. using LineHandle = System.Int32;  
  4.   
  5. class LineManager  
  6. {  
  7.     private GameObject m_root = null;   
  8.     private GameObject m_origin = null;  
  9.     private Stack<GameObject> m_linePool = null;  
  10.     private Dictionary<int, GameObject> m_drawLines = null;  
  11.     private int m_index = 0;  
  12.  
  13.     #region 外部接口  
  14.     //参数:起始点、终止点、颜色、速度  
  15.     public LineHandle DrawLine(www.078881.cn    Vector3 start,Vector3 end,Color color,float speed)  
  16.     {  
  17.         //从池中拿出  
  18.         GameObject line = getFromPool();  
  19.         Transform lineTransform = line.transform;  
  20.         Transform childTransform = lineTransform.GetChild(0);  
  21.   
  22.         Mesh mesh = childTransform.GetComponent<MeshFilter>().mesh;  
  23.         var vertices = mesh.vertices;  
  24.           
  25.         //设置颜色  
  26.         var colors = new Color[vertices.Length];  
  27.         for (int i = 0; i < colors.Length; i++)  
  28.         {  
  29.             colors[i] = color;  
  30.         }  
  31.         mesh.colors = colors;  
  32.   
  33.         //设置浮transform位置朝向,子transform缩放  
  34.         Vector3 position = new Vector3((www.leyou2.net/ start.x+end.x)/2,(start.y+end.y)/2,(start.z+end.z)/2);  
  35.         Vector3 scale =new Vector3(1,Vector3.Distance(start,end),1);  
  36.         Vector3 direction = (end - start).normalized;  
  37.         lineTransform.localPosition www.yongshiyule178.com= position;  
  38.         childTransform.localScale = scale;  
  39.         lineTransform.localRotation =www.meiwanyule.cn  Quaternion.LookRotation(direction);  
  40.   
  41.         //设置Y轴缩放,速度值  
  42.         var uv2 = new Vector2[vertices.Length];  
  43.         for (int i = 0; i < uv2.Length; i++)  
  44.         {  
  45.             uv2[i] = new Vector2(childTransform.localScale.y / childTransform.localScale.x, speed);  
  46.         }  
  47.         mesh.uv2 = uv2;  
  48.   
  49.         //放入字典  
  50.         m_drawLines.Add(++m_index, line);  
  51.   
  52.         return m_index;  
  53.     }  
  54.     //参数:索引  
  55.     public void BackLine(LineHandle handle)  
  56.     {  
  57.         if(m_drawLines.ContainsKey(handle))  
  58.         {  
  59.             GameObject line = m_drawLines[handle];  
  60.             m_drawLines.Remove( www.leyouzaixian2.com handle);  
  61.             backToPool(line);  
  62.         }  
  63.     }  
  64.     #endregion  
  65.  
  66.     #region 内部实现  
  67.     private void Init()  
  68.     {  
  69.         m_origin = Resources.Load("DrawLine/Line") as GameObject;  
  70.         m_linePool = new Stack<GameObject>(23);  
  71.         m_root = new GameObject("LineRoot");  
  72.         m_drawLines = new Dictionary<int, GameObject>(23);  
  73.         m_index = 0;  
  74.     }  
  75.   
  76.     private GameObject getFromPool()  
  77.     {  
  78.         if (m_linePool.Count==0)  
  79.         {  
  80.             GameObject gameObject = GameObject.Instantiate(m_origin);  
  81.             gameObject.transform.parent = m_root.transform;  
  82.             return gameObject;  
  83.         }  
  84.         else  
  85.         {  
  86.             return m_linePool.Pop();  
  87.         }  
  88.     }  
  89.   
  90.     private void backToPool(GameObject line)  
  91.     {  
  92.         line.transform.localPosition = new Vector3(1000,1000,1000);  
  93.         m_linePool.Push(line);  
  94.     }  
  95.     #endregion  
  96.  
  97.     #region singleton  
  98.     private static LineManager _instance = null;  
  99.     private LineManager() { Init(); }  
  100.     public static LineManager Instance  
  101.     {  
  102.         get {  
  103.             if (_instance==null)  
  104.             {  
  105.                 _instance = new LineManager();  
  106.             }  
  107.             return _instance;  
  108.         }  
  109.         private set { }  
  110.     }  
  111.     #endregion  
  112. }  

 

4.Shader代码部分

[plain] view plain  copy
 
    1. Shader "lineShader"{  
    2.         Properties  
    3.         {  
    4.             _MainTex("Line Texture",2D) = "white"{}  
    5.         _ShowLength("Show Length",Float)=6.0 //展现的长度  
    6.         _AlphaLength("Alpha Length",Float)=5.0 //有透明效果的长度  
    7.         _AllShowLength("AllShow Length",Float)=10 //临界值,如果线比它短,那么全都显示  
    8.         }  
    9.          SubShader{  
    10.        Tags{ "Queue" = "AlphaTest" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout" }  
    11.     Pass  
    12.     {  
    13.            Tags{ "LightMode" = "ForwardBase" }  
    14.            ZWrite Off  
    15.            Blend SrcAlpha OneMinusSrcAlpha  
    16.   
    17.   
    18.         CGPROGRAM  
    19.         #pragma vertex vert  
    20.         #pragma fragment frag  
    21.   
    22.         #include "Lighting.cginc"  
    23.   
    24.         sampler2D _MainTex;  
    25.         float4 _MainTex_ST;  
    26.     float _ShowLength;  
    27.     float _AlphaLength;  
    28.     float _AllShowLength;  
    29.   
    30.         struct a2v  
    31.         {  
    32.             float4 vertex:POSITION;  
    33.             float4 texcoord:TEXCOORD0;  
    34.             fixed4 color:COLOR0;  
    35.         float2 uv2 : TEXCOORD1; //第二套UV:x分量存储y轴缩放,y分量存储滚动速度               
    36.         };  
    37.   
    38.         struct v2f  
    39.         {  
    40.             float4 pos:SV_POSITION;  
    41.             float2 uv:TEXCOORD1;  
    42.         fixed4 color:COLOR1;  
    43.         float2 uv2:TEXCOORD2; //x分量存储Y轴缩放固定值,y分量存储滚动前uv.y  
    44.         };  
    45.   
    46.         v2f vert(a2v v)  
    47.         {  
    48.             v2f o;  
    49.             o.pos = UnityObjectToClipPos(v.vertex);  
    50.             o.uv.xy = TRANSFORM_TEX(v.texcoord,_MainTex);  
    51.             o.uv.y *=v.uv2.x;//Y轴缩放  
    52.             o.color=v.color;  
    53.                   
    54.         o.uv2=o.uv;  
    55.         o.uv2.x=v.uv2.x; //o.uv2.x存储Y轴缩放固定值,y轴当前uv.y  
    56.             o.uv.y -=v.uv2.y* _Time.z; //Y轴随时间偏移  
    57.             return o;  
    58.         }  
    59.   
    60.         fixed4 frag(v2f i) :SV_Target  
    61.         {  
    62.             fixed4 color = tex2D(_MainTex,i.uv.xy);  
    63.             //后半段透明值与前半段透明值,取最大值  
    64.         i.color.a=max(clamp((_ShowLength-(i.uv2.x-i.uv2.y))/_AlphaLength,0,1),clamp((_ShowLength-i.uv2.y)/_AlphaLength,0,1)); //i.uv2.y存储此片元的UV.y  
    65.             //当比_AllShowLength小的时候全部显示  
    66.             i.color.a+=clamp(_AllShowLength-i.uv2.x,0,1); //大于0  
    67.         i.color.a *= color.a;  
    68.             return i.color;  
    69.         }  
    70.   
    71.         ENDCG  
    72.         }  
    73.         }  
    74.         //Fallback "VertexLit"  
    75. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值