Пример #1
0
        public void PackerTexture1()
        {
            Texture2D[] packerTexs = _texMap.Values.ToArray();
            _texMap.Clear();

            _mainTex          = new Texture2D(_defaultWh, _defaultWh, TextureFormat.ARGB32, false);
            _mainTex.wrapMode = TextureWrapMode.Clamp;

            const int       padding = 0;
            RectanglePacker packer  = new RectanglePacker(_mainTex.width, _mainTex.height, padding);

            for (int i = 0; i < packerTexs.Length; i++)
            {
                packer.insertRectangle(packerTexs[i].width, packerTexs[i].height, i);
            }
            packer.packRectangles();

            IntegerRectangle rect = new IntegerRectangle();

            for (int i = 0; i < packer.rectangleCount; i++)
            {
                rect = packer.getRectangle(i, rect);
                int index = packer.getRectangleId(i);

                _mainTex.SetPixels32(rect.x, rect.y, rect.width, rect.height, packerTexs[index].GetPixels32());
                Sprite sprite = Sprite.Create(_mainTex, new Rect(rect.x, rect.y, rect.width, rect.height),
                                              Vector2.zero, _pixelsPerUnit, 0, SpriteMeshType.FullRect);
                _spriteMap.Add(packerTexs[i].name, sprite);
            }
            _mainTex.Apply();
        }
Пример #2
0
        public bool CreateAtlas(Texture[] textures, int width = 1024, int height = 1024, int padding = 1)
        {
            bool result = false;

            rectangles.Clear();
            dictionary.Clear();
            packer.ResetAll(width, height, padding);

            CreateMaterial();
            CreateRenderTexture(width, height);

            for (int i = 0; i < textures.Length; i++)
            {
                rectangles.Add(new Rect(0, 0, textures[i].width, textures[i].height));
            }

            for (int i = 0; i < rectangles.Count; i++)
            {
                packer.insertRectangle((int)rectangles[i].width, (int)rectangles[i].height, i);
            }

            packer.packRectangles();

            int index = 0;

            if (packer.rectangleCount > 0)
            {
                for (int j = 0; j < packer.rectangleCount; j++)
                {
                    integerRect = packer.getRectangle(j, integerRect);
                    index       = packer.getRectangleId(j);

                    if (!dictionary.ContainsKey(textures[index].name))
                    {
                        Rect rect = new Rect();
                        rect.x      = (float)integerRect.x / width;
                        rect.y      = (float)integerRect.y / height;
                        rect.width  = (float)integerRect.width / width;
                        rect.height = (float)integerRect.height / height;

                        dictionary.Add(textures[index].name, rect);
                    }

                    DrawTexture(textures[index], renderTexture,
                                new Rect(integerRect.x, integerRect.y, integerRect.width, integerRect.height));
                }

                result = true;
            }

            return(result);
        }
    private void updateRectangles()
    {
        DateTime  start   = DateTime.Now;
        const int padding = 1;

        if (mPacker == null)
        {
            mPacker = new RectanglePacker((int)sliderWidth.value, (int)sliderHeight.value, padding);
        }
        else
        {
            mPacker.reset((int)sliderWidth.value, (int)sliderHeight.value, padding);
        }

        for (int i = 0; i < RECTANGLE_COUNT; i++)
        {
            mPacker.insertRectangle((int)mRectangles[i].width, (int)mRectangles[i].height, i);
        }

        mPacker.packRectangles();
        Clear();
        DateTime end = DateTime.Now;

        if (mPacker.rectangleCount > 0)
        {
            packingTimeText.text = mPacker.rectangleCount + " rectangles packed in " + (end - start).Milliseconds + "ms";
            IntegerRectangle rect = new IntegerRectangle();
            for (int j = 0; j < mPacker.rectangleCount; j++)
            {
                rect = mPacker.getRectangle(j, rect);
                int       index   = mPacker.getRectangleId(j);
                Texture2D texture = textures[index];
                SetTexture(texture, IntegerRectangle2Rect(rect));
            }
        }
    }
