public bool AddImage(Texture2D img, string name, Borders borders) { AtlasNode node = _root.Insert(img, borders); if (node == null) { return(false); } _rectangleMap.Add(name, node.Rectangle); try { Image.SetPixels((int)node.Rectangle.x, (int)node.Rectangle.y, (int)node.Rectangle.width, (int)node.Rectangle.height, img.GetPixels()); } catch (Exception exceptionMessage) { Debug.Log(exceptionMessage); InstallerAtlasProperties.SetReadable(img); Image.SetPixels((int)node.Rectangle.x, (int)node.Rectangle.y, (int)node.Rectangle.width, (int)node.Rectangle.height, img.GetPixels()); } if (borders.IsRepeat) { Color color = new Color(); for (int b = 0; b < borders.RepeatSize; b++) { for (int i = 0; i < img.width; i++) { color = img.GetPixel(i, 0); Image.SetPixel(i + (int)node.Rectangle.x, (int)node.Rectangle.y - b - 1, color); color = img.GetPixel(i, img.height - 1); Image.SetPixel(i + (int)node.Rectangle.x, (int)node.Rectangle.y + (int)node.Rectangle.height + b, color); } } for (int b = 0; b < borders.RepeatSize; b++) { for (int i = 0; i < img.height; i++) { color = img.GetPixel(0, i); Image.SetPixel((int)node.Rectangle.x - b - 1, (int)node.Rectangle.y + i, color); color = img.GetPixel(img.width - 1, i); Image.SetPixel((int)node.Rectangle.x + (int)node.Rectangle.width + b, (int)node.Rectangle.y + i, color); } } } if (Image == null) { Debug.LogError("Image null!"); } return(true); }
public AtlasTexture(int width, int height, Borders borders = null) { if (borders == null) { borders = new Borders(); } Image = new Texture2D(width, height); Color32 col = new Color32(255, 255, 255, 0); Color32[] pixels = Image.GetPixels32(); for (int i = 0; i < pixels.Length; ++i) { pixels[i] = col; } Image.SetPixels32(pixels); Image.Apply(); _root = new AtlasNode(borders.Left, borders.Bottom, width - borders.Left, height - borders.Bottom); }
public AtlasNode Insert(Texture2D image, Borders borders) { //if we're not a leaf then if (!IsLeaf()) { //try inserting into first child AtlasNode newNode = Child[0].Insert(image, borders); if (newNode != null) { return(newNode); } //no room, insert into second return(Child[1].Insert(image, borders)); } else { //if there's already a texture here, return if (this.Image != null) { return(null); } //if we're too small, return if (image.width > Rectangle.width || image.height > Rectangle.height) { return(null); } //if we're just right, accept if (image.width == Rectangle.width && image.height == Rectangle.height) { this.Image = image; return(this); } //decide which way to split int dw = (int)Rectangle.width - image.width - borders.Width; int dh = (int)Rectangle.height - image.height - borders.Height; //create some kids if (dw > dh) { Child[0] = new AtlasNode( (int)Rectangle.x, (int)Rectangle.y, (int)image.width, (int)Rectangle.height); Child[1] = new AtlasNode( (int)Rectangle.x + (int)image.width + borders.Width, (int)Rectangle.y, (int)Rectangle.width - (int)image.width - borders.Width, (int)Rectangle.height); } else { Child[0] = new AtlasNode( (int)Rectangle.x, (int)Rectangle.y, (int)Rectangle.width, (int)image.height); Child[1] = new AtlasNode( (int)Rectangle.x, (int)Rectangle.y + (int)image.height + borders.Height, (int)Rectangle.width, (int)Rectangle.height - (int)image.height - borders.Height); } //insert into first child we created return(Child[0].Insert(image, borders)); } }