public bool Pack(SGT_PackerOutput po)
        {
            // Fits into an existing node?
            foreach (var node in nodes)
            {
                if (node.Pack(po) == true)
                {
                    return(true);
                }
            }

            // Expand?
            if (size < maxSize)
            {
                var n0 = new SGT_PackerNode(id, 0, size, size * 2, size);
                var n1 = new SGT_PackerNode(id, size, 0, size, size);

                if (n1.Pack(po) == false)
                {
                    return(false);
                }

                nodes.Add(n0);
                nodes.Add(n1);

                size *= 2;

                return(true);
            }
            // Can't expand
            else
            {
                return(false);
            }
        }
Esempio n. 2
0
        public bool Pack(SGT_PackerOutput po)
        {
            // Fits into a child?
            if (nodes != null)
            {
                foreach (var node in nodes)
                {
                    if (node.Pack(po) == true)
                    {
                        return(true);
                    }
                }
            }
            // Fits into this node?
            else if (po.OutputRect.width <= w && po.OutputRect.height <= h)
            {
                if (po.OutputRect.width == w)
                {
                    // Perfect fit?
                    if (po.OutputRect.height == h)
                    {
                        nodes = new SGT_PackerNode[0];
                    }
                    // Just top?
                    else
                    {
                        nodes    = new SGT_PackerNode[1];
                        nodes[0] = new SGT_PackerNode(binId, x, y + (int)po.OutputRect.height, w, h - (int)po.OutputRect.height);
                    }
                }
                // Both?
                else
                {
                    nodes    = new SGT_PackerNode[2];
                    nodes[0] = new SGT_PackerNode(binId, x, y + (int)po.OutputRect.height, w, h - (int)po.OutputRect.height);
                    nodes[1] = new SGT_PackerNode(binId, x + (int)po.OutputRect.width, y, w - (int)po.OutputRect.width, (int)po.OutputRect.height);
                }

                po.OutputTextureId = binId;
                po.OutputRect      = new Rect(x + po.OutputRect.x, y + po.OutputRect.y, po.InputRectTrimmed.width, po.InputRectTrimmed.height);

                return(true);
            }

            return(false);
        }