Beispiel #1
0
        static ImagePackNode InternalPack(RectInt[] rects, int padding)
        {
            if (rects == null || rects.Length == 0)
            {
                return new ImagePackNode()
                       {
                           rect = new RectInt(0, 0, 0, 0)
                       }
            }
            ;
            var sortedRects = new ImagePackRect[rects.Length];

            for (int i = 0; i < rects.Length; ++i)
            {
                sortedRects[i]       = new ImagePackRect();
                sortedRects[i].rect  = rects[i];
                sortedRects[i].index = i;
            }
            Array.Sort <ImagePackRect>(sortedRects);
            var root = new ImagePackNode();

            root.rect = new RectInt(0, 0, (int)NextPowerOfTwo((ulong)rects[0].width), (int)NextPowerOfTwo((ulong)rects[0].height));

            for (int i = 0; i < rects.Length; ++i)
            {
                if (!root.Insert(sortedRects[i], padding)) // we can't fit
                {
                    int newWidth = root.rect.width, newHeight = root.rect.height;
                    if (root.rect.width < root.rect.height)
                    {
                        newWidth = (int)NextPowerOfTwo((ulong)root.rect.width + 1);
                    }
                    else
                    {
                        newHeight = (int)NextPowerOfTwo((ulong)root.rect.height + 1);
                    }
                    // Reset all packing and try again
                    root      = new ImagePackNode();
                    root.rect = new RectInt(0, 0, newWidth, newHeight);
                    i         = -1;
                }
            }
            return(root);
        }
        static ImagePackNode InternalPack(RectInt[] rects, int padding)
        {
            var sortedRects = new ImagePackRect[rects.Length];

            for (int i = 0; i < rects.Length; ++i)
            {
                sortedRects[i]       = new ImagePackRect();
                sortedRects[i].rect  = rects[i];
                sortedRects[i].index = i;
            }
            Array.Sort <ImagePackRect>(sortedRects);
            var root = new ImagePackNode();

            root.rect = new RectInt(0, 0, 32, 32);

            for (int i = 0; i < rects.Length; ++i)
            {
                if (!root.Insert(sortedRects[i], padding)) // we can't fit
                {
                    int newWidth = 0, newHeight = 0;
                    if (root.rect.width < root.rect.height)
                    {
                        newWidth = (int)NextPowerOfTwo((ulong)root.rect.width + 1);
                    }
                    else
                    {
                        newHeight = (int)NextPowerOfTwo((ulong)root.rect.height + 1);
                    }
                    if (newWidth > k_MaxTextureSize || newHeight > k_MaxTextureSize)
                    {
                        Debug.LogAssertion("unable to pack, reached max texture size");
                        break;
                    }

                    root.AdjustSize(newWidth, newHeight);
                    --i;
                }
            }
            return(root);
        }