NGUI下alpha通道的分离与合并

U3D发布到iOS平台时,图片压缩格式一般设置成PVRTC,而发布到Android平台时候一般需要设置成ETC1,这样做的目的是为了图片压缩力度更大,降低内存的消耗。但是问题来了,Android下ETC1格式不支持Alpha通道,而且带Alpha通道的图消耗非常大。所以,为了能降低各种贴图的消耗,需要把贴图的RGB与alpha通道拆分出来保存为两张图,一张图只保存RGB信息,另外一张只保存Alpha信息。

    1.使用texturepacker制作图集

     首先在网上随便找了两张带Alpha通道的图片,导入texturepacker,设置如下图:


    publish后生成一张PNG图和一个txt文件如图:


然后导入到Unity3D中,制作图集,具体如何制作NGIU图集可Google(不太喜欢用度娘),做好的图集如图:


    好了,到此我们的图集做好了,然后随便弄个按钮或者精灵出来,看看它消耗多大(因为随便找的图,所以比较大,暂时不要惊慌失措,只是为了后面有一个比较),如下图:


    到此,原图占用2M。

    2.拆分贴图的ALpha通道

    具体如何拆分Alpha通道,有很多种办法,比如直接在PS中拆分,也可以在Unity3D中写工具,工具的具体代码可参考博友的文章:http://blog.csdn.NET/u010153703/article/details/45502895。其实核心就这部分:

  1. Texture2D rgbTex = new Texture2D(sourcetex.width, sourcetex.height, TextureFormat.RGB24, true);  
  2.        Texture2D alphaTex = new Texture2D((int)(sourcetex.width * sizeScale), (int)(sourcetex.height * sizeScale), TextureFormat.RGB24, true);  
  3.   
  4.        for (int i = 0; i < sourcetex.width; ++i)  
  5.            for (int j = 0; j < sourcetex.height; ++j)  
  6.            {  
  7.                Color color = sourcetex.GetPixel(i, j);  
  8.                Color rgbColor = color;  
  9.                Color alphaColor = color;  
  10.                alphaColor.r = color.a;  
  11.                alphaColor.g = color.a;  
  12.                alphaColor.b = color.a;  
  13.                rgbTex.SetPixel(i, j, rgbColor);  
  14.                alphaTex.SetPixel((int)(i * sizeScale), (int)(j * sizeScale), alphaColor);  
  15.            }  
  16.   
  17.        rgbTex.Apply();  
  18.        alphaTex.Apply();  
  19.   
  20.        byte[] bytes = rgbTex.EncodeToPNG();  
  21.        File.WriteAllBytes(GetRGBTexPath(_texPath), bytes);  
  22.        bytes = alphaTex.EncodeToPNG();  
  23.        File.WriteAllBytes(GetAlphaTexPath(_texPath), bytes);  

对上图中TP这张图拆分后得到TP_RGB和TP_Alpha两张图:


对于拆分后的两张图怎么合并回去呢,可以通过在Shader中处理,具体代码如下:

  1. Shader "Unlit/Merge RGB And Alpha"  
  2. {  
  3.  Properties  
  4.  {  
  5.  _MainTex ("rgb tex", 2D) = "black" {}  
  6.  _AlphaTex("alpha tex",2D) = "white"{}  
  7.  }  
  8.  SubShader  
  9.  {  
  10.  LOD 100  
  11.  Tags  
  12.  {  
  13.  "Queue" = "Transparent"  
  14.  "IgnoreProjector" = "True"  
  15.  "RenderType" = "Transparent"  
  16.  }  
  17.  Cull Off  
  18.  Lighting Off  
  19.  ZWrite Off  
  20.  Fog { Mode Off }  
  21.  Offset -1, -1  
  22.  Blend SrcAlpha OneMinusSrcAlpha  
  23.  Pass  
  24.  {  
  25.  CGPROGRAM  
  26.  #pragma vertex vert  
  27.  #pragma fragment frag  
  28.  #include "UnityCG.cginc"  
  29.  struct appdata_t  
  30.  {  
  31.  float4 vertex : POSITION;  
  32.  float2 texcoord : TEXCOORD0;  
  33.  fixed4 color : COLOR;  
  34.  };  
  35.  struct v2f  
  36.  {  
  37.  float4 vertex : SV_POSITION;  
  38.  half2 texcoord : TEXCOORD0;  
  39.  fixed4 color : COLOR;  
  40.  };  
  41.  sampler2D _MainTex;  
  42.  float4 _MainTex_ST;  
  43.  sampler2D _AlphaTex;  
  44.  float4 _AlphaTex_ST;  
  45.  v2f vert (appdata_t v)  
  46.  {  
  47.  v2f o;  
  48.  o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);  
  49.  o.texcoord = v.texcoord;  
  50.  o.color = v.color;  
  51.  return o;  
  52.  }  
  53.  fixed4 frag (v2f i) : COLOR  
  54.  {  
  55.  //fixed4 col = tex2D(_MainTex, i.texcoord) * i.color;  
  56.  //return col;  
  57.  fixed4 texcol = tex2D(_MainTex, i.texcoord);   
  58.  fixed4 result = texcol;  
  59.  result.a = tex2D(_AlphaTex,i.texcoord).r*i.color.a;  
  60.  //灰度计算  
  61.   if (i.color.r < 0.001)   
  62.     {   
  63.         //float grey  =  dot(result.rgb, float3(0.299, 0.587, 0.114));   
  64.         //整数运算速度更快  
  65.         float gray = (result.r*299 + result.g*587 + result.b*114 + 500) / 1680;  
  66.         result.rgb = float3(gray, gray, gray);   
  67.     }   
  68.  return result;  
  69.  }  
  70.  ENDCG  
  71.  }  
  72.  }  
  73.  SubShader  
  74.  {  
  75.  LOD 100  
  76.  Tags  
  77.  {  
  78.  "Queue" = "Transparent"  
  79.  "IgnoreProjector" = "True"  
  80.  "RenderType" = "Transparent"  
  81.  }  
  82.  Pass  
  83.  {  
  84.  Cull Off  
  85.  Lighting Off  
  86.  ZWrite Off  
  87.  Fog { Mode Off }  
  88.  Offset -1, -1  
  89.  ColorMask RGB  
  90.  AlphaTest Greater .01  
  91.  Blend SrcAlpha OneMinusSrcAlpha  
  92.  ColorMaterial AmbientAndDiffuse  
  93.  SetTexture [_MainTex]  
  94.  {  
  95.  Combine Texture * Primary  
  96.  }  
  97.  }  
  98.  }  
  99. }  
                                       

这时候,从新再弄个按钮或者精灵来看看效果如下:


基本上可以很好的还原原图。好了,我们在Profiler比较一下现在这两张图到底占了多少内存,入下图:


拆分后的两张图加起来大概1.25M,可喜可贺呀羡慕

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值