Example #1
0
 private bool Probe(MB2_TexturePacker.Image[] imgsToAdd, int idealAtlasW, int idealAtlasH, float imgArea, int maxAtlasDim, MB2_TexturePacker.ProbeResult pr)
 {
     MB2_TexturePacker.Node node = new MB2_TexturePacker.Node();
     node.r = new MB2_TexturePacker.PixRect(0, 0, idealAtlasW, idealAtlasH);
     for (int i = 0; i < imgsToAdd.Length; i++)
     {
         if (node.Insert(imgsToAdd[i], false) == null)
         {
             return(false);
         }
         if (i == imgsToAdd.Length - 1)
         {
             int num  = 0;
             int num2 = 0;
             this.GetExtent(node, ref num, ref num2);
             bool  flag;
             float num8;
             float num9;
             if (this.doPowerOfTwoTextures)
             {
                 int num3 = Mathf.Min(this.CeilToNearestPowerOfTwo(num), maxAtlasDim);
                 int num4 = Mathf.Min(this.CeilToNearestPowerOfTwo(num2), maxAtlasDim);
                 if (num4 < num3 / 2)
                 {
                     num4 = num3 / 2;
                 }
                 if (num3 < num4 / 2)
                 {
                     num3 = num4 / 2;
                 }
                 flag = (num <= maxAtlasDim && num2 <= maxAtlasDim);
                 float num5 = Mathf.Max(1f, (float)num / (float)maxAtlasDim);
                 float num6 = Mathf.Max(1f, (float)num2 / (float)maxAtlasDim);
                 float num7 = (float)num3 * num5 * (float)num4 * num6;
                 num8 = 1f - (num7 - imgArea) / num7;
                 num9 = 1f;
             }
             else
             {
                 num8 = 1f - ((float)(num * num2) - imgArea) / (float)(num * num2);
                 if (num < num2)
                 {
                     num9 = (float)num / (float)num2;
                 }
                 else
                 {
                     num9 = (float)num2 / (float)num;
                 }
                 flag = (num <= maxAtlasDim && num2 <= maxAtlasDim);
             }
             pr.Set(num, num2, node, flag, num8, num9);
             if (this.LOG_LEVEL >= MB2_LogLevel.debug)
             {
                 MB2_Log.LogDebug(string.Concat(new object[]
                 {
                     "Probe success efficiency w=",
                     num,
                     " h=",
                     num2,
                     " e=",
                     num8,
                     " sq=",
                     num9,
                     " fits=",
                     flag
                 }), new object[0]);
             }
             return(true);
         }
     }
     Debug.LogError("Should never get here.");
     return(false);
 }
