一种3D空间的柱状多边形检测实现-LMLPHP

最近无意中拓展出这个东西,基于之前写的2D多边形检测:

http://www.cnblogs.com/hont/p/6105997.html

而判断两条线相交的方法替换成了我后来写的差乘判断:

http://www.cnblogs.com/hont/p/6106043.html

应用还是比较广泛的,特别是多边形选区和地形处理上

代码实现:

using UnityEngine;

public class Test : MonoBehaviour
{
public Transform testComparePoint;
public Transform[] pointsArray;
public float height = ; public bool IsInRange(Vector3 comparePoint)
{
var localComparePoint = transform.worldToLocalMatrix.MultiplyPoint3x4(comparePoint); var flag = true;
flag &= localComparePoint.y <= height;
flag &= localComparePoint.y >= -height;
flag &= IsConcaveContain2D(pointsArray, localComparePoint); if (flag)
return true; return false;
} public bool IsConcaveContain2D(Transform[] points, Vector3 compare)
{
const float VIRTUAL_RAYCAST_LEN = ; var comparePoint = (points[].localPosition + points[].localPosition) * 0.5f;
var originPoint = compare;
comparePoint += (comparePoint - originPoint).normalized * VIRTUAL_RAYCAST_LEN; int count = ;
for (int i = ; i < points.Length; i++)
{
var a = points[i % points.Length];
var b = points[(i + ) % points.Length]; var r = IsLineSegmentIntersection(a.localPosition, b.localPosition, originPoint, comparePoint); if (r) count++;
} return count % == ;
} public bool IsLineSegmentIntersection(Vector3 a, Vector3 b, Vector3 c, Vector3 d)
{
var crossA = Mathf.Sign(Vector3.Cross(d - c, a - c).y);
var crossB = Mathf.Sign(Vector3.Cross(d - c, b - c).y); if (Mathf.Approximately(crossA, crossB)) return false; var crossC = Mathf.Sign(Vector3.Cross(b - a, c - a).y);
var crossD = Mathf.Sign(Vector3.Cross(b - a, d - a).y); if (Mathf.Approximately(crossC, crossD)) return false; return true;
} void OnDrawGizmos()
{
if (pointsArray == null) return; if (testComparePoint != null)
{
var comparePoint = testComparePoint.transform.position;
if (IsInRange(comparePoint))
{
Gizmos.color = Color.red;
}
} var cacheMatrix = Gizmos.matrix;
Gizmos.matrix = transform.localToWorldMatrix; for (int i = ; i < pointsArray.Length; i++)
{
var a = pointsArray[i];
var b = pointsArray[(i + ) % pointsArray.Length]; if (a == null) continue;
if (b == null) continue; var minA = a.localPosition;
var minB = b.localPosition; var maxA = a.localPosition;
var maxB = b.localPosition; minA.y = -height;
minB.y = -height; maxA.y = height;
maxB.y = height; Gizmos.DrawLine(minA, minB);
Gizmos.DrawLine(maxA, maxB); Gizmos.DrawLine(minA, maxA);
Gizmos.DrawLine(minB, maxB);
} Gizmos.color = Color.clear; Gizmos.matrix = cacheMatrix;
}
}
05-27 12:14