line renderer 画线

来源:互联网 发布:pc机安装mac 编辑:程序博客网 时间:2024/06/05 14:47

Unity3D 绘制线条的方法可谓多种多样,但是绘制的线条可以在 Game 视图下面显示的方法并不多,本篇通过 Line Renderer 方法绘制可以在 Game 视图下面显示的线条。原文链接:http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/。

新建立一个 C# 脚本,取名:DrawLinesByLineRender.cs 代码如下:

001using UnityEngine;
002using System.Collections;
003 
004public class DrawLinesByLineRender : MonoBehaviour {
005 
006    public Shader shader;
007 
008    private Vector3 curr;
009    private Vector3 last = new Vector3(0,0,-100.0f);
010     
011    private int canvasIndex = 0;
012    private float lineSizeLarge = 0.02f;
013    private float lineSizeSmall = 0.02f;
014     
015    private Color lineColorLarge = new Color(0,0,0,0.5f);
016    private Color lineColorSmall = new Color(0,0,0,0.1f);
017     
018    private ArrayList points;
019     
020    GUIStyle labelStyle;
021    GUIStyle linkStyle;
022     
023    private float speed = 100f;
024 
025    void Start () {
026        labelStyle = new GUIStyle();
027        labelStyle.normal.textColor = Color.black;
028         
029        linkStyle = new GUIStyle();
030        linkStyle.normal.textColor = Color.blue;
031         
032        points = new ArrayList();
033    }
034     
035    void OnGUI() {
036        GUI.Label (new Rect (10, 10, 300, 24), "LR. Cursor keys to rotate (with Shift for slow)", labelStyle);
037        int vc = canvasIndex + points.Count;
038        GUI.Label (new Rect (10, 26, 300, 24), "Drawin " + vc + " lines. 'C' to clear", labelStyle);
039         
040        GUI.Label (new Rect (10, Screen.height - 20, 250, 24), ".Inspired by a demo from ", labelStyle);
041        if(GUI.Button (new Rect (150, Screen.height - 20, 300, 24),"mrdoob", linkStyle)) {
042            Application.OpenURL("http://mrdoob.com/lab/javascript/harmony/");
043        }
044    }
045     
046    void Update () {
047        float sp = speed * Time.deltaTime;
048        if(Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.LeftShift)) sp = sp * 0.1f;
049        if(Input.GetKey(KeyCode.UpArrow)) transform.Rotate(-sp, 0, 0);
050        if(Input.GetKey(KeyCode.DownArrow)) transform.Rotate(sp, 0, 0);
051        if(Input.GetKey(KeyCode.LeftArrow)) transform.Rotate(0, -sp, 0);
052        if(Input.GetKey(KeyCode.RightArrow)) transform.Rotate(0, sp, 0);
053         
054        if(Input.GetKeyDown(KeyCode.C)) {
055            points = new ArrayList();
056            foreach (Transform line in transform) {
057                GameObject go = line.gameObject;
058                Destroy(go.GetComponent(typeof(LineRenderer)));
059                Destroy(line);
060            }
061        }
062         
063        if(Input.GetMouseButton(0)) {
064            curr = Camera.main.ScreenToWorldPoint(newVector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z * -1.0f));
065            curr = transform.InverseTransformPoint(curr);
066 
067            if(last.z != -100.0f) {
068                createLine(last, curr, lineSizeLarge, lineColorLarge);
069                 
070                foreach(Vector3 p in points) {
071                    Vector3 s = p;
072                    float d = Vector3.Distance(s, curr);
073                    if(d < 1 && Random.value > 0.9f) createLine(s, curr, lineSizeSmall, lineColorSmall);
074                }
075                 
076                points.Add(curr);
077            }
078             
079            last = curr;
080        else {
081            last.z = -100.0f;
082        }
083         
084         
085    }
086     
087    private void createLine(Vector3 start, Vector3 end, float lineSize, Color c) {
088        GameObject canvas = new GameObject("canvas" + canvasIndex);
089        canvas.transform.parent = transform;
090        canvas.transform.rotation = transform.rotation;
091        LineRenderer lines = (LineRenderer) canvas.AddComponent<LineRenderer>();
092        lines.material = new Material(shader);
093        lines.material.color = c;
094        lines.useWorldSpace = false;
095        lines.SetWidth(lineSize, lineSize);
096        lines.SetVertexCount(2);
097        lines.SetPosition(0, start);
098        lines.SetPosition(1, end);
099        canvasIndex++;
100    }
101}

新建立一个空对象,然后把 DrawLinesByLineRender.cs 组件挂载到空对象上,如图:

运行游戏,最终效果如图:

0 0