public static TileSheet GenerateTileSheet(List <SpriteData> spritesList, PaletteData paletteData, int padding) { var widest = 0; var tallest = 0; foreach (var s in spritesList) { if (s.Width > widest) { widest = s.Width; } if (s.Height > tallest) { tallest = s.Height; } } // Calculate number of rows and columns. We need this to be an even number for the shader to work var rowCount = Mathf.CeilToInt(Mathf.Sqrt(spritesList.Count)); if (rowCount % 2 != 0) { rowCount++; } var w = rowCount * widest; var h = rowCount * tallest; w += rowCount * padding * 2; h += rowCount * padding * 2; var spriteSheet = new TileSheet(); spriteSheet.Texture = new Texture2D(w, h); spriteSheet.Rows = rowCount; spriteSheet.Columns = rowCount; spriteSheet.TileWidth = widest; spriteSheet.TileHeight = tallest; spriteSheet.Padding = padding; int index = 0; foreach (var s in spritesList) { var tex = GenerateSprite(s, paletteData); var destX = (widest + padding * 2) * (index % rowCount) + padding; var destY = (tallest + padding * 2) * (index / rowCount) + padding; var source = tex.GetPixels(); spriteSheet.Texture.SetPixels(destX, destY, tex.width, tex.height, source); // Padding for (var i = 1; i <= padding; i++) { for (var x = 0; x < tex.width; x++) { spriteSheet.Texture.SetPixel(destX + x, destY - i, tex.GetPixel(x, 0)); spriteSheet.Texture.SetPixel(destX + x, (destY + (tex.height - 1)) + i, tex.GetPixel(x, tex.height - 1)); } for (var y = 0; y < tex.height; y++) { spriteSheet.Texture.SetPixel(destX - i, (destY) + y, tex.GetPixel(0, y)); spriteSheet.Texture.SetPixel((destX + (tex.width - 1)) + i, (destY) + y, tex.GetPixel(tex.width - 1, y)); } } for (var x = 0; x < padding; x++) { for (var y = 0; y < padding; y++) { spriteSheet.Texture.SetPixel((destX - padding) + x, (destY - 2) + y, tex.GetPixel(0, 0)); spriteSheet.Texture.SetPixel((destX + tex.width) + x, (destY - 2) + y, tex.GetPixel(tex.width - 1, 0)); spriteSheet.Texture.SetPixel((destX + tex.width) + x, (destY + tex.height) + y, tex.GetPixel(tex.width - 1, tex.height - 1)); spriteSheet.Texture.SetPixel((destX - padding) + x, (destY + tex.height) + y, tex.GetPixel(0, tex.height - 1)); } } spriteSheet.LookupTable.Add(s.Name, new TileSheetSprite() { TileNum = index, Width = tex.width, Height = tex.height }); index++; } spriteSheet.Texture.Apply(); return(spriteSheet); }
public static TileSheet GenerateWallTextureSheet(List <WallTextureData> textureList, Dictionary <string, SpriteData> patches, PaletteData paletteData, int padding) { var widest = 0; var tallest = 0; foreach (var t in textureList) { if (t.Width > widest) { widest = t.Width; } if (t.Height > tallest) { tallest = t.Height; } } var largest = widest > tallest ? widest : tallest; var rowCount = Mathf.CeilToInt(Mathf.Sqrt(textureList.Count)); if (rowCount % 2 != 0) { rowCount++; } var w = rowCount * largest; var h = rowCount * largest; w += rowCount * padding * 2; h += rowCount * padding * 2; var spriteSheet = new TileSheet(); spriteSheet.Rows = rowCount; spriteSheet.Columns = rowCount; spriteSheet.TileWidth = largest; spriteSheet.TileHeight = largest; spriteSheet.Padding = padding; spriteSheet.Texture = new Texture2D(w, h); int index = 0; foreach (var t in textureList) { var tex = GenerateWallTexture(t, patches, paletteData); var destX = (largest + padding * 2) * (index % rowCount) + padding; var destY = (largest + padding * 2) * (index / rowCount) + padding; //Debug.Log(t.Name); var source = tex.GetPixels(); spriteSheet.Texture.SetPixels(destX, destY, tex.width, tex.height, source); // Padding for (var i = 1; i <= padding; i++) { for (var x = 0; x < tex.width; x++) { spriteSheet.Texture.SetPixel(destX + x, destY - i, tex.GetPixel(x, 0)); spriteSheet.Texture.SetPixel(destX + x, (destY + (tex.height - 1)) + i, tex.GetPixel(x, tex.height - 1)); } for (var y = 0; y < tex.height; y++) { spriteSheet.Texture.SetPixel(destX - i, (destY) + y, tex.GetPixel(0, y)); spriteSheet.Texture.SetPixel((destX + (tex.width - 1)) + i, (destY) + y, tex.GetPixel(tex.width - 1, y)); } } for (var x = 0; x < padding; x++) { for (var y = 0; y < padding; y++) { spriteSheet.Texture.SetPixel((destX - padding) + x, (destY - 2) + y, tex.GetPixel(0, 0)); spriteSheet.Texture.SetPixel((destX + tex.width) + x, (destY - 2) + y, tex.GetPixel(tex.width - 1, 0)); spriteSheet.Texture.SetPixel((destX + tex.width) + x, (destY + tex.height) + y, tex.GetPixel(tex.width - 1, tex.height - 1)); spriteSheet.Texture.SetPixel((destX - padding) + x, (destY + tex.height) + y, tex.GetPixel(0, tex.height - 1)); } } spriteSheet.LookupTable.Add(t.Name, new TileSheetSprite() { TileNum = index, Width = tex.width, Height = tex.height }); index++; } spriteSheet.Texture.Apply(); return(spriteSheet); }