unity 隨時間變化的shader(20170301)

參照【風宇沖】Unity3D教程寶典之Shader篇:第四講制作一個美麗的地球Unity3D教程寶典之Shader篇:第五講LOGO閃光效果

這兩篇文章都是引入時間這一因素,進行shader的移動達到的效果

一、【風宇沖】Unity3D教程寶典之Shader篇:第四講制作一個美麗的地球

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即橫向在動

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也在動,但是動的更快一些

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 深度值= 該點的云顏色

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;

};

//頂點函數沒什么特別的,和常規一樣

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函數之前,否則無法識別。

//核心:計算函數,角度,uv,光帶的x長度,間隔,開始時間,偏移,單次循環時間

float inFlash(float angle,float2 uv,float xLength,int interval,int beginTime, float offX, float loopTime )

{

//亮度值

float brightness =0;

//傾斜角

float angleInRad = 0.0174444 * angle;

//當前時間

float currentTime = _Time.y;

//獲取本次光照的起始時間

int currentTimeInt = _Time.y/interval;

currentTimeInt *=interval;

//獲取本次光照的流逝時間 = 當前時間 - 起始時間

float currentTimePassed = currentTime -currentTimeInt;

if(currentTimePassed >beginTime)

{

//底部左邊界和右邊界

float xBottomLeftBound;

float xBottomRightBound;

//此點邊界

float xPointLeftBound;

float xPointRightBound;

float x0 = currentTimePassed-beginTime;

x0 /= loopTime;

//設置右邊界

xBottomRightBound = x0;

//設置左邊界

xBottomLeftBound = x0 - xLength;

//投影至x的長度 = y/ tan(angle)

float xProjL;

xProjL= (uv.y)/tan(angleInRad);

//此點的左邊界 = 底部左邊界 - 投影至x的長度

xPointLeftBound = xBottomLeftBound - xProjL;

//此點的右邊界 = 底部右邊界 - 投影至x的長度

xPointRightBound = xBottomRightBound - xProjL;

//邊界加上一個偏移

xPointLeftBound += offX;

xPointRightBound += offX;

//如果該點在區域內

if(uv.x > xPointLeftBound && uv.x < xPointRightBound)

{

//得到發光區域的中心點

float midness = (xPointLeftBound + xPointRightBound)/2;

//趨近中心點的程度,0表示位于邊緣,1表示位于中心點

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;

//根據uv取得紋理顏色,和常規一樣

float4 texCol = tex2D(_MainTex,i.uv);

//傳進i.uv等參數,得到亮度值

float tmpBrightness;

tmpBrightness =inFlash(75,i.uv,0.25,5,2,0.15,0.7);

//圖像區域,判定設置為 顏色的A > 0.5,輸出為材質顏色+光亮值

if(texCol.w >0.5)

outp? =texCol+float4(1,1,1,1)*tmpBrightness;

//空白區域,判定設置為 顏色的A <=0.5,輸出空白

else

outp =float4(0,0,0,0);

return outp;

}

ENDCG

}

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容