Ejemplo n.º 1
0
        public static List <TexturedMesh> MultiCombineTextures(List <TexturedMesh> Texturedmeshes)
        {
            List <TexturedMesh> output = new List <TexturedMesh>();
            int maxwidth  = 2048;
            int maxheight = 2048;

            List <TextureBlock> TextureBlocks = new List <TextureBlock>();

            // add meshes to textureblocks
            foreach (TexturedMesh item in Texturedmeshes)
            {
                bool placed = false;
                foreach (TextureBlock tb in TextureBlocks)
                {
                    if (tb.Texturename == item.TextureName)
                    {
                        tb.meshes.Add(item.mesh);
                        placed = true;
                    }
                }

                if (placed == false)
                {
                    TextureBlocks.Add(new TextureBlock(item));
                }
            }

            //sort textures by decreasing height
            TextureBlocks = SortTextures(TextureBlocks);

            // add textures to shelfs
            List <Shelf> shelfs = new List <Shelf>();

            shelfs.Add(new Shelf(maxwidth));

            bool blockplaced = false;

            foreach (TextureBlock texblock in TextureBlocks)
            {
                foreach (Shelf shelf in shelfs)
                {
                    blockplaced = false;
                    blockplaced = shelf.AddTexture(texblock);
                }
                if (blockplaced == false)
                {
                    Shelf shelf = new Shelf(maxwidth);
                    shelf.AddTexture(texblock);
                    shelfs.Add(shelf);
                }
            }


            // combine shelfs into stacks of maxheight
            bool         finished = false;
            List <Shelf> combinedShelfs;
            List <int>   shelfids;

            while (finished == false)
            {
                combinedShelfs = new List <Shelf>();
                shelfids       = new List <int>();
                int totalheight = 0;
                for (int i = 0; i < shelfs.Count; i++)
                {
                    if (totalheight + shelfs[i].height < maxheight)
                    {
                        combinedShelfs.Add(shelfs[i]);
                        shelfids.Add(i);
                        totalheight += shelfs[i].height;
                    }
                }
                shelfids.Reverse();
                foreach (int item in shelfids)
                {
                    shelfs.RemoveAt(item);
                }

                output.Add(createTexturedMesh(combinedShelfs));

                if (shelfs.Count == 0)
                {
                    finished = true;
                }
            }


            return(output);
        }
Ejemplo n.º 2
0
        public static TexturedMesh CombineMeshes(List <TexturedMesh> Texturedmeshes)
        {
            double totalArea = 0;
            double widest    = 0;
            double highest   = 0;

            foreach (TexturedMesh item in Texturedmeshes)
            {
                totalArea += item.texture.width * item.texture.height;
                if (item.texture.width > widest)
                {
                    widest = item.texture.width;
                }
                if (item.texture.height > highest)
                {
                    highest = item.texture.height;
                }
            }
            int size = GetMinimumSize(totalArea, widest, highest);

            if (size == (int)totalArea)
            {
                Debug.Log("afbeeldingen te groot"); return(null);
            }


            List <TextureBlock> TextureBlocks = new List <TextureBlock>();

            // add meshes to textureblocks
            foreach (TexturedMesh item in Texturedmeshes)
            {
                bool placed = false;
                foreach (TextureBlock tb in TextureBlocks)
                {
                    if (tb.Texturename == item.TextureName)
                    {
                        tb.meshes.Add(item.mesh);
                        placed = true;
                    }
                }

                if (placed == false)
                {
                    TextureBlocks.Add(new TextureBlock(item));
                }
            }

            TextureBlocks = SortTextures(TextureBlocks);

            List <Shelf> shelfs = new List <Shelf>();

            shelfs.Add(new Shelf(size));

            bool blockplaced = false;

            foreach (TextureBlock texblock in TextureBlocks)
            {
                foreach (Shelf shelf in shelfs)
                {
                    blockplaced = false;
                    blockplaced = shelf.AddTexture(texblock);
                }
                if (blockplaced == false)
                {
                    Shelf shelf = new Shelf(size);
                    shelf.AddTexture(texblock);
                    shelfs.Add(shelf);
                }
            }
            int totalheight = 0;

            foreach (Shelf shelf in shelfs)
            {
                totalheight += shelf.height;
            }
            Texture2D TotaalTexture = new Texture2D(size, totalheight);
            int       baseheight    = 0;

            List <CombineInstance> submeshes = new List <CombineInstance>();

            //create the combined texture
            foreach (Shelf shelf in shelfs)
            {
                Color[] shelfpixels = shelf.ShelfTexture.GetPixels(0, 0, size, shelf.height, 0);
                TotaalTexture.SetPixels(0, baseheight, size, shelf.height, shelfpixels, 0);
                TotaalTexture.Apply(true);
                shelf.baseheight = baseheight;
                baseheight      += shelf.height;
            }
            foreach (Shelf shelf in shelfs)
            {
                List <CombineInstance> shelfmeshes = shelf.ReturnMeshes(baseheight);
                foreach (CombineInstance item in shelfmeshes)
                {
                    submeshes.Add(item);
                }
            }
            TexturedMesh output    = new TexturedMesh();
            Mesh         totalMesh = new Mesh();

            totalMesh.CombineMeshes(submeshes.ToArray(), true, false);
            foreach (var meshHolder in submeshes)
            {
                Destroy(meshHolder.mesh);
            }
            output.mesh    = totalMesh;
            output.texture = TotaalTexture;

            return(output);
        }