2

I'm currently working a mapping tool that will generate a custom map for my game. I've got the mesh generation working perfectly but I can't seen to figure out how to correctly UV map my faces to get the textures consistent. I've got the UV map working on the floor perfectly just by using the respective coordinate pair but the walls don't seem to work the same.

Does anyone know the proper way to map a UV map to a vertical wall?

Here are some examples of my current walls: enter image description here enter image description here

I'm currently using this for the UV mapping of the walls

        currentMesh.uvs.Add(new Vector2(0, 0));
        currentMesh.uvs.Add(new Vector2(1, 0));
        currentMesh.uvs.Add(new Vector2(0, 1));
        currentMesh.uvs.Add(new Vector2(1, 1));
Kuliu
  • 171
  • 2
  • 12

2 Answers2

1

For situations like this, I do the following steps:
1. Make the texture a square one(for reducing calculation in code below). Also, make it seamless and wrap mode to repeat.
2. Find out the Length and Width of the rectangle like this:

Length = (float)Vector3.Distance(mesh.vertices [0],mesh.vertices [1])
Width = (float)Vector3.Distance(mesh.vertices [0],mesh.vertices [3])

3.Then I UV map using this code:

    Vector2[] uv = new Vector2[mesh.vertices.Length];
    float min = Length>Width?Length:Width;
    for (int i = 0, y = 0; i < uv.Length; i++){
        uv [i] = new Vector2 ((float)mesh.vertices [i].z / min, (float)mesh.vertices [i].y / min);    
    }

Here, I took .z and .y, as my plane is in yz plane. What this code do is, it finds the smaller arm of the rectangle and fits the texture according to it.
Here's a screenshot of the result: enter image description here

Here's the texture that I used:
enter image description here

I've learnt this from here:
http://catlikecoding.com/unity/tutorials/procedural-grid/

ZayedUpal
  • 1,603
  • 11
  • 12
1

I've managed to figure it out. What I ended up doing was taking the X and Z of the vertex and taking the weight of the two depending on the direction of the wall and it ended up with the results I wanted

        Vector3 heading = new Vector3(floor2.x - floor1.x, 0, floor2.z - floor1.z);
        Vector3 direction = heading / heading.magnitude;

        currentMesh.uvs.Add(new Vector2(((floor1.x * direction.x) + (floor1.z * direction.z)) / scale, floor1.y / scale));
        currentMesh.uvs.Add(new Vector2(((floor2.x * direction.x) + (floor2.z * direction.z)) / scale, floor2.y / scale));
        currentMesh.uvs.Add(new Vector2(((ceil1.x * direction.x) + (ceil1.z * direction.z)) / scale, ceil1.y / scale));
        currentMesh.uvs.Add(new Vector2(((ceil2.x * direction.x) + (ceil2.z * direction.z)) / scale, ceil2.y / scale));

enter image description here

Kuliu
  • 171
  • 2
  • 12