Skip to content

Commit

Permalink
Merge pull request #381 from anatawa12/better-error-for-meshinfo2-error
Browse files Browse the repository at this point in the history
Better error for meshinfo2 error
  • Loading branch information
anatawa12 authored Aug 25, 2023
2 parents 0d78faf + a844125 commit cf1f1a0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog].
- Meshes generated by AAO now have name `#371`
- This will improve compatibility with UniVRM.
- VPM Package now doesn't include Test code `#372` `#373`
- Better error infomation for MeshInfo2 error `#381`

### Deprecated

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog].
- Meshes generated by AAO now have name `#371`
- This will improve compatibility with UniVRM.
- VPM Package now doesn't include Test code `#372` `#373`
- Better error infomation for MeshInfo2 error `#381`

### Deprecated

Expand Down
15 changes: 9 additions & 6 deletions Editor/Processors/EditSkinnedMeshComponentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,15 @@ public void SaveToMesh(OptimizerSession session)
var meshInfo = keyValuePair.Value;
var meshFilter = targetRenderer.GetComponent<MeshFilter>();

var mesh = meshFilter.sharedMesh
? session.MayInstantiate(meshFilter.sharedMesh)
: session.AddToAsset(new Mesh());
meshInfo.WriteToMesh(mesh);
meshFilter.sharedMesh = mesh;
targetRenderer.sharedMaterials = meshInfo.SubMeshes.Select(x => x.SharedMaterial).ToArray();
BuildReport.ReportingObject(targetRenderer, () =>
{
var mesh = meshFilter.sharedMesh
? session.MayInstantiate(meshFilter.sharedMesh)
: session.AddToAsset(new Mesh());
meshInfo.WriteToMesh(mesh);
meshFilter.sharedMesh = mesh;
targetRenderer.sharedMaterials = meshInfo.SubMeshes.Select(x => x.SharedMaterial).ToArray();
});
}
}
}
Expand Down
76 changes: 43 additions & 33 deletions Editor/Processors/SkinnedMeshes/MeshInfo2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,47 @@ public MeshInfo2(SkinnedMeshRenderer renderer)
var mesh = renderer.sharedMesh
? renderer.sharedMesh
: new Mesh { name = $"AAOGeneratedMesh({renderer.name})" };
ReadSkinnedMesh(mesh);

BuildReport.ReportingObject(renderer, true, () =>
{
ReadSkinnedMesh(mesh);

// if there's no bones: add one fake bone
if (Bones.Count == 0)
SetIdentityBone(renderer.rootBone ? renderer.rootBone : renderer.transform);
// if there's no bones: add one fake bone
if (Bones.Count == 0)
SetIdentityBone(renderer.rootBone ? renderer.rootBone : renderer.transform);

Bounds = renderer.localBounds;
RootBone = renderer.rootBone ? renderer.rootBone : renderer.transform;
Bounds = renderer.localBounds;
RootBone = renderer.rootBone ? renderer.rootBone : renderer.transform;

for (var i = 0; i < mesh.blendShapeCount; i++)
BlendShapes[i] = (BlendShapes[i].name, renderer.GetBlendShapeWeight(i));
for (var i = 0; i < mesh.blendShapeCount; i++)
BlendShapes[i] = (BlendShapes[i].name, renderer.GetBlendShapeWeight(i));

SetMaterials(renderer);
SetMaterials(renderer);

var bones = renderer.bones;
for (var i = 0; i < bones.Length && i < Bones.Count; i++) Bones[i].Transform = bones[i];
var bones = renderer.bones;
for (var i = 0; i < bones.Length && i < Bones.Count; i++) Bones[i].Transform = bones[i];

AssertInvariantContract("SkinnedMeshRenderer");
AssertInvariantContract("SkinnedMeshRenderer");
});
}

