Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Ravart · Oct 11, 2011 at 02:22 PM · boneskeleton

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;
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scarpa · Feb 15, 2013 at 04:51 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ravart · Oct 11, 2011 at 08:43 PM 0
Share

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

avatar image Benjames · May 05, 2014 at 12:48 AM 0
Share

I'm struggling getting this to work. Could you post your code.

avatar image unity_user_12345 · May 28, 2015 at 04:29 AM 0
Share

@Bunny83 could you please advise me in this question http://answers.unity3d.com/questions/974969/access-different-vertices-of-a-mecanim.html ?

avatar image Bunny83 · May 28, 2015 at 01:19 PM 0
Share

@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...

avatar image unity_user_12345 · May 29, 2015 at 09:09 AM 0
Share

@Bunny83 no worries, please let me know, and hope you arrive safely :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you 'get' specific bones in a character skeleton via script. 2 Answers

Skinning character (attaching mesh to skeleton) 2 Answers

Swap mesh parts i.e. attach one skinned mesh to another via bone 1 Answer

How to move/rotate joints using c#? 1 Answer

is there a way to include bvh file in unity ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges