Skip to content

Commit

Permalink
Potential fix for RecalculateTangents causing issues with appearace o…
Browse files Browse the repository at this point in the history
…f non belly body parts
  • Loading branch information
thojmr committed Jan 27, 2022
1 parent 5379680 commit da4a221
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions PregnancyPlus/AI_PregnancyPlus/AI_PregnancyPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
<Compile Include="..\PregnancyPlus.Core\Tools\MeshIdentifier.cs" />
<Compile Include="..\PregnancyPlus.Core\Tools\BellyInfo.cs" />
<Compile Include="..\PregnancyPlus.Core\Tools\Timer.cs" />
<Compile Include="..\PregnancyPlus.Core\Tools\TangentSolver.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions PregnancyPlus/HS2_PregnancyPlus/HS2_PregnancyPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
<Compile Include="..\PregnancyPlus.Core\Tools\MeshIdentifier.cs" />
<Compile Include="..\PregnancyPlus.Core\Tools\BellyInfo.cs" />
<Compile Include="..\PregnancyPlus.Core\Tools\Timer.cs" />
<Compile Include="..\PregnancyPlus.Core\Tools\TangentSolver.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions PregnancyPlus/KKS_PregnancyPlus/KKS_PregnancyPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
<Compile Include="..\PregnancyPlus.Core\Tools\MeshIdentifier.cs" />
<Compile Include="..\PregnancyPlus.Core\Tools\BellyInfo.cs" />
<Compile Include="..\PregnancyPlus.Core\Tools\Timer.cs" />
<Compile Include="..\PregnancyPlus.Core\Tools\TangentSolver.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions PregnancyPlus/KK_PregnancyPlus/KK_PregnancyPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<Compile Include="..\PregnancyPlus.Core\Tools\MeshIdentifier.cs" />
<Compile Include="..\PregnancyPlus.Core\Tools\BellyInfo.cs" />
<Compile Include="..\PregnancyPlus.Core\Tools\Timer.cs" />
<Compile Include="..\PregnancyPlus.Core\Tools\TangentSolver.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 10 additions & 5 deletions PregnancyPlus/PregnancyPlus.Core/PPCharaController.BlendShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,17 @@ internal Mesh PrepForBlendShape(SkinnedMeshRenderer smr, string renderKey)

//Calculate the new normals, but don't show them. We just want it for the blendshape shape target
meshCopyTarget.vertices = md[renderKey].inflatedVertices;
// meshCopyTarget.RecalculateBounds();
meshCopyTarget.RecalculateBounds();
NormalSolver.RecalculateNormals(meshCopyTarget, 40f, md[renderKey].alteredVerticieIndexes);
//Since we are hacking this readable state, prevent hard crash when calculating tangents on originally unreadable meshes
//Note: if we recalculate tangents after chaning normals, it causes problems with nipples, and probably other body parts
//TODO implement https://answers.unity.com/questions/7789/calculating-tangents-vector4.html ?
if (meshCopyTarget.isReadable) meshCopyTarget.RecalculateTangents();
//Since we are hacking this readable state, prevent hard crash when calculating tangents on originally unreadable meshes
if (meshCopyTarget.isReadable)
{
//I think I fixed this accidently at some point during the bindpose refactor, but ill leave this here in case it comes up again
//Note: if we recalculate tangents after chaning normals, it causes problems with nipples, and probably other body parts, so we undo the calculation for non belly verts
// var originalTangents = meshCopyTarget.tangents;
meshCopyTarget.RecalculateTangents();
// meshCopyTarget.tangents = TangentSolver.UnRecalculateTangents(meshCopyTarget.tangents, originalTangents, md[renderKey].alteredVerticieIndexes);
}

nativeDetour.Undo();
return meshCopyTarget;
Expand Down
20 changes: 20 additions & 0 deletions PregnancyPlus/PregnancyPlus.Core/tools/TangentSolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using UnityEngine;

public static class TangentSolver
{
//After we do tangent recalculation, replace old tangents where the belly does not touch (to prevent nipples and others from chaning appearance)
// I did this because the Unity recalculate tangents code is super fast, so its easier/quicker to work backward from there
public static Vector4[] UnRecalculateTangents(Vector4[] newTangents, Vector4[] oldTangents, bool[] indexedVerts)
{
for (var i = 0; i < oldTangents.Length; i++)
{
//If the vert is not a belly vert, set it back to its old tangent value
if (!indexedVerts[i])
{
newTangents[i] = oldTangents[i];
}
}

return newTangents;
}
}

0 comments on commit da4a221

Please sign in to comment.