1. 程式人生 > >unity3d GL繪製一個三角形

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.     }

這個指令碼要掛載在攝像機上。