Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Is it possible to turn a meshRenderer to skinnermeshRenderer

Discussion in 'Animation' started by shibi2017, Mar 17, 2022.

  1. shibi2017

    shibi2017

    Joined:
    Jan 18, 2018
    Posts:
    153
    For example, I got 2 fbx file.
    The mesh information is same: a simple t-pose human.We call them meshA(rigged) and meshB(not eigged).
    and I improt two files to unity, one should work with skinnermeshrenerer and I set it to humanoid type, the other work with meshrenderer.

    But, can I export the bones. boneweights and bindpose data from meshA and copy them to a new skinnermesh with meshB as a sharedmesh?

    In the unity api, there is a code example in api: bindpose to create a skinnermeshrenderer by code:
    https://docs.unity3d.com/ScriptReference/Mesh-bindposes.html

    and I change the code to export data from meshA and copy to meshB, some code here:
    export the data is simle, just declare an array and stroe them:
    Code (CSharp):
    1.         Transform[] BoneData = activeGameObject.GetComponent<SkinnedMeshRenderer>().bones;
    2.         Matrix4x4[] BindPoseData = activeGameObject.GetComponent<SkinnedMeshRenderer>().sharedMesh.bindposes;
    3.         BoneWeight[] BoneWeightsData = activeGameObject.GetComponent<SkinnedMeshRenderer>().sharedMesh.boneWeights;
    and copy:
    Code (CSharp):
    1. activeGameObject.AddComponent<Animation>();
    2.         if (activeGameObject.GetComponent<SkinnedMeshRenderer>())
    3.         {
    4.  
    5.         }
    6.         else
    7.         {
    8.             activeGameObject.AddComponent<SkinnedMeshRenderer>(); // auto replace the MeshRenderer
    9.         }
    10.         SkinnedMeshRenderer rend = activeGameObject.GetComponent<SkinnedMeshRenderer>();
    11.         Animation anim = activeGameObject.GetComponent<Animation>();
    12.  
    13.         // build the mesh
    14.         Mesh mesh = new Mesh();
    15.         mesh = rend.sharedMesh;
    16.         // assign bone weights to mesh
    17.         BoneWeight[] copyBoneWeights = _CustomData.boneData.boneWeights;
    18.         int safeLength =0;
    19.         mesh.boneWeights = new BoneWeight[mesh.vertices.Length];
    20. mesh.boneWeights = copyBoneWeights;

    In my testing, when I copy data to meshB, it seems work well and no error or warnings. But visuially the meshB disappear. I can not tell why.
    So, is it possible do this with the method mentioned above?