- Home /
Repeating texture in procedural mesh
Hello. These days i've been writing a script that creates a procedural road, and at least the mesh is ok. However, when I try to apply a striped texture on road center, it gives me this weird effect:
So I tried to offset the UV points generated in my script:
float magnitude = (extrusionPoint-previousPoint).magnitude;
endUV[vertexIndex+0] = new Vector2(textureOffset, 0);
endUV[vertexIndex+1] = new Vector2(textureOffset, 1);
endUV[vertexIndex+2] = new Vector2(magnitude/5f + textureOffset, 0);
endUV[vertexIndex+3] = new Vector2(magnitude/5f + textureOffset, 1);
textureOffset = (((magnitude/5f)%1f) + textureOffset)%1;
(Assuming that the tiling is proportional to the distance between points divided by 5)
in conclusion, yep, I'm not so sure about the "textureOffset" parameter, how to use it or, generally speaking, how to make the road look smooth & continous, so any suggestion is well accepted!
Thank you for your time and sorry for my barely sufficient English.
[EDIT] Solution:
I found a solution after playing with the inspector, as suggested by Fattie, to understand how tiling works; furthermore, UV points came out to be inverted (so the texture was offsetted in the wrong direction). I'm adding the final code here in case someone is going to need it, however I don't know if there's a better way to implement this tiling :D
float textureOffset = 0f;
float tilingFactor = 10f; // How long must be the texture?
// This is placed inside the extrusion loop
float magnitude = (finalPoint-startPoint).magnitude;
float tiling = (magnitude / tilingFactor) + textureOffset;
uv[vertexIndex+0] = new Vector2(textureOffset , 0);
uv[vertexIndex+1] = new Vector2(textureOffset , 1);
uv[vertexIndex+2] = new Vector2(tiling , 0);
uv[vertexIndex+3] = new Vector2(tiling , 1);
textureOffset = (magnitude / tilingFactor)%1 + textureOffset;
Final result:
Answer by Fattie · Jul 16, 2012 at 01:10 PM
suggest strongly you just play with "textureOffset" IN THE INSPECTOR in unity.
in that way you can learn about it quite nicely. (you can also animate it, using the Unity animator - another way to get your head around it.)
Thank you! After playing with it i found a sort of algorithm that seems to do the job. I'm going to add it in the post.
i really hope it helps. what you're trying to do is very hard !!