unityd GL用線段連城一個空心三角形
阿新 • • 發佈:2018-12-26
繪製三角形,要保證三個點可以構成三角形,本方法是繪製了三條相連的線段。
示例程式碼如下:
- public Material material;
- void OnPostRender ()
- {
- if (!material) {
- Debug.LogError ("請給材質賦值");
- return;
- }
- material.SetPass (0);
- GL.LoadOrtho ();//把繪製的物件顯示在平面上
- GL.Begin (GL.LINES);//劃線
- //三個頂點要順時針排列
- DrawLines (30, 0, 100, 250);
- DrawLines (100, 250, 200, 100);
- DrawLines (200, 100, 30, 0);
- GL.End ();
- }
- void DrawLines (float x1, float y1, float x2, float y2)
- {
- //注意GL.Vertex 與GL.Vertex3的區別
- GL.Vertex (new Vector3 (x1 / Screen.width, y1 / Screen.height, 0));
- GL.Vertex (new Vector3 (x2 / Screen.width, y2 / Screen.height, 0));
- }
這個指令碼要掛載在攝像機上。