Skip to content

Commit

Permalink
Cleanup, fix tangent/normal mixup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ottermandias committed Jan 16, 2024
1 parent 47e6e70 commit da1b9e9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Penumbra/Import/Models/Export/MeshExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void AddToScene(SceneBuilder scene)
? scene.AddSkinnedMesh(data.Mesh, Matrix4x4.Identity, joints)
: scene.AddRigidMesh(data.Mesh, Matrix4x4.Identity);

var extras = new Dictionary<string, object>();
var extras = new Dictionary<string, object>(data.Attributes.Length);
foreach (var attribute in data.Attributes)
extras.Add(attribute, true);

Expand Down
2 changes: 1 addition & 1 deletion Penumbra/Import/Models/Import/SubMeshImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private SubMesh Create()

private void BuildIndices()
{
// TODO: glTF supports a bunch of primitive types, ref. Schema2.PrimitiveType. All this code is currently assuming that it's using plain triangles (4). It should probably be generalised to other formats - I _suspect_ we should be able to get away with evaulating the indices to triangles with GetTriangleIndices, but will need investigation.
// TODO: glTF supports a bunch of primitive types, ref. Schema2.PrimitiveType. All this code is currently assuming that it's using plain triangles (4). It should probably be generalised to other formats - I _suspect_ we should be able to get away with evaluating the indices to triangles with GetTriangleIndices, but will need investigation.
_indices = _primitive.GetIndices().Select(idx => (ushort)idx).ToArray();
}

