2using System.Collections;
3using System.Collections.Generic;
22 public static bool ClosestPointsOnTwoLines(Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2, out Vector3 closestPointLine1, out Vector3 closestPointLine2)
24 closestPointLine1 = Vector3.zero;
25 closestPointLine2 = Vector3.zero;
27 float a = Vector3.Dot(lineVec1, lineVec1);
28 float b = Vector3.Dot(lineVec1, lineVec2);
29 float e = Vector3.Dot(lineVec2, lineVec2);
36 Vector3 r = linePoint1 - linePoint2;
37 float c = Vector3.Dot(lineVec1, r);
38 float f = Vector3.Dot(lineVec2, r);
40 float s = (b*f - c*e) / d;
41 float t = (a*f - c*b) / d;
43 closestPointLine1 = linePoint1 + lineVec1 * s;
44 closestPointLine2 = linePoint2 + lineVec2 * t;
58 public static bool PointOnLine(Ray InLineA, Ray InLineB, out Vector3 OutPointA, out Vector3 OutPointB)
60 return ClosestPointsOnTwoLines(InLineA.origin, InLineA.direction, InLineB.origin, InLineB.direction, out OutPointA, out OutPointB);
66 public static bool PointOnPlane(Ray ray, Vector3 planePosition, Vector3 planeNormal, out Vector3 hit)
74 public static bool PointOnPlane(Ray ray, Plane plane, out Vector3 hit)
78 if(plane.Raycast(ray, out distance))
80 hit = ray.GetPoint(distance);
90 private static Vector3 Mask(Vector3 vec)
92 return new Vector3( vec.x > 0f ? 1f : -1f,
93 vec.y > 0f ? 1f : -1f,
94 vec.z > 0f ? 1f : -1f);
97 private static float Mask(
float val)
99 return val > 0f ? 1f : -1f;
104 Vector3 viewDir = -Mask(
new Vector3(Vector3.Dot(rayDirection, target.right),
105 Vector3.Dot(rayDirection, target.up),
106 Vector3.Dot(rayDirection, target.forward)));
115 if( Mathf.Abs(mouseDelta.magnitude) < .0001f)
118 Vector2 or = cam.WorldToScreenPoint(origin);
119 Vector2 ud = cam.WorldToScreenPoint(origin + upDir);
120 Vector2 rd = cam.WorldToScreenPoint(origin + rightDir);
122 float mouseDotUp = Vector2.Dot(mouseDelta, ud - or);
123 float mouseDotRight = Vector2.Dot(mouseDelta, rd - or);
125 if( Mathf.Abs(mouseDotUp) > Mathf.Abs(mouseDotRight))
126 return Mathf.Sign(mouseDotUp);
128 return Mathf.Sign(mouseDotRight);
138 float delta = Vector2.Distance(lhs, rhs);
139 float scale = 1f / Mathf.Min(Screen.width, Screen.height);
142 if( Mathf.Abs(lhs.x - rhs.x) > Mathf.Abs(lhs.y - rhs.y) )
143 return delta * scale * ( (lhs.x-rhs.x) > 0f ? 1f : -1f );
145 return delta * scale * ( (lhs.y-rhs.y) > 0f ? 1f : -1f );
153 Camera cam = Camera.main;
155 Transform t = cam.transform;
156 float z = Vector3.Dot(position-t.position, cam.transform.forward);
157 Vector3 lhs = cam.WorldToScreenPoint(t.position + (t.forward * z));
158 Vector3 rhs = cam.WorldToScreenPoint(t.position + (t.right + t.forward * z));
159 return 1f/(lhs-rhs).magnitude;
167 Matrix4x4 m = transform.worldToLocalMatrix;
168 Ray local =
new Ray(m.MultiplyPoint(ray.origin), m.MultiplyVector(ray.direction));
176 public static GameObject
ObjectRaycast(Ray ray, IEnumerable<GameObject> objects,
bool ignoreSelection =
false)
179 float distance = Mathf.Infinity;
180 float best = Mathf.Infinity;
181 GameObject obj =
null;
182 Bounds bounds =
new Bounds(Vector3.zero, Vector3.one * 0.02f);
186 foreach(GameObject go
in objects)
192 renderer = go.GetComponent<Renderer>();
194 if( renderer !=
null )
196 Debug.DrawRay(ray.origin, ray.direction,
Color.magenta, 5);
197 if( renderer.bounds.IntersectRay(ray, out distance) )
199 MeshFilter mf = go.GetComponent<MeshFilter>();
201 if( mf !=
null && mf.sharedMesh !=
null &&
MeshRaycast(mf.sharedMesh, localRay, out hit))
203 if(hit.distance < best)
213 bounds.center = go.transform.position;
215 if(bounds.IntersectRay(ray, out distance))
217 if( distance < best )
232 public static GameObject
ObjectRaycast(Ray ray, IEnumerable<GameObject> objects,
float maxdist,
bool ignoreMeshFilter =
false)
235 float distance = Mathf.Infinity;
236 float best = maxdist;
237 GameObject obj =
null;
238 Bounds bounds =
new Bounds(Vector3.zero, Vector3.one * 0.02f);
242 foreach (GameObject go
in objects)
246 renderer = go.GetComponent<Renderer>();
248 if (renderer !=
null)
251 Debug.DrawLine(ray.origin, ray.GetPoint(maxdist),
Color.magenta, 5);
252 if (renderer.bounds.IntersectRay(ray, out distance))
254 if (ignoreMeshFilter)
264 MeshFilter mf = go.GetComponent<MeshFilter>();
266 if (mf !=
null && mf.sharedMesh !=
null &&
MeshRaycast(mf.sharedMesh, localRay, out hit))
268 if (hit.distance < best)
279 bounds.center = go.transform.position;
281 if (bounds.IntersectRay(ray, out distance))
294 public static GameObject
ObjectRaycast(Ray ray, IEnumerable<GameObject> objects,
float maxdist,
bool ignoreMeshFilter =
false, List<GameObject> ignoreList =
null)
297 float distance = Mathf.Infinity;
298 float best = maxdist;
299 GameObject obj =
null;
300 Bounds bounds =
new Bounds(Vector3.zero, Vector3.one * 0.02f);
304 foreach (GameObject go
in objects)
306 if (ignoreList !=
null)
307 if (ignoreList.Contains(go))
continue;
311 renderer = go.GetComponent<Renderer>();
313 if (renderer !=
null)
316 Debug.DrawLine(ray.origin, ray.GetPoint(maxdist),
Color.magenta, 5);
317 if (renderer.bounds.IntersectRay(ray, out distance))
319 if (ignoreMeshFilter)
329 MeshFilter mf = go.GetComponent<MeshFilter>();
331 if (mf !=
null && mf.sharedMesh !=
null &&
MeshRaycast(mf.sharedMesh, localRay, out hit))
333 if (hit.distance < best)
344 bounds.center = go.transform.position;
346 if (bounds.IntersectRay(ray, out distance))
366 float distance = Mathf.Infinity;
367 float best = Mathf.Infinity;
368 GameObject obj =
null;
369 Bounds bounds =
new Bounds(Vector3.zero, Vector3.one * 0.02f);
374 foreach(GameObject go
in objects)
378 renderer = go.GetComponent<Renderer>();
380 if( renderer !=
null )
382 Debug.DrawRay(ray.origin, ray.direction,
Color.magenta, 5);
383 if( renderer.bounds.IntersectRay(ray, out distance) )
385 MeshFilter mf = go.GetComponent<MeshFilter>();
387 if( mf !=
null && mf.sharedMesh !=
null &&
MeshRaycast(mf.sharedMesh, localRay, out hit))
389 if (hit.distance < best)
400 bounds.center = go.transform.position;
402 if(bounds.IntersectRay(ray, out distance))
404 if( distance < best )
418 Vector3[] vertices = mesh.vertices;
419 int[] triangles = mesh.triangles;
421 float dist = Mathf.Infinity;
422 Vector3 point = Vector3.zero;
425 for(
int i = 0; i < triangles.Length; i += 3)
427 a = vertices[triangles[i+0]];
428 b = vertices[triangles[i+1]];
429 c = vertices[triangles[i+2]];
431 if(pb_Geometry.RayIntersectsTriangle(ray, a, b, c,
Culling.Front, out dist, out point))
435 hit.distance = Vector3.Distance(hit.point, ray.origin);
436 hit.normal = Vector3.Cross(b-a, c-a);
437 hit.triangle =
new int[] { triangles[i], triangles[i+1], triangles[i+2] };
449 public static float DistancePoint2DToLine(Camera cam, Vector2 mousePosition, Vector3 worldPosition1, Vector3 worldPosition2)
451 Vector2 v0 = cam.WorldToScreenPoint(worldPosition1);
452 Vector2 v1 = cam.WorldToScreenPoint(worldPosition2);
468 float l2 = ((v.x - w.x)*(v.x - w.x)) + ((v.y - w.y)*(v.y - w.y));
470 if (l2 == 0.0f)
return Vector2.Distance(p, v);
475 float t = Vector2.Dot(p - v, w - v) / l2;
478 return Vector2.Distance(p, v);
480 return Vector2.Distance(p, w);
482 Vector2 projection = v + t * (w - v);
484 return Vector2.Distance(p, projection);
495 float xmin = Mathf.Infinity, xmax = -Mathf.Infinity, ymin = Mathf.Infinity, ymax = -Mathf.Infinity;
497 for(
int i = 0; i < polygon.Length; i++)
499 if(polygon[i].x < xmin)
501 else if(polygon[i].x > xmax)
504 if(polygon[i].y < ymin)
506 else if(polygon[i].y > ymax)
510 if(point.x < xmin || point.x > xmax || point.y < ymin || point.y > ymax)
513 Vector2 rayStart =
new Vector2(xmin - 1f, ymax + 1f);
517 for(
int i = 0; i < polygon.Length; i += 2)
523 return collisions % 2 != 0;
532 s1.x = p1.x - p0.x; s1.y = p1.y - p0.y;
533 s2.x = p3.x - p2.x; s2.y = p3.y - p2.y;
536 s = (-s1.y * (p0.x - p2.x) + s1.x * (p0.y - p2.y)) / (-s2.x * s1.y + s1.x * s2.y);
537 t = ( s2.x * (p0.y - p2.y) - s2.y * (p0.x - p2.x)) / (-s2.x * s1.y + s1.x * s2.y);
539 return (s >= 0 && s <= 1 && t >= 0 && t <= 1);
static float CalcMouseDeltaSignWithAxes(Camera cam, Vector3 origin, Vector3 upDir, Vector3 rightDir, Vector2 mouseDelta)
static GameObject ObjectRaycast(Ray ray, IEnumerable< GameObject > objects, float maxdist, bool ignoreMeshFilter=false)
static float GetHandleSize(Vector3 position)
static float CalcSignedMouseDelta(Vector2 lhs, Vector2 rhs)
static bool PointOnPlane(Ray ray, Plane plane, out Vector3 hit)
static float DistancePointLineSegment(Vector2 p, Vector2 v, Vector2 w)
static bool GetLineSegmentIntersect(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3)
static GameObject ObjectRaycast(Ray ray, IEnumerable< GameObject > objects, float maxdist, bool ignoreMeshFilter=false, List< GameObject > ignoreList=null)
static bool PointOnLine(Ray InLineA, Ray InLineB, out Vector3 OutPointA, out Vector3 OutPointB)
static Ray TransformRay(Ray ray, Transform transform)
static GameObject ObjectRaycast(Ray ray, IEnumerable< GameObject > objects, out pb_RaycastHit besthit)
static bool PointInPolygon(Vector2[] polygon, Vector2 point)
static bool ClosestPointsOnTwoLines(Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2, out Vector3 closestPointLine1, out Vector3 closestPointLine2)
static Vector3 DirectionMask(Transform target, Vector3 rayDirection)
static bool MeshRaycast(Mesh mesh, Ray ray, out pb_RaycastHit hit)
static float DistancePoint2DToLine(Camera cam, Vector2 mousePosition, Vector3 worldPosition1, Vector3 worldPosition2)
static bool PointOnPlane(Ray ray, Vector3 planePosition, Vector3 planeNormal, out Vector3 hit)
static GameObject ObjectRaycast(Ray ray, IEnumerable< GameObject > objects, bool ignoreSelection=false)
static GameObject activeGameObject