private void CreateImg(AtlasItem _item, Texture2D _tx) { Texture2D txAtlas = _tx; int hMax = (int)_item.rect.height; int wMax = (int)_item.rect.width; // 最接近hMax的2的幂值 hMax = Mathf.NextPowerOfTwo(hMax); wMax = Mathf.NextPowerOfTwo(wMax); Helper.Debug(string.Format("创建图片:{0},H:{1},W:{2}", _item.name, hMax, wMax)); // 获取制定Rect区域的像素值 Color[] pColor = txAtlas.GetPixels((int)_item.rect.x, (int)_item.rect.y, (int)_item.rect.width, (int)_item.rect.height); Texture2D txImg = new Texture2D(wMax, hMax); txImg.SetPixels(0, 0, wMax, hMax, pColor); // 我日,这个是必须的,否则这个Texture是坏损的!!!!!! // 为什么没有Apply(),但是保存出来还是对的? 因为执行EncodeToJPG()的时候,默认执行了Apply() txImg.Apply(); string strPath = Application.streamingAssetsPath + "/" + _item.name; File.WriteAllBytes(strPath, txImg.EncodeToJPG()); Sprite spr = Sprite.Create(txImg, new Rect(0, 0, wMax, hMax), Vector2.one, 96); m_img.sprite = spr; }
private void CheckConfluenceLink(AtlasMark linkMark, AtlasItem link, Action <string> report) { var cflMeta = (dynamic)linkMark.Attrs["__confluenceMetadata"]; var linkType = (string)cflMeta.linkType; var linkText = link.Text.Trim(); if (linkType == "page") { var targetPageTitle = ((string)cflMeta.contentTitle).Trim(); if (linkText != targetPageTitle) { report($"Link '{link.Text}' is different from target Confluence page '{targetPageTitle}'"); } } else if (linkType == "attachment") { var targetFileName = ((string)cflMeta.fileName).Trim(); if (linkText != targetFileName) { report($"Link '{link.Text}' is different from target attachment file name '{targetFileName}'"); } } else if (linkType == "self") { // not much we can do here } else { report($"Link '{linkText}' has unrecognized link type '{linkType}'"); } }
public void PreLoadAlphaTexture(string textureName) { if (string.IsNullOrEmpty(textureName)) { return; } string alphaTexuteName = ""; if (UIAtlasInfo.textureMap.TryGetValue(textureName, out alphaTexuteName)) { } else { return; } AtlasItem _atlasItem; if (atlasDic.TryGetValue(alphaTexuteName, out _atlasItem)) { } else { atlasDic[alphaTexuteName] = new AtlasItem(alphaTexuteName); } }
private static string GetElementText(AtlasItem x) { if (x.Type == "hardBreak") { return(" "); } return(x.Text); }
/// <summary> /// Gets details of a particular atlas item. /// If texture does not exist in atlas an empty item is returned. /// </summary> /// <param name="key">Key of item.</param> /// <returns>AtlasItem. Key member contains -1 if item not present.</returns> public AtlasItem GetAtlasItem(int key) { AtlasItem item = new AtlasItem(); if (!atlasItems.TryGetValue(key, out item)) { item.key = -1; } return(item); }
private static void CheckExternalLink(AtlasMark linkMark, AtlasItem link, Action <string> report) { var href = (string)linkMark.Attrs["href"]; var linkText = link.Text.Trim(); if (href != linkText) { report($"Link '{linkText}' is different from underlying URL '{href}'"); } }
/// <summary> /// Rebuilds source textures into new atlas. /// </summary> public void Rebuild(int borderSize) { // Create sequential array of texture references int index = 0; Texture2D[] textureRefs = new Texture2D[textureItems.Count]; foreach (var ti in textureItems) { textureRefs[index++] = ti.Value.texture; } // Create atlas texture atlasTexture = new Texture2D(maxAtlasDim, maxAtlasDim, TextureFormat.RGBA32, mipMaps); Rect[] rects = atlasTexture.PackTextures(textureRefs, padding, maxAtlasDim, !stayReadable); // Shrink UV rect to compensate for internal border if (borderSize > 0) { float ru = 1f / atlasTexture.width; float rv = 1f / atlasTexture.height; for (int i = 0; i < rects.Length; i++) { Rect rct = rects[i]; rct.xMin += borderSize * ru; rct.xMax -= borderSize * ru; rct.yMin += borderSize * rv; rct.yMax -= borderSize * rv; rects[i] = rct; } } // Rebuild dict index = 0; atlasItems.Clear(); foreach (var ti in textureItems) { // Create atlas item AtlasItem item = new AtlasItem() { key = ti.Key, rect = rects[index], textureItem = ti.Value, }; atlasItems.Add(ti.Key, item); index++; } // Promote to new material GetMaterial(); }
public bool SetAtlasMaterial(Image image, string textureName, bool isGray = false) { if (string.IsNullOrEmpty(textureName)) { return(false); } string alphaTexuteName = ""; if (UIAtlasInfo.textureMap.TryGetValue(textureName, out alphaTexuteName)) { } else { return(false); } UIAtlasInfo.AlphaChannel channel; if (UIAtlasInfo.textureChannelMap.TryGetValue(textureName, out channel)) { } if (isGray) { channel = (UIAtlasInfo.AlphaChannel)(3 + (int)channel); } AtlasItem _atlasItem; if (atlasDic.TryGetValue(alphaTexuteName, out _atlasItem)) { if (_atlasItem.Alphatexture != null) { _atlasItem.SetMaterial(image, channel); } else { _atlasItem.AddWaiteList(new ImageItem(image, channel)); } } else { atlasDic[alphaTexuteName] = new AtlasItem(alphaTexuteName, new ImageItem(image, channel)); } return(true); }
void ShowGlyphLocation(TempGlyphMap tmpGlyphMap) { if (_pic1Gfx == null) { _pic1Gfx = pictureBox1.CreateGraphics(); } _pic1Gfx.Clear(pictureBox1.BackColor); _pic1Gfx.DrawImage(_atlasBmp, 0, 0); AtlasItem glyphMap = tmpGlyphMap.glyphMap; _pic1Gfx.DrawRectangle(Pens.Red, new Rectangle { X = glyphMap.Left, Y = glyphMap.Top, Width = glyphMap.Width, Height = glyphMap.Height }); }
public void OnLoadImgByAtlas() { m_nIndex = m_nIndex > m_pAtlas.Count - 1 ? 0 : m_nIndex; //m_nIndex = 0; AtlasItem item = m_pAtlas[m_nIndex++]; string strAtlasPath = m_strAtlasPath + "/" + item.atlas; MyWWWMgr.Instance.GetResByWWW("file:///" + strAtlasPath, (w) => { CreateImg(item, w.texture); }, (resp) => { Helper.Debug("加载图集:" + strAtlasPath + "失败!"); }); }
private void NoteAfterHardBreak(AtlasItem paragraph, Action <string> report) { var parts = SplitAtHardBreaks(paragraph.Content); foreach (var part in parts) { var textItems = part .Where(x => x.Type == "text" && !x.HasMark("code")); var text = string.Join(" ", textItems.Select(x => x.Text)); if (Beginnings.Any(x => StartsWith(text, x))) { report(text); } } }
/// <summary> /// Checks whether two items can be packed to the same texture /// </summary> public static bool AreAtlasItemsCompatible(List <AtlasItem> items, AtlasItem item1, AtlasItem item2) { if (item1.CookingRules.WrapMode != item2.CookingRules.WrapMode) { return(false); } if (item1.CookingRules.MinFilter != item2.CookingRules.MinFilter) { return(false); } if (item1.CookingRules.MagFilter != item2.CookingRules.MagFilter) { return(false); } if (item1.CookingRules.MipMaps != item2.CookingRules.MipMaps) { return(false); } if (items.Count > 0) { if (item1.CookingRules.WrapMode != TextureWrapMode.Clamp || item2.CookingRules.WrapMode != TextureWrapMode.Clamp) { return(false); } } switch (Platform) { case TargetPlatform.Android: case TargetPlatform.iOS: return(item1.CookingRules.PVRFormat == item2.CookingRules.PVRFormat && item1.Bitmap.HasAlpha == item2.Bitmap.HasAlpha); case TargetPlatform.Win: case TargetPlatform.Mac: return(item1.CookingRules.DDSFormat == item2.CookingRules.DDSFormat); case TargetPlatform.Unity: return(true); default: throw new ArgumentException(); } }
private static void PackItemsToAtlas(List <AtlasItem> items, Size size, out double packRate) { items.ForEach(i => i.Allocated = false); // Take in account 1 pixel border for each side. var a = new RectAllocator(new Size(size.Width + 2, size.Height + 2)); AtlasItem firstAllocatedItem = null; foreach (var item in items) { var sz = new Size(item.Bitmap.Width + 2, item.Bitmap.Height + 2); if (firstAllocatedItem == null || AreAtlasItemsCompatible(items, firstAllocatedItem, item)) { if (a.Allocate(sz, out item.AtlasRect)) { item.Allocated = true; firstAllocatedItem = firstAllocatedItem ?? item; } } } packRate = a.GetPackRate(); }
public void DrawImage(GLPainter glPainter, AtlasImageBinder atlasImgBinder, float left, float top) { if (atlasImgBinder.State == BinderState.Loaded && atlasImgBinder.LatestPainterId != _painterId) { atlasImgBinder.State = BinderState.Unload; atlasImgBinder.LatestPainterId = _painterId; } switch (atlasImgBinder.State) { case BinderState.Loaded: { if (PixelFarm.Drawing.ImageBinder.GetCacheInnerImage(atlasImgBinder) is GLBitmap glbmp) { AtlasItem atlasItem = atlasImgBinder.AtlasItem; Rectangle srcRect = new Rectangle(atlasItem.Left, atlasItem.Top, //diff from font atlas*** atlasItem.Width, atlasItem.Height); switch (atlasImgBinder.TextureKind) { default: case TextureKind.Msdf: throw new NotSupportedException(); case TextureKind.Bitmap: { //atlasImgBinder.State = BinderState.Loaded; //ImageBinder.SetCacheInnerImage(atlasImgBinder, glbmp, false); //atlasImgBinder.AtlasItem = atlasItem; glPainter.Core.DrawSubImage(glbmp, srcRect, left, top); } break; } } } break; case BinderState.Unload: { atlasImgBinder.LatestPainterId = _painterId; //load img first if (_bmpAtlas == null || _lastestImgFile != atlasImgBinder.AtlasName) { _bmpAtlas = _atlasManager.GetBitmapAtlas(atlasImgBinder.AtlasName, out _glBmp); if (_bmpAtlas == null) { //error atlasImgBinder.State = BinderState.Error; //not found return; } _lastestImgFile = atlasImgBinder.AtlasName; } //-------- if (_bmpAtlas.TryGetItem(atlasImgBinder.ImageName, out AtlasItem atlasItem)) { //found map data Rectangle srcRect = new Rectangle(atlasItem.Left, atlasItem.Top, //diff from font atlas*** atlasItem.Width, atlasItem.Height); TextureKind textureKind = _bmpAtlas.TextureKind; switch (textureKind) { default: case TextureKind.Msdf: throw new NotSupportedException(); case TextureKind.Bitmap: { atlasImgBinder.State = BinderState.Loaded; ImageBinder.SetCacheInnerImage(atlasImgBinder, _glBmp, false); atlasImgBinder.AtlasItem = atlasItem; atlasImgBinder.SetPreviewImageSize(atlasItem.Width, atlasItem.Height); atlasImgBinder.RaiseImageChanged(); atlasImgBinder.TextureKind = textureKind; glPainter.Core.DrawSubImage(_glBmp, srcRect, left, top); } break; } } else { atlasImgBinder.State = BinderState.Error; //not found } } break; } #if DEBUG if (atlasImgBinder.State == BinderState.Unload) { } #endif }
/// <summary> /// Gets details of a particular atlas item. /// If texture does not exist in atlas an empty item is returned. /// </summary> /// <param name="key">Key of item.</param> /// <returns>AtlasItem. Key member contains -1 if item not present.</returns> public AtlasItem GetAtlasItem(int key) { AtlasItem item = new AtlasItem(); if (!atlasItems.TryGetValue(key, out item)) item.key = -1; return item; }
static void BuildAtlasChain(string atlasChain) { for (var i = 0; i < MaxAtlasChainLength; i++) { var atlasPath = GetAtlasPath(atlasChain, i); if (AssetBundle.FileExists(atlasPath)) { DeleteFileFromBundle(atlasPath); } else { break; } } var pluginItems = new Dictionary <string, List <AtlasItem> >(); var items = new Dictionary <AtlasOptimization, List <AtlasItem> >(); items[AtlasOptimization.Memory] = new List <AtlasItem>(); items[AtlasOptimization.DrawCalls] = new List <AtlasItem>(); foreach (var fileInfo in The.Workspace.AssetFiles.Enumerate(".png")) { var cookingRules = cookingRulesMap[fileInfo.Path]; if (cookingRules.TextureAtlas == atlasChain) { var srcTexturePath = AssetPath.Combine(The.Workspace.AssetsDirectory, fileInfo.Path); Bitmap bitmap; using (var stream = File.OpenRead(srcTexturePath)) { bitmap = new Bitmap(stream); } if (ShouldDownscale(bitmap, cookingRules)) { bitmap = DownscaleTexture(bitmap, srcTexturePath, cookingRules); } // Ensure that no image exceeded maxAtlasSize limit DownscaleTextureToFitAtlas(ref bitmap, srcTexturePath); var item = new AtlasItem { Path = Path.ChangeExtension(fileInfo.Path, ".atlasPart"), Bitmap = bitmap, CookingRules = cookingRules, SourceExtension = Path.GetExtension(fileInfo.Path) }; var k = cookingRules.AtlasPacker; if (!string.IsNullOrEmpty(k) && k != "Default") { List <AtlasItem> l; if (!pluginItems.TryGetValue(k, out l)) { pluginItems.Add(k, l = new List <AtlasItem>()); } l.Add(item); } else { items[cookingRules.AtlasOptimization].Add(item); } } } var initialAtlasId = 0; foreach (var kv in items) { if (kv.Value.Any()) { if (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); } foreach (var item in kv.Value) { item.Bitmap.Dispose(); } } } 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); } }
public static Bitmap OpenAtlasItemBitmapAndRescaleIfNeeded(TargetPlatform platform, AtlasItem item) { var srcTexturePath = AssetPath.Combine(The.Workspace.AssetsDirectory, Path.ChangeExtension(item.Path, item.SourceExtension)); Bitmap bitmap; using (var stream = File.OpenRead(srcTexturePath)) { bitmap = new Bitmap(stream); } if (item.BitmapInfo == null) { if (ShouldDownscale(platform, bitmap, item.CookingRules)) { var newBitmap = DownscaleTexture(platform, bitmap, srcTexturePath, item.CookingRules); bitmap.Dispose(); bitmap = newBitmap; } // Ensure that no image exceeded maxAtlasSize limit DownscaleTextureToFitAtlas(ref bitmap, srcTexturePath); } else if (bitmap.Width != item.BitmapInfo.Width || bitmap.Height != item.BitmapInfo.Height) { var newBitmap = bitmap.Rescale(item.BitmapInfo.Width, item.BitmapInfo.Height); bitmap.Dispose(); bitmap = newBitmap; } return(bitmap); }