Exemplo n.º 1
0
        private void CopyAllocatedItemsToAtlas(List <TextureTools.AtlasItem> items, string atlasChain, int atlasId, Size size)
        {
            var atlasPath   = AssetCooker.GetAtlasPath(atlasChain, atlasId);
            var atlasPixels = new Color4[size.Width * size.Height];

            foreach (var item in items.Where(i => i.Allocated))
            {
                var atlasRect = item.AtlasRect;
                using (var bitmap = TextureTools.OpenAtlasItemBitmapAndRescaleIfNeeded(AssetCooker.Platform, item)) {
                    CopyPixels(bitmap, atlasPixels, atlasRect.A.X, atlasRect.A.Y, size.Width, size.Height);
                }
                var atlasPart = new TextureAtlasElement.Params {
                    AtlasRect = atlasRect,
                    AtlasPath = Path.ChangeExtension(atlasPath, null)
                };
                var srcPath = Path.ChangeExtension(item.Path, item.SourceExtension);
                InternalPersistence.Instance.WriteObjectToBundle(AssetCooker.OutputBundle, item.Path, atlasPart, Persistence.Format.Binary,
                                                                 item.SourceExtension, AssetCooker.InputBundle.GetFileLastWriteTime(srcPath), AssetAttributes.None, item.CookingRules.SHA1);
                // Delete non-atlased texture since now its useless
                var texturePath = Path.ChangeExtension(item.Path, AssetCooker.GetPlatformTextureExtension());
                if (AssetCooker.OutputBundle.FileExists(texturePath))
                {
                    AssetCooker.DeleteFileFromBundle(texturePath);
                }
                UserInterface.Instance.IncreaseProgressBar();
            }
            Console.WriteLine("+ " + atlasPath);
            var firstItem = items.First(i => i.Allocated);

            using (var atlas = new Bitmap(atlasPixels, size.Width, size.Height)) {
                AssetCooker.ImportTexture(atlasPath, atlas, firstItem.CookingRules, AssetCooker.InputBundle.GetFileLastWriteTime(atlasPath), CookingRulesSHA1: null);
            }
        }
Exemplo n.º 2
0
        private void BuildAtlasChain(string atlasChain)
        {
            for (var i = 0; i < AssetCooker.MaxAtlasChainLength; i++)
            {
                var atlasPath = AssetCooker.GetAtlasPath(atlasChain, i);
                if (AssetCooker.OutputBundle.FileExists(atlasPath))
                {
                    AssetCooker.DeleteFileFromBundle(atlasPath);
                }
                else
                {
                    break;
                }
            }
            var pluginItems = new Dictionary <string, List <TextureTools.AtlasItem> >();
            var items       = new Dictionary <AtlasOptimization, List <TextureTools.AtlasItem> >();

            items[AtlasOptimization.Memory]    = new List <TextureTools.AtlasItem>();
            items[AtlasOptimization.DrawCalls] = new List <TextureTools.AtlasItem>();
            foreach (var fileInfo in AssetBundle.Current.EnumerateFileInfos(null, textureExtension))
            {
                var cookingRules = AssetCooker.CookingRulesMap[fileInfo.Path];
                if (cookingRules.TextureAtlas == atlasChain)
                {
                    var item = new TextureTools.AtlasItem {
                        Path            = Path.ChangeExtension(fileInfo.Path, atlasPartExtension),
                        CookingRules    = cookingRules,
                        SourceExtension = Path.GetExtension(fileInfo.Path)
                    };
                    var bitmapInfo = TextureTools.BitmapInfo.FromFile(AssetCooker.InputBundle, fileInfo.Path);
                    if (bitmapInfo == null)
                    {
                        using (var bitmap = TextureTools.OpenAtlasItemBitmapAndRescaleIfNeeded(AssetCooker.Platform, item)) {
                            item.BitmapInfo = TextureTools.BitmapInfo.FromBitmap(bitmap);
                        }
                    }
                    else
                    {
                        var srcTexturePath = AssetPath.Combine(The.Workspace.AssetsDirectory, Path.ChangeExtension(item.Path, item.SourceExtension));
                        if (TextureTools.ShouldDownscale(AssetCooker.Platform, bitmapInfo, item.CookingRules))
                        {
                            TextureTools.DownscaleTextureInfo(AssetCooker.Platform, bitmapInfo, srcTexturePath, item.CookingRules);
                        }
                        // Ensure that no image exceeded maxAtlasSize limit
                        TextureTools.DownscaleTextureToFitAtlas(bitmapInfo, srcTexturePath);
                        item.BitmapInfo = bitmapInfo;
                    }
                    var k = cookingRules.AtlasPacker;
                    if (!string.IsNullOrEmpty(k) && k != "Default")
                    {
                        List <TextureTools.AtlasItem> l;
                        if (!pluginItems.TryGetValue(k, out l))
                        {
                            pluginItems.Add(k, l = new List <TextureTools.AtlasItem>());
                        }
                        l.Add(item);
                    }
                    else
                    {
                        items[cookingRules.AtlasOptimization].Add(item);
                    }
                }
            }
            var initialAtlasId = 0;

            foreach (var kv in items)
            {
                if (kv.Value.Any())
                {
                    if (AssetCooker.Platform == TargetPlatform.iOS)
                    {
                        Predicate <PVRFormat> isRequireSquare = (format) => {
                            return
                                (format == PVRFormat.PVRTC2 ||
                                 format == PVRFormat.PVRTC4 ||
                                 format == PVRFormat.PVRTC4_Forced);
                        };
                        var square    = kv.Value.Where(item => isRequireSquare(item.CookingRules.PVRFormat)).ToList();
                        var nonSquare = kv.Value.Where(item => !isRequireSquare(item.CookingRules.PVRFormat)).ToList();
                        initialAtlasId = PackItemsToAtlas(atlasChain, square, kv.Key, initialAtlasId, true);
                        initialAtlasId = PackItemsToAtlas(atlasChain, nonSquare, kv.Key, initialAtlasId, false);
                    }
                    else
                    {
                        initialAtlasId = PackItemsToAtlas(atlasChain, kv.Value, kv.Key, initialAtlasId, false);
                    }
                }
            }
            var packers = PluginLoader.CurrentPlugin.AtlasPackers.ToDictionary(i => i.Metadata.Id, i => i.Value);

            foreach (var kv in pluginItems)
            {
                if (!packers.ContainsKey(kv.Key))
                {
                    throw new InvalidOperationException($"Packer {kv.Key} not found");
                }
                initialAtlasId = packers[kv.Key](atlasChain, kv.Value, initialAtlasId);
            }
        }