1. 程式人生 > >Shader實現LOGO的閃光效果

Shader實現LOGO的閃光效果

Shader "Unlit/logo"
{
Properties{
_MainTex("Base (RGB)", 2D) = "white" {} //在屬性面板上顯示貼圖
_FlashColor("Flash Color", Color) = (1,1,1,1)//閃光的顏色
_Angle("Flash Angle", Range(0, 180)) = 45//閃光的角度
_Width("Flash Width", Range(0, 1)) = 0.2//閃光的寬度
_LoopTime("Loop Time", Float) = 1//持續的時間
_Interval("Time Interval", Float) = 3//每一次的間隔
       _BeginTime ("Begin Time", Float) = 2//第一次開始的時間
}
SubShader
{
Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }//"Queue"表示渲染的順序,"RenderType"表示渲染的型別


CGPROGRAM
#pragma surface surf Lambert alpha exclude_path:prepass noforwardadd//surface表示採用哪種渲染型別,surf是定義的方法,與下文相對應
//alpha是混合,至於後面那個加與不加效果一樣,反正我是沒弄明白
#pragma target 3.0


//在shader中用面板的屬性要從新進行一次定義,名字必須一致
sampler2D _MainTex;
float4 _FlashColor;
float _Angle;
float _Width;
float _LoopTime;
float _Interval;
        float _BeginTime;
//作為surf的輸入
struct Input
{
half2 uv_MainTex;
};


float inFlash(half2 uv)
{
float brightness = 0;//亮度值


float angleInRad = 0.0174444 * _Angle;//傾斜角
float tanInverseInRad = 1.0 / tan(angleInRad);//_Angle在0~45°,tanInverseInRad>1;_Angle在45~90°,0<tanInverseInRad<1;
//_Angle在90~135°,-1<tanInverseInRad<0;_Angle在135~180°,-625<tanInverseInRad<-1,以後的又從頭開始迴圈


            float currentTime = _Time.y - _BeginTime;//第一次開始的時間
float totalTime = _Interval + _LoopTime;//從第一次播放到第二次開始播放的間隔
float currentTurnStartTime = (int)((currentTime / totalTime)) * totalTime;
float currentTurnTimePassed = currentTime - currentTurnStartTime - _Interval;


bool onLeft = (tanInverseInRad > 0);
float xBottomFarLeft = onLeft ? 0.0 : tanInverseInRad;
float xBottomFarRight = onLeft ? (1.0 + tanInverseInRad) : 1.0;


float percent = currentTurnTimePassed / _LoopTime;
float xBottomRightBound = xBottomFarLeft + percent * (xBottomFarRight - xBottomFarLeft);
float xBottomLeftBound = xBottomRightBound - _Width;


float xProj = uv.x + uv.y * tanInverseInRad;


if (xProj > xBottomLeftBound && xProj < xBottomRightBound)
{
brightness = 1.0 - abs(2.0 * xProj - (xBottomLeftBound + xBottomRightBound)) / _Width;
}


return brightness;
}


void surf(Input IN, inout SurfaceOutput o)
{
half4 texCol = tex2D(_MainTex, IN.uv_MainTex);//根據uv取得每個畫素的顏色
float brightness = inFlash(IN.uv_MainTex);//傳進i.uv等引數,得到亮度值


o.Albedo = texCol.rgb + _FlashColor.rgb * brightness;//根據每個RGB進行疊加
o.Alpha = texCol.a;
}


ENDCG
}


FallBack "Unlit/Transparent"
}