예제 #1
0
        /// <summary>
        /// Gets the pixel regions of <paramref name="atlas"/> and sorts them using <paramref name="sortType"/>.
        /// </summary>
        /// <param name="atlas">The <see cref="dfAtlas"/> to get the pixel regions from.</param>
        /// <param name="sortType">The <see cref="AtlasItemSortType"/> used to sort the list of pixel regions.</param>
        /// <returns>A list with all pixel regions in <paramref name="atlas"/> that is sorted using <paramref name="sortType"/></returns>
        public static List <RectInt> GetSortedPixelRegions(this dfAtlas atlas, AtlasItemSortType sortType)
        {
            List <RectInt> rawList    = atlas.GetPixelRegions();
            List <RectInt> sortedList = new List <RectInt>();

            while (sortedList.Count < rawList.Count)
            {
                RectInt rint             = new RectInt(0, 0, 0, 0);
                bool    rintIsUnassigned = true;
                for (int i = 0; i < rawList.Count; i++)
                {
                    if (!sortedList.Contains(rawList[i]))
                    {
                        if (rintIsUnassigned)
                        {
                            rint             = rawList[i];
                            rintIsUnassigned = false;
                        }
                        else
                        {
                            rint = ((rint.GetPointInRint(sortType).x >= rawList[i].GetPointInRint(sortType).x&& rint.GetPointInRint(sortType).y >= rawList[i].GetPointInRint(sortType).y) ? rawList[i] : rint);
                        }
                    }
                }
                sortedList.Add(rint);
            }
            return(sortedList);
        }
        public static void DumpdfAtlas(dfAtlas atlas)
        {
            string collectionName = atlas.name;

            string    defName;
            Texture2D texture, output;
            int       width, height, minX, minY, maxX, maxY, w, h;

            Color[] pixels;


            var itemSizes = atlas.GetPixelRegions();

            for (int i = 0; i < itemSizes.Count; i++)
            {
                var def = atlas.Items[i];
                if (def == null)
                {
                    continue;
                }


                defName = string.IsNullOrEmpty(def.name) ? collectionName + "_" + i : def.name;


                texture = (Texture2D)atlas.Texture.GetReadable();
                width   = texture.width;
                height  = texture.height;



                minX = itemSizes[i].xMin;
                minY = itemSizes[i].yMin;
                maxX = itemSizes[i].xMax;
                maxY = itemSizes[i].yMax;

                w = maxX - minX;
                h = maxY - minY;


                if (w <= 0 || h <= 0)
                {
                    ToolsCharApi.PrintError <string>($"[{defName}]: is to small. minX: {minX}, minY: {minY}, maxX: {maxX}, maxY: {maxY}");
                    //ToolsCharApi.ExportTexture(new Texture2D(1, 1) { name = defName });
                    continue;
                }
                ;

                pixels = texture.GetPixels(minX, minY, w, h);

                output = new Texture2D(w, h);
                output.SetPixels(pixels);
                output.Apply();
                output.name = def.name;
                //BotsModule.Log(output.name, BotsModule.TEXT_COLOR);
                ToolsCharApi.ExportTexture(output, "SpriteDump/df/" + collectionName);
            }
        }
예제 #3
0
        /// <summary>
        /// Gets the first empty space in <paramref name="atlas"/> that has at least the size of <paramref name="pixelScale"/>.
        /// </summary>
        /// <param name="atlas">The <see cref="dfAtlas"/> to find the empty space in.</param>
        /// <param name="pixelScale">The required size of the empty space.</param>
        /// <returns>The rect of the empty space divided by the atlas texture's size.</returns>
        public static Rect FindFirstValidEmptySpace(this dfAtlas atlas, IntVector2 pixelScale)
        {
            if (atlas == null || atlas.Texture == null || !atlas.Texture.IsReadable())
            {
                return(new Rect(0f, 0f, 0f, 0f));
            }
            Vector2Int     point      = new Vector2Int(0, 0);
            int            pointIndex = -1;
            List <RectInt> rects      = atlas.GetPixelRegions();

            //float x = 0;
            //float y = 0;


            while (true)
            {
                bool shouldContinue = false;
                foreach (RectInt rint in rects)
                {
                    if (rint.DoseOverlap(new RectInt(point, pixelScale.ToVector2Int())))
                    {
                        shouldContinue = true;
                        pointIndex++;
                        if (pointIndex >= rects.Count)
                        {
                            return(new Rect(0f, 0f, 0f, 0f));
                        }
                        point = rects[pointIndex].max + Vector2Int.one;
                        if (point.x > atlas.Texture.width || point.y > atlas.Texture.height)
                        {
                            atlas.ResizeAtlas(new IntVector2(atlas.Texture.width * 2, atlas.Texture.height * 2));
                        }
                        break;
                    }
                    bool shouldBreak = false;
                    foreach (RectInt rint2 in rects)
                    {
                        RectInt currentRect = new RectInt(point, pixelScale.ToVector2Int());
                        if (rint2.x < currentRect.x || rint2.y < currentRect.y)
                        {
                            continue;
                        }
                        else
                        {
                            if (currentRect.DoseOverlap(rint2))
                            {
                                shouldContinue = true;
                                shouldBreak    = true;
                                pointIndex++;
                                if (pointIndex >= rects.Count)
                                {
                                    return(new Rect(0f, 0f, 0f, 0f));
                                }
                                point = rects[pointIndex].max + Vector2Int.one;
                                if (point.x > atlas.Texture.width || point.y > atlas.Texture.height)
                                {
                                    atlas.ResizeAtlas(new IntVector2(atlas.Texture.width * 2, atlas.Texture.height * 2));
                                }
                                break;
                            }
                        }
                    }
                    if (shouldBreak)
                    {
                        break;
                    }
                }
                if (shouldContinue)
                {
                    continue;
                }
                RectInt currentRect2 = new RectInt(point, pixelScale.ToVector2Int());
                if (currentRect2.xMax > atlas.Texture.width || currentRect2.yMax > atlas.Texture.height)
                {
                    atlas.ResizeAtlas(new IntVector2(atlas.Texture.width * 2, atlas.Texture.height * 2));
                }
                break;
            }
            RectInt currentRect3 = new RectInt(point, pixelScale.ToVector2Int());
            Rect    rect         = new Rect((float)currentRect3.x / atlas.Texture.width, (float)currentRect3.y / atlas.Texture.height, (float)currentRect3.width / atlas.Texture.width, (float)currentRect3.height / atlas.Texture.height);

            return(rect);
        }