カメラのポリライン クリッピング アルゴリズムのリファレンス

カメラのポリライン クリッピング アルゴリズムのリファレンス

注意すべき点は 2 つあります。

1. パラメータ Ray と Plane が平行状態にあり、戻り値が false で、パラメータ Enter に値 0 が割り当てられている場合、メソッド Plane.Raycast は交点を生成しません。パラメータ Ray と Plane が平行状態にない場合、Ray が Plane の方向を指していれば、Ray の方向と Plane の法線の点積結果が大きいかどうかに関係なく、交点が存在します。 0の場合はtrueを返し、入力は正の値です。Rayの方向がPlaneを指していない場合、実際にはRayの方向の反対方向がPlaneと交差しますが、このときの戻り値はfalseです。 Enter は負の値です。

2. 2 点が錐台の外側にある場合、これら 2 点と錐台との交点がこれら 2 点の間にない場合、実際にはこれら 2 点と錐台との間に交点は存在しません。

	//折线裁剪
	public static List<Line3D> GetCuttingLines(Line3D line, Camera cam)
	{
		if (line.points == null || line.points.Length < 2) return null;

		List<Line3D> listCuttingLine = new List<Line3D>();

		Plane[] planes = GeometryUtility.CalculateFrustumPlanes(cam);

		List<Vector3> curListPoint = new List<Vector3>();
		bool inPre = true;
		for (int i = 0; i < line.points.Length; i++)
		{
			bool inFrustum = true;
			for (int k = 0; k < 6; k++)
			{
				if (!planes[k].GetSide(line.points[i]))
				{
					inFrustum = false;
					break;
				}
			}

			if (inFrustum)
			{
				if (curListPoint == null)
				{
					curListPoint = new List<Vector3>();
				}

				if (i == 0)
				{
					curListPoint.Add(line.points[i]);
				}
				else
				{
					if (!inPre)
					{
						float enterMin = float.MaxValue;
						Ray ray = new Ray(line.points[i], line.points[i - 1] - line.points[i]);
						foreach (Plane plane in planes)
						{
							if (Vector3.Dot(plane.normal, ray.direction) > 0) continue;
							float enter;
							if (plane.Raycast(ray, out enter))
							{
								if (enterMin > enter) enterMin = enter;
							}
						}
						curListPoint.Add(ray.GetPoint(enterMin));
					}
					curListPoint.Add(line.points[i]);
					if (i >= line.points.Length - 1)
					{
						Line3D lineCutting = new Line3D();
						lineCutting.color = line.color;
						lineCutting.points = curListPoint.ToArray();
						listCuttingLine.Add(lineCutting);
						curListPoint = null;
					}
				}
				//
				inPre = true;
			}
			else
			{
				if (i == 0)
				{
					inPre = false;
				}
				else
				{
					if (inPre)
					{
						if (curListPoint != null)
						{
							float enterMin = float.MaxValue;
							Ray ray = new Ray(line.points[i - 1], line.points[i] - line.points[i - 1]);
							foreach (Plane plane in planes)
							{
								if (Vector3.Dot(plane.normal, ray.direction) > 0) continue;
								float enter;
								if (plane.Raycast(ray, out enter))
								{
									if (enterMin > enter) enterMin = enter;
								}
							}
							curListPoint.Add(ray.GetPoint(enterMin));
							Line3D lineCutting = new Line3D();
							lineCutting.color = line.color;
							lineCutting.points = curListPoint.ToArray();
							listCuttingLine.Add(lineCutting);
							curListPoint = null;
						}
					}
					else
					{
						float enterMin = float.MaxValue;
						Vector3 direction = line.points[i] - line.points[i - 1];
						Ray ray = new Ray(line.points[i - 1], direction);
						Vector3 hitA = Vector3.zero;
						foreach (Plane plane in planes)
						{
							if (Vector3.Dot(plane.normal, ray.direction) > 0) continue;
							float enter;
							if (plane.Raycast(ray, out enter))
							{
								if (enterMin > enter) enterMin = enter;
							}
						}
						if (enterMin * enterMin > direction.sqrMagnitude) continue;

						hitA = ray.GetPoint(enterMin);

						enterMin = float.MaxValue;
						ray = new Ray(line.points[i], -direction);
						Vector3 hitB = Vector3.zero;
						foreach (Plane plane in planes)
						{
							if (Vector3.Dot(plane.normal, ray.direction) > 0) continue;
							float enter;
							if (plane.Raycast(ray, out enter))
							{
								if (enterMin > enter) enterMin = enter;
							}
						}
						if (enterMin * enterMin > direction.sqrMagnitude) continue;

						hitB = ray.GetPoint(enterMin);

						Vector3 hitCenter = (hitA + hitB) * 0.5f;

						bool hitCenterIn = true;
						for (int k = 0; k < 6; k++)
						{
							if (!planes[k].GetSide(hitCenter))
							{
								hitCenterIn = false;
								break;
							}
						}

						if (hitCenterIn)
						{
							Line3D lineCutting = new Line3D();
							lineCutting.color = line.color;
							lineCutting.points = new Vector3[] { hitA, hitB };
							listCuttingLine.Add(lineCutting);
						}
					}
				}
				//
				inPre = false;
			}
		}

		return listCuttingLine;
	}

Line3D クラスを追加します。

using UnityEngine;

public class Line
{
	public Color color = Color.white;
	public bool close = false;
}
public class Line3D : Line
{
	public Vector3[] points = new Vector3[] { };
}

public class Line2D : Line
{
	public Vector2[] points = new Vector2[] { };
	public float width = 2;

	Vector3[] _ps3D;
	public Vector3[] ps3D
	{
		get
		{
			_ps3D = new Vector3[points.Length];
			for (int i = 0; i < points.Length; i++)
			{
				_ps3D[i] = points[i];
			}
			return _ps3D;
		}
	}
}

おすすめ

転載: blog.csdn.net/ttod/article/details/132172988