public bool Insert(ImagePacker.ImagePackRect insert, int padding) { int insertWidth = insert.rect.width + padding * 2; int insertHeight = insert.rect.height + padding * 2; if (insertWidth > rect.width || insertHeight > rect.height) { return(false); } if (left == null) { left = new ImagePackNode(); left.rect = new RectInt(rect.x, rect.y, rect.width, insertHeight); right = new ImagePackNode(); right.rect = new RectInt(rect.x, rect.y + insertHeight, rect.width, rect.height - insertHeight); } if (!left.InsertNotRoot(insert, padding)) { return(right.InsertNotRoot(insert, padding)); } return(true); }
static bool SplitRects(ImagePackNode node, ImagePacker.ImagePackRect insert, int padding, out ImagePackNode left, out ImagePackNode right) { // Find the best way to split the rect based on a new rect left = right = null; var tryRects = new[] { new ImagePackNode(), new ImagePackNode(), new ImagePackNode(), new ImagePackNode() }; tryRects[0].rect = new RectInt(node.rect.x + node.imageWidth.x, node.rect.y, node.rect.width - node.imageWidth.x, node.rect.height); tryRects[1].rect = new RectInt(node.rect.x, node.rect.y + node.imageWidth.y, node.imageWidth.x, node.rect.height - node.imageWidth.y); tryRects[2].rect = new RectInt(node.rect.x, node.rect.y + node.imageWidth.y, node.rect.width, node.rect.height - node.imageWidth.y); tryRects[3].rect = new RectInt(node.rect.x + node.imageWidth.x, node.rect.y, node.rect.width - node.imageWidth.x, node.imageWidth.y); float smallestSpace = float.MinValue; for (int i = 0; i < tryRects.GetLength(0); ++i) { //for (int j = 0; j < tryRects.GetLength(1); ++j) { Vector2Int newSpaceLeft; if (tryRects[i].TryInsert(insert, padding, out newSpaceLeft)) { if (smallestSpace < newSpaceLeft.sqrMagnitude) { smallestSpace = newSpaceLeft.sqrMagnitude; int index = i / 2 * 2; left = tryRects[index]; right = tryRects[index + 1]; } } } } return(left != null); }
public void Visit(ImagePackNode node) { if (node.imageId == -1) { emptyAreas.Add(node.rect); } }
public void Visit(ImagePackNode node) { if (node.imageId != -1) { if (positions.Length < node.imageId + 1) { var p = positions; Array.Resize(ref p, node.imageId + 1); positions = p; } positions[node.imageId].x = node.rect.x; positions[node.imageId].y = node.rect.y; } }
static ImagePackNode InternalPack(RectInt[] rects, int padding) { if (rects == null || rects.Length == 0) { return new ImagePackNode() { rect = new RectInt(0, 0, 0, 0) } } ; var sortedRects = new ImagePackRect[rects.Length]; for (int i = 0; i < rects.Length; ++i) { sortedRects[i] = new ImagePackRect(); sortedRects[i].rect = rects[i]; sortedRects[i].index = i; } Array.Sort <ImagePackRect>(sortedRects); var root = new ImagePackNode(); root.rect = new RectInt(0, 0, (int)NextPowerOfTwo((ulong)rects[0].width), (int)NextPowerOfTwo((ulong)rects[0].height)); for (int i = 0; i < rects.Length; ++i) { if (!root.Insert(sortedRects[i], padding)) // we can't fit { int newWidth = root.rect.width, newHeight = root.rect.height; if (root.rect.width < root.rect.height) { newWidth = (int)NextPowerOfTwo((ulong)root.rect.width + 1); } else { newHeight = (int)NextPowerOfTwo((ulong)root.rect.height + 1); } // Reset all packing and try again root = new ImagePackNode(); root.rect = new RectInt(0, 0, newWidth, newHeight); i = -1; } } return(root); }
static ImagePackNode InternalPack(RectInt[] rects, int padding) { var sortedRects = new ImagePackRect[rects.Length]; for (int i = 0; i < rects.Length; ++i) { sortedRects[i] = new ImagePackRect(); sortedRects[i].rect = rects[i]; sortedRects[i].index = i; } Array.Sort <ImagePackRect>(sortedRects); var root = new ImagePackNode(); root.rect = new RectInt(0, 0, 32, 32); for (int i = 0; i < rects.Length; ++i) { if (!root.Insert(sortedRects[i], padding)) // we can't fit { int newWidth = 0, newHeight = 0; if (root.rect.width < root.rect.height) { newWidth = (int)NextPowerOfTwo((ulong)root.rect.width + 1); } else { newHeight = (int)NextPowerOfTwo((ulong)root.rect.height + 1); } if (newWidth > k_MaxTextureSize || newHeight > k_MaxTextureSize) { Debug.LogAssertion("unable to pack, reached max texture size"); break; } root.AdjustSize(newWidth, newHeight); --i; } } return(root); }
bool InsertNotRoot(ImagePacker.ImagePackRect insert, int padding) { int insertWidth = insert.rect.width + padding * 2; int insertHeight = insert.rect.height + padding * 2; if (insertWidth > rect.width || insertHeight > rect.height) { return(false); } if (imageId == -1) { imageId = insert.index; var dw = rect.width - insertWidth; var dh = rect.height - insertHeight; left = new ImagePackNode(); right = new ImagePackNode(); if (dw > dh) { left.rect = new RectInt(rect.x + insertWidth, rect.y, rect.width - insertWidth, rect.height); right.rect = new RectInt(rect.x, rect.y + insertHeight, insertWidth, rect.height - insertHeight); } else { left.rect = new RectInt(rect.x, rect.y + insertHeight, rect.width, rect.height - insertHeight); right.rect = new RectInt(rect.x + insertWidth, rect.y, rect.width - insertWidth, insertHeight); } } else { if (!left.InsertNotRoot(insert, padding)) { return(right.InsertNotRoot(insert, padding)); } } return(true); }