Generating UVs for a scripted mesh

I generated a plane through script, now I need to generate UVs for it so it can render a texture on it. I having a difficult time understanding how to do this as im used to the visual tools I use in 3d packages.

All I would like to do is stretch out the texture to be the size of my scripted plane.

I used this example from the documentation, It allowed me to render a texture but not correctly (just a bunch of lines) I messed with some of its parameters and havnt had any luck.

        Vector3[] vertices = new Vector3[4];
    Vector2[] uvs = new Vector2[vertices.Length];
    int i = 0;
    while (i < uvs.Length) {
        uvs <em>= new Vector2(vertices_.y, vertices*.x);*_</em>
 <em>_*i++;*_</em>
 <em>_*}*_</em>
 <em><em>*m_Mesh.uv = uvs;*</em></em>
<em>_*```*_</em>

You'll want to divide vertices_.y and vertices*.x by the width and height of your plane. Your UV values should be between 0 and 1 in both dimensions if you want the texture to just stretch across the plane and not repeat.

*_

I have created this class to calculate UV’s for any given triangular vertices. It works for meshes that are programmatically generated from triangles (where the triangle vertices are stored clockwise). It assumes the length of the vertex array is a multiple of 3 (it doesn’t check the array’s length).

The public method’s first parameter takes the triangle vertices (in multiples of 3). The second parameter is a float, used to scale the texture coordinates (just use 1.0f to have default scaling). It returns an array of UV values, the array will be the same length as the input vertices array.

public class UvCalculator
{
	private enum Facing { Up, Forward, Right };
	
	public static Vector2[] CalculateUVs(Vector3[] v/*vertices*/, float scale)
	{
		var uvs = new Vector2[v.Length];
		
		for (int i = 0 ; i < uvs.Length; i += 3)
		{
			int i0 = i;
			int i1 = i+1;
			int i2 = i+2;
			
			Vector3 v0 = v[i0];
			Vector3 v1 = v[i1];
			Vector3 v2 = v[i2];
			
			Vector3 side1 = v1 - v0;
			Vector3 side2 = v2 - v0;
			var direction = Vector3.Cross(side1, side2);
			var facing = FacingDirection(direction);
			switch (facing)
			{
			case Facing.Forward:
				uvs[i0] = ScaledUV(v0.x, v0.y, scale);
				uvs[i1] = ScaledUV(v1.x, v1.y, scale);
				uvs[i2] = ScaledUV(v2.x, v2.y, scale);
				break;
			case Facing.Up:
				uvs[i0] = ScaledUV(v0.x, v0.z, scale);
				uvs[i1] = ScaledUV(v1.x, v1.z, scale);
				uvs[i2] = ScaledUV(v2.x, v2.z, scale);
				break;
			case Facing.Right:
				uvs[i0] = ScaledUV(v0.y, v0.z, scale);
				uvs[i1] = ScaledUV(v1.y, v1.z, scale);
				uvs[i2] = ScaledUV(v2.y, v2.z, scale);
				break;
			}
		}
		return uvs;
	}
	
	private static bool FacesThisWay(Vector3 v, Vector3 dir, Facing p, ref float maxDot, ref Facing ret)
	{
		float t = Vector3.Dot(v, dir);
		if (t > maxDot)
		{
			ret = p;
			maxDot = t;
			return true;
		}
		return false;
	}
	
	private static Facing FacingDirection(Vector3 v)
	{
		var ret = Facing.Up;
		float maxDot = Mathf.NegativeInfinity;
		
		if (!FacesThisWay(v, Vector3.right, Facing.Right, ref maxDot, ref ret))
			FacesThisWay(v, Vector3.left, Facing.Right, ref maxDot, ref ret);
		
		if (!FacesThisWay(v, Vector3.forward, Facing.Forward, ref maxDot, ref ret))
			FacesThisWay(v, Vector3.back, Facing.Forward, ref maxDot, ref ret);
		
		if (!FacesThisWay(v, Vector3.up, Facing.Up, ref maxDot, ref ret))
			FacesThisWay(v, Vector3.down, Facing.Up, ref maxDot, ref ret);
		
		return ret;
	}
	
	private static Vector2 ScaledUV(float uv1, float uv2, float scale)
	{
		return new Vector2(uv1 / scale, uv2 / scale);
	}
}

Hi thanks @intrepidis for sharing, I had to change the Facing.Right slightly to prevent the UV from rotating 90 degrees on portions of a circular mesh:

            case Facing.Right:
                //This was causing circular meshes to switch uv sideways
                //uvs.Add(ScaledUV(v0.y, v0.z, scale));
                //uvs.Add(ScaledUV(v1.y, v1.z, scale));
                //uvs.Add(ScaledUV(v2.y, v2.z, scale));

                uvs.Add(ScaledUV(v0.z, v0.y, scale));
                uvs.Add(ScaledUV(v1.z, v1.y, scale));
                uvs.Add(ScaledUV(v2.z, v2.y, scale));
                break;

I am using Unwrapping.GenerateSecondaryUVSet() to reasonable effect for light mapping, although I find that in certain circumstances I get bleeding between unrelated geometry. There is another static UV generation method on the Unwrapping class that may also help.