Search Unity

Problem with procedurally generated Mesh

Discussion in 'Scripting' started by sacunha, Sep 25, 2017.

  1. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    I'm currently working on a script to extrude a 2D shape into a 3D object. I'm using the attached script to create said object, however, I'm in a bit of trouble that I can't figure out: Normals.
    I'm not calculating my normals correctly. My light reflections and shadows are a bit off.

    That is my first problem.
    My second problem is textures, whenever I apply a texture to the object, I cannot see it, and for that I have no clue on how to solve it.

    If anyone could take a look at the code and help me I'd greatly apreciate it and would have my eternal thanks :D
     

    Attached Files:

    • Test.cs
      File size:
      3.2 KB
      Views:
      710
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    You don't generally need to calculate them yourself, you can use https://docs.unity3d.com/ScriptReference/Mesh.RecalculateNormals.html instead.

    For texturing you need to apply UV values (mesh.uv) - these match up to each vertex. The UV values generally go between 0 and 1 (although they can go lower or higher for tiling) where 0,0 to 1,1 would be the extents of your texture.
     
  3. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    I've tried to use Mesh.RecalculateNormals but it has horrile results, that is why I ended up calculating them by hand.
    Will try to calculate the UV values but one problem still remains.
    Thanks.
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    You don't seem to be calculating your normals properly. Normals can be complicated to calculate automatically because it depends on what you want to achieve. Have a look at this diagram:

    http://answers.unity3d.com/storage/temp/10241-ybtff.jpg

    In the first picture each corner vertex is shared amongst all the triangles so they only have one normal each. This normal is pointing at an average direction of each face that uses it.

    In the second picture each corner vertex is duplicated 3 times so each side of the cube is using it's own vertices. That way allows you to have the normals pointing in the triangle direction of each cube side.

    The 'recalculateNormals' function will probably work better if you duplicate the vertices so they aren't shared between the top and bottom and middle of your extruded object.
     
    sacunha likes this.
  5. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    If I understood you correctly, I'm using the same verteces for diferent faces, wich cause normals to be calculated incorrectly. I should have 'duplicate' vertices, depending on the number of faces they are connected to, in order for all normals to be calculated. Is that it?
     
  6. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Yes, that's correct. In this picture (http://i.stack.imgur.com/7sklR.jpg) you can see that the left image has shared vertices so the normals make the lighting appear smooth over the sphere. On the right image the vertices are unique to each triangle so the normals will make it appear like individual polygons.

    How do you know the normals aren't working? Is it causing problems with your lighting? Might be worth posting an image of what's going wrong so I can get a better idea.
     
  7. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    It does look like you've got shared vertices/normals at the edges of your extruded object. Your shadows look ok to me though - from what I remember the shadowing code doesn't use the normals anyway, it just renders from the point of view of the light to work out what is in shadow and what isn't.