Search Unity

A rail- UVmapping problem

Discussion in 'General Graphics' started by vincespeed, Jun 18, 2018.

  1. vincespeed

    vincespeed

    Joined:
    Mar 8, 2014
    Posts:
    70
    Hello guys,
    It's a bit of days I'm struggling with a uvmapping problem, and I thought I could ask you for a bit of help :)
    I've searched the forum but it doesn't seem to be something already asked.
    Here's the problem: I have a rail-shaped mesh (actually, it is the road of a closed racetrack) which has its own uvmapping set in order to have a texture cover an entire chunk of road (I hope this is clear from the image below).
    What I want to achieve, is to have the same texture to repeat not just every chunk, but to repeat every given number of chunks -let's say five-.
    The problem is not simple as it may seem at first... because triangles and vertices are not ordered in the vertex and triangles arrays. This means that triangle[1] may not be the one attached to triangle[0] and so on :(
    I am currently lost in the "search for next triangle" thing, and it would be really appreciated if someone can lead me in the correct direction in solving this issue :)
    Thank you very much guys!
     
  2. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    Do you generate the mesh on your own? What is the actual problem, you only show current UV of the first 4 points. If they continue that way then you could only multiply the y value by a factor.

    You don't need any triangles, just make sure the vertecies are ordered in a propper way, here they seem to be. Also you don't get Triangles in the mesh class, a list of 3 inidicies. All you get is the index of a vertex and each 3 vertecies in the row make up one triangle.
    There is no triangle variable, but triangles. If you mean that then triangle[0] is always connected to triangle[1]. But those are the first two vertecies of the first triangle, not two triangles.

    SO all you could do is to change the y value of the UV by the number: row = floor(vertex-id / 2).
    E.g. uv.y = tiley * row. In your case tiley = 0.2f.
     
    Last edited: Jun 18, 2018
  3. vincespeed

    vincespeed

    Joined:
    Mar 8, 2014
    Posts:
    70
    Thank you for answering :)
    I see your point. And I explained badly what I meant when I say that triangles are not in order.
    In the image above, one expect that the 2nd chunck vertices are "4,5,6,7" and so on. But they are not. When the track was created in a software cad, it has not been modeled from start to end, but with other criteria. So it means that, before applying the formula you mentioned, I need to get the right triangles, based on the previous.
    I don't know if I am explaining this right. Triangles informations are not stored in the same order that they are connected each other, into the vertex and triangles array...
    So, my main problem at the moment is reordering vertices, but all methods I tried to write failed. I'll write some pseudocode to explain what I think I should do to reorder them:
    Code (CSharp):
    1. void handleFollowingChunck(){
    2. Vector3 previousVertex=GetComponent<MeshFilter>().mesh.vertices[3];
    3. for(int i=4;i<vertices.Length-4;i+=4){
    4. if(mesh.vertices[i]==previousVertex){
    5. //I found the first vertex of the following chunk
    6. //I Do Remapping code on the 4 vertices;
    7. previousVertex=mesh.vertices[i+3];
    8. }
    9. }
    10. }
    Still, there's something wrong with my code, and I won't be able to get all chuncks in the spatial order.
     
  4. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    You have more issues like uneven distance for each row. Sounds like you want to do that in a 3D software or even rebuild the track. If it's just like you showed it wwould be the easiest solution, given that you can do that.

    Otherwise I could think of some solutions but it sounds more complicated. What you can do is to seach for a triangle that shares two vertecies with the current one, that will be the next triangle. While you do that you can make a new mesh for instance and order the vertecies the right way.

    So you start with the first triangle (maybe you got to find that too). You get 3 vertecies. Now look for another triangle that shares two points of those 3. The other vertex it has is the new vertex D. You add those vertecies and triangles to lists.
    Go on and search for the next triangle which also includes vertex D. (Or you keep track of already found triangles.)

    Once you did that you could generate a new mesh or simply generate a lookup table for the existing vertecies so that you can find them in the right order.

    Now there are a few issues, if the structure is completely out of order then you can't be sure that odd values of vertex inidcies are X=1 and even values are X=0 for example. In this case you have to analyze the topology to find that out. And as mentioned if the distance varies you have to solve that as well. But all of that becomes even more complicated if you have curved surfaces. So if you can do it in 3D software, do it there.
     
    vincespeed likes this.
  5. vincespeed

    vincespeed

    Joined:
    Mar 8, 2014
    Posts:
    70
    Yep! It is like you say! At first, I also thought it would be easier on the modeling software I use (rhinoceros). Paradoxically, for a series of Mesh editing stuff... I found easier using Unity and writing my own code there.
    For this problem too, it doesn't seem like rhinoceros has any tool that can help me sorting this mess out.
    Anyway, once back home I'll test a new routine I came up with... I hope it can help and will let you know :)
    Thank you for your help!!