Unity3d GL绘制一个跟随鼠标的曲线

把鼠标的移动轨迹以曲线的方式显示出来。

示例代码如下:

  1.     public Material material;//材质,必须要有
  2.     private List<Vector3> lineInfo;//鼠标坐标的集合
  3.     void Start ()
  4.     {
  5.         lineInfo = new List<Vector3> ();//初始化集合
  6.     }
  7.     
  8.     // Update is called once per frame
  9.     void Update ()
  10.     {
  11. //        if (Input.GetMouseButton (0)) {
  12. //            
  13. //            lineInfo.Add (Input.mousePosition);
  14. //        }
  15.         lineInfo.Add (Input.mousePosition);//将鼠标坐标加入到集合当中
  16.     }
  17.     void OnPostRender ()
  18.     {
  19. //        if (Input.GetMouseButton (0)) {
  20.             
  21.         if (!material) {
  22.             Debug.LogError ("请给材质赋值");
  23.             return;
  24.         }
  25.             
  26.         GL.PushMatrix ();
  27.         material.SetPass (0);
  28.         //material.color = Color.white;
  29.         GL.LoadOrtho ();//绘制对象显示在平面上
  30.         GL.Begin (GL.LINES);//开始划线
  31.         GL.Color (Color.red);//线的颜色,我这边颜色是不会改变的,还没找出问题,希望有人能帮我搞定这个颜色不变得问题。
  32.         int size = lineInfo.Count;
  33.         for (int i = 0; i < size - 1; i++) {
  34.             Vector3 start = lineInfo [i];
  35.             Vector3 end = lineInfo [i + 1];
  36.             DrawLine (start.x, start.y, end.x, end.y);
  37.         }
  38.             
  39.         GL.End ();
  40.         GL.PopMatrix ();
  41. //        }    
  42.     }
  43.     void DrawLine (float x1, float y1, float x2, float y2)
  44.     {
  45.         GL.Vertex (new Vector3 (x1 / Screen.width, y1 / Screen.height, 0));
  46.         GL.Vertex (new Vector3 (x2 / Screen.width, y2 / Screen.height, 0));
  47.     }

上面颜色不会改变,希望看到的小伙伴能帮忙搞定。 这个脚本要挂载在摄像机上。

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/82190993