- Home /
Skinned Mesh how to connect bones to mesh help
Hi everyone!
I want to bind my mesh with script code to a bone animation! Most of the parts I understand, but I have trouble to find the connection between bones and mesh!
Hope someone can tell me, what I'm missing, or explain it to me. bones and BoneWeight seems logical not connected, but that can't be? Thx!
gameObject.AddComponent<Animation>();
SkinnedMeshRenderer renderer = gameObject.AddComponent<SkinnedMeshRenderer>();
/*MESH*/
Mesh sourceMesh = Instantiate(baseMesh) as Mesh;
sourceMesh.RecalculateNormals();
BoneWeight[] weights = new BoneWeight[sourceMesh.vertexCount];
for(int i = 0; i < weights.Length; i++){
weights[i].boneIndex0 = 0;
weights[i].weight0 = 1;
}
sourceMesh.boneWeights = weights;
/*SKELETON*/
Transform[] bones;
Transform sourceSkeleton = Instantiate(skeleton) as Transform;
sourceSkeleton.name = "skeleton";
sourceSkeleton.parent = gameObject.transform;
bones = gameObject.GetComponentsInChildren<Transform>();
/*BIND POSES*/
Matrix4x4[] bindPoses = new Matrix4x4[bones.Length];
for(int i = 0; i < bindPoses.Length; i++){
bindPoses[i] = bones[i].worldToLocalMatrix * transform.localToWorldMatrix;
}
/*MATERIAL*/
Material[] materials = new Material[3];
materials[0] = materials[1] = materials[2] = new Material(Shader.Find(" Diffuse"));
renderer.materials = materials;
/*END*/
sourceMesh.bindposes = bindPoses;
renderer.bones = bones;
renderer.sharedMesh = sourceMesh;
Hi DarkPhoenixX ! I try to do the exact same thing has you did. I would like to get more informations about that. Could we take contact!? Thanks! keven_poulin@hotmail.fr
Answer by Bunny83 · Oct 11, 2011 at 04:42 PM
BoneWeight.boneIndex0, 1, 2, 3 hold an index into the bones array to specify which bone(s) should be used by this vertex. BoneWeight.weight0, 1, 2, 3 is the corresponding weight for the bone. Note that the weights are normalized by Unity. So if you use 4 bones and you set all weights to 0.1 all 4 will be 0.25.
Finally the boneweights belong to the Mesh but the bones itself (the Transform array) belongs to the SkinnedMeshRenderer. When the skinnedMeshRenderer renders the Mesh it uses the boneweights of the mesh together with the bones array to skin the Mesh.
To conclude:
Each vertex have it's boneweigth. The boneweight's boneindex references a generic bones array which belongs to the Renderer.
The actual connection between them is this line:
renderer.sharedMesh = sourceMesh;
which tells the renderer to use this mesh.
Thanks Bunny83 for your great and quick answer. Your first sentence brought me enlightenment! ;)
Unity Docs aren't very useful, since this topic seems to be an underdog. It's great to have you people around!
Cheers, DarkPhoenixX
I'm struggling getting this to work. Could you post your code.
@Bunny83 could you please advise me in this question http://answers.unity3d.com/questions/974969/access-different-vertices-of-a-mecanim.html ?
@unity-user-12345: I'm currently in Greece and only have my tablet with me. I'll can help you once I'm back home ^^. However that will take about a week...
@Bunny83 no worries, please let me know, and hope you arrive safely :)