Adding to SkinnedMeshRenderer some MeshFilters

I’m trying to add 3 MeshFilters to one SkinnedMeshRenderer.

There it my code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class MyBakeScript : MonoBehaviour {

    public SkinnedMeshRenderer Lod;
    public MeshFilter[] meshFilters;
    public Texture2D[] mainTextures;
    private CombineInstance[] combine;

	void Update () 
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            Matrix4x4 myTransform = transform.worldToLocalMatrix;
            combine = new CombineInstance[meshFilters.Length + 3];

            int counter = 0;
            for (int i = 0; i < meshFilters.Length; i++)
            {
                combine_.mesh = meshFilters*.sharedMesh;*_

combine_.transform = meshFilters*.transform.localToWorldMatrix;
//meshFilters.gameObject.SetActive(false);
counter++;
}*_

for (int sub = 0; sub < Lod.sharedMesh.subMeshCount; sub++)
{
combine[counter].mesh = Lod.sharedMesh;
combine[counter].transform = Lod.transform.localToWorldMatrix;
combine[counter].subMeshIndex = sub;
counter++;
}

var go = new GameObject(“Combined mesh”);
go.transform.parent = transform;
go.transform.localPosition = Vector3.zero;
go.transform.localRotation = Quaternion.identity;
go.transform.localScale = Vector3.one;

var smr = go.AddComponent();

//Generate atlas
Texture2D atlas = new Texture2D(1024, 1024);
Rect[] uvs = atlas.PackTextures(mainTextures, 1, 1024);
Material mat = new Material(Shader.Find(“Armor”));
mat.SetTexture(“_MainTex”, atlas);
mat.SetTexture(“_MaskTex”, atlas);
go.renderer.sharedMaterial = mat;

smr.sharedMesh = new Mesh();
smr.sharedMesh.CombineMeshes(combine, true, true);
smr.sharedMesh.boneWeights = Lod.sharedMesh.boneWeights;
smr.bones = Lod.bones;
List bindposes = new List();
for (int i = 0; i < Lod.bones.Length; i++)
{
bindposes.Add(Lod.bones_.worldToLocalMatrix * transform.localToWorldMatrix);
}
smr.sharedMesh.bindposes = bindposes.ToArray();
smr.sharedMesh.uv = MyRecalculateUVs(smr, uvs).ToArray();
}
}_

List MyRecalculateUVs(SkinnedMeshRenderer smr, Rect[] uvs)
{
Vector2[] uva = smr.sharedMesh.uv;
List resultUV = new List();

int counter = 0;

for (int i = 0; i < combine.Length; i++)
{
for(int j = 0; j < combine*.mesh.uv.Length; j++)*
{
resultUV.Add(new Vector2((uva[counter].x * uvs.width) + uvs.x, (uva[counter].y * uvs_.height) + uvs*.y));
counter++;
}
}
return resultUV;
}
}
As a result, a got an error:
![alt text][1]
[1]: http://dl1.joxi.net/drive/0001/1192/99496/150302/ccc4023a77.jpg*_

I’m understand, that count of total boneWeight is less then total count of vertices, because i added new mesh with new vertices. But how i can calculate and add missing boneWeights?

First of all: you messed something up:

  • You use MeshFilter with MeshRenderer together if you want to render rigid Mesh
  • OR you use SkinnedMeshRenderer if you want to render skinned mesh (or mesh with BlendShapes)

I don’t see why you need to mix those up. I think you should just use MeshRenderer and then you won’t need to set bondWeights.