Beispiel #1
0
        public void TestCreateTextureAtlasToOutput()
        {
            var textureElements = new List<AtlasTextureElement>
            {
                CreateElement("MediumPurple", 130, 158, 10, TextureAddressMode.Border, Color.MediumPurple, Color.SteelBlue),
                CreateElement("Red", 127, 248, 10, TextureAddressMode.Border, Color.Red, Color.SteelBlue),
                CreateElement("Blue", 212, 153, 10, TextureAddressMode.Border, Color.Blue, Color.SteelBlue),
                CreateElement("Gold", 78, 100, 10, TextureAddressMode.Border, Color.Gold, Color.SteelBlue),
                CreateElement("RosyBrown", 78, 100, 10, TextureAddressMode.Border, Color.RosyBrown, Color.SteelBlue),
                CreateElement("SaddleBrown", 400, 100, 10, TextureAddressMode.Border, Color.SaddleBrown, Color.SteelBlue),
                CreateElement("Salmon", 400, 200, 10, TextureAddressMode.Border, Color.Salmon, Color.SteelBlue),
                CreateElement("PowderBlue", 190, 200, 10, TextureAddressMode.Border, Color.PowderBlue, Color.SteelBlue),
                CreateElement("Orange", 200, 230, 10, TextureAddressMode.Border, Color.Orange, Color.SteelBlue),
                CreateElement("Silver", 100, 170, 10, TextureAddressMode.Border, Color.Silver, Color.SteelBlue),
                CreateElement("SlateGray", 100, 170, 10, TextureAddressMode.Border, Color.SlateGray, Color.SteelBlue),
                CreateElement("Tan", 140, 110, 10, TextureAddressMode.Border, Color.Tan, Color.SteelBlue),
            };

            var texturePacker = new TexturePacker
            {
                AllowMultipack = false,
                AllowRotation = true,
                MaxHeight = 1024,
                MaxWidth = 1024
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.IsTrue(canPackAllTextures);

            // Obtain texture atlases
            var textureAtlases = texturePacker.AtlasTextureLayouts;

            Assert.AreEqual(1, textureAtlases.Count);

            if (!texturePacker.AllowNonPowerOfTwo)
            {
                Assert.IsTrue(MathUtil.IsPow2(textureAtlases[0].Width));
                Assert.IsTrue(MathUtil.IsPow2(textureAtlases[0].Height));
            }

            // Create atlas texture
            var atlasTexture = AtlasTextureFactory.CreateTextureAtlas(textureAtlases[0], false);
            SaveAndCompareTexture(atlasTexture, "TestCreateTextureAtlasToOutput");

            Assert.AreEqual(textureAtlases[0].Width, atlasTexture.Description.Width);
            Assert.AreEqual(textureAtlases[0].Height, atlasTexture.Description.Height);

            atlasTexture.Dispose();

            foreach (var texture in textureAtlases.SelectMany(textureAtlas => textureAtlas.Textures))
            {
                texture.Texture.Dispose();
            }
        }
Beispiel #2
0
        public void TestLoadImagesToCreateAtlas()
        {
            var textureElements = new List<AtlasTextureElement>();

            for (var i = 0; i < 8; ++i)
                textureElements.Add(CreateElementFromFile("image" + i, 100, TextureAddressMode.Wrap, TextureAddressMode.Border));

            for (var i = 0; i < 8; ++i)
                textureElements.Add(CreateElementFromFile("image" + i, 100, TextureAddressMode.Mirror, TextureAddressMode.Clamp));

            var texturePacker = new TexturePacker
            {
                AllowMultipack = false,
                AllowRotation = false,
                MaxHeight = 2048,
                MaxWidth = 2048
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.IsTrue(canPackAllTextures);

            // Obtain texture atlases
            var textureAtlases = texturePacker.AtlasTextureLayouts;

            // Create atlas texture
            var atlasTexture = AtlasTextureFactory.CreateTextureAtlas(textureAtlases[0], false);

            SaveAndCompareTexture(atlasTexture, "TestLoadImagesToCreateAtlas", ImageFileType.Dds);
            atlasTexture.Dispose();

            foreach (var texture in textureAtlases.SelectMany(textureAtlas => textureAtlas.Textures))
                texture.Texture.Dispose();
        }
Beispiel #3
0
        public void TestNullSizeTexture()
        {
            var textureElements = new List<AtlasTextureElement> { CreateElement("A", 0, 0) };

            var texturePacker = new TexturePacker
            {
                AllowMultipack = false,
                AllowRotation = true,
                MaxHeight = 2000,
                MaxWidth = 2000
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.IsTrue(canPackAllTextures);

            // Obtain texture atlases
            var textureAtlases = texturePacker.AtlasTextureLayouts;

            Assert.AreEqual(0, textureAtlases.Count);
        }
Beispiel #4
0
        public void TestNullSizeElements()
        {
            var textureElements = new List<AtlasTextureElement>
            {
                CreateElement("A", 10, 10, 5),
                CreateElement("B", 11, 0, 6),
                CreateElement("C", 12, 13, 7),
                CreateElement("D", 0, 14, 8),
                CreateElement("E", 14, 15, 9),
                CreateElement("F", 0, 0, 10),
                CreateElement("G", 16, 17, 11),
            };

            var texturePacker = new TexturePacker
            {
                AllowMultipack = false,
                AllowRotation = true,
                MaxHeight = 2000,
                MaxWidth = 2000
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.IsTrue(canPackAllTextures);

            // Obtain texture atlases
            var textureAtlases = texturePacker.AtlasTextureLayouts;

            Assert.AreEqual(1, textureAtlases.Count);
            Assert.AreEqual(4, textureAtlases[0].Textures.Count);
            Assert.IsNull(textureAtlases[0].Textures.Find(e => e.Name == "B"));
            Assert.IsNull(textureAtlases[0].Textures.Find(e => e.Name == "D"));
            Assert.IsNull(textureAtlases[0].Textures.Find(e => e.Name == "F"));
            Assert.IsNotNull(textureAtlases[0].Textures.Find(e => e.Name == "A"));
            Assert.IsNotNull(textureAtlases[0].Textures.Find(e => e.Name == "C"));
            Assert.IsNotNull(textureAtlases[0].Textures.Find(e => e.Name == "E"));
            Assert.IsNotNull(textureAtlases[0].Textures.Find(e => e.Name == "G"));
        }
Beispiel #5
0
        public void TestRegionOutOfTexture()
        {
            var textureElements = new List<AtlasTextureElement>
            {
                CreateElementFromFile("image9", 10, TextureAddressMode.Mirror, TextureAddressMode.Clamp, new RotableRectangle(-100, 30, 400, 250)),
                CreateElementFromFile("image10", 10, TextureAddressMode.Wrap, TextureAddressMode.Border, new RotableRectangle(-50, -30, 300, 400, true)),
            };

            var texturePacker = new TexturePacker
            {
                AllowMultipack = false,
                AllowRotation = true,
                AllowNonPowerOfTwo = true,
                MaxWidth = 1024,
                MaxHeight = 1024,
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.IsTrue(canPackAllTextures);

            // Obtain texture atlases
            var textureAtlases = texturePacker.AtlasTextureLayouts;

            Assert.AreEqual(1, textureAtlases.Count);

            // Create atlas texture
            var atlasTexture = AtlasTextureFactory.CreateTextureAtlas(textureAtlases[0], false);

            Assert.AreEqual(textureAtlases[0].Width, atlasTexture.Description.Width);
            Assert.AreEqual(textureAtlases[0].Height, atlasTexture.Description.Height);

            SaveAndCompareTexture(atlasTexture, "TestRegionOutOfTexture");

            textureElements[0].Texture.Dispose();
            atlasTexture.Dispose();
        }
Beispiel #6
0
        public void TestTextureAtlasFactoryImageParts()
        {
            var textureElements = new List<AtlasTextureElement>
            {
                CreateElementFromFile("imagePart0", 26, TextureAddressMode.Border, TextureAddressMode.Mirror, new RotableRectangle(0, 0, 128, 128)),
                CreateElementFromFile("imagePart0", 26, TextureAddressMode.Clamp, TextureAddressMode.Clamp, new RotableRectangle(128, 128, 128, 128)),
                CreateElementFromFile("imagePart0", 26, TextureAddressMode.MirrorOnce, TextureAddressMode.Wrap, new RotableRectangle(128, 0, 128, 128)),
                CreateElementFromFile("imagePart1", 26, TextureAddressMode.Clamp, TextureAddressMode.Mirror, new RotableRectangle(376, 0, 127, 256)),
                CreateElementFromFile("imagePart1", 26, TextureAddressMode.Mirror, TextureAddressMode.Clamp, new RotableRectangle(10, 10, 254, 127)),
                CreateElement("empty", 0, 0, 26),
                CreateElementFromFile("imagePart2", 26, TextureAddressMode.Clamp, TextureAddressMode.Clamp, new RotableRectangle(0, 0, 128, 64)),
            };

            var texturePacker = new TexturePacker
            {
                AllowMultipack = false,
                AllowRotation = true,
                MaxWidth = 2048,
                MaxHeight = 2048,
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.IsTrue(canPackAllTextures);

            // Obtain texture atlases
            var textureAtlases = texturePacker.AtlasTextureLayouts;

            Assert.AreEqual(1, textureAtlases.Count);
            Assert.AreEqual(texturePacker.MaxWidth/2, textureAtlases[0].Width);
            Assert.AreEqual(texturePacker.MaxHeight/4, textureAtlases[0].Height);

            // Create atlas texture
            var atlasTexture = AtlasTextureFactory.CreateTextureAtlas(textureAtlases[0], false);

            Assert.AreEqual(textureAtlases[0].Width, atlasTexture.Description.Width);
            Assert.AreEqual(textureAtlases[0].Height, atlasTexture.Description.Height);

            SaveAndCompareTexture(atlasTexture, "TestTextureAtlasFactoryImageParts");

            textureElements[0].Texture.Dispose();
            atlasTexture.Dispose();
        }
Beispiel #7
0
        public void TestTextureAtlasFactory()
        {
            var textureElements = new List<AtlasTextureElement>
            {
                CreateElement("A", 100, 200, 0, Color.MediumPurple),
            };

            var texturePacker = new TexturePacker
            {
                AllowMultipack = false,
                AllowRotation = true,
                MaxHeight = 2000,
                MaxWidth = 2000
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.IsTrue(canPackAllTextures);

            // Obtain texture atlases
            var textureAtlases = texturePacker.AtlasTextureLayouts;

            Assert.AreEqual(1, textureAtlases.Count);
            Assert.IsTrue(MathUtil.IsPow2(textureAtlases[0].Width));
            Assert.IsTrue(MathUtil.IsPow2(textureAtlases[0].Height));

            // Create atlas texture
            var atlasTexture = AtlasTextureFactory.CreateTextureAtlas(textureAtlases[0], false);

            Assert.AreEqual(textureAtlases[0].Width, atlasTexture.Description.Width);
            Assert.AreEqual(textureAtlases[0].Height, atlasTexture.Description.Height);

            SaveAndCompareTexture(atlasTexture, "TestTextureAtlasFactory");

            textureElements[0].Texture.Dispose();
            atlasTexture.Dispose();
        }
Beispiel #8
0
        public void TestTextureAtlasFactoryRotation2()
        {
            var textureElements = new List<AtlasTextureElement>
            {
                CreateElementFromFile("image9", 25, TextureAddressMode.Clamp, TextureAddressMode.Clamp),
                CreateElementFromFile("image10", 25, TextureAddressMode.Mirror, TextureAddressMode.Mirror),
            };

            var texturePacker = new TexturePacker
            {
                AllowMultipack = false,
                AllowRotation = true,
                AllowNonPowerOfTwo = true,
                MaxWidth = 356,
                MaxHeight = 306,
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.IsTrue(canPackAllTextures);

            // Obtain texture atlases
            var textureAtlases = texturePacker.AtlasTextureLayouts;

            Assert.AreEqual(1, textureAtlases.Count);
            Assert.AreEqual(texturePacker.MaxWidth, textureAtlases[0].Width);
            Assert.AreEqual(texturePacker.MaxHeight, textureAtlases[0].Height);

            // Create atlas texture
            var atlasTexture = AtlasTextureFactory.CreateTextureAtlas(textureAtlases[0], false);

            Assert.AreEqual(textureAtlases[0].Width, atlasTexture.Description.Width);
            Assert.AreEqual(textureAtlases[0].Height, atlasTexture.Description.Height);

            SaveAndCompareTexture(atlasTexture, "TestTextureAtlasFactoryRotation2");

            textureElements[0].Texture.Dispose();
            atlasTexture.Dispose();
        }
Beispiel #9
0
        public void TestTexturePackerWithBorder()
        {
            var textureAtlases = new List<AtlasTextureLayout>();

            var textureElements = new List<AtlasTextureElement>
            {
                CreateElement("A", 100, 200, 2),
                CreateElement("B", 57, 22, 2),
            };

            var texturePacker = new TexturePacker
            {
                AllowMultipack = true,
                AllowRotation = true,
                MaxHeight = 512,
                MaxWidth = 512
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);
            textureAtlases.AddRange(texturePacker.AtlasTextureLayouts);

            Assert.IsTrue(canPackAllTextures);
            Assert.AreEqual(2, textureElements.Count);
            Assert.AreEqual(1, textureAtlases.Count);

            Assert.IsTrue(MathUtil.IsPow2(textureAtlases[0].Width));
            Assert.IsTrue(MathUtil.IsPow2(textureAtlases[0].Height));

            // Test if border is applied in width and height
            var textureA = textureAtlases[0].Textures.Find(rectangle => rectangle.Name == "A");
            var textureB = textureAtlases[0].Textures.Find(rectangle => rectangle.Name == "B");

            Assert.AreEqual(textureA.SourceRegion.Width + 2 * textureA.BorderSize, textureA.DestinationRegion.Width);
            Assert.AreEqual(textureA.SourceRegion.Height + 2 * textureA.BorderSize, textureA.DestinationRegion.Height);

            Assert.AreEqual(textureB.SourceRegion.Width + 2 * textureB.BorderSize,
                (!textureB.DestinationRegion.IsRotated) ? textureB.DestinationRegion.Width : textureB.DestinationRegion.Height);
            Assert.AreEqual(textureB.SourceRegion.Height + 2 * textureB.BorderSize,
                (!textureB.DestinationRegion.IsRotated) ? textureB.DestinationRegion.Height : textureB.DestinationRegion.Width);
        }
Beispiel #10
0
        public void TestTexturePackerWithMultiPack()
        {
            var textureElements = CreateFakeTextureElements();

            var texturePacker = new TexturePacker
            {
                AllowMultipack = true,
                AllowRotation = true,
                MaxHeight = 300,
                MaxWidth = 300,
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.AreEqual(2, textureElements.Count);
            Assert.AreEqual(0, texturePacker.AtlasTextureLayouts.Count);
            Assert.IsFalse(canPackAllTextures);

            texturePacker.Reset();
            texturePacker.MaxWidth = 1500;
            texturePacker.MaxHeight = 800;

            canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.IsTrue(canPackAllTextures);
            Assert.AreEqual(1, texturePacker.AtlasTextureLayouts.Count);
            Assert.AreEqual(textureElements.Count, texturePacker.AtlasTextureLayouts[0].Textures.Count);

            Assert.IsTrue(MathUtil.IsPow2(texturePacker.AtlasTextureLayouts[0].Width));
            Assert.IsTrue(MathUtil.IsPow2(texturePacker.AtlasTextureLayouts[0].Height));
        }
Beispiel #11
0
        public void TestRotationElement2()
        {
            var textureElements = new List<AtlasTextureElement>
            {
                CreateElementFromFile("imageRotated1", 0, TextureAddressMode.Clamp, TextureAddressMode.Clamp)
            };

            var texturePacker = new TexturePacker
            {
                AllowMultipack = true,
                AllowRotation = true,
                MaxWidth = 256,
                MaxHeight = 128,
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.IsTrue(canPackAllTextures);
            Assert.AreEqual(1, texturePacker.AtlasTextureLayouts.Count);

            var atlasTexture = AtlasTextureFactory.CreateTextureAtlas(texturePacker.AtlasTextureLayouts[0], false);

            SaveAndCompareTexture(atlasTexture, "TestRotationElement2");
        }
Beispiel #12
0
        public void TestTexturePackerEmptyList()
        {
            var textureElements = new List<AtlasTextureElement>();

            var texturePacker = new TexturePacker
            {
                AllowMultipack = true,
                AllowRotation = true,
                MaxHeight = 300,
                MaxWidth = 300,
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.AreEqual(0, textureElements.Count);
            Assert.AreEqual(0, texturePacker.AtlasTextureLayouts.Count);
            Assert.IsTrue(canPackAllTextures);
        }
Beispiel #13
0
        public void TestTexturePackerFitAllElements()
        {
            var textureElements = CreateFakeTextureElements();

            var texturePacker = new TexturePacker
            {
                AllowMultipack = false,
                AllowRotation = true,
                AllowNonPowerOfTwo = true,
                MaxHeight = 2000,
                MaxWidth = 2000
            };

            var canPackAllTextures = texturePacker.PackTextures(textureElements);

            Assert.IsTrue(canPackAllTextures);
        }