Comments and answers for "Reverse Spherifying A Cube"
http://answers.unity.com/questions/764203/reverse-spherifying-a-cube.html
The latest comments and answers for the question "Reverse Spherifying A Cube"Comment by Bunny83 on Bunny83's answer
http://answers.unity.com/comments/764319/view.html
@Chimera3D: Found a much easier solution. See my edit in the answer.Tue, 05 Aug 2014 12:31:40 GMTBunny83Comment by Chimera3D on Chimera3D's answer
http://answers.unity.com/comments/764281/view.html
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;
}Tue, 05 Aug 2014 11:29:01 GMTChimera3DAnswer by Bunny83
http://answers.unity.com/answers/764232/view.html
I'm not sure why someone would need that, but the easiest way would be a two step approach:
1. 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)
2. 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;
}
}Tue, 05 Aug 2014 09:57:31 GMTBunny83