Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by sunilnair · Jun 10, 2016 at 01:06 PM · meshmecanimhumanoidskinnedmeshrendererboneweights

Binding a mecanim skeleton to a new mesh

Hi,

I am trying to add the skeleton to a new mesh in my scene. The original mesh contains the skeleton and the model moves according to the bone position and rotation. I now have a new mesh without a skeleton. I want to skin this new mesh to this existing skeleton. Below is my code where I copy the bone weights and add the skinned mesh renderer.

It doesn't work. It deforms my new mesh terribly. Just for background, my new mesh is just an x-scaled version of the old mesh without the skeleton attached.

  void Start () 
     {
         vertices = mesh.vertices;
 
         //Get original mesh bone weights
         weights=new BoneWeight[mesh.vertexCount];
         weights = mesh.boneWeights;
         
         // Get original mesh Renderer
         top = this.transform.Find("Tops").gameObject;
         SkinnedMeshRenderer meshRend = top.GetComponent<SkinnedMeshRenderer> ();
 
         //Get original mesh Skeleton
         Transform[] originalMeshBones;
         originalMeshBones = meshRend.bones;
         Matrix4x4[] bindPoses = mesh.bindposes;
         for(int i = 0; i < 16; i++){
             bindPoses[i] = originalMeshBones[i].worldToLocalMatrix * transform.localToWorldMatrix;
         }
 
         //Blend Mesh settings; tshirt is the blend mesh game object          
         tshirt.AddComponent<Animator>();
         tshirt.GetComponent<Animator> ().avatar = humanAvatar;//human avatar is the original mesh avatar
         tshirt.GetComponent<Animator> ().applyRootMotion = true;
 
 
         tshirt = tshirt.transform.Find ("Tops").gameObject;
 
                //add skinned mesh renderer component to the new(blend) mesh
         blendMeshRend = tshirt.AddComponent<SkinnedMeshRenderer> ();
         
                 //copy the bone weights and bind poses and bones
         BoneWeight[] blendMeshWeights=new BoneWeight[mesh.vertexCount];
         blendMesh.boneWeights = weights;
         Debug.Log ("vertex 2: " + blendMesh.boneWeights [2].weight2 + "vertex 100:" + blendMesh.boneWeights [100].weight0);
 
         blendMesh.bindposes = bindPoses;
         Debug.Log ("bind poses 0"+blendMesh.bindposes [7].m00);
         blendMeshRend.bones = originalMeshBones;
 
         //tshirt.transform.parent = this.transform;
         blendMeshRend.sharedMesh = blendMesh;
         blendMeshRend.rootBone = meshRend.rootBone;
     }
Comment
Add comment · Show 2
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 Cherno · Jun 13, 2016 at 11:34 AM 0
Share

Both rigs need to have to same hierarchy and number of bones.

http://answers.unity3d.com/questions/44355/shared-skeleton-and-animation-state.html

avatar image sunilnair · Jun 13, 2016 at 11:45 AM 0
Share

I have just one rig: the main skeleton. I need to bind a mesh (which is not skinned currently) to this skeleton.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xXx12aithexXx · Jun 10, 2016 at 03:18 PM

why not just bind the bones from a 3D editor ? and switch out the mesh .... I like were you're going with this so I'm guessing there is a reason to make it more complex?

Comment
Add comment · Show 1 · 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 sunilnair · Jun 13, 2016 at 11:20 AM 0
Share

Hi, Thanks for your comment. I need to change the bone positions of the skeleton in my script at run time. Since I update the skeleton in script, I cannot bind it in a 3D editor before. Now, if I change the joint positions with the new mesh attached, it will further deform the mesh. This is what I want to:

  1. Update the bone positions of the skeleton.

  2. Bind the new mesh (which correctly matches the new bone positions) to this skeleton

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

Wrong bindPoses with generated mesh 1 Answer

Why do legacy & humanoid animations look different on the same character? 1 Answer

Why does my Blender model get messed up? 0 Answers

Converting Bootcamp soldier to Mecanim 1 Answer

Mecanim: undesired mesh intersections 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