Unity3d GL繪製一個跟隨滑鼠的曲線
阿新 • • 發佈:2018-12-26
- public Material material;//材質,必須要有
- private List<Vector3> lineInfo;//滑鼠座標的集合
- void Start ()
- {
- lineInfo = new List<Vector3> ();//初始化集合
- }
- // Update is called once per frame
- void Update ()
- {
- // if (Input.GetMouseButton (0)) {
- //
- // lineInfo.Add (Input.mousePosition);
- // }
- lineInfo.Add (Input.mousePosition);//將滑鼠座標加入到集合當中
- }
- void OnPostRender ()
- {
- // if (Input.GetMouseButton (0)) {
- if (!material) {
- Debug.LogError ("請給材質賦值");
- return;
- }
- GL.PushMatrix ();
- material.SetPass (0);
- //material.color = Color.white;
- GL.LoadOrtho ();//繪製物件顯示在平面上
- GL.Begin (GL.LINES);//開始劃線
- GL.Color (Color.red);//線的顏色,我這邊顏色是不會改變的,還沒找出問題,希望有人能幫我搞定這個顏色不變得問題。
- int size = lineInfo.Count;
- for (int i = 0; i < size - 1; i++) {
- Vector3 start = lineInfo [i];
- Vector3 end = lineInfo [i + 1];
- DrawLine (start.x, start.y, end.x, end.y);
- }
- GL.End ();
- GL.PopMatrix ();
- // }
- }
- void DrawLine (float x1, float y1, float x2, float y2)
- {
- GL.Vertex (new Vector3 (x1 / Screen.width, y1 / Screen.height, 0));
- GL.Vertex (new Vector3 (x2 / Screen.width, y2 / Screen.height, 0));
- }