自学shader路上的副产品,记录下,可以扩展成容器内的液体,进度条等。
Shader "Custom/CSGProgress"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Progress("Progress",float) = 0.5
_Speed("Speed",float) = 0.1
_Color1("Color1",Color)=(1,1,1,1)
_Color2("Color2",Color)=(1,1,1,1)
_Color3("Color3",Color)=(1,1,1,1)
_Color4("Color4",Color) = (1,1,1,1)
_FishTex("fish",2D) = "white" {}
_FishTex2("fish2",2D) = "white" {}
_BoatTex("boat",2D) = "white" {}
_Dis("_Dis",float) = 0.3
_Dim("_Dim",Range(-0.1,0.1)) = 0.03
_ChangeRange("ChangRange", int) = 50
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#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;
float2 uv2 : TEXCOORD1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Progress;
float _Speed;
float4 _Color1;
float4 _Color2;
float4 _Color3;
float4 _Color4;
sampler2D _FishTex;
float4 _FishTex_ST;
sampler2D _FishTex2;
sampler2D _BoatTex;
float _Dis;
float _Dim;
int _ChangeRange;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.uv2 = TRANSFORM_TEX(v.uv, _FishTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
float3 Circle(inout float3 color,float3 showColor,float2 uv,float2 center,float radius,float edge)
{
float d = length(uv-center);
float c = smoothstep(radius,radius-edge,d);
color += showColor * c;
return color;
}
fixed4 frag(v2f i) : SV_Target
{
float time = _Time.y;
float3 color=lerp(_Color1, _Color2, i.uv.y);
float rate = _ScreenParams.x / _ScreenParams.y;
i.uv.x *= rate;
float3 tempC = Circle(color, _Color3, i.uv, float2(0.5* rate, 0.5), 0.2, 0.01);
float tempValue = (cos(time * 4 + (i.uv.x * (20 * frac(sin(_Time.x * 0.1) * 10)))) * 0.02);
if (i.uv.y < (_Progress + tempValue))
{
tempC = Circle(color, _Color4, i.uv, float2(0.5 * rate, 0.5), 0.2, 0.06);
}
Circle(color, _Color4, i.uv, float2(0.5 * rate, _Speed *0.8), 0.04, 0.01);
Circle(color, _Color4, i.uv, float2(0.45 * rate, _Speed), 0.04, 0.01);
Circle(color, _Color4, i.uv, float2(0.48 * rate, _Speed *1.2), 0.04, 0.01);
Circle(color, _Color4, i.uv, float2(0.53 * rate, _Speed *1.1), 0.04, 0.01);
Circle(color, _Color4, i.uv, float2(0.56 * rate, _Speed *0.9), 0.04, 0.01);
fixed4 cFish1 = tex2D(_FishTex, (i.uv - float2(0.5*rate, 0.34+ tempValue*0.3)) * 8);
fixed4 cFish2 = tex2D(_FishTex2, (i.uv - float2(0.4 * rate, 0.34 + tempValue * 0.26)) * 6);
fixed4 boat = tex2D(_BoatTex, (i.uv - float2(0.5 * rate, 0.5 + tempValue)) * 8);
if (cFish1.a>0)
{
color *= cFish1;
}
if (cFish2.a>0)
{
color *= cFish2;
}
if (boat.a>0)
{
color = boat;
}
fixed4 col = fixed4(color.rgb,1);
return col;
}
ENDCG
}
}
}
|