- Home /
uv Tiling y error
edit Follow up question
hello community! I am having a problem coordinating the uv. I currently have this done, but it does not work as it should, it is something basic but, ALMOST, it meets the requirements. What I want is that the texture always keeps its "tiling Y" and is divided by the total distance between nodes ...
This is what I currently have for UV: Vector2[] uvs = new Vector2[road.verts.Count];
for (int i = 0; i < road.verts.Count; i+=2) {
float total = i / (float)(road.verts.Count-1);
float v = 1 - Mathf.Abs (2 * total - 1);
uvs [i] = new Vector2 (0, v);
uvs [i + 1] = new Vector2 (1, v);
}
m.uv = uvs;
I leave a sample image
Answer by Bunny83 · Aug 13, 2019 at 01:26 AM
Your approach does not take the actual distance between the nodes into account. If that screenshot actually represents the result of the code you've posted, it looks like your actually used texture is not a tiled texture but a texture with about 24 center marks, is that correct?
Usually for procedural roads you want to use a tilable texture that only contains a single segment of the road. So simply one space and one line so when placing the same texture after the first it continues the road pattern perfectly.
When you have a tilable texture (which usually would be about a square texture) you should make sure the texture's wrap mode is set to repeat. This way UV coordinates outside the range of 0-1 are repeated properly. So for example if the first V coordinate is "0" and the next one is set to "5" that single segment will have 5 copies of the whole texture. Now it's only a matter of actually calculating the distance between two nodes and use the accumulated distance as V coordinate. Usually you want to scale that V coordinate additionally by your road width and maybe also by a "correction factor" in case the texture isn't perfectly square or you actually want to stretch / squish the texture along the road.
You would simply do that like this:
// Note I assume the for loop from your other question where you iterated through your nodes list:
float vScale = 1f;
int count = nodes.Count;
List<Vector2> uvs = new List<Vector2>(count * 2);
Vector3 lastPoint = new Vector3();
float currentV = 0;
for (int i = 0; i < count; i++)
{
// your vertex and normal generation code
if (i == 0)
{
uvs.Add(new Vertex(0, 0));
uvs.Add(new Vertex(0, 1));
lastPoint = nodes[i];
}
else
{
Vector3 p = nodes[i];
Vector3 dist = p - lastPoint;
currentV += dist.magnitude * 2 * width * vScale;
uvs.Add(new Vertex(currentV, 0));
uvs.Add(new Vertex(currentV, 1));
lastPoint = p;
}
}
This should generate UV coordinates so that the center of the road roughly has the same distance mapping everywhere. With a vScale of 1 we essentially map 1 unity unit onto the whole texture. If you want to actually use your "~24" segment texture, you can simply set your vScale to 1f/24f
. This assumes that "width" is half the road width like you used it in your other question.
Note: If that's not the behaviour you want, you should have been more precise when you said:
it does not work as it should
and
AL$$anonymous$$OST, it meets the requirements
We don't know exactly how it "should" work and what your "requirements" are ^^. Note since the actual edges at the corners are slanted when you have any kind of direction change the texture is sheared, This might be irrelevant depending on the details on the texture. However if a proper parallel projection is wanted you essentially loose the continuity of the texture across the connecting edges. Though most the times this is is less important. Though calculating the proper UVs for a parallel projected texture is slightly more complicated since you need different UVs for the two corners of an edge. This also means you need a UV seam at each connecting edge, so you would need to duplicate the vertices at each connection.
Though as long as you don't have too many larger details on the road texture and the road doesn't have too narrow corners this shouldn't be an issue.
Oh and just as a remainder: Unity does support other mesh topologies than triangles. Since your street always consists of quads you could actually model your road out of quads. So you would only need 4 indices ins$$anonymous$$d of 6 for one segment.
Answer by gonzalitosk8 · Aug 13, 2019 at 02:57 AM
Thanks for the prompt response, I am using unity 5, applying your code, it didn't work ... update it and it looks similar to how I currently have it
And yes, I use a special texture for this case, which I try at 3ds max and it looks great!
Your answer
Follow this Question
Related Questions
Procedural Mesh UV problem 0 Answers
need guide in uv texture 0 Answers
Update a texture at runtime using UV 0 Answers
Circular Mesh UV interpolation for tileable texture 1 Answer