unity 实现遮罩效果_LOGO闪光效果

来源:互联网 发布:java 文本聚类 编辑:程序博客网 时间:2024/05/17 18:03

  这个效果在很多LOGO及广告宣传中都会用到。商业开发的做法应该是拿一张闪光的图,对其做uv移动,然后和原图两张图混合,这样运算会小很多,需要储存的变量也会小很多。本讲不用图而完全通过计算得出闪光区域,主要是借此加深uv计算实现特殊效果的实现,以及计算uv的方法。代码注释配合图解已经比较详细了,跟着做就行,随便找一张半透明的图来做底图即可。

http://www.xuebuyuan.com/2959899.html


  1. Shader "Custom/LogoShader" {
  2.     Properties {
  3.         _MainTex ("Texture"2D"white" }
  4.     }
  5.     SubShader
  6.     {
  7.    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
  8.    Blend SrcAlpha OneMinusSrcAlpha 
  9.         AlphaTest Greater 0.1
  10.         pass
  11.         {
  12.             CGPROGRAM
  13.             #pragma vertex vert
  14.             #pragma fragment frag
  15.             #include "UnityCG.cginc"
  16.        
  17.             sampler2D _MainTex;
  18.             float4 _MainTex_ST;
  19.            
  20.             struct v2f {
  21.                 float4  pos SV_POSITION;
  22.                 float2  uv TEXCOORD0;
  23.             };
  24.            
  25.             //顶点函数没什么特别的,和常规一样
  26.             v2f vert (appdata_base v)
  27.             {
  28.                 v2f o;
  29.                    o.pos mul(UNITY_MATRIX_MVP,v.vertex);
  30.                 o.uv    TRANSFORM_TEX(v.texcoord,_MainTex);
  31.                 return o;
  32.             }
  33.            
  34.             //必须放在使用其的 frag函数之前,否则无法识别。
  35.             //核心:计算函数,角度,uv,光带的x长度,间隔,开始时间,偏移,单次循环时间
  36.             float inFlash(float angle,float2 uv,float xLength,int interval,int beginTimefloat offXfloat loopTime )
  37.             {
  38.                 //亮度值
  39.                 float brightness =0;
  40.                
  41.                 //倾斜角
  42.                 float angleInRad 0.0174444 angle;
  43.                
  44.                 //当前时间
  45.                 float currentTime _Time.y;
  46.            
  47.                 //获取本次光照的起始时间
  48.                 int currentTimeInt _Time.y/interval;
  49.                 currentTimeInt *=interval;
  50.                
  51.                 //获取本次光照的流逝时间 当前时间 起始时间
  52.                 float currentTimePassed currentTime -currentTimeInt;
  53.                 if(currentTimePassed >beginTime)
  54.                 {
  55.                     //底部左边界和右边界
  56.                     float xBottomLeftBound;
  57.                     float xBottomRightBound;

  58.                     //此点边界
  59.                     float xPointLeftBound;
  60.                     float xPointRightBound;
  61.                    
  62.                     float x0 currentTimePassed-beginTime;
  63.                     x0 /= loopTime;
  64.            
  65.                     //设置右边界
  66.                     xBottomRightBound x0;
  67.                    
  68.                     //设置左边界
  69.                     xBottomLeftBound x0 xLength;
  70.                    
  71.                     //投影至x的长度 ytan(angle)
  72.                     float xProjL;
  73.                     xProjL(uv.y)/tan(angleInRad);

  74.                     //此点的左边界 底部左边界 投影至x的长度
  75.                     xPointLeftBound xBottomLeftBound xProjL;
  76.                     //此点的右边界 底部右边界 投影至x的长度
  77.                     xPointRightBound xBottomRightBound xProjL;
  78.                    
  79.                     //边界加上一个偏移
  80.                     xPointLeftBound += offX;
  81.                     xPointRightBound += offX;
  82.                    
  83.                     //如果该点在区域内
  84.                     if(uv.x xPointLeftBound && uv.x xPointRightBound)
  85.                     {
  86.                         //得到发光区域的中心点
  87.                         float midness (xPointLeftBound xPointRightBound)/2;
  88.                        
  89.                         //趋近中心点的程度,0表示位于边缘,1表示位于中心点
  90.                         float rate(xLength -2*abs(uv.x midness))/ (xLength);
  91.                         brightness rate;
  92.                     }
  93.                 }
  94.                 brightnessmax(brightness,0);
  95.                
  96.                 //返回颜色 纯白色 亮度
  97.                 float4 col float4(1,1,1,1*brightness;
  98.                 return brightness;
  99.             }
  100.            
  101.             float4 frag (v2f iCOLOR
  102.             {
  103.                  float4 outp;
  104.                 
  105.                  //根据uv取得纹理颜色,和常规一样
  106.                 float4 texCol tex2D(_MainTex,i.uv);
  107.        
  108.                 //传进i.uv等参数,得到亮度值
  109.                 float tmpBrightness;
  110.                 tmpBrightness =inFlash(75,i.uv,0.25f,5f,2f,0.15,0.7f);
  111.            
  112.                 //图像区域,判定设置为 颜色的A 0.5,输出为材质颜色+光亮值
  113.                 if(texCol.w >0.5)
  114.                         outp  =texCol+float4(1,1,1,1)*tmpBrightness;
  115.                 //空白区域,判定设置为 颜色的A <=0.5,输出空白
  116.                 else
  117.                     outp =float4(0,0,0,0);

  118.                 return outp;
  119.             }
  120.             ENDCG
  121.         }
  122.     }

  123. }
原创粉丝点击