unity ngui优化 Alpha通道处理

2015-10-25 雨辰 Unity3D


1:首先分离 Alpha 通道
[MenuItem("Tools/Atlas/分离TextureAlpha通道")]
public static void DivideTexture()
{
    //获取图片
    string path = AssetDatabase.GetAssetPath(Selection.activeInstanceID);
    Debug.Log(path);

    //设置原图可编辑(RGBA) xxx.png
    TextureImporter importer = TextureImporter.GetAtPath(path) as TextureImporter;
    importer.isReadable = true;
    importer.SaveAndReimport();

    //读取原图(RGBA)
    Texture2D source = AssetDatabase.LoadAssetAtPath<Texture>(path) as Texture2D;
    float sizeScale = 1;
    Texture2D rgbTex = new Texture2D(source.width, source.height, TextureFormat.RGB24, true);
    Texture2D alphaTex = new Texture2D((int)(source.width * sizeScale), (int)(source.height * sizeScale), TextureFormat.RGB24, true);
    Color[] rgbColors = new Color[source.width * source.height];
    Color[] alphaColors = new Color[source.width * source.height];

    for (int i = 0; i < source.width; ++i)
    {
        for (int j = 0; j < source.height; ++j)
        {
            Color color = source.GetPixel(i, j);
            Color rgbColor = color;
            if (color.a == 0.0f)
            {
                rgbColor.r = 0;
                rgbColor.g = 0;
                rgbColor.b = 0;
            }
            rgbColor.a = 1;
            rgbColors[source.width * j + i] = color;

            Color alphaColor = color;
            alphaColor.r = color.a;
            alphaColor.g = color.a;
            alphaColor.b = color.a;
            alphaColor.a = 1;
            alphaColors[source.width * j + i] = alphaColor;
        }
    }
    rgbTex.SetPixels(rgbColors);
    alphaTex.SetPixels(alphaColors);
    rgbTex.Apply();
    alphaTex.Apply();

    //生成分离图片RGB + Alpha
    byte[] bytes = rgbTex.EncodeToPNG();
    File.WriteAllBytes(path.Replace(source.name + ".png", source.name + "_RGB.png"), bytes);
    bytes = alphaTex.EncodeToPNG();
    File.WriteAllBytes(path.Replace(source.name + ".png", source.name + "_A.png"), bytes);
    AssetDatabase.ImportAsset(path.Replace(source.name + ".png", source.name + "_RGB.png"));
    AssetDatabase.ImportAsset(path.Replace(source.name + ".png", source.name + "_A.png"));
    AssetDatabase.Refresh();

    Debug.Log("finish");
}
2:修改 ngui shader


这是原Shader Unlit/Transparent Colored,用来对比



Shader "Unlit/Transparent Colored"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
    }

    ...
        ...
            sampler2D _MainTex;
	    float4 _MainTex_ST;

            fixed4 frag (v2f IN) : SV_Target
            {
                // Sample the texture
                half4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
                return col;
            }
}
对应的Unlit/Transparent Colored Divide, 修改为




Shader "Unlit/Transparent Colored Divide"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
        _AlphaTex("Alpha (A)",2D) = "white"{}
    }
    ...
        ...
            sampler2D _MainTex;
	    sampler2D _AlphaTex
            float4 _MainTex_ST;
            fixed4 frag (v2f IN) : SV_Target
            {
                fixed4 texcol = tex2D(_MainTex, IN.texcoord) * IN.color;

                //Alpha通道合并
                texcol.a *= tex2D(_AlphaTex, IN.texcoord).r;  

                return texcol;
            }
}
只是在Shader中增加了一个_AlphaTex 
我们分离的时候将图片的Alpha值存入_Alpha图片的r值中,所以在合并的时候就用这个图片的r值来控制整个图片的a值 
至于其他的Shader,也是和这个一样的修改即可



标签: Unity3D-Shader Unity3D

发表评论:

雨辰 joyimp|@2011-2018 京ICP备16030765号