UnityShader 透明效果
关于渲染队列:
实现透明度测试效果
clip(x):如果给定参数的任何一个分量是负值,则舍弃当前像素的输出颜色。
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Unlit/AlphaTestMat"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Cutoff ("Cutoff", Range(0, 1.0)) = 0.3
_Color ("Color", Color) = (1.0, 1.0, 1.0, 1.0)
_Specular("Specular", Color) = (1.0, 1.0, 1.0, 1.0)
_Gloss("Gloss", float) = 8.0
}
SubShader
{
Tags {
"RenderType"="AlphaTest"
"IgnoreProjector"="True"
"RenderType"="TransparentCutout"
}
Pass
{
Tags{"LightMode"="ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float _Cutoff;
half4 _Color;
half4 _Specular;
float _Gloss;
struct a2v{
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
struct v2f{
float4 vertex : SV_POSITION;
float3 worldNormal : TEXCOORD1;
float3 worldLight : TEXCOORD2;
float3 worldView : TEXCOORD3;
float2 uv : TEXCOORD0;
};
v2f vert(a2v i){
v2f o;
UNITY_INITIALIZE_OUTPUT(v2f, o);
o.vertex = UnityObjectToClipPos(i.vertex);
float4 worldPos = mul(unity_ObjectToWorld, i.vertex);
o.worldNormal = normalize(UnityObjectToWorldNormal(i.normal));
o.worldLight = normalize(UnityWorldSpaceLightDir(worldPos));
o.worldView = normalize(UnityWorldSpaceViewDir(worldPos));
o.uv = TRANSFORM_TEX(i.texcoord, _MainTex);
return o;
}
half4 frag(v2f i) : SV_TARGET{
half4 meshColor = tex2D(_MainTex, i.uv);
clip(meshColor.a - _Cutoff);
half3 diffuse = _LightColor0 * meshColor.rgb * saturate(dot(i.worldNormal, i.worldLight));
half3 ambient = UNITY_LIGHTMODEL_AMBIENT * _Color;
half3 specular = _LightColor0 * _Specular * pow(saturate(dot(reflect(-i.worldLight, i.worldNormal), i.worldView)), _Gloss);
return half4(diffuse + ambient + specular, 1.0);
}
ENDCG
}
}
}
透明度混合实现
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Unlit/AlphaBlendMat"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_AlphaScale ("AlphaScale", Range(0, 1.0)) = 0.3
_Color ("Color", Color) = (1.0, 1.0, 1.0, 1.0)
_Specular("Specular", Color) = (1.0, 1.0, 1.0, 1.0)
_Gloss("Gloss", float) = 8.0
}
SubShader
{
Tags {
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
}
Pass
{
Tags{"LightMode"="ForwardBase"}
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float _AlphaScale;
half4 _Color;
half4 _Specular;
float _Gloss;
struct a2v{
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
struct v2f{
float4 vertex : SV_POSITION;
float3 worldNormal : TEXCOORD1;
float3 worldLight : TEXCOORD2;
float3 worldView : TEXCOORD3;
float2 uv : TEXCOORD0;
};
v2f vert(a2v i){
v2f o;
UNITY_INITIALIZE_OUTPUT(v2f, o);
o.vertex = UnityObjectToClipPos(i.vertex);
float4 worldPos = mul(unity_ObjectToWorld, i.vertex);
o.worldNormal = normalize(UnityObjectToWorldNormal(i.normal));
o.worldLight = normalize(UnityWorldSpaceLightDir(worldPos));
o.worldView = normalize(UnityWorldSpaceViewDir(worldPos));
o.uv = TRANSFORM_TEX(i.texcoord, _MainTex);
return o;
}
half4 frag(v2f i) : SV_TARGET{
half4 meshColor = tex2D(_MainTex, i.uv);
half3 diffuse = _LightColor0 * meshColor.rgb * saturate(dot(i.worldNormal, i.worldLight));
half3 ambient = UNITY_LIGHTMODEL_AMBIENT * _Color;
half3 specular = _LightColor0 * _Specular * pow(saturate(dot(reflect(-i.worldLight, i.worldNormal), i.worldView)), _Gloss);
return half4(diffuse + ambient + specular, meshColor.a * _AlphaScale);
}
ENDCG
}
}
}
在透明度混合过程中,对于半透明的物体会最后进行渲染,优先渲染非透明物体,之后将透明物体的颜色与非透明混合。
但由于模型之间的互相交叉,有时候会得到错误的半透明效果,这时候就可以使用开启深度写入的透明度混合。只需要在透明度混合的基础上增加一个Pass,进行深度写入但不输出颜色。
Pass{
ZWrite On
ColorMask 0
}
由于在Queue中已经设置了Transparent,所以透明物体会最后进行渲染,所以进行深度检测不会遮挡非透明物体。
ShaderLab混合模式
Shaderlab中提供了很多的透明混合方式,部分效果如下,但这里不做出说明。
透明渲染的双面效果
我们很容易能发现,在透明度测试的实现过程中,模型只有一面能被渲染出来。这是由于Shader的Cull指令默认为Back,背对着摄像机的渲染图元不会被渲染出来。所以将Cull指令设置为Off即可。
但对于透明度混合,由于关闭了深度写入,可能对导致模型背面比正面先进行渲染,得到错误的渲染结果。可以通过使用两个Pass,第一个渲染模型背面(Cull Front),第二个渲染模型正面(Cull Back)。