Ejemplo n.º 1
0
        static OperationResult GenerateAtlasProcessTexture(TextureData current, ref GenerateAtlasData data)
        {
            if (data.CurrentRowYEnd != -1 && data.CurrentDim.y != current.TileDimension().y)
            {
                data.X  = 0;
                data.Y += (data.CurrentDim.y * (2 * k_repeatedPixels + data.TileWidthHeight));
                data.CurrentRowYEnd = -1;
            }

            if (data.CurrentRowYEnd == -1)
            {
                data.CurrentDim     = current.TileDimension();
                data.CurrentRowYEnd = data.Y + (data.CurrentDim.y * (2 * k_repeatedPixels + data.TileWidthHeight));

                if (data.CurrentRowYEnd > data.AtlasSize.y)
                {
                    Debug.LogError("Atlas cannot be created because you run out of space!");
                    return(OperationResult.Error);
                }
            }

            data.CurrentDim.x = current.TileDimension().x;

            // copy texture
            var tex = current.GetTexture(data.AtlasType);

            if (tex != null)
            {
                CopyTextureToAtlas(tex, data.OutputTexture, data.X, data.Y);
            }

            var uVOffset = new Vector2((float)data.X / data.AtlasSize.x, (float)data.Y / data.AtlasSize.y);

            if (data.AtlasType == EAtlasType.Default)
            {
                current.UVOffset = uVOffset;
            }

            data.X += data.CurrentDim.x * (2 * k_repeatedPixels + data.TileWidthHeight);

            var nextXEnd = data.X + (data.CurrentDim.x * (2 * k_repeatedPixels + data.TileWidthHeight));

            if (nextXEnd <= data.AtlasSize.x)
            {
                return(OperationResult.OK);
            }

            data.X = 0;
            data.Y = data.CurrentRowYEnd;
            data.CurrentRowYEnd = -1;

            return(OperationResult.OK);
        }
Ejemplo n.º 2
0
        // ReSharper disable once FlagArgument
        static Texture2D GenerateAtlas(LevelTextureConfig config, EAtlasType atlasType)
        {
            var textureCollections = config.LevelTextureCollections;
            var tileWidthAndHeight = 2 * config.HalfTileDimension;
            var atlasSize          = new Vector2Int(config.AtlasDimension, config.AtlasDimension);

            //int widthAndHeight = texWidthAndHeight + (repeatedPixels * 2);
            var outputTexture = new Texture2D(config.AtlasDimension, config.AtlasDimension, TextureFormat.ARGB32, false);

            config.UVInset    = (float)k_repeatedPixels / config.AtlasDimension;
            config.UVTileSize = (float)tileWidthAndHeight / atlasSize.x;

            var allTexData = GatherAllTextures(textureCollections, config);

            var data = new GenerateAtlasData()
            {
                AtlasType       = atlasType,
                OutputTexture   = outputTexture,
                CurrentDim      = Vector2Int.zero,
                AtlasSize       = atlasSize,
                TileWidthHeight = tileWidthAndHeight,
                CurrentRowYEnd  = -1,
                X = 0,
                Y = 0
            };
            //currentRowYStart = 0,
            //blockXStart = 0,
            //blockXEnd = -1,
            //blockX = 0,
            //blockY = 0;

            var progressIndex = 0;
            var initialCount  = allTexData.Count;

            while (allTexData.Count > 0)
            {
                EditorUtility.DisplayProgressBar("Generating Atlas", $"{progressIndex}/{initialCount}", (float)(progressIndex++) / initialCount);
                var current = allTexData[0];

                if (GenerateAtlasProcessTexture(current, ref data) == OperationResult.Error)
                {
                    break;
                }
                allTexData.RemoveAt(0);
            }

            EditorUtility.ClearProgressBar();
            return(outputTexture);
        }