Пример #4
0
        protected IEnumerator createPack(string savePath = "")
        {
            if (savePath != "")
            {
                if (deletePreviousCacheVersion && Directory.Exists(Application.persistentDataPath + "/AssetPacker/" + cacheName + "/"))
                {
                    foreach (string dirPath in Directory.GetDirectories(Application.persistentDataPath + "/AssetPacker/" + cacheName + "/", "*", SearchOption.AllDirectories))
                    {
                        Directory.Delete(dirPath, true);
                    }
                }

                Directory.CreateDirectory(savePath);
            }

            List <Texture2D> textures = new List <Texture2D>();
            List <string>    images   = new List <string>();

            foreach (TextureToPack itemToRaster in itemsToRaster)
            {
                WWW loader = new WWW("file:///" + itemToRaster.file);

                yield return(loader);

                textures.Add(loader.texture);
                images.Add(itemToRaster.id);
            }

            int textureSize = allow4096Textures ? 4096 : 2048;

            List <Rect> rectangles = new List <Rect>();

            for (int i = 0; i < textures.Count; i++)
            {
                if (textures[i].width > textureSize || textures[i].height > textureSize)
                {
                    throw new Exception("A texture size is bigger than the sprite sheet size!");
                }
                else
                {
                    rectangles.Add(new Rect(0, 0, textures[i].width, textures[i].height));
                }
            }

            const int padding = 1;

            int numSpriteSheet = 0;

            while (rectangles.Count > 0)
            {
                Texture2D texture   = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false);
                Color32[] fillColor = texture.GetPixels32();
                for (int i = 0; i < fillColor.Length; ++i)
                {
                    fillColor[i] = Color.clear;
                }

                RectanglePacker packer = new RectanglePacker(texture.width, texture.height, padding);

                for (int i = 0; i < rectangles.Count; i++)
                {
                    packer.insertRectangle((int)rectangles[i].width, (int)rectangles[i].height, i);
                }

                packer.packRectangles();

                if (packer.rectangleCount > 0)
                {
                    texture.SetPixels32(fillColor);
                    IntegerRectangle    rect          = new IntegerRectangle();
                    List <TextureAsset> textureAssets = new List <TextureAsset>();

                    List <Rect>      garbageRect    = new List <Rect>();
                    List <Texture2D> garabeTextures = new List <Texture2D>();
                    List <string>    garbageImages  = new List <string>();

                    for (int j = 0; j < packer.rectangleCount; j++)
                    {
                        rect = packer.getRectangle(j, rect);

                        int index = packer.getRectangleId(j);

                        texture.SetPixels32(rect.x, rect.y, rect.width, rect.height, textures[index].GetPixels32());

                        TextureAsset textureAsset = new TextureAsset();
                        textureAsset.x      = rect.x;
                        textureAsset.y      = rect.y;
                        textureAsset.width  = rect.width;
                        textureAsset.height = rect.height;
                        textureAsset.name   = images[index];

                        textureAssets.Add(textureAsset);

                        garbageRect.Add(rectangles[index]);
                        garabeTextures.Add(textures[index]);
                        garbageImages.Add(images[index]);
                    }

                    foreach (Rect garbage in garbageRect)
                    {
                        rectangles.Remove(garbage);
                    }

                    foreach (Texture2D garbage in garabeTextures)
                    {
                        textures.Remove(garbage);
                    }

                    foreach (string garbage in garbageImages)
                    {
                        images.Remove(garbage);
                    }

                    texture.Apply();

                    if (savePath != "")
                    {
                        File.WriteAllBytes(savePath + "/data" + numSpriteSheet + ".png", texture.EncodeToPNG());
                        File.WriteAllText(savePath + "/data" + numSpriteSheet + ".json", JsonUtility.ToJson(new TextureAssets(textureAssets.ToArray())));
                        ++numSpriteSheet;
                    }

                    foreach (TextureAsset textureAsset in textureAssets)
                    {
                        mSprites.Add(textureAsset.name, Sprite.Create(texture, new Rect(textureAsset.x, textureAsset.y, textureAsset.width, textureAsset.height), Vector2.zero, pixelsPerUnit, 0, SpriteMeshType.FullRect));
                    }
                }
            }

            OnProcessCompleted.Invoke();
        }
        private void updateRectangles()
        {
            DateTime  start   = DateTime.Now;
            const int padding = 1;

            if (mPacker == null)
            {
                mPacker = new RectanglePacker((int)sliderWidth.value, (int)sliderHeight.value, padding);
            }
            else
            {
                mPacker.reset((int)sliderWidth.value, (int)sliderHeight.value, padding);
            }

            for (int i = 0; i < RECTANGLE_COUNT; i++)
            {
                mPacker.insertRectangle((int)mRectangles[i].width, (int)mRectangles[i].height, i);
            }

            mPacker.packRectangles();

            DateTime end = DateTime.Now;

            if (mPacker.rectangleCount > 0)
            {
                packingTimeText.text = mPacker.rectangleCount + " rectangles packed in " + (end - start).Milliseconds + "ms";

                mTexture.SetPixels32(mFillColor);
                IntegerRectangle rect = new IntegerRectangle();
                Color32[]        tmpColor;

                for (int j = 0; j < mPacker.rectangleCount; j++)
                {
                    rect = mPacker.getRectangle(j, rect);

                    int size = rect.width * rect.height;

                    tmpColor = new Color32[size];
                    for (int k = 0; k < size; ++k)
                    {
                        tmpColor[k] = Color.black;
                    }

                    mTexture.SetPixels32(rect.x, rect.y, rect.width, rect.height, tmpColor);

                    int   index = mPacker.getRectangleId(j);
                    Color color = convertHexToRGBA((uint)(0xFF171703 + (((18 * ((index + 4) % 13)) << 16) + ((31 * ((index * 3) % 8)) << 8) + 63 * (((index + 1) * 3) % 5))));

                    size -= 4;

                    tmpColor = new Color32[size];
                    for (int k = 0; k < size; ++k)
                    {
                        tmpColor[k] = color;
                    }

                    mTexture.SetPixels32(rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2, tmpColor);
                }

                mTexture.Apply();
            }
        }
