Unity 3D柱状图效果
1.单个柱状效果展示
2.从上到下渐变透明材质Shader
Shader "Unlit/NewUnlitShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color("Color",Color) = (1,1,1,1)
_Alpha("Alpha",Range(0,1) ) = 0.2
_Alpha2("Alpha2",Range(0,1) ) = 0.2
_Power("Power",range(0,10)) = 1
}
SubShader
{
Pass
{
Tags { "RenderType"="Transparent" }
Cull Front
Blend SrcAlpha One
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;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
float _Alpha2;
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;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
//fixed4 col = tex2D(_MainTex, i.uv);
float v = i.uv.y;
//float v = 1 - pow((1 - i.uv.y), 4);
fixed4 c = fixed4(_Color.xyz ,v * _Alpha2);
return c;
}
ENDCG
}
Pass
{
Tags { "RenderType"="Transparent" }
Cull Back
Blend SrcAlpha One
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;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
float _Alpha;
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;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
//fixed4 col = tex2D(_MainTex, i.uv);
float y = i.uv.y;
float v = 1 - pow(1 - (1 - y ), 4);
fixed4 c = fixed4(_Color.xyz * _Alpha, (1 - v) *_Alpha);
return c;
}
ENDCG
}
Pass
{
Tags { "RenderType"="Transparent" }
Blend SrcAlpha One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float3 viewDir : TEXCOORD1;
float4 vertex : SV_POSITION;
float3 normal : normal;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
float _Alpha;
float _Power;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.viewDir = ObjSpaceViewDir(v.vertex);
o.normal = v.normal;
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
float y2 = i.uv.y;
float v = 1 - pow(1 - (1 - y2 ), 4);
//float y = i.uv.y + _Time.y * _Speed;
//float sy = (sin(_Time.y) + 1) / 2;
float fresnel = pow(1 - saturate(dot(normalize(i.viewDir), normalize(i.normal))), _Power);
for(int m = 0; m <= 10; m++)
{
if(i.uv.y <= (m + 1) / 10 && i.uv.y >= m / 10)
{
return float4(_Color.rgb *_Alpha * fresnel,_Alpha * ((m + 1) / 10)) ;
}
}
return _Color * fresnel;
// if(i.uv.y <= sy && i.uv.y >= sy - 0.1)
// {
// return float4(_Color.rgb,_Alpha * 2) ;
// }
// else
// {
// fixed4 c = fixed4(_Color.xyz * _Alpha, (1 - v) *_Alpha);
// return c;
// }
//fixed4 col = tex2D(_MainTex, uv * _Alpha);
}
ENDCG
}
}
}
3.效果用例
unity 新建柱状体,赋予上面材质,调整合适参数。