示例#1
0
        public PackNode Insert(Image img)
        {
            if (!this.IsLeaf)             //we're not a leaf so try inserting into a child
            {
                PackNode newNode = _children[0].Insert(img);
                if (newNode != null)
                {
                    return(newNode);
                }
                // no room in first, insert into second
                return(_children[1].Insert(img));
            }
            //if there's already a lightmap here, return
            if (_imgId >= 0)
            {
                return(null);
            }

            //if we're too small, return
            if (_rct.Width + 1 < img.Size.Width || _rct.Height + 1 < img.Size.Height)
            {
                return(null);
            }

            //if we're just right, accept
            if (_rct.Width + 1 == img.Size.Width && _rct.Height + 1 == img.Size.Height)
            {
                return(this);
            }

            //otherwise, gotta split this node and create some kids
            _children    = new PackNode[2];
            _children[0] = new PackNode();
            _children[1] = new PackNode();

            //decide which way to split
            int dw = this._rct.Width - img.Width;
            int dh = this._rct.Height - img.Height;

            if (dw > dh)
            {
                _children[0]._rct = ERectangle.FromLTRB(_rct.Left, _rct.Top, _rct.Left + img.Width - 1, _rct.Bottom);
                _children[1]._rct = ERectangle.FromLTRB(_rct.Left + img.Width, _rct.Top, _rct.Right, _rct.Bottom);
            }
            else
            {
                _children[0]._rct = ERectangle.FromLTRB(_rct.Left, _rct.Top, _rct.Right, _rct.Top + img.Height - 1);
                _children[1]._rct = ERectangle.FromLTRB(_rct.Left, _rct.Top + img.Height, _rct.Right, _rct.Bottom);
            }

            //insert into first child we created
            return(this._children[0].Insert(img));
        }
示例#2
0
        public static Bitmap TreePack(Size textureSize, Image[] images, out Image[] unpackedImages, out EPoint[] locs)
        {
            Bitmap   bmpTexture = new Bitmap(textureSize.Width, textureSize.Height, PixelFormat.Format32bppArgb);
            Graphics g          = Graphics.FromImage(bmpTexture);

            PackNode root = new PackNode();

            root._rct = new ERectangle(0, 0, textureSize.Width, textureSize.Height);

            locs = new EPoint[images.Length];

            ArrayList unhandled = new ArrayList();

            for (int i = images.Length - 1; i >= 0; i--)
            {
                Image img = images[i];

                PackNode node = root.Insert(img);
                if (node == null)
                {
                    unhandled.Add(img);
                    continue;
                }
                locs[i]     = new EPoint(node._rct.X, node._rct.Y);
                node._imgId = i;
                g.DrawImageUnscaled(img, node._rct.Left, node._rct.Top);
            }
            if (true)
            {
                ArrayList leaves = new ArrayList();
                root.GetUnusedLeaves(leaves);
                Random rnd = new Random();
                foreach (PackNode leaf in leaves)
                {
                    g.FillRectangle(new SolidBrush(Color.FromArgb(rnd.Next(254), rnd.Next(254), rnd.Next(254))), leaf._rct.ToRectangleF());
                }
            }

            unpackedImages = new Image[unhandled.Count];
            for (int i = 0; i < unpackedImages.Length; i++)
            {
                unpackedImages[i] = (Image)unhandled[i];
            }

            return(bmpTexture);
        }
示例#3
0
        public PackNode Insert(Image img)
        {
            if (!this.IsLeaf) //we're not a leaf so try inserting into a child
            {
                PackNode newNode = _children[0].Insert(img);
                if (newNode != null)
                    return newNode;
                // no room in first, insert into second
                return _children[1].Insert(img);
            }
            //if there's already a lightmap here, return
            if (_imgId >= 0)
                return null;

            //if we're too small, return
            if (_rct.Width+1 < img.Size.Width || _rct.Height+1 < img.Size.Height)
                return null;

            //if we're just right, accept
            if (_rct.Width+1 == img.Size.Width && _rct.Height+1 == img.Size.Height)
                return this;

            //otherwise, gotta split this node and create some kids
            _children = new PackNode[2];
            _children[0] = new PackNode();
            _children[1] = new PackNode();

            //decide which way to split
            int dw = this._rct.Width - img.Width;
            int dh = this._rct.Height - img.Height;

            if (dw > dh)
            {
                _children[0]._rct = ERectangle.FromLTRB(_rct.Left, _rct.Top, _rct.Left+img.Width-1, _rct.Bottom);
                _children[1]._rct = ERectangle.FromLTRB(_rct.Left+img.Width, _rct.Top, _rct.Right, _rct.Bottom);
            }
            else
            {
                _children[0]._rct = ERectangle.FromLTRB(_rct.Left, _rct.Top, _rct.Right, _rct.Top+img.Height-1);
                _children[1]._rct = ERectangle.FromLTRB(_rct.Left, _rct.Top+img.Height, _rct.Right, _rct.Bottom);
            }

            //insert into first child we created
            return this._children[0].Insert(img);
        }
示例#4
0
        public static Bitmap TreePack(Size textureSize, Image[] images, out Image[] unpackedImages, out EPoint[] locs)
        {
            Bitmap bmpTexture = new Bitmap(textureSize.Width,textureSize.Height, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(bmpTexture);

            PackNode root = new PackNode();
            root._rct = new ERectangle(0,0,textureSize.Width,textureSize.Height);

            locs = new EPoint[images.Length];

            ArrayList unhandled = new ArrayList();
            for (int i=images.Length-1; i>=0;i--)
            {
                Image img = images[i];

                PackNode node = root.Insert(img);
                if (node == null)
                {
                    unhandled.Add(img);
                    continue;
                }
                locs[i] = new EPoint(node._rct.X, node._rct.Y);
                node._imgId = i;
                g.DrawImageUnscaled(img, node._rct.Left, node._rct.Top);
            }
            if (true)
            {
                ArrayList leaves = new ArrayList();
                root.GetUnusedLeaves(leaves);
                Random rnd = new Random();
                foreach (PackNode leaf in leaves)
                {
                    g.FillRectangle(new SolidBrush(Color.FromArgb(rnd.Next(254), rnd.Next(254), rnd.Next(254))), leaf._rct.ToRectangleF());
                }
            }

            unpackedImages = new Image[unhandled.Count];
            for (int i=0; i<unpackedImages.Length; i++)
                unpackedImages[i] = (Image)unhandled[i];

            return bmpTexture;
        }