參照【風(fēng)宇沖】Unity3D教程寶典之Shader篇:第四講制作一個(gè)美麗的地球和Unity3D教程寶典之Shader篇:第五講LOGO閃光效果
這兩篇文章都是引入時(shí)間這一因素,進(jìn)行shader的移動(dòng)達(dá)到的效果
一、【風(fēng)宇沖】Unity3D教程寶典之Shader篇:第四講制作一個(gè)美麗的地球
Shader "Custom/earth" {
Properties {
_MainTex ("Texture", 2D) = "white" { }
_Cloud ("_Cloud", 2D) = "white" { }
}
SubShader {
Tags{"Queue" = "Transparent" "RenderType"="Transparent"}
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _Color;
sampler2D _MainTex;
sampler2D _Cloud;
struct v2f {
float4? pos : SV_POSITION;
float2? uv : TEXCOORD0;
} ;
float4 _MainTex_ST;
v2f vert (appdata_base v)
{
//和之前一樣
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
return o;
}
half4 frag (v2f i) : COLOR
{
//地球的貼圖uv, x即橫向在動(dòng)
float u_x = i.uv.x + -0.1*_Time;
float2 uv_earth=float2( u_x , i.uv.y);
half4 texcolor_earth = tex2D (_MainTex, uv_earth);
//云層的貼圖uv的x也在動(dòng)阿逃,但是動(dòng)的更快一些
float2 uv_cloud;
u_x = i.uv.x + -0.2*_Time;
uv_cloud=float2( u_x , i.uv.y);
half4 tex_cloudDepth = tex2D (_Cloud, uv_cloud);
//純白 x 深度值= 該點(diǎn)的云顏色
half4 texcolor_cloud = float4(1,1,1,0) * (tex_cloudDepth.x);
//地球云彩顏色混合
return lerp(texcolor_earth,texcolor_cloud,0.5f);
}
ENDCG}}}
二薪捍、Unity3D教程寶典之Shader篇:第五講LOGO閃光效果
Shader "Custom/logo" {
Properties {
//_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
//_Glossiness ("Smoothness", Range(0,1)) = 0.5
//_Metallic ("Metallic", Range(0,1)) = 0.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;
struct v2f {
float4? pos : SV_POSITION;
float2? uv : TEXCOORD0;
};
//頂點(diǎn)函數(shù)沒什么特別的,和常規(guī)一樣
v2f vert (appdata_base v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
o.uv =? ? TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
//必須放在使用其的 frag函數(shù)之前演熟,否則無法識(shí)別。
//核心:計(jì)算函數(shù),角度,uv,光帶的x長(zhǎng)度脐恩,間隔镐侯,開始時(shí)間,偏移驶冒,單次循環(huán)時(shí)間
float inFlash(float angle,float2 uv,float xLength,int interval,int beginTime, float offX, float loopTime )
{
//亮度值
float brightness =0;
//傾斜角
float angleInRad = 0.0174444 * angle;
//當(dāng)前時(shí)間
float currentTime = _Time.y;
//獲取本次光照的起始時(shí)間
int currentTimeInt = _Time.y/interval;
currentTimeInt *=interval;
//獲取本次光照的流逝時(shí)間 = 當(dāng)前時(shí)間 - 起始時(shí)間
float currentTimePassed = currentTime -currentTimeInt;
if(currentTimePassed >beginTime)
{
//底部左邊界和右邊界
float xBottomLeftBound;
float xBottomRightBound;
//此點(diǎn)邊界
float xPointLeftBound;
float xPointRightBound;
float x0 = currentTimePassed-beginTime;
x0 /= loopTime;
//設(shè)置右邊界
xBottomRightBound = x0;
//設(shè)置左邊界
xBottomLeftBound = x0 - xLength;
//投影至x的長(zhǎng)度 = y/ tan(angle)
float xProjL;
xProjL= (uv.y)/tan(angleInRad);
//此點(diǎn)的左邊界 = 底部左邊界 - 投影至x的長(zhǎng)度
xPointLeftBound = xBottomLeftBound - xProjL;
//此點(diǎn)的右邊界 = 底部右邊界 - 投影至x的長(zhǎng)度
xPointRightBound = xBottomRightBound - xProjL;
//邊界加上一個(gè)偏移
xPointLeftBound += offX;
xPointRightBound += offX;
//如果該點(diǎn)在區(qū)域內(nèi)
if(uv.x > xPointLeftBound && uv.x < xPointRightBound)
{
//得到發(fā)光區(qū)域的中心點(diǎn)
float midness = (xPointLeftBound + xPointRightBound)/2;
//趨近中心點(diǎn)的程度苟翻,0表示位于邊緣,1表示位于中心點(diǎn)
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 (v2f i) : COLOR
{
float4 outp;
//根據(jù)uv取得紋理顏色骗污,和常規(guī)一樣
float4 texCol = tex2D(_MainTex,i.uv);
//傳進(jìn)i.uv等參數(shù)崇猫,得到亮度值
float tmpBrightness;
tmpBrightness =inFlash(75,i.uv,0.25,5,2,0.15,0.7);
//圖像區(qū)域,判定設(shè)置為 顏色的A > 0.5,輸出為材質(zhì)顏色+光亮值
if(texCol.w >0.5)
outp? =texCol+float4(1,1,1,1)*tmpBrightness;
//空白區(qū)域需忿,判定設(shè)置為 顏色的A <=0.5,輸出空白
else
outp =float4(0,0,0,0);
return outp;
}
ENDCG
}
}
}