- Home /
SkinnedMeshRenderer combiner produces invisible mesh
Years ago I found working skinned mesh combiner from https://github.com/unity-hackday/project/blob/master/unity/Assets/Plugins/CharacterGenerator.cs and modified it for my needs by taking just the generating part as here:
public void Generate()
{
List<CombineInstance> combineInstances = new List<CombineInstance>();
List<Material> materials = new List<Material>();
List<Transform> bones = new List<Transform>();
Transform[] transforms = gameObject.GetComponentsInChildren<Transform>();
foreach (SkinnedMeshRenderer smr in allSmrComponents)
{
materials.AddRange(smr.materials);
for (int sub = 0; sub < smr.sharedMesh.subMeshCount; sub++)
{
CombineInstance ci = new CombineInstance();
ci.mesh = smr.sharedMesh;
ci.transform = smr.transform.localToWorldMatrix;
ci.subMeshIndex = sub;
combineInstances.Add(ci);
}
Transform[] meshBones = smr.bones;
foreach (Transform bone in meshBones)
{
foreach (Transform transform in transforms)
{
if (transform.name != bone.name)
continue;
bones.Add(transform);
break;
}
}
smr.gameObject.SetActive(false);
}
combinedGO = new GameObject("Combined Mesh");
combinedGO.layer = gameObject.layer;
SkinnedMeshRenderer renderer = combinedGO.AddComponent<SkinnedMeshRenderer>();
renderer.sharedMesh = new Mesh();
renderer.sharedMesh.name = "combinedMesh";
renderer.sharedMesh.CombineMeshes(combineInstances.ToArray(), false, false);
renderer.bones = bones.ToArray();
renderer.materials= new Material[1] { materials[0] };
}
After I updated Unity, no idea to which version, probably it was when Unity5 became Unity 20XX.Y.Y. the combiner stopped working. No errors appeared but the gameobject "Combined Mesh" was invisible, also I think some values were wrong i.e SMR bounds seem to be zeroed and root bone missing.
I figured maybe recalculating bounds
renderer.sharedMesh.RecalculateNormals();
renderer.sharedMesh.RecalculateBounds();
renderer.rootBone = myRootBone;
but they were still zero. I tried debugging if any mesh information is passed to SMR and at least vertex count showed 10k, so there is something.
In short: can you figure what's wrong in the combining the smr here? This was one day a working piece of code.
Your answer
![](https://koobas.hobune.stream/wayback/20220612221029im_/https://answers.unity.com/themes/thub/images/avi.jpg)