【原】unity3D调用GL任意画线

 

 

白色上面为什么画不上?
此文是对某位大神文章的翻译,忘记从哪摘抄的了 原文使用js写的我把它改成C#。此文存在一个问题,当屏幕上有texture的话,貌似texture上画不上。期待高手一块解决。另外当快速拖动鼠标时,发现画的曲线不够平滑,正在尝试用贝塞尔解决,期待高手一块探讨。GLDraw
快速画线,不光滑问题搞定。

 贝塞尔曲线:

  1 using UnityEngine;
  2 using System.Collections;
  3 
  4 
  5 public class joint{
  6   public Vector3 org;
  7   public Vector3 end;
  8 }
  9 
 10 public class TestGL : MonoBehaviour {
 11     
 12      Event e  ;  
 13     private Vector3 orgPos;  
 14     private Vector3 endPos;  
 15     bool canDrawLines;
 16     ArrayList posAL;
 17     public    Material lineMaterial;
 18     // Use this for initialization
 19     void Start () 
 20     {
 21         posAL=new  ArrayList();
 22     }
 23     
 24     void Update() 
 25     {
 26         if(Input.GetMouseButton(0))
 27         {
 28              canDrawLines = true;  
 29         }
 30         
 31         
 32         if(e.type!=null &canDrawLines)  
 33         {  
 34             if(e.type == EventType.MouseDown)  
 35             {  
 36                 orgPos=Input.mousePosition;  
 37                 endPos=Input.mousePosition;  
 38                   
 39             }  
 40             if(e.type==EventType.MouseDrag)  
 41             {  
 42                 endPos=Input.mousePosition;  
 43                      joint tempjoint = new joint();  
 44                      tempjoint.org=orgPos;  
 45                      tempjoint.end=endPos;  
 46                      posAL.Add(tempjoint);
 47                 orgPos=Input.mousePosition;  
 48             }  
 49             if(e.type==EventType.MouseUp)  
 50             {  
 51         
 52                 endPos=Input.mousePosition;  
 53             }  
 54         }  
 55     
 56     }
 57     
 58     /// <summary>
 59     /// GLs the draw line1.
 60     /// 
 61     ///       * 
 62     ///      * *
 63     ///     *   *
 64     ///    *  *  *   //红点为begin  灰点为end1   黑点为中点
 65     ///   *       *
 66     ///  *         *
 67     /// *           *
 68     ///

 69     /// </summary>
 70     void GLDrawLine1( )  
 71     {  
 72         //DrawBezier(new Vector2(100,100),new Vector2(300,500),new Vector2(800,100),new Vector2(600,700),Color.red,1);
 73         for(int i=0;i<posAL.Count-1;i+=2)  
 74         {  
 75                 joint temp=(joint)posAL[i];//左边*线的对象 包含起点、终点
 76                 joint temp1=(joint)posAL[i+1];//右边*线的对象 包含起点、终点
 77                 Vector3 begin=(temp.org+temp.end)/2f;//左边*线的中点坐标
 78                 Vector3 end1=(temp1.org+temp1.end)/2f;//右边*线的中点坐标
 79                 Vector3 x1=(begin+end1)/2f;//计算中点坐标
 80                 Vector3 offSet=temp.end-x1;//顶点和中点的差值
 81                 Vector3 startTangent=new Vector3(begin.x+offSet.x,begin.y+offSet.y,0);//左边控制点
 82                 Vector3 endTangent=new Vector3(end1.x+offSet.x,end1.y+offSet.y,0);//右边的控制点 
 83                 DrawBezier(temp.org,startTangent,temp1.end,endTangent,Color.red,1);    
 84         }  
 85     }  
 90     void DrawLine(Vector2 beg ,Vector2 end )  
 91     {  
 92         if(!canDrawLines)  
 93         return;  
 94         GL.PushMatrix ();  
 95         GL.LoadOrtho ();  
 96         beg.x=beg.x/Screen.width;  //      
 97         end.x=end.x/Screen.width;  
 98         beg.y=beg.y/Screen.height;  
 99         end.y=end.y/Screen.height;  
100         lineMaterial.SetPass( 0 );  
101         GL.Begin( GL.LINES );  
102         GL.Color( new Color(1,1,1,0.5f) );  
103         GL.Vertex3( beg.x,beg.y,0);  
104         GL.Vertex3( end.x,end.y,0);  
105         GL.End();  
106         GL.PopMatrix ();
107     } 
108         
109     void OnGUI()  
110     {  
111         e = Event.current;  
112     
113         if(GUI.Button(new  Rect(150,0,100,50),"End Lines"))  
114         {  
115             ClearLines();  
116         }  
117     } 
118     void ClearLines()  
119     {  
120         canDrawLines = false;  
121         posAL.Clear();  
122     }
126     void OnPostRender()
127     {  
128         GLDrawLine1() ; 
129     }   
130     
131     public  void DrawBezier(Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color color, float width)
132     {
133         int segments = Mathf.FloorToInt((start - end).magnitude/20) *3; // Three segments per distance of 20
134         DrawBezier(start, startTangent, end, endTangent, color, width, segments);
135     }
136     
137     public  void DrawBezier(Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color color, float width, int segments)
138     {
139         Vector2 startVector = CubeBezier(start, startTangent, end, endTangent, 0);
140         for (int i = 1; i < segments; i++)
141         {
142             Vector2 endVector = CubeBezier(start, startTangent, end, endTangent, i/(float)segments);
143             DrawLine(startVector, endVector);
144             startVector = endVector;
145         }
146     }
147     private  Vector2 CubeBezier(Vector2 s, Vector2 st, Vector2 e, Vector2 et, float t)
148     {
149         float rt = 1-t;
150         float rtt = rt * t;
151         return rt*rt*rt * s + 3 * rt * rtt * st + 3 * rtt * t * et + t*t*t* e;
152     }
153 }
posted @ 2012-07-10 22:59  U_探索  阅读(1721)  评论(0编辑  收藏  举报