Last active
October 29, 2022 21:14
-
-
Save JuanDiegoMontoya/d8788148dcb9780848ce8bf50f89b7bb to your computer and use it in GitHub Desktop.
Functions for getting the index and UV for a cube face struck by a direction vector originating inside the cube (i.e., cubemap coord to texture index and UV).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int GetCubeFaceIndex(vec3 dir) | |
{ | |
float x = abs(dir.x); | |
float y = abs(dir.y); | |
float z = abs(dir.z); | |
if (x > y && x > z) | |
return 0 + (dir.x > 0 ? 0 : 1); | |
else if (y > z) | |
return 2 + (dir.y > 0 ? 0 : 1); | |
return 4 + (dir.z > 0 ? 0 : 1); | |
} | |
vec2 GetCubeUVFromDir(int faceIndex, vec3 dir) | |
{ | |
vec2 uv; | |
switch (faceIndex) | |
{ | |
case 0: uv = vec2(-dir.z, dir.y); break; // +X | |
case 1: uv = vec2(dir.z, dir.y); break; // -X | |
case 2: uv = vec2(dir.x, -dir.z); break; // +Y | |
case 3: uv = vec2(dir.x, dir.z); break; // -Y | |
case 4: uv = vec2(dir.x, dir.y); break; // +Z | |
default: uv = vec2(-dir.x, dir.y); break;// -Z | |
} | |
return uv * .5 + .5; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment