Ejemplo n.º 1
0
    public void Pack()
    {
        atlasPath = null;
        jsonPath  = null;

        //@todo
        if (_PURE_WHITE == null)
        {
            _PURE_WHITE = AssetDatabase.LoadAssetAtPath <Texture2D>("Assets/SeinJSUnityToolkit/Shaders/white.png");
        }

        atlasTexture = new Texture2D(maxWidth, maxHeight);
        Graphics.CopyTexture(_PURE_WHITE, 0, 0, 0, 0, maxWidth, maxHeight, atlasTexture, 0, 0, 0, 0);

        Utils.DoActionForTextures(ref images, (texs) =>
        {
            rects = BinPacker.Pack(texs, atlasTexture, padding);
            atlasTexture.Apply();
        });

        if (rects == null)
        {
            EditorUtility.DisplayDialog("Error!", "Pack failed, atlas' size may be too small !", "OK");
            return;
        }

        if (saveAfterPack)
        {
            Save();
        }
    }
Ejemplo n.º 2
0
        public void Pack_AdjustedGlyphs_All()
        {
            // Arrange
            var glyphs = new List <Bitmap>();

            glyphs.Add(new Bitmap(5, 5));
            glyphs[0].SetPixel(2, 1, Color.White);
            glyphs[0].SetPixel(2, 2, Color.White);
            glyphs[0].SetPixel(2, 3, Color.White);
            glyphs[0].SetPixel(1, 2, Color.White);
            glyphs[0].SetPixel(3, 2, Color.White);
            glyphs.Add(new Bitmap(5, 5));
            glyphs[1].SetPixel(2, 2, Color.White);
            glyphs[1].SetPixel(2, 1, Color.White);
            glyphs[1].SetPixel(3, 2, Color.White);
            glyphs.Add(new Bitmap(2, 2));
            glyphs[2].SetPixel(0, 0, Color.White);
            glyphs[2].SetPixel(1, 0, Color.White);
            var adjustedGlyphs = FontMeasurement.MeasureWhiteSpace(glyphs);
            var binPacker      = new BinPacker(new Size(5, 3));

            // Act
            var boxes = binPacker.Pack(adjustedGlyphs).ToArray();

            // Assert
            boxes.Length.Should().Be(3);
            boxes[0].position.Should().Be(Point.Empty);
            boxes[1].position.Should().Be(new Point(3, 0));
            boxes[2].position.Should().Be(new Point(3, 2));
        }
Ejemplo n.º 3
0
        static void TestBinPack()
        {
            List <Bin> bins = new List <Bin>();

            for (int i = 0; i < 10; ++i)
            {
                Bin b = new Bin();
                b.Size = new Vec2i(100, 100);
                bins.Add(b);
            }

            Bin b2 = new Bin();

            b2.Size = new Vec2i(12, 200);
            bins.Add(b2);

            Bin b3 = new Bin();

            b3.Size = new Vec2i(1, 90);
            bins.Add(b3);

            BinPacker bp = new BinPacker(512);

            bp.Pack(bins);

            Console.WriteLine("Total size: {0} x {1}", bp.Width, bp.Height);
        }
Ejemplo n.º 4
0
        public void Pack_AdjustedGlyphs_Space()
        {
            // Arrange
            var glyphs = new List <Bitmap>();

            glyphs.Add(new Bitmap(5, 5));
            var adjustedGlyphs = FontMeasurement.MeasureWhiteSpace(glyphs);
            var binPacker      = new BinPacker(new Size(6, 6));

            // Act
            var boxes = binPacker.Pack(adjustedGlyphs).ToArray();

            // Assert
            boxes.Length.Should().Be(1);
            boxes[0].position.Should().Be(new Point(1, 1));
        }
