Skip to content

Instantly share code, notes, and snippets.

@yutopp
Created May 5, 2021 17:22
Show Gist options
  • Save yutopp/65d504c1ed3d8e36c49c047e1ddd14c1 to your computer and use it in GitHub Desktop.
Save yutopp/65d504c1ed3d8e36c49c047e1ddd14c1 to your computer and use it in GitHub Desktop.
//var matrix = Matrix4x4.identity;
//matrix.SetTRS(bones[1].localPosition, bones[1].localRotation, bones[1].localScale);
var boneTrans = smr.bones.Select(bt => Export(bt)).ToArray();
var boneIndices = boneTrans.Select(t => t.Index).ToArray();
int? matricesAccIndex = null;
if (mesh.bindposes.Length > 0)
{
var localToWorldT = Matrix4x4.Translate(trans.position);
var localToWorldR = Matrix4x4.Rotate(trans.rotation);
var localToWorldS = Matrix4x4.Scale(trans.lossyScale);
if (trans.localToWorldMatrix != Matrix4x4.TRS(trans.position, trans.rotation, trans.lossyScale))
{
throw new Exception();
}
var calculatedBindposes = boneTrans.Select((bt, i) =>
{
var boneWorldToLocalT = Matrix4x4.Translate(bt.Value.position).inverse;
var worldToLocalRot = Matrix4x4.Rotate(Quaternion.Inverse(bt.Value.rotation));
var q = bt.Value.lossyScale;
var worldToLocalScale = Matrix4x4.Scale(new Vector3(1f / q.x, 1f / q.y, 1f / q.z));
var bindPoseT = CoordUtils.GetTranslate(boneWorldToLocalT * localToWorldT);
var bindPoseRot = CoordUtils.GetRotation(worldToLocalRot * localToWorldR);
var bindPoseScale = CoordUtils.GetScale(worldToLocalScale * localToWorldS);
var b = Matrix4x4.TRS(bindPoseT, bindPoseRot, bindPoseScale);
Debug.Log($"==> {mesh.bindposes[i]} ??? {mesh.bindposes[i]}");
return new BindposeTRS {
Translate = bindPoseT,
Rotation = bindPoseRot,
Scale = bindPoseScale,
};
}).ToArray();
if (mesh.bindposes.Count() != calculatedBindposes.Count())
{
throw new Exception($"Number of bindposes: original({mesh.bindposes.Count()}) != calculated({calculatedBindposes.Count()})");
}
foreach (var (b, i) in mesh.bindposes.Select((b, i) => (b, i)))
{
var cb = calculatedBindposes[i];
if (!CoordUtils.GetTranslate(b).Approximately(cb.Translate))
{
Debug.LogWarning($"{i}({boneTrans[i].Value.name}) => {CoordUtils.GetTranslate(b)} != {cb.Translate}");
}
if (CoordUtils.GetRotation(b) != cb.Rotation)
{
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment