- Home /

# Reverse Spherifying A Cube

I have [what I believe is] a relatively simple geometry question - how can I take a cube that has been transformed into a sphere (normalize the vertices of it and multiply by them by a value - the radius of the resulting sphere) and transform that sphere back into the original cube without any information from the original cube, except for the original cube's dimensions? Essentially:

**Answer** by Bunny83
·
Aug 05, 2014 at 09:57 AM

I'm not sure why someone would need that, but the easiest way would be a two step approach:

figure out to which side a vertex belongs. that's quite easy as you just have to find out which component of the vertex is the largest (absolute value)

Once you have the normal vector of the face you can use Unitys mathematical Plane struct to project each vertex onto the surface of your cube

Something like that:

```
void DeSpherify(Vector3[] aVertices, float aHalfCubeSize)
{
for(int i = 0; i < aVertices.Length; i++)
{
Vector3 v = aVertices[i];
Vector3 n;
if (Mathf.Abs(v.x) > Mathf.Abs(v.y))
{
if (Mathf.Abs(v.x) > Mathf.Abs(v.z))
n = new Vector3(Mathf.Sign(v.x),0,0);
else
n = new Vector3(0,0,Mathf.Sign(v.z));
}
else
{
if (Mathf.Abs(v.y) > Mathf.Abs(v.z))
n = new Vector3(0,Mathf.Sign(v.y),0);
else
n = new Vector3(0,0,Mathf.Sign(v.z));
}
var plane = new Plane(n * aHalfCubeSize, -n);
float dist;
var ray = new Ray(Vector3.zero, v.normalized);
if (plane.Raycast(ray, out dist))
{
aVertices[i] = ray.GetPoint(dist);
}
}
}
```

Wrote from scratch, haven't testet (yet) ;)

*edit*

Actually there's a much easier approach ;) I just thought about. You still need to find out which component is the largest, but then all you have to do is bringing that componwnt to your desired length (either normalized or your halfcubesize)

```
Vector3 Cubify(Vector3 aVertex)
{
float x = Mathf.Abs(aVertex.x);
float y = Mathf.Abs(aVertex.y);
float z = Mathf.Abs(aVertex.z);
if (x > y)
{
if (x > z)
return aVertex * 1f/x;
else
return aVertex * 1f/z;
}
else
{
if (y > z)
return aVertex * 1f/y;
else
return aVertex * 1f/z;
}
}
```

Thanks! I slightly modified the code to work for my specific purposes and getting it to fully work but this is what I ended up with (I did the for loop stuff in a different function).

```
Vector3 DeSpherify(Vector3 point)
{
Vector3 v = point;
Vector3 n;
if ($$anonymous$$athf.Abs(v.x) > $$anonymous$$athf.Abs(v.y))
{
if ($$anonymous$$athf.Abs(v.x) > $$anonymous$$athf.Abs(v.z))
n = new Vector3($$anonymous$$athf.Sign(v.x),0,0);
else
n = new Vector3(0,0,$$anonymous$$athf.Sign(v.z));
}
else
{
if ($$anonymous$$athf.Abs(v.y) > $$anonymous$$athf.Abs(v.z))
n = new Vector3(0,$$anonymous$$athf.Sign(v.y),0);
else
n = new Vector3(0,0,$$anonymous$$athf.Sign(v.z));
}
Plane plane = new Plane(-n, n * 300f);
float dist;
Ray ray = new Ray(Vector3.zero, v.normalized);
if (plane.Raycast(ray, out dist))
{
return ray.GetPoint(dist);
}else
return Vector3.zero;
}
```

@Chimera3D: Found a much easier solution. See my edit in the answer.

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

### Follow this Question

### Related Questions

Why am I getting 2 different results with RotateAround? Trying to move cubes on surface of sphere. 0 Answers

Calculating mesh over GameObject scale 1 Answer

Make an object rotate instantly upon an event occuring in C# 1 Answer

Sphere clipping in cube 1 Answer

How to make a "pressure sensor" of sorts that when rolled over switches to another scene 2 Answers