1、我们的物件组成如下:
这是没烘焙之前。
草使用的是我们自己的shader,plane使用的是内置的standard着色器。
自己shader如下:
Shader "Unlit/GrassCutoff"
{
Properties
{
_MainTex2 ("Texture", 2D) = "white" {}
_Cutoff2("_Cutoff", Range(0,1)) = 0
}
SubShader
{
LOD 100
Pass
{
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;
};
sampler2D _MainTex2;
float4 _MainTex2_ST;
float _Cutoff2;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex2);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex2, i.uv);
clip(col.a - _Cutoff2);
return col;
}
ENDCG
}
}
}
这是烘焙之后的样子。显然是不正确的。
改成这个样子:
Shader "Unlit/GrassCutoff"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Cutoff2("_Cutoff", Range(0,1)) = 0
}
SubShader
{
LOD 100
Tags { "RenderType" = "TransparentCutout" "Queue" = "AlphaTest"}
Pass
{
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;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Cutoff2;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
clip(col.a - _Cutoff2);
return col;
}
ENDCG
}
}
}
烘焙之后,结果正确。
提高点烘焙分辨率,结果如下:
ok,我们主要做了两个改动:
_MainTex (“Texture”, 2D) = “white” {}
Tags { “RenderType” = “TransparentCutout” “Queue” = “AlphaTest”}
结果就正确了。
在文章:
https://catlikecoding.com/unity/tutorials/rendering/part-16/
https://www.jianshu.com/p/7983e09f7b0e
都说明了,unity的烘焙器,识别指定的关键字。
(1)_MainTex的a通道
(2)_Cutoff
(3)RenderType 和Queue设置正确
半透明的物体也这么设置即可。