Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Chimera3D · Aug 05, 2014 at 09:12 AM · transformcubesphereverticesnormalization

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:

alt text

planets-1-cubemap.png (100.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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:

  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;
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chimera3D · Aug 05, 2014 at 11:29 AM 0
Share

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;
 
     }

avatar image Bunny83 · Aug 05, 2014 at 12:31 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

How to add a different texture to each face of a cube. through script? 1 Answer

how to subdivide faces for a cube sphere 0 Answers

How do I guarantee a generated sphere's mesh is facing outward? 1 Answer

Setting triangles failing 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges