- Home /
Procedural Mesh UVing
When you're making a mesh from hand, (see here), how does the mesh.uv buisness work? Because if you've got an array, which you've got from generating thousands of vertices, then I'm not writing out Vector2(x,y) thousands of times! What am I meant to do? How can I split up my array, perhaps?
EDIT: That wasn't very clear. What I mean is, I have this compiler error: Mesh.uv is out of bounds. The supplied array needs to be the same size as the Mesh.vertices array. In the mesh example, you appear to have to write out the Vector2 statement as many times as there are vertics. I'm looking for a way to do this on the fly.
What are you talking about, "mesh.uv business"? Every case is going to be different. You generate the UVs in the cleanest way possible. The array is generally going to be procedurally generated, with you feeding it some kind of ruleset which generates the Vector2's in at least one loop. This isn't unique to UVs; this happens with any kind of substantial array.
What do you mean by "on the fly". You have to create a Vector2[] with the same size of your vertices array. Do you want to use the x ans y vertex coordinates as UVs? Just use a for loop and copy them....
I've added an example to my answer if that's what you wanted to do.
Answer by Bunny83 · Jun 13, 2011 at 08:54 PM
The Mesh class gives you 6 arrays:
to specify your vertex-data. Each of those array, if they are used, have to have the same size as the vertices array. So colors[4]
will be the vertex color of your 5th vertex and belongs to the position in vertices[4]
.
Those 6 arrays work as one array and define your vertices.
The triangles array hold indices to those arrays above to form triangles. The size must always be a multiple of 3 (since a triangle have 3 corners).
I'm not quite sure what's your actual problem... Do you have trouble to fill your uvs? Noone can help you here. UVs are normally created by the artist during unwrapping. If you create all dynamically you have to know how you need them...
An example to use the x,y coordinates as UVs:
var myVectices : Vector3[]; // you have created your vertices already and stored in this array
var myUVs : Vector2[] = new Vector2[myVectices.Length]; // Create array with the same element count
for(var i = 0; i<myVectices.Length; i++)
{
myUVs[i] = Vector2(myVectices[i].x,myVectices[i].y);
}
It doesn't make much sense in most cases to use the x,y values directly (except they are within 0..1 or you have a tiled texture)
Ohh and make sure you assign your vertices array first! If you assign the UV array to your mesh before you've set the vertices array you will get an out-of-bounds error like explained in the docs...
Sorry if I'm being really n00bish today, but don't I need to iterate through the array first? Because otherwise I'll get the 'x is not a member of Vector3[]' error?
Answer by hellcats · Jun 13, 2011 at 08:51 PM
Just use a for loop (assuming you have some kind of way of generating your uv coordinates for a given x,y,z vertex location). I just used Sin and Cos here.
var uv = new Vector2[1000];
for (var i = 0; i < 1000; i++) {
uv[i].x = Mathf.Sin(i / 1000.0f);
uv[i].y = Mathf.Cos(i / 1000.0f);
}
mesh.uv = uv;
Nice example but $$anonymous$$athf.Sin/Cos takes an angle in radian not a value between 0 and 1 ;)
Ah, but a value between 0 and 1 still is a radian :) I was just trying to make some smoothly varying values for u and v.
:D sure it is but doesn't make much sense :D
Of course it's just a sample ;)
Isn't there an un-guaranteed order into your cached array of vertices? I mean, when you reference .mesh.vertices, [0] isn't necessarily going to be the "first" vertex is it? There really isn't a first vertex at all is there - the order into the array will vary per mesh eh? This issue has thrown me off where the .uvs array is concerned and the need to know which index the 0,0 uv is in and so forth. This issue gets off into uv issues I suppose, but I admit confusion as the need to "know" the order of placement into any given array so you "know" which uv you are working with in order to animate or use for re-cropping a texture.
Unless the engine rearranges the elements of the array, arrays will always be consecutive within the memory space allocated.
Your answer
Follow this Question
Related Questions
Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer
UV Mapping a Sphere at runtime 1 Answer
Manual UV-Mapping of a Primitive Cube 1 Answer
Mesh.uv is out of range 1 Answer
Procedural Mesh/UV 0 Answers