本人小白,第一次发帖,如果做的不好的地方,请多多指教,多多包涵!
首先这个功能是项目上的,组长让我写的一个小功能,不知道对其他人有没用,我就写下来吧,后续还有一些其他功能,有时间我再写一点。
我毕竟也是小白,可能有些东西比较鸡肋,代码写的也不好。。。不多说了,开始~~~
需求是用GL画线段,然后给出一个垂直距离,判断这个线段垂直距离区域里有多少个坐标点在里面。
我这里用简单的cube代替。
几个关键点我简单说下吧
1.鼠标画两点,屏幕转世界坐标。再根据垂直距离,算出矩形框的4个点。
这个算4个点,肯定有其他好的方法,大家如果有别的思路的话。。。
public Vector3[] CaulFourWorldPo(Vector3 inStart, Vector3 inEnd)
{
Vector3 []FourPointPos = new Vector3[4];
float angle = Vector2.Angle(Vector2.right, Vector3ToVector2(inEnd) - Vector3ToVector2(inStart));
if ((inStart.x > inEnd.x && inStart.z > inEnd.z) || (inStart.x < inEnd.x && inStart.z < inEnd.z))
{
FourPointPos[0] = new Vector3(inStart.x - Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inStart.z + Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
FourPointPos[1] = new Vector3(inStart.x + Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inStart.z - Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
FourPointPos[2] = new Vector3(inEnd.x + Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inEnd.z - Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
FourPointPos[3] = new Vector3(inEnd.x - Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inEnd.z + Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
return FourPointPos;
}
if ((inStart.x > inEnd.x && inStart.z < inEnd.z) || (inStart.x < inEnd.x && inStart.z > inEnd.z))
{
FourPointPos[0] = new Vector3(inStart.x - Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inStart.z - Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
FourPointPos[1] = new Vector3(inStart.x + Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inStart.z + Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
FourPointPos[2] = new Vector3(inEnd.x + Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inEnd.z + Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
FourPointPos[3] = new Vector3(inEnd.x - Mathf.Abs(Mathf.Sin(Mathf.PI * angle / 180f)) * distance, inStart.y, inEnd.z - Mathf.Abs(Mathf.Cos(Mathf.PI * angle / 180f)) * distance);
return FourPointPos;
}
return FourPointPos;
}
2.判断坐标点是否在区域里
首先是判断该点和线段两点的夹角是否小于90,并且到线段的垂直距离,小于最先给出的那个值。
夹角就用 Vector2.Angle判断就好了。
垂直距离用初中公式。
private float PointToStraightlineDistance(Vector3 inStart, Vector3 inEnd, Vector3 targetPoint)
{
Vector2 startVector2 = Vector3ToVector2(inStart);
Vector2 endVector2 = Vector3ToVector2(inEnd);
float A = endVector2.y - startVector2.y;
float B = startVector2.x - endVector2.x;
float C = endVector2.x * startVector2.y - startVector2.x * endVector2.y;
float denominator = Mathf.Sqrt(A * A + B * B);
Vector2 pointVe2 = Vector3ToVector2(targetPoint);
return Mathf.Abs((A * pointVe2.x + B * pointVe2.y + C) / denominator);
}
private Vector2 Vector3ToVector2(Vector3 vector3)
{
return new Vector2(vector3.x, vector3.z);
}
3.把上方的四个点,用于GL画线就好了。然后再判断,画线是用的下方这个。。
void OnPostRender()
if (drawRectangle)
{
GL.PushMatrix()
if (!rectMat)
return;
rectMat.SetPass(0);
GL.LoadPixelMatrix()
GL.Begin(GL.QUADS);
GL.Color(new Color(rectColor.r, rectColor.g, rectColor.b, 0.1f));
GL.Vertex3(targetPointDistancePos[0].x, targetPointDistancePos[0].y, 0);
GL.Vertex3(targetPointDistancePos[1].x, targetPointDistancePos[1].y, 0);
GL.Vertex3(targetPointDistancePos[2].x, targetPointDistancePos[2].y, 0);
GL.Vertex3(targetPointDistancePos[3].x, targetPointDistancePos[3].y, 0);
GL.End();
GL.Begin(GL.LINES);
GL.Color(rectColor)
GL.Vertex3(targetPointDistancePos[0].x, targetPointDistancePos[0].y, 0);
GL.Vertex3(targetPointDistancePos[1].x, targetPointDistancePos[1].y, 0);
GL.Vertex3(targetPointDistancePos[1].x, targetPointDistancePos[1].y, 0);
GL.Vertex3(targetPointDistancePos[2].x, targetPointDistancePos[2].y, 0);
GL.Vertex3(targetPointDistancePos[2].x, targetPointDistancePos[2].y, 0);
GL.Vertex3(targetPointDistancePos[3].x, targetPointDistancePos[3].y, 0);
GL.Vertex3(targetPointDistancePos[3].x, targetPointDistancePos[3].y, 0);
GL.Vertex3(targetPointDistancePos[0].x, targetPointDistancePos[0].y, 0);
GL.End();
GL.PopMatrix()
}
}
大致效果
链接:https://pan.baidu.com/s/1sNCiQC-T-b2DwTmMPxsg5A
提取码:d0wa
复制这段内容后打开百度网盘手机App,操作更方便哦