Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Benjames · Aug 27, 2018 at 10:16 PM · vector3mathspherenormalinverse

Reverse of this function?

I would like to have a function that finds x,y,z with a given vector. Opposite of this function.

     public Vector3 grid_norm(int x, int y, int z, int gridSize) {
         Vector3 v = new Vector3(x, y, z) * 2f / gridSize - Vector3.one;
         float x2 = v.x * v.x;
         float y2 = v.y * v.y;
         float z2 = v.z * v.z;
         Vector3 s;
         s.x = v.x * Mathf.Sqrt(1f - y2 / 2f - z2 / 2f + y2 * z2 / 3f);
         s.y = v.y * Mathf.Sqrt(1f - x2 / 2f - z2 / 2f + x2 * z2 / 3f);
         s.z = v.z * Mathf.Sqrt(1f - x2 / 2f - y2 / 2f + x2 * y2 / 3f);
         return (s);
     }
Comment
Add comment · Show 5
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 JVene · Aug 27, 2018 at 11:43 PM 0
Share

@Benjames I don't yet recognize the formula from code, so can you identify what this grid_norm actually calculates?

avatar image Bunny83 JVene · Aug 28, 2018 at 01:40 AM 0
Share

This method is an alternative cube to sphere mapping with less distortion than just normalizing a point on the cube surface. You can find more about that here.


Though i don't think there's an easy solution for this. Since the mapping for a single coordinate involves all 3 input coordinates you most likely need a case split anyways. Just normalizing the coordinates is much simpler for both way conversion

avatar image Benjames · Aug 28, 2018 at 02:55 AM 0
Share

Yeah it's from the catlike coding tutorial on cube sphere.

I made an array of the vertices of the sphere. Six 2d grids, if that makes sense?

Anyways I'm trying to make it relatively easy to edit the sphere and splats maps in unity editor.

I need or want a way to convert worldspace vector into array slots.

Thanks for the reference bunny83 .

[edit] In hindsight I should't of worded my question like that. I think I am going to find a "close enough" way using angles to find the slots in the array.

So far I can separate the six sides with cases.

Lets say I make the grid's dimensions 90x90 and find the angle between each vertices on one axis. Its almost 1 degree difference each time but not consistently. I bet there is some sort of relationship, like a ratio, between the "angle of vertices" and the "middle of the grid"

avatar image Benjames · Aug 28, 2018 at 06:09 PM 0
Share

So here is a code snippet of what I am settling on, it's not super accurate. "$$anonymous$$ost of the time is close enough"

     public int getSlotfromNorm(Vector3 s, int gridSize, ref int l, ref int j) {
         
         float yangle = $$anonymous$$athf.Atan2(-s.z, -s.x) * $$anonymous$$athf.Rad2Deg;
         float xangle = $$anonymous$$athf.Atan2(-s.z, -s.y) * $$anonymous$$athf.Rad2Deg;
         float zangle = $$anonymous$$athf.Atan2(-s.x, -s.y) * $$anonymous$$athf.Rad2Deg;
         int type = 0;
 
         if (yangle > 45 && yangle < 135 && xangle > 45 && xangle < 135) {//zpos 4
             j = (int)(((yangle - 45f) / 90f) * gridSize);
             l = (int)(((xangle - 45f) / 90f) * gridSize);
             type = 4;
         }
 
         if (yangle > -135 && yangle < -45 && xangle > -135 && xangle < -45) {//zneg 5
             j = (int)((($$anonymous$$athf.Abs(yangle) - 45f) / 90f) * gridSize);
             l = (int)((($$anonymous$$athf.Abs(xangle) - 45f) / 90f) * gridSize);
             type = 5;
         }
 
         if (yangle > -45 && yangle < 45 && zangle > 45 && zangle < 135) { //xpos 0
             j = (int)(((yangle - 45f) / -90f) * gridSize);
             l = (int)(((zangle - 45f) / 90f) * gridSize);
             type = 0;
         }
 
         if ((yangle > 135 || yangle < -135) && zangle > -135 && zangle < -45) { //xneg 1
             j = (int)(((yangle - 135f) / 90f) * gridSize);
             if (yangle < 0) {
                 j = (int)(((yangle +225) / 90f) * gridSize);
             }           
             l = (int)((($$anonymous$$athf.Abs(zangle) - 45f) / 90f) * gridSize);
             type = 1;
         }
 
         if ((zangle > 135 || zangle < -135) && (xangle > 135 || xangle < -135)) { //yneg 3
             l = (int)(((zangle - 135f) / 90f) * gridSize);
             if (zangle < 0) {
                 l = (int)(((zangle + 225f) / 90f) * gridSize);
             }
             j = (int)(((xangle - 135f) / 90f) * gridSize);
             if (xangle < 0) {
                 j = (int)(((xangle + 225f) / 90f) * gridSize);
             }
             type = 3;
         }
 
         if ((zangle > -45 && zangle < 45) && (xangle > -45 && xangle < 45)) { //ypos 2
             l = (int)(((zangle - 45f) / -90f) * gridSize);
             j = (int)(((xangle - 45f) / -90f) * gridSize);
             type = 2;
         }
         return (type);
     }
avatar image Benjames Benjames · Aug 29, 2018 at 04:23 AM 0
Share

Okay that code didn't work as well as I thought it was. So I ended up ditching the "catlike coding" code snippet and went with normalizing the grid coordinates to make my sphere. With the combination of a code snippet from Bunny83's link called DeSpherify and my last code snippet I was able to achieve what i was looking for....I think

0 Replies

· Add your reply
  • Sort: 

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

118 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Move an object to a specific distance 0 Answers

How can I have a distance between 2 points = 0-1 (min = 0, max = 1) and in between is a decimal? [C#] 2 Answers

Normal distribution random 3 Answers

Need help with the physics and angles of gerbils in a plastic ball.. 2 Answers

localPosition vs Position Subtraction as vector direction 1 Answer


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