代码如下:
Shader "Hidden/Loaing_03"
{
Properties
{
_MainColor("MainColor", Color) = (1, 1, 1, 1)
_RectWidth("RectWidth", Range(0, 1)) = 0.1
_RectHeight("RectHeight", Range(0, 1)) = 0.1
_Spacing("Spacing", Range(0, 1)) = 0.1
_Wait("Wait", Range(0, 1)) = 0.1
_Scale("Scale", Range(0, 1)) = 0.1
_Breath("Breath", float) = 1
_Num("Num", int) = 1
_Dir("Dir", int) = 1
}
SubShader
{
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 _MainColor;
fixed _RectWidth;
fixed _RectHeight;
fixed _Spacing;
fixed _Breath;
fixed _Scale;
fixed _Wait;
int _Num;
int _Dir;
fixed drawRect(fixed2 uv, fixed2 center, fixed2 size)
{
fixed2 newUv = uv - center;
return step(-0.5 * size.x, newUv.x) * step(newUv.x, 0.5 * size.x) * step(-0.5 * size.y, newUv.y) * step(newUv.y, 0.5 * size.y);
}
fixed4 frag (v2f i) : SV_Target
{
fixed2 newUv = i.uv - fixed2(0.5, 0.5);
// 显示区域长度
fixed displayAreaWidth = (_RectWidth + _Spacing) * _Num - _Spacing;
fixed x = newUv.x + displayAreaWidth * 0.5;
int displayArea = step(0, x) * step(x, displayAreaWidth);
fixed itemWidth = _RectWidth + _Spacing * (_Num - 1) / _Num;
int index = x / itemWidth;
x = index * _RectWidth + _RectWidth * 0.5 + index * _Spacing - displayAreaWidth * 0.5;
fixed h = _RectHeight + _Dir * _Scale * sin(_Time.y * _Breath + radians(index * _Wait * 360));
fixed rect = drawRect(newUv, fixed2(x, 0),fixed2(_RectWidth, h));
_MainColor.a = rect * displayArea;
return _MainColor;
}
ENDCG
}
}
}