hey all, could really do with some help here, ive been playing around with meshes and got it all working nicely, excpet the lovely lil thing about UV mapping…
so please please help me as i dont a clue about UV mapping =)
Vector2[] uvs = new Vector2[NewMesh.vertices.Length];
for (var i = 0; i < uvs.Length; i++)
{
uvs[i] = new Vector2(vertices[i].x, vertices[i].z);
}
NewMesh.uv = uvs;
It depends on what effect your trying to achieve and how it is currently set up but the example on the docs annoys me, mainly because your talking about texture space with UV’s not mesh space. With the example above, you are setting your UV’s to match the position of your mesh in world space. This basically means your texture will be applied based on how your mesh is positioned. Now normally with UV’s the texture coordinates are 0 - 1, with 0,0 being the top left of your texture and 1,1 being the bottom right. In Unity, the positioning is upside down, so 0,1 is bottom right and 1,0 is top left. So all that being said, if you wanted a single texture applied to the mesh you have proposed above, with the texture orientated the same way, you would need to set the UV’s on the mesh as follows:
Just as further clarification, the UV’s are pointers to your texture. So if your UV’s are > 1 then you are effectively repeating your texture. So, instead of thinking UV values are the texture points on your mesh, think of UV values as the mesh points on your texture.
All good, working in texture space within unity can be a pain for anything bar simple items. If you know how big your mesh will be or if you always use a standardized mesh size your fine, otherwise you should use a 3d modeling program for more complex UV mesh assignments.
Popper, by the looks you are still thinking in World / Model space, not in UV space. Check out this guide to UV’s: UV Mapping Basics. Effectively what you need to do is figure out your generation end state. IE, what is the final mesh I want to generate going to look like. For you to do this successfully by script your mesh needs to build a consistent way for it to be unwrapped correctly. In 3D space, your mesh needs to be flattened out. The easiest way to do this with a cube it to detach the sides and unwrap it until you get six squares over the space of your texture, like this (Sorry for the Visio in advanced :-P):
Using your script you’re still trying to apply values > 1 to the UV space, which will give you repeating etc. What you really need to decide is what are you trying to achieve here ? IE, these generated script blocks need to look like what ? Are you thinking of doing something like minecraft where you are turning voxels into a mesh then applying a texture to it ? If you are looking to apply tiles to a mesh in a logical way, you might be better of constructing structure to deal with everything in a square / quads style method.