Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UV on generated Mesh

Discussion in 'Scripting' started by Smileys, Jul 14, 2022.

  1. Smileys

    Smileys

    Joined:
    Feb 27, 2014
    Posts:
    81
    I am trying to get my head around Mesh generation and am stuck at the UV part. What i am trying to do is assigning each quad of my generated grid/plane a unique UV position. Like in blender when you Unwrap one of many faces, you are able to independently place this face on the texture without messing around with the other UVs. However if i look into the data Unitys Mesh is using, it seems that each vertex is assigned a unique UV position. When dealing with a grid where each quad/triangle is connected to the next one, i would need to have more than one UV for each vertex (hope that explanation makes sense).

    Is there a way to create the desired behavior or do i need to generate lots of individual quads instead of connected ones ?

    Here is the test code that i am using. It generates 2 quads and stretches the texture over both:
    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class UVTest : MonoBehaviour
    3. {
    4.     public Material material;
    5.  
    6.     MeshFilter _filter;
    7.     MeshRenderer _renderer;
    8.     Mesh _mesh;
    9.  
    10.     void OnEnable()
    11.     {
    12.         CreateMesh();
    13.  
    14.         //Setup Mesh Filter
    15.         _filter = gameObject.GetComponent<MeshFilter>();
    16.         if (_filter == null)
    17.             _filter = gameObject.AddComponent<MeshFilter>();
    18.  
    19.         _filter.mesh = _mesh;
    20.  
    21.         //Setup Mesh Renderer
    22.         _renderer = gameObject.GetComponent<MeshRenderer>();
    23.         if (_renderer == null)
    24.             _renderer = gameObject.AddComponent<MeshRenderer>();
    25.         _renderer.material = material;
    26.  
    27.     }
    28.  
    29.     void CreateMesh()
    30.     {
    31.         _mesh = new Mesh();
    32.         Vector3[] verts = new Vector3[]
    33.         {
    34.             new Vector3(0,0,0),
    35.             new Vector3(0,1,0),
    36.  
    37.             new Vector3(1,0,0),
    38.             new Vector3(1,1,0),
    39.  
    40.              new Vector3(2,0,0),
    41.             new Vector3(2,1,0),
    42.  
    43.         };
    44.         _mesh.vertices = verts;
    45.  
    46.         int[] tris = new int[]
    47.         {
    48.             0,1,2,
    49.             2,1,3,
    50.  
    51.             2,3,4,
    52.             4,3,5
    53.         };
    54.         _mesh.triangles = tris;
    55.  
    56.         _mesh.RecalculateNormals();
    57.  
    58.         Vector2[] uvs = new Vector2[]
    59.         {
    60.             new Vector2(0,0),
    61.             new Vector2(0,1),
    62.  
    63.             new Vector2(0.5f,0), //Those middle UVs are 'shared' how
    64.             new Vector2(0.5f,1), //am i supposed to have them at different texture coordinates for 2 quads
    65.  
    66.             new Vector2(1,0),
    67.             new Vector2(1,1),
    68.         };
    69.         _mesh.uv = uvs;
    70.  
    71.     }
    72. }
    *Attached image of the desired effect in blender
    UV_Blender.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Verts are 1-to-1 correlated to UVs in Unity.

    If you need a discontinuity in UV mapping between faces then you need to dupe vertices, using the correct duplicate in separate faces.

    You're welcome to sniff around my MakeGeo project for such examples.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo

    There's also a
    SetUVToWorld.cs
    script in there that might be useful in some cases. It does a triplanar UV remap on the fly at Start() time.
     
    Bunny83 likes this.
  3. Smileys

    Smileys

    Joined:
    Feb 27, 2014
    Posts:
    81
    Lets say i want this behavior. On top of that i want the Verts to be 'welded/linked' so that the mesh behaves as a connected unit when i modify the position of one vert. Then i would have to maintain a seperate List that keeps track of said connections - right?

    Thanks will def. have a look at the repo.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Yeah, that would be it... for instance if your center vertex was actually four verts (one to be used in each corner quad), then you would keep the list of those verts in a master list of "verts I care about"
     
    Smileys and Bunny83 like this.