流光效果

Shader "Custom/Shader0" {
    Properties
    {
        _MainTex("Texture",2D) = "white"{}
        _Angle ("Angle", Range (0,360)) = 0
    }
    SubShader 
    {
        Tags{"Queue"="Transparent""IgnoreProjector"="True""RenderType"="Transparent"}
        Blend SrcAlpha OneMinusSrcAlpha
        AlphaTest Greater 0.1
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Angle;

            struct vertexOutput
            {
                float4 pos:POSITION;
                float2 uv:TEXCOORD0;
            };
            
            vertexOutput vert(appdata_base v)
            {
                vertexOutput  output;
                output.pos = mul(UNITY_MATRIX_MVP,v.vertex);
                output.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
                return output;
            }
            

            float inFlash(float angle,float2 uv,float xLength,int interval,int beginTime,float offx, float loopTime)
            {
                //亮度值
                float brightness = 0;
                //倾斜角
                float angleInRad = 0.0174444*angle;
                //当前时间
                float currentTime = _Time.y;
                //获取本次光照的起始时间
                int currentTimeInt = _Time.y/interval;
                currentTimeInt *=interval;

                //获取本次光照的流逝时间 = 当前时间-起始时间
                float currentTimePassed = currentTime - currentTimeInt;
                if(currentTimePassed > beginTime)
                {
                    //底部左边界和右边界
                    float xBottomLeftBound;
                    float xBottomRightBound;

                    //起点边界
                    float xPointLeftBound;
                    float xPointRightBound;

                    float xO = currentTimePassed - beginTime;
                    xO /= loopTime;

                    //设置右边界
                    xBottomRightBound = xO;

                    //设置左边界
                    xBottomLeftBound = xO - xLength;

                    //投影至X的长度 = y/tan(angle)
                    float xProjL;
                    xProjL = (uv.y)/tan(angleInRad);

                    //起点的左边界 = 底部左边界 - 投影至X 的长度
                    xPointLeftBound =xBottomLeftBound - xProjL;
                    //起点的右边界 = 底部右边界 - 投影至X的长度
                    xPointRightBound = xBottomRightBound - xProjL;

                    //边界加上一个偏移
                    xPointLeftBound  += offx;
                    xPointRightBound += offx;

                    //如果该点在区域内
                    if(uv.x>xPointLeftBound && uv.x<xPointRightBound)
                    {
                        //得到发光区域的中心点
                        float midness = (xPointLeftBound  + xPointRightBound)/2;

                         //趋近中心点的程度,0表示位于边缘,1表示位于中心点
                        float rate= (xLength -2*abs(uv.x - midness))/ (xLength);
                        brightness = rate;
                    }

                }
                brightness = max(brightness,0);
                //返回颜色  = 纯白色 * 亮度
                float4 col =  float4(1,1,1,1)*brightness;
                return brightness;
            }
            float4 frag(vertexOutput input):COLOR
            {
                float4 outp;
                float4 tex = tex2D(_MainTex,input.uv);
                //float brightness;
                float brightness = inFlash(_Angle,input.uv,0.25,5,2,0.15,0.7);

                 //图像区域,判定设置为 颜色的A > 0.5,输出为材质颜色+光亮值
                if(tex.w >0.5)
                        outp  =tex+float4(1,1,1,1)*brightness;
                //空白区域,判定设置为 颜色的A <=0.5,输出空白
                else
                    outp =float4(0,0,0,0);

                return outp;
            }
            ENDCG
        }
        
    } 
    FallBack "Diffuse"
}

 

posted @ 2014-11-27 19:31  kadajEvo  阅读(109)  评论(0)    收藏  举报