public MeshInfo2(MeshRenderer renderer)
{
SourceRenderer = renderer;
var mesh = renderer.GetComponent<MeshFilter>().sharedMesh;
ReadStaticMesh(mesh);
BuildReport.ReportingObject(renderer, true, () =>
{
var mesh = renderer.GetComponent<MeshFilter>().sharedMesh;
ReadStaticMesh(mesh);

SetIdentityBone(renderer.transform);
SetIdentityBone(renderer.transform);

Bounds = mesh.bounds;
RootBone = renderer.transform;
Bounds = mesh.bounds;
RootBone = renderer.transform;

SetMaterials(renderer);
SetMaterials(renderer);

AssertInvariantContract("MeshRenderer");
AssertInvariantContract("MeshRenderer");
});
}

private void SetMaterials(Renderer renderer)
Expand Down Expand Up @@ -418,20 +425,23 @@ public void WriteToMesh(Mesh destMesh)

public void WriteToSkinnedMeshRenderer(SkinnedMeshRenderer targetRenderer, OptimizerSession session)
{
var mesh = targetRenderer.sharedMesh
? session.MayInstantiate(targetRenderer.sharedMesh)
: session.AddToAsset(new Mesh { name = $"AAOGeneratedMesh{targetRenderer.name}" });

WriteToMesh(mesh);
targetRenderer.sharedMesh = mesh;
for (var i = 0; i < BlendShapes.Count; i++)
targetRenderer.SetBlendShapeWeight(i, BlendShapes[i].weight);
targetRenderer.sharedMaterials = SubMeshes.Select(x => x.SharedMaterial).ToArray();
targetRenderer.bones = Bones.Select(x => x.Transform).ToArray();

targetRenderer.rootBone = RootBone;
if (Bounds != default)
targetRenderer.localBounds = Bounds;
BuildReport.ReportingObject(targetRenderer, () =>
{
var mesh = targetRenderer.sharedMesh
? session.MayInstantiate(targetRenderer.sharedMesh)
: session.AddToAsset(new Mesh { name = $"AAOGeneratedMesh{targetRenderer.name}" });

WriteToMesh(mesh);
targetRenderer.sharedMesh = mesh;
for (var i = 0; i < BlendShapes.Count; i++)
targetRenderer.SetBlendShapeWeight(i, BlendShapes[i].weight);
targetRenderer.sharedMaterials = SubMeshes.Select(x => x.SharedMaterial).ToArray();
targetRenderer.bones = Bones.Select(x => x.Transform).ToArray();

targetRenderer.rootBone = RootBone;
if (Bounds != default)
targetRenderer.localBounds = Bounds;
});
}
}

Expand Down
20 changes: 17 additions & 3 deletions Internal/ErrorReporter/Editor/BuildReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,23 @@ internal static void LogException(Exception e, string additionalStackTrace = "")
}
}

public static T ReportingObject<T>(UnityEngine.Object obj, Func<T> action)
public static T ReportingObject<T>(Object obj, Func<T> action) => ReportingObject(obj, false, action);

public static T ReportingObject<T>(Object obj, bool needThrow, Func<T> action)
{
if (obj != null) CurrentReport._references.Push(obj);
try
{
return action();
}
catch (ReportedException)
{
throw; // rethrow only
}
catch (Exception e)
{
ReportInternalError(e, 2);
if (needThrow) throw new ReportedException();
return default;
}
finally
Expand All @@ -204,14 +211,17 @@ public static T ReportingObject<T>(UnityEngine.Object obj, Func<T> action)

private static void ReportInternalError(Exception exception, int strips)
{
if (exception is ReportedException) return; // reported exception is known internal error
var additionalStackTrace = string.Join("\n",
Environment.StackTrace.Split('\n').Skip(strips)) + "\n";
LogException(exception, additionalStackTrace);
}

public static void ReportingObject(UnityEngine.Object obj, Action action)
public static void ReportingObject(Object obj, Action action) => ReportingObject(obj, false, action);

public static void ReportingObject(Object obj, bool needThrow, Action action)
{
ReportingObject(obj, () =>
ReportingObject(obj, needThrow, () =>
{
action();
return true;
Expand Down Expand Up @@ -248,5 +258,9 @@ public static void RemapPaths(string original, string cloned)

ErrorReportUI.ReloadErrorReport();
}

private class ReportedException : Exception
{
}
}
}

0 comments on commit cf1f1a0

Please sign in to comment.