SkinnedMeshRenderer.sharedMesh == no mesh [SOLVED]

I am trying to alter character’s mesh without spoiling the original asset (which IS modified when I access sharedMesh)…

But after character generation I see no mesh at all… what am I doing wrong?

#
Mesh new_mesh = new Mesh();
#
 
#
            int c0 = smr.sharedMesh.vertices.Length;
#
            new_mesh.vertices = new Vector3[c0];
#
            new_mesh.normals = new Vector3[c0];
#
            //new_mesh.colors = new Color[c0];
#
            new_mesh.uv = new Vector2[c0];
#
            for (int a = 0; a < c0; a++)
#
            {
#
                new_mesh.vertices[a] = smr.sharedMesh.vertices[a];
#
                new_mesh.normals[a] = smr.sharedMesh.normals[a];
#
                //new_mesh.colors[a] = smr.sharedMesh.colors[a];
#
                new_mesh.uv[a] = smr.sharedMesh.uv[a];
#
            }
#
 
#
            int tri_length = smr.sharedMesh.triangles.Length;
#
            new_mesh.triangles = new int[tri_length];
#
            for (int a = 0; a < tri_length; a++)
#
            {
#
                new_mesh.triangles[a] = smr.sharedMesh.triangles[a];
#
            }
#
 
#
            int c1 = smr.sharedMesh.bindposes.Length;
#
            new_mesh.bindposes = new Matrix4x4[c1];
#
            for (int a = 0; a < c1; a++)
#
                new_mesh.bindposes[a] = smr.sharedMesh.bindposes[a];
#
 
#
            int c2 = smr.sharedMesh.boneWeights.Length;
#
            new_mesh.boneWeights = new BoneWeight[c2];
#
            for (int a = 0; a < c2; a++)
#
                new_mesh.boneWeights[a] = smr.sharedMesh.boneWeights[a];
#
 
#
            smr.sharedMesh = new_mesh;

:frowning:

Ok, it seems I solved the problem.

I tried to modify vertices and other stuff like mesh.vertices[i], but I forgot that I need to create a buffer Vector3[] verts, copy vertices there and then do mesh.vertices = verts;

That helps for an ordinary mesh, but does not work with SMR…

Is there any way to alter UVs on SMR without damaging the original streamed asset?? :? :? :?

Ok, I am not sure what I did wrong in the previous piece of code - but I’ve just completely solved my problem with a “combine one mesh into one mesh”-workaround like this:

combineInstances = new List<CombineInstance>();
            for (int sub = 0; sub < smr.sharedMesh.subMeshCount; sub++)
            {
                CombineInstance ci = new CombineInstance();
                ci.mesh = smr.sharedMesh;
                ci.subMeshIndex = sub;
                combineInstances.Add(ci);
            }
            smr.sharedMesh = new Mesh();
            smr.sharedMesh.CombineMeshes(combineInstances.ToArray(), true, false);

i save the verts, uvs, etc from the skinned mesh renderer’s shared mesh at Awake(). The on OnDisable(), I restore them to the original values. so far, its worked for me as I work within the editor. I’m sure once the game is done, it will be fine as well.

Thanks so much the_gnoblin for finding this workaround all those years ago. SkinnedMeshRenderer still does not expose the mesh property, only sharedMesh. Your code is still extremely useful.

.mesh is essentially just a shortcut for Instantiate(sharedMesh). Which is a bit cleaner than doing the combine.

7347134--893819--upload_2021-7-20_22-12-35.png
reference from docs: https://docs.unity3d.com/ScriptReference/MeshFilter-mesh.html

1 Like