BoneWeight の無い SkinndedMeshRenderer への bake transform

This commit is contained in:
ousttrue 2023-12-08 18:30:48 +09:00
parent 6e13c87c00
commit cf4dfb0fdb
2 changed files with 12 additions and 5 deletions

View File

@ -76,9 +76,12 @@ namespace UniGLTF.MeshUtility
return dst;
}
public static void ApplyRotationAndScale(this Mesh src, Matrix4x4 m)
public static void ApplyRotationAndScale(this Mesh src, Matrix4x4 m, bool removeTranslation = true)
{
m.SetColumn(3, new Vector4(0, 0, 0, 1)); // remove translation
if (removeTranslation)
{
m.SetColumn(3, new Vector4(0, 0, 0, 1)); // remove translation
}
src.ApplyMatrix(m);
}

View File

@ -147,9 +147,13 @@ namespace UniGLTF.MeshUtility
if (!hasBoneWeight)
{
// Before bake, bind no weight bones
//Debug.LogFormat("no weight: {0}", srcMesh.name);
srcMesh = srcMesh.Copy(true);
var rootBone = src.rootBone ?? src.transform;
srcMesh.ApplyRotationAndScale(rootBone.localToWorldMatrix, false);
Debug.LogFormat($"apply: {src.transform} {rootBone}");
var bw = new BoneWeight
{
boneIndex0 = 0,
@ -162,8 +166,8 @@ namespace UniGLTF.MeshUtility
weight3 = 0.0f,
};
srcMesh.boneWeights = Enumerable.Range(0, srcMesh.vertexCount).Select(x => bw).ToArray();
src.rootBone = src.transform;
src.bones = new[] { src.transform };
src.rootBone = rootBone;
src.bones = new[] { rootBone };
src.sharedMesh = srcMesh;
}