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

演示工程:下載地址
//功能需求:模擬數(shù)據(jù)傳送效果,高亮色塊從模型上方移動(dòng)到下方
//功能分析:這里采用UV動(dòng)畫的方式來實(shí)現(xiàn),利用Alpha貼圖控制流動(dòng)的形狀
// 利用Alpha遮罩貼圖,控制模型中哪些地方需要進(jìn)行流動(dòng)
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; //流動(dòng)動(dòng)畫間歇時(shí)間
fixed _FlowDuring; //流動(dòng)動(dòng)畫播放時(shí)間
sampler2D _FlowMaskTex; //流動(dòng)遮罩
fixed _FlowDirection; //流動(dòng)方向
float _FlowBeginTime; //流動(dòng)效果開始的時(shí)間
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坐標(biāo)
//scale - 貼圖縮放
//idleTime - 每次循環(huán)開始后多長時(shí)間,開始流動(dòng)
//loopTime - 單次流動(dòng)時(shí)間
fixed4 getFlowColor(half2 uv,int scale,fixed idleTime,fixed loopTime)
{
//當(dāng)前運(yùn)行時(shí)間
half flowTime_ = _Time.y - _FlowBeginTime;
//上一次循環(huán)開始,到本次循環(huán)開始的時(shí)間間隔
half internal = idleTime + loopTime;
//當(dāng)前循環(huán)執(zhí)行時(shí)間
half curLoopTime = fmod(flowTime_,internal);
//每次開始流動(dòng)之前,有個(gè)停止間隔,檢測(cè)是否可以流動(dòng)了
if(curLoopTime > idleTime)
{
//已經(jīng)流動(dòng)時(shí)間
half actionTime = curLoopTime - idleTime;
//流動(dòng)進(jìn)度百分比
half actionPercentage = actionTime / loopTime;
half length = 1.0 / scale;
//從下往上流動(dòng)
//計(jì)算方式:設(shè):y = ax + b,其中y為下邊界值,x為流動(dòng)進(jìn)度
//根據(jù)我們要求可以,x=0時(shí)y=-length;x=1時(shí)y=1;帶入解方程
half bottomBorder = actionPercentage * (1+length) - length;
half topBorder = bottomBorder + length;
//從上往下流動(dòng)
//求解方法與上面類似
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
{
//計(jì)算漫反射系數(shù)
fixed3 albedo = tex2D(_MainTex,v.uv) * _MainColor;
//計(jì)算環(huán)境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 worldNormal = normalize(v.worldNormal); //世界坐標(biāo)的法線方向
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(v.worldPos)); //世界坐標(biāo)的光照方向
fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(v.worldPos)); //世界坐標(biāo)的視角方向
//計(jì)算漫反射顏色,采用Half-Lambert模型
fixed3 lightColor = _LightColor0.rgb;
fixed3 diffuse = lightColor * albedo * max(0,0.5*dot(worldNormal,worldLightDir)+0.5);
//計(jì)算高光,采用Blinn-Phone高光模型
fixed3 halfDir = normalize(worldViewDir + worldLightDir);
fixed3 specColor = _Specular * lightColor * pow(max(0,dot(worldNormal,halfDir)),_Gloss);
//疊加流動(dòng)貼圖
fixed4 flowColor = getFlowColor(v.uv,_FlowTex_ST.y,_FlowIdleTime,_FlowDuring);
fixed4 flowMaskColor = tex2D(_FlowMaskTex,v.uv);
//與遮罩貼圖進(jìn)行混合,只顯示遮罩貼圖不透明的部分
flowColor.a = flowMaskColor.a * flowColor.a * _FlowColor.a;
fixed3 finalDiffuse = lerp(diffuse,_FlowColor,flowColor.a);
return fixed4(ambient + finalDiffuse+specColor,1);
}
ENDCG
}
}
Fallback "Diffuse"
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
C#利用PrintDocument定制打印單據(jù)的小例子
這篇文章主要給大家介紹了關(guān)于C#利用PrintDocument定制打印單據(jù)的小例子,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
C# 并行和多線程編程——Task進(jìn)階知識(shí)
這篇文章主要介紹了C# 并行和多線程編程——Task進(jìn)階知識(shí)的的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02
C#多線程處理多個(gè)隊(duì)列數(shù)據(jù)的方法
本文將結(jié)合實(shí)例代碼,介紹C#多線程處理多個(gè)隊(duì)列數(shù)據(jù)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
C#實(shí)現(xiàn)在應(yīng)用程序間發(fā)送消息的方法示例
這篇文章主要介紹了C#實(shí)現(xiàn)在應(yīng)用程序間發(fā)送消息的方法,結(jié)合具體實(shí)例形式分析了C#實(shí)現(xiàn)項(xiàng)目之間信息發(fā)送、接收等交互操作相關(guān)技巧,需要的朋友可以參考下2017-06-06
C#操作SQLite實(shí)現(xiàn)數(shù)據(jù)的增刪改查
SQLite是一個(gè)輕量級(jí)、跨平臺(tái)的關(guān)系型數(shù)據(jù)庫,在小型項(xiàng)目中,方便,易用,同時(shí)支持多種開發(fā)語言。本文將用C#語言對(duì)SQLite 的一個(gè)封裝,實(shí)現(xiàn)數(shù)據(jù)的增刪改查。需要的可以參考一下2022-01-01

