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

Set Texture to Terrain (Setting up UVs..)

Discussion in 'Scripting' started by Nakel, Feb 21, 2021.

  1. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    Hello I'm trying to set an image of any size in my terrain MESH that looks like this:
    perlin-noise-terrain-mesh1.png

    I know it involves setting up the uvs... I also know the float length of X and Z of the map.

    Can someone please provide an example on how to accomplish it...
     
    Last edited: Feb 21, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Terrains don't really give you that sorta UV control, as far as I know. Perhaps someone with new Unity terrain experience can contradict?

    When you add the texture, you specify the size that texture should be in world units. If your terrain is 500x500, then you apply a texture, tell it the texture is 500x500, like so:

    Screen Shot 2021-02-20 at 8.54.28 PM.png

    That square on the right is a 500x500 Terrain, obviously.
     
  3. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    SORRY!
    I meant MESH terrain... Might be easier... right?

    I found this code on a brackeys tutorial, but I cant get it to work, I get more UVs than vertices count

    Code (CSharp):
    1. uvs = new Vector2[vertices.Length];
    2. for (int i = 0, z = 0; z <= zSize; z++){
    3. for (int x = 0; x <= xSize; x++){
    4. uvs[i] = new Vector2((float)x/xSize, (float)z/zSize);
    5. i++;
    6. }}
    I would also love to strech the image on X or Z dynamically like some sort of "Zoom to match the image"
     
    Last edited: Feb 21, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    UVs and vertices have to match exactly in count. It looks superficially at least you have that correct above... are you feeding both vertices and uvs into the mesh? If so, where you do that, print the .Length of each array at that point, make sure they match.

    If you wanna see more procedural generation stuff, including random UV stuff, check out my MakeGeo project.

    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
     
  5. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    I finally got it working like this :D

    Code (CSharp):
    1.  
    2.         Vector3[] vertex = mesh.vertices;
    3.         uvs = new Vector2[quantity];
    4.         for (int i = 0; i < quantity; i++)
    5.         {
    6.             uvs[i] = new Vector2(vertex[i].x / xSize , vertex[i].z / zSize);
    7.         }
    but... could you help me by doing some sort of "slider" that shrinks or extends the image in X, so that I will make it for Z too...
     

    Attached Files:

    Last edited: Feb 21, 2021
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    It's actually pretty easy, I bet you can do it yourself!

    I recommend looking up a Youtube tutorial on how to use a slider to adjust a property, such as a float.

    Then when you make the UVs above, use that float (or two floats) to scale one of the directions.

    Then when the slider function gets called and the slider changes, take that value and regenerate your UVs to scale.

    The tricky part about a slider is that when you register a method for the slider to call on changed, the slider DOES NOT tell you its current value. It has an option to pass a float but that won't help you. You have to look up the value from inside the slider and use it yourself. You can look at like volume control settings tutorials for an equivalent example.
     
  7. Nakel

    Nakel

    Joined:
    Sep 28, 2017
    Posts:
    106
    is It necessary to change the uvs? instead of main texture offset and scale... I mean is it better that way?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Sure, go nuts if it works for you.