Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problems applying texture to a custom mesh

Discussion in 'Editor & General Support' started by Epilol, Feb 12, 2016.

  1. Epilol

    Epilol

    Joined:
    Jul 21, 2015
    Posts:
    85
    Hello guys!
    I'm making a tool to generate procedural meshes for streets by script (using triangulator). The street looks ok but when I try to apply a texture I have some problems.

    For example with this material

    road_mat_1.png

    the street looks like this:

    street_1.png

    if I rotate the texture, using this material

    road_mat_2.png

    this is the outcome:

    street_2.png

    Maybe an uv problem? Or what could it be?

    Thanks a lot in advance!
     
  2. Epilol

    Epilol

    Joined:
    Jul 21, 2015
    Posts:
    85
    Anyone?
     
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    It just depends on how you are setting up the uvs. It's hard to provide any help without knowing what your code is doing.
     
  4. Epilol

    Epilol

    Joined:
    Jul 21, 2015
    Posts:
    85
    Yes you are right, this is the code:

    Code (CSharp):
    1. // computes streets' mesh, excluding the intersections between them
    2. public List<Mesh> ExecuteMultiDifference(List<GameObject> objs, List<int> intersections, int index)
    3. {
    4.     // list that will contain the computed meshes that will be returned
    5.     List<Mesh> diffMeshes = new List<Mesh>();
    6.  
    7.     // lists used by clipper to execute the difference
    8.     List<List<IntPoint>> subj = new List<List<IntPoint>>();
    9.     List<List<IntPoint>> clip = new List<List<IntPoint>>();
    10.  
    11.     // conversion from Vector3 to IntPoint, used by clipper
    12.     List<IntPoint> path = FromVecToIntPoint(objs[index].GetComponent<MeshFilter>().sharedMesh.vertices);
    13.  
    14.     // Adding elements to subj and clip
    15.     subj.Add(path);
    16.  
    17.     foreach (int j in intersections)
    18.     {
    19.         List<IntPoint> path2 = FromVecToIntPoint(objs[j].GetComponent<MeshFilter>().sharedMesh.vertices);
    20.         clip.Add(path2);
    21.     }
    22.  
    23.     // used to store the output of clipper
    24.     List<List<IntPoint>> solution = new List<List<IntPoint>>();
    25.  
    26.     // execution of clipper difference
    27.     Clipper c = new Clipper();
    28.     c.AddPaths(subj, PolyType.ptSubject, true);
    29.     c.AddPaths(clip, PolyType.ptClip, true);
    30.     c.Execute(ClipType.ctDifference, solution, PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd);
    31.  
    32.     List<Vector2> diff = new List<Vector2>();
    33.  
    34.     for (int i = 0; i < solution.Count; i++)
    35.     {
    36.         // conversion from IntPoint to Vector3
    37.         diff = (FromIntPointToVec(solution[i])).ToList();
    38.  
    39.         // triangulator
    40.         Triangulator tr = new Triangulator(diff.ToArray());
    41.         int[] indices = tr.Triangulate();
    42.  
    43.         // init the Vector3 vertices array
    44.         Vector3[] vertices = new Vector3[diff.Count];
    45.  
    46.         // init the Vector2 uvs array
    47.         Vector2[] uvs= new Vector2[diff.Count];
    48.  
    49.         // computes the vertices and the uvs
    50.         for (int k = 0; k < vertices.Length; k++)
    51.         {
    52.             vertices[k] = new Vector3(diff[k].x, 0, diff[k].y);
    53.             uvs[k] = new Vector2(diff[k].x, 0);
    54.         }
    55.  
    56.         // creates the mesh
    57.         Mesh msh = new Mesh();
    58.         msh.vertices = vertices;
    59.         msh.triangles = indices;
    60.         msh.uv = uvs;
    61.         msh.RecalculateNormals();
    62.         msh.RecalculateBounds();
    63.         diffMeshes.Add(msh);
    64.     }
    65.  
    66.     return diffMeshes;
    67. }
     
  5. Epilol

    Epilol

    Joined:
    Jul 21, 2015
    Posts:
    85
    bump, anyone please?
     
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    It is very difficult to tell what is going on. The script doesn't have context. Appears you are using a couple of libraries. (clipper/triangulator). Presumably triangulator is the one from the wiki, which draws a polygon. And guessing the Clipper ref is from the clipper lib @ angusj.com. So your script draws a polygon and clips it.

    But you haven't really explained what you are expecting vs. what you are getting. You shown some screen shots, but didn't describe the problem. If I were to guess, it seems like you are trying to make roads, and have the lines on the roads to follow the direction of the road, yes?

    If that is the case, the triangulator/clipper isn't the right solution. That will just create a shape, it doesn't take into account directionality. You will need to use something spline/path based to generate the mesh. There are some packages on the asset store for that.

    If that isn't a correct assumption, please provide more detail.
     
  7. Epilol

    Epilol

    Joined:
    Jul 21, 2015
    Posts:
    85
    Yes this is correct. The problem is that I can't use assets from the asset store as it is a project for my university.