- Home /

# UV-ing a sphere procedurally

So, I am trying to learn more about how to UV something procedurally. The first toy problem I'm trying to solve is how do you UV the default UV on the default Unity sphere.

From left to right - default, planar, and Wikipedia spherical

for(var i=0;i<uvs.Length;i++){ var r = Mathf.Sqrt(vertices[i].x*vertices[i].x + vertices[i].y*vertices[i].y + vertices[i].z*vertices[i].z); uvs[i] = Vector2(vertices[i].z/r,vertices[i].x/r); }

If I am conveying the UV coordinates right, it's not planar or the Wikipedia sphere UV (code snippet above)...

For some reason the other part of my question got truncated, and edit mode deletes half the post o.O -- the question is how do you loop thru the uv coordinates to get the uv mapping of the unity default sphere

**Answer** by Wolfram
·
May 29, 2011 at 12:07 AM

It's more like something in the line of:

```
uvs[i] = Vector2(Mathf.Atan2(vertices[i].z,vertices[i].x)/Mathf.Pi/2,Mathf.Acos(vertices[i].y/r)/Mathf.Pi);
```

If you cut the sphere anywhere horizontally (=the X-Z-plane, if Y is up), you will get a circle. The position, or more precisely, the angle of a point on that circle is determined by the x and z coordinates. To compute this "longitude" for a given point on that circle, you can use the http://en.wikipedia.org/wiki/Atan2 function. However, since it returns a value in radians, where 2*Pi is a full circle, you need to divide by 2*Pi to map to the 0..1 range.

Similarly, for the height, you can compute the "latitude" angle using the ratio height:radius, so that the "north pole" results in acos(1/1)=0, and the "south pole" in acos(-1/1)=Pi - again, the angle is in radians, so here divide by Pi (since we only have a half circle) to map to the 0..1 range. $$anonymous$$ore intuitively, real latitude ranges from asin(1)=Pi/2 (=90 deg) to asin(-1)=-Pi/2 (=-90 deg).

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.