Skip to content

Commit

Permalink
Merge pull request #336 from anatawa12/unsupported-error
Browse files Browse the repository at this point in the history
MergeSkinnedMesh weight differ warning
  • Loading branch information
anatawa12 authored Aug 19, 2023
2 parents d16047f + d506e40 commit f79d2a0
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The format is based on [Keep a Changelog].
- Material Slot with null material is created if there are more SubMesh than Material Slots `#337`
- AAO silently ignored multi pass rendering `#337`
- For now, multi pass rendering of last SubMesh is not (yet) supported so now cause error but will be supported.
- There is no warning about BlendShape weight difference `#336`

### Security

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The format is based on [Keep a Changelog].
- Material Slot with null material is created if there are more SubMesh than Material Slots `#337`
- AAO silently ignored multi pass rendering `#337`
- For now, multi pass rendering of last SubMesh is not (yet) supported so now cause error but will be supported.
- There is no warning about BlendShape weight difference `#336`

### Security

Expand Down
12 changes: 6 additions & 6 deletions Editor/EditSkinnedMeshComponentUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ private static void OnDestroy(EditSkinnedMeshComponent component)
public static bool IsModifiedByEditComponent(SkinnedMeshRenderer renderer) =>
EditorShared.IsModifiedByEditComponent(renderer);

public static string[] GetBlendShapes(SkinnedMeshRenderer renderer) => EditorShared.GetBlendShapes(renderer);
public static (string name, float weight)[] GetBlendShapes(SkinnedMeshRenderer renderer) => EditorShared.GetBlendShapes(renderer);

public static string[] GetBlendShapes(SkinnedMeshRenderer renderer, EditSkinnedMeshComponent before) =>
public static (string name, float weight)[] GetBlendShapes(SkinnedMeshRenderer renderer, EditSkinnedMeshComponent before) =>
EditorShared.GetBlendShapes(renderer, before);

public static Material[] GetMaterials(SkinnedMeshRenderer renderer) => EditorShared.GetMaterials(renderer);
Expand Down Expand Up @@ -81,9 +81,9 @@ public bool IsModifiedByEditComponent(SkinnedMeshRenderer renderer) =>
readonly Dictionary<SkinnedMeshRenderer, SkinnedMeshProcessors> ProcessorsByRenderer =
new Dictionary<SkinnedMeshRenderer, SkinnedMeshProcessors>();

public string[] GetBlendShapes(SkinnedMeshRenderer renderer) => GetBlendShapes(renderer, null);
public (string name, float weight)[] GetBlendShapes(SkinnedMeshRenderer renderer) => GetBlendShapes(renderer, null);

public string[] GetBlendShapes(SkinnedMeshRenderer renderer, EditSkinnedMeshComponent before) =>
public (string name, float weight)[] GetBlendShapes(SkinnedMeshRenderer renderer, EditSkinnedMeshComponent before) =>
GetProcessors(renderer)?.GetBlendShapes(before) ?? SourceMeshInfoComputer.BlendShapes(renderer);

public Material[] GetMaterials(SkinnedMeshRenderer renderer) => GetMaterials(renderer, null);
Expand Down Expand Up @@ -185,7 +185,7 @@ private IMeshInfoComputer GetComputer(EditSkinnedMeshComponent before = null) =>
? GetComputers().Last()
: GetComputers()[_sorted.FindIndex(x => x.Component == before)];

public string[] GetBlendShapes(EditSkinnedMeshComponent before = null) => GetComputer(before).BlendShapes();
public (string, float)[] GetBlendShapes(EditSkinnedMeshComponent before = null) => GetComputer(before).BlendShapes();

public Material[] GetMaterials(EditSkinnedMeshComponent before = null, bool fast = true) =>
GetComputer(before).Materials(fast);
Expand Down Expand Up @@ -218,7 +218,7 @@ private T CheckRecursive<T>(Func<T> compute)
}
}

public string[] BlendShapes() => CheckRecursive(() => _origin.BlendShapes());
public (string, float)[] BlendShapes() => CheckRecursive(() => _origin.BlendShapes());
public Material[] Materials(bool fast = true) => CheckRecursive(() => _origin.Materials(fast));
}
}
Expand Down
6 changes: 3 additions & 3 deletions Editor/FreezeBlendShapeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override void OnInspectorGUIInner()
var label = new GUIContent();

