代码:https://github.com/HushengStudent/UnityAvatarProject
参考:https://blog.uwa4d.com/archives/avartar.html
参考文章中,通过Combine Mesh降低DrawCall,合并材质的时候,纹理是通过合并成一个纹理,Mesh计算新的UV坐标来实现的。
本文旨在修改纹理合并的这一个过程,通过修改Shader的UV计算来实现。
UV
为了方便计算UV,这里设置四个纹理大小都一样(方便计算UV)。
//四个纹理的uv布局;
var tempUVList = new List<Rect>() {
new Rect(0f,0f,0.5f,0.5f),
new Rect(0.5f,0f,0.5f,0.5f),
new Rect(0f,0.5f,0.5f,0.5f),
new Rect(0.5f,0.5f,0.5f,0.5f),
};
var texUV = tempUVList.ToArray();
Shader
身体部件纹理
_Texture_jiao ("_Texture_jiao", 2D) = "white" {}
_Texture_shen ("_Texture_shen", 2D) = "white" {}
_Texture_shou ("_Texture_shou", 2D) = "white" {}
_Texture_tou ("_Texture_tou", 2D) = "white" {}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _Texture_jiao);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//身体部件遮罩
fixed jiao_mask = (1 - step(0.5,i.uv.x)) * (1 - step(0.5,i.uv.y));
//身体部件UV计算
fixed2 jiao_uv = fixed2(i.uv.x,i.uv.y)*2;
fixed4 jiao_color = tex2D(_Texture_jiao, saturate(jiao_uv)) * jiao_mask;
fixed shen_mask = step(0.5,i.uv.x) * (1 - step(0.5,i.uv.y));
fixed2 shen_uv = fixed2(i.uv.x-0.5,i.uv.y)*2;
fixed4 shen_color = tex2D(_Texture_shen, saturate(shen_uv)) * shen_mask;
fixed shou_mask = (1 - step(0.5,i.uv.x)) * step(0.5,i.uv.y);
fixed2 shou_uv = fixed2(i.uv.x,i.uv.y-0.5)*2;
fixed4 shou_color = tex2D(_Texture_shou, saturate(shou_uv)) * shou_mask;
fixed tou_mask = step(0.5,i.uv.x) * step(0.5,i.uv.y);
fixed2 tou_uv = fixed2(i.uv.x-0.5,i.uv.y-0.5)*2;
fixed4 tou_color = tex2D(_Texture_tou, saturate(tou_uv)) * tou_mask;
return jiao_color + shen_color + shou_color + tou_color;
}