本文永久地址:http://www.omuying.com/article/77.aspx,
【文章转载请注明出处!】
在前两篇中,我们使用了 Graphics(查看详情)以及 Line Renderer 方法绘制线条(查看详情),这次我们通过 GL 来绘制线条,这种方法与前两种方法相比,效率与性能上面差不多。原文链接:http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/。
新建立一个 C# 脚本,取名:DrawLinesByGL.cs 代码如下:
002 | using System.Collections; |
004 | public class DrawLinesByGL : MonoBehaviour |
006 | public Shader shader; |
008 | private static Material m; |
009 | private GameObject g; |
010 | private float speed = 100.0f; |
011 | private Vector3[] lp; |
012 | private Vector3[] sp; |
015 | private GUIStyle labelStyle; |
016 | private GUIStyle linkStyle; |
019 | labelStyle = new GUIStyle(); |
020 | labelStyle.normal.textColor = Color.black; |
022 | linkStyle = new GUIStyle(); |
023 | linkStyle.normal.textColor = Color.blue; |
025 | m = new Material(shader); |
026 | g = new GameObject( "g" ); |
031 | void processInput() { |
032 | float s = speed * Time.deltaTime; |
033 | if (Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.LeftShift)) s = s * 0.1f; |
034 | if (Input.GetKey(KeyCode.UpArrow)) g.transform.Rotate(-s, 0, 0); |
035 | if (Input.GetKey(KeyCode.DownArrow)) g.transform.Rotate(s, 0, 0); |
036 | if (Input.GetKey(KeyCode.LeftArrow)) g.transform.Rotate(0, -s, 0); |
037 | if (Input.GetKey(KeyCode.RightArrow)) g.transform.Rotate(0, s, 0); |
039 | if (Input.GetKeyDown(KeyCode.C)) { |
040 | g.transform.rotation = Quaternion.identity; |
049 | if (Input.GetMouseButton(0)) { |
051 | Vector3 e = GetNewPoint(); |
053 | if (s != Vector3.zero) { |
054 | for ( int i = 0; i < lp.Length; i += 2) { |
055 | float d = Vector3.Distance(lp[i], e); |
056 | if (d < 1 && Random.value > 0.9f) sp = AddLine(sp, lp[i], e, false ); |
059 | lp = AddLine(lp, s, e, false ); |
073 | if (Input.GetMouseButtonDown(0)) { |
077 | if (Input.GetMouseButton(0)) { |
079 | lp = AddLine(lp, s, e, true ); |
082 | if (Input.GetMouseButtonUp(0)) { |
084 | lp = AddLine(lp, s, e, false ); |
088 | Vector3[] AddLine(Vector3[] l, Vector3 s, Vector3 e, bool tmp) { |
090 | if (!tmp || vl == 0) l = resizeVertices(l, 2); |
098 | Vector3[] resizeVertices(Vector3[] ovs, int ns) { |
099 | Vector3[] nvs = new Vector3[ovs.Length + ns]; |
100 | for ( int i = 0; i < ovs.Length; i++) nvs[i] = ovs[i]; |
104 | Vector3 GetNewPoint() { |
105 | return g.transform.InverseTransformPoint( |
106 | Camera.main.ScreenToWorldPoint( |
107 | new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z * -1.0f) |
112 | void OnPostRender() { |
115 | GL.MultMatrix(g.transform.transform.localToWorldMatrix); |
116 | GL.Begin( GL.LINES ); |
117 | GL.Color( new Color(0,0,0,0.4f) ); |
119 | for ( int i = 0; i < lp.Length; i++) { |
120 | GL.Vertex3(lp[i].x, lp[i].y, lp[i].z); |
123 | GL.Color( new Color(0,0,0,0.1f) ); |
125 | for ( int i = 0; i < sp.Length; i++) { |
126 | GL.Vertex3(sp[i].x, sp[i].y, sp[i].z); |
134 | GUI.Label ( new Rect (10, 10, 300, 24), "GL. Cursor keys to rotate (with Shift for slow)" , labelStyle); |
135 | int vc = lp.Length + sp.Length; |
136 | GUI.Label ( new Rect (10, 26, 300, 24), "Pushing " + vc + " vertices. 'C' to clear" , labelStyle); |
138 | GUI.Label ( new Rect (10, Screen.height - 20, 250, 24), ".Inspired by a demo from " , labelStyle); |
139 | if (GUI.Button ( new Rect (150, Screen.height - 20, 300, 24), "mrdoob" , linkStyle)) { |
然后把 DrawLinesByGL.cs 组件挂载到 MainCamera 对象上,如图:
运行游戏,最终效果如图: