Skip to content

Commit

Permalink
Fix secondary indices on vertexes overwriting initial value
Browse files Browse the repository at this point in the history
+ improve logging from Meddle.Utils project
+ change some type names
  • Loading branch information
PassiveModding committed Sep 21, 2024
1 parent 9f81ba5 commit 8f51ee5
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 310 deletions.
1 change: 1 addition & 0 deletions Meddle/Meddle.Plugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public Plugin(IDalamudPluginInterface pluginInterface)
app = host.Build();
log = app.Services.GetRequiredService<ILogger<Plugin>>();
Logger = log;
Meddle.Utils.Global.Logger = log;
NativeDll.Initialize(app.Services.GetRequiredService<IDalamudPluginInterface>().AssemblyLocation
.DirectoryName);

Expand Down
11 changes: 6 additions & 5 deletions Meddle/Meddle.Plugin/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@
},
"Microsoft.Extensions.DependencyInjection.Abstractions": {
"type": "Transitive",
"resolved": "8.0.0",
"contentHash": "cjWrLkJXK0rs4zofsK4bSdg+jhDLTaxrkXu4gS6Y7MAlCvRyNNgwY/lJi5RDlQOnSZweHqoyvgvbdvQsRIW+hg=="
"resolved": "8.0.1",
"contentHash": "fGLiCRLMYd00JYpClraLjJTNKLmMJPnqxMaiRzEBIIvevlzxz33mXy39Lkd48hu1G+N21S7QpaO5ZzKsI6FRuA=="
},
"Microsoft.Extensions.Diagnostics": {
"type": "Transitive",
Expand Down Expand Up @@ -277,10 +277,10 @@
},
"Microsoft.Extensions.Logging.Abstractions": {
"type": "Transitive",
"resolved": "8.0.0",
"contentHash": "arDBqTgFCyS0EvRV7O3MZturChstm50OJ0y9bDJvAcmEPJm0FFpFyjU/JLYyStNGGey081DvnQYlncNX5SJJGA==",
"resolved": "8.0.1",
"contentHash": "RIFgaqoaINxkM2KTOw72dmilDmTrYA0ns2KW4lDz4gZ2+o6IQ894CzmdL3StM2oh7QQq44nCWiqKqc4qUI9Jmg==",
"dependencies": {
"Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0"
"Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.1"
}
},
"Microsoft.Extensions.Logging.Configuration": {
Expand Down Expand Up @@ -512,6 +512,7 @@
"meddle.utils": {
"type": "Project",
"dependencies": {
"Microsoft.Extensions.Logging.Abstractions": "[8.0.1, )",
"SharpGLTF.Core": "[1.0.2, )",
"SharpGLTF.Toolkit": "[1.0.2, )",
"SkiaSharp": "[2.88.8, )",
Expand Down
36 changes: 22 additions & 14 deletions Meddle/Meddle.Utils/Export/Vertex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum VertexType : byte
ByteFloat4 = 8,
Half2 = 13,
Half4 = 14,
UByte8 = 17 // 8 byte array for bone weights/bone indexes; 0,4,1,5,2,6,3,7
UByte8 = 17
}

public enum VertexUsage : byte
Expand All @@ -25,20 +25,22 @@ public enum VertexUsage : byte
BlendWeights = 1, // => 1 => BLENDWEIGHT0
BlendIndices = 2, // => 7 => BLENDINDICES0
Normal = 3, // => 2 => NORMAL0
UV = 4, // => (UsageIndex +) 8 => TEXCOORD0
Tangent2 = 5, // => 14 => TANGENT0
Tangent1 = 6, // => 15 => BINORMAL0
TexCoord = 4, // => (UsageIndex +) 8 => TEXCOORD0
Flow = 5, // => 14 => TANGENT0
Binormal = 6, // => 15 => BINORMAL0
Color = 7, // (UsageIndex +) 3 => COLOR0
}

public Vector3? Position;
public float[]? BlendWeights;
public byte[]? BlendIndices;
public Vector3? Normal;
public Vector4? UV;
public Vector4? TexCoord;
public Vector4? TexCoord2; // only using X/Y afaik
public Vector4? Color;
public Vector4? Tangent2;
public Vector4? Tangent1;
public Vector4? Color2;
public Vector4? Flow;
public Vector4? Binormal;

private static class VertexItem
{
Expand Down Expand Up @@ -180,17 +182,23 @@ public static void Apply(ref Vertex vertex, ReadOnlySpan<byte> buffer, VertexEle
case VertexUsage.Normal:
vertex.Normal = ReadVector3(buf, (VertexType)element.Type);
break;
case VertexUsage.UV:
vertex.UV = ReadVector4(buf, (VertexType)element.Type);
case VertexUsage.TexCoord when element.UsageIndex == 0:
vertex.TexCoord = ReadVector4(buf, (VertexType)element.Type);
break;
case VertexUsage.Color:
case VertexUsage.TexCoord when element.UsageIndex != 0:
vertex.TexCoord2 = ReadVector4(buf, (VertexType)element.Type);
break;
case VertexUsage.Color when element.UsageIndex == 0:
vertex.Color = ReadVector4(buf, (VertexType)element.Type);
break;
case VertexUsage.Tangent2:
vertex.Tangent2 = ReadVector4(buf, (VertexType)element.Type);
case VertexUsage.Color when element.UsageIndex != 0:
vertex.Color2 = ReadVector4(buf, (VertexType)element.Type);
break;
case VertexUsage.Flow:
vertex.Flow = ReadVector4(buf, (VertexType)element.Type);
break;
case VertexUsage.Tangent1:
vertex.Tangent1 = ReadVector4(buf, (VertexType)element.Type);
case VertexUsage.Binormal:
vertex.Binormal = ReadVector4(buf, (VertexType)element.Type);
break;
default:
throw new Exception($"Unsupported usage {element.Usage} [{element.Type}]");
Expand Down
2 changes: 1 addition & 1 deletion Meddle/Meddle.Utils/Files/Structs/Model/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public unsafe partial struct BoundingBox {
[StructLayout(LayoutKind.Sequential, Size = 8)]
public struct VertexElement {
public byte Stream;
public byte Offset;
public byte Offset;
public byte Type;
public byte Usage;
public byte UsageIndex;
Expand Down
9 changes: 9 additions & 0 deletions Meddle/Meddle.Utils/Global.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Meddle.Utils;

public static class Global
{
public static ILogger Logger { get; set; } = NullLogger.Instance;
}
1 change: 1 addition & 0 deletions Meddle/Meddle.Utils/Meddle.Utils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="SharpGLTF.Core" Version="1.0.2" />
<PackageReference Include="SharpGLTF.Toolkit" Version="1.0.2" />
<PackageReference Include="SkiaSharp" Version="2.88.8"/>
Expand Down
65 changes: 27 additions & 38 deletions Meddle/Meddle.Utils/MeshBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Meddle.Utils.Export;
using Meddle.Utils.Files;
using Meddle.Utils.Materials;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using SharpGLTF.Geometry;
using SharpGLTF.Geometry.VertexTypes;
using SharpGLTF.Materials;
Expand All @@ -17,11 +19,11 @@ public class MeshBuilder
private int[]? JointLut { get; }
private MaterialBuilder MaterialBuilder { get; }
private RaceDeformer? RaceDeformer { get; }
private Type GeometryT { get; }
private Type MaterialT { get; }
private Type SkinningT { get; }
private Type VertexBuilderT { get; }
private Type MeshBuilderT { get; }
public Type GeometryT { get; }
public Type MaterialT { get; }
public Type SkinningT { get; }
public Type VertexBuilderT { get; }
public Type MeshBuilderT { get; }

private IReadOnlyList<PbdFile.Deformer> Deformers { get; }

Expand All @@ -37,7 +39,7 @@ public MeshBuilder(
Mesh = mesh;
JointLut = jointLut;
MaterialBuilder = materialBuilder;

GeometryT = GetVertexGeometryType(Mesh.Vertices);
MaterialT = GetVertexMaterialType(Mesh.Vertices);
SkinningT = GetVertexSkinningType(Mesh.Vertices, JointLut != null);
Expand All @@ -59,7 +61,7 @@ public MeshBuilder(
Vertices = BuildVertices();
}

public IReadOnlyList<IVertexBuilder> BuildVertices()
private IReadOnlyList<IVertexBuilder> BuildVertices()
{
return Mesh.Vertices.Select(BuildVertex).ToList();
// Parallel impl keep index
Expand Down Expand Up @@ -178,13 +180,8 @@ private static Type GetVertexGeometryType(IReadOnlyList<Vertex> vertex)
{
return typeof(VertexPosition);
}

// if (vertex[0].Tangent2 != null && vertex[0].Tangent1 != null)
// {
// return typeof(VertexPositionNormalTangent2);
// }

if (vertex[0].Tangent1 != null)
if (vertex[0].Binormal != null)
{
return typeof(VertexPositionNormalTangent);
}
Expand Down Expand Up @@ -231,13 +228,13 @@ private IVertexGeometry CreateGeometryParamCache(Vertex vertex, Type type, IRead
// Tangent W should be 1 or -1, but sometimes XIV has their -1 as 0?
return new VertexPositionNormalTangent(currentPos,
vertex.Normal!.Value.SanitizeNormal(),
(vertex.Tangent1!.Value with { W = vertex.Tangent1.Value.W == 1 ? 1 : -1 }).SanitizeTangent());
// case not null when type == typeof(VertexPositionNormalTangent2):
// return new VertexPositionNormalTangent2(vertex.Position!.Value,
// vertex.Normal!.Value,
// vertex.Tangent1!.Value with { W = vertex.Tangent1.Value.W == 1 ? 1 : -1 },
// vertex.Tangent2!.Value with { W = vertex.Tangent2.Value.W == 1 ? 1 : -1 });
(vertex.Binormal!.Value with { W = vertex.Binormal.Value.W == 1 ? 1 : -1 }).SanitizeTangent());
default:
Global.Logger.LogWarning("Unknown vertex type, defaulting to VertexPosition {Vertex}", JsonSerializer.Serialize(vertex, new JsonSerializerOptions
{
WriteIndented = true,
IncludeFields = true
}));
return new VertexPosition(vertex.Position!.Value);
}
// ReSharper restore CompareOfFloatsByEqualityOperator
Expand All @@ -251,24 +248,16 @@ private static Type GetVertexMaterialType(IReadOnlyList<Vertex> vertex)
return typeof(VertexColor1);
}

var hasColor = vertex[0].Color != null;
var hasUv = vertex[0].UV != null;
if (hasColor && hasUv)
{
return typeof(VertexColor1Texture2);
}

if (hasColor)
var firstVertex = vertex[0];
// (UsageIndex +) 8 => TEXCOORD_0
// (UsageIndex +) 3 => COLOR0
return (firstVertex.Color, UV: firstVertex.TexCoord) switch
{
return typeof(VertexColor1);
}

if (hasUv)
{
return typeof(VertexTexture2);
}

return typeof(VertexColor1);
(not null, not null) => typeof(VertexColor1Texture2),
(not null, null) => typeof(VertexColor1),
(null, not null) => typeof(VertexTexture2),
_ => typeof(VertexColor1)
};
}

private static Vector4 GetColor(Vertex vertex, MaterialBuilder materialBuilder)
Expand Down Expand Up @@ -296,12 +285,12 @@ private static IVertexMaterial CreateMaterialParamCache(Vertex vertex, Type type
}
case not null when type == typeof(VertexTexture2):
{
var (xy, zw) = ToVec2(vertex.UV!.Value);
var (xy, zw) = ToVec2(vertex.TexCoord!.Value);
return new VertexTexture2(xy, zw);
}
case not null when type == typeof(VertexColor1Texture2):
{
var (xy, zw) = ToVec2(vertex.UV!.Value);
var (xy, zw) = ToVec2(vertex.TexCoord!.Value);
return new VertexColor1Texture2(GetColor(vertex, materialBuilder), xy, zw);
}
default:
Expand Down
22 changes: 20 additions & 2 deletions Meddle/Meddle.Utils/ModelBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
using Meddle.Utils.Export;
using Meddle.Utils.Materials;
using System.Text.Json;
using Meddle.Utils.Export;
using Microsoft.Extensions.Logging;
using SharpGLTF.Geometry;
using SharpGLTF.Materials;

namespace Meddle.Utils;

public static class ModelBuilder
{
private static JsonSerializerOptions SerializerOptions => new()
{
IncludeFields = true,
};

public static IReadOnlyList<MeshExport> BuildMeshes(
Model model,
IReadOnlyList<MaterialBuilder> materials,
Expand Down Expand Up @@ -40,6 +46,18 @@ public static IReadOnlyList<MeshExport> BuildMeshes(
meshBuilder = new MeshBuilder(mesh, null, material, raceDeformer);
}

Global.Logger.LogDebug("[{Path}] Building mesh {MeshIdx}\n{Mesh}",
model.Path,
mesh.MeshIdx,
JsonSerializer.Serialize(new
{
Material = material.Name,
GeometryType = meshBuilder.GeometryT.Name,
MaterialType = meshBuilder.MaterialT.Name,
SkinningType = meshBuilder.SkinningT.Name,
Vertex = (Vertex?)(mesh.Vertices.Count == 0 ? null : mesh.Vertices[0]),
}, SerializerOptions));

var modelPathName = Path.GetFileNameWithoutExtension(model.Path);

if (mesh.SubMeshes.Count == 0)
Expand Down
Loading

0 comments on commit 8f51ee5

Please sign in to comment.