Search Unity

Mesh Uvs Issue

Discussion in 'Scripting' started by Zero_Xue, May 7, 2018.

  1. Zero_Xue

    Zero_Xue

    Joined:
    Apr 18, 2012
    Posts:
    116
    Right am new to this whole mesh generation thing, i got it down but am having issues with UVs and well its not tiling... should also point out am using a texture atlas Heres and Example



    Code (CSharp):
    1.                 // Create the mesh
    2.                 GameObject NewChunk = new GameObject();
    3.                 NewChunk.transform.name = "Chunk " + ChunkNumber;
    4.                 NewChunk.transform.position = Pos;
    5.                 Mesh msh = new Mesh();
    6.                 msh.vertices = Points.ToArray();
    7.                 List<int> Tris = new List<int>();
    8.                 for (int i = 0; i < Points.Count; i++)
    9.                 {
    10.                     List<int> SetTris1 = new List<int>();
    11.                     List<int> SetTris2 = new List<int>();
    12.  
    13.                     SetTris1.Add(GetPointIndexAt(Points[i], Points));
    14.                     SetTris1.Add(GetPointIndexAt(Points[i] + new Vector3(0, 0, 1), Points));
    15.                     SetTris1.Add(GetPointIndexAt(Points[i] + new Vector3(1, 0, 0), Points));
    16.                     if (!SetTris1.Contains(-1))
    17.                     {
    18.                         Tris.AddRange(SetTris1);
    19.                     }
    20.  
    21.                     SetTris2.Add(GetPointIndexAt(Points[i] + new Vector3(1, 0, 0), Points));
    22.                     SetTris2.Add(GetPointIndexAt(Points[i] + new Vector3(0, 0, 1), Points));
    23.                     SetTris2.Add(GetPointIndexAt(Points[i] + new Vector3(1, 0, 1), Points));
    24.                     if (!SetTris2.Contains(-1))
    25.                     {
    26.                         Tris.AddRange(SetTris2);
    27.                     }
    28.                 }
    29.  
    30.                 msh.triangles = Tris.ToArray();
    31.  
    32.                 Vector2[] Uvs = new Vector2[Points.Count]; // Create array with the same element count
    33.                 for (var i = 0; i < Points.Count; i++)
    34.                 {
    35.                     //Uvs[i] = new Vector2(Points[i].x, Points[i].y);
    36.                     List<int> UVPoints = new List<int>();
    37.                     UVPoints.Add(GetPointIndexAt(Points[i], Points));
    38.                     UVPoints.Add(GetPointIndexAt(Points[i] + new Vector3(0, 0, 1), Points));
    39.                     UVPoints.Add(GetPointIndexAt(Points[i] + new Vector3(1, 0, 1), Points));
    40.                     UVPoints.Add(GetPointIndexAt(Points[i] + new Vector3(1, 0, 0), Points));
    41.                     if (!UVPoints.Contains(-1))
    42.                     {
    43.                         Uvs[UVPoints[0]] = UVMap.GetUVMap("cobblestone_mossy").UV_Map[1];
    44.                         Uvs[UVPoints[1]] = UVMap.GetUVMap("cobblestone_mossy").UV_Map[3];
    45.                         Uvs[UVPoints[2]] = UVMap.GetUVMap("cobblestone_mossy").UV_Map[2];
    46.                         Uvs[UVPoints[3]] = UVMap.GetUVMap("cobblestone_mossy").UV_Map[0];
    47.                     }
    48.                 }
    49.                 msh.uv = Uvs;
    50.                 msh.RecalculateNormals();
    51.                 msh.RecalculateBounds();
    52.                 NewChunk.AddComponent(typeof(MeshRenderer));
    53.                 NewChunk.GetComponent<Renderer>().material.SetTexture("_MainTex", ChunkAtlas);
    54.                 MeshFilter filter = NewChunk.AddComponent(typeof(MeshFilter)) as MeshFilter;
    55.                 filter.mesh = msh;
    Not sure what the issue is.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,063
    Unless you create a continuous series of individual faces (each with corner vertices that recycle the same area of texture space), tiling is only going to happen at the edge of a power-of-two texture.

    If you are working with a texture atlas, it won't automatically wrap within that sub-area, but instead include the entire atlas dimensions before it wraps.

    In your case above, looks like you have the adequate number of mesh points to make wrapping faces with an atlas, you just have to reassign the UVs so they stay within the bounds of where the desired texture is, ie., the cobble.
     
  3. Zero_Xue

    Zero_Xue

    Joined:
    Apr 18, 2012
    Posts:
    116
    am guessing power-of-two textures sizes right (32x32,64x64,128x128 and so on) in which case the atlas packs them each texture as 128x128 tiles am calling the start x,y and end x,y in the UVMap array, am completly new to trying to UV map procedurally, am i right in thinking i can use the same UV point more then once? cos am thinking my issue is am calling the UV Point as a end point then overwriting it as a start point, do i need to make 2 UV points on each vertice so i can assign both a start and end point?

    Sorry for the really bad formatting on that.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,063
    Actually, yeah, that's exactly it, my bad for not seeing that first.

    Each vertex can only have a single UV point, so to do what you propose (manually tiling it), you would need to make each quad have its own four vertices with identical UVs mapped so it appears to wrap over the same cobble.
     
  5. Zero_Xue

    Zero_Xue

    Joined:
    Apr 18, 2012
    Posts:
    116
    No worries, but thanks for comfirming my suspision atleast know i know what the issue is, time to havea fiddle with the code and come up with something =)

    Thank again Kurt
     
    Kurt-Dekker likes this.