serializedObject.Update();
foreach (var shapeKeyName in shapes)
foreach (var (shapeKeyName, _) in shapes)
{
var rect = EditorGUILayout.GetControlRect();
label.text = shapeKeyName;
Expand All @@ -41,13 +41,13 @@ protected override void OnInspectorGUIInner()
{
if (GUILayout.Button(CL4EE.Tr("FreezeBlendShape:button:Check All")))
{
foreach (var shapeKeyName in shapes)
foreach (var (shapeKeyName, _) in shapes)
_shapeKeysSet.GetElementOf(shapeKeyName).EnsureAdded();
}

if (GUILayout.Button(CL4EE.Tr("FreezeBlendShape:button:Invert All")))
{
foreach (var shapeKeyName in shapes)
foreach (var (shapeKeyName, _) in shapes)
{
var element = _shapeKeysSet.GetElementOf(shapeKeyName);
if (element.Contains) element.EnsureRemoved();
Expand Down
22 changes: 19 additions & 3 deletions Editor/MergeSkinnedMeshEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ static MergeSkinnedMeshEditor()
{
ComponentValidation.RegisterValidator<MergeSkinnedMesh>(component =>
{
var err = new List<ErrorLog>();
if (component.GetComponent<SkinnedMeshRenderer>().sharedMesh)
return new[] { ErrorLog.Warning("MergeSkinnedMesh:warning:MeshIsNotNone") };
return null;
err.Add(ErrorLog.Warning("MergeSkinnedMesh:warning:MeshIsNotNone"));

err.AddRange(component.renderersSet.GetAsSet()
.SelectMany(EditSkinnedMeshComponentUtil.GetBlendShapes)
.GroupBy(x => x.name, x => x.weight)
.Where(grouping => grouping.Distinct().Count() != 1)
.Select(grouping => ErrorLog.Warning("MergeSkinnedMesh:warning:blendShapeWeightMismatch", grouping.Key)));

return err;
});
}

Expand All @@ -56,10 +64,18 @@ private void OnEnable()

protected override void OnInspectorGUIInner()
{
if (((MergeSkinnedMesh)target).GetComponent<SkinnedMeshRenderer>().sharedMesh)
var component = (MergeSkinnedMesh)target;
if (component.GetComponent<SkinnedMeshRenderer>().sharedMesh)
{
EditorGUILayout.HelpBox(CL4EE.Tr("MergeSkinnedMesh:warning:MeshIsNotNone"), MessageType.Warning);
}

foreach (var grouping in component.renderersSet.GetAsSet()
.SelectMany(EditSkinnedMeshComponentUtil.GetBlendShapes)
.GroupBy(x => x.name, x => x.weight)
.Where(grouping => grouping.Distinct().Count() != 1))
EditorGUILayout.HelpBox(string.Format(CL4EE.Tr("MergeSkinnedMesh:warning:blendShapeWeightMismatch"), grouping.Key),
MessageType.Warning);

EditorGUILayout.PropertyField(_renderersSetProp);
EditorGUILayout.PropertyField(_staticRenderersSetProp);
Expand Down
10 changes: 5 additions & 5 deletions Editor/Processors/SkinnedMeshes/EditSkinnedMeshProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal interface IEditSkinnedMeshProcessor

internal interface IMeshInfoComputer
{
string[] BlendShapes();
(string name, float weight)[] BlendShapes();
Material[] Materials(bool fast = true);
}

Expand All @@ -62,7 +62,7 @@ public AbstractMeshInfoComputer(IMeshInfoComputer upstream)
_upstream = upstream;
}

public virtual string[] BlendShapes() => _upstream?.BlendShapes() ?? Array.Empty<string>();
public virtual (string name, float weight)[] BlendShapes() => _upstream?.BlendShapes() ?? Array.Empty<(string, float)>();

public virtual Material[] Materials(bool fast = true) => _upstream?.Materials(fast) ?? Array.Empty<Material>();
}
Expand All @@ -75,14 +75,14 @@ internal class SourceMeshInfoComputer : IMeshInfoComputer
public SourceMeshInfoComputer(SkinnedMeshRenderer target) => _target = target;


public static string[] BlendShapes(SkinnedMeshRenderer renderer) => Enumerable
public static (string, float)[] BlendShapes(SkinnedMeshRenderer renderer) => Enumerable
.Range(0, renderer.sharedMesh.blendShapeCount)
.Select(i => renderer.sharedMesh.GetBlendShapeName(i))
.Select(i => (renderer.sharedMesh.GetBlendShapeName(i), renderer.GetBlendShapeWeight(i)))
.ToArray();

public static Material[] Materials(SkinnedMeshRenderer renderer) => renderer.sharedMaterials;

public string[] BlendShapes() => BlendShapes(_target);
public (string, float)[] BlendShapes() => BlendShapes(_target);
public Material[] Materials(bool fast = true) => Materials(_target);
}
}
4 changes: 2 additions & 2 deletions Editor/Processors/SkinnedMeshes/FreezeBlendShapeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ class MeshInfoComputer : AbstractMeshInfoComputer
public MeshInfoComputer(FreezeBlendShapeProcessor processor, IMeshInfoComputer upstream) : base(upstream)
=> _processor = processor;

public override string[] BlendShapes()
public override (string, float)[] BlendShapes()
{
var set = _processor.Component.FreezingShapeKeys;
return base.BlendShapes().Where(x => !set.Contains(x)).ToArray();
return base.BlendShapes().Where(x => !set.Contains(x.name)).ToArray();
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions Editor/Processors/SkinnedMeshes/MergeSkinnedMeshProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ class MeshInfoComputer : IMeshInfoComputer

public MeshInfoComputer(MergeSkinnedMeshProcessor processor) => _processor = processor;

public string[] BlendShapes() =>
public (string, float)[] BlendShapes() =>
_processor.Component.renderersSet.GetAsList()
.SelectMany(EditSkinnedMeshComponentUtil.GetBlendShapes)
.Distinct()
.Distinct(BlendShapeNameComparator.Instance)
.ToArray();

public Material[] Materials(bool fast = true)
Expand All @@ -197,6 +197,21 @@ public Material[] Materials(bool fast = true)
.materials
.ToArray();
}

private class BlendShapeNameComparator : IEqualityComparer<(string name, float weight)>
{
public static readonly BlendShapeNameComparator Instance = new BlendShapeNameComparator();

public bool Equals((string name, float weight) x, (string name, float weight) y)
{
return x.name == y.name;
}

public int GetHashCode((string name, float weight) obj)
{
return obj.name?.GetHashCode() ?? 0;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class MeshInfoComputer : AbstractMeshInfoComputer
public MeshInfoComputer(RemoveMeshByBlendShapeProcessor processor, IMeshInfoComputer upstream) : base(upstream)
=> _processor = processor;

public override string[] BlendShapes()
public override (string, float)[] BlendShapes()
{
var set = _processor.Component.RemovingShapeKeys;
return base.BlendShapes().Where(x => !set.Contains(x)).ToArray();
return base.BlendShapes().Where(x => !set.Contains(x.name)).ToArray();
}
}
}
Expand Down
10 changes: 1 addition & 9 deletions Editor/Processors/SkinnedMeshes/RemoveMeshInBoxProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ public override void Process(OptimizerSession session, MeshInfo2 target, MeshInf
target.Vertices.RemoveAll(x => !usingVertices.Contains(x));
}

public override IMeshInfoComputer GetComputer(IMeshInfoComputer upstream) => new MeshInfoComputer(this, upstream);

class MeshInfoComputer : AbstractMeshInfoComputer
{
private readonly RemoveMeshInBoxProcessor _processor;

public MeshInfoComputer(RemoveMeshInBoxProcessor processor, IMeshInfoComputer upstream) : base(upstream)
=> _processor = processor;
}
public override IMeshInfoComputer GetComputer(IMeshInfoComputer upstream) => upstream;
}
}
6 changes: 3 additions & 3 deletions Editor/RemoveMeshByBlendShapeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override void OnInspectorGUIInner()

