1. 程式人生 > >unityd GL用線段連城一個空心三角形

unityd GL用線段連城一個空心三角形

繪製三角形,要保證三個點可以構成三角形,本方法是繪製了三條相連的線段。

示例程式碼如下:

  1.     public Material material;
  2.     void OnPostRender ()
  3.     {
  4.         if (!material) {
  5.             Debug.LogError ("請給材質賦值");
  6.             return;
  7.         }
  8.         material.SetPass (0);
  9.         GL.LoadOrtho ();//把繪製的物件顯示在平面上
  10.         GL.Begin (GL.LINES);//劃線
  11.        //三個頂點要順時針排列
  12.         DrawLines (30, 0, 100, 250);
  13.         DrawLines (100, 250, 200, 100);
  14.         DrawLines (200, 100, 30, 0);
  15.         GL.End ();
  16.     }
  17.     void DrawLines (float x1, float y1, float x2, float y2)
  18.     {
  19.        //注意GL.Vertex 與GL.Vertex3的區別
  20.         GL.Vertex (new Vector3 (x1 / Screen.width, y1 / Screen.height, 0));
  21.         GL.Vertex (new Vector3 (x2 / Screen.width, y2 / Screen.height, 0));
  22.     }

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