Example #2
0
        private Rect[] _GetRects(List <Vector2> imgWidthHeights, int maxDimension, int padding, int minImageSizeX, int minImageSizeY, int masterImageSizeX, int masterImageSizeY, out int outW, out int outH, int recursionDepth)
        {
            if (this.LOG_LEVEL >= MB2_LogLevel.debug)
            {
                Debug.Log(string.Format("_GetRects numImages={0}, maxDimension={1}, padding={2}, minImageSizeX={3}, minImageSizeY={4}, masterImageSizeX={5}, masterImageSizeY={6}, recursionDepth={7}", new object[]
                {
                    imgWidthHeights.Count,
                    maxDimension,
                    padding,
                    minImageSizeX,
                    minImageSizeY,
                    masterImageSizeX,
                    masterImageSizeY,
                    recursionDepth
                }));
            }
            if (recursionDepth > 10)
            {
                Debug.LogError("Maximum recursion depth reached. Couldn't find packing for these textures.");
                outW = 0;
                outH = 0;
                return(new Rect[0]);
            }
            float num  = 0f;
            int   num2 = 0;
            int   num3 = 0;

            MB2_TexturePacker.Image[] array = new MB2_TexturePacker.Image[imgWidthHeights.Count];
            for (int i = 0; i < array.Length; i++)
            {
                MB2_TexturePacker.Image image = array[i] = new MB2_TexturePacker.Image(i, (int)imgWidthHeights[i].x, (int)imgWidthHeights[i].y, padding, minImageSizeX, minImageSizeY);
                num += (float)(image.w * image.h);
                num2 = Mathf.Max(num2, image.w);
                num3 = Mathf.Max(num3, image.h);
            }
            if ((float)num3 / (float)num2 > 2f)
            {
                if (this.LOG_LEVEL >= MB2_LogLevel.debug)
                {
                    MB2_Log.LogDebug("Using height Comparer", new object[0]);
                }
                Array.Sort <MB2_TexturePacker.Image>(array, new MB2_TexturePacker.ImageHeightComparer());
            }
            else if ((double)((float)num3 / (float)num2) < 0.5)
            {
                if (this.LOG_LEVEL >= MB2_LogLevel.debug)
                {
                    MB2_Log.LogDebug("Using width Comparer", new object[0]);
                }
                Array.Sort <MB2_TexturePacker.Image>(array, new MB2_TexturePacker.ImageWidthComparer());
            }
            else
            {
                if (this.LOG_LEVEL >= MB2_LogLevel.debug)
                {
                    MB2_Log.LogDebug("Using area Comparer", new object[0]);
                }
                Array.Sort <MB2_TexturePacker.Image>(array, new MB2_TexturePacker.ImageAreaComparer());
            }
            int num4 = (int)Mathf.Sqrt(num);
            int num6;
            int num5;

            if (this.doPowerOfTwoTextures)
            {
                num5 = (num6 = this.RoundToNearestPositivePowerOfTwo(num4));
                if (num2 > num6)
                {
                    num6 = this.CeilToNearestPowerOfTwo(num6);
                }
                if (num3 > num5)
                {
                    num5 = this.CeilToNearestPowerOfTwo(num5);
                }
            }
            else
            {
                num6 = num4;
                num5 = num4;
                if (num2 > num4)
                {
                    num6 = num2;
                    num5 = Mathf.Max(Mathf.CeilToInt(num / (float)num2), num3);
                }
                if (num3 > num4)
                {
                    num6 = Mathf.Max(Mathf.CeilToInt(num / (float)num3), num2);
                    num5 = num3;
                }
            }
            if (num6 == 0)
            {
                num6 = 1;
            }
            if (num5 == 0)
            {
                num5 = 1;
            }
            int num7 = (int)((float)num6 * 0.15f);
            int num8 = (int)((float)num5 * 0.15f);

            if (num7 == 0)
            {
                num7 = 1;
            }
            if (num8 == 0)
            {
                num8 = 1;
            }
            int num9  = 2;
            int num10 = num5;

            while (num9 >= 1 && num10 < num4 * 1000)
            {
                bool flag = false;
                num9 = 0;
                int num11 = num6;
                while (!flag && num11 < num4 * 1000)
                {
                    MB2_TexturePacker.ProbeResult probeResult = new MB2_TexturePacker.ProbeResult();
                    if (this.LOG_LEVEL >= MB2_LogLevel.trace)
                    {
                        Debug.Log(string.Concat(new object[]
                        {
                            "Probing h=",
                            num10,
                            " w=",
                            num11
                        }));
                    }
                    if (this.Probe(array, num11, num10, num, maxDimension, probeResult))
                    {
                        flag = true;
                        if (this.bestRoot == null)
                        {
                            this.bestRoot = probeResult;
                        }
                        else if (probeResult.GetScore(this.doPowerOfTwoTextures) > this.bestRoot.GetScore(this.doPowerOfTwoTextures))
                        {
                            this.bestRoot = probeResult;
                        }
                    }
                    else
                    {
                        num9++;
                        num11 = this.StepWidthHeight(num11, num7, maxDimension);
                        if (this.LOG_LEVEL >= MB2_LogLevel.debug)
                        {
                            MB2_Log.LogDebug(string.Concat(new object[]
                            {
                                "increasing Width h=",
                                num10,
                                " w=",
                                num11
                            }), new object[0]);
                        }
                    }
                }
                num10 = this.StepWidthHeight(num10, num8, maxDimension);
                if (this.LOG_LEVEL >= MB2_LogLevel.debug)
                {
                    MB2_Log.LogDebug(string.Concat(new object[]
                    {
                        "increasing Height h=",
                        num10,
                        " w=",
                        num11
                    }), new object[0]);
                }
            }
            outW = 0;
            outH = 0;
            if (this.doPowerOfTwoTextures)
            {
                outW = Mathf.Min(this.CeilToNearestPowerOfTwo(this.bestRoot.w), maxDimension);
                outH = Mathf.Min(this.CeilToNearestPowerOfTwo(this.bestRoot.h), maxDimension);
                if (outH < outW / 2)
                {
                    outH = outW / 2;
                }
                if (outW < outH / 2)
                {
                    outW = outH / 2;
                }
            }
            else
            {
                outW = this.bestRoot.w;
                outH = this.bestRoot.h;
            }
            if (this.bestRoot == null)
            {
                return(null);
            }
            if (this.LOG_LEVEL >= MB2_LogLevel.debug)
            {
                MB2_Log.LogDebug(string.Concat(new object[]
                {
                    "Best fit found: atlasW=",
                    outW,
                    " atlasH",
                    outH,
                    " w=",
                    this.bestRoot.w,
                    " h=",
                    this.bestRoot.h,
                    " efficiency=",
                    this.bestRoot.efficiency,
                    " squareness=",
                    this.bestRoot.squareness,
                    " fits in max dimension=",
                    this.bestRoot.fitsInMaxSize
                }), new object[0]);
            }
            List <MB2_TexturePacker.Image> list = new List <MB2_TexturePacker.Image>();

            MB2_TexturePacker.flattenTree(this.bestRoot.root, list);
            list.Sort(new MB2_TexturePacker.ImgIDComparer());
            if (list.Count != array.Length)
            {
                Debug.LogError("Result images not the same lentgh as source");
            }
            int   minImageSizeX2 = minImageSizeX;
            int   minImageSizeY2 = minImageSizeY;
            bool  flag2          = false;
            float num12          = (float)padding / (float)outW;

            if (this.bestRoot.w > maxDimension)
            {
                num12 = (float)padding / (float)maxDimension;
                float num13 = (float)maxDimension / (float)this.bestRoot.w;
                if (this.LOG_LEVEL >= MB2_LogLevel.warn)
                {
                    Debug.LogWarning("Packing exceeded atlas width shrinking to " + num13);
                }
                for (int j = 0; j < list.Count; j++)
                {
                    MB2_TexturePacker.Image image2 = list[j];
                    if ((float)image2.w * num13 < (float)masterImageSizeX)
                    {
                        if (this.LOG_LEVEL >= MB2_LogLevel.debug)
                        {
                            Debug.Log("Small images are being scaled to zero. Will need to redo packing with larger minTexSizeX.");
                        }
                        flag2          = true;
                        minImageSizeX2 = Mathf.CeilToInt((float)minImageSizeX / num13);
                    }
                    int num14 = (int)((float)(image2.x + image2.w) * num13);
                    image2.x = (int)(num13 * (float)image2.x);
                    image2.w = num14 - image2.x;
                }
                outW = maxDimension;
            }
            float num15 = (float)padding / (float)outH;

            if (this.bestRoot.h > maxDimension)
            {
                num15 = (float)padding / (float)maxDimension;
                float num16 = (float)maxDimension / (float)this.bestRoot.h;
                if (this.LOG_LEVEL >= MB2_LogLevel.warn)
                {
                    Debug.LogWarning("Packing exceeded atlas height shrinking to " + num16);
                }
                for (int k = 0; k < list.Count; k++)
                {
                    MB2_TexturePacker.Image image3 = list[k];
                    if ((float)image3.h * num16 < (float)masterImageSizeY)
                    {
                        if (this.LOG_LEVEL >= MB2_LogLevel.debug)
                        {
                            Debug.Log("Small images are being scaled to zero. Will need to redo packing with larger minTexSizeY.");
                        }
                        flag2          = true;
                        minImageSizeY2 = Mathf.CeilToInt((float)minImageSizeY / num16);
                    }
                    int num17 = (int)((float)(image3.y + image3.h) * num16);
                    image3.y = (int)(num16 * (float)image3.y);
                    image3.h = num17 - image3.y;
                }
                outH = maxDimension;
            }
            Rect[] array2;
            if (!flag2)
            {
                array2 = new Rect[list.Count];
                for (int l = 0; l < list.Count; l++)
                {
                    MB2_TexturePacker.Image image4 = list[l];
                    Rect rect = array2[l] = new Rect((float)image4.x / (float)outW + num12, (float)image4.y / (float)outH + num15, (float)image4.w / (float)outW - num12 * 2f, (float)image4.h / (float)outH - num15 * 2f);
                    if (this.LOG_LEVEL >= MB2_LogLevel.debug)
                    {
                        MB2_Log.LogDebug(string.Concat(new object[]
                        {
                            "Image: ",
                            l,
                            " imgID=",
                            image4.imgId,
                            " x=",
                            rect.x * (float)outW,
                            " y=",
                            rect.y * (float)outH,
                            " w=",
                            rect.width * (float)outW,
                            " h=",
                            rect.height * (float)outH,
                            " padding=",
                            padding
                        }), new object[0]);
                    }
                }
            }
            else
            {
                if (this.LOG_LEVEL >= MB2_LogLevel.debug)
                {
                    Debug.Log("==================== REDOING PACKING ================");
                }
                this.bestRoot = null;
                array2        = this._GetRects(imgWidthHeights, maxDimension, padding, minImageSizeX2, minImageSizeY2, masterImageSizeX, masterImageSizeY, out outW, out outH, recursionDepth + 1);
            }
            if (this.LOG_LEVEL >= MB2_LogLevel.debug)
            {
                MB2_Log.LogDebug("Done GetRects", new object[0]);
            }
            return(array2);
        }