介绍UnityShader的表面剔除(表面剪裁)技术
ShaderLab Cull命令
shaderLab语句 | 说明 |
---|---|
Cull Off | 不剔除 |
Cull Back(默认) | 剔除背面(内表面) |
Cull Front | 剔除正面(外表面) |
什么是表面剔除?为什么要进行表面剔除?
当一个mesh组件的信息被传递后,我们可以通过代码决定哪些部分渲染(render)出来,而哪些部分不要,这个过程就像把那些不要的部分剔除了,我们看不到他,虽然他的mesh信息还在,但是我们的GPU不会去处理它,肯定比剔除前GPU的性能消耗要低。
这个过程就好比我们的mesh组件是一个透明的膜,我们假设这个胶纸我们根本看不到,而片段着色器在着色的时候像毛笔选择性地上色,最后的效果是我们可能看到膜的一部分是可见的,但是不见的地方,膜还是存在的,只是我们没有给他上色,我们既看不看他们,也不需要再他们上面画宝贵的墨水(GPU并行处理能力)
引用自:
(原) 解读Unity中的CG编写Shader系列3——表面剔除与剪裁模式
表面剔除效果测试示例
一个Cull的应用实例-物体的外描边
代码
Shader "Hidden/Outline"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_OutlineWidth ("outline width", Float) = 0
_OutlineColor ("outline color", Color) = (1,1,1,1)
}
SubShader
{
Cull Off
//使用cull front把外表面裁掉
Pass
{
name "InnerFace"
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float _OutlineWidth;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex+v.normal*float4(_OutlineWidth,_OutlineWidth,_OutlineWidth,0));
o.uv = v.uv;
return o;
}
fixed4 _OutlineColor;
fixed4 frag (v2f i) : SV_Target
{
// fixed4 col = tex2D(_MainTex, i.uv);
fixed4 col = _OutlineColor;
return col;
}
ENDCG
}
Pass
{
name "OuterFace"
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;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}