public static IntersectRaySphere ( |
||
ray | ||
sphereOrigin | Vector3 | |
sphereRadius | float | |
t | float | |
q | Vector3 | |
Результат | bool |
public static bool IntersectRaySphere(Ray ray, Vector3 sphereOrigin, float sphereRadius, ref float t, ref Vector3 q)
{
Vector3 lhs = ray.origin - sphereOrigin;
float num = Vector3.Dot(lhs, ray.direction);
float num2 = Vector3.Dot(lhs, lhs) - (sphereRadius * sphereRadius);
if ((num2 > 0f) && (num > 0f))
{
return false;
}
float f = (num * num) - num2;
if (f < 0f)
{
return false;
}
t = -num - Mathf.Sqrt(f);
if (t < 0f)
{
t = 0f;
}
q = ray.origin + (t * ray.direction);
return true;
}
public static int FindNearest(Vector2 point, Transform cloudTransform, IEditablePoint points) { Ray ray = HandleUtility.GUIPointToWorldRay(point); Dictionary <int, float> dictionary = new Dictionary <int, float>(); for (int i = 0; i < points.Count; i++) { float num = 0f; Vector3 zero = Vector3.zero; if (MathUtils.IntersectRaySphere(ray, cloudTransform.TransformPoint(points.GetPosition(i)), points.GetPointScale() * 0.5f, ref num, ref zero)) { if (num > 0f) { dictionary.Add(i, num); } } } int result; if (dictionary.Count <= 0) { result = -1; } else { IOrderedEnumerable <KeyValuePair <int, float> > source = from x in dictionary orderby x.Value select x; result = source.First <KeyValuePair <int, float> >().Key; } return(result); }