public static bool Generate(D2D_Destructible destructible, D2D_SpriteSplitOrder splitOrder) { cells.Clear(); if (destructible != null && destructible.AlphaTex != null) { target = destructible; tex = target.AlphaTex; width = tex.width; height = tex.height; total = width * height; pixels = tex.GetPixels32(); if (cells.Capacity < total) { cells.Capacity = total; } var threshold = (byte)(target.SplitThreshold * 255.0f); for (var i = 0; i < total; i++) { cells.Add(pixels[i].a >= threshold); } fills.Clear(); var validFillCount = 0; for (var i = 0; i < total; i++) { if (cells[i] == true) { currentFill = new Fill(); fills.Add(currentFill); currentFill.XMin = currentFill.XMax = i % width; currentFill.YMin = currentFill.YMax = i / width; BeginFloodFill(i, currentFill.XMin, currentFill.YMin); // Skip the first floodfill if (currentFill.Count >= target.SplitMinPixels) { currentFill.Valid = true; validFillCount += 1; } } } // Can we split? if (validFillCount > 1) { var firstSet = false; switch (splitOrder) { case D2D_SpriteSplitOrder.KeepLargest: fills.Sort((a, b) => b.Count.CompareTo(a.Count)); break; case D2D_SpriteSplitOrder.KeepSmallest: fills.Sort((a, b) => a.Count.CompareTo(b.Count)); break; } foreach (var fill in fills) { if (fill.Valid == true) { if (firstSet == false) { firstSet = true; Split(destructible, fill, false); } else { var clonedGameObject = D2D_Helper.CloneGameObject(destructible.gameObject, destructible.transform.parent); var clonedDestructible = clonedGameObject.GetComponent <D2D_Destructible>(); Split(clonedDestructible, fill, true); } } } return(true); } if (validFillCount == 0) { D2D_Helper.Destroy(destructible.gameObject); } } return(false); }
public static bool Generate(D2D_Destructible destructible, D2D_SpriteSplitOrder splitOrder) { cells.Clear(); if (destructible != null && destructible.AlphaTex != null) { target = destructible; tex = target.AlphaTex; width = tex.width; height = tex.height; total = width * height; pixels = tex.GetPixels32(); if (cells.Capacity < total) { cells.Capacity = total; } var threshold = (byte)(target.SplitThreshold * 255.0f); for (var i = 0; i < total; i++) { cells.Add(pixels[i].a >= threshold); } fills.Clear(); var validFillCount = 0; for (var i = 0; i < total; i++) { if (cells[i] == true) { currentFill = new Fill(); fills.Add(currentFill); currentFill.XMin = currentFill.XMax = i % width; currentFill.YMin = currentFill.YMax = i / width; BeginFloodFill(i, currentFill.XMin, currentFill.YMin); // Skip the first floodfill if (currentFill.Count >= target.SplitMinPixels) { currentFill.Valid = true; validFillCount += 1; } } } // Can we split? if (validFillCount > 1) { var firstSet = false; switch (splitOrder) { case D2D_SpriteSplitOrder.KeepLargest: fills.Sort((a, b) => b.Count.CompareTo(a.Count)); break; case D2D_SpriteSplitOrder.KeepSmallest: fills.Sort((a, b) => a.Count.CompareTo(b.Count)); break; } foreach (var fill in fills) { if (fill.Valid == true) { if (firstSet == false) { firstSet = true; Split(destructible, fill, false); } else { var clonedGameObject = D2D_Helper.CloneGameObject(destructible.gameObject, destructible.transform.parent); var clonedDestructible = clonedGameObject.GetComponent<D2D_Destructible>(); Split(clonedDestructible, fill, true); } } } return true; } if (validFillCount == 0) { D2D_Helper.Destroy(destructible.gameObject); } } return false; }