Jump to content
  • Advertisement
Sign in to follow this  
george7378

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!

 

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

 

planets-1-cubemap.png

 

...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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
Share on other sites

Hi!

 

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

 

planets-1-cubemap.png

 

...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 smile.png

 

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 this post


Link to post
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);

 

 

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

 

 

Cheers!

Edited by kauna

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!