Created
July 1, 2016 08:27
-
-
Save benloong/1536d9b03b2e31a7d71225cae77c64c5 to your computer and use it in GitHub Desktop.
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
public bool Raycast(Ray r, out float distance) | |
{ | |
Vector3 min = this.min; | |
Vector3 max = this.max; | |
float tmin = float.NegativeInfinity, tmax = float.PositiveInfinity; | |
Vector3 dir_inv = new Vector3(); | |
dir_inv.x = r.direction.x == 0 ? float.PositiveInfinity : 1 / r.direction.x; | |
dir_inv.y = r.direction.y == 0 ? float.PositiveInfinity : 1 / r.direction.y; | |
dir_inv.z = r.direction.z == 0 ? float.PositiveInfinity : 1 / r.direction.z; | |
distance = 0; | |
for (int i = 0; i < 3; ++i) | |
{ | |
float t1 = (min[i] - r.origin[i]) * dir_inv[i]; | |
float t2 = (max[i] - r.origin[i]) * dir_inv[i]; | |
tmin = Mathf.Max(tmin, Mathf.Min(t1, t2)); | |
tmax = Mathf.Min(tmax, Mathf.Max(t1, t2)); | |
distance = tmin * tmin; | |
} | |
distance = Mathf.Sqrt(distance); | |
return tmax > Mathf.Max(tmin, 0.0f); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment