Beispiel #1
0
    void CompressTo16Bit(Texture2D texture, string path)
    {
        var t0     = Time.realtimeSinceStartup;
        var pixels = texture.GetPixels32(0);
        // アルファチャネルを使っているか調べる
        bool hasAlpha = true;

        if (texture.format == TextureFormat.RGB24)
        {
            hasAlpha = false;
        }
        else
        {
            for (int i = 0; i < pixels.Length; i++)
            {
                if (pixels[i].a != 255)
                {
                    hasAlpha = true;
                    break;
                }
            }
        }
        var width  = texture.width;
        var height = texture.height;

        System.Func <Color32, Color32> func = null;
        var format = (TextureFormat)int.MinValue;         // 絶対不正値

        if (hasAlpha)
        {
//			func = ColorReductionUtil.To1111; // デバグ用1bit化
//			func = ColorReductionUtil.To3320; // デバグ用332bit化
            func   = ColorReductionUtil.To4444;
            format = TextureFormat.ARGB4444;
        }
        else
        {
//			func = ColorReductionUtil.To1111; // デバグ用1bit化
//			func = ColorReductionUtil.To3320; // デバグ用332bit化
            func   = ColorReductionUtil.To5650;
            format = TextureFormat.RGB565;
        }
        // 全ミップレベルで回す
        for (int i = 0; i < texture.mipmapCount; i++)
        {
            if (i != 0)             // 一番上はすでに取ってあるので繰り返さない
            {
                pixels = texture.GetPixels32(i);
            }
            ColorReductionUtil.FloydSteinberg(pixels, func, width, height);
            texture.SetPixels32(pixels, i);
            width  = Mathf.Max(1, width / 2);            // 次のサイズへ
            height = Mathf.Max(1, height / 2);           // 次のサイズへ
        }
        EditorUtility.CompressTexture(texture, format, quality: 100);
        var t1 = Time.realtimeSinceStartup;

        Debug.Log("ColorReductionImporter: " + path + " t:" + (t1 - t0));
    }
    void CompressTo4444(Texture2D texture, string path, bool dither)
    {
        var    pixels = texture.GetPixels32();
        var    width  = texture.width;
        var    height = texture.height;
        string postfix;

        if (dither)
        {
            ColorReductionUtil.FloydSteinberg(pixels, ColorReductionUtil.To4444, width, height);
        }
        else
        {
            for (int i = 0; i < pixels.Length; i++)
            {
                pixels[i] = ColorReductionUtil.To4444(pixels[i]);
            }
        }
        texture.SetPixels32(pixels);
        EditorUtility.CompressTexture(texture, TextureFormat.ARGB4444, quality: 100);
    }