双面渲染的透明效果
效果透明度测试的双面渲染透明效果
因为透明度测试没有关深度写入,所以这个透明物体的像素级别的深度信息都是排序正确的,所以直接使用Cull指令即可
Pass
{
Tags{"LightModel"="ForwardBase"}
Cull Off
}
透明度混合的双面渲染透明效果
透明度混合关闭了深度写入,所有这个透明物体没有正确的先后顺序,而渲染透明效果的很重要的一点就是:一定要保证图元是从后往前渲染,所以如果直接关掉剔除功能,是得不到正确的前后面的。所以使用两个Pass来渲染,第一个只渲染背面,第二个值渲染正面,由于Pass是按顺序执行的,所有我们的背面总在正面之前渲染,所以能得到正确的渲染关系。
Shader "Unlit/AlphaBlend"
{
Properties
{
_Color("Color Tint",Color)=(1,1,1,1)
_MainTex("Main Tex",2D) = "white"{}
_AlphaScale("Alpha Scale",Range(0,1))=1
}
SubShader
{
Tags{"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Pass
{
Tags{"LightModel"="ForwardBase"}
Cull Front //先渲染背面
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#include "Lighting.cginc"
#pragma vertex vert
#pragma fragment frag
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _AlphaScale;
struct a2v
{
float4 vertex:POSITION;
float3 normal:NORMAL;
float4 texcoord:TEXCOORD0;
};
struct v2f
{
float4 pos:SV_POSITION;
float3 worldPos:TEXCOORD0;
float3 worldNormal:TEXCOORD1;
float2 uv:TEXCOORD2;
};
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
fixed4 frag(v2f i):SV_TARGET
{
float3 wordNormal = normalize(i.worldNormal);
float3 worldLightDir = UnityWorldSpaceLightDir(i.worldPos);
fixed4 texColor = tex2D(_MainTex,i.uv);
fixed3 albedo = texColor.rgb*_Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * saturate(dot(wordNormal,worldLightDir));
return fixed4(ambient+diffuse,texColor.a*_AlphaScale);
}
ENDCG
}
Pass
{
Tags{"LightModel"="ForwardBase"}
Cull Back //在渲染正面
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#include "Lighting.cginc"
#pragma vertex vert
#pragma fragment frag
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _AlphaScale;
struct a2v
{
float4 vertex:POSITION;
float3 normal:NORMAL;
float4 texcoord:TEXCOORD0;
};
struct v2f
{
float4 pos:SV_POSITION;
float3 worldPos:TEXCOORD0;
float3 worldNormal:TEXCOORD1;
float2 uv:TEXCOORD2;
};
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
fixed4 frag(v2f i):SV_TARGET
{
float3 wordNormal = normalize(i.worldNormal);
float3 worldLightDir = UnityWorldSpaceLightDir(i.worldPos);
fixed4 texColor = tex2D(_MainTex,i.uv);
fixed3 albedo = texColor.rgb*_Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * saturate(dot(wordNormal,worldLightDir));
return fixed4(ambient+diffuse,texColor.a*_AlphaScale);
}
ENDCG
}
}
Fallback "Transparent/VertexLit"
}
网友评论