半透shader什么时候用到呢
1.如果主角被东西挡住,摄像机要不拉近要不把挡住的物体半透
2.很多游戏用到残影,一般残影都是半透的,然后慢慢透明
从左到右
1.使用标准材质球
2.使用半透材质球
3.使用外发光的半透材质球
4.使用内发光和外发光的材质球
shader比较简单直接上代码
这些都是surface shader
1.半透shader
Shader "Snoopy/AlphaBase"
{
Properties
{
_MainTex("Particle Texture", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
//这里加一个pass为了不会穿透叠自己的颜色,可以取消这个pass看看效果
Pass
{
ZWrite On
ColorMask 0
}
CGPROGRAM
//使用alpha通道
#pragma surface surf Lambert alpha
struct Input
{
float2 uv_MainTex;
};
sampler2D _MainTex;
fixed4 _Color;
void surf(Input IN, inout SurfaceOutput o)
{
float4 col = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = col.rgb * _Color.rgb;
o.Alpha = _Color.a;
}
ENDCG
}
Fallback "VertexLit"
}
2.外发光的半透shader
Shader "Snoopy/AlphaRimBase"
{
Properties
{
_MainTex("Particle Texture", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)
_RimColor("Rim Color", Color) = (0.5,0.5,0.5,0.5)
_RimPower("Rim Power", Range(0.0,5.0)) = 2.5
}
SubShader
{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Pass
{
ZWrite On
ColorMask 0
}
CGPROGRAM
#pragma surface surf Lambert alpha
struct Input
{
float3 viewDir;
float2 uv_MainTex;
};
sampler2D _MainTex;
float4 _Color;
float4 _RimColor;
float _RimPower;
void surf(Input IN, inout SurfaceOutput o)
{
float4 col = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = col.rgb * _Color.rgb;
//边缘高光
half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow(rim, 6 - _RimPower);
o.Alpha = _Color.a;
}
ENDCG
}
Fallback "VertexLit"
}
3.外发光和内发光的半透shader
Shader "Snoopy/AlphaRimPro"
{
Properties
{
_MainTex ("Particle Texture", 2D) = "white" {}
_RimColor ("Rim Color", Color) = (0.5,0.5,0.5,0.5)
_InnerColor ("Inner Color", Color) = (0.5,0.5,0.5,0.5)
_InnerColorPower ("Inner Color Power", Range(0.0,1.0)) = 0.5
_RimPower ("Rim Power", Range(0.0,5.0)) = 2.5
_AlphaPower ("Alpha Rim Power", Range(0.0,8.0)) = 4.0
_AllPower ("All Power", Range(0.0, 10.0)) = 1.0
_InnerAlphaBase ("Inner Alpha Base", Range(0.0, 1.0)) = 1.0
}
SubShader
{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Pass
{
// 开启深度写入
ZWrite On
// 设置颜色通道的写掩码,0为不写入任何颜色
ColorMask 0
}
CGPROGRAM
#pragma surface surf Lambert alpha
struct Input
{
float3 viewDir;
float2 uv_MainTex;
INTERNAL_DATA
};
sampler2D _MainTex;
float4 _RimColor;
float _RimPower;
float _AlphaPower;
float _AlphaMin;
float _InnerColorPower;
float _AllPower;
float4 _InnerColor;
float _InnerAlphaBase;
void surf (Input IN, inout SurfaceOutput o)
{
float4 col = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = col.rgb;
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow (rim, _RimPower)*_AllPower+(_InnerColor.rgb*2*_InnerColorPower);
o.Alpha = (_InnerAlphaBase + (pow (rim, _AlphaPower))*_AllPower) * col.a;
}
ENDCG
}
Fallback "VertexLit"
}
工程下载地址
链接:https://pan.baidu.com/s/11LTiIW0lIosNqokpucq6SQ
提取码:td8s