Search Unity

Small problem when resource loading a second mesh into a submesh

Discussion in 'Scripting' started by miniduck, Mar 14, 2013.

  1. miniduck

    miniduck

    Joined:
    Sep 27, 2012
    Posts:
    117
    Hi all!
    I'm trying to load a second mesh into a submesh but it's failing. I've created a super simple scene to try to get it right but still failing.
    I have two fbx triangles in my project. When running it only shows the first one - the print out bellow looks correct to me but the inspector for the created mesh in the gameobject during runtime says 6 verts and 1(!) poly.

    It's attached to an empty gameobject and running during Start().

    Prob something simple very simple... :D

    Thanks alot for taking the time!

    This is my code:
    Code (csharp):
    1.         Mesh base_mesh = gameObject.GetComponent<MeshFilter>().mesh; //just an empty meshfilter added in editor already
    2.        
    3.         Mesh tri = (Mesh)Resources.Load("triangle", typeof(Mesh));
    4.         Mesh tri2 = (Mesh)Resources.Load("triangle2", typeof(Mesh));       
    5.        
    6.         List<Vector3> all_vertices = new List<Vector3>();
    7.         all_vertices.AddRange(tri.vertices);
    8.         all_vertices.AddRange(tri2.vertices);
    9.        
    10.         base_mesh.vertices = all_vertices.ToArray();
    11.         base_mesh.subMeshCount = 2;
    12.  
    13.         Vector2[] uvs = new Vector2[base_mesh.vertices.Length];        
    14.         for (int i = 0; i < base_mesh.vertices.Length; i++)
    15.             uvs[i] = new Vector2 (base_mesh.vertices[i].x, base_mesh.vertices[i].z);
    16.         base_mesh.uv = uvs;
    17.                
    18.         Material[] materials = new Material[2];
    19.         for(int i = 0; i < materials.Length ; i++)
    20.             materials[i] = Resources.Load("Materials/building",typeof(Material)) as Material;
    21.         renderer.materials = materials;
    22.        
    23.         base_mesh.SetTriangles(tri.triangles,0);
    24.        
    25.         int[] tri2_triangles = tri2.triangles;     
    26.         for (int i = 0; i < tri2_triangles.Length; i++)
    27.             tri2_triangles[i] = tri2_triangles[i] + 3; // offset by tri verts
    28.         base_mesh.SetTriangles(tri2_triangles,1);
    29.        
    30.         base_mesh.RecalculateBounds();
    31.         base_mesh.RecalculateNormals();
    32.        
    33.         gameObject.GetComponent<MeshFilter>().mesh = base_mesh;
    34.        
    35.  
    36.        
    37.         print ("Just checking...");
    38.         print("Total vertices count: " + base_mesh.vertices.Length);
    39.         for(int i = 0; i < base_mesh.vertices.Length ; i ++)
    40.             print("\tvert " + i + ": " + base_mesh.vertices[i]);
    41.         for (int sub_count = 0; sub_count < base_mesh.subMeshCount; sub_count++)
    42.         {
    43.             print ("Submesh " + sub_count + ":");
    44.             int[] sub_tris = base_mesh.GetTriangles(sub_count);
    45.             for(int i = 0; i < sub_tris.Length ; i ++)
    46.                 print ("\t " + i + ": using vert " + sub_tris[i]);
    47.         }
    What it prints:
    Code (csharp):
    1. Just checking...
    2. Total vertices count: 6
    3.     vert 0: (5.0, 4.0, 5.0)
    4.     vert 1: (-5.0, 4.0, 5.0)
    5.     vert 2: (5.0, 4.0, -5.0)
    6.     vert 3: (-5.0, 4.0, -5.0)
    7.     vert 4: (5.0, 4.0, -5.0)
    8.     vert 5: (-5.0, 4.0, 5.0)
    9. Submesh 0:
    10.      0: using vert 0
    11.      1: using vert 2
    12.      2: using vert 1
    13. Submesh 1:
    14.      0: using vert 3
    15.      1: using vert 5
    16.      2: using vert 4
     
    Last edited: Mar 14, 2013
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    If I'm doing it right in my head, one of those triangles is backwards from the other. Are you sure you're not just seeing the front of one and (not seeing) the back of the other?
     
  3. miniduck

    miniduck

    Joined:
    Sep 27, 2012
    Posts:
    117
    Thanks for looking!

    Actually the order on the second was wrong. Right indexes but just the wrong order (not inverted)... Very strange unity didnt pick it up and would render a faulty one..

    Setting them manually fixed it..

    Cheers