private void OptimizeAtlas() { UMAData.AtlasList umaAtlasList = umaData.atlasList; for (int atlasIndex = 0; atlasIndex < umaAtlasList.atlas.Count; atlasIndex++) { Vector2 usedArea = new Vector2(0, 0); for (int atlasElementIndex = 0; atlasElementIndex < umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions.Count; atlasElementIndex++) { if (umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions[atlasElementIndex].atlasRegion.xMax > usedArea.x) { usedArea.x = umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions[atlasElementIndex].atlasRegion.xMax; } if (umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions[atlasElementIndex].atlasRegion.yMax > usedArea.y) { usedArea.y = umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions[atlasElementIndex].atlasRegion.yMax; } } Vector2 tempResolution = new Vector2(umaGenerator.atlasResolution, umaGenerator.atlasResolution); bool done = false; while (!done) { if (tempResolution.x * 0.5f >= usedArea.x) { tempResolution = new Vector2(tempResolution.x * 0.5f, tempResolution.y); } else { done = true; } } done = false; while (!done) { if (tempResolution.y * 0.5f >= usedArea.y) { tempResolution = new Vector2(tempResolution.x, tempResolution.y * 0.5f); } else { done = true; } } umaAtlasList.atlas[atlasIndex].cropResolution = tempResolution; } }
private void UpdateUV() { UMAData.AtlasList umaAtlasList = umaData.atlasList; for (int atlasIndex = 0; atlasIndex < umaAtlasList.atlas.Count; atlasIndex++) { Vector2 finalAtlasAspect = new Vector2(umaGenerator.atlasResolution / umaAtlasList.atlas[atlasIndex].cropResolution.x, umaGenerator.atlasResolution / umaAtlasList.atlas[atlasIndex].cropResolution.y); for (int atlasElementIndex = 0; atlasElementIndex < umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions.Count; atlasElementIndex++) { Rect tempRect = umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions[atlasElementIndex].atlasRegion; tempRect.xMin = tempRect.xMin * finalAtlasAspect.x; tempRect.xMax = tempRect.xMax * finalAtlasAspect.x; tempRect.yMin = tempRect.yMin * finalAtlasAspect.y; tempRect.yMax = tempRect.yMax * finalAtlasAspect.y; umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions[atlasElementIndex].atlasRegion = tempRect; } } }
private void CalculateRects() { Rect nullRect = new Rect(0, 0, 0, 0); UMAData.AtlasList umaAtlasList = umaData.atlasList; for (int atlasIndex = 0; atlasIndex < umaAtlasList.atlas.Count; atlasIndex++) { umaAtlasList.atlas[atlasIndex].cropResolution = new Vector2(umaGenerator.atlasResolution, umaGenerator.atlasResolution); umaAtlasList.atlas[atlasIndex].resolutionScale = atlasResolutionScale; umaAtlasList.atlas[atlasIndex].mipmap = mipMapAdjust; packTexture.Init(umaGenerator.atlasResolution, umaGenerator.atlasResolution, false); bool textureFit = true; for (int atlasElementIndex = 0; atlasElementIndex < umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions.Count; atlasElementIndex++) { UMAData.AtlasMaterialDefinition tempMaterialDef = umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions[atlasElementIndex]; if (tempMaterialDef.atlasRegion == nullRect) { tempMaterialDef.atlasRegion = packTexture.Insert(Mathf.FloorToInt(tempMaterialDef.source.baseTexture[0].width * umaAtlasList.atlas[atlasIndex].resolutionScale * tempMaterialDef.source.slotData.overlayScale), Mathf.FloorToInt(tempMaterialDef.source.baseTexture[0].height * umaAtlasList.atlas[atlasIndex].resolutionScale * tempMaterialDef.source.slotData.overlayScale), MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestLongSideFit); tempMaterialDef.isRectShared = false; umaAtlasList.atlas[atlasIndex].shader = tempMaterialDef.source.materialSample.shader; if (tempMaterialDef.atlasRegion == nullRect) { textureFit = false; if (umaGenerator.fitAtlas) { Debug.LogWarning("Atlas resolution is too small, Textures will be reduced."); } else { Debug.LogError("Atlas resolution is too small, not all textures will fit."); } } for (int atlasElementIndex2 = atlasElementIndex; atlasElementIndex2 < umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions.Count; atlasElementIndex2++) { if (atlasElementIndex != atlasElementIndex2) { if (tempMaterialDef.source.baseTexture[0] == umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions[atlasElementIndex2].source.baseTexture[0]) { umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions[atlasElementIndex2].atlasRegion = tempMaterialDef.atlasRegion; umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions[atlasElementIndex2].isRectShared = true; } } } } if (!textureFit && umaGenerator.fitAtlas) { //Reset calculation and reduce texture sizes textureFit = true; atlasElementIndex = -1; umaAtlasList.atlas[atlasIndex].resolutionScale = umaAtlasList.atlas[atlasIndex].resolutionScale * 0.5f; umaAtlasList.atlas[atlasIndex].mipmap++; packTexture.Init(umaGenerator.atlasResolution, umaGenerator.atlasResolution, false); for (int atlasElementIndex2 = 0; atlasElementIndex2 < umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions.Count; atlasElementIndex2++) { umaAtlasList.atlas[atlasIndex].atlasMaterialDefinitions[atlasElementIndex2].atlasRegion = nullRect; } } } } }