Skip to content

Commit

Permalink
fix: MeshRemovalProvider does not consider UV Usage Compatibility API
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Oct 16, 2024
1 parent 8721081 commit a7e5270
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 2 additions & 0 deletions API-Editor/MeshRemovalProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace Anatawa12.AvatarOptimizer.API
/// This class is designed to not have false-positive removal prediction.
/// if the mesh might be kept, this class will report as not removed.
///
/// You should not change the mesh data after creating this class except for evacuated UV channels registered with <see cref="UVUsageCompabilityAPI"/>.
///
/// For example, current implementation does not support removing mesh after merging mesh with MergeSkinnedMesh
/// but primitives merged after MergeSkinnedMesh will be reported as not removed.
///
Expand Down
25 changes: 19 additions & 6 deletions Editor/APIInternal/MeshRemovalProviderImpl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Anatawa12.AvatarOptimizer.API;
using nadena.dev.ndmf.preview;
using Unity.Collections;
Expand All @@ -20,14 +21,15 @@ private static void Register()
var removeMeshByMask = renderer.GetComponent<RemoveMeshByMask>();
var removeMeshByBlendShape = renderer.GetComponents<RemoveMeshByBlendShape>();
var removeMeshInBox = renderer.GetComponents<RemoveMeshInBox>();
var evacuate = renderer.GetComponent<UVUsageCompabilityAPIImpl.Evacuate>();

if (removeMeshByMask == null && removeMeshByBlendShape.Length == 0 && removeMeshInBox.Length == 0)
return null;

if (!renderer.sharedMesh.isReadable)
return null;

return new MeshRemovalProviderImpl(renderer, removeMeshByMask, removeMeshByBlendShape, removeMeshInBox);
return new MeshRemovalProviderImpl(renderer, evacuate, removeMeshByMask, removeMeshByBlendShape, removeMeshInBox);
}

private NativeArray<bool> _removedByBlendShape;
Expand All @@ -49,10 +51,10 @@ public MaterialSlotInformation(bool[] shouldRemove, int width, int height)
}
}

private MeshRemovalProviderImpl(
SkinnedMeshRenderer renderer,
RemoveMeshByMask? removeMeshByMask,
RemoveMeshByBlendShape[] removeMeshByBlendShape,
private MeshRemovalProviderImpl(SkinnedMeshRenderer renderer,
UVUsageCompabilityAPIImpl.Evacuate? evacuate,
RemoveMeshByMask? removeMeshByMask,
RemoveMeshByBlendShape[] removeMeshByBlendShape,
RemoveMeshInBox[] removeMeshInBox)
{
if (removeMeshByBlendShape.Length != 0)
Expand All @@ -69,7 +71,18 @@ private MeshRemovalProviderImpl(
if (removeMeshByMask != null)
{
_removedByMask = new MaterialSlotInformation[renderer.sharedMesh.subMeshCount];
_uv = renderer.sharedMesh.uv;
var evacuated = evacuate?.EvacuateIndex(0) ?? 0;
_uv = evacuated switch
{
0 => renderer.sharedMesh.uv,
1 => renderer.sharedMesh.uv2,
2 => renderer.sharedMesh.uv3,
3 => renderer.sharedMesh.uv4,
4 => renderer.sharedMesh.uv5,
5 => renderer.sharedMesh.uv6,
6 => renderer.sharedMesh.uv7,
7 => renderer.sharedMesh.uv8,
};

for (var index = 0; index < removeMeshByMask.materials.Length && index < _removedByMask.Length; index++)
{
Expand Down
8 changes: 8 additions & 0 deletions Editor/APIInternal/UVUsageCompabilityAPIImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public struct Evacuation
public int originalChannel;
public int savedChannel;
}

public int EvacuateIndex(int index)
{
foreach (var evacuation in evacuations)
if (evacuation.originalChannel == index)
index = evacuation.savedChannel;
return index;
}
}

public class RevertEvacuate : EditSkinnedMeshComponent
Expand Down

0 comments on commit a7e5270

Please sign in to comment.