示例
Shader "Custom/Gradient"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)
_Diameter ("Diameter", Range(0.0, 10.0)) = 2.0
}
SubShader
{
Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
LOD 100
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform fixed4 _Color;
uniform float _Diameter;
uniform float _DistanceInMeters;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 uva : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
float scale = lerp(_Diameter, 0, v.vertex.y);
o.vertex = UnityObjectToClipPos(v.vertex);
fixed2 uv = TRANSFORM_TEX(v.uv, _MainTex);
o.uva = float4(uv.x, uv.y, 0, scale);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uva.xy);
col *= _Color;
col.a = i.uva.a;
return col;
}
ENDCG
}
}
}
效果(渐变的胶囊体)