摄像机折线裁剪算法参考

摄像机折线裁剪算法参考

需要说明的是两点:

一、Plane.Raycast这个方法在参数Ray与Plane是平行状态的时候不会产生交点,返回值为false,同时参数enter赋值为0。如果参数Ray与Plane不是平行状态,那么就一定会产生交点,在这种情况下,如果Ray指向了Plane所处的方向,不管是Ray的direction与Plane的normal的点乘结果是否大于0,都会返回true,同时enter为正值,如果Ray的方向没有指向Plane,实际上Ray的direction的反方向也会与Plane产生交点,但是此时返回值为false,enter为负值。

二、当两个点都在Frustum外面的时候,如果基于这两个点的射线与Frustum之间没有焦点或者有焦点,但是交点位置不在这两个点之间,那么实际上这两个点之间的连线与Frustum就没有交点。

	//折线裁剪
	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.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.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.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;
		}
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值