- Home /

# 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);
}
```

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

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

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"

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

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

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

How to get vector X distance from point on Y angle? 4 Answers

Basic vector math addition 1 Answer

Getting an inversely proportionate value 1 Answer

Get angle relative to GameObject 1 Answer