private void OptimizeAtlas()
{
for (int atlasIndex = 0; atlasIndex < atlassedMaterials.Count; atlasIndex++)
{
var material = atlassedMaterials[atlasIndex];
Vector2 usedArea = new Vector2(0, 0);
for (int atlasElementIndex = 0; atlasElementIndex < material.materialFragments.Count; atlasElementIndex++)
{
if (material.materialFragments[atlasElementIndex].atlasRegion.xMax > usedArea.x)
{
usedArea.x = material.materialFragments[atlasElementIndex].atlasRegion.xMax;
}
if (material.materialFragments[atlasElementIndex].atlasRegion.yMax > usedArea.y)
{
usedArea.y = material.materialFragments[atlasElementIndex].atlasRegion.yMax;
}
}
//Headless mode ends up with zero usedArea
if (Mathf.Approximately(usedArea.x, 0f) || Mathf.Approximately(usedArea.y, 0f))
{
material.cropResolution = new Vector2(0.0f, 0.0f);
return;
}
Vector2 tempResolution = new Vector2(umaGenerator.atlasResolution, umaGenerator.atlasResolution);
bool done = false;
while (!done && Mathf.Abs(usedArea.x) > 0.0001)
{
if (tempResolution.x * 0.5f >= usedArea.x)
{
tempResolution = new Vector2(tempResolution.x * 0.5f, tempResolution.y);
}
else
{
done = true;
}
}
done = false;
while (!done && Mathf.Abs(usedArea.y) > 0.0001)
{
if (tempResolution.y * 0.5f >= usedArea.y)
{
tempResolution = new Vector2(tempResolution.x, tempResolution.y * 0.5f);
}
else
{
done = true;
}
}
material.cropResolution = tempResolution;
}
}