Unity下GL没有画圆的函数,只能自己来了。

如果能帮到大家,我也很高兴。

虽然没有画圆的函数,但是能画直线,利用这一点,配合微积分什么的,就可以画出来了。反正就是花很多连在一起的直线,每条直线足够短的时候,就足够圆了

 void DrawCircle(float x, float y, float z, float r, float accuracy)
{
GL.PushMatrix ();
//绘制2D图像
GL.LoadOrtho (); float stride = r * accuracy;
float size = / accuracy;
float x1 = x, x2 = x, y1 = , y2 = ;
float x3 = x, x4 = x, y3 = , y4 = ; double squareDe;
squareDe = r * r - Math.Pow (x - x1, );
squareDe = squareDe > ? squareDe : ;
y1 = (float)(y + Math.Sqrt (squareDe));
squareDe = r * r - Math.Pow (x - x1, );
squareDe = squareDe > ? squareDe : ;
y2 = (float)(y - Math.Sqrt (squareDe));
for (int i = ; i < size; i++) {
x3 = x1 + stride;
x4 = x2 - stride;
squareDe = r * r - Math.Pow (x - x3, );
squareDe = squareDe > ? squareDe : ;
y3 = (float)(y + Math.Sqrt (squareDe));
squareDe = r * r - Math.Pow (x - x4, );
squareDe = squareDe > ? squareDe : ;
y4 = (float)(y - Math.Sqrt (squareDe)); //绘制线段
GL.Begin (GL.LINES);
GL.Color (Color.blue);
GL.Vertex (new Vector3 (x1 / Screen.width, y1 / Screen.height, z));
GL.Vertex (new Vector3 (x3 / Screen.width, y3 / Screen.height, z));
GL.End ();
GL.Begin (GL.LINES);
GL.Color (Color.blue);
GL.Vertex (new Vector3 (x2 / Screen.width, y1 / Screen.height, z));
GL.Vertex (new Vector3 (x4 / Screen.width, y3 / Screen.height, z));
GL.End ();
GL.Begin (GL.LINES);
GL.Color (Color.blue);
GL.Vertex (new Vector3 (x1 / Screen.width, y2 / Screen.height, z));
GL.Vertex (new Vector3 (x3 / Screen.width, y4 / Screen.height, z));
GL.End ();
GL.Begin (GL.LINES);
GL.Color (Color.blue);
GL.Vertex (new Vector3 (x2 / Screen.width, y2 / Screen.height, z));
GL.Vertex (new Vector3 (x4 / Screen.width, y4 / Screen.height, z));
GL.End (); x1 = x3;
x2 = x4;
y1 = y3;
y2 = y4;
}
GL.PopMatrix ();
}

参数分别为: x,y,z 中心点三维坐标, r 圆的半径, accuracy 精度,精度越小,越圆

如有错误,请不吝指教

05-28 10:26