UV Mapping - Help please XD

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 =)

how ive setup my vertices -

The Code am using to UV Map -

            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;

The Result -

Any help would be awesome =)

~Popper

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:

Vertex: 0 UV: 0,0
Vertex: 1 UV: 0,1
Vertex: 2 UV: 0.5,0
Vertex: 3 UV: 0.5,1
Vertex: 4 UV: 1,0
Vertex: 5 UV: 1,1

Does that make sense ?

Edit:

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.

ah crap forgot to mention that bit - i would like the texture to follow the mesh and bend with it as such like not just tile across =)

So what you want to do with that mesh is:

Vector2[] uvs = new Vector2[NewMesh.vertices.Length]; 
uvs[0] = new Vector2(0, 0);
uvs[1] = new Vector2(1, 0);
uvs[2] = new Vector2(0, 0.5);
uvs[3] = new Vector2(1, 0.5);
uvs[4] = new Vector2(0, 1);
uvs[5] = new Vector2(1, 1);
NewMesh.uv = uvs;

This assumes you built your mesh from 0 to 5 on your diagram above.

yea but heres alot more vertices that just 1 of many :stuck_out_tongue:

crispie you my sir - are a legend :stuck_out_tongue:

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.

ok new challenge - am going 3D >…<

the UV map is all screwed - the top and bottom are fine but the sides are messed up…

Triangles are set up and indexes are still the same - picture above to show 2d Indexes and below to show 3d =)

[3D Index below]

the sides just dont really show texture at all…

PS i dont care about the top texture or bottom just the side textures :slight_smile:

owh code am using is

                Vector3[] Meshvertices = NewMesh.vertices;
                Vector2[] uvs = new Vector2[NewMesh.vertices.Length];
                int pos = 0;
                for (var i = 0; i < uvs.Length; i += 2)
                {
                    uvs[i] = new Vector2(0, pos);
                    uvs[i + 1] = new Vector2(1, pos);
                    pos++;
                }
                NewMesh.uv = uvs;

[Result am getting]


Its applying the texture correctly on top and bottom but not the sides =)

~Popper

try to use a UV mapp application …
or send me the object and the texture to help you, i will apply .

that would be a bit pointless as the mesh is generated through a script, so theres no point in using a external program to do the UV map…

cheers for the responce anway.

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.