# Spherified cube

This topic is 1507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi!

Just a quick question regarding the process of converting a cube to a sphere, like this:#

...am I right in guessing that this could be done in a vertex shader by taking each vertex, finding a vector from the vertex to the centre of the cube and moving the vertex along that vector until its length matches the desired sphere radius? Or is it more complicated than that?

Thanks a lot :)

##### Share on other sites

That's OK, I had to do it once as an excercise while learning about shaders and that's the approach I used. I had to animate that transformation, so I passed the time to the shader and interpolated the current position with the final position.

Edited by DiegoSLTS

##### Share on other sites

Hi!

Just a quick question regarding the process of converting a cube to a sphere, like this:#

...am I right in guessing that this could be done in a vertex shader by taking each vertex, finding a vector from the vertex to the centre of the cube and moving the vertex along that vector until its length matches the desired sphere radius? Or is it more complicated than that?

Thanks a lot

That'll work pretty well.

If you want to reduce the distortion slightly (i.e. reduce the difference in size between the largest quads and the smallest quads), then there is another more complicated method you might want to look at (I use it in Rapture): http://mathproofs.blogspot.co.uk/2005/07/mapping-cube-to-sphere.html

I would only do the more complex approach if the distortion is a problem though.

##### Share on other sites

If your cube is around the model space origin, wouldn't it work just by normalizing the vertex position and multiplying it by the desired radius?

ie.

float4 SpheriefiedPos = float4(normalize(Input.Pos.xyz) * radius, 1.0f);

and to animate this,

float4 FinalPos = lerp(SpheriefiedPos, Input.Pos, factor);

 Or is there a problem which will introduce some undesired distortion?

Cheers!

Edited by kauna

##### Share on other sites

am I right in guessing that this could be done in a vertex shader

In theory, yes. And for small cubes/spheres this will work fine.

Unfortunately, planets tend to be pretty large, and you will quickly run out of floating-point precision in the vertex shader. It tends to become necessary to do this calculation on the CPU using doubles, and then pass the results to the GPU in small patches, each centered around a local origin.

##### Share on other sites

Thankyou for the replies. I tried it and found that it is indeed incredibly simple to do assuming that the cube's model space origin is right at its centre, as kauna said.

swiftcoder: at the moment I'm just experimenting and this was just a passing thought. I am eventually hoping to apply something like this to planets but I will most likely scale the planet itself down and just render it closer so that I can still use a small mesh.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633662
• Total Posts
3013229
×