Search Unity

Unity doesn't display runtime generated mesh

Discussion in 'Scripting' started by kamac, Jul 22, 2014.

  1. kamac

    kamac

    Joined:
    Mar 21, 2014
    Posts:
    13
    Hey there.

    I generate a mesh at runtime. When I assign vertices, normals and indices (triangles) to my mesh, and call RecalculateBounds, it returns center: (0,0,0) extents: (0,0,0) and I cannot see my object.

    So, I've tried exporting my data to .obj format and loading it up in various viewers.
    I've tried blender and two online 3D model viewers and my model was loaded up and displayed correctly.

    Then, I've tried to load up that generated .obj file to Unity, and it, too, did display it correctly (but as I guess it did some changes to it).

    My question is- what could be wrong, and how could I fix it? What changes does Unity commit to my file so that it is able to display it?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Post code bra
     
  3. kamac

    kamac

    Joined:
    Mar 21, 2014
    Posts:
    13
    It's too complex :/


    #Edit
    I am binding most functions from C DLL, so it looks like this:

    Code (CSharp):
    1.  
    2. var omesh = new Mesh();
    3. var vertices = Bindings.GetVertices();
    4. var vertices_size = Bindings.GetVerticesSize();
    5. var indices = Bindings.GetIndices();
    6. var indices_size = Bindings.GetIndicesSize();
    7. omesh.vertices = new Vector3[vertices_size];
    8. for(int i = 0; i < vertices_size; i++) {
    9.         float x = Bindings.Vertices_getXAt(vertices, i);
    10.         float y = Bindings.Vertices_getYAt(vertices, i);
    11.         float z = Bindings.Vertices_getZAt(vertices, i);
    12.         omesh.vertices[i] = new Vector3(x,y,z);
    13.         x = Bindings.Vertices_getNormalXAt(vertices, i);
    14.         y = Bindings.Vertices_getNormalYAt(vertices, i);
    15.         z = Bindings.Vertices_getNormalZAt(vertices, i);
    16.         omesh.normals[i] = new Vector3(x,y,z);
    17. }
    18. omesh.triangles = new Vector3[indices_size];
    19. for(int i = 0; i < indices_size; i++) {
    20.        omesh.triangles[i] = Bindings.GetTriangle(indices, i);
    21. }
    22.  
    23. GetComponent<MeshFilter>().mesh = omesh;
    24.  
    Doesn't show how the vertices and indices are generated (that's thousands of lines...), but it's something.
     
    Last edited: Jul 22, 2014
  4. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Do you have a Mesh Renderer component on your Game Object?
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Good thing you posted some code bra
    http://docs.unity3d.com/ScriptReference/Mesh-vertices.html
    "Returns a copy of the vertex positions or assigns a new vertex positions array."
    So on line 16 you are just changing the value in an array that doesn't actually apply to anything.
    Probably the same on line 20

    You must get the arrays (a copy) and assign to a variable
    Work on that variable
    Then assign the arrays back to mesh.verts & triangles