Exemple #1
0
        public void TestMaxRectsPackWithoutRotation()
        {
            var maxRectPacker = new MaxRectanglesBinPack();
            maxRectPacker.Initialize(100, 100, false);

            // This data set remain only 1 rectangle that cant be packed
            var elementToPack = new List<AtlasTextureElement>
            {
                CreateElement(null, 80, 100),
                CreateElement(null, 100, 20),
            };

            maxRectPacker.PackRectangles(elementToPack);

            Assert.AreEqual(1, elementToPack.Count);
            Assert.AreEqual(1, maxRectPacker.PackedElements.Count);
        }
Exemple #2
0
        public void TestMaxRectsPackWithRotation()
        {
            var maxRectPacker = new MaxRectanglesBinPack();
            maxRectPacker.Initialize(100, 100, true);

            // This data set remain only 1 rectangle that cant be packed
            var packRectangles = new List<AtlasTextureElement>
            {
                CreateElement("A", 80, 100),
                CreateElement("B", 100, 20),
            };

            maxRectPacker.PackRectangles(packRectangles);

            Assert.AreEqual(0, packRectangles.Count);
            Assert.AreEqual(2, maxRectPacker.PackedElements.Count);
            Assert.IsTrue(maxRectPacker.PackedElements.Find(e => e.Name == "B").DestinationRegion.IsRotated);
        }
        public void TestMaxRectsPackArbitaryRectangles()
        {
            var maxRectPacker = new MaxRectanglesBinPack();
            maxRectPacker.Initialize(100, 100, true);

            // This data set remain only 1 rectangle that cant be packed
            var packRectangles = new List<AtlasTextureElement>
            {
                CreateElement(null, 55, 70),
                CreateElement(null, 55, 30),
                CreateElement(null, 25, 30),
                CreateElement(null, 20, 30),
                CreateElement(null, 45, 30),
                CreateElement(null, 25, 40),
                CreateElement(null, 20, 40),
            };

            maxRectPacker.PackRectangles(packRectangles);

            Assert.AreEqual(1, packRectangles.Count);
            Assert.AreEqual(6, maxRectPacker.PackedElements.Count);
        }
Exemple #4
0
        /// <summary>
        /// Create the best atlas layout possible given the elementsToPack to pack, the algorithm and the atlas maximum size.
        /// Note: when all the elementsToPack cannot fit into the texture, it tries to pack as much as possible of them.
        /// </summary>
        /// <returns>False if</returns>
        private AtlasTextureLayout CreateBestAtlasLayout(List <AtlasTextureElement> elementsToPack, TexturePackingMethod algorithm, out List <AtlasTextureElement> remainingElements)
        {
            remainingElements = elementsToPack;

            var textureAtlas = new AtlasTextureLayout();

            var bestElementPackedCount = int.MaxValue;

            // Generate sub size array
            var subSizeArray = CreateSubSizeArray(atlasMaxSize.X, atlasMaxSize.Y, 512, 512);

            foreach (var subArray in subSizeArray)
            {
                var currentRemaingElements = new List <AtlasTextureElement>(elementsToPack);

                // Reset packer state
                maxRectPacker.Initialize(subArray.Width, subArray.Height, AllowRotation);

                // Pack
                maxRectPacker.PackRectangles(currentRemaingElements, algorithm);

                // Find true size from packed regions
                var packedSize = CalculatePackedRectanglesBound(maxRectPacker.PackedElements);

                // Alter the size of atlas so that it is a power of two
                if (!AllowNonPowerOfTwo)
                {
                    packedSize.Width  = MathUtil.NextPowerOfTwo(packedSize.Width);
                    packedSize.Height = MathUtil.NextPowerOfTwo(packedSize.Height);

                    if (packedSize.Width > subArray.Width || packedSize.Height > subArray.Height)
                    {
                        continue;
                    }
                }

                if (currentRemaingElements.Count >= bestElementPackedCount)
                {
                    continue;
                }

                // Found new best pack, cache it
                bestElementPackedCount = currentRemaingElements.Count;

                // Resize texture atlas
                textureAtlas.Width  = packedSize.Width;
                textureAtlas.Height = packedSize.Height;

                textureAtlas.Textures.Clear();

                // Store all packed regions into Atlas
                foreach (var element in maxRectPacker.PackedElements)
                {
                    textureAtlas.Textures.Add(element.Clone());
                }

                remainingElements = currentRemaingElements;
            }

            return(textureAtlas);
        }