private void mainProgressLoop(ProgressBar progress, Texture2D[] packedTextures, AtlasUvs channelMapping) { progress.Begin(5, "", "", () => { foreach (var channel in MeshUtil.allUvChannels) { progress.Begin(1, "", channel + ": ", () => { doPerChannelPack(progress, channel, packedTextures, channelMapping); }); } finalizeTextures(progress, packedTextures); }); }
private void doPerChannelPack(ProgressBar progress, UVChannelFlags channel, Texture2D[] packedTextures, AtlasUvs channelMapping) { var mainTextureFeature = _features.Query().FirstOrDefault(f => f.channel == channel); if (mainTextureFeature == null) { return; } Texture2D defaultTexture, packedTexture; Texture2D[] rawTextureArray, processedTextureArray; progress.Step("Prepare " + channel); prepareForPacking(mainTextureFeature, out defaultTexture, out packedTexture, out rawTextureArray, out processedTextureArray); progress.Step("Pack " + channel); var packedRects = packedTexture.PackTextures(processedTextureArray, _padding, _maxAtlasSize, makeNoLongerReadable: false); packedTexture.Apply(updateMipmaps: true, makeNoLongerReadable: true); packedTextures[_features.IndexOf(mainTextureFeature)] = packedTexture; packSecondaryTextures(progress, channel, mainTextureFeature, packedTexture, packedRects, packedTextures); //Correct uvs to account for the added border for (int i = 0; i < packedRects.Length; i++) { float dx = 1.0f / packedTexture.width; float dy = 1.0f / packedTexture.height; Rect r = packedRects[i]; if (processedTextureArray[i] != defaultTexture) { dx *= _border; dy *= _border; } r.x += dx; r.y += dy; r.width -= dx * 2; r.height -= dy * 2; packedRects[i] = r; } for (int i = 0; i < rawTextureArray.Length; i++) { channelMapping.SetRect(channel.Index(), rawTextureArray[i], packedRects[i]); } }
/// <summary> /// Actually perform the build for the atlas. This method outputs the atlas textures, and the atlas /// uvs that map textures into the atlas. This method takes in a progress bar so that the atlas /// process can be tracked visually, since it can take quite a bit of time when there are a lot of /// textures to pack. /// </summary> public void RebuildAtlas(ProgressBar progress, out Texture2D[] packedTextures, out AtlasUvs channelMapping) { if (!Utils.IsCompressible(_format)) { Debug.LogWarning("Format " + _format + " is not compressible! Using ARGB32 instead."); _format = TextureFormat.ARGB32; } _atlasHash = _currHash; packedTextures = new Texture2D[_features.Count]; channelMapping = new AtlasUvs(); mainProgressLoop(progress, packedTextures, channelMapping); //Clear cache foreach (var texture in _cachedProcessedTextures.Values) { UnityEngine.Object.DestroyImmediate(texture); } _cachedProcessedTextures.Clear(); foreach (var texture in _cachedDefaultTextures.Values) { UnityEngine.Object.DestroyImmediate(texture); } _cachedDefaultTextures.Clear(); }