Ejemplo n.º 1
0
        private static Rect[] GetPackedTexture(Dictionary <MaterialData, RendererList> matMap, out Texture2D texture)
        {
            var packingList = new List <PackingData>();

            foreach (var pair in matMap)
            {
                for (int index = 0; index < pair.Value.List.Count; index++)
                {
                    var rData = pair.Value.List[index];
                    if (rData.Mesh == null)
                    {
                        continue;
                    }
                    var uvs = rData.Mesh.uv;
                    // Get Remap UV
                    var     minmax = new Vector4(1, 1, 0, 0);
                    Vector2 uv;
                    for (int i = 0; i < uvs.Length; i++)
                    {
                        uv       = uvs[i];
                        minmax.x = Mathf.Min(minmax.x, uv.x);
                        minmax.y = Mathf.Min(minmax.y, uv.y);
                        minmax.z = Mathf.Max(minmax.z, uv.x);
                        minmax.w = Mathf.Max(minmax.w, uv.y);
                    }
                    var remapUV = new Rect(minmax.x, minmax.y, minmax.z - minmax.x, minmax.w - minmax.y);
                    rData.RemapUV = remapUV;

                    // Get New Texture
                    if (rData.Texture)
                    {
                        int sourceWidth  = rData.Texture.width;
                        int sourceHeight = rData.Texture.height;
                        int width        = (int)(sourceWidth * remapUV.width);
                        int height       = (int)(sourceHeight * remapUV.height);
                        var colors       = rData.Texture.GetPixels(
                            (int)(remapUV.x * sourceWidth),
                            (int)(remapUV.y * sourceHeight),
                            width,
                            height
                            );
                        rData.PackingIndex = packingList.Count;
                        packingList.Add(new PackingData(width, height, colors));
                    }
                    else
                    {
                        rData.PackingIndex = packingList.Count;
                        packingList.Add(new PackingData(1, 1, new Color[1]));
                    }
                }
            }

            return(RectPacking.PackTextures(out texture, packingList, true, false));
        }
Ejemplo n.º 2
0
        private static Result Pack2DTextures(Color[][] colorss, int[] widths, int[] heights, Vector2 pivot)
        {
            int    tCount     = colorss.Length;
            Result resultInfo = new Result()
            {
                Pivots = new Vector2[6] {
                    pivot, pivot, pivot, pivot, pivot, pivot
                },
            };

            Texture2D[] textures = new Texture2D[colorss.Length];
            for (int i = 0; i < textures.Length; i++)
            {
                textures[i] = new Texture2D(widths[i], heights[i], TextureFormat.ARGB32, false);
                textures[i].SetPixels(colorss[i]);
                textures[i].Apply();
            }

            var packingList = new List <PackingData>();

            for (int i = 0; i < textures.Length; i++)
            {
                var t = textures[i];
                packingList.Add(new PackingData(t.width, t.height, t.GetPixels(), false));
            }

            Rect[] rects = RectPacking.PackTextures(out resultInfo.Texture, packingList, false, true);
            resultInfo.Width  = resultInfo.Texture.width;
            resultInfo.Height = resultInfo.Texture.height;
            for (int i = 0; i < rects.Length; i++)
            {
                rects[i].x      *= resultInfo.Width;
                rects[i].y      *= resultInfo.Height;
                rects[i].width  *= resultInfo.Width;
                rects[i].height *= resultInfo.Height;
                rects[i].width   = rects[i].width - 1;
                rects[i].height  = rects[i].height - 1;
            }
            resultInfo.Rects = rects;
            return(resultInfo);
        }