Skip to content

Commit

Permalink
Merge pull request #1098 from anatawa12/fix-remove-zero-sized-polygon
Browse files Browse the repository at this point in the history
fix: Remove Zero Sized Polygon may remove small polygons
  • Loading branch information
anatawa12 authored Jun 16, 2024
2 parents afc436e + 9136483 commit 8b0eaab
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog].
### Removed

### Fixed
- Remove Zero Sized Polygon may remove small polygons `#1098`

### Security

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog].

### Fixed
- BlendTree with NormalizedBlendValues Broken with MergeBlendTree `#1096`
- Remove Zero Sized Polygon may remove small polygons `#1098`

### Security

Expand Down
41 changes: 8 additions & 33 deletions Editor/Processors/RemoveZeroSizedPolygonProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Anatawa12.AvatarOptimizer.Processors.SkinnedMeshes;
using nadena.dev.ndmf;
using UnityEngine;
Expand All @@ -24,40 +25,14 @@ private static void Process(MeshInfo2 meshInfo2, RemoveZeroSizedPolygon _)
{
foreach (var subMesh in meshInfo2.SubMeshes)
{
var dstI = 0;
for (var srcI = 0; srcI < subMesh.Triangles.Count; srcI += 3)
subMesh.RemovePrimitives("RemoveZeroSizedPolygon", poly =>
{
if (!IsPolygonEmpty(subMesh.Triangles[srcI], subMesh.Triangles[srcI + 1], subMesh.Triangles[srcI + 2]))
{
subMesh.Triangles[dstI] = subMesh.Triangles[srcI];
subMesh.Triangles[dstI + 1] = subMesh.Triangles[srcI + 1];
subMesh.Triangles[dstI + 2] = subMesh.Triangles[srcI + 2];
dstI += 3;
}
}

subMesh.Triangles.RemoveRange(dstI, subMesh.Triangles.Count - dstI);
if (poly.Any(v => v.BlendShapes.Count != 0)) return false;
var first = poly[0];
var firstWeights = new HashSet<(Bone bone, float weight)>(first.BoneWeights);
return poly.Skip(1).All(v => first.Position.Equals(v.Position) && firstWeights.SetEquals(v.BoneWeights));
});
}
}

private static bool IsPolygonEmpty(Vertex a, Vertex b, Vertex c)
{
// BlendShapes are hard to check so disallow it
// TODO: check BlendShape delta is same
if (a.BlendShapes.Count != 0) return false;
if (b.BlendShapes.Count != 0) return false;
if (c.BlendShapes.Count != 0) return false;

// check three points are at same position
// TODO: should we use cross product instead?
if (a.Position != b.Position) return false;
if (a.Position != c.Position) return false;

// check bone and bone weights are same
var aWeights = new HashSet<(Bone bone, float weight)>(a.BoneWeights);
if (!aWeights.SetEquals(b.BoneWeights)) return false;
if (!aWeights.SetEquals(c.BoneWeights)) return false;
return true;
}
}
}
}

0 comments on commit 8b0eaab

Please sign in to comment.