x


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.

more ▼

asked Jun 13 '11 at 08:38 PM

Muzz5 gravatar image

Muzz5
1.2k 65 83 94

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.

Jun 13 '11 at 08:46 PM Jessy

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....

Jun 13 '11 at 09:31 PM Bunny83

I've added an example to my answer if that's what you wanted to do.

Jun 13 '11 at 09:40 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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)

more ▼

answered Jun 13 '11 at 08:54 PM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

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...

Jun 13 '11 at 09:39 PM Bunny83

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?

Jun 13 '11 at 09:46 PM Muzz5

My bad! I've changed it ;)

Jun 13 '11 at 10:42 PM Bunny83

Thanks! Upvoted.

Jun 14 '11 at 07:21 AM Muzz5
(comments are locked)
10|3000 characters needed characters left

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;
more ▼

answered Jun 13 '11 at 08:51 PM

hellcats gravatar image

hellcats
651 8 12 23

Nice example but Mathf.Sin/Cos takes an angle in radian not a value between 0 and 1 ;)

Jun 13 '11 at 08:58 PM Bunny83

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.

Jun 14 '11 at 02:16 AM hellcats

:D sure it is but doesn't make much sense :D

Of course it's just a sample ;)

Jun 15 '11 at 12:43 AM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1366
x300
x238
x12

asked: Jun 13 '11 at 08:38 PM

Seen: 2147 times

Last Updated: Jun 15 '11 at 12:43 AM