核心命令
- Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType" = "Transprent" }
// 加入透明物体渲染队列,忽略投影器影响,提前归入组
- ZWrite Off
//关闭深度写入
- Blend SrcAlpha OneMinusSrcAlpha
//开启混合
效果图
透明测试,透明混合,深度写入半透明效果
透明测试
Shader "Unlit/016"
{
Properties
{
_MainTex ("MainTex", 2D )= "white" {}
_Diffuse("Diffuse",Color) = (1,1,1,1)
_Cutoff("Alpha Cutoff",Range(0,1)) = 0.5
}
SubShader
{
Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" }
LOD 100
Pass
{
//定义光照流水线
Tags{"LightMode" = "ForwardBase"}
//关闭背面遮挡剔除
Cull off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Diffuse;
float _Cutoff;
struct v2f
{
float4 vertex :SV_POSITION;
fixed3 worldNormal: TEXCOORD0;
float3 worldPos : TEXCOORD1;
float2 uv : TEXCOORD2;
};
v2f vert (appdata_base v)
{
v2f o;
//顶点位置
o.vertex = UnityObjectToClipPos(v.vertex);
//法线方向
fixed3 worldNormal =UnityObjectToWorldNormal( v.normal);
o.worldNormal=worldNormal;
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
//UV =顶点纹理坐标进行缩放+偏移
o.uv =TRANSFORM_TEX(v.texcoord, _MainTex);//v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//纹素值=对纹理进行采样(采样纹理,float2纹理坐标)
fixed4 texColor = tex2D(_MainTex,i.uv);
if((texColor.a - _Cutoff)<0)
{
discard;
}
//光源方向
fixed3 worldLightDir = UnityWorldSpaceLightDir(i.worldPos);
//漫反射光=入射光线强度*纹素值*材质的漫反射系数*取值为正数(表面法线方向 · 光源方向)
fixed3 diffuse = _LightColor0.rgb * texColor.rgb * _Diffuse.rgb * (dot(worldLightDir,i.worldNormal)*0.5+0.5);
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 color = ambient + diffuse ;
return fixed4(color,1);
}
ENDCG
}
}
FallBack "Diffuse"
}
透明混合
Shader "Unlit/017"
{
Properties
{
_MainTex ("MainTex", 2D )= "white" {}
_Diffuse("Diffuse",Color) = (1,1,1,1)
_AlphaScale("Alpha Scale", Range(0,1)) = 1
}
SubShader
{
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType" = "Transprent" }
LOD 100
//关闭深度写入
ZWrite Off
//开启混合
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
//定义光照流水线
Tags{"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Diffuse;
float _AlphaScale;
struct v2f
{
float4 vertex :SV_POSITION;
fixed3 worldNormal: TEXCOORD0;
float3 worldPos : TEXCOORD1;
float2 uv : TEXCOORD2;
};
v2f vert (appdata_base v)
{
v2f o;
//顶点位置
o.vertex = UnityObjectToClipPos(v.vertex);
//法线方向
fixed3 worldNormal =UnityObjectToWorldNormal( v.normal);
o.worldNormal=worldNormal;
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
//UV =顶点纹理坐标进行缩放+偏移
o.uv =TRANSFORM_TEX(v.texcoord, _MainTex);//v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//纹素值=对纹理进行采样(采样纹理,float2纹理坐标)
fixed4 texColor = tex2D(_MainTex,i.uv);
//光源方向
fixed3 worldLightDir = UnityWorldSpaceLightDir(i.worldPos);
//漫反射光=入射光线强度*纹素值*材质的漫反射系数*取值为正数(表面法线方向 · 光源方向)
fixed3 diffuse = _LightColor0.rgb * texColor.rgb * _Diffuse.rgb * (dot(worldLightDir,i.worldNormal)*0.5+0.5);
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 color = ambient + diffuse ;
return fixed4(color,texColor.a * _AlphaScale);
}
ENDCG
}
}
FallBack "Treansparent/VertexLit"
}
深度写入半透明效果
Shader "Unlit/018"
{
Properties
{
_MainTex ("MainTex", 2D )= "white" {}
_Diffuse("Diffuse",Color) = (1,1,1,1)
_AlphaScale("Alpha Scale", Range(0,1)) = 1
}
SubShader
{
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType" = "Transprent" }
LOD 100
pass
{
//开启深度写入
ZWrite On
//颜色遮罩,意味着不写入任何颜色通道
ColorMask 0
}
Pass
{
//定义光照流水线
Tags{"LightMode" = "ForwardBase"}
//关闭深度写入
ZWrite Off
//开启混合
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Diffuse;
float _AlphaScale;
struct v2f
{
float4 vertex :SV_POSITION;
fixed3 worldNormal: TEXCOORD0;
float3 worldPos : TEXCOORD1;
float2 uv : TEXCOORD2;
};
v2f vert (appdata_base v)
{
v2f o;
//顶点位置
o.vertex = UnityObjectToClipPos(v.vertex);
//法线方向
fixed3 worldNormal =UnityObjectToWorldNormal( v.normal);
o.worldNormal=worldNormal;
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
//UV =顶点纹理坐标进行缩放+偏移
o.uv =TRANSFORM_TEX(v.texcoord, _MainTex);//v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//纹素值=对纹理进行采样(采样纹理,float2纹理坐标)
fixed4 texColor = tex2D(_MainTex,i.uv);
//光源方向
fixed3 worldLightDir = UnityWorldSpaceLightDir(i.worldPos);
//漫反射光=入射光线强度*纹素值*材质的漫反射系数*取值为正数(表面法线方向 · 光源方向)
fixed3 diffuse = _LightColor0.rgb * texColor.rgb * _Diffuse.rgb * (dot(worldLightDir,i.worldNormal)*0.5+0.5);
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 color = ambient + diffuse ;
return fixed4(color,texColor.a * _AlphaScale);
}
ENDCG
}
}
FallBack "Treansparent/VertexLit"
}
网友评论