unity3d GL绘制一个三角形

绘制三角形,保证三个点要能构成三角形。

示例代码如下:

  1. public Material mat;
  2.     void OnPostRender ()
  3.     {
  4.         DrawTriangle (30, 0, 100, 250, 200, 100, mat);//三角形的三个定点坐标
  5.     }
  6.     void DrawTriangle (float x1, float y1, float x2, float y2, float x3, float y3, Material mat)
  7.     {
  8.         GL.PushMatrix ();
  9.         mat.SetPass (0);
  10.         GL.LoadOrtho ();//把绘制对象显示在平面上
  11.         GL.Begin (GL.TRIANGLES);//绘制三角形
  12.         //GL.Begin (GL.LINES);//绘制线
  13.         //三个点的顺序是顺时针方向
  14.         GL.Vertex3 (x1 / Screen.width, y1 / Screen.height, 0);
  15.         GL.Vertex3 (x2 / Screen.width, y2 / Screen.height, 0);
  16.         GL.Vertex3 (x3 / Screen.width, y3 / Screen.height, 0);
  17.         GL.End ();
  18.         GL.PopMatrix ();
  19.     }

这个脚本要挂载在摄像机上。 

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/82191062