- Home /
Some submeshes of my gameobject dissapears when I set the bindposes
Well, I'm trying to animate some Source models imported into Unity3D using this Unity-Source-Tools library:
https://github.com/lewa-j/Unity-Source-Tools
To animate a Mesh generated by this tool I need to create a SkinnedMeshRenderer
replacing the MeshRenderer
and the MeshFilter
components, so I created this method:
private static void ReplaceRenderer(this GameObject obj, SourceStudioModelv2 model)
{
if (obj.GetComponent<SkinnedMeshRenderer>() != null)
return;
var mesh = Object.Instantiate(obj.GetComponent<MeshFilter>().sharedMesh);
const bool debugPoses = true;
const bool useNativePoses = true;
var nativeBuilder = debugPoses ? new StringBuilder($"Native Matrix\n{new string('=', 10)}\n") : null;
var unityBuilder = debugPoses ? new StringBuilder($"Unity Matrix\n{new string('=', 10)}\n") : null;
var nativeMatrix = model.GetBindPosesFromModel(nativeBuilder).ToArray();
var unityMatrix = obj.transform.GetBindPoses(model.Bones, unityBuilder).ToArray();
if (debugPoses)
{
Debug.Log(nativeBuilder.ToString());
Debug.Log(unityBuilder.ToString());
}
var matrix = useNativePoses
? nativeMatrix
: unityMatrix;
mesh.bindposes = matrix;
Object.Destroy(obj.GetComponent<MeshFilter>());
Object.Destroy(obj.GetComponent<MeshRenderer>());
var skinned = obj.AddComponent<SkinnedMeshRenderer>();
skinned.sharedMesh = mesh;
skinned.rootBone = obj.transform;
skinned.bones = model.Bones;
}
As you can see, I'm creating two bind poses, because one of them is created from this part of the code: https://github.com/lewa-j/Unity-Source-Tools/blob/c5469deb1d630c1582c940ff3cdb0f69fc55eda3/Assets/Code/Read/SourceStudioModel.cs#L724-L728
And this is how I create the bindposes:
Native Poses
private static IEnumerable<Matrix4x4> GetBindPosesFromModel(this SourceStudioModelv2 model, StringBuilder sb = null)
{
for (var i = 0; i < model.MdlBones.Length; i++)
{
var mdlBone = model.MdlBones[i];
// From: https://stackoverflow.com/a/49268287/3286975
var matrix = (new Matrix4x4(
new Vector4(mdlBone.PoseToBone[0], mdlBone.PoseToBone[1], mdlBone.PoseToBone[2],
mdlBone.PoseToBone[3]),
new Vector4(mdlBone.PoseToBone[4], mdlBone.PoseToBone[5], mdlBone.PoseToBone[6],
mdlBone.PoseToBone[7]),
new Vector4(mdlBone.PoseToBone[8], mdlBone.PoseToBone[9], mdlBone.PoseToBone[10],
mdlBone.PoseToBone[11]),
new Vector4(0, 0, 0, 1)
)).transpose;
sb?.AppendLine($"{i}: {matrix.ToString("F2")}");
yield return matrix;
}
}
Unity Poses
private static IEnumerable<Matrix4x4> GetBindPoses(this Transform transform, Transform[] bones, StringBuilder sb = null)
{
for (var i = 0; i < bones.Length; i++)
{
var bone = bones[i];
var matrix = bone.worldToLocalMatrix * transform.localToWorldMatrix;
sb?.AppendLine($"{i}: {matrix.ToString("F2")}");
yield return matrix;
}
}
And these are the values that each of them gives to me:
Unity Bindposes
And this is the result:
Native Bindposes
But this one doesn't shows anything.
I saw this reference on internet: https://hg.alliedmods.net/hl2sdks/hl2sdk/file/4966093d3274/utils/studiomdl/simplify.cpp#l3745
But I'm not sure if I'll be able to transcript this into Unity and if it will be useful...
If you would like to test this model out I created a *.unitypackage
file for you to test:
https://mega.nz/file/xKpRSYwJ#j3ckM3GyFDFZKI6Y-OvN2cmcGC_GudGe2finLRQoA4U
So I don't know how to proceed in this case, could anybody help me out with this?