mirror of
https://github.com/aelurum/AssetStudio.git
synced 2026-11-18 11:48:47 -05:00
Add option to export images in "assetName@hash" name format
- Redesigned spriteHelper & texture2DConverter - Improved speed of texture2D preview a bit
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ZstdSharp.Port" Version="0.8.6" />
|
||||
<PackageReference Include="System.IO.Hashing" Version="10.0.4" />
|
||||
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.8" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ namespace AssetStudio
|
||||
public ResourceReader image_data;
|
||||
public StreamingInfo m_StreamData;
|
||||
public StreamingInfo m_DataStreamData; //Tuanjie
|
||||
[JsonIgnore]
|
||||
private ulong dataHash; //we need to store hashes because of sprites
|
||||
|
||||
public Texture2D() { }
|
||||
|
||||
@@ -179,6 +181,28 @@ namespace AssetStudio
|
||||
: new ResourceReader(reader, reader.BaseStream.Position, image_data_size);
|
||||
}
|
||||
|
||||
public ulong GetImageDataHash()
|
||||
{
|
||||
if (dataHash != 0)
|
||||
return dataHash;
|
||||
|
||||
if (image_data.Size == 0)
|
||||
return 0;
|
||||
|
||||
var buff = BigArrayPool<byte>.Shared.Rent(image_data.Size);
|
||||
try
|
||||
{
|
||||
_ = image_data.GetData(buff);
|
||||
dataHash = System.IO.Hashing.XxHash3.HashToUInt64(buff.AsSpan(0, image_data.Size));
|
||||
}
|
||||
finally
|
||||
{
|
||||
BigArrayPool<byte>.Shared.Return(buff);
|
||||
}
|
||||
|
||||
return dataHash;
|
||||
}
|
||||
|
||||
// https://docs.unity3d.com/2023.3/Documentation/Manual/class-TextureImporterOverride.html
|
||||
private int GetImageDataSize(TextureFormat textureFormat)
|
||||
{
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace AssetStudioCLI
|
||||
public ClassIDType Type;
|
||||
public string Text;
|
||||
public string UniqueID;
|
||||
public ulong Hash;
|
||||
public GameObjectNode Node;
|
||||
|
||||
public AssetItem(Object asset)
|
||||
@@ -23,6 +24,7 @@ namespace AssetStudioCLI
|
||||
TypeString = Type.ToString();
|
||||
m_PathID = asset.m_PathID;
|
||||
FullSize = asset.byteSize;
|
||||
Hash = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ namespace AssetStudioCLI.Options
|
||||
AssetName,
|
||||
AssetName_PathID,
|
||||
PathID,
|
||||
AssetName_Hash,
|
||||
}
|
||||
|
||||
internal enum ExportListType
|
||||
@@ -245,10 +246,11 @@ namespace AssetStudioCLI.Options
|
||||
optionDefaultValue: FilenameFormat.AssetName,
|
||||
optionName: "-f, --filename-format <value>",
|
||||
optionDescription: "Specify the file name format for exported assets\n" +
|
||||
"<Value: assetName(default) | assetName_pathID | pathID>\n" +
|
||||
"<Value: assetName(default) | assetName_pathID | pathID | assetName_hash>\n" +
|
||||
"AssetName - Asset file names will look like \"assetName.extension\"\n" +
|
||||
"AssetName_pathID - Asset file names will look like \"assetName @pathID.extension\"\n" +
|
||||
"PathID - Asset file names will look like \"pathID.extension\"\n",
|
||||
"PathID - Asset file names will look like \"pathID.extension\"\n" +
|
||||
"AssetName_hash - (images only) image names will look like \"assetName @hash.extension\"\n",
|
||||
optionExample: "Example: \"-f assetName_pathID\"\n",
|
||||
optionHelpGroup: HelpGroups.General
|
||||
);
|
||||
@@ -931,6 +933,9 @@ namespace AssetStudioCLI.Options
|
||||
case "pathid":
|
||||
o_filenameFormat.Value = FilenameFormat.PathID;
|
||||
break;
|
||||
case "assetname_hash":
|
||||
o_filenameFormat.Value = FilenameFormat.AssetName_Hash;
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine($"{"Error".Color(brightRed)} during parsing [{option.Color(brightYellow)}] option. Unsupported file name format: [{value.Color(brightRed)}].\n");
|
||||
ShowOptionDescription(o_filenameFormat);
|
||||
|
||||
@@ -19,7 +19,10 @@ namespace AssetStudioCLI
|
||||
if (CLIOptions.convertTexture)
|
||||
{
|
||||
var type = CLIOptions.o_imageFormat.Value;
|
||||
if (!TryExportFile(exportPath, item, "." + type.ToString().ToLower(), out var exportFullPath))
|
||||
var converter = new Texture2DConverter(m_Texture2D);
|
||||
if (CLIOptions.o_filenameFormat.Value == FilenameFormat.AssetName_Hash)
|
||||
item.Hash = converter.GetTextureHash();
|
||||
if (!TryExportFile(exportPath, item, $".{type}", out var exportFullPath))
|
||||
return false;
|
||||
|
||||
if (CLIOptions.o_logLevel.Value <= LoggerEvent.Debug)
|
||||
@@ -45,7 +48,7 @@ namespace AssetStudioCLI
|
||||
debugLog += sb.ToString();
|
||||
}
|
||||
|
||||
if (!m_Texture2D.TryConvertToImage(flip: true, out var image))
|
||||
if (!converter.TryConvertToBgra32Image(flip: true, out var image))
|
||||
{
|
||||
Logger.Error($"{debugLog}Export error. Failed to convert texture \"{m_Texture2D.m_Name}\" into image");
|
||||
return false;
|
||||
@@ -74,11 +77,13 @@ namespace AssetStudioCLI
|
||||
{
|
||||
debugLog = "";
|
||||
var type = CLIOptions.o_imageFormat.Value;
|
||||
var alphaMask = SpriteMaskMode.On;
|
||||
if (!TryExportFile(exportPath, item, "." + type.ToString().ToLower(), out var exportFullPath))
|
||||
var converter = new SpriteConverter((Sprite)item.Asset);
|
||||
if (CLIOptions.o_filenameFormat.Value == FilenameFormat.AssetName_Hash)
|
||||
item.Hash = converter.GetSpriteHash();
|
||||
if (!TryExportFile(exportPath, item, $".{type}", out var exportFullPath))
|
||||
return false;
|
||||
var image = ((Sprite)item.Asset).GetImage(alphaMask);
|
||||
if (image != null)
|
||||
|
||||
if (converter.TryConvertToImage(out var image, maskMode: SpriteMaskMode.On))
|
||||
{
|
||||
using (image)
|
||||
{
|
||||
@@ -192,8 +197,12 @@ namespace AssetStudioCLI
|
||||
case FilenameFormat.PathID:
|
||||
fileName = item.m_PathID.ToString();
|
||||
break;
|
||||
case FilenameFormat.AssetName_Hash when item.Hash > 0:
|
||||
fileName = $"{fileName} @{item.Hash:x}";
|
||||
break;
|
||||
}
|
||||
fullPath = Path.Combine(dir, fileName + extension);
|
||||
extension = extension.ToLower();
|
||||
fullPath = Path.Combine(dir, $"{fileName}{extension}");
|
||||
if (ExportPathDict.TryAdd(fullPath, true))
|
||||
{
|
||||
if (CanWrite(fullPath, dir, canOverwrite))
|
||||
@@ -201,9 +210,9 @@ namespace AssetStudioCLI
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (filenameFormat == FilenameFormat.AssetName)
|
||||
else if (filenameFormat == FilenameFormat.AssetName || (filenameFormat == FilenameFormat.AssetName_Hash && item.Hash == 0))
|
||||
{
|
||||
fullPath = Path.Combine(dir, fileName + item.UniqueID + extension);
|
||||
fullPath = Path.Combine(dir, $"{fileName}{item.UniqueID}{extension}");
|
||||
if (CanWrite(fullPath, dir, canOverwrite))
|
||||
{
|
||||
return true;
|
||||
|
||||
@@ -53,7 +53,7 @@ namespace AssetStudioGUI
|
||||
#endregion
|
||||
|
||||
#region SpriteControl
|
||||
private SpriteMaskMode spriteMaskVisibleMode = SpriteMaskMode.On;
|
||||
private SpriteMaskMode spriteMaskVisibleMode = SpriteMaskMode.Preview;
|
||||
#endregion
|
||||
|
||||
#region TexControl
|
||||
@@ -441,16 +441,16 @@ namespace AssetStudioGUI
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (lastSelectedItem?.Type == ClassIDType.Sprite && !((Sprite)lastSelectedItem.Asset).m_RD.alphaTexture.IsNull)
|
||||
else if (lastSelectedItem?.Type == ClassIDType.Sprite && new SpriteConverter((Sprite)lastSelectedItem.Asset).m_AlphaTexture2D != null)
|
||||
{
|
||||
switch (e.KeyCode)
|
||||
{
|
||||
case Keys.A:
|
||||
spriteMaskVisibleMode = spriteMaskVisibleMode == SpriteMaskMode.On ? SpriteMaskMode.Off : SpriteMaskMode.On;
|
||||
spriteMaskVisibleMode = spriteMaskVisibleMode == SpriteMaskMode.Preview ? SpriteMaskMode.Off : SpriteMaskMode.Preview;
|
||||
need = true;
|
||||
break;
|
||||
case Keys.M:
|
||||
spriteMaskVisibleMode = spriteMaskVisibleMode == SpriteMaskMode.MaskOnly ? SpriteMaskMode.On : SpriteMaskMode.MaskOnly;
|
||||
spriteMaskVisibleMode = spriteMaskVisibleMode == SpriteMaskMode.MaskOnly ? SpriteMaskMode.Preview : SpriteMaskMode.MaskOnly;
|
||||
need = true;
|
||||
break;
|
||||
}
|
||||
@@ -976,15 +976,14 @@ namespace AssetStudioGUI
|
||||
|
||||
private void PreviewTexture2D(AssetItem assetItem, Texture2D m_Texture2D)
|
||||
{
|
||||
if (!m_Texture2D.TryConvertToImage(true, out var image))
|
||||
var bitmap = new DirectBitmap(m_Texture2D, flip: true);
|
||||
if (bitmap.Bitmap == null)
|
||||
{
|
||||
StatusStripUpdate("Unsupported image for preview");
|
||||
bitmap.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
var bitmap = new DirectBitmap(image);
|
||||
image.Dispose();
|
||||
|
||||
assetItem.InfoText =
|
||||
$"Width: {m_Texture2D.m_Width}" +
|
||||
$"\nHeight: {m_Texture2D.m_Height}" +
|
||||
@@ -1488,15 +1487,15 @@ namespace AssetStudioGUI
|
||||
|
||||
private void PreviewSprite(AssetItem assetItem, Sprite m_Sprite)
|
||||
{
|
||||
var image = m_Sprite.GetImage(spriteMaskMode: spriteMaskVisibleMode);
|
||||
if (image != null)
|
||||
var converter = new SpriteConverter(m_Sprite);
|
||||
if (converter.TryConvertToImage(out var image, maskMode: spriteMaskVisibleMode))
|
||||
{
|
||||
var bitmap = new DirectBitmap(image);
|
||||
image.Dispose();
|
||||
assetItem.InfoText = $"Width: {bitmap.Width}\nHeight: {bitmap.Height}\n";
|
||||
PreviewTexture(bitmap);
|
||||
|
||||
if (!m_Sprite.m_RD.alphaTexture.IsNull)
|
||||
if (converter.m_AlphaTexture2D != null)
|
||||
{
|
||||
assetItem.InfoText += $"Alpha Mask: {spriteMaskVisibleMode}\n";
|
||||
StatusStripUpdate("'Ctrl'+'A' - Enable/Disable alpha mask usage. 'Ctrl'+'M' - Show alpha mask only.");
|
||||
@@ -1510,9 +1509,11 @@ namespace AssetStudioGUI
|
||||
|
||||
private void PreviewTexture(DirectBitmap bitmap)
|
||||
{
|
||||
previewPanel.Image.Dispose();
|
||||
imageTexture?.Dispose();
|
||||
imageTexture = bitmap;
|
||||
previewPanel.Image = imageTexture.Bitmap;
|
||||
|
||||
if (imageTexture.Width > previewPanel.Width || imageTexture.Height > previewPanel.Height)
|
||||
previewPanel.SizeMode = PictureBoxSizeMode.Zoom;
|
||||
else
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace AssetStudioGUI
|
||||
public ClassIDType Type;
|
||||
public string InfoText;
|
||||
public string UniqueID;
|
||||
public ulong Hash;
|
||||
public GameObjectTreeNode TreeNode;
|
||||
|
||||
public AssetItem(Object asset)
|
||||
@@ -24,6 +25,7 @@ namespace AssetStudioGUI
|
||||
TypeString = Type.ToString();
|
||||
m_PathID = asset.m_PathID;
|
||||
FullSize = asset.byteSize;
|
||||
Hash = 0;
|
||||
}
|
||||
|
||||
public void SetSubItems()
|
||||
|
||||
@@ -20,12 +20,28 @@ namespace AssetStudioGUI
|
||||
m_bitmap = new Bitmap(Width, Height, Stride, PixelFormat.Format32bppArgb, m_handle.AddrOfPinnedObject());
|
||||
}
|
||||
|
||||
public DirectBitmap(Texture2D texture2D, bool flip)
|
||||
{
|
||||
Width = texture2D.m_Width;
|
||||
Height = texture2D.m_Height;
|
||||
var converter = new Texture2DConverter(texture2D);
|
||||
Bits = BigArrayPool<byte>.Shared.Rent(converter.OutputDataSize);
|
||||
if (converter.GetBgra32RawImageData(Bits, flip))
|
||||
{
|
||||
m_handle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
|
||||
m_bitmap = new Bitmap(Width, Height, Stride, PixelFormat.Format32bppArgb, m_handle.AddrOfPinnedObject());
|
||||
}
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
if (m_bitmap != null)
|
||||
{
|
||||
m_bitmap.Dispose();
|
||||
m_handle.Free();
|
||||
}
|
||||
BigArrayPool<byte>.Shared.Return(Bits);
|
||||
}
|
||||
m_bitmap = null;
|
||||
|
||||
Generated
+4
-2
@@ -201,14 +201,16 @@
|
||||
// filenameFormatComboBox
|
||||
//
|
||||
this.filenameFormatComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
|
||||
this.filenameFormatComboBox.DropDownWidth = 198;
|
||||
this.filenameFormatComboBox.FormattingEnabled = true;
|
||||
this.filenameFormatComboBox.Items.AddRange(new object[] {
|
||||
"asset name",
|
||||
"asset name@pathID",
|
||||
"pathID"});
|
||||
"pathID",
|
||||
"asset name@hash (images only)"});
|
||||
this.filenameFormatComboBox.Location = new System.Drawing.Point(210, 25);
|
||||
this.filenameFormatComboBox.Name = "filenameFormatComboBox";
|
||||
this.filenameFormatComboBox.Size = new System.Drawing.Size(139, 23);
|
||||
this.filenameFormatComboBox.Size = new System.Drawing.Size(149, 23);
|
||||
this.filenameFormatComboBox.TabIndex = 3;
|
||||
//
|
||||
// exportSpriteWithAlphaMask
|
||||
|
||||
@@ -18,7 +18,10 @@ namespace AssetStudioGUI
|
||||
if (Properties.Settings.Default.convertTexture)
|
||||
{
|
||||
var type = Properties.Settings.Default.convertType;
|
||||
if (!TryExportFile(exportPath, item, "." + type.ToString().ToLower(), out var exportFullPath))
|
||||
var converter = new Texture2DConverter(m_Texture2D);
|
||||
if (Properties.Settings.Default.filenameFormat == 3) //asset name@hash
|
||||
item.Hash = converter.GetTextureHash();
|
||||
if (!TryExportFile(exportPath, item, $".{type}", out var exportFullPath))
|
||||
return false;
|
||||
|
||||
if (GUILogger.ShowDebugMessage)
|
||||
@@ -44,9 +47,9 @@ namespace AssetStudioGUI
|
||||
debugLog += sb.ToString();
|
||||
}
|
||||
|
||||
if (!m_Texture2D.TryConvertToImage(flip: true, out var image))
|
||||
if (!converter.TryConvertToBgra32Image(flip: true, out var image))
|
||||
{
|
||||
Logger.Warning($"Failed to convert texture \"{m_Texture2D.m_Name}\" into image");
|
||||
Logger.Warning($"{debugLog}Failed to convert texture \"{m_Texture2D.m_Name}\" into image");
|
||||
return false;
|
||||
}
|
||||
using (image)
|
||||
@@ -73,11 +76,14 @@ namespace AssetStudioGUI
|
||||
{
|
||||
debugLog = "";
|
||||
var type = Properties.Settings.Default.convertType;
|
||||
var spriteMaskMode = Properties.Settings.Default.exportSpriteWithMask ? SpriteMaskMode.Export : SpriteMaskMode.Off;
|
||||
if (!TryExportFile(exportPath, item, "." + type.ToString().ToLower(), out var exportFullPath))
|
||||
var converter = new SpriteConverter((Sprite)item.Asset);
|
||||
if (Properties.Settings.Default.filenameFormat == 3) //asset name@hash
|
||||
item.Hash = converter.GetSpriteHash();
|
||||
if (!TryExportFile(exportPath, item, $".{type}", out var exportFullPath))
|
||||
return false;
|
||||
var image = ((Sprite)item.Asset).GetImage(spriteMaskMode: spriteMaskMode);
|
||||
if (image != null)
|
||||
|
||||
var spriteMaskMode = Properties.Settings.Default.exportSpriteWithMask ? SpriteMaskMode.On : SpriteMaskMode.Off;
|
||||
if (converter.TryConvertToImage(out var image, maskMode: spriteMaskMode))
|
||||
{
|
||||
using (image)
|
||||
{
|
||||
@@ -191,8 +197,12 @@ namespace AssetStudioGUI
|
||||
case 2: //pathID
|
||||
fileName = item.m_PathID.ToString();
|
||||
break;
|
||||
case 3 when item.Hash > 0: //asset name@hash
|
||||
fileName = $"{fileName} @{item.Hash:x}";
|
||||
break;
|
||||
}
|
||||
fullPath = Path.Combine(dir, fileName + extension);
|
||||
extension = extension.ToLower();
|
||||
fullPath = Path.Combine(dir, $"{fileName}{extension}");
|
||||
if (ExportPathDict.TryAdd(fullPath, true))
|
||||
{
|
||||
if (CanWrite(fullPath, dir, canOverwrite))
|
||||
@@ -200,9 +210,9 @@ namespace AssetStudioGUI
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (filenameFormatIndex == 0) //assetName
|
||||
else if (filenameFormatIndex == 0 || (filenameFormatIndex == 3 && item.Hash == 0)) //assetName, asset name@hash
|
||||
{
|
||||
fullPath = Path.Combine(dir, fileName + item.UniqueID + extension);
|
||||
fullPath = Path.Combine(dir, $"{fileName}{item.UniqueID}{extension}");
|
||||
if (CanWrite(fullPath, dir, canOverwrite))
|
||||
{
|
||||
return true;
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
using SixLabors.ImageSharp.Formats.Bmp;
|
||||
using SixLabors.ImageSharp.Formats.Tga;
|
||||
using SixLabors.ImageSharp.Formats.Webp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using System.IO;
|
||||
|
||||
namespace AssetStudio
|
||||
@@ -48,5 +50,31 @@ namespace AssetStudio
|
||||
image.WriteToStream(stream, imageFormat);
|
||||
return stream;
|
||||
}
|
||||
|
||||
public static void ApplyRgbMask(this Image<Bgra32> srcTex, Image<Bgra32> texMask, bool fastResize = false)
|
||||
{
|
||||
using (texMask)
|
||||
{
|
||||
if (srcTex.Width != texMask.Width || srcTex.Height != texMask.Height)
|
||||
{
|
||||
var resampler = fastResize ? KnownResamplers.NearestNeighbor : KnownResamplers.Bicubic;
|
||||
texMask.Mutate(x => x.Resize(srcTex.Width, srcTex.Height, resampler));
|
||||
}
|
||||
|
||||
srcTex.ProcessPixelRows(texMask, (sourceTex, targetTexMask) =>
|
||||
{
|
||||
for (var y = 0; y < texMask.Height; y++)
|
||||
{
|
||||
var texRow = sourceTex.GetRowSpan(y);
|
||||
var maskRow = targetTexMask.GetRowSpan(y);
|
||||
for (var x = 0; x < maskRow.Length; x++)
|
||||
{
|
||||
var grayscale = (byte)((maskRow[x].R + maskRow[x].G + maskRow[x].B) / 3);
|
||||
texRow[x].A = grayscale;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using SixLabors.ImageSharp.Drawing.Processing;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@@ -15,92 +16,133 @@ namespace AssetStudio
|
||||
{
|
||||
Off,
|
||||
On,
|
||||
Preview,
|
||||
MaskOnly,
|
||||
Export
|
||||
}
|
||||
|
||||
public static class SpriteHelper
|
||||
public class SpriteConverter
|
||||
{
|
||||
public static Image<Bgra32> GetImage(this Sprite m_Sprite, SpriteMaskMode spriteMaskMode = SpriteMaskMode.On)
|
||||
private Sprite sprite;
|
||||
private Rectf textureRect;
|
||||
private Vector2 textureRectOffset;
|
||||
private float downscaleMultiplier;
|
||||
private SpriteSettings settingsRaw;
|
||||
private bool cutImageWithCanvas;
|
||||
private int resultWidth;
|
||||
private int resultHeight;
|
||||
|
||||
public Texture2D m_Texture2D { get; }
|
||||
public Texture2D m_AlphaTexture2D { get; }
|
||||
|
||||
public SpriteConverter(Sprite m_Sprite, bool cutWithCanvas = true)
|
||||
{
|
||||
sprite = m_Sprite;
|
||||
cutImageWithCanvas = cutWithCanvas;
|
||||
|
||||
if (m_Sprite.m_SpriteAtlas != null && m_Sprite.m_SpriteAtlas.TryGet(out var m_SpriteAtlas))
|
||||
{
|
||||
if (m_SpriteAtlas.m_RenderDataMap.TryGetValue(m_Sprite.m_RenderDataKey, out var spriteAtlasData) && spriteAtlasData.texture.TryGet(out var m_Texture2D))
|
||||
if (m_SpriteAtlas.m_RenderDataMap.TryGetValue(m_Sprite.m_RenderDataKey, out var spriteAtlasData) && spriteAtlasData.texture.TryGet(out var texture2D))
|
||||
{
|
||||
return CutImage(m_Sprite, m_Texture2D, spriteAtlasData.textureRect, spriteAtlasData.textureRectOffset, spriteAtlasData.downscaleMultiplier, spriteAtlasData.settingsRaw);
|
||||
m_Texture2D = texture2D;
|
||||
if (spriteAtlasData.alphaTexture.TryGet(out var alphaTexture2D))
|
||||
m_AlphaTexture2D = alphaTexture2D;
|
||||
textureRect = spriteAtlasData.textureRect;
|
||||
textureRectOffset = spriteAtlasData.textureRectOffset;
|
||||
downscaleMultiplier = spriteAtlasData.downscaleMultiplier;
|
||||
settingsRaw = spriteAtlasData.settingsRaw;
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (m_Sprite.m_RD.texture.TryGet(out var texture2D))
|
||||
{
|
||||
if (m_Sprite.m_RD.texture.TryGet(out var m_Texture2D) && m_Sprite.m_RD.alphaTexture.TryGet(out var m_AlphaTexture2D) && spriteMaskMode != SpriteMaskMode.Off)
|
||||
{
|
||||
Image<Bgra32> tex = null;
|
||||
if (spriteMaskMode != SpriteMaskMode.MaskOnly)
|
||||
{
|
||||
tex = CutImageWithCanvas(m_Sprite, m_Texture2D, m_Sprite.m_RD.textureRect, m_Sprite.m_RD.textureRectOffset, m_Sprite.m_RD.downscaleMultiplier, m_Sprite.m_RD.settingsRaw);
|
||||
m_Texture2D = texture2D;
|
||||
if (m_Sprite.m_RD.alphaTexture.TryGet(out var alphaTexture2D))
|
||||
m_AlphaTexture2D = alphaTexture2D;
|
||||
textureRect = m_Sprite.m_RD.textureRect;
|
||||
textureRectOffset = m_Sprite.m_RD.textureRectOffset;
|
||||
downscaleMultiplier = m_Sprite.m_RD.downscaleMultiplier;
|
||||
settingsRaw = m_Sprite.m_RD.settingsRaw;
|
||||
}
|
||||
var alphaTex = CutImageWithCanvas(m_Sprite, m_AlphaTexture2D, m_Sprite.m_RD.textureRect, m_Sprite.m_RD.textureRectOffset, m_Sprite.m_RD.downscaleMultiplier, m_Sprite.m_RD.settingsRaw);
|
||||
|
||||
switch (spriteMaskMode)
|
||||
if (textureRect == null)
|
||||
return;
|
||||
|
||||
resultWidth = cutImageWithCanvas
|
||||
? (int)sprite.m_Rect.width
|
||||
: (int)textureRect.width;
|
||||
resultHeight = cutImageWithCanvas
|
||||
? (int)sprite.m_Rect.height
|
||||
: (int)textureRect.height;
|
||||
}
|
||||
|
||||
public bool TryConvertToImage(out Image<Bgra32> spriteImage, SpriteMaskMode maskMode)
|
||||
{
|
||||
spriteImage = null;
|
||||
if (m_Texture2D == null)
|
||||
return false;
|
||||
|
||||
if (m_AlphaTexture2D != null)
|
||||
{
|
||||
var alphaTex = CutSprite(m_AlphaTexture2D);
|
||||
if (maskMode != SpriteMaskMode.MaskOnly)
|
||||
{
|
||||
spriteImage = CutSprite(m_Texture2D);
|
||||
}
|
||||
|
||||
switch (maskMode)
|
||||
{
|
||||
case SpriteMaskMode.On:
|
||||
tex.ApplyRGBMask(alphaTex, isPreview: true);
|
||||
return tex;
|
||||
case SpriteMaskMode.Export:
|
||||
tex.ApplyRGBMask(alphaTex);
|
||||
return tex;
|
||||
case SpriteMaskMode.MaskOnly:
|
||||
return alphaTex;
|
||||
spriteImage = alphaTex;
|
||||
break;
|
||||
case SpriteMaskMode.Preview when alphaTex != null:
|
||||
spriteImage.ApplyRgbMask(alphaTex, fastResize: true);
|
||||
break;
|
||||
case SpriteMaskMode.On when alphaTex != null:
|
||||
spriteImage.ApplyRgbMask(alphaTex);
|
||||
break;
|
||||
}
|
||||
return spriteImage != null;
|
||||
}
|
||||
else if (m_Sprite.m_RD.texture.TryGet(out m_Texture2D))
|
||||
|
||||
spriteImage = CutSprite(m_Texture2D);
|
||||
return spriteImage != null;
|
||||
}
|
||||
|
||||
private Image<Bgra32> CutSprite(Texture2D texture2D)
|
||||
{
|
||||
return CutImageWithCanvas(m_Sprite, m_Texture2D, m_Sprite.m_RD.textureRect, m_Sprite.m_RD.textureRectOffset, m_Sprite.m_RD.downscaleMultiplier, m_Sprite.m_RD.settingsRaw);
|
||||
}
|
||||
}
|
||||
var croppedImage = CutImage(texture2D);
|
||||
if (croppedImage == null)
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void ApplyRGBMask(this Image<Bgra32> tex, Image<Bgra32> texMask, bool isPreview = false)
|
||||
{
|
||||
using (texMask)
|
||||
{
|
||||
if (tex.Width != texMask.Width || tex.Height != texMask.Height)
|
||||
{
|
||||
var resampler = isPreview ? KnownResamplers.NearestNeighbor : KnownResamplers.Bicubic;
|
||||
texMask.Mutate(x => x.Resize(tex.Width, tex.Height, resampler));
|
||||
}
|
||||
if (!cutImageWithCanvas)
|
||||
return croppedImage;
|
||||
|
||||
tex.ProcessPixelRows(texMask, (sourceTex, targetTexMask) =>
|
||||
using (croppedImage)
|
||||
{
|
||||
for (int y = 0; y < texMask.Height; y++)
|
||||
{
|
||||
var texRow = sourceTex.GetRowSpan(y);
|
||||
var maskRow = targetTexMask.GetRowSpan(y);
|
||||
for (int x = 0; x < maskRow.Length; x++)
|
||||
{
|
||||
var grayscale = (byte)((maskRow[x].R + maskRow[x].G + maskRow[x].B) / 3);
|
||||
texRow[x].A = grayscale;
|
||||
}
|
||||
}
|
||||
});
|
||||
var canvas = new Image<Bgra32>(resultWidth, resultHeight);
|
||||
canvas.Mutate(ctx => ctx.DrawImage(
|
||||
croppedImage,
|
||||
new Point((int)textureRectOffset.X, (int)(resultHeight - textureRectOffset.Y - croppedImage.Height)),
|
||||
1f)
|
||||
);
|
||||
|
||||
return canvas;
|
||||
}
|
||||
}
|
||||
|
||||
private static Image<Bgra32> CutImage(Sprite m_Sprite, Texture2D m_Texture2D, Rectf textureRect, Vector2 textureRectOffset, float downscaleMultiplier, SpriteSettings settingsRaw)
|
||||
private Image<Bgra32> CutImage(Texture2D texture2D)
|
||||
{
|
||||
if (!m_Texture2D.TryConvertToImage(false, out var textureImage))
|
||||
if (!texture2D.TryConvertToImage(false, out var textureImage))
|
||||
return null;
|
||||
|
||||
if (downscaleMultiplier > 0f && downscaleMultiplier != 1f)
|
||||
{
|
||||
var width = (int)(m_Texture2D.m_Width / downscaleMultiplier);
|
||||
var height = (int)(m_Texture2D.m_Height / downscaleMultiplier);
|
||||
var width = (int)(texture2D.m_Width / downscaleMultiplier);
|
||||
var height = (int)(texture2D.m_Height / downscaleMultiplier);
|
||||
textureImage.Mutate(x => x.Resize(width, height));
|
||||
}
|
||||
|
||||
var rectX = (int)MathF.Floor(textureRect.x);
|
||||
var rectY = (int)MathF.Floor(textureRect.y);
|
||||
var rectX = (int)textureRect.x;
|
||||
var rectY = (int)textureRect.y;
|
||||
var rectRight = (int)MathF.Ceiling(textureRect.x + textureRect.width);
|
||||
var rectBottom = (int)MathF.Ceiling(textureRect.y + textureRect.height);
|
||||
rectRight = Math.Min(rectRight, textureImage.Width);
|
||||
@@ -133,9 +175,9 @@ namespace AssetStudio
|
||||
{
|
||||
try
|
||||
{
|
||||
var matrix = Matrix3x2.CreateScale(m_Sprite.m_PixelsToUnits);
|
||||
matrix *= Matrix3x2.CreateTranslation(m_Sprite.m_Rect.width * m_Sprite.m_Pivot.X - textureRectOffset.X, m_Sprite.m_Rect.height * m_Sprite.m_Pivot.Y - textureRectOffset.Y);
|
||||
var triangles = GetTriangles(m_Sprite.m_RD);
|
||||
var matrix = Matrix3x2.CreateScale(sprite.m_PixelsToUnits);
|
||||
matrix *= Matrix3x2.CreateTranslation(sprite.m_Rect.width * sprite.m_Pivot.X - textureRectOffset.X, sprite.m_Rect.height * sprite.m_Pivot.Y - textureRectOffset.Y);
|
||||
var triangles = GetTriangles(sprite.m_RD);
|
||||
var points = triangles.Select(x => x.Select(y => new PointF(y.X, y.Y)));
|
||||
var pathBuilder = new PathBuilder(matrix);
|
||||
foreach (var p in points)
|
||||
@@ -177,7 +219,7 @@ namespace AssetStudio
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Warning($"{m_Sprite.m_Name} Unable to render the packed sprite correctly.\n{e}");
|
||||
Logger.Warning($"{sprite.m_Name} Unable to render the packed sprite correctly.\n{e}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,19 +228,6 @@ namespace AssetStudio
|
||||
return spriteImage;
|
||||
}
|
||||
|
||||
private static Image<Bgra32> CutImageWithCanvas(Sprite m_Sprite, Texture2D m_Texture2D, Rectf textureRect, Vector2 textureRectOffset, float downscaleMultiplier, SpriteSettings settingsRaw)
|
||||
{
|
||||
var cropped = CutImage(m_Sprite, m_Texture2D, textureRect, textureRectOffset, downscaleMultiplier, settingsRaw);
|
||||
if (cropped == null)
|
||||
return null;
|
||||
|
||||
var canvas = new Image<Bgra32>((int)MathF.Floor(m_Sprite.m_Rect.width), (int)MathF.Floor(m_Sprite.m_Rect.height));
|
||||
canvas.Mutate(ctx => ctx.DrawImage(cropped, new Point((int)MathF.Floor(textureRectOffset.X), (int)MathF.Floor(m_Sprite.m_Rect.height - textureRectOffset.Y - cropped.Height)), 1f));
|
||||
|
||||
cropped.Dispose();
|
||||
return canvas;
|
||||
}
|
||||
|
||||
private static Vector2[][] GetTriangles(SpriteRenderData m_RD)
|
||||
{
|
||||
if (m_RD.vertices != null) //5.6 down
|
||||
@@ -254,5 +283,40 @@ namespace AssetStudio
|
||||
return triangles.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public ulong GetSpriteHash()
|
||||
{
|
||||
ulong spriteHash = 0;
|
||||
if (m_Texture2D == null)
|
||||
return spriteHash;
|
||||
|
||||
spriteHash = m_Texture2D.GetImageDataHash();
|
||||
|
||||
if (m_AlphaTexture2D != null)
|
||||
{
|
||||
spriteHash ^= m_AlphaTexture2D.GetImageDataHash();
|
||||
}
|
||||
|
||||
return (m_Texture2D.m_Width, m_Texture2D.m_Height) == (resultWidth, resultHeight)
|
||||
? spriteHash
|
||||
: spriteHash ^ GetRectHash();
|
||||
}
|
||||
|
||||
private ulong GetRectHash()
|
||||
{
|
||||
Span<byte> buff = stackalloc byte[40];
|
||||
BinaryPrimitives.WriteInt32LittleEndian(buff, (int)textureRect.x);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(buff.Slice(4), (int)textureRect.y);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(buff.Slice(8), (int)textureRect.width);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(buff.Slice(12), (int)textureRect.height);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(buff.Slice(16), (int)textureRectOffset.X);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(buff.Slice(20), (int)textureRectOffset.Y);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(buff.Slice(24), (int)sprite.m_Rect.x);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(buff.Slice(28), (int)sprite.m_Rect.y);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(buff.Slice(32), (int)sprite.m_Rect.width);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(buff.Slice(36), (int)sprite.m_Rect.height);
|
||||
|
||||
return System.IO.Hashing.XxHash3.HashToUInt64(buff);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
|
||||
namespace AssetStudio
|
||||
{
|
||||
public static class SpriteExtensions
|
||||
{
|
||||
public static bool TryGetImage(this Sprite m_Sprite, out Image<Bgra32> spriteImage, SpriteMaskMode spriteMaskMode = SpriteMaskMode.On)
|
||||
{
|
||||
return new SpriteConverter(m_Sprite).TryConvertToImage(out spriteImage, spriteMaskMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using System.IO;
|
||||
|
||||
namespace AssetStudio
|
||||
{
|
||||
@@ -10,32 +8,13 @@ namespace AssetStudio
|
||||
{
|
||||
public static bool TryConvertToImage(this Texture2D m_Texture2D, bool flip, out Image<Bgra32> image)
|
||||
{
|
||||
image = null;
|
||||
var converter = new Texture2DConverter(m_Texture2D);
|
||||
var buff = BigArrayPool<byte>.Shared.Rent(converter.OutputDataSize);
|
||||
var spanBuff = buff.AsSpan(0, converter.OutputDataSize);
|
||||
try
|
||||
{
|
||||
if (!converter.DecodeTexture2D(buff))
|
||||
return false;
|
||||
|
||||
image = Image.LoadPixelData<Bgra32>(spanBuff, m_Texture2D.m_Width, m_Texture2D.m_Height);
|
||||
|
||||
if (flip)
|
||||
{
|
||||
image.Mutate(x => x.Flip(FlipMode.Vertical));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
BigArrayPool<byte>.Shared.Return(buff, clearArray: true);
|
||||
}
|
||||
return new Texture2DConverter(m_Texture2D).TryConvertToBgra32Image(flip, out image);
|
||||
}
|
||||
|
||||
public static MemoryStream ConvertToStream(this Texture2D m_Texture2D, ImageFormat imageFormat, bool flip)
|
||||
{
|
||||
if (!TryConvertToImage(m_Texture2D, flip, out var image))
|
||||
var converter = new Texture2DConverter(m_Texture2D);
|
||||
if (!converter.TryConvertToBgra32Image(flip, out var image))
|
||||
return null;
|
||||
|
||||
using (image)
|
||||
|
||||
@@ -136,6 +136,7 @@
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<PrecompiledHeaderFile>stdafx.h</PrecompiledHeaderFile>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
@@ -172,6 +173,7 @@
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<PrecompiledHeaderFile>stdafx.h</PrecompiledHeaderFile>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
|
||||
Reference in New Issue
Block a user