- Home /
Creating a sphere out of six planes
Hello,
I want to create a sphere from six planes (to use normal maps on them). But I have problems with the math. I got the math from here http://mathproofs.blogspot.com/2005/07/mapping-cube-to-sphere.html
Converted it to a formular I want to use in unity
var mesh : Mesh = GetComponent(MeshFilter).mesh;
var vertices : Vector3[] = mesh.vertices;
var normals : Vector3[] = mesh.normals;
for (var i = 0; i < vertices.Length; i++)
{
vertices[i] += Vector3(Mathf.Sqrt(
1-(Mathf.Pow(vertices[i].y,2) / 2)
- (Mathf.Pow(vertices[i].z,2) / 2)
+ ((Mathf.Pow(vertices[i].y,2) * Mathf.Pow(vertices[i].z,2)) / 3)),
Mathf.Sqrt(
1-(Mathf.Pow(vertices[i].z,2) / 2)
- (Mathf.Pow(vertices[i].x,2) / 2)
+ ((Mathf.Pow(vertices[i].z,2) * Mathf.Pow(vertices[i].x,2)) / 3)),
Mathf.Sqrt(
1-(Mathf.Pow(vertices[i].x,2) / 2)
- (Mathf.Pow(vertices[i].y,2) / 2)
+ ((Mathf.Pow(vertices[i].x,2) * Mathf.Pow(vertices[i].y,2)) / 3)));
}
mesh.vertices = vertices;
Im fairly sure I did something wrong with the formular , im really not good in mathematics.
Thats how it looks like that:
You see its just wrong :)
Its like its shrinked, it is not flat, its a little "rounded", but its not like I want it, also much much too low poly (I tried it with a from 3ds max imported high poly cube, same result) I used that formular from the website:
Looks like you forgot the x*
at the start (and the y*
and 'z*') -- right in front of all the square roots.
Plus, there are probably easier ways to do whatever normal map thing it is.
I also had this x* ... it doesnt worked and looked even more crappy, so i removed it. Easier ways? What do you have in $$anonymous$$d? afaik thats the easiest way.
Turning a cube into a sphere, leaving normals alone, will give the new sphere a "flat-sided" shading -- the exact opposite of what most people want. But, if you really wanted that, you could start with a sphere and assign normals (1,0,0), (0,1,0) ... to each vert based on the largest axis.
You might also check your original model has (0,0,0) in the exact center (the formula seems to assume that.)
Well what I have to do with the normals? I just changed vertices, I tried to change normals but nothing happens (I never worked with them). Whats the difference between normals and vertices? The position of the object is $$anonymous$$or. Please see my comment below for actual state. Still dont know how to go on :/
making round things from 6 planes?I have done something related, UV mapping any procedural shaped with 6 planes, called box projection, you can map spheres also, although I haven't figured the normals yet.
Answer by DaveA · Feb 14, 2012 at 12:11 AM
Some thoughts: You should only need to deform one plane (Unity's default plane has 10 x 10 cells, if you need more, use Blender to make one, or generate it from scratch). Make sure you are deforming in the correct direction for that plane. Also you probably want to use = instead of += to assign the new values
Yes I tried also = , its nearly the same result. What do you mean by one plane? Its common to use six planes to create a sphere out of it, never heard of just one plane. The cube you see there is made of 6 planes, so there is every direction included, but you see the result :/
If you tried it on just one plane, you'd get just one of those shallow bowl shaped meshes. It would be easier to see any problems. Once that worked, you could copy it, rotated to make the other 5 "faces."
I tried it with just one plane, with 6 seperated and with a high poly cube. All is the same result, it has nothing to do with orientation or position :/ I make no progress, yesterday tried the whole day how to create the sphere. But its always nearly the same result as you see above.
I used this code now :
for (var i = 0; i < vertices.Length; i++)
{
vertices[i] = Vector3(vertices[i].x*($$anonymous$$athf.Sqrt(
1-($$anonymous$$athf.Pow(vertices[i].y,2) / 2)
- ($$anonymous$$athf.Pow(vertices[i].z,2) / 2)
+ (($$anonymous$$athf.Pow(vertices[i].y,2) * $$anonymous$$athf.Pow(vertices[i].z,2)) / 3))),
vertices[i].y*($$anonymous$$athf.Sqrt(
1-($$anonymous$$athf.Pow(vertices[i].z,2) / 2)
- ($$anonymous$$athf.Pow(vertices[i].x,2) / 2)
+ (($$anonymous$$athf.Pow(vertices[i].z,2) * $$anonymous$$athf.Pow(vertices[i].x,2)) / 3))),
vertices[i].z*($$anonymous$$athf.Sqrt(
1-($$anonymous$$athf.Pow(vertices[i].x,2) / 2)
- ($$anonymous$$athf.Pow(vertices[i].y,2) / 2)
+ (($$anonymous$$athf.Pow(vertices[i].x,2) * $$anonymous$$athf.Pow(vertices[i].y,2)) / 3))));
}
And the result is that:
http://imageshack.us/photo/my-images/263/crapcube.png/ out of that plane: http://imageshack.us/photo/my-images/836/planei.png/
Your code above works perfectly -- I tried it on a high-poly cube, with (0,0,0) in the center. It deforms it into a perfect sphere, keeping the same lighting as when it was a cube.
I can't think of a use for it. Normal mapping works fine on regular spheres.
Bump maps work perfectly on a sphere. But if you want to create a Planet, you need mountains, hills, craters, canyons and so on. I want to do that with a highmap. That cant be used on a sphere, but on 6 pla nes that are then formed to a sphere. Thats the usual way how to do it. Also normal spheres got too much disortion.
I wonder why this is working for you ... Its not working for me o.o.
I try that for days now and want to know what i do wrong, if my code is correct --
Answer by Xete · Feb 08, 2013 at 07:38 AM
okay i may have found out the problem although i am not sure, simply have:
var amount : float = 1.0f; var refresh:boolean = false; function Start () { tryu(); }
at top then this function Update () { if(refresh){ tryu(); refresh=false; } }
//you be able to see once u keep pressing refresh it begins to turn into a sphere, although not perfectly atm, and thanks for sharing, p.s first time postin so sorry if its already anwsered
okay improvement, now time to sleep ,lucid dream timeeeeeeee, anyways so before starting on the inspector mode, simple change the amount to 0.5f, and with one refresh on a high poly cube will make it nicely perfect sphere, well like 80%perfect, it kinda expands the cube making it more suitable rather than shrinking it to sphere which isnt good
doesnt seem to work on planes tho, it has the same issue in blender,works fine with cubes..sorry
(meant to be try kinda hurried cuz it was sleep time) and wohoo atm i managed to get the spherical thing working with both a cube and a plane, it just applies your code to the object many times u want, once u have it working just once then you can just put it in start function, of course u gotta change the amount too, where it goes is Vector3(vertices[i].x/ amount *.... <-- each time u calculating a vertice position, then change ur amount to 0.5 in inspector and it shud work once (for plane into spherical form) and for cube well u have to run it and then refresh it again in the inspector to make it a sphere,, plane =1 refresh, cube = 2 refresh, atm im trying to apply random heights for each plane and sorry if i dnt make sense