Allow keeping existing mdl materials

This commit is contained in:
ackwell 2024-01-10 22:39:48 +11:00
parent d2f93f8562
commit 64aed56f7c
2 changed files with 32 additions and 5 deletions

View file

@ -15,6 +15,8 @@ public partial class ModEditWindow
public MdlFile Mdl { get; private set; } public MdlFile Mdl { get; private set; }
private List<string>[] _attributes; private List<string>[] _attributes;
public bool ImportKeepMaterials;
public List<Utf8GamePath>? GamePaths { get; private set; } public List<Utf8GamePath>? GamePaths { get; private set; }
public int GamePathIndex; public int GamePathIndex;
@ -110,6 +112,7 @@ public partial class ModEditWindow
/// <summary> Export model to an interchange format. </summary> /// <summary> Export model to an interchange format. </summary>
/// <param name="outputPath"> Disk path to save the resulting file to. </param> /// <param name="outputPath"> Disk path to save the resulting file to. </param>
/// <param name="mdlPath"> .mdl game path to resolve satellite files such as skeletons relative to. </param>
public void Export(string outputPath, Utf8GamePath mdlPath) public void Export(string outputPath, Utf8GamePath mdlPath)
{ {
IEnumerable<SklbFile> skeletons; IEnumerable<SklbFile> skeletons;
@ -143,14 +146,37 @@ public partial class ModEditWindow
{ {
IoException = task.Exception?.ToString(); IoException = task.Exception?.ToString();
if (task is { IsCompletedSuccessfully: true, Result: not null }) if (task is { IsCompletedSuccessfully: true, Result: not null })
{ FinalizeImport(task.Result);
Initialize(task.Result);
_dirty = true;
}
PendingIo = false; PendingIo = false;
}); });
} }
/// <summary> Finalise the import of a .mdl, applying any post-import transformations and state updates. </summary>
/// <param name="newMdl"> Model data to finalize. </param>
private void FinalizeImport(MdlFile newMdl)
{
if (ImportKeepMaterials)
MergeMaterials(newMdl, Mdl);
Initialize(newMdl);
_dirty = true;
}
/// <summary> Merge material configuration from the source onto the target. </summary>
/// <param name="target"> Model that will be updated. </param>
/// <param name="source"> Model to copy material configuration from. </param>
public void MergeMaterials(MdlFile target, MdlFile source)
{
target.Materials = source.Materials;
for (var meshIndex = 0; meshIndex < target.Meshes.Length; meshIndex++)
{
target.Meshes[meshIndex].MaterialIndex = meshIndex < source.Meshes.Length
? source.Meshes[meshIndex].MaterialIndex
: (ushort)0;
}
}
/// <summary> Read a .sklb from the active collection or game. </summary> /// <summary> Read a .sklb from the active collection or game. </summary>
/// <param name="sklbPath"> Game path to the .sklb to load. </param> /// <param name="sklbPath"> Game path to the .sklb to load. </param>
private SklbFile ReadSklb(string sklbPath) private SklbFile ReadSklb(string sklbPath)

View file

@ -79,6 +79,8 @@ public partial class ModEditWindow
using (var frame = ImRaii.FramedGroup("Import", size, headerPreIcon: FontAwesomeIcon.FileImport)) using (var frame = ImRaii.FramedGroup("Import", size, headerPreIcon: FontAwesomeIcon.FileImport))
{ {
ImGui.Checkbox("Keep current materials", ref tab.ImportKeepMaterials);
if (ImGuiUtil.DrawDisabledButton("Import from glTF", Vector2.Zero, "Imports a glTF file, overriding the content of this mdl.", if (ImGuiUtil.DrawDisabledButton("Import from glTF", Vector2.Zero, "Imports a glTF file, overriding the content of this mdl.",
tab.PendingIo)) tab.PendingIo))
_fileDialog.OpenFilePicker("Load model from glTF.", "glTF{.gltf,.glb}", (success, paths) => _fileDialog.OpenFilePicker("Load model from glTF.", "glTF{.gltf,.glb}", (success, paths) =>
@ -86,7 +88,6 @@ public partial class ModEditWindow
if (success && paths.Count > 0) if (success && paths.Count > 0)
tab.Import(paths[0]); tab.Import(paths[0]);
}, 1, _mod!.ModPath.FullName, false); }, 1, _mod!.ModPath.FullName, false);
ImGui.Dummy(new Vector2(ImGui.GetFrameHeight()));
} }
if (_dragDropManager.CreateImGuiTarget("ModelDragDrop", out var files, out _) && GetFirstModel(files, out var importFile)) if (_dragDropManager.CreateImGuiTarget("ModelDragDrop", out var files, out _) && GetFirstModel(files, out var importFile))