unity3d GL繪製一個三角形
阿新 • • 發佈:2018-12-26
繪製三角形,保證三個點要能構成三角形。
示例程式碼如下:
- public Material mat;
- void OnPostRender ()
- {
- DrawTriangle (30, 0, 100, 250, 200, 100, mat);//三角形的三個定點座標
- }
- void DrawTriangle (float x1, float y1, float x2, float y2, float x3, float y3, Material mat)
- {
- GL.PushMatrix ();
- mat.SetPass (0);
- GL.LoadOrtho ();//把繪製物件顯示在平面上
- GL.Begin (GL.TRIANGLES);//繪製三角形
- //GL.Begin (GL.LINES);//繪製線
- //三個點的順序是順時針方向
- GL.Vertex3 (x1 / Screen.width, y1 / Screen.height, 0);
- GL.Vertex3 (x2 / Screen.width, y2 / Screen.height, 0);
- GL.Vertex3 (x3 / Screen.width, y3 / Screen.height, 0);
- GL.End ();
- GL.PopMatrix ();
- }
這個指令碼要掛載在攝像機上。