- Home /
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[i] = new Vector2(vertices[i].y, vertices[i].x);
i++;
}
m_Mesh.uv = uvs;
Answer by flaviusxvii · May 13, 2011 at 05:42 PM
You'll want to divide vertices[i].y and vertices[i].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.
Or, since he only has 4 verts, just handset them to (0,0) (0,1) (1,1) and (1,0): uvs[0]=new Vector2(0,0); ...
. (0,0) is the lower-left corner and (1,1) is the upper-right. For a subdivided plane, yeah, you have to divide.
True. I was addressing the more general case but you're absolutely right.
Thanks, and Im assu$$anonymous$$g in cases where I want the texture to repeat I just have to make the number in the axis even smaller, correct?
If you want the texture to repeat 3 times, then multiply your UV values by 3.
Answer by intrepidis · Aug 02, 2014 at 08:37 PM
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);
}
}
initialize maxDot with 0 ins$$anonymous$$d of NegativeInfinity and there's a bug gone ;)
THAN$$anonymous$$ YOU! This was driving me crazy I couldn't figure out what the issue was.
I have 4 vertices and this does not work for me: IndexOutOfRangeException: Index was outside the bounds of the array. UvCalculator.CalculateUVs (UnityEngine.Vector3[] v, System.Single scale)
Well he did say his code assumes your vertices are a multiple of 3. 4 is not a multiple of 3. I have run into the same issue, and its an easy fix to loop around the final odd vertices back to the beginning. Add these small if checks into the code:
<...>
int i0 = i;
int i1 = i+1;
int i2 = i+2;
//Special handling if vertex count isn't a multiple of 3
if(i == uvs.Length - 1) {
i1 = 0;
i2 = 1;
}
if(i == uvs.Length - 2) {
i2 = 0;
}
Vector3 v0 = v[i0];
Vector3 v1 = v[i1];
Vector3 v2 = v[i2];
<...>
Answer by Bovine · May 06, 2012 at 06:47 PM
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.
Answer by tapticc · Nov 06, 2016 at 08:08 PM
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;