Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
- [Core] Combine ModelConverter options.
Browse files Browse the repository at this point in the history
- [Core] Fix bug with loading `.zip` files
- [Core] Added new entry [UnityCN]
  • Loading branch information
Razmoth committed Nov 30, 2023
1 parent 6945bff commit 7321cf9
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 45 deletions.
24 changes: 20 additions & 4 deletions AssetStudio.CLI/Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,17 @@ public static bool ExportAnimator(AssetItem item, string exportPath, List<AssetI
exportFullPath = Path.Combine(exportPath, item.Text + item.UniqueID, item.Text + ".fbx");
}
var m_Animator = (Animator)item.Asset;
var options = new ModelConverter.Options()
{
imageFormat = Properties.Settings.Default.convertType,
game = Studio.Game,
collectAnimations = Properties.Settings.Default.collectAnimations,
uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(Properties.Settings.Default.uvs),
texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(Properties.Settings.Default.texs),
};
var convert = animationList != null
? new ModelConverter(m_Animator, Properties.Settings.Default.convertType, Properties.Settings.Default.texs, Properties.Settings.Default.uvs, Studio.Game, Properties.Settings.Default.collectAnimations, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(m_Animator, Properties.Settings.Default.convertType, Properties.Settings.Default.texs, Properties.Settings.Default.uvs, Studio.Game, Properties.Settings.Default.collectAnimations);
? new ModelConverter(m_Animator, options, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(m_Animator, options);
ExportFbx(convert, exportFullPath);
return true;
}
Expand All @@ -351,9 +359,17 @@ public static bool ExportGameObject(AssetItem item, string exportPath, List <Ass

public static bool ExportGameObject(GameObject gameObject, string exportPath, List<AssetItem> animationList = null)
{
var options = new ModelConverter.Options()
{
imageFormat = Properties.Settings.Default.convertType,
game = Studio.Game,
collectAnimations = Properties.Settings.Default.collectAnimations,
uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(Properties.Settings.Default.uvs),
texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(Properties.Settings.Default.texs),
};
var convert = animationList != null
? new ModelConverter(gameObject, Properties.Settings.Default.convertType, Properties.Settings.Default.texs, Properties.Settings.Default.uvs, Studio.Game, Properties.Settings.Default.collectAnimations, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(gameObject, Properties.Settings.Default.convertType, Properties.Settings.Default.texs, Properties.Settings.Default.uvs, Studio.Game, Properties.Settings.Default.collectAnimations);
? new ModelConverter(gameObject, options, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(gameObject, options);

if (convert.MeshList.Count == 0)
{
Expand Down
36 changes: 30 additions & 6 deletions AssetStudio.GUI/Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,17 @@ public static bool ExportAnimator(AssetItem item, string exportPath, List<AssetI
exportFullPath = Path.Combine(exportPath, item.Text + item.UniqueID, item.Text + ".fbx");
}
var m_Animator = (Animator)item.Asset;
var options = new ModelConverter.Options()
{
imageFormat = Properties.Settings.Default.convertType,
game = Studio.Game,
collectAnimations = Properties.Settings.Default.collectAnimations,
uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(Properties.Settings.Default.uvs),
texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(Properties.Settings.Default.texs),
};
var convert = animationList != null
? new ModelConverter(m_Animator, Properties.Settings.Default.convertType, Properties.Settings.Default.texs, Properties.Settings.Default.uvs, Studio.Game, Properties.Settings.Default.collectAnimations, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(m_Animator, Properties.Settings.Default.convertType, Properties.Settings.Default.texs, Properties.Settings.Default.uvs, Studio.Game, Properties.Settings.Default.collectAnimations);
? new ModelConverter(m_Animator, options, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(m_Animator, options);
ExportFbx(convert, exportFullPath);
return true;
}
Expand All @@ -355,19 +363,35 @@ public static bool ExportGameObject(AssetItem item, string exportPath, List <Ass

public static void ExportGameObject(GameObject gameObject, string exportPath, List<AssetItem> animationList = null)
{
var options = new ModelConverter.Options()
{
imageFormat = Properties.Settings.Default.convertType,
game = Studio.Game,
collectAnimations = Properties.Settings.Default.collectAnimations,
uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(Properties.Settings.Default.uvs),
texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(Properties.Settings.Default.texs),
};
var convert = animationList != null
? new ModelConverter(gameObject, Properties.Settings.Default.convertType, Properties.Settings.Default.texs, Properties.Settings.Default.uvs, Studio.Game, Properties.Settings.Default.collectAnimations, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(gameObject, Properties.Settings.Default.convertType, Properties.Settings.Default.texs, Properties.Settings.Default.uvs, Studio.Game, Properties.Settings.Default.collectAnimations);
? new ModelConverter(gameObject, options, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(gameObject, options);
exportPath = exportPath + FixFileName(gameObject.m_Name) + ".fbx";
ExportFbx(convert, exportPath);
}

public static void ExportGameObjectMerge(List<GameObject> gameObject, string exportPath, List<AssetItem> animationList = null)
{
var rootName = Path.GetFileNameWithoutExtension(exportPath);
var options = new ModelConverter.Options()
{
imageFormat = Properties.Settings.Default.convertType,
game = Studio.Game,
collectAnimations = Properties.Settings.Default.collectAnimations,
uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(Properties.Settings.Default.uvs),
texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(Properties.Settings.Default.texs),
};
var convert = animationList != null
? new ModelConverter(rootName, gameObject, Properties.Settings.Default.convertType, Properties.Settings.Default.texs, Properties.Settings.Default.uvs, Studio.Game, Properties.Settings.Default.collectAnimations, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(rootName, gameObject, Properties.Settings.Default.convertType, Properties.Settings.Default.texs, Properties.Settings.Default.uvs, Studio.Game, Properties.Settings.Default.collectAnimations);
? new ModelConverter(rootName, gameObject, options, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(rootName, gameObject, options);
ExportFbx(convert, exportPath);
}

Expand Down
20 changes: 18 additions & 2 deletions AssetStudio.GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,12 +1290,28 @@ private void PreviewMesh(Mesh m_Mesh)

private void PreviewGameObject(GameObject m_GameObject)
{
var model = new ModelConverter(m_GameObject, Properties.Settings.Default.convertType, Properties.Settings.Default.texs, Properties.Settings.Default.uvs, Studio.Game, false, Array.Empty<AnimationClip>());
var options = new ModelConverter.Options()
{
imageFormat = Properties.Settings.Default.convertType,
game = Studio.Game,
collectAnimations = Properties.Settings.Default.collectAnimations,
uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(Properties.Settings.Default.uvs),
texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(Properties.Settings.Default.texs),
};
var model = new ModelConverter(m_GameObject, options, Array.Empty<AnimationClip>());
PreviewModel(model);
}
private void PreviewAnimator(Animator m_Animator)
{
var model = new ModelConverter(m_Animator, Properties.Settings.Default.convertType, Properties.Settings.Default.texs, Properties.Settings.Default.uvs, Studio.Game, false, Array.Empty<AnimationClip>());
var options = new ModelConverter.Options()
{
imageFormat = Properties.Settings.Default.convertType,
game = Studio.Game,
collectAnimations = Properties.Settings.Default.collectAnimations,
uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(Properties.Settings.Default.uvs),
texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(Properties.Settings.Default.texs),
};
var model = new ModelConverter(m_Animator, options, Array.Empty<AnimationClip>());
PreviewModel(model);
}

Expand Down
66 changes: 33 additions & 33 deletions AssetStudio.Utility/ModelConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,23 @@ public class ModelConverter : IImported
public List<ImportedKeyframedAnimation> AnimationList { get; protected set; } = new List<ImportedKeyframedAnimation>();
public List<ImportedMorph> MorphList { get; protected set; } = new List<ImportedMorph>();

private Game Game;
private ImageFormat imageFormat;
private Options options;
private Avatar avatar;
private HashSet<AnimationClip> animationClipHashSet = new HashSet<AnimationClip>();
private Dictionary<AnimationClip, string> boundAnimationPathDic = new Dictionary<AnimationClip, string>();
private Dictionary<uint, string> bonePathHash = new Dictionary<uint, string>();
private Dictionary<Texture2D, string> textureNameDictionary = new Dictionary<Texture2D, string>();
private Dictionary<Transform, ImportedFrame> transformDictionary = new Dictionary<Transform, ImportedFrame>();
Dictionary<uint, string> morphChannelNames = new Dictionary<uint, string>();
Dictionary<string, (bool, int)> uvs = new Dictionary<string, (bool, int)>();
Dictionary<string, int> texs = new Dictionary<string, int>();

public ModelConverter(GameObject m_GameObject, ImageFormat imageFormat, string texs, string uvs, Game game, bool collectAnimations, AnimationClip[] animationList = null)
public ModelConverter(GameObject m_GameObject, Options options, AnimationClip[] animationList = null)
{
Game = game;
this.imageFormat = imageFormat;
this.texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(texs);
this.uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(uvs);
this.options = options;

if (m_GameObject.m_Animator != null)
{
InitWithAnimator(m_GameObject.m_Animator);
if (animationList == null && collectAnimations)
if (animationList == null && this.options.collectAnimations)
{
CollectAnimationClip(m_GameObject.m_Animator);
}
Expand All @@ -55,16 +50,14 @@ public ModelConverter(GameObject m_GameObject, ImageFormat imageFormat, string t
ConvertAnimations();
}

public ModelConverter(string rootName, List<GameObject> m_GameObjects, ImageFormat imageFormat, string texs, string uvs, Game game, bool collectAnimations, AnimationClip[] animationList = null)
public ModelConverter(string rootName, List<GameObject> m_GameObjects, Options options, AnimationClip[] animationList = null)
{
Game = game;
this.imageFormat = imageFormat;
this.texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(texs);
this.uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(uvs);
this.options = options;

RootFrame = CreateFrame(rootName, Vector3.Zero, new Quaternion(0, 0, 0, 0), Vector3.One);
foreach (var m_GameObject in m_GameObjects)
{
if (m_GameObject.m_Animator != null && animationList == null && collectAnimations)
if (m_GameObject.m_Animator != null && animationList == null && this.options.collectAnimations)
{
CollectAnimationClip(m_GameObject.m_Animator);
}
Expand All @@ -88,14 +81,12 @@ public ModelConverter(string rootName, List<GameObject> m_GameObjects, ImageForm
ConvertAnimations();
}

public ModelConverter(Animator m_Animator, ImageFormat imageFormat, string texs, string uvs, Game game, bool collectAnimations, AnimationClip[] animationList = null)
public ModelConverter(Animator m_Animator, Options options, AnimationClip[] animationList = null)
{
Game = game;
this.imageFormat = imageFormat;
this.texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(texs);
this.uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(uvs);
this.options = options;

InitWithAnimator(m_Animator);
if (animationList == null && collectAnimations)
if (animationList == null && this.options.collectAnimations)
{
CollectAnimationClip(m_Animator);
}
Expand Down Expand Up @@ -313,8 +304,8 @@ private void ConvertMeshRenderer(Renderer meshR)
for (int uv = 0; uv < 8; uv++)
{
var key = $"UV{uv}";
iMesh.hasUV[uv] = mesh.GetUV(uv)?.Length > 0 && uvs[key].Item1;
iMesh.uvType[uv] = uvs[key].Item2;
iMesh.hasUV[uv] = mesh.GetUV(uv)?.Length > 0 && options.uvs[key].Item1;
iMesh.uvType[uv] = options.uvs[key].Item2;
}
iMesh.hasTangent = mesh.m_Tangents != null && mesh.m_Tangents.Length == mesh.m_VertexCount * 4;
iMesh.hasColor = mesh.m_Colors?.Length > 0;
Expand Down Expand Up @@ -720,7 +711,7 @@ private ImportedMaterial ConvertMaterial(Material mat)
iMat.Textures.Add(texture);

int dest = -1;
if (texs.TryGetValue(texEnv.Key, out var targetDest))
if (options.texs.TryGetValue(texEnv.Key, out var targetDest))
dest = targetDest;
else if (texEnv.Key == "_MainTex")
dest = 0;
Expand All @@ -733,7 +724,7 @@ private ImportedMaterial ConvertMaterial(Material mat)

texture.Dest = dest;

var ext = $".{imageFormat.ToString().ToLower()}";
var ext = $".{options.imageFormat.ToString().ToLower()}";
if (textureNameDictionary.TryGetValue(m_Texture2D, out var textureName))
{
texture.Name = textureName;
Expand Down Expand Up @@ -779,7 +770,7 @@ private void ConvertTexture2D(Texture2D m_Texture2D, string name)
return;
}

var stream = m_Texture2D.ConvertToStream(imageFormat, true);
var stream = m_Texture2D.ConvertToStream(options.imageFormat, true);
if (stream != null)
{
using (stream)
Expand Down Expand Up @@ -909,9 +900,9 @@ private void ConvertAnimations()
var m_ClipBindingConstant = animationClip.m_ClipBindingConstant ?? m_Clip.ConvertValueArrayToGenericBinding();
var m_ACLClip = m_Clip.m_ACLClip;
var aclCount = m_ACLClip.CurveCount;
if (m_ACLClip.IsSet && !Game.Type.IsSRGroup())
if (m_ACLClip.IsSet && !options.game.Type.IsSRGroup())
{
m_ACLClip.Process(Game, out var values, out var times);
m_ACLClip.Process(options.game, out var values, out var times);
for (int frameIndex = 0; frameIndex < times.Length; frameIndex++)
{
var time = times[frameIndex];
Expand All @@ -931,7 +922,7 @@ private void ConvertAnimations()
for (int curveIndex = 0; curveIndex < frame.keyList.Count;)
{
var index = frame.keyList[curveIndex].index;
if (!Game.Type.IsSRGroup())
if (!options.game.Type.IsSRGroup())
index += (int)aclCount;
ReadCurveData(iAnim, m_ClipBindingConstant, index, frame.time, streamedValues, 0, ref curveIndex);
}
Expand All @@ -945,14 +936,14 @@ private void ConvertAnimations()
for (int curveIndex = 0; curveIndex < m_DenseClip.m_CurveCount;)
{
var index = streamCount + curveIndex;
if (!Game.Type.IsSRGroup())
if (!options.game.Type.IsSRGroup())
index += (int)aclCount;
ReadCurveData(iAnim, m_ClipBindingConstant, (int)index, time, m_DenseClip.m_SampleArray, (int)frameOffset, ref curveIndex);
}
}
if (m_ACLClip.IsSet && Game.Type.IsSRGroup())
if (m_ACLClip.IsSet && options.game.Type.IsSRGroup())
{
m_ACLClip.Process(Game, out var values, out var times);
m_ACLClip.Process(options.game, out var values, out var times);
for (int frameIndex = 0; frameIndex < times.Length; frameIndex++)
{
var time = times[frameIndex];
Expand Down Expand Up @@ -1174,5 +1165,14 @@ private string GetChannelNameFromHash(uint attribute)
return null;
}
}

public record Options
{
public ImageFormat imageFormat;
public Game game;
public bool collectAnimations;
public Dictionary<string, (bool, int)> uvs;
public Dictionary<string, int> texs;
}
}
}
1 change: 1 addition & 0 deletions AssetStudio/AssetsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ private void LoadZipFile(FileReader reader)
streamReader.Position = 0;

FileReader entryReader = new FileReader(dummyPath, streamReader);
entryReader = entryReader.PreProcessing(Game);
LoadFile(entryReader);
if (entryReader.FileType == FileType.ResourceFile)
{
Expand Down
4 changes: 4 additions & 0 deletions AssetStudio/Keys.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,9 @@
{
"Name": "Frostpunk: Beyond the Ice",
"Key": "7368756978696E673838383838383838"
},
{
"Name": "Cat Fantasy",
"Key": "43614461566637323538576877363433"
}
]

0 comments on commit 7321cf9

Please sign in to comment.