您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了Unity3D Shader如何實(shí)現(xiàn)流光效果,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lái)看看吧。
流光效果圖:
演示工程:下載地址
//功能需求:模擬數(shù)據(jù)傳送效果,高亮色塊從模型上方移動(dòng)到下方 //功能分析:這里采用UV動(dòng)畫(huà)的方式來(lái)實(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)畫(huà)間歇時(shí)間 fixed _FlowDuring; //流動(dòng)動(dòng)畫(huà)播放時(shí)間 sampler2D _FlowMaskTex; //流動(dòng)遮罩 fixed _FlowDirection; //流動(dòng)方向 float _FlowBeginTime; //流動(dòng)效果開(kāi)始的時(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)開(kāi)始后多長(zhǎng)時(shí)間,開(kāi)始流動(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)開(kāi)始,到本次循環(huán)開(kāi)始的時(shí)間間隔 half internal = idleTime + loopTime; //當(dāng)前循環(huán)執(zhí)行時(shí)間 half curLoopTime = fmod(flowTime_,internal); //每次開(kāi)始流動(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) //求解方法與上面類(lèi)似 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" }
以上就是關(guān)于Unity3D Shader如何實(shí)現(xiàn)流光效果的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。