Texture on custom mesh splits at edge.

Hi! So i’m trying to generate a railroad for my top-down game. What I do is I take an array of points and their velocities and generate a mesh accordingly. It is a simple mesh consisting of a sequence of quads. Like this:
[57369-railroad.jpg*_|57369]

My problem comes from the way the texture is applied, specifically - the way it distorts when the turn is too steep : [57371-warping.jpg*_|57371]

You can see the way the texture splits and warps at the edge between the tris. I am pretty sure i’m mapping the uvs correctly(I’ve illustrated the coordinates that i assign to the vertexes of each quad). Now, I suspect that the problem can be solved by smoothing the mesh out, but I don’t see a reason to increase my polycount when all I want is a simple texture adjustment. So my question is - how can I stretch and apply the texture relative to the quad, rather than the tris? How can I avoid this splitting without adding more polygons? Here’s a sample of my code, in case you suspect that’s where the problem stems from:

void GenerateMesh(Vector3[] points, Vector3[] directions) {
	//Destroys the old mesh if there is one
	if (railwayMesh != null) {
		DestroyImmediate(railwayMesh);
		railwayMesh = null;
	}
	Mesh mesh = new Mesh ();

	Vector3[] verts = new Vector3[points.Length*4 - 4];
	Vector2[] uvs = new Vector2[points.Length*4 - 4];
	int[] tris = new int[(points.Length - 1)*6];

	uvs[0] = new Vector2(0.0f, 0.0f);
	uvs[1] = new Vector2(1.0f, 0.0f);
	uvs[uvs.Length-2] = new Vector2(1.0f, 0.0f);
	uvs[uvs.Length-1] = new Vector2(1.0f, 1.0f);

	for (int i = 0; i < points.Length; i++) {

		//Here i generate the 3d coordinates of the two vertexes relative
		//to the position of the current point in our curve and its velocity.
		Vector3 reverse = (directions <em>+ points_) - points*;*_</em>

Vector3 leftPoint = points + (Vector3)(new Vector2(-reverse.y, reverse.x) / Mathf.Sqrt(reverse.xreverse.x + reverse.yreverse.y) * railwayWidth);
Vector3 rightPoint = points + (Vector3)(new Vector2(-reverse.y, reverse.x) / Mathf.Sqrt(reverse.xreverse.x + reverse.yreverse.y) * -railwayWidth);

* //I assign the positions of the vertex. Note that i use*
* //two vertexes at the connecting points between two quads,*
* //so that i can assign the uvs for both the previous and*
* //the next quad.*
* if(i != 0 && i != points.Length-1) {*
_ verts[i*4 - 2] = leftPoint;
verts[i*4 - 1] = rightPoint;
verts[i*4] = leftPoint;
verts[i*4 + 1] = rightPoint;
* }
else if(i == 0) {
verts[0] = leftPoint;
verts[1] = rightPoint;
}
else if(i == points.Length - 1) {
verts[i*4 - 2] = leftPoint;
verts[i*4 - 1] = rightPoint;
}
if(i != 0 && i != points.Length-1) {
uvs[i*4 - 2] = new Vector2(0.0f, 1.0f);
uvs[i*4 - 1] = new Vector2(1.0f, 1.0f);
uvs[i*4] = new Vector2(0.0f, 0.0f);
uvs[i*4 + 1] = new Vector2(1.0f, 0.0f);
}
if(i != 0) {
tris[i*6 - 6] = i4 - 4;

tris[i*6 - 5] = i4 - 2;
tris[i*6 - 4] = i4 - 3;

tris[i*6 - 3] = i4 - 2;
tris[i*6 - 2] = i4 - 1;

tris[i*6 - 1] = i4 - 3;
}
}
mesh.vertices = verts;
mesh.triangles = tris;
mesh.uv = uvs;
mesh.RecalculateNormals();
GameObject newRailway = new GameObject ();
newRailway.name = “Railway”;
newRailway.AddComponent ().mesh = mesh;
newRailway.AddComponent ().material = railwayMaterial;
railwayMesh = newRailway;
}
I want to apologize in case i’m asking a question that’s already been answered, but i’ve been looking for a solution for a while and posting here is my last resort. Lastly, I want to thank you in advance for your assistance!

_*

Well, as far as i know there’s no way to actually “correct” this in Unity since Unity only uses uv texture coordinates (Vector2d array). See this page. The graphics hardware actually renders each triangle on it’s own. However since only 3 points are given for a single triangle the texture is only sheared but doesn’t change it’s scaling to match the “perspective” effect. That would require the information of the “4th” vertex which doesn’t belong to the actual triangle.

It would be great if someone has a solution for this ^^. If an actually “right angled” quad is rendered with perspective, the GPU does this correction automatically based on the distance from the camera. However when you render a flat trapezoid the result will look like in your example when using just “uv” / “st” coordinates.

edit

As i thought there’s no way to specify a 4 component texture coordinate in Unity. However Jessy has a solution to use the second texture coordinate channel to provide that information and calculate the correct coordinate in the fragment shader