This site uses strictly necessary cookies. More Information

X- Home /

# Mapping a cube to a sphere

I found this great articles http://mathproofs.blogspot.co.uk/2005/07/mapping-cube-to-sphere.html which describes mathematically how to map a cube to sphere, I was wondering how I could translate this into unity to map a programmatically generated cube mesh into a sphere?

I translated the equation into C# but I have no idea what I'm doing with it?

```
sx = x * Mathf.Sqrt(1.0f - y * y * 0.5f - z * z * 0.5f + y * y * z * z / 3.0f);
sy = y * Mathf.Sqrt(1.0f - z * z * 0.5f - x * x * 0.5f + z * z * x * x / 3.0f);
sz = z * Mathf.Sqrt(1.0f - x * x * 0.5f - y * y * 0.5f + x * x * y * y / 3.0f);
```

**Answer** by Bunny83
·
Dec 02, 2012 at 10:47 PM

It seems you try the same thing that we do in this question.

While it seems that your forumla is correct, it's just a complicated way of normalizing the vector ;)

Just take a position on the cube and normalize it.

```
var CubePoint = new Vector(cubeX, cubeY, cubeZ);
var SpherePoint = CubePoint.normalized;
```

This will map a point on the cube surface to the corresponding point on the unit sphere. Just multiply the vector with an float value to change the radius

```
var SpherePoint = CubePoint.normalized * radius;
```

Thanks for the reply, I tried what your suggested and it just seems to make a larger cube. I'm using the primitive cube and normalizing each vert.

uhm... the primitive cube has only 4 verts per face. You need much more to do this transformation. Just look at the example in the post you've linked:

Your cube only has the corner vertices which are of course connected with straight lines. When you map the corners onto the unit sphere, it will still look like a cube. You just need more vertices. In my test project (hopefully i will post it the next days) i currently use a 64x64 grid on each cube face while you use a 1x1 grid ;)

Perfect!! I've been wanting to achieve this for ages, Thanks @Bunny83 I'm working on the height map side of things now, using that original question helps a bunch.

btw. there's another problem with your formula and the unity primitive cube: The formula only works with cube coordinates between -1 and 1. Unity's cube vertices are between -0.5 and 0.5. This breaks the cube deffinition which this formula is based on.

I'm still impressed by the formula ;) It's a lot more complicated and only works with cube-surface coordinates as input. Normalizing is much simpler and works with any input.

btw just in case you don't know what normalizing does, here's the formula:

```
length = $$anonymous$$athf.Sqrt(x*x + y*y + z*z);
sx = x / length
sy = y / length
sz = z / length
```

No problem ;)

The spherifying and the heightmapping is quite easy. The real problem is to fix the edges between the 6 meshes.

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