Helper for arranging many small sprites into a single larger sheet.
        /// <summary>
        /// Converts an array of sprite filenames into a texture atlas object.
        /// </summary>
        public override TextureAtlasContent Process(string[] input, ContentProcessorContext context)
        {
            logger = context.Logger;
            var textureAtlas = new TextureAtlasContent
            {
                animationFPS = (int)animationFPS
            };
            var sourceSprites = new List <BitmapContent>();
            var imagePaths    = new List <string>();

            // first, we need to sort through and figure out which passed in paths are images and which are folders
            foreach (var inputPath in input)
            {
                // first, the easy one. if it isnt a directory its an image so just add it
                if (!Directory.Exists(inputPath))
                {
                    if (isValidImageFile(inputPath))
                    {
                        imagePaths.Add(inputPath);
                    }
                    continue;
                }

                // we have a directory. we need to recursively add all images in all subfolders
                processDirectory(inputPath, imagePaths, textureAtlas);
            }

            // Loop over each input sprite filename
            foreach (var inputFilename in imagePaths)
            {
                // Store the name of this sprite.
                var spriteName = getSpriteNameFromFilename(inputFilename, input);
                textureAtlas.spriteNames.Add(spriteName, sourceSprites.Count);
                context.Logger.LogMessage("Adding texture: {0}", spriteName);

                // Load the sprite texture into memory.
                var textureReference = new ExternalReference <TextureContent>(inputFilename);
                var texture          = context.BuildAndLoadAsset <TextureContent, TextureContent>(textureReference, "TextureProcessor");

                if (inputFilename.Contains(".9"))
                {
                    logger.LogMessage("\tprocessing nine patch texture");
                    textureAtlas.nineSliceSplits[spriteName] = processNinePatchTexture(texture);
                }

                sourceSprites.Add(texture.Faces[0][0]);
            }

            // Pack all the sprites into a single large texture.
            var packedSprites = TextureAtlasPacker.packSprites(sourceSprites, textureAtlas.spriteRectangles, compressTexture, context);

            textureAtlas.texture.Mipmaps.Add(packedSprites);

            if (compressTexture)
            {
                textureAtlas.texture.ConvertBitmapType(typeof(Dxt5BitmapContent));
            }

            return(textureAtlas);
        }
Esempio n. 2
0
        /// <summary>
        /// Converts an array of sprite filenames into a texture atlas object.
        /// </summary>
        public override TextureAtlasContent Process(string[] input, ContentProcessorContext context)
        {
            Logger = context.Logger;
            var textureAtlas = new TextureAtlasContent
            {
                AnimationFPS = (int)AnimationFPS,
            };
            var sourceSprites = new List <BitmapContent>();
            var imagePaths    = new List <string>();

            // first, we need to sort through and figure out which passed in paths are images and which are folders
            foreach (var inputPath in input)
            {
                // first, the easy one. if it isnt a directory its an image so just add it
                if (!Directory.Exists(inputPath))
                {
                    if (IsValidImageFile(inputPath))
                    {
                        imagePaths.Add(inputPath);
                    }
                    continue;
                }

                // we have a directory. we need to recursively add all images in all subfolders
                ProcessDirectory(inputPath, imagePaths, textureAtlas);
            }

            // Loop over each input sprite filename
            foreach (var inputFilename in imagePaths)
            {
                // Store the name of this sprite.
                var spriteName = GetSpriteNameFromFilename(inputFilename, input);
                textureAtlas.SpriteNames.Add(spriteName, sourceSprites.Count);
                context.Logger.LogMessage("Adding texture: {0}", spriteName);

                // Load the sprite texture into memory.
                var textureReference = new ExternalReference <TextureContent>(inputFilename);
                var texture          =
                    context.BuildAndLoadAsset <TextureContent, TextureContent>(textureReference, "TextureProcessor");

                if (inputFilename.Contains(".9"))
                {
                    Logger.LogMessage("\tprocessing nine patch texture");
                    textureAtlas.NineSliceSplits[spriteName] = ProcessNinePatchTexture(texture);
                }

                // Convert sprite's color key color to transparent
                if (ColorKeyEnabled)
                {
                    var originalType = texture.Faces[0][0].GetType();
                    try
                    {
                        texture.ConvertBitmapType(typeof(PixelBitmapContent <Vector4>));
                    }
                    catch (Exception ex)
                    {
                        context.Logger.LogImportantMessage("Could not convert input texture for processing. " +
                                                           ex.ToString());
                        throw ex;
                    }

                    var bmp = (PixelBitmapContent <Vector4>)texture.Faces[0][0];
                    bmp.ReplaceColor(ColorKeyColor.ToVector4(), Vector4.Zero);
                    texture.Faces[0][0] = bmp;
                    texture.ConvertBitmapType(originalType);
                }

                sourceSprites.Add(texture.Faces[0][0]);
            }

            // Pack all the sprites into a single large texture.
            var packedSprites = TextureAtlasPacker.PackSprites(sourceSprites, textureAtlas.SpriteRectangles,
                                                               CompressTexture, context);

            textureAtlas.Texture.Mipmaps.Add(packedSprites);

            if (CompressTexture)
            {
                textureAtlas.Texture.ConvertBitmapType(typeof(Dxt5BitmapContent));
            }

            return(textureAtlas);
        }