Search Unity

Unity mesh data, triangles, vertices

Discussion in 'Scripting' started by ScriptGeek, Jan 26, 2013.

  1. ScriptGeek

    ScriptGeek

    Joined:
    Mar 4, 2011
    Posts:
    45
    I'm a bit confuzzled about what's going on here, so I'm just going to jump into the details about a problem I'm having...

    I created a very simple model in Blender 2.64 and imported it into Unity 4.0.1f2. In my script, I access the mesh data triangles and vertices in search of available locations to spawn prefabs at runtime. The locations are calculated from the vertex data obtained from selecting a random triangle from the mesh. For some unknown reason the locations retrieved are out of phase with the model in the scene, specifically the locations appear to end up appearing like where the model should be if the model were rotated at some arbitrary axis/angle. I've tried using Transform.InverseTransformPoint and Transform.TransformPoint to see if maybe the rotation of the model was throwing off the positions of my Debug.DrawLine statements, which I was using to determine which triangles were being selected. I've searched around for answers but I have come up short.
     
  2. ScriptGeek

    ScriptGeek

    Joined:
    Mar 4, 2011
    Posts:
    45
    Ok, on a whim I tried the trick I used for getting animations to show up correctly using the right rotations and it's working now. This is really strange indeed, given that the modeling data used to render the model appears to be correct when displaying in the editor and at runtime, but when accessing the mesh model data's triangles and vertices the coordinates are rotated. So, to describe my solution to my problem so you know what I'm talking about, when working in Blender rotate the model about the x-axis 270 degrees, hit Alt-A and select Rotations (to reset the rotation), then rotate the model 90 degrees and save. Done.
     
  3. Stephan-B

    Stephan-B

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Out of phase how?

    Vertex positions are relative to the mesh. So if the Mesh is rotated and somewhere in space, you need to account for that.

    Code (csharp):
    1.  
    2. spawn.transform.position = whatever_Mesh_transform.TransformPoint(vertices[2]);
    3.  
    Also, in terms of triangles, check to make sure the import options on the Mesh are set to optimized. Without that you may end up with more vertices / triangles which could throw you off in terms of having more vertices / triangles and them not being the ones you expect.
     
  4. ScriptGeek

    ScriptGeek

    Joined:
    Mar 4, 2011
    Posts:
    45
    Thank you for your reply.

    By, "Out of phase", I meant that the modeled, textured mesh appeared rotated about some axis from a test I conducted to see what was going on by using Debug.DrawLine statements to display the triangles in the mesh. The results of the DrawLines displayed a mesh that was inconsistent with the modeled, textured mesh by way of a rotation difference.

    I did try to use the TransformPoint function from the Transform API, as stated in my first post, and it did not provide any explanation.

    In the import options on the Mesh, the settings were set to optimized.

    I suspect the problem was with the coordinate systems being different from Blender to Unity and importing does not properly handle the conversion between the two systems, since after changing the rotation in Blender, applying it, changing the rotation again, then saving again seemed to solve the problem. I'm still not sure why my theory works.