unity安卓游戏优化ETC压缩及Alpha通道处理

发表于2016-01-07
评论5 4.1k浏览

  在unity中。带有透明通道的图片压缩后。均会出现一定的质量的下降。并且带有透明通道的图片占用内存较大,通过翻阅大量典籍,本人找到并验证了一条可行性解决方案,特分享给大家。具体做法就是将透明通道和图片内容剥离开来。再用4个Shader合并。这样就能减少一半的大小。

1、首先。我们将图片放入texturepacker中。txt类型选unity - json data(txt),导出图片类型选tga。然后导出。如下:


2、接着。我们将tga图片放入PS中。找到图片通道处,并做如下处理: 

如上。我们选中Alpha 1.右键。删除该透明通道。然后将图片存储为bmp图片。

然后。我们可以在菜单中后退一步。或者重新打开没有删除透明通道的图片。执行如下操作。

· 选中Alpha1. 按 ctrl + c 复制改透明通道。

· 选中 红 通道。ctrl + v 粘贴通道。绿 蓝 通道执行同样的操作。

· 删除Alpha 1 透明通道。将图片保存为bmp。

将1步骤中导出的txt文件与1)与3)步骤中的bmp格式文件导入unity中。


3、接下来,我们将图片移到Unity中。开始制作图集。然后给图集的材质球赋值我们的shader(Shader代码在最下方)。


4、最后,我们将txt文件与材质球拖放到图集的相应位置,图集制作完成


5、在结尾,将自己编写的shader代码附上:

Shader "Unlit/UIaddETC" 

{

    Properties 

{

 _MainTex ("Base (RGB)", 2D) = "white" { }

 _AlphaTex("AlphaTex",2D) = "white"{}

}

    SubShader

{

 LOD 100

 Tags

 {

  "Queue" = "Transparent"

"IgnoreProjector" = "True"

"RenderType" = "Transparent"

 } 

 Cull Off

 Lighting Off

 ZWrite Off

 Fog { Mode Off }

 Offset -1, -1

 Blend SrcAlpha OneMinusSrcAlpha

         Pass

 {

CGPROGRAM

#pragma vertex vert

#pragma fragment frag

#include "UnityCG.cginc" 

sampler2D _MainTex;

sampler2D _AlphaTex;

float _AlphaFactor;

struct appdata_t

{

float4 vertex : POSITION;

float2 texcoord : TEXCOORD0;

fixed4 color : COLOR;

};

struct v2f 

{

float4 vertex : SV_POSITION;

half2 texcoord : TEXCOORD0;

fixed4 color : COLOR;

}; 

half4 _MainTex_ST;

half4 _AlphaTex_ST; 

v2f vert (appdata_t v)

{

v2f o;

o.vertex = mul (UNITY_MATRIX_MVP, v.vertex);

o.texcoord =  v.texcoord;

o.color = v.color;

return o;

half4 frag (v2f i) : COLOR

{

half4 texcol = tex2D (_MainTex, i.texcoord); 

half4 result = texcol ;

result.a = tex2D(_AlphaTex,i.texcoord)*i.color.a ;

return result;

}

ENDCG

 }

    }

SubShader

{

LOD 100

Tags

{

"Queue" = "Transparent"

"IgnoreProjector" = "True"

"RenderType" = "Transparent"

}

Pass

{

Cull Off

Lighting Off

ZWrite Off

Fog { Mode Off }

Offset -1, -1

ColorMask RGB

Blend SrcAlpha OneMinusSrcAlpha

ColorMaterial AmbientAndDiffuse

SetTexture [_MainTex]

{

Combine Texture * Primary

}

}

}

因为unity ScrollView 会让处理过的图片显示有问题,故参照NGUI自带的shader(例:Transparent Colored,Transparent Colored 1,Transparent Colored 2,Transparent Colored 3,)也对应写了三个shader,在此附上最二个shader如下:

Shader "Hidden/Unlit/UIaddETC 1" {

Properties {

 _MainTex ("Base (RGB)", 2D) = "white" { }

 _AlphaTex("AlphaTex",2D) = "white"{}

 }

    SubShader

{

         LOD 200

 Tags

 {

"Queue" = "Transparent+8"

"IgnoreProjector" = "True"

     "RenderType" = "Transparent+8"

 }

         Pass

 {

Cull Off

Lighting Off

ZWrite Off

Offset -1, -1

Fog { Mode Off }

ColorMask RGB

Blend SrcAlpha OneMinusSrcAlpha

CGPROGRAM

#pragma vertex vert

#pragma fragment frag 

#include "UnityCG.cginc" 

sampler2D _MainTex;

sampler2D _AlphaTex;

float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0);

float2 _ClipArgs0 = float2(1000.0, 1000.0);

float _AlphaFactor;

struct appdata_t

{

float4 vertex : POSITION;

float2 texcoord : TEXCOORD0;

fixed4 color : COLOR;

};

struct v2f 

{

float4 vertex : POSITION;

half4 color : COLOR;

float2 texcoord : TEXCOORD0;

float2 worldPos : TEXCOORD1;

};

half4 _MainTex_ST;

half4 _AlphaTex_ST; 

v2f vert (appdata_t v)

{

v2f o;

o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);

o.texcoord =  v.texcoord;

o.color = v.color;

o.worldPos = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy;

return o;

half4 frag (v2f i) : COLOR

{

   //  Softness factor

float2 factor = (float2(1.0, 1.0) - abs(i.worldPos)) * _ClipArgs0;

half4 texcol = tex2D (_MainTex, i.texcoord) * i.color;

 half4 result = texcol ; 

result.a *= tex2D(_AlphaTex,i.texcoord)*clamp( min(factor.x, factor.y), 0.0, 1.0); 

return result;

}

ENDCG

 }

    }

     SubShader

{

LOD 100

Tags

{

"Queue" = "Transparent+8"

"IgnoreProjector" = "True"

"RenderType" = "Transparent+8"

}

Pass

{

Cull Off

Lighting Off

ZWrite Off

Fog { Mode Off }

ColorMask RGB

Blend SrcAlpha OneMinusSrcAlpha

ColorMaterial AmbientAndDiffuse

SetTexture [_MainTex]

{

Combine Texture * Primary

}

}

}

  相信聪明的你已经发现了规律,接下去的UIaddETC2与UIaddETC3大家应该也能写出来了,在此就不附代码了。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引