Search Unity

Issues with UV in generated mesh

Discussion in 'General Graphics' started by SpeakUpGames, Nov 4, 2017.

  1. SpeakUpGames

    SpeakUpGames

    Joined:
    Nov 25, 2014
    Posts:
    56
    I'm having some issues assigning UV values to my script generated mesh. It's likely because I don't completely understand UV mapping, so if anyone also could point me to a good informational guide for mesh UVs that would be helpful (internet searching has caused some confusing results).

    I'm generating a mesh like this:

    The mesh is arbitrarily long. I want the mesh renderer material to repeat for every individual square, E.g vertexes [1,2,3,4] have the whole texture, [5,6,7,8] full texture, repeat...

    Setting each group's UVs like this doesn't render correctly:
    Code (CSharp):
    1.  
    2.             // where i is the bottom left
    3.             uv[i] = new Vector2(0, 0);
    4.             uv[i + 1] = new Vector2(1, 0);
    5.             uv[i + 2] = new Vector2(0, 1);
    6.             uv[i + 3] = new Vector2(1, 1);
    7.  
    Can someone help fill in the gaps as to the proper usage?
     
  2. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    Can you show us how you generate the mesh vertecies and triangles and what the result looks like?
    Also why not using 1,2,3,4 - 3,4,5,6
    instead of 1,2,3,4 - 5,6,7,8 So I mean you can simply merge verticies, the UV will simply repeat, you only have to assign values greater 1 for the uv, e.g. point 5 (in your case 7) would be new Vector2(0, 2);
    Triangle definition will then be 3,5,4 instead of 5,7,6 for exmple.
     
  3. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
    UVs are the coordinates in normalised texture space. Your description of what you are doing seems fine, I don't think it's a lack of understanding. As McDev02 points out, you can exceed the bounds of the texture, and as the default behaviour is to "wrap" around in texture space and this can help you reuse vertices (so what you have as vertices 5 & 6 need not exist and the vertices that follow for the end points of the 2nd quad can have uv coords (0.0, 2.0) and (1.0, 2.0))
     
  4. SpeakUpGames

    SpeakUpGames

    Joined:
    Nov 25, 2014
    Posts:
    56
    Yes, I know I can reuse the vertices but the code becomes very confusing at that point. The extra vertices make it much more readable since I can treat each square as completely independent.
    I guess my confusion stems from this point:
    Why does the second quad have (0.0, 2.0) and (1.0, 2.0), could it not simply have (0, 1) and (0, 1) like the first? I assume that would be the case because 'normalized' implies the values should always be between 0 and 1
     
  5. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
    You don't have to do that, that was simply me trying to elaborate on what MacDev02 said about reusing vertices...each quad / triangle pair can have exactly the same UV coords like you want and that's fine too.

    I think your issue is elsewhere. Like are you sure your UVs match up with your positions, and that your indicies are alright if you are using an indexed mesh?
     
  6. SpeakUpGames

    SpeakUpGames

    Joined:
    Nov 25, 2014
    Posts:
    56
    I think they are correct but I can check again. I'm working with for loops mostly so perhaps that is causing me trouble but it's only the UV maps that seem incorrect. I'm getting mostly a solid color on all the meshes except for one, which got the texture but it's not right.