Unity RGBA16 + Dither

参考:http://www.cnblogs.com/zsb517/p/6322773.html


private void HandleTextureTag(TextureImporter texImp)
    {
        string path = assetPath.ToLower();
        if (path.StartsWith(UI_PATH_PRE))
        {

            texImp.spritePackingTag = "";
        }
    }

    private void HandleTextureFormat(TextureImporter texImp, Texture2D texture)
    {
        string path = assetPath.ToLower();
        if (path.StartsWith(UI_PATH_PRE))
        {
            
            TextureImporterPlatformSettings setting = new TextureImporterPlatformSettings();
            //setting.format = TextureImporterFormat.RGBA16;
            //setting.overridden = true;
            setting.textureCompression = TextureImporterCompression.Uncompressed;

            texImp.SetPlatformTextureSettings(setting);

            //texImp.SetPlatformTextureSettings("Android", 2048, TextureImporterFormat.RGBA16);

            EditorUtility.CompressTexture(texture, TextureFormat.RGBA4444, TextureCompressionQuality.Best);
        }
    }
    // 测试用的,结论,高斯模糊不够Dither好看
    private void GaussianDitherTexture(Texture2D texture)
    {
        float[] curve = new float[7] { 0.0205f, 0.0855f, 0.232f, 0.324f, 0.232f, 0.0855f, 0.0205f };

        Color[] curve4 = new Color[7];
        curve4[0] = new Color ( 0.0205f, 0.0205f, 0.0205f, 0f );
        curve4[1] = new Color ( 0.0855f, 0.0855f, 0.0855f, 0 );
        curve4[2] = new Color ( 0.232f, 0.232f, 0.232f, 0 );
        curve4[3] = new Color ( 0.324f, 0.324f, 0.324f, 1 );
        curve4[4] = new Color ( 0.232f, 0.232f, 0.232f, 0 );
        curve4[5] = new Color ( 0.0855f, 0.0855f, 0.0855f, 0 );
        curve4[6] = new Color ( 0.0205f, 0.0205f, 0.0205f, 0 );

        //int interval = 2;

        //half2 coords = uv - netFilterWidth * 3.0;

        //half4 color = 0;
         加权平均
        //for (int i = 0; i < 7; i++)
        //{
        //    half4 tap = tex2D(_MainTex, coords);
        //    // 像素值乘上对应的权值
        //    color += tap * curve4[i];
        //    // 移到下一个像素
        //    coords += netFilterWidth;
        //}


        var texw = texture.width;
        var texh = texture.height;
        var pixels = texture.GetPixels();
        var offs = 0;
        int netFilterWidth = 2;

        // 水平的
        for (var y = 0; y < texh; y++)
        {
            for (var x = 0; x < texw; x++)
            {
                Color col = Color.black;
                int xBegin = x - netFilterWidth * 3;

                for (int i = 0; i < 7; i++)
                {
                    int curX = xBegin + i * netFilterWidth;
                    curX = Mathf.Clamp(curX, 0, texw-1);
                    int curIndex = y * texw + curX;
                    Color curCol = pixels[curIndex];
                    col += curCol * curve4[i];
                }

                pixels[offs] = col;
            }

            offs++;

        }

        offs = 0;
        for (var y = 0; y < texh; y++)
        {
            for (var x = 0; x < texw; x++)
            {
                Color col = Color.black;

                int yBegin = y - netFilterWidth * 3;

                for (int i = 0; i < 7; i++)
                {
                    int curY = yBegin + i * netFilterWidth;

                    curY = Mathf.Clamp(curY, 0, texh-1);
                    int curIndex = curY * texw + x;
                    Color curCol = pixels[curIndex];
                    col += curCol * curve4[i];
                }

                pixels[offs] = col;
            }

            offs++;

        }

        texture.SetPixels(pixels);
        texture.Apply();
    }

    private void DitherTexture(Texture2D texture)
    {
        var texw = texture.width;
        var texh = texture.height;

        var pixels = texture.GetPixels();
        var offs = 0;

        var k1Per15 = 1.0f / 15.0f;
        var k1Per16 = 1.0f / 16.0f;
        var k3Per16 = 3.0f / 16.0f;
        var k5Per16 = 5.0f / 16.0f;
        var k7Per16 = 7.0f / 16.0f;

        for (var y = 0; y < texh; y++)
        {
            for (var x = 0; x < texw; x++)
            {
                float a = pixels[offs].a;
                float r = pixels[offs].r;
                float g = pixels[offs].g;
                float b = pixels[offs].b;

                var a2 = Mathf.Clamp01(Mathf.Floor(a * 16) * k1Per15);
                var r2 = Mathf.Clamp01(Mathf.Floor(r * 16) * k1Per15);
                var g2 = Mathf.Clamp01(Mathf.Floor(g * 16) * k1Per15);
                var b2 = Mathf.Clamp01(Mathf.Floor(b * 16) * k1Per15);

                var ae = a - a2;
                var re = r - r2;
                var ge = g - g2;
                var be = b - b2;

                pixels[offs].a = a2;
                pixels[offs].r = r2;
                pixels[offs].g = g2;
                pixels[offs].b = b2;

                var n1 = offs + 1;   // (x+1,y)
                var n2 = offs + texw - 1; // (x-1 , y+1)
                var n3 = offs + texw;  // (x, y+1)
                var n4 = offs + texw + 1; // (x+1 , y+1)

                if (x < texw - 1)
                {
                    pixels[n1].a += ae * k7Per16;
                    pixels[n1].r += re * k7Per16;
                    pixels[n1].g += ge * k7Per16;
                    pixels[n1].b += be * k7Per16;
                }

                if (y < texh - 1)
                {
                    pixels[n3].a += ae * k5Per16;
                    pixels[n3].r += re * k5Per16;
                    pixels[n3].g += ge * k5Per16;
                    pixels[n3].b += be * k5Per16;

                    if (x > 0)
                    {
                        pixels[n2].a += ae * k3Per16;
                        pixels[n2].r += re * k3Per16;
                        pixels[n2].g += ge * k3Per16;
                        pixels[n2].b += be * k3Per16;
                    }

                    if (x < texw - 1)
                    {
                        pixels[n4].a += ae * k1Per16;
                        pixels[n4].r += re * k1Per16;
                        pixels[n4].g += ge * k1Per16;
                        pixels[n4].b += be * k1Per16;
                    }
                }

                offs++;
            }
        }

        texture.SetPixels(pixels);
        texture.Apply();

        
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值