Skip to content

Commit

Permalink
Merge branch 'master-1.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Oct 16, 2023
2 parents 063042c + 930aeda commit c702efd
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 52 deletions.
31 changes: 30 additions & 1 deletion CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ The format is based on [Keep a Changelog].

### Security

## [1.5.6-beta.2] - 2023-10-16
### Changed
- Make no-op as possible if no AAO component attached for your avatar [`#603`](https://github.com/anatawa12/AvatarOptimizer/pull/603)
- Error Report window is refreshed after exiting play mode [`#606`](https://github.com/anatawa12/AvatarOptimizer/pull/606)

### Fixed
- Update notice may show incorrect version [`#602`](https://github.com/anatawa12/AvatarOptimizer/pull/602)
- `Preview` button is not disabled even if mesh is none [`#605`](https://github.com/anatawa12/AvatarOptimizer/pull/605)

## [1.5.6-beta.1] - 2023-10-16
### Fixed
- Multi-frame BlendShape can be broken [`#601`](https://github.com/anatawa12/AvatarOptimizer/pull/601)

## [1.5.5] - 2023-10-15
## [1.5.5-rc.1] - 2023-10-15
### Fixed
- BlendShape can be broken with MergeBone Optimization [`#599`](https://github.com/anatawa12/AvatarOptimizer/pull/599)

## [1.5.5-beta.1] - 2023-10-15
### Fixed
- Constraints and Animations can be broken with Automatic MergeBone [`#594`](https://github.com/anatawa12/AvatarOptimizer/pull/594)
- NRE with SMR with None with preview system [`#596`](https://github.com/anatawa12/AvatarOptimizer/pull/596)
- Some Multi-Frame BlendShape broken [`#597`](https://github.com/anatawa12/AvatarOptimizer/pull/597)

## [1.5.4] - 2023-10-14
### Added
- Add compatibility for Satania's KiseteneEx [`#584`](https://github.com/anatawa12/AvatarOptimizer/pull/584)
Expand Down Expand Up @@ -887,7 +911,12 @@ This release is mistake.
- Merge Bone
- Clear Endpoint Position

[Unreleased]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.4...HEAD
[Unreleased]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.6-beta.2...HEAD
[1.5.6-beta.2]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.6-beta.1...v1.5.6-beta.2
[1.5.6-beta.1]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.5...v1.5.6-beta.1
[1.5.5]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.5-rc.1...v1.5.5
[1.5.5-rc.1]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.5-beta.1...v1.5.5-rc.1
[1.5.5-beta.1]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.4...v1.5.5-beta.1
[1.5.4]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.3...v1.5.4
[1.5.3]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.3-beta.1...v1.5.3
[1.5.3-beta.1]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.2...v1.5.3-beta.1
Expand Down
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,27 @@ The format is based on [Keep a Changelog].
### Added

### Changed
- Make no-op as possible if no AAO component attached for your avatar `#603`
- Error Report window is refreshed after exiting play mode `#606`

### Deprecated

### Removed

### Fixed
- Multi-frame BlendShape can be broken `#601`
- Update notice may show incorrect version `#602`
- `Preview` button is not disabled even if mesh is none `#605`

### Security

## [1.5.5] - 2023-10-15
### Fixed
- Constraints and Animations can be broken with Automatic MergeBone [`#594`](https://github.com/anatawa12/AvatarOptimizer/pull/594)
- NRE with SMR with None with preview system [`#596`](https://github.com/anatawa12/AvatarOptimizer/pull/596)
- Some Multi-Frame BlendShape broken [`#597`](https://github.com/anatawa12/AvatarOptimizer/pull/597)
- BlendShape can be broken with MergeBone Optimization [`#599`](https://github.com/anatawa12/AvatarOptimizer/pull/599)

## [1.5.4] - 2023-10-14
### Added
- Add compatibility for Satania's KiseteneEx [`#584`](https://github.com/anatawa12/AvatarOptimizer/pull/584)
Expand Down Expand Up @@ -587,7 +599,8 @@ The format is based on [Keep a Changelog].
- Merge Bone
- Clear Endpoint Position

[Unreleased]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.4...HEAD
[Unreleased]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.5...HEAD
[1.5.5]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.4...v1.5.5
[1.5.4]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.3...v1.5.4
[1.5.3]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.2...v1.5.3
[1.5.2]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.1...v1.5.2
Expand Down
17 changes: 9 additions & 8 deletions Editor/CheckForUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,22 @@ static async Task<string> GetLatestVersion(bool beta, string version)
beta
? "https://vpm.anatawa12.com/avatar-optimizer/beta/latest.txt"
: "https://vpm.anatawa12.com/avatar-optimizer/latest.txt";

var updatedAtKey =
beta
? "com.anatawa12.avatar-optimizer.beta.latest.updated"
: "com.anatawa12.avatar-optimizer.latest.updated";
var latestVersionKey =

var keyPrefix =
beta
? "com.anatawa12.avatar-optimizer.beta.latest.value"
: "com.anatawa12.avatar-optimizer.latest.value";
? "com.anatawa12.avatar-optimizer.beta.latest"
: "com.anatawa12.avatar-optimizer.latest";
var updatedAtKey = $"{keyPrefix}.updated";
var checkedWithKey = $"{keyPrefix}.checked-with";
var latestVersionKey = $"{keyPrefix}.value";

// fetch cached version
var cachedVersion = EditorPrefs.GetString(latestVersionKey);
if (!VersionRegex.IsMatch(cachedVersion))
cachedVersion = null;

if (cachedVersion != null
&& EditorPrefs.GetString(checkedWithKey, "") == CurrentVersionName
&& DateTime.TryParse(EditorPrefs.GetString(updatedAtKey, ""), out var updatedAt)
&& updatedAt >= DateTime.UtcNow - TimeSpan.FromHours(1))
{
Expand Down Expand Up @@ -135,6 +135,7 @@ static async Task<string> GetLatestVersion(bool beta, string version)
{
// we successfully fetched latest version!
EditorPrefs.SetString(latestVersionKey, fetchedLatestVersion);
EditorPrefs.SetString(checkedWithKey, CurrentVersionName);
EditorPrefs.SetString(updatedAtKey, DateTime.UtcNow.ToString("O"));
return fetchedLatestVersion;
}
Expand Down
18 changes: 16 additions & 2 deletions Editor/EditModePreview/MeshPreviewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private void Update()
{
var editorObj = ActiveEditor();
if (editorObj is GameObject go &&
go.GetComponent<SkinnedMeshRenderer>().sharedMesh &&
RemoveMeshPreviewController.EditorTypes.Any(t => go.GetComponent(t)))
{
StartPreview(go);
Expand All @@ -83,6 +84,7 @@ public enum PreviewState
PreviewAble,
PreviewingThat,

NoMesh,
PreviewingOther,
ActiveEditorMismatch,
}
Expand All @@ -99,8 +101,15 @@ private PreviewState StateForImpl([CanBeNull] Component component)
if (AnimationMode.InAnimationMode())
return PreviewState.PreviewingOther;

if (gameObject && ActiveEditor() as GameObject != gameObject)
return PreviewState.ActiveEditorMismatch;
if (gameObject)
{
if (ActiveEditor() as GameObject != gameObject)
return PreviewState.ActiveEditorMismatch;

var renderer = gameObject.GetComponent<SkinnedMeshRenderer>();
if (!renderer || !renderer.sharedMesh)
return PreviewState.NoMesh;
}

return PreviewState.PreviewAble;
}
Expand All @@ -125,6 +134,11 @@ private void ShowPreviewControlImpl(Component component)
Enabled = false;
}
break;
case PreviewState.NoMesh:
EditorGUI.BeginDisabledGroup(true);
GUILayout.Button("Preview (no Mesh)");
EditorGUI.EndDisabledGroup();
break;
case PreviewState.PreviewingOther:
EditorGUI.BeginDisabledGroup(true);
GUILayout.Button("Preview (other Previewing)");
Expand Down
6 changes: 4 additions & 2 deletions Editor/OptimizerPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ protected override void Configure()
InPhase(BuildPhase.Resolving)
.WithRequiredExtensions(new [] {typeof(BuildReportContext)}, seq =>
{
seq.Run("Info if AAO is Out of Date", _ =>
seq.Run("Info if AAO is Out of Date", ctx =>
{
if (CheckForUpdate.OutOfDate)
// we skip check for update
var components = ctx.AvatarRootObject.GetComponentInChildren<AvatarTagComponent>(true);
if (components && CheckForUpdate.OutOfDate)
BuildReport.LogInfo("CheckForUpdate:out-of-date",
CheckForUpdate.LatestVersionName, CheckForUpdate.CurrentVersionName);
})
Expand Down
6 changes: 4 additions & 2 deletions Editor/Processors/MergeBoneProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ protected override void Execute(BuildContext context)
// normalize map
mergeMapping.FlattenMapping();

if (mergeMapping.Count == 0) return;

BuildReport.ReportingObjects(context.GetComponents<SkinnedMeshRenderer>(), renderer =>
{
var meshInfo2 = context.GetMeshInfoFor(renderer);
Expand Down Expand Up @@ -160,12 +162,12 @@ private void DoBoneMap2(MeshInfo2 meshInfo2, Dictionary<Transform, Transform> me
vertex.Tangent = new Vector4(tangentVec3.x, tangentVec3.y, tangentVec3.z, vertex.Tangent.w);
foreach (var frames in vertex.BlendShapes.Values)
{
for (var i = 0; i < frames.Count; i++)
for (var i = 0; i < frames.Length; i++)
{
var frame = frames[i];
frames[i] = new Vertex.BlendShapeFrame(
weight: frame.Weight,
position: transBindPose.MultiplyPoint3x4(frame.Position),
position: transBindPose.MultiplyPoint3x3(frame.Position),
normal: transBindPose.MultiplyPoint3x3(frame.Normal),
tangent: transBindPose.MultiplyPoint3x3(frame.Tangent)
);
Expand Down
2 changes: 2 additions & 0 deletions Editor/Processors/MeshInfo2Holder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ internal class MeshInfo2Holder

public MeshInfo2Holder(GameObject rootObject)
{
var avatarTagComponent = rootObject.GetComponentInChildren<AvatarTagComponent>(true);
if (avatarTagComponent == null) return;
foreach (var renderer in rootObject.GetComponentsInChildren<SkinnedMeshRenderer>(true))
{
Profiler.BeginSample($"Read Skinned Mesh {renderer.name}");
Expand Down
65 changes: 42 additions & 23 deletions Editor/Processors/SkinnedMeshes/MeshInfo2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,44 @@ public void ReadSkinnedMesh([NotNull] Mesh mesh)

BlendShapes.Add((shapeName, 0.0f));

var shapes = new List<Vertex.BlendShapeFrame>[Vertices.Count];
var frameCount = mesh.GetBlendShapeFrameCount(i);

for (int frame = 0; frame < mesh.GetBlendShapeFrameCount(i); frame++)
var shapes = new Vertex.BlendShapeFrame[Vertices.Count][];
for (var vertex = 0; vertex < shapes.Length; vertex++)
shapes[vertex] = new Vertex.BlendShapeFrame[frameCount];

for (var frame = 0; frame < frameCount; frame++)
{
mesh.GetBlendShapeFrameVertices(i, frame, deltaVertices, deltaNormals, deltaTangents);
var weight = mesh.GetBlendShapeFrameWeight(i, frame);

for (var vertex = 0; vertex < deltaNormals.Length; vertex++)
{
if (deltaVertices[vertex] == Vector3.zero && deltaNormals[vertex] == Vector3.zero && deltaTangents[vertex] == Vector3.zero)
continue;
if (shapes[vertex] == null)
shapes[vertex] = new List<Vertex.BlendShapeFrame>();
shapes[vertex].Add(new Vertex.BlendShapeFrame(weight, deltaVertices[vertex],
deltaNormals[vertex], deltaTangents[vertex]));
}
var deltaVertex = deltaVertices[vertex];
var deltaNormal = deltaNormals[vertex];
var deltaTangent = deltaTangents[vertex];
shapes[vertex][frame] =
new Vertex.BlendShapeFrame(weight, deltaVertex, deltaNormal, deltaTangent);
}
}

for (var vertex = 0; vertex < shapes.Length; vertex++)
if (shapes[vertex] is List<Vertex.BlendShapeFrame> shapeFrames)
Vertices[vertex].BlendShapes[shapeName] = shapeFrames;
{
if (IsMeaningful(shapes[vertex]))
Vertices[vertex].BlendShapes[shapeName] = shapes[vertex];
}
}

bool IsMeaningful(Vertex.BlendShapeFrame[] frames)
{
foreach (var (_, position, normal, tangent) in frames)
{
if (position != Vector3.zero) return true;
if (normal != Vector3.zero) return true;
if (tangent != Vector3.zero) return true;
}

return false;
}
Profiler.EndSample();
Profiler.EndSample();
Expand Down Expand Up @@ -490,8 +507,9 @@ public void WriteToMesh(Mesh destMesh)
{
var vertex = Vertices[vertexI];

vertex.TryGetBlendShape(shapeName, weight, out var position, out var normal,
out var tangent);
vertex.TryGetBlendShape(shapeName, weight,
out var position, out var normal, out var tangent,
getDefined: true);
positions[vertexI] = position;
normals[vertexI] = normal;
tangents[vertexI] = tangent;
Expand Down Expand Up @@ -570,8 +588,8 @@ internal class Vertex
public List<(Bone bone, float weight)> BoneWeights = new List<(Bone, float)>();

// Each frame must sorted increasingly
public readonly Dictionary<string, List<BlendShapeFrame>> BlendShapes =
new Dictionary<string, List<BlendShapeFrame>>();
public readonly Dictionary<string, BlendShapeFrame[]> BlendShapes =
new Dictionary<string, BlendShapeFrame[]>();

public readonly struct BlendShapeFrame
{
Expand Down Expand Up @@ -633,7 +651,8 @@ public void SetTexCoord(int index, Vector4 value)
}
}

public bool TryGetBlendShape(string name, float weight, out Vector3 position, out Vector3 normal, out Vector3 tangent)
public bool TryGetBlendShape(string name, float weight, out Vector3 position, out Vector3 normal,
out Vector3 tangent, bool getDefined = false)
{
if (!BlendShapes.TryGetValue(name, out var frames))
{
Expand All @@ -643,15 +662,15 @@ public bool TryGetBlendShape(string name, float weight, out Vector3 position, ou
return false;
}

if (frames.Count == 0)
if (frames.Length == 0)
{
position = default;
normal = default;
tangent = default;
return false;
}

if (Mathf.Abs(weight) <= 0.0001f && ZeroForWeightZero())
if (!getDefined && Mathf.Abs(weight) <= 0.0001f && ZeroForWeightZero())
{
position = Vector3.zero;
normal = Vector3.zero;
Expand All @@ -661,7 +680,7 @@ public bool TryGetBlendShape(string name, float weight, out Vector3 position, ou

bool ZeroForWeightZero()
{
if (frames.Count == 1) return true;
if (frames.Length == 1) return true;
var first = frames.First();
var end = frames.Last();

Expand All @@ -672,7 +691,7 @@ bool ZeroForWeightZero()
return false;
}

if (frames.Count == 1)
if (frames.Length == 1)
{
// simplest and likely
var frame = frames[0];
Expand Down Expand Up @@ -713,13 +732,13 @@ bool ZeroForWeightZero()

// otherwise, lerp between two surrounding frames OR nearest two frames

for (var i = 1; i < frames.Count; i++)
for (var i = 1; i < frames.Length; i++)
{
if (weight <= frames[i].Weight)
return (frames[i - 1], frames[i]);
}

return (frames[frames.Count - 2], frames[frames.Count - 1]);
return (frames[frames.Length - 2], frames[frames.Length - 1]);
}

float InverseLerpUnclamped(float a, float b, float value) => (value - a) / (b - a);
Expand All @@ -744,7 +763,7 @@ private Vertex(Vertex vertex)
TexCoord7 = vertex.TexCoord7;
Color = vertex.Color;
BoneWeights = vertex.BoneWeights.ToList();
BlendShapes = new Dictionary<string, List<BlendShapeFrame>>(vertex.BlendShapes);
BlendShapes = new Dictionary<string, BlendShapeFrame[]>(vertex.BlendShapes);
}

public Vertex Clone() => new Vertex(this);
Expand Down
Loading

0 comments on commit c702efd

Please sign in to comment.