I have a mesh that represents portion of the surface of a sphere and I am having some trouble figuring out the UV coordinates for the verts.
Given the generation code for the verts could someone explain / provide an example of how I can determine the uv coordinates?
The "Region" passed in contains north, south, east, and west in degrees. The code converts the values to radians and determines the amount to step for the desired number of verts in both horizontal and vertical directions.
That much works fine.
For this "region" a portion of the surface of a sphere is generated and I want to place a texture on that portion where the corners of the texture are also the corners of the mesh.
In other words how do I do the "todo" in the code below ...
public void BuildVerts(Voxels.Objects.PlanetRegion region, int planetSize, int verticals, int horizontals, out Vector3[] verts, out Vector2[] uvs)
{
// determine range and stepping variable
var range = region.ToRadians();
var verticalStep = (range.East - range.West) / horizontals;
var horizontalStep = (range.North - range.South) / verticals;
// define result containers
var vertList = new List<Vector3>();
var uvList = new List<Vector2>();
// ok lets do this
for (double az = range.West; az <= range.East; az += horizontalStep)
{
for (double inc = range.South; inc <= range.North; inc += verticalStep)
{
var newVert = new Vector3(
(float)(planetSize * Math.Sin(az) * Math.Cos(inc)),
(float)(planetSize * Math.Sin(az) * Math.Sin(inc)),
(float)(planetSize * Math.Cos(az))
);
//TODO: Determine this!
var newUv = new Vector2();
vertList.Add(newVert);
uvList.Add(newUv);
}
}
verts = vertList.ToArray();
uvs = uvList.ToArray();
}
You could use the azimuth and inclination for this:
u = (az - range.West) / (range.East - range.West);
v = (inc - range.South) / (range.North - range.South);
This simply maps your steps from 0 to 1. Note that this will give you a distorted texture map, i.e. small areas (e.g. top of the sphere) will use the full width of your texture. If you don't want this distortion, you need to drop your requirement of the corners of the mesh also being the corners of the texture.