var label = new GUIContent();

foreach (var shapeKeyName in shapes)
foreach (var (shapeKeyName, _) in shapes)
{
var rect = EditorGUILayout.GetControlRect();
label.text = shapeKeyName;
Expand All @@ -45,13 +45,13 @@ protected override void OnInspectorGUIInner()
{
if (GUILayout.Button(CL4EE.Tr("RemoveMeshByBlendShape:button:Check All")))
{
foreach (var shapeKeyName in shapes)
foreach (var (shapeKeyName, _) in shapes)
_shapeKeysSet.GetElementOf(shapeKeyName).EnsureAdded();
}

if (GUILayout.Button(CL4EE.Tr("RemoveMeshByBlendShape:button:Invert All")))
{
foreach (var shapeKeyName in shapes)
foreach (var (shapeKeyName, _) in shapes)
{
var element = _shapeKeysSet.GetElementOf(shapeKeyName);
if (element.Contains) element.EnsureRemoved();
Expand Down
7 changes: 6 additions & 1 deletion Localization/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,14 @@ msgstr "Components"

# region MergeSkinnedMesh

msgid "MergeSkinnedMesh:warning:blendShapeWeightMismatch"
msgstr ""
"Some weights of BlendShape '{0}' of some source SkinnedMeshRenderer are not same value.\n"
"In this case, the weight of final SkinnedMeshRenderer is not defined so please make uniform weight or freeze BlendShape."

msgid "MergeSkinnedMesh:warning:MeshIsNotNone"
msgstr ""
""Mesh of SkinnedMeshRenderer is not None!\n"
"Mesh of SkinnedMeshRenderer is not None!\n"
"You should add MergeSkinnedMesh onto new GameObject with new SkinnedMeshRenderer!"

msgid "MergeSkinnedMesh:prop:renderers"
Expand Down
5 changes: 5 additions & 0 deletions Localization/ja.po
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ msgstr "コンポーネント"

# region MergeSkinnedMesh

msgid "MergeSkinnedMesh:warning:blendShapeWeightMismatch"
msgstr ""
"マージ元のSkinnedMeshRendererのBlendShape '{0}'のWeightが揃っていません。\n"
"複数のWeightのうちどのWeightが適用されるかが不定であるためWeightを揃えるか固定・削除してください。"

msgid "MergeSkinnedMesh:warning:MeshIsNotNone"
msgstr ""
"SkinnedMeshRendererのMeshがNoneではありません!\n"
Expand Down

0 comments on commit f79d2a0

Please sign in to comment.