Problems with uv coordinates on generated mesh

I’m working on a project where I need to generate some mesh circles.

In my first test everything worked fine ( see image 1 ), but I would like the mesh to be connected - the last face should connect back to the first to vertices, and this is where my problems start. I spend hours trying to make this work, but no matter what I do, the texture seems to repeat on the last face ( see image 2 3 ).



I using this code to make the circle. If you want to test it just attach the code to an empty gameObject.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]

public class MeshCircle : MonoBehaviour
{
	//
	public int numFaces = 10;
	public int innerRadius = 10;
	public int outerRadius = 12;
	public Material mat;
	
	public Vector3[] verts;
	private Vector2[] uvs;
	public int[] trinangles;
	private Color[] vertColor;
	
	private MeshFilter myMeshFilter;
	private Mesh myMesh;
	
	private float xPos=0f, yPos = 0f, zPos = 0f;
	
	private int index = 0;
	private int triangleIndex = 0;
	
	void Start()
	{
		myMeshFilter = this.GetComponent(typeof(MeshFilter)) as MeshFilter;
		myMesh = myMeshFilter.mesh;
		
		verts = new Vector3[numFaces * 2];
		uvs = new Vector2[numFaces * 2];
		vertColor = new Color[numFaces * 2];
		trinangles = new int[(numFaces-0) * 2 * 3];
		
		myMesh.Clear();
		
		if(mat)
			this.renderer.material = mat;
		
		for (int i = 0; i < numFaces; i++)
		{
			float angle = Mathf.PI * 2 / numFaces * i;
			
			xPos = Mathf.Cos(angle) * outerRadius;
			yPos = Mathf.Sin(angle) * outerRadius;
			
			verts[index] = new Vector3(xPos, yPos, zPos);
			
			xPos = Mathf.Cos(angle) * innerRadius;
			yPos = Mathf.Sin(angle) * innerRadius;
			
			verts[index + 1] = new Vector3(xPos, yPos, zPos);
			
			float uvScale = 1f / (numFaces-1) * i;
			
			uvs[index] = new Vector2(uvScale, 1);
			uvs[index + 1] = new Vector2(uvScale, 0);
			
			index += 2;
		}
		
		index = 0;
		
		for (int i = 0; i < numFaces-1; i++)
		{
			trinangles[i * 6 + 0] = index;//i * 2;
			trinangles[i * 6 + 1] = index + 3;//i * 2 + 3;
			trinangles[i * 6 + 2] = index + 1;//i * 2 + 1;
			
			trinangles[i * 6 + 3] = index;//i * 2;
			trinangles[i * 6 + 4] = index + 2;//i * 2 + 2;
			trinangles[i * 6 + 5] = index + 3;//i * 2 + 3;
			
			index += 2;
			triangleIndex = i * 6 + 5;
		}
		
		
		// connect the last face back to the 2 first vertices //
		trinangles[triangleIndex + 1] = index;
		trinangles[triangleIndex + 2] = 1;
		trinangles[triangleIndex + 3] = index + 1;
			
		trinangles[triangleIndex + 4] = index;
		trinangles[triangleIndex + 5] = 0;
		trinangles[triangleIndex + 6] = 1;
		
		
		
		myMesh.vertices = verts;
		myMesh.uv = uvs;
		myMesh.triangles = trinangles;
		myMesh.RecalculateBounds();
		
	}
}

I hope someone out there can see where my problem is.

Thanks in advance

Bjerre

You directly connect the last vertices of the circle to the first. The uv coordinates of the last vertices are around 1, the coordinates of the first vertices are around 0. This means the faces between those vertices will contain pretty much the entire texture. You cannot re-use the original vertices that start the circle, you will need to create new ones at the exact same location, but with texture coordinates of 1 instead of 0.

Don’t know why I didn’t see that.

Thank you very very much for pointing that out.

/ Bjerre

You could re-use the vertices, but you’d have to go “up and down” with the UV coordinates, in a fashion like this:

segment 0: 0
segment 1: 0.5
segment 2: 1
segment 3: 0.5

It would limit the kind of texture you can use on the circle, but it should work well in this particular case.