Comments and answers for "How to assign UV coordinates to arbitrary mesh."
http://answers.unity.com/questions/457752/how-to-assign-uv-coordinates-to-arbitrary-mesh.html
The latest comments and answers for the question "How to assign UV coordinates to arbitrary mesh."Comment by aldonaletto on aldonaletto's answer
http://answers.unity.com/comments/457996/view.html
Good point: I assumed here that the wall segments are generated sequentially, thus accumulating the segment widths should work as a current left coordinate. If segments aren't generated sequentially, this will fail miserably!
And yes, the right X may be greater than 1.0: that's why the module is applied only when updating curX, which is the next left coordinate. This way, for each segment the left X is in the range 0..1, while the right X may extend to any value; when calculating the left X for the next segment, only the frac part is taken into account.Thu, 16 May 2013 21:12:44 GMTaldonalettoComment by Fattie on Fattie's answer
http://answers.unity.com/comments/457959/view.html
"Use only the fractionary
// part...
curX = rightX % 1;
"
Don't forget if a segment is longer than the texture it needs to be more than 1 deltaThu, 16 May 2013 20:30:43 GMTFattieComment by Fattie on Fattie's answer
http://answers.unity.com/comments/457957/view.html
"and keep track of the current position when generating new segments"
Hey Aldo wot up. Just for the record: i've often found it better in practice to do it statelessly, each "piece of wall" (or whatever) just goes back to whatever the starting point is and figures out for itself, there and then, what it should be.
so, in practice there might be a line of code: "my left hand start = add up lengths of all pieces left of me"
it's extremely simple. very likely they're in a list or something ...... so it's absolutely trivial.Thu, 16 May 2013 20:29:38 GMTFattieComment by Fattie
http://answers.unity.com/comments/457949/view.html
"Am I going to have to work out magnitudes between vertexes"
Yes. You are going to have to work out everything.Thu, 16 May 2013 20:23:03 GMTFattieComment by Owen-Reynolds
http://answers.unity.com/comments/457814/view.html
"work out magnitudes between vertexes": Yes.
An easy version takes maybe two lines of code. Just copy XZ distance-so-far directly into V (UV's will gladly go past 1, tiling.) Later, multiply by a scale-factor.Thu, 16 May 2013 16:12:54 GMTOwen-ReynoldsAnswer by aldonaletto
http://answers.unity.com/answers/457800/view.html
If you just want to have a constant 1:1 aspect along each segment, things are easier. If you have to ensure continuity from one segment to the next, then you must also set the uv offset and keep track of the current position when generating new segments.
Supposing that you have just calculated the four vertex of a new segment in variables v1 to v4, where v1 is the top-left vertex, v2 is top-right, v3 is bottom-right and v4 is bottom-left, their corresponding uv coordinates can be calculated in variables uv1 to uv4 as follows:
![alt text][1]
...
var segW = Vector3.Distance(v1, v2); // calculate segment width
var segH = Vector3.Distance(v1, v4); // calculate segment height
var rightX: float = segW/segH; // calculate right X coordinate
uv1 = Vector2(0,1); // uv relative to the top-left vertex
uv2 = Vector2(rightX,1); // uv relative to the top-right vertex
uv3 = Vector2(rightX,0); // uv relative to the bottom-right vertex
uv4 = Vector2(0,0); // uv relative to the bottom-left vertex
...
If you need continuity, then a *curX* member variable must be kept and updated after generating each new segment:
var curX: float = 0; // define curX outside any function
...
var segW = Vector3.Distance(v1, v2); // calculate segment width
var segH = Vector3.Distance(v1, v4); // calculate segment height
var rightX: float = curX + segW/segH; // calculate right X coord
uv1 = Vector2(curX,1); // uv relative to the top-left vertex
uv2 = Vector2(rightX,1); // uv relative to the top-right vertex
uv3 = Vector2(rightX,0); // uv relative to the bottom-right vertex
uv4 = Vector2(curX, 0); // uv relative to the bottom-left vertex
// update curX to the next wall segment. Use only the fractionary
// part, since big uv values may cause visible artifacts due to
// the limited precision used in shaders
curX = rightX % 1;
...
[1]: /storage/temp/11081-wallsegment.pngThu, 16 May 2013 15:50:41 GMTaldonaletto