Ejemplo n.º 5
0
        /*
         * This function try to rearranges all objects on the build platform so they
         * do not touch each other
         */
        public void RearrangeObjects()
        {
            BinPacker bp = new BinPacker();
            List<BinPacker.BinRect> rects = new List<BinPacker.BinRect>();
            foreach (Object3d obj in m_objects)
            {
                if (obj.tag != Object3d.OBJ_NORMAL)
                    continue;
                float w = obj.m_max.x - obj.m_min.x;
                float h = obj.m_max.y - obj.m_min.y;
                BinPacker.BinRect rc = new BinPacker.BinRect(w, h, obj);
                rects.Add(rc);
            }

            float pw = (float)UVDLPApp.Instance().m_printerinfo.m_PlatXSize;
            float ph = (float)UVDLPApp.Instance().m_printerinfo.m_PlatYSize;
            bp.Pack(rects, (int)pw, (int)ph, true);

            // find pack size
            int maxw = 0;
            int maxh = 0;
            foreach (BinPacker.BinRect rc in rects)
            {
                if (rc.packed && ((rc.x + rc.w) > maxw))
                    maxw = rc.x + rc.w;
                if (rc.packed && ((rc.y + rc.h) > maxh))
                    maxh = rc.y + rc.h;
            }

            // find offsets to center all objects
            float offsx = -(float)maxw / 2f + 0.5f;
            float offsy = -(float)maxh / 2f + 0.5f;

            // move all objects to new positions
            foreach (BinPacker.BinRect rc in rects)
            {
                Object3d obj = (Object3d)rc.obj;
                if (rc.packed)
                {
                    if (rc.rotated)
                        obj.Rotate(0, 0, 1.570796326f);
                    float dx = rc.x - obj.m_min.x + offsx;
                    float dy = rc.y - obj.m_min.y + offsy;
                    obj.Translate(dx, dy, 0, true);
                }
                else
                {
                    // object could not fit, place outside platform
                    obj.Translate(pw / 2f - obj.m_min.x, 0, 0, true);
                }
            }
            UVDLPApp.Instance().RaiseAppEvent(eAppEvent.eReDraw,"Redraw new arrangement");
        }
Ejemplo n.º 6
0
        public void GenereteUserTexture()
        {
            if (mUserTexture >= 0)
            {
                DeleteTexture(mUserTexture);
            }

            // step 1, arrange all textures in a one big bitmap;
            Bitmap bmp = new Bitmap(2048, 2048, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            float  tw  = bmp.Width;
            float  th  = bmp.Height;

            BinPacker bp = new BinPacker();
            List <BinPacker.BinRect> rects = new List <BinPacker.BinRect>();

            foreach (C2DImage img in mNewImages)
            {
                BinPacker.BinRect rc = new BinPacker.BinRect(img.w, img.h, img);
                rects.Add(rc);
            }

            bp.Pack(rects, bmp.Width, bmp.Height, false);

            List <C2DImage> packedImages = new List <C2DImage>();

            foreach (BinPacker.BinRect rc in rects)
            {
                C2DImage img = (C2DImage)rc.obj;
                if (rc.packed)
                {
                    img.x      = rc.x;
                    img.y      = rc.y;
                    img.x1     = (img.x + 0.5f) / tw;
                    img.y1     = (img.y + 0.5f) / th;
                    img.x2     = img.x1 + (float)(img.w - 1) / tw;
                    img.y2     = img.y1 + (float)(img.h - 1) / th;
                    img.scalex = tw;
                    img.scaley = th;
                    for (int i = 0; i < img.bmp.Height; i++)
                    {
                        System.Drawing.Imaging.BitmapData ddata = bmp.LockBits(new Rectangle((int)img.x, (int)img.y + i, img.bmp.Width, 1),
                                                                               System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        System.Drawing.Imaging.BitmapData sdata = img.bmp.LockBits(new Rectangle(0, i, img.bmp.Width, 1),
                                                                                   System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        int    numBytes  = 4 * img.bmp.Width;
                        byte[] rgbValues = new byte[numBytes];
                        Marshal.Copy(sdata.Scan0, rgbValues, 0, numBytes);
                        Marshal.Copy(rgbValues, 0, ddata.Scan0, numBytes);
                        bmp.UnlockBits(ddata);
                        img.bmp.UnlockBits(sdata);
                    }
                    packedImages.Add(img);
                    mNewImages.Remove(img);
                }
            }
            if (mNewImages.Count != 0)
            {
                DebugLogger.Instance().LogError("Not all user images could be loaded");
            }
            //bmp.Save("packedbmp.png", System.Drawing.Imaging.ImageFormat.Png);
            mUserTexture = LoadTextureImage(bmp);
            foreach (C2DImage img in packedImages)
            {
                img.tex            = mUserTexture;
                ImgDbase[img.name] = img;
            }
            needPacking = false;
        }
Ejemplo n.º 7
0
        public void GenereteUserTexture()
        {
            if (mUserTexture >= 0)
                DeleteTexture(mUserTexture);

            // step 1, arrange all textures in a one big bitmap;
            Bitmap bmp = new Bitmap(2048, 2048, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics gr = Graphics.FromImage(bmp);
            gr.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
            //gr.ResetTransform();
            //gr.PageUnit = GraphicsUnit.Pixel;
            using (Brush br = new SolidBrush(Color.FromArgb(0, 0, 0, 0)))
            {
                gr.FillRectangle(br, new Rectangle(0,0,bmp.Width,bmp.Height));
            }
            float tw = bmp.Width;
            float th = bmp.Height;

            BinPacker bp = new BinPacker();
            List<BinPacker.BinRect> rects = new List<BinPacker.BinRect>();
            foreach (C2DImage img in mNewImages)
            {
                BinPacker.BinRect rc = new BinPacker.BinRect(img.w, img.h, img);
                rects.Add(rc);
            }

            bp.Pack(rects, bmp.Width, bmp.Height, false);

            List<C2DImage> packedImages = new List<C2DImage>();
            foreach (BinPacker.BinRect rc in rects)
            {
                C2DImage img = (C2DImage)rc.obj;
                if (rc.packed)
                {
                    img.x = rc.x;
                    img.y = rc.y;
                    img.x1 = (img.x + 0.5f) / tw;
                    img.y1 = (img.y + 0.5f) / th;
                    img.x2 = img.x1 + (float)(img.w - 1) / tw;
                    img.y2 = img.y1 + (float)(img.h - 1) / th;
                    img.scalex = tw;
                    img.scaley = th;
                    gr.DrawImage(img.bmp, img.x, img.y, img.bmp.Width, img.bmp.Height);
                    //img.bmp.Save("origbmp.png", System.Drawing.Imaging.ImageFormat.Png);
                    packedImages.Add(img);
                    mNewImages.Remove(img);
                }
            }
            if (mNewImages.Count != 0)
            {
                DebugLogger.Instance().LogError("Not all user images could be loaded");
            }
            //bmp.Save("packedbmp.png", System.Drawing.Imaging.ImageFormat.Png);
            mUserTexture = LoadTextureImage(bmp);
            foreach (C2DImage img in packedImages)
            {
                img.tex = mUserTexture;
                ImgDbase[img.name] = img;
            }
            needPacking = false;
        }
Ejemplo n.º 8
0
        /*
         * This function try to rearranges all objects on the build platform so they
         * do not touch each other
         */
        public void RearrangeObjects()
        {
            BinPacker bp = new BinPacker();
            List <BinPacker.BinRect> rects = new List <BinPacker.BinRect>();

            foreach (Object3d obj in m_objects)
            {
                if (obj.tag != Object3d.OBJ_NORMAL)
                {
                    continue;
                }
                float             w  = obj.m_max.x - obj.m_min.x;
                float             h  = obj.m_max.y - obj.m_min.y;
                BinPacker.BinRect rc = new BinPacker.BinRect(w, h, obj);
                rects.Add(rc);
            }

            float pw = (float)UVDLPApp.Instance().m_printerinfo.m_PlatXSize;
            float ph = (float)UVDLPApp.Instance().m_printerinfo.m_PlatYSize;

            bp.Pack(rects, (int)pw, (int)ph, true);

            // find pack size
            int maxw = 0;
            int maxh = 0;

            foreach (BinPacker.BinRect rc in rects)
            {
                if (rc.packed && ((rc.x + rc.w) > maxw))
                {
                    maxw = rc.x + rc.w;
                }
                if (rc.packed && ((rc.y + rc.h) > maxh))
                {
                    maxh = rc.y + rc.h;
                }
            }

            // find offsets to center all objects
            float offsx = -(float)maxw / 2f + 0.5f;
            float offsy = -(float)maxh / 2f + 0.5f;

            // move all objects to new positions
            foreach (BinPacker.BinRect rc in rects)
            {
                Object3d obj = (Object3d)rc.obj;
                if (rc.packed)
                {
                    if (rc.rotated)
                    {
                        obj.Rotate(0, 0, 1.570796326f);
                    }
                    float dx = rc.x - obj.m_min.x + offsx;
                    float dy = rc.y - obj.m_min.y + offsy;
                    obj.Translate(dx, dy, 0, true);
                }
                else
                {
                    // object could not fit, place outside platform
                    obj.Translate(pw / 2f - obj.m_min.x, 0, 0, true);
                }
            }
            UVDLPApp.Instance().RaiseAppEvent(eAppEvent.eReDraw, "Redraw new arrangement");
        }