From 82b76481360aafce62ce5859ba7d5a5f2d1d8787 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Tue, 17 Oct 2023 11:04:31 +0900 Subject: [PATCH 1/7] fix: bindpose optimization may break mesh with bones with scale zero --- Editor/Math/Matrix3x3.cs | 4 ++++ Editor/Processors/MergeBoneProcessor.cs | 27 +++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Editor/Math/Matrix3x3.cs b/Editor/Math/Matrix3x3.cs index 8435f996c..f593bd25e 100644 --- a/Editor/Math/Matrix3x3.cs +++ b/Editor/Math/Matrix3x3.cs @@ -13,6 +13,10 @@ struct Matrix3x3 : IEquatable public static Matrix3x3 zero = new Matrix3x3(0, 0, 0, 0, 0, 0, 0, 0, 0); public static Matrix3x3 identity = new Matrix3x3(1, 0, 0, 0, 1, 0, 0, 0, 1); + public float determinant => + (m00 * m11 * m22) + (m01 * m12 * m20) + (m02 * m10 * m21) + - (m00 * m12 * m21) - (m01 * m10 * m22) - (m02 * m11 * m20); + // @formatter:off public float m00; public float m10; diff --git a/Editor/Processors/MergeBoneProcessor.cs b/Editor/Processors/MergeBoneProcessor.cs index a52c08e7d..0256cb223 100644 --- a/Editor/Processors/MergeBoneProcessor.cs +++ b/Editor/Processors/MergeBoneProcessor.cs @@ -123,7 +123,7 @@ private void DoBoneMap2(MeshInfo2 meshInfo2, Dictionary me else { // we assume fist bone we find is the most natural bone. - if (!primaryBones.ContainsKey(bone.Transform)) + if (!primaryBones.ContainsKey(bone.Transform) && ValidBindPose(bone.Bindpose)) primaryBones.Add(bone.Transform, bone); } } @@ -161,7 +161,7 @@ private void DoBoneMap2(MeshInfo2 meshInfo2, Dictionary 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.Length; i++) + for (var i = 0; i < frames.Length; i++) { var frame = frames[i]; frames[i] = new Vertex.BlendShapeFrame( @@ -204,6 +204,29 @@ private void DoBoneMap2(MeshInfo2 meshInfo2, Dictionary me } } + private bool ValidBindPose(Matrix4x4 matrix) + { + const float SMALL = 0.001f; + const float BIG = 10000; + + // if scaling part of bindpose is too small or too big, it can lead to invalid bind pose optimization + var scaling = Mathf.Abs(new Matrix3x3(matrix).determinant); + + if (float.IsInfinity(scaling)) return false; + if (float.IsNaN(scaling)) return false; + if (scaling < SMALL) return false; + if (scaling > BIG) return false; + + // if offset part of bindpose is too big, it may lead to invalid bind pose optimization + + var offset = matrix.offset; + if (Mathf.Abs(offset.x) > BIG) return false; + if (Mathf.Abs(offset.y) > BIG) return false; + if (Mathf.Abs(offset.z) > BIG) return false; + + return true; + } + private readonly struct BoneUniqKey : IEquatable { private readonly Matrix4x4 _bindPoseInfo; From aeb7c0bd9c7305f0d3985b34ac3102eb68726f2e Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Tue, 17 Oct 2023 11:07:20 +0900 Subject: [PATCH 2/7] fix: error spam from Preview system --- Editor/EditModePreview/MeshPreviewController.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Editor/EditModePreview/MeshPreviewController.cs b/Editor/EditModePreview/MeshPreviewController.cs index 5df181b39..cbcfc4fbb 100644 --- a/Editor/EditModePreview/MeshPreviewController.cs +++ b/Editor/EditModePreview/MeshPreviewController.cs @@ -70,6 +70,7 @@ private void Update() { var editorObj = ActiveEditor(); if (editorObj is GameObject go && + go.GetComponent() && go.GetComponent().sharedMesh && RemoveMeshPreviewController.EditorTypes.Any(t => go.GetComponent(t))) { From 53dcb77bb3faac82c50c7e59edcdcd54acbe46a1 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Tue, 17 Oct 2023 11:10:57 +0900 Subject: [PATCH 3/7] docs(changelog): BindPose Optimization may break mesh with scale 0 bone --- CHANGELOG-PRERELEASE.md | 1 + CHANGELOG.md | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG-PRERELEASE.md b/CHANGELOG-PRERELEASE.md index 7dc5c6391..58d54f3ec 100644 --- a/CHANGELOG-PRERELEASE.md +++ b/CHANGELOG-PRERELEASE.md @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog]. ### Removed ### Fixed +- BindPose Optimization may break mesh with scale 0 bone `#612` ### Security diff --git a/CHANGELOG.md b/CHANGELOG.md index fb593ed84..c45cb1c82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog]. - 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` +- BindPose Optimization may break mesh with scale 0 bone `#612` ### Security From 69db52e1337efbfb5796725258151e2f7af758e8 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Tue, 17 Oct 2023 11:15:11 +0900 Subject: [PATCH 4/7] docs(changelog): Error from Preview System when opening inspector of GameObject without SkinnedMeshRenderer --- CHANGELOG-PRERELEASE.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG-PRERELEASE.md b/CHANGELOG-PRERELEASE.md index 7dc5c6391..69ee48e12 100644 --- a/CHANGELOG-PRERELEASE.md +++ b/CHANGELOG-PRERELEASE.md @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog]. ### Removed ### Fixed +- Error from Preview System when opening inspector of GameObject without SkinnedMeshRenderer `#613` ### Security From 716ca93a04176323bf92458587e0cfd65f73476a Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Tue, 17 Oct 2023 11:30:29 +0900 Subject: [PATCH 5/7] chore: remove mesh not readable error --- Editor/Processors/SkinnedMeshes/MeshInfo2.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Editor/Processors/SkinnedMeshes/MeshInfo2.cs b/Editor/Processors/SkinnedMeshes/MeshInfo2.cs index 1df1216ac..a1c898118 100644 --- a/Editor/Processors/SkinnedMeshes/MeshInfo2.cs +++ b/Editor/Processors/SkinnedMeshes/MeshInfo2.cs @@ -37,11 +37,6 @@ public MeshInfo2(SkinnedMeshRenderer renderer) { SourceRenderer = renderer; var mesh = renderer.sharedMesh; - if (mesh && !mesh.isReadable) - { - BuildReport.LogFatal("The Mesh is not readable. Please Check Read/Write")?.WithContext(mesh); - return; - } BuildReport.ReportingObject(renderer, true, () => { @@ -79,11 +74,6 @@ public MeshInfo2(MeshRenderer renderer) { var meshFilter = renderer.GetComponent(); var mesh = meshFilter ? meshFilter.sharedMesh : null; - if (mesh && !mesh.isReadable) - { - BuildReport.LogFatal("The Mesh is not readable. Please Check Read/Write")?.WithContext(mesh); - return; - } if (mesh) ReadStaticMesh(mesh); From 67e06ca73a6c6c9b55d46f69b793acad1e7c3ffe Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Tue, 17 Oct 2023 11:41:07 +0900 Subject: [PATCH 6/7] docs(changelog): Error for Read/Write Mesh off Mesh --- CHANGELOG-PRERELEASE.md | 2 ++ CHANGELOG.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/CHANGELOG-PRERELEASE.md b/CHANGELOG-PRERELEASE.md index 8114d6d95..42b7b14b1 100644 --- a/CHANGELOG-PRERELEASE.md +++ b/CHANGELOG-PRERELEASE.md @@ -14,6 +14,8 @@ The format is based on [Keep a Changelog]. ### Deprecated ### Removed +- Error for Read/Write Mesh off Mesh `#615` + - Since AAO creates Mesh every time, no more error is required! ### Fixed - BindPose Optimization may break mesh with scale 0 bone `#612` diff --git a/CHANGELOG.md b/CHANGELOG.md index c45cb1c82..4061b5322 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ The format is based on [Keep a Changelog]. ### Deprecated ### Removed +- Error for Read/Write Mesh off Mesh `#615` + - Since AAO creates Mesh every time, no more error is required! ### Fixed - Multi-frame BlendShape can be broken `#601` From 012d264c740eb629aa2304df98daccb4ab478413 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 17 Oct 2023 02:52:31 +0000 Subject: [PATCH 7/7] chore: bump version to 1.5.6-rc.1 --- CHANGELOG-PRERELEASE.md | 16 +++++++++++----- package.json | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CHANGELOG-PRERELEASE.md b/CHANGELOG-PRERELEASE.md index 42b7b14b1..3a5524582 100644 --- a/CHANGELOG-PRERELEASE.md +++ b/CHANGELOG-PRERELEASE.md @@ -14,15 +14,20 @@ The format is based on [Keep a Changelog]. ### Deprecated ### Removed -- Error for Read/Write Mesh off Mesh `#615` - - Since AAO creates Mesh every time, no more error is required! ### Fixed -- BindPose Optimization may break mesh with scale 0 bone `#612` -- Error from Preview System when opening inspector of GameObject without SkinnedMeshRenderer `#613` ### Security +## [1.5.6-rc.1] - 2023-10-17 +### Removed +- Error for Read/Write Mesh off Mesh [`#615`](https://github.com/anatawa12/AvatarOptimizer/pull/615) + - Since AAO creates Mesh every time, no more error is required! + +### Fixed +- BindPose Optimization may break mesh with scale 0 bone [`#612`](https://github.com/anatawa12/AvatarOptimizer/pull/612) +- Error from Preview System when opening inspector of GameObject without SkinnedMeshRenderer [`#613`](https://github.com/anatawa12/AvatarOptimizer/pull/613) + ## [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) @@ -915,7 +920,8 @@ This release is mistake. - Merge Bone - Clear Endpoint Position -[Unreleased]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.6-beta.2...HEAD +[Unreleased]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.6-rc.1...HEAD +[1.5.6-rc.1]: https://github.com/anatawa12/AvatarOptimizer/compare/v1.5.6-beta.2...v1.5.6-rc.1 [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 diff --git a/package.json b/package.json index 88f652642..cf1e03195 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.anatawa12.avatar-optimizer", - "version": "1.5.6-beta.2", + "version": "1.5.6-rc.1", "unity": "2019.4", "description": "Set of Anatawa12's Small Avatar Optimization Utilities", "displayName": "Anatawa12's AvatarOptimizer",