Unity3D Shader實現(xiàn)流光效果
更新時間:2020年04月29日 09:24:42 作者:skask007
這篇文章主要為大家詳細介紹了Unity3D Shader實現(xiàn)流光效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Unity3D Shader實現(xiàn)流光效果的具體代碼,供大家參考,具體內容如下
流光效果圖:

演示工程:下載地址
//功能需求:模擬數(shù)據(jù)傳送效果,高亮色塊從模型上方移動到下方
//功能分析:這里采用UV動畫的方式來實現(xiàn),利用Alpha貼圖控制流動的形狀
// 利用Alpha遮罩貼圖,控制模型中哪些地方需要進行流動
Shader "Custom/DataFlowEffect"
{
Properties
{
_MainColor("Main Color",Color) = (1,1,1,1)
_MainTex("Main Texture",2D) = "white"{}
_Specular("Specular",Color) = (1,1,1,1)
_Gloss("Gloss",Range(0,255)) = 20.0
_FlowTex("Flow Tex (A)",2D) = "black"{}
_FlowColor("Flow Color (RGBA)",Color)=(1,1,1,1)
_FlowIdleTime("FlowInternal",Range(0,10))=1.0
_FlowDuring("FlowDuring",Range(0,10))=1.0
_FlowMaskTex("FlowMasking (A)",2D)="white"{}
_FlowDirection("FlowDirection",Int)= 0
_FlowBeginTime("Flow Begin Time",Float)=0
}
SubShader
{
Tags{"RenderType" = "Opaque" "Queue"="Geometry"}
Pass
{
Tags{"LightMode"="ForwardBase"}
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
sampler2D _MainTex; //顏色貼圖
half4 _MainTex_ST; //顏色UV 縮放和偏移
fixed3 _MainColor; //漫反射顏色
fixed3 _Specular; //高光顏色
fixed _Gloss; //高光度
sampler2D _FlowTex; //數(shù)據(jù)流圖片
fixed4 _FlowColor; //數(shù)據(jù)流顏色疊加
half4 _FlowTex_ST; //數(shù)據(jù)流貼圖UV的縮放和偏移
fixed _FlowIdleTime; //流動動畫間歇時間
fixed _FlowDuring; //流動動畫播放時間
sampler2D _FlowMaskTex; //流動遮罩
fixed _FlowDirection; //流動方向
float _FlowBeginTime; //流動效果開始的時間
struct a2v
{
half4 pos: POSITION;
half3 normal :NORMAL;
half4 texcoord : TEXCOORD0;
};
struct v2f
{
half4 position : SV_POSITION;
half2 uv : TEXCOORD0;
half3 worldNormal : TEXCOORD1;
half3 worldPos : TEXCOORD2;
half2 flowUV : TEXCOORD3;
};
v2f vert(a2v i)
{
v2f v;
v.position = UnityObjectToClipPos(i.pos);
v.uv = i.texcoord * _MainTex_ST.xy + _MainTex_ST.zw;
v.worldNormal = mul(unity_ObjectToWorld,i.normal);
v.worldPos = mul(unity_ObjectToWorld,i.pos);
v.flowUV = i.texcoord * _FlowTex_ST.xy + _FlowTex_ST.zw;
return v;
}
//uv - vert的uv坐標
//scale - 貼圖縮放
//idleTime - 每次循環(huán)開始后多長時間,開始流動
//loopTime - 單次流動時間
fixed4 getFlowColor(half2 uv,int scale,fixed idleTime,fixed loopTime)
{
//當前運行時間
half flowTime_ = _Time.y - _FlowBeginTime;
//上一次循環(huán)開始,到本次循環(huán)開始的時間間隔
half internal = idleTime + loopTime;
//當前循環(huán)執(zhí)行時間
half curLoopTime = fmod(flowTime_,internal);
//每次開始流動之前,有個停止間隔,檢測是否可以流動了
if(curLoopTime > idleTime)
{
//已經(jīng)流動時間
half actionTime = curLoopTime - idleTime;
//流動進度百分比
half actionPercentage = actionTime / loopTime;
half length = 1.0 / scale;
//從下往上流動
//計算方式:設:y = ax + b,其中y為下邊界值,x為流動進度
//根據(jù)我們要求可以,x=0時y=-length;x=1時y=1;帶入解方程
half bottomBorder = actionPercentage * (1+length) - length;
half topBorder = bottomBorder + length;
//從上往下流動
//求解方法與上面類似
if(_FlowDirection < 0)
{
topBorder = (-1-length) * actionPercentage + 1 + length;
bottomBorder = topBorder - length;
}
if(uv.y < topBorder && uv.y > bottomBorder)
{
half y = (uv.y - bottomBorder) / length;
return tex2D(_FlowTex,fixed2(uv.x,y));
}
}
return fixed4(1,1,1,0);
}
fixed4 frag(v2f v):SV_Target
{
//計算漫反射系數(shù)
fixed3 albedo = tex2D(_MainTex,v.uv) * _MainColor;
//計算環(huán)境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 worldNormal = normalize(v.worldNormal); //世界坐標的法線方向
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(v.worldPos)); //世界坐標的光照方向
fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(v.worldPos)); //世界坐標的視角方向
//計算漫反射顏色,采用Half-Lambert模型
fixed3 lightColor = _LightColor0.rgb;
fixed3 diffuse = lightColor * albedo * max(0,0.5*dot(worldNormal,worldLightDir)+0.5);
//計算高光,采用Blinn-Phone高光模型
fixed3 halfDir = normalize(worldViewDir + worldLightDir);
fixed3 specColor = _Specular * lightColor * pow(max(0,dot(worldNormal,halfDir)),_Gloss);
//疊加流動貼圖
fixed4 flowColor = getFlowColor(v.uv,_FlowTex_ST.y,_FlowIdleTime,_FlowDuring);
fixed4 flowMaskColor = tex2D(_FlowMaskTex,v.uv);
//與遮罩貼圖進行混合,只顯示遮罩貼圖不透明的部分
flowColor.a = flowMaskColor.a * flowColor.a * _FlowColor.a;
fixed3 finalDiffuse = lerp(diffuse,_FlowColor,flowColor.a);
return fixed4(ambient + finalDiffuse+specColor,1);
}
ENDCG
}
}
Fallback "Diffuse"
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
C#利用PrintDocument定制打印單據(jù)的小例子
這篇文章主要給大家介紹了關于C#利用PrintDocument定制打印單據(jù)的小例子,文中通過示例代碼介紹的非常詳細,對大家學習或者使用C#具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-05-05
C#操作SQLite實現(xiàn)數(shù)據(jù)的增刪改查
SQLite是一個輕量級、跨平臺的關系型數(shù)據(jù)庫,在小型項目中,方便,易用,同時支持多種開發(fā)語言。本文將用C#語言對SQLite 的一個封裝,實現(xiàn)數(shù)據(jù)的增刪改查。需要的可以參考一下2022-01-01

