Skip to content

Instantly share code, notes, and snippets.

@hakanai
Last active June 17, 2018 12:54
Show Gist options
  • Save hakanai/044474f94590bd8842eff9c3823e6f18 to your computer and use it in GitHub Desktop.
Save hakanai/044474f94590bd8842eff9c3823e6f18 to your computer and use it in GitHub Desktop.
AABB-Ray intersect returning which side was hit (wrong logic)
uint faceIndexFromDirection(float3 worldViewPos, float3 worldViewDir)
{
//todo carry these from the source
float3 objectViewPos = mul(unity_WorldToObject, float4(worldViewPos, 1)).xyz;
float3 objectViewDir = mul(unity_WorldToObject, float4(worldViewDir, 0)).xyz;
fixed3 directionSign = sign(objectViewDir);
float3 directionInverse = 1.0 / objectViewDir;
float3 xHit = (-directionSign.x - objectViewPos.x) * directionInverse.x * objectViewDir;
float3 yHit = (-directionSign.y - objectViewPos.y) * directionInverse.y * objectViewDir;
float3 zHit = (-directionSign.z - objectViewPos.z) * directionInverse.z * objectViewDir;
return (abs(xHit.y) <= 1.0 && abs(xHit.z) <= 1.0) ? (directionSign.x > 0 ? 1 : 6)
: (abs(yHit.z) <= 1.0 && abs(yHit.x) <= 1.0) ? (directionSign.y > 0 ? 2 : 5)
: (abs(zHit.x) <= 1.0 && abs(zHit.y) <= 1.0) ? (directionSign.z > 0 ? 3 : 4)
: -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment