mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-13 14:29:38 -05:00
fix BoneNormalizer
This commit is contained in:
@@ -51,11 +51,7 @@ namespace UniGLTF.MeshUtility
|
||||
/// <param name="bakeCurrentBlendShape">BlendShapeを0クリアするか否か。false の場合 BlendShape の現状を Bake する</param>
|
||||
/// <param name="createAvatar">Avatarを作る関数</param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<Transform, MeshAttachInfo> NormalizeHierarchyFreezeMesh(
|
||||
GameObject go,
|
||||
bool removeScaling = true,
|
||||
bool removeRotation = true
|
||||
)
|
||||
public static Dictionary<Transform, MeshAttachInfo> NormalizeHierarchyFreezeMesh(GameObject go)
|
||||
{
|
||||
//
|
||||
// 各メッシュから回転・スケールを取り除いてBinding行列を再計算する
|
||||
@@ -76,29 +72,11 @@ namespace UniGLTF.MeshUtility
|
||||
bool FreezeRotation, bool FreezeScaling)
|
||||
{
|
||||
var boneMap = go.transform.Traverse().ToDictionary(x => x, x => new EuclideanTransform(x.rotation, x.position));
|
||||
// Func<Transform, Transform> getSrc = dst =>
|
||||
// {
|
||||
// foreach (var (k, v) in boneMap)
|
||||
// {
|
||||
// if (v == dst)
|
||||
// {
|
||||
// return k;
|
||||
// }
|
||||
// }
|
||||
// throw new NotImplementedException();
|
||||
// };
|
||||
|
||||
foreach (var (src, tr) in boneMap)
|
||||
// first, update hierarchy
|
||||
foreach (var src in go.transform.Traverse())
|
||||
{
|
||||
src.position = tr.Translation;
|
||||
if (FreezeRotation)
|
||||
{
|
||||
src.rotation = Quaternion.identity;
|
||||
}
|
||||
else
|
||||
{
|
||||
src.rotation = tr.Rotation;
|
||||
}
|
||||
var tr = boneMap[src];
|
||||
if (FreezeScaling)
|
||||
{
|
||||
src.localScale = Vector3.one;
|
||||
@@ -108,6 +86,21 @@ namespace UniGLTF.MeshUtility
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
if (FreezeRotation)
|
||||
{
|
||||
src.rotation = Quaternion.identity;
|
||||
}
|
||||
else
|
||||
{
|
||||
src.rotation = tr.Rotation;
|
||||
}
|
||||
|
||||
src.position = tr.Translation;
|
||||
}
|
||||
|
||||
// second, replace mesh
|
||||
foreach (var (src, tr) in boneMap)
|
||||
{
|
||||
if (newMesh.TryGetValue(src, out var info))
|
||||
{
|
||||
info.ReplaceMesh(src.gameObject);
|
||||
|
||||
@@ -160,9 +160,7 @@ namespace UniGLTF.MeshUtility
|
||||
if (FreezeBlendShape || FreezeRotation || FreezeScaling)
|
||||
{
|
||||
// MeshをBakeする
|
||||
var newMesh = BoneNormalizer.NormalizeHierarchyFreezeMesh(go,
|
||||
removeScaling: FreezeScaling,
|
||||
removeRotation: FreezeRotation);
|
||||
var newMesh = BoneNormalizer.NormalizeHierarchyFreezeMesh(go);
|
||||
|
||||
// - ヒエラルキーから回転・拡縮を除去する
|
||||
// - BakeされたMeshで置き換える
|
||||
|
||||
@@ -161,8 +161,6 @@ namespace UniGLTF.MeshUtility
|
||||
weight3 = 0.0f,
|
||||
};
|
||||
srcMesh.boneWeights = Enumerable.Range(0, srcMesh.vertexCount).Select(x => bw).ToArray();
|
||||
srcMesh.bindposes = new Matrix4x4[] { Matrix4x4.identity };
|
||||
|
||||
src.rootBone = src.transform;
|
||||
src.bones = new[] { src.transform };
|
||||
src.sharedMesh = srcMesh;
|
||||
|
||||
Reference in New Issue
Block a user