Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity only draws first submesh

Discussion in 'Scripting' started by SinisterMephisto, Dec 18, 2014.

  1. SinisterMephisto

    SinisterMephisto

    Joined:
    Feb 18, 2011
    Posts:
    179
    I have a procedurally created skinned mesh

    Code (csharp):
    1.  
    2.  //create Game object and meshes etc
    3. Mesh mesh = new Mesh();
    4. mesh.name = new string(shape.name);
    5. mesh.vertices = verts.ToArray();
    6. mesh.subMeshCount = submesh.Count;
    7. for (int i = 0; i < submesh.Count; ++i)
    8. {
    9.     mesh.SetTriangles(submesh[i], i);
    10. }
    11.  
    12. mesh.normals = skNorms.ToArray();
    13. renderer.sharedMaterial = new Material(Shader.Find("Diffuse"));
    14. skRenderer.sharedMesh = mesh;
    15. mesh.boneWeights = weights.ToArray();
    16. mesh.bindposes = bindPoses;
    17. skRenderer.bones = bones.ToArray();
    18.  
    There are about 90 sub-meshes but only the first one is drawn
     
  2. SinisterMephisto

    SinisterMephisto

    Joined:
    Feb 18, 2011
    Posts:
    179
    I also tried this using the regular mesh filter + renderer combo. No luck
     
  3. SinisterMephisto

    SinisterMephisto

    Joined:
    Feb 18, 2011
    Posts:
    179
    I guess this is an editor mode problem.
    Unity does not instantiate sub meshes in editor mode
    So I had to "double buffer" to force initialization of the mesh then copy it over.
    Code (csharp):
    1.  
    2.  ////////////TRIS
    3.                     List<int> _tris = new List<int>();
    4.                     for (int i = 0; i < mesh.triangles.Length; i++)
    5.                     {
    6.                         int t = mesh.triangles[i];
    7.                         _tris.Add(t);
    8.                     }
    9.                     mesh.triangles = _tris.ToArray();
    10.  
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Submeshes require a material array. So renderer.sharedMaterials, rather than renderer.sharedMaterial.

    --Eric
     
  5. SinisterMephisto

    SinisterMephisto

    Joined:
    Feb 18, 2011
    Posts:
    179
    But Meshes draw without materials
    Mine didn't even show the pink thing. It showed nothing at all except the first sub-mesh

    Actually that worked.
     
    Last edited: Dec 21, 2014