Skip to content

Commit

Permalink
Merge pull request #495 from anatawa12/merge-bone-with-extreamly-smal…
Browse files Browse the repository at this point in the history
…l-scale

fix: MergeBone may lost position information with extreamly small object
  • Loading branch information
anatawa12 authored Sep 21, 2023
2 parents eea9c05 + a9e1dbe commit c764df5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog].

### Fixed
- Left eye disappears `#493`
- MergeBone will loose transform information with extreamly small parent scale `#495`

### Security

Expand Down
25 changes: 16 additions & 9 deletions Editor/Math/Matrix4x4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ struct Matrix4x4 : IEquatable<Matrix4x4>

// @formatter:off
public float m00;
public float m01;
public float m02;
public float m03;
public float m10;
public float m11;
public float m12;
public float m13;
public float m20;
public float m21;
public float m22;
public float m23;
public float m30;
public float m01;
public float m11;
public float m21;
public float m31;
public float m02;
public float m12;
public float m22;
public float m32;
public float m03;
public float m13;
public float m23;
public float m33;
// @formatter:on

public Vector3 offset => new Vector3(m03, m13, m23);
public Quaternion rotation => ToUnity().rotation;
public Vector3 lossyScale => ToUnity().lossyScale;

public Vector3 MultiplyPoint3x4(Vector3 point)
{
Vector3 vector3;
Expand Down Expand Up @@ -156,5 +160,8 @@ public Matrix4x4(Vector4 column0, Vector4 column1, Vector4 column2, Vector4 colu
public override bool Equals(object obj) => obj is Matrix4x4 other && Equals(other);
public override int GetHashCode() => ToUnity().GetHashCode();
public override string ToString() => ToUnity().ToString();

public static Matrix4x4 TRS(Vector3 pos, Quaternion rot, Vector3 scale) => UnityMatrix4x4.TRS(pos, rot, scale);
public static Matrix4x4 TRS(Transform t) => UnityMatrix4x4.TRS(t.localPosition, t.localRotation, t.localScale);
}
}
12 changes: 9 additions & 3 deletions Editor/Processors/MergeBoneProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ public void Process(OptimizerSession session)
// if intermediate objects are inactive, moved bone should be initially inactive
// animations are not performed correctly but if bones activity is animated, automatic
// merge bone doesn't merge such bone so ignore that for manual merge bone.
var (activeSelf, namePrefix) = ActiveSelfAndNamePrefix(mapping, mapped);
var (activeSelf, namePrefix, matrix) = ComputeParentInformation(mapping, mapped);
foreach (var child in mapping.DirectChildrenEnumerable().ToArray())
{
if (mergeMapping.ContainsKey(child)) continue;
var mat = matrix * Matrix4x4.TRS(child);
child.parent = mapped;
child.localPosition = mat.offset;
child.localRotation = mat.rotation;
child.localScale = mat.lossyScale;
if (!activeSelf) child.gameObject.SetActive(false);
if (avoidNameConflict)
child.name = namePrefix + "$" + child.name + "$" + (counter++);
Expand All @@ -95,19 +99,21 @@ public void Process(OptimizerSession session)
if (pair)
Object.DestroyImmediate(pair.gameObject);

(bool activeSelf, string namePrefix) ActiveSelfAndNamePrefix(Transform transform, Transform parent)
(bool activeSelf, string namePrefix, Matrix4x4 matrix) ComputeParentInformation(Transform transform, Transform parent)
{
var segments = new List<string>();
var activeSelf = true;
var matrix = Matrix4x4.identity;
for (; transform != parent; transform = transform.parent)
{
segments.Add(transform.name);
activeSelf &= transform.gameObject.activeSelf;
matrix = Matrix4x4.TRS(transform) * matrix;
}

segments.Reverse();

return (activeSelf, string.Join("$", segments));
return (activeSelf, string.Join("$", segments), matrix);
}
}

Expand Down

0 comments on commit c764df5

Please sign in to comment.