CangJie/Assets/Shader/转场/LoadScreen.shader

95 lines
2.5 KiB
Plaintext
Raw Normal View History

Shader "Unlit/LoadScreen"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_BlurRadius ("BlurRadius", Range(0, 50)) = 0 //模糊半径
_TextureSize ("TextureSize", Float) = 640
_SecTex("SecTex",2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _SecTex;
float4 _MainTex_ST;
int _BlurRadius;
float _TextureSize;
float GetGaussWeight(float x, float y, float sigma)
{
float sigma2 = pow(sigma, 2.0f);
float left = 1 / (2 * sigma2 * 3.1415926f);
float right = exp(-(x*x+y*y)/(2*sigma2)); //e的指数幂
return left * right;
}
float4 GaussBlur(float2 uv) //高斯公式
{
float sigma = (float)_BlurRadius / 3.0f;//权重
float4 col = float4(0, 0, 0, 0);
for (int x = - _BlurRadius; x <= _BlurRadius; ++x)
{
for (int y = - _BlurRadius; y <= _BlurRadius; ++y)
{
//获取周围像素的颜色
//转为uv上的坐标值
float4 color = tex2D(_SecTex, uv + float2(x / _TextureSize, y / _TextureSize));
//获取此像素的权重
float weight = GetGaussWeight(x, y, sigma);
//计算此点的最终颜色
col += color * weight; //颜色乘以权重
}
}
return col;
}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
float4 frag (v2f i) : SV_Target
{
float4 black = float4(0,0,0,0);
float4 col = GaussBlur(i.uv);
col*=1+_BlurRadius;
col=smoothstep(0.1,0.9,col);
col=step(col,0.5);
col*=tex2D(_MainTex,i.uv);
col = lerp(col,black,_BlurRadius*(1.0/50));
return col;
}
ENDCG
}
}
}