Skip to content

Commit

Permalink
[MeshSetEditor] Fixes inverted tangent (1.0.6.2) (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicDreamsOfCode authored Oct 2, 2022
1 parent 478e926 commit 88eda33
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions Plugins/MeshSetPlugin/FrostyMeshSetEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,16 +998,16 @@ private FbxNode FBXExportSubObject(FbxScene scene, MeshSetSection section, long

if (elem.Format == VertexElementFormat.Half4)
{
tangent.X = HalfUtils.Unpack(reader.ReadUShort());
tangent.Y = HalfUtils.Unpack(reader.ReadUShort());
tangent.Z = HalfUtils.Unpack(reader.ReadUShort());
tangent.X = -HalfUtils.Unpack(reader.ReadUShort());
tangent.Y = -HalfUtils.Unpack(reader.ReadUShort());
tangent.Z = -HalfUtils.Unpack(reader.ReadUShort());
binormalSigns.Add(HalfUtils.Unpack(reader.ReadUShort()));
}
else
{
tangent.X = reader.ReadFloat();
tangent.Y = reader.ReadFloat();
tangent.Z = reader.ReadFloat();
tangent.X = -reader.ReadFloat();
tangent.Y = -reader.ReadFloat();
tangent.Z = -reader.ReadFloat();
binormalSigns.Add(reader.ReadFloat());
}
}
Expand Down Expand Up @@ -1068,9 +1068,9 @@ private FbxNode FBXExportSubObject(FbxScene scene, MeshSetSection section, long
};
}

tangent.X = HalfUtils.Unpack(reader.ReadUShort());
tangent.Y = HalfUtils.Unpack(reader.ReadUShort());
tangent.Z = HalfUtils.Unpack(reader.ReadUShort());
tangent.X = -HalfUtils.Unpack(reader.ReadUShort());
tangent.Y = -HalfUtils.Unpack(reader.ReadUShort());
tangent.Z = -HalfUtils.Unpack(reader.ReadUShort());

layerElemTangent.DirectArray.Add(tangent.X, tangent.Y, tangent.Z);
binormal.Z = HalfUtils.Unpack(reader.ReadUShort());
Expand Down Expand Up @@ -2532,7 +2532,10 @@ private void ProcessSection(FbxNode[] sectionNodes, MeshSetLod meshLod, int sect

Vector3 normal = Vector3.TransformNormal(vertex.GetValue<Vector3>("Normal"), sectionMatrix);
Vector3 tangent = Vector3.TransformNormal(vertex.GetValue<Vector3>("Tangent"), sectionMatrix);
Vector3 binormal = Vector3.TransformNormal(vertex.GetValue<Vector3>("Binormal"), sectionMatrix);
Vector3 binormal = Vector3.TransformNormal(vertex.GetValue<Vector3>("Binormal"), sectionMatrix);

// for some reason the tangent gets stored inverted
tangent *= -1.0f;

ushort[] finalBoneIndices = vertex.GetValue<ushort[]>("BoneIndices");
byte[] finalBoneWeights = vertex.GetValue<byte[]>("BoneWeights");
Expand Down Expand Up @@ -2572,9 +2575,32 @@ private void ProcessSection(FbxNode[] sectionNodes, MeshSetLod meshLod, int sect

case VertexElementUsage.BinormalSign:
{
if (elem.Format == VertexElementFormat.Half) chunkWriter.Write(HalfUtils.Pack((Vector3.Dot(Vector3.Cross(normal, tangent), binormal)) < 0.0f ? 1.0f : -1.0f));
else chunkWriter.Write((Vector3.Dot(Vector3.Cross(normal, tangent), binormal)) < 0.0f ? 1.0f : -1.0f);
}
if (elem.Format == VertexElementFormat.Half)
{
chunkWriter.Write(HalfUtils.Pack((Vector3.Dot(binormal, Vector3.Cross(normal, tangent))) < 0.0f ? 1.0f : -1.0f));
}
else if (elem.Format == VertexElementFormat.Half4 || elem.Format == VertexElementFormat.Float4)
{
if (elem.Format == VertexElementFormat.Half4)
{
chunkWriter.Write(HalfUtils.Pack(tangent.X));
chunkWriter.Write(HalfUtils.Pack(tangent.Y));
chunkWriter.Write(HalfUtils.Pack(tangent.Z));
chunkWriter.Write(HalfUtils.Pack((Vector3.Dot(binormal, Vector3.Cross(normal, tangent))) < 0.0f ? 1.0f : -1.0f));
}
else
{
chunkWriter.Write(tangent.X);
chunkWriter.Write(tangent.Y);
chunkWriter.Write(tangent.Z);
chunkWriter.Write((Vector3.Dot(binormal, Vector3.Cross(normal, tangent))) < 0.0f ? 1.0f : -1.0f);
}
}
else
{
chunkWriter.Write((Vector3.Dot(binormal, Vector3.Cross(normal, tangent))) < 0.0f ? 1.0f : -1.0f);
}
}
break;

case VertexElementUsage.BoneIndices:
Expand Down

0 comments on commit 88eda33

Please # to comment.