Пример #6
0
        public bool Push(string key, Texture2D texture)
        {
            if (string.IsNullOrEmpty(key))
            {
                return(true);
            }

            if (texture == null)
            {
                return(true);
            }

            for (int i = 0; i < m_sprites.Count; i++)
            {
                m_sprites[i].index = i;
                m_sprites[i].color = m_texture.GetPixels((int)m_sprites[i].rect.x, (int)m_sprites[i].rect.y, (int)m_sprites[i].rect.width, (int)m_sprites[i].rect.height);
            }

            m_sprites.Add(new DynamicSprite()
            {
                key   = key,
                index = -1,
                rect  = new Rect(0, 0, texture.width, texture.height),
                color = texture.GetPixels(),
            });

            m_packer.reset(width, height, padding);

            for (int i = 0; i < m_sprites.Count; i++)
            {
                m_packer.insertRectangle((int)m_sprites[i].rect.width, (int)m_sprites[i].rect.height, m_sprites[i].index);
            }

            int count = m_packer.packRectangles();

            enough = m_sprites.Count > count;

            if (enough)
            {
                m_sprites.RemoveAt(m_sprites.FindIndex(x => x.key == key));

                for (int i = 0; i < m_sprites.Count; i++)
                {
                    m_sprites[i].color = null;
                }

                return(false);
            }
            else
            {
                if (texture != null)
                {
                    texture = null;
                }

                if (m_texture.format != TextureFormat.RGBA32)
                {
                    Object.Destroy(m_texture);

                    m_texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
                }
                m_texture.SetPixels32(m_color);

                IntegerRectangle rect = new IntegerRectangle();

                Color32[] color;

                int index, id, size;

                for (int i = 0; i < count; i++)
                {
                    rect = m_packer.getRectangle(i, rect);

                    id = m_packer.getRectangleId(i);

                    index = m_sprites.FindIndex(x => x.index == id);

                    if (index != -1)
                    {
                        m_sprites[index].rect = new Rect(rect.x, rect.y, rect.width, rect.height);

                        color = new Color32[m_sprites[index].color.Length];

                        for (int j = 0; j < m_sprites[index].color.Length; j++)
                        {
                            color[j] = m_sprites[index].color[j];
                        }
                    }
                    else
                    {
                        size = rect.width * rect.height;

                        color = new Color32[size];

                        for (int j = 0; j < size; j++)
                        {
                            color[j] = Color.black;
                        }
                    }
                    m_texture.SetPixels32(rect.x, rect.y, rect.width, rect.height, color);
                }

                for (int i = 0; i < m_sprites.Count; i++)
                {
                    m_sprites[i].color = null;
                }

                if (compress)
                {
                    m_texture.Compress(true);
                }

                m_texture.Apply();

                return(true);
            }
        }
Пример #7
0
        protected IEnumerator createPack(string savePath = "")
        {
            if (savePath != "")
            {
                if (deletePreviousCacheVersion && Directory.Exists(Application.persistentDataPath + "/AssetPacker/" + cacheName + "/"))
                {
                    foreach (string dirPath in Directory.GetDirectories(Application.persistentDataPath + "/AssetPacker/" + cacheName + "/", "*", SearchOption.AllDirectories))
                    {
                        Directory.Delete(dirPath, true);
                    }
                }

                Directory.CreateDirectory(savePath);
            }

            List <Texture2D> textures = new List <Texture2D>();
            List <string>    images   = new List <string>();

            foreach (TextureToPack itemToRaster in itemsToRaster)
            {
                Texture2D baseTexture = itemToRaster.preLoadedTexture;

                if (!baseTexture)
                {
                    WWW loader = new WWW("file:///" + itemToRaster.file);

                    yield return(loader);

                    baseTexture = loader.texture;
                }

                if (itemToRaster.sliceParams != null)
                {
                    TextureToPack.GridSlice sliceParams = itemToRaster.sliceParams;

                    if (sliceParams.width > baseTexture.width || sliceParams.height > baseTexture.height)
                    {
                        throw new Exception(string.Format("Width and/or height of texture {0} is less than the provided slice parameters.", itemToRaster.id));
                    }

                    if (!(baseTexture.width % sliceParams.width == 0 && baseTexture.height % sliceParams.height == 0))
                    {
                        throw new Exception(string.Format("Width and/or height of texture {0} not a multiple of the provided slice parameters.", itemToRaster.id));
                    }

                    int totalRows    = baseTexture.height / sliceParams.height;
                    int totalColumns = baseTexture.width / sliceParams.width;

                    int textureIndex = 0;

                    // Scan each slice left to right, top to bottom
                    for (int rowIndex = 0; rowIndex < totalRows; ++rowIndex)
                    {
                        for (int columnIndex = 0; columnIndex < totalColumns; ++columnIndex)
                        {
                            int x = columnIndex * sliceParams.width;
                            int y = (totalRows - rowIndex - 1) * sliceParams.height;    // Starting from the top, not the bottom. Specific to GH3 sprite sheets, not nesacarily universal.

                            Color[]   pixels  = baseTexture.GetPixels(x, y, sliceParams.width, sliceParams.height);
                            Texture2D texture = new Texture2D(sliceParams.width, sliceParams.height);
                            texture.SetPixels(pixels);
                            texture.Apply();

                            textures.Add(texture);
                            images.Add(string.Format("{0}_{1}", itemToRaster.id, textureIndex.ToString("D8")));

                            ++textureIndex;
                        }
                    }
                }
                else
                {
                    textures.Add(baseTexture);
                    images.Add(itemToRaster.id);
                }
            }

            int textureSize = allow4096Textures ? 4096 : 2048;

            List <Rect> rectangles = new List <Rect>();

            for (int i = 0; i < textures.Count; i++)
            {
                if (textures[i].width > textureSize || textures[i].height > textureSize)
                {
                    throw new Exception("A texture size is bigger than the sprite sheet size!");
                }
                else
                {
                    rectangles.Add(new Rect(0, 0, textures[i].width, textures[i].height));
                }
            }

            const int padding = 1;

            int numSpriteSheet = 0;

            while (rectangles.Count > 0)
            {
                Texture2D texture   = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false);
                Color32[] fillColor = texture.GetPixels32();
                for (int i = 0; i < fillColor.Length; ++i)
                {
                    fillColor[i] = Color.clear;
                }

                RectanglePacker packer = new RectanglePacker(texture.width, texture.height, padding);

                for (int i = 0; i < rectangles.Count; i++)
                {
                    packer.insertRectangle((int)rectangles[i].width, (int)rectangles[i].height, i);
                }

                packer.packRectangles();

                if (packer.rectangleCount > 0)
                {
                    texture.SetPixels32(fillColor);
                    IntegerRectangle    rect          = new IntegerRectangle();
                    List <TextureAsset> textureAssets = new List <TextureAsset>();

                    List <Rect>      garbageRect    = new List <Rect>();
                    List <Texture2D> garabeTextures = new List <Texture2D>();
                    List <string>    garbageImages  = new List <string>();

                    for (int j = 0; j < packer.rectangleCount; j++)
                    {
                        rect = packer.getRectangle(j, rect);

                        int index = packer.getRectangleId(j);

                        texture.SetPixels32(rect.x, rect.y, rect.width, rect.height, textures[index].GetPixels32());

                        TextureAsset textureAsset = new TextureAsset();
                        textureAsset.x      = rect.x;
                        textureAsset.y      = rect.y;
                        textureAsset.width  = rect.width;
                        textureAsset.height = rect.height;
                        textureAsset.name   = images[index];

                        textureAssets.Add(textureAsset);

                        garbageRect.Add(rectangles[index]);
                        garabeTextures.Add(textures[index]);
                        garbageImages.Add(images[index]);
                    }

                    foreach (Rect garbage in garbageRect)
                    {
                        rectangles.Remove(garbage);
                    }

                    foreach (Texture2D garbage in garabeTextures)
                    {
                        textures.Remove(garbage);
                    }

                    foreach (string garbage in garbageImages)
                    {
                        images.Remove(garbage);
                    }

                    texture.Apply();

                    if (savePath != "")
                    {
                        File.WriteAllBytes(savePath + "/data" + numSpriteSheet + ".png", texture.EncodeToPNG());
                        File.WriteAllText(savePath + "/data" + numSpriteSheet + ".json", JsonUtility.ToJson(new TextureAssets(textureAssets.ToArray()), true));
                        ++numSpriteSheet;
                    }

                    foreach (TextureAsset textureAsset in textureAssets)
                    {
                        Sprite sprite = Sprite.Create(texture, new Rect(textureAsset.x, textureAsset.y, textureAsset.width, textureAsset.height), spritePivot, pixelsPerUnit, 0, SpriteMeshType.FullRect);
                        sprite.name = textureAsset.name;
                        mSprites.Add(textureAsset.name, sprite);
                    }
                }
            }

            OnProcessCompleted.Invoke();
        }