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.

The Mesh class gives you 6 arrays:

  • [.vertices][1]
  • [.colors][2]
  • [.normals][3]
  • [.tangents][4]
  • [.uv][5]
  • [.uv2][6]

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][7] 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 <em>= Vector2(myVectices_.x,myVectices*.y);*_</em>

}
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)
*[1]: http://unity3d.com/support/documentation/ScriptReference/Mesh-vertices.html*_
[2]: http://unity3d.com/support/documentation/ScriptReference/Mesh-colors.html*_
_[3]: http://unity3d.com/support/documentation/ScriptReference/Mesh-normals.html*_

*[4]: http://unity3d.com/support/documentation/ScriptReference/Mesh-tangents.html*_
[5]: http://unity3d.com/support/documentation/ScriptReference/Mesh-uv.html*_
_[6]: http://unity3d.com/support/documentation/ScriptReference/Mesh-uv2.html*_

_*[7]: http://unity3d.com/support/documentation/ScriptReference/Mesh-triangles.html*_

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*.x = Mathf.Sin(i / 1000.0f);*

_ uv*.y = Mathf.Cos(i / 1000.0f);_
_
}*_

mesh.uv = uv;