Expand Down
56 changes: 27 additions & 29 deletions Penumbra/Import/Models/Import/VertexAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public static VertexAttribute Position(Accessors accessors, IEnumerable<Accessor

public static VertexAttribute? Tangent1(Accessors accessors, IEnumerable<Accessors> morphAccessors, ushort[] indices)
{
if (!accessors.TryGetValue("NORMAL", out var normalAccessor))
if (!accessors.TryGetValue("NORMAL", out var normalAccessor))
{
Penumbra.Log.Warning("Normals are required to facilitate import or calculation of tangents.");
return null;
Expand All @@ -261,34 +261,31 @@ public static VertexAttribute Position(Accessors accessors, IEnumerable<Accessor
return null;
}

var element = new MdlStructs.VertexElement()
var element = new MdlStructs.VertexElement
{
Stream = 1,
Type = (byte)MdlFile.VertexType.ByteFloat4,
Usage = (byte)MdlFile.VertexUsage.Tangent1,
};

// Per glTF specification, TANGENT morph values are stored as vec3, with the W component always considered to be 0.
var tangentMorphValues = morphAccessors
.Select(a => a.GetValueOrDefault("TANGENT")?.AsVector3Array())
.ToArray();

var normalMorphValues = morphAccessors
.Select(a => a.GetValueOrDefault("TANGENT")?.AsVector3Array())
.ToArray();
var morphValues = morphAccessors
.Select(a => (Tangent: a.GetValueOrDefault("TANGENT")?.AsVector3Array(),
Normal: a.GetValueOrDefault("NORMAL")?.AsVector3Array()))
.ToList();

return new VertexAttribute(
element,
index => BuildBitangent(tangents[index], normals[index]),
buildMorph: (morphIndex, vertexIndex) =>
{
var tangent = tangents[vertexIndex];
var tangentDelta = tangentMorphValues[morphIndex]?[vertexIndex];
var tangent = tangents[vertexIndex];
var tangentDelta = morphValues[morphIndex].Tangent?[vertexIndex];
if (tangentDelta != null)
tangent += new Vector4(tangentDelta.Value, 0);
var normal = normals[vertexIndex];
var normalDelta = normalMorphValues[morphIndex]?[vertexIndex];
var normal = normals[vertexIndex];
var normalDelta = morphValues[morphIndex].Normal?[vertexIndex];
if (normalDelta != null)
normal += normalDelta.Value;
Expand All @@ -297,13 +294,13 @@ public static VertexAttribute Position(Accessors accessors, IEnumerable<Accessor
);
}

/// <summary> Build a byte array representing bitagent data computed from the provided tangent and normal. </summary>
/// <summary> Build a byte array representing bitangent data computed from the provided tangent and normal. </summary>
/// <remarks> XIV primarily stores bitangents, rather than tangents as with most other software, so we calculate on import. </remarks>
private static byte[] BuildBitangent(Vector4 tangent, Vector3 normal)
{
var handedness = tangent.W;
var tangent3 = new Vector3(tangent.X, tangent.Y, tangent.Z);
var bitangent = Vector3.Normalize(Vector3.Cross(normal, tangent3));
var tangent3 = new Vector3(tangent.X, tangent.Y, tangent.Z);
var bitangent = Vector3.Normalize(Vector3.Cross(normal, tangent3));
bitangent *= handedness;

// Byte floats encode 0..1, and bitangents are stored as -1..1. Convert.
Expand All @@ -319,19 +316,20 @@ private static byte[] BuildBitangent(Vector4 tangent, Vector3 normal)
return null;

var positions = accessors["POSITION"].AsVector3Array();
var uvs = uvAccessor.AsVector2Array();
var uvs = uvAccessor.AsVector2Array();

// TODO: Surface this in the UI.
Penumbra.Log.Warning("Calculating tangents, this may result in degraded light interaction. For best results, ensure tangents are caculated or retained during export from 3D modelling tools.");
Penumbra.Log.Warning(
"Calculating tangents, this may result in degraded light interaction. For best results, ensure tangents are caculated or retained during export from 3D modelling tools.");

var vertexCount = positions.Count;

// https://github.com/TexTools/xivModdingFramework/blob/master/xivModdingFramework/Models/Helpers/ModelModifiers.cs#L1569
// https://gamedev.stackexchange.com/a/68617
// https://marti.works/posts/post-calculating-tangents-for-your-mesh/post/
var tangents = new Vector3[vertexCount];
var tangents = new Vector3[vertexCount];
var bitangents = new Vector3[vertexCount];

// Iterate over triangles, calculating tangents relative to the UVs.
for (var index = 0; index < indices.Length; index += 3)
{
Expand All @@ -344,16 +342,16 @@ private static byte[] BuildBitangent(Vector4 tangent, Vector3 normal)
var position2 = positions[vertexIndex2];
var position3 = positions[vertexIndex3];

var texcoord1 = uvs[vertexIndex1];
var texcoord2 = uvs[vertexIndex2];
var texcoord3 = uvs[vertexIndex3];
var texCoord1 = uvs[vertexIndex1];
var texCoord2 = uvs[vertexIndex2];
var texCoord3 = uvs[vertexIndex3];

// Calculate deltas for the position XYZ, and texcoord UV.
var edge1 = position2 - position1;
var edge2 = position3 - position1;
var uv1 = texcoord2 - texcoord1;
var uv2 = texcoord3 - texcoord1;

var uv1 = texCoord2 - texCoord1;
var uv2 = texCoord3 - texCoord1;

// Solve.
var r = 1.0f / (uv1.X * uv2.Y - uv1.Y * uv2.X);
Expand All @@ -378,7 +376,7 @@ private static byte[] BuildBitangent(Vector4 tangent, Vector3 normal)
bitangents[vertexIndex3] += bitangent;
}

// All the triangles have been calcualted, normalise the results for each vertex.
// All the triangles have been calculated, normalise the results for each vertex.
var result = new Vector4[vertexCount];
for (var vertexIndex = 0; vertexIndex < vertexCount; vertexIndex++)
{
Expand All @@ -387,7 +385,7 @@ private static byte[] BuildBitangent(Vector4 tangent, Vector3 normal)
var b = bitangents[vertexIndex];

// Gram-Schmidt orthogonalize and calculate handedness.
var tangent = Vector3.Normalize(t - n * Vector3.Dot(n, t));
var tangent = Vector3.Normalize(t - n * Vector3.Dot(n, t));
var handedness = Vector3.Dot(Vector3.Cross(t, b), n) > 0 ? 1 : -1;

result[vertexIndex] = new Vector4(tangent, handedness);
Expand Down

0 comments on commit da1b9e9

Please sign in to comment.