x


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)...

alt text

more ▼

asked May 28 '11 at 11:47 PM

ina gravatar image

ina
3.3k 492 547 597

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

May 28 '11 at 11:48 PM ina
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
more ▼

answered May 29 '11 at 12:07 AM

Wolfram gravatar image

Wolfram
9k 8 20 52

why? and why are things scaled by pi by 2?

May 29 '11 at 02:25 AM ina

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. More intuitively, real latitude ranges from asin(1)=Pi/2 (=90 deg) to asin(-1)=-Pi/2 (=-90 deg).

May 29 '11 at 02:53 AM Wolfram
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1353
x298
x236
x197
x138

asked: May 28 '11 at 11:47 PM

Seen: 1928 times

Last Updated: Dec 17 '11 at 10:58 PM