Search Unity

Repeated texture (tiled) on generated (2d) mesh

Discussion in '2D' started by Odiobill, Oct 12, 2018.

  1. Odiobill

    Odiobill

    Joined:
    Jul 16, 2018
    Posts:
    10
    Dear forum members,

    I'm pretty new to Unity and I'm trying to generate a curvy 2d "terrain" calculating every vertice using Perlin noise. After a few days of tutorials and mistakes, I've achieved my goal also thanks to the "Triangulator" available in the Unity wiki, with a result similar to the following picture:


    https://imgur.com/a/3w1Qsr3

    The problem is that I have no clue about how to calculate the right UVs for the vertices trying to apply a seamless texture, that should then be repeated along all the generated mesh and not "stretched" around it. Can you point me out to something useful for fixing this?

    Thanks a lot!
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,444
    first image is missing..

    should it look like this, it just fills the mesh evenly
    upload_2018-10-12_19-55-31.png

    or the texture would kind of follow the height, like
     
  3. Odiobill

    Odiobill

    Joined:
    Jul 16, 2018
    Posts:
    10
    Yes, sorry. The link, however, points to the same picture.

    Yes, it should look like this: I'd like to have the texture just being repeated around the mesh.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,444
    could try this, giving planar uv coordinates to vertices, there is example script at
    https://docs.unity3d.com/ScriptReference/Mesh-uv.html

    or, manually calculating vertex uv, if you know the min-max y positions (ugly picture below)
    upload_2018-10-12_21-1-17.png


    or, could try screenspace uv texture (if your screen is static view, not scrolling camera), there is example shader at
    "Detail Texture in Screen Space" (so would modify it to use maintexture instead of detail texture)
    https://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html
     
  5. Odiobill

    Odiobill

    Joined:
    Jul 16, 2018
    Posts:
    10
    Sorry but I really can't see how to calculate the UVs forgetting a repeated/tiled texture, since the number of UVs must be the same of the number of vertices. I generate the vertices with the following code (more or less):


    Code (CSharp):
    1.  
    2.         Vector2[] vertices2D = new Vector2[SIZE+2];
    3.         float x = 0f;
    4.         vertices2D[0] = new Vector2(x, (float)yBottom);
    5.         for (int i = 1; i <= SIZE; i++) {
    6.             float y = Mathf.PerlinNoise(x / detailScale, seed / detailScale) * heightScale + yStart;
    7.             vertices2D[i] = new Vector2(x, y);
    8.             x += 1f / verticesPerUnit;
    9.         }
    10.         vertices2D[SIZE+1] = new Vector2(x, (float)yBottom);
    11.  
    So I actually have vertices only on the top of the curvy terrain, plus two more for the bottom left and bottom right corner. The texture itself is seamless and it should be repeated, not stretched...
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,444
    how does it look if you set the uv's like in that example?

    or since you have 2d array already, try use that for mesh.uv's as is.
    then can adjust material tiling to see if it works nicely. (and to adjust how many time it repeats)
     
    Last edited: Oct 12, 2018
  7. Odiobill

    Odiobill

    Joined:
    Jul 16, 2018
    Posts:
    10
    Uhm, I thought that the UV array should contain Vector2's with X and Y from 0f to 1f, relatively to the position of that point to be mapped in the texture. I tried to use the exact same array for the UVs, and the result is this:


    https://i.imgur.com/dCys2Lt.png
     
  8. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,444
    from texture importer can set mode to repeat (instead of Clamp), but looks like the values have some issues still..

    yes, values would go from 0-1, and it wraps them if its larger.

    i was doing similar thing here, just tested that it works if set uvs to x and y values (and using small tiling value)
    https://unitycoder.com/blog/2013/08/11/scorched-earth-terrain-wip/
    upload_2018-10-13_13-36-14.png