Esempio n. 1
0
        public bool Insert(ImagePacker.ImagePackRect insert, int padding)
        {
            int insertWidth  = insert.rect.width + padding * 2;
            int insertHeight = insert.rect.height + padding * 2;

            if (insertWidth > rect.width || insertHeight > rect.height)
            {
                return(false);
            }

            if (left == null)
            {
                left       = new ImagePackNode();
                left.rect  = new RectInt(rect.x, rect.y, rect.width, insertHeight);
                right      = new ImagePackNode();
                right.rect = new RectInt(rect.x, rect.y + insertHeight, rect.width, rect.height - insertHeight);
            }

            if (!left.InsertNotRoot(insert, padding))
            {
                return(right.InsertNotRoot(insert, padding));
            }

            return(true);
        }
        static bool SplitRects(ImagePackNode node, ImagePacker.ImagePackRect insert, int padding, out ImagePackNode left, out ImagePackNode right)
        {
            // Find the best way to split the rect based on a new rect
            left = right = null;
            var tryRects = new[]
            {
                new ImagePackNode(), new ImagePackNode(),
                new ImagePackNode(), new ImagePackNode()
            };

            tryRects[0].rect = new RectInt(node.rect.x + node.imageWidth.x, node.rect.y, node.rect.width - node.imageWidth.x, node.rect.height);
            tryRects[1].rect = new RectInt(node.rect.x, node.rect.y + node.imageWidth.y, node.imageWidth.x, node.rect.height - node.imageWidth.y);
            tryRects[2].rect = new RectInt(node.rect.x, node.rect.y + node.imageWidth.y, node.rect.width, node.rect.height - node.imageWidth.y);
            tryRects[3].rect = new RectInt(node.rect.x + node.imageWidth.x, node.rect.y, node.rect.width - node.imageWidth.x, node.imageWidth.y);
            float smallestSpace = float.MinValue;

            for (int i = 0; i < tryRects.GetLength(0); ++i)
            {
                //for (int j = 0; j < tryRects.GetLength(1); ++j)
                {
                    Vector2Int newSpaceLeft;
                    if (tryRects[i].TryInsert(insert, padding, out newSpaceLeft))
                    {
                        if (smallestSpace < newSpaceLeft.sqrMagnitude)
                        {
                            smallestSpace = newSpaceLeft.sqrMagnitude;
                            int index = i / 2 * 2;
                            left  = tryRects[index];
                            right = tryRects[index + 1];
                        }
                    }
                }
            }
            return(left != null);
        }
        public bool Insert(ImagePacker.ImagePackRect insert, int padding)
        {
            int insertWidth  = insert.rect.width + padding * 2;
            int insertHeight = insert.rect.height + padding * 2;

            if (insertWidth > rect.width || insertHeight > rect.height)
            {
                return(false);
            }

            if (imageId == -1)
            {
                imageId    = insert.index;
                imageWidth = new Vector2Int(insertWidth, insertHeight);
            }
            else
            {
                if (left == null && !SplitRects(this, insert, padding, out left, out right))
                {
                    return(false);
                }
                // We assign to the node that has a better fit for the image
                Vector2Int spaceLeft, spaceRight;
                bool       insertLeft, insertRight;
                insertLeft  = left.TryInsert(insert, padding, out spaceLeft);
                insertRight = right.TryInsert(insert, padding, out spaceRight);
                if (insertLeft && insertRight)
                {
                    if (spaceLeft.sqrMagnitude < spaceRight.sqrMagnitude)
                    {
                        left.Insert(insert, padding);
                    }
                    else
                    {
                        right.Insert(insert, padding);
                    }
                }
                else if (insertLeft)
                {
                    left.Insert(insert, padding);
                }
                else if (insertRight)
                {
                    right.Insert(insert, padding);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
        public bool TryInsert(ImagePacker.ImagePackRect insert, int padding, out Vector2Int remainingSpace)
        {
            remainingSpace = Vector2Int.zero;
            int insertWidth  = insert.rect.width + padding * 2;
            int insertHeight = insert.rect.height + padding * 2;

            if (insertWidth > rect.width || insertHeight > rect.height)
            {
                return(false);
            }

            if (imageId == -1)
            {
                remainingSpace.x = rect.width - insertWidth;
                remainingSpace.y = rect.height - insertHeight;
            }
            else
            {
                Vector2Int    spaceLeft, spaceRight;
                bool          insertLeft, insertRight;
                ImagePackNode tryLeft, tryRight;
                tryLeft  = left;
                tryRight = right;
                if (left == null && !SplitRects(this, insert, padding, out tryLeft, out tryRight))
                {
                    return(false);
                }

                insertLeft  = tryLeft.TryInsert(insert, padding, out spaceLeft);
                insertRight = tryRight.TryInsert(insert, padding, out spaceRight);
                if (insertLeft && insertRight)
                {
                    remainingSpace = spaceLeft.sqrMagnitude < spaceRight.sqrMagnitude ? spaceLeft : spaceRight;
                }
                else if (insertLeft)
                {
                    remainingSpace = spaceLeft;
                }
                else if (insertRight)
                {
                    remainingSpace = spaceRight;
                }
                else
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 5
0
        void Pack()
        {
            int count = m_PackStep > 0 && m_PackStep < m_PackRects.Count ? m_PackStep : m_PackRects.Count;

            m_PackingRect = new ImagePacker.ImagePackRect[m_PackRects.Count];
            for (int i = 0; i < m_PackRects.Count; ++i)
            {
                m_PackingRect[i] = new ImagePacker.ImagePackRect()
                {
                    rect  = m_PackRects[i],
                    index = i
                };
            }
            Array.Sort(m_PackingRect);
            ImagePacker.Pack(m_PackingRect.Take(count).Select(x => x.rect).ToArray(), m_Padding, out m_PackResult, out m_PackWidth, out m_PackHeight);
        }
Esempio n. 6
0
        bool InsertNotRoot(ImagePacker.ImagePackRect insert, int padding)
        {
            int insertWidth  = insert.rect.width + padding * 2;
            int insertHeight = insert.rect.height + padding * 2;

            if (insertWidth > rect.width || insertHeight > rect.height)
            {
                return(false);
            }

            if (imageId == -1)
            {
                imageId = insert.index;
                var dw = rect.width - insertWidth;
                var dh = rect.height - insertHeight;
                left  = new ImagePackNode();
                right = new ImagePackNode();
                if (dw > dh)
                {
                    left.rect  = new RectInt(rect.x + insertWidth, rect.y, rect.width - insertWidth, rect.height);
                    right.rect = new RectInt(rect.x, rect.y + insertHeight, insertWidth, rect.height - insertHeight);
                }
                else
                {
                    left.rect  = new RectInt(rect.x, rect.y + insertHeight, rect.width, rect.height - insertHeight);
                    right.rect = new RectInt(rect.x + insertWidth, rect.y, rect.width - insertWidth, insertHeight);
                }
            }
            else
            {
                if (!left.InsertNotRoot(insert, padding))
                {
                    return(right.InsertNotRoot(insert, padding));
                }
            }
            return(true);
        }