Minor cleanup.

This commit is contained in:
Ottermandias 2024-01-14 13:35:46 +01:00
parent 7c83e30e9f
commit 3b9d841014
4 changed files with 115 additions and 118 deletions

View file

@ -17,6 +17,7 @@ public class MaterialExporter
public struct Material
{
public MtrlFile Mtrl;
public Dictionary<TextureUsage, Image<Rgba32>> Textures;
// variant?
}
@ -49,7 +50,7 @@ public class MaterialExporter
ParallelRowIterator.IterateRows(ImageSharpConfiguration.Default, normal.Bounds(), in operation);
// Check if full textures are provided, and merge in if available.
Image<Rgba32> baseColor = operation.BaseColor;
var baseColor = operation.BaseColor;
if (material.Textures.TryGetValue(TextureUsage.SamplerDiffuse, out var diffuse))
{
MultiplyOperation.Execute(diffuse, operation.BaseColor);
@ -70,17 +71,15 @@ public class MaterialExporter
maskTexture.Mutate(context => context.Resize(baseColor.Width, baseColor.Height));
maskTexture.ProcessPixelRows(baseColor, (maskAccessor, baseColorAccessor) =>
{
for (int y = 0; y < maskAccessor.Height; y++)
for (var y = 0; y < maskAccessor.Height; y++)
{
var maskSpan = maskAccessor.GetRowSpan(y);
var baseColorSpan = baseColorAccessor.GetRowSpan(y);
for (int x = 0; x < maskSpan.Length; x++)
for (var x = 0; x < maskSpan.Length; x++)
baseColorSpan[x].FromVector4(baseColorSpan[x].ToVector4() * new Vector4(maskSpan[x].R / 255f));
}
});
// TODO: handle other textures stored in the mask?
}
@ -95,15 +94,22 @@ public class MaterialExporter
// As a future refactor, it would be neat to accept a single-channel field here, and then do composition of other stuff later.
private readonly struct ProcessCharacterNormalOperation(Image<Rgba32> normal, MtrlFile.ColorTable table) : IRowOperation
{
public Image<Rgba32> Normal { get; private init; } = normal.Clone();
public Image<Rgba32> BaseColor { get; private init; } = new Image<Rgba32>(normal.Width, normal.Height);
public Image<Rgb24> Specular { get; private init; } = new Image<Rgb24>(normal.Width, normal.Height);
public Image<Rgb24> Emissive { get; private init; } = new Image<Rgb24>(normal.Width, normal.Height);
public Image<Rgba32> Normal { get; } = normal.Clone();
public Image<Rgba32> BaseColor { get; } = new(normal.Width, normal.Height);
public Image<Rgb24> Specular { get; } = new(normal.Width, normal.Height);
public Image<Rgb24> Emissive { get; } = new(normal.Width, normal.Height);
private Buffer2D<Rgba32> NormalBuffer => Normal.Frames.RootFrame.PixelBuffer;
private Buffer2D<Rgba32> BaseColorBuffer => BaseColor.Frames.RootFrame.PixelBuffer;
private Buffer2D<Rgb24> SpecularBuffer => Specular.Frames.RootFrame.PixelBuffer;
private Buffer2D<Rgb24> EmissiveBuffer => Emissive.Frames.RootFrame.PixelBuffer;
private Buffer2D<Rgba32> NormalBuffer
=> Normal.Frames.RootFrame.PixelBuffer;
private Buffer2D<Rgba32> BaseColorBuffer
=> BaseColor.Frames.RootFrame.PixelBuffer;
private Buffer2D<Rgb24> SpecularBuffer
=> Specular.Frames.RootFrame.PixelBuffer;
private Buffer2D<Rgb24> EmissiveBuffer
=> Emissive.Frames.RootFrame.PixelBuffer;
public void Invoke(int y)
{
@ -112,7 +118,7 @@ public class MaterialExporter
var specularSpan = SpecularBuffer.DangerousGetRowSpan(y);
var emissiveSpan = EmissiveBuffer.DangerousGetRowSpan(y);
for (int x = 0; x < normalSpan.Length; x++)
for (var x = 0; x < normalSpan.Length; x++)
{
ref var normalPixel = ref normalSpan[x];
@ -145,7 +151,7 @@ public class MaterialExporter
private static TableRow GetTableRowIndices(float input)
{
// These calculations are ported from character.shpk.
var smoothed = MathF.Floor(((input * 7.5f) % 1.0f) * 2)
var smoothed = MathF.Floor(input * 7.5f % 1.0f * 2)
* (-input * 15 + MathF.Floor(input * 15 + 0.5f))
+ input * 15;
@ -189,22 +195,19 @@ public class MaterialExporter
where TPixel1 : unmanaged, IPixel<TPixel1>
where TPixel2 : unmanaged, IPixel<TPixel2>
{
public void Invoke(int y)
{
var targetSpan = target.Frames.RootFrame.PixelBuffer.DangerousGetRowSpan(y);
var multiplierSpan = multiplier.Frames.RootFrame.PixelBuffer.DangerousGetRowSpan(y);
for (int x = 0; x < targetSpan.Length; x++)
{
for (var x = 0; x < targetSpan.Length; x++)
targetSpan[x].FromVector4(targetSpan[x].ToVector4() * multiplierSpan[x].ToVector4());
}
}
}
// TODO: These are hardcoded colours - I'm not keen on supporting highly customiseable exports, but there's possibly some more sensible values to use here.
private static Vector4 _defaultHairColor = new Vector4(130, 64, 13, 255) / new Vector4(255);
private static Vector4 _defaultHighlightColor = new Vector4(77, 126, 240, 255) / new Vector4(255);
// TODO: These are hardcoded colours - I'm not keen on supporting highly customizable exports, but there's possibly some more sensible values to use here.
private static readonly Vector4 DefaultHairColor = new Vector4(130, 64, 13, 255) / new Vector4(255);
private static readonly Vector4 DefaultHighlightColor = new Vector4(77, 126, 240, 255) / new Vector4(255);
/// <summary> Build a material following the semantics of hair.shpk. </summary>
private static MaterialBuilder BuildHair(Material material, string name)
@ -214,7 +217,7 @@ public class MaterialExporter
const uint valueFace = 0x6E5B8F10;
var isFace = material.Mtrl.ShaderPackage.ShaderKeys
.Any(key => key.Category == categoryHairType && key.Value == valueFace);
.Any(key => key is { Category: categoryHairType, Value: valueFace });
var normal = material.Textures[TextureUsage.SamplerNormal];
var mask = material.Textures[TextureUsage.SamplerMask];
@ -224,15 +227,15 @@ public class MaterialExporter
var baseColor = new Image<Rgba32>(normal.Width, normal.Height);
normal.ProcessPixelRows(mask, baseColor, (normalAccessor, maskAccessor, baseColorAccessor) =>
{
for (int y = 0; y < normalAccessor.Height; y++)
for (var y = 0; y < normalAccessor.Height; y++)
{
var normalSpan = normalAccessor.GetRowSpan(y);
var maskSpan = maskAccessor.GetRowSpan(y);
var baseColorSpan = baseColorAccessor.GetRowSpan(y);
for (int x = 0; x < normalSpan.Length; x++)
for (var x = 0; x < normalSpan.Length; x++)
{
var color = Vector4.Lerp(_defaultHairColor, _defaultHighlightColor, maskSpan[x].A / 255f);
var color = Vector4.Lerp(DefaultHairColor, DefaultHighlightColor, maskSpan[x].A / 255f);
baseColorSpan[x].FromVector4(color * new Vector4(maskSpan[x].R / 255f));
baseColorSpan[x].A = normalSpan[x].A;
@ -247,10 +250,10 @@ public class MaterialExporter
.WithAlpha(isFace ? AlphaMode.BLEND : AlphaMode.MASK, 0.5f);
}
private static Vector4 _defaultEyeColor = new Vector4(21, 176, 172, 255) / new Vector4(255);
private static readonly Vector4 DefaultEyeColor = new Vector4(21, 176, 172, 255) / new Vector4(255);
/// <summary> Build a material following the semantics of iris.shpk. </summary>
// NOTE: This is largely the same as the hair material, but is also missing a few features that would cause it to diverge. Keeping seperate for now.
// NOTE: This is largely the same as the hair material, but is also missing a few features that would cause it to diverge. Keeping separate for now.
private static MaterialBuilder BuildIris(Material material, string name)
{
var normal = material.Textures[TextureUsage.SamplerNormal];
@ -261,15 +264,15 @@ public class MaterialExporter
var baseColor = new Image<Rgba32>(normal.Width, normal.Height);
normal.ProcessPixelRows(mask, baseColor, (normalAccessor, maskAccessor, baseColorAccessor) =>
{
for (int y = 0; y < normalAccessor.Height; y++)
for (var y = 0; y < normalAccessor.Height; y++)
{
var normalSpan = normalAccessor.GetRowSpan(y);
var maskSpan = maskAccessor.GetRowSpan(y);
var baseColorSpan = baseColorAccessor.GetRowSpan(y);
for (int x = 0; x < normalSpan.Length; x++)
for (var x = 0; x < normalSpan.Length; x++)
{
baseColorSpan[x].FromVector4(_defaultEyeColor * new Vector4(maskSpan[x].R / 255f));
baseColorSpan[x].FromVector4(DefaultEyeColor * new Vector4(maskSpan[x].R / 255f));
baseColorSpan[x].A = normalSpan[x].A;
normalSpan[x].A = byte.MaxValue;
@ -302,30 +305,26 @@ public class MaterialExporter
var resizedNormal = normal.Clone(context => context.Resize(diffuse.Width, diffuse.Height));
diffuse.ProcessPixelRows(resizedNormal, (diffuseAccessor, normalAccessor) =>
{
for (int y = 0; y < diffuseAccessor.Height; y++)
for (var y = 0; y < diffuseAccessor.Height; y++)
{
var diffuseSpan = diffuseAccessor.GetRowSpan(y);
var normalSpan = normalAccessor.GetRowSpan(y);
for (int x = 0; x < diffuseSpan.Length; x++)
{
for (var x = 0; x < diffuseSpan.Length; x++)
diffuseSpan[x].A = normalSpan[x].B;
}
}
});
// Clear the blue channel out of the normal now that we're done with it.
normal.ProcessPixelRows(normalAccessor =>
{
for (int y = 0; y < normalAccessor.Height; y++)
for (var y = 0; y < normalAccessor.Height; y++)
{
var normalSpan = normalAccessor.GetRowSpan(y);
for (int x = 0; x < normalSpan.Length; x++)
{
for (var x = 0; x < normalSpan.Length; x++)
normalSpan[x].B = byte.MaxValue;
}
}
});
return BuildSharedBase(material, name)

View file

@ -21,11 +21,9 @@ namespace Penumbra.Import.Models;
using Schema2 = SharpGLTF.Schema2;
using LuminaMaterial = Lumina.Models.Materials.Material;
public sealed class ModelManager(IFramework framework, ActiveCollections collections, IDataManager gameData, GamePathParser parser, TextureManager textureManager) : SingleTaskQueue, IDisposable
public sealed class ModelManager(IFramework framework, ActiveCollections collections, GamePathParser parser) : SingleTaskQueue, IDisposable
{
private readonly IFramework _framework = framework;
private readonly IDataManager _gameData = gameData;
private readonly TextureManager _textureManager = textureManager;
private readonly ConcurrentDictionary<IAction, (Task, CancellationTokenSource)> _tasks = new();
@ -47,11 +45,13 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect
var action = new ImportGltfAction(inputPath);
return Enqueue(action).ContinueWith(task =>
{
if (task.IsFaulted && task.Exception != null)
if (task is { IsFaulted: true, Exception: not null })
throw task.Exception;
return action.Out;
});
}
/// <summary> Try to find the .sklb paths for a .mdl file. </summary>
/// <param name="mdlPath"> .mdl file to look up the skeletons for. </param>
/// <param name="estManipulations"> Modified extra skeleton template parameters. </param>
@ -168,7 +168,12 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect
return task;
}
private class ExportToGltfAction(ModelManager manager, MdlFile mdl, IEnumerable<string> sklbPaths, Func<string, byte[]> read, string outputPath)
private class ExportToGltfAction(
ModelManager manager,
MdlFile mdl,
IEnumerable<string> sklbPaths,
Func<string, byte[]> read,
string outputPath)
: IAction
{
public void Execute(CancellationToken cancel)
@ -213,13 +218,13 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect
// finicky at the best of times, and can outright cause a CTD if they
// get upset. Running each conversion on its own tick seems to make
// this consistently non-crashy across my testing.
Task<string> CreateHavokTask((SklbFile Sklb, int Index) pair) =>
manager._framework.RunOnTick(
Task<string> CreateHavokTask((SklbFile Sklb, int Index) pair)
=> manager._framework.RunOnTick(
() => HavokConverter.HkxToXml(pair.Sklb.Skeleton),
delayTicks: pair.Index, cancellationToken: cancel);
}
/// <summary> Read a .mtrl and hydrate its textures. </summary>
/// <summary> Read a .mtrl and populate its textures. </summary>
private MaterialExporter.Material BuildMaterial(string relativePath, CancellationToken cancel)
{
var path = manager.ResolveMtrlPath(relativePath);
@ -242,21 +247,15 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect
var texturePath = texture.Path;
if (texture.DX11)
{
var lastSlashIndex = texturePath.LastIndexOf('/');
var directory = lastSlashIndex == -1 ? texturePath : texturePath.Substring(0, lastSlashIndex);
var fileName = Path.GetFileName(texturePath);
if (!fileName.StartsWith("--"))
{
texturePath = $"{directory}/--{fileName}";
}
texturePath = $"{Path.GetDirectoryName(texturePath)}/--{fileName}";
}
using var textureData = new MemoryStream(read(texturePath));
var image = TexFileParser.Parse(textureData);
var pngImage = TextureManager.ConvertToPng(image, cancel).AsPng;
if (pngImage == null)
throw new Exception("Failed to convert texture to png.");
return pngImage;
return pngImage ?? throw new Exception("Failed to convert texture to png.");
}
public bool Equals(IAction? other)

View file

@ -181,9 +181,9 @@ public partial class ModEditWindow
}
/// <summary> Merge attribute configuration from the source onto the target. </summary>
/// <param name="target" Model that will be update. ></param>
/// <param name="target"> Model that will be updated. ></param>
/// <param name="source"> Model to copy attribute configuration from. </param>
public void MergeAttributes(MdlFile target, MdlFile source)
public static void MergeAttributes(MdlFile target, MdlFile source)
{
target.Attributes = source.Attributes;
@ -197,7 +197,7 @@ public partial class ModEditWindow
target.SubMeshes[subMeshIndex].AttributeIndexMask = 0u;
// Rather than comparing sub-meshes directly, we're grouping by parent mesh in an attempt
// to maintain semantic connection betwen mesh index and submesh attributes.
// to maintain semantic connection between mesh index and sub mesh attributes.
if (meshIndex >= source.Meshes.Length)
continue;
var sourceMesh = source.Meshes[meshIndex];
@ -214,8 +214,8 @@ public partial class ModEditWindow
{
IoExceptions = exception switch {
null => [],
AggregateException ae => ae.Flatten().InnerExceptions.ToList(),
Exception other => [other],
AggregateException ae => [.. ae.Flatten().InnerExceptions],
_ => [exception],
};
}
@ -234,7 +234,7 @@ public partial class ModEditWindow
? _edit._gameData.GetFile(path)?.Data
: File.ReadAllBytes(resolvedPath.Value.ToPath());
// TODO: some callers may not care about failures - handle exceptions seperately?
// TODO: some callers may not care about failures - handle exceptions separately?
return bytes ?? throw new Exception(
$"Resolved path {path} could not be found. If modded, is it enabled in the current collection?");
}

View file

@ -129,7 +129,7 @@ public partial class ModEditWindow
);
}
private void DrawIoExceptions(MdlTab tab)
private static void DrawIoExceptions(MdlTab tab)
{
if (tab.IoExceptions.Count == 0)
return;
@ -140,18 +140,17 @@ public partial class ModEditWindow
var spaceAvail = ImGui.GetContentRegionAvail().X - ImGui.GetStyle().ItemSpacing.X - 100;
foreach (var (exception, index) in tab.IoExceptions.WithIndex())
{
using var id = ImRaii.PushId(index);
var message = $"{exception.GetType().Name}: {exception.Message}";
var textSize = ImGui.CalcTextSize(message).X;
if (textSize > spaceAvail)
message = message.Substring(0, (int)Math.Floor(message.Length * (spaceAvail / textSize))) + "...";
message = message[..(int)Math.Floor(message.Length * (spaceAvail / textSize))] + "...";
using (var exceptionNode = ImRaii.TreeNode($"{message}###exception{index}"))
{
using var exceptionNode = ImRaii.TreeNode(message);
if (exceptionNode)
ImGuiUtil.TextWrapped(exception.ToString());
}
}
}
private void DrawGamePathCombo(MdlTab tab)
{