Set UVs depending on input

I’ve made a function, to create basically a face for the a mesh, that i’m building through scripting. Altho, vertice, triangles and normals is all setup and calculated, UV’s is not.

I honestly, have no idea how to setup the UV’s, neither how it corresponds to what is what (An explanation too would be appreciated)

This is my create face code. It takes “Input” (A position, width and height) and then changes this, depending on that. I would think, that how it works?


private void CreateFace(Vector3 position,int width, int height) {
	Mesh_Vertices.Add(new Vector3(position.x, position.y, 0));
	Mesh_Vertices.Add(new Vector3(position.x+width, position.y, 0));
	Mesh_Vertices.Add(new Vector3(position.x, position.y+height, 0));
	Mesh_Vertices.Add(new Vector3(position.x+width, position.y+height, 0));

	int a = Mesh_Vertices.Count-1;
	int b = a-1;
	int c = a-2;
	int d = a-3;



	//This isn't correctly setup.
	Mesh_Uvs.Add(new Vector2(0,0));
	Mesh_Uvs.Add(new Vector2(1,0));
	Mesh_Uvs.Add(new Vector2(0,1));
	Mesh_Uvs.Add(new Vector2(1,1));

Your uv coordinates are coordinates within your texture, having 0,0 at bottom/left and 1,1 at the top right.

uv coords

so if your texture is 256x128 px in size and you have 64x64 tiles defined, this would make 4x2 tiles, which means if you want to grab the bottom/left tile, your texture coordinates defining the diagonal of that tile, would be 0,0 / 0.25,0.5

What’s the problem with your setup right now? Is the texture mirrored?