- Home /
How do UVs work?
I know this seems like a silly question but I have no experience in this area and all I know is that it has something to do with textures. At the moment I am creating a mesh at run time and I am unsure how to texture it exactly with the UVs. I have checked in the Unity script reference but their examples dont seem very explanatory in this area. If you could just tell me how I would put a texture onto a square (2 tri, 4 vert) mesh that would be great, Thanks!
Great question, with a great title! Others will be able to easily search for this and find a good explanation below. :) +1!
I would very much like to upvote this question (if i had the rep for it.) Thanks for asking!
Answer by equalsequals · Aug 08, 2011 at 08:58 PM
UV(W) coordinates are a normalized (0 to 1) 2-Dimensional coordinate system, where the origin (0,0) exists in the bottom left corner of the space. UV is in this case synonymous to XY, but since XY is used in the XYZ 3D space so we use UVW in the texture space to avoid confusion.
Note that there is a W which corresponds to the Z axis, but it won't be used in a Texture2D as it is 2D.
So with that said, think of this 0 to 1 space as 0% to 100% of the image. If your texture is 128x128 then having a coordinate of 1 in either the U or V would mean that that particular vertex will exist at the normalized bounds of our texture space.
Example: a vertex with a UV of .5,.5 will be in the exact center of the image.
This works for non-square as well. Example: an image of 512,128 and a vertex with a UV coordinate of .5,.5 will exist at 256,64 in our texture.
Now, you asked how a 4vert quad would look, in short it would look something like this:
top-left 0,1 top-right 1,1 bottom-left 0,0 bottom-right 1,0
Depending on how you've assembled your triangles will vary on what vertex is what, but follow those guidelines and you should be able to figure it out.
[Edit] Here is a quick class that builds a quad. I did this step by step and tried to avoid a lot of the short hand I'd normally use to be as decipherable as possible.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
public class DrawQuad : MonoBehaviour
{
// Use this for initialization
void Start ()
{
Mesh mesh = new Mesh();
Vector3[] vertices = new Vector3[4];
vertices[0] = new Vector3(0,0,0); //top-left
vertices[1] = new Vector3(1,0,0); //top-right
vertices[2] = new Vector3(0,-1,0); //bottom-left
vertices[3] = new Vector3(1,-1,0); //bottom-right
mesh.vertices = vertices;
int[] triangles = new int[6]{0,1,2,3,2,1};
mesh.triangles = triangles;
//this is also acceptable!
//mesh.SetTriangleStrip(new int[4]{0,1,2,3}, 0);
Vector2[] uvs = new Vector2[4];
uvs[0] = new Vector2(0,1); //top-left
uvs[1] = new Vector2(1,1); //top-right
uvs[2] = new Vector2(0,0); //bottom-left
uvs[3] = new Vector2(1,0); //bottom-right
mesh.uv = uvs;
Vector3[] normals = new Vector3[4]{Vector3.forward,Vector3.forward,Vector3.forward,Vector3.forward};
mesh.normals = normals;
//you could also call this instead...
//mesh.RecalculateNormals();
//grab our filter.. set the mesh
MeshFilter filter = GetComponent<MeshFilter>();
filter.mesh = mesh;
//you can do your material stuff here...
//MeshRenderer r = GetComponent<MeshRenderer>();
//r.material = new Material(Shader.Find("my_shader_here"));
}
}
Hope that helps.
==
Thanks, this is really helpful, the only problem is I am having a tough time getting this to work, is there any way you could give a scripting example?
Good answer. Its worth adding that uvs outside of the (0,0) to (1,1) range will repeat the texture as long as clamping is off.
Thanks so much for this code, this is really helpful! A few more quick questions (if it is not to much of a hassle), How would combine multiple of these together? How do the triangles work exactly? What I am trying to get at is if I want to add other faces how would I go about doing that? Thanks again!