您好,登錄后才能下訂單哦!
這篇文章主要介紹Unity3d如何使用LineRenderer畫線,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
LineRenderer線渲染器主要是用于在3D中渲染線段,雖然我們也可以使用GL圖像庫來渲染線段,但是使用LineRenderer我們可以對線段進行更多的操作,例如:設(shè)置顏色,寬度等。在這里要注意LineRenderer渲染出的線段的兩個端點是3D世界中的點,即他是屬于世界坐標(World Point)中的。
LineRenderer是以組件形成存在的,首先我們新建一個空的Game Object,然后我們選擇“Component→Effects→Line Renderer”,即可為其添加LineRenderer組件了。
其實我們也可以通過腳本來為其添加LineRenderer組件:
LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>();
獲取LineRenderer組件:
lineRenderer = GetComponent<LineRenderer>();
【 案例】根據(jù)鼠標左擊的位置,來持續(xù)繪制線段
首先我們在場景中新建一個空的 GameObject,并Reset一下。然后將Script1腳本添加給他。
using UnityEngine; using System.Collections; using System.Collections.Generic; //[RequireComponent(typeof(LineRenderer))] public class DrawLine : MonoBehaviour { private LineRenderer line; private bool isMousePressed; private List<Vector3> pointsList; private Vector3 mousePos; //private Vector3 m_DownCamPos; //private Vector3 m_mouseDownStartPos; //主相機節(jié)點下 // Structure for line points struct myLine { public Vector3 StartPoint; public Vector3 EndPoint; }; // ----------------------------------- void Awake() { _Init(); } private void _Init() { if (m_init) return; m_init = true; // Create line renderer component and set its property //line = this.GetComponent<LineRenderer>(); line = gameObject.AddComponent<LineRenderer>(); line.material = new Material(Shader.Find("Particles/Additive")); line.SetVertexCount(0); line.SetWidth(0.1f, 0.1f); line.SetColors(Color.green, Color.green); line.useWorldSpace = true; isMousePressed = false; pointsList = new List<Vector3>(); line.sortingLayerName = "Ignore Raycast"; line.sortingOrder = 999; // renderer.material.SetTextureOffset( //m_mainCam = this.GetComponent<Camera>(); } bool m_init = false; //Camera m_mainCam; // ----------------------------------- void Update() { // If mouse button down, remove old line and set its color to green if (Input.GetMouseButtonDown(1)) { isMousePressed = true; line.SetVertexCount(0); pointsList.RemoveRange(0, pointsList.Count); line.SetColors(Color.green, Color.green); //m_DownCamPos = Camera.main.transform.position; //m_mouseDownStartPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 1f)); } else if (Input.GetMouseButtonUp(1)) { isMousePressed = false; } //if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2)) { // Vector3 crelpos = Camera.main.transform.position; // for (int i = 0; i < pointsList.Count; i++) { // line.SetPosition(i, crelpos + pointsList[i]); // //line.SetPosition(pointsList.Count - 1, (Vector3)pointsList[pointsList.Count - 1]); // } //} // Drawing line when mouse is moving(presses) if (isMousePressed) { Vector3 crelpos = Camera.main.transform.position; //將鼠標點擊的屏幕坐標轉(zhuǎn)換為世界坐標,然后存儲到position中 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 1f)); Vector3 offsetpos = mousePos - crelpos; //Vector3 hitp = Vector3.zero; //bool ok = GetRayCastZero2DPlanePoint(Camera.main, Camera.main.transform.TransformPoint(0f, 0f, Camera.main.nearClipPlane + 1f).z, out hitp); //mousePos = hitp; if (!pointsList.Contains(offsetpos)) { pointsList.Add(offsetpos); line.SetVertexCount(pointsList.Count); for (int i = 0; i < pointsList.Count; i++) { line.SetPosition(i, crelpos + pointsList[i]); //line.SetPosition(pointsList.Count - 1, (Vector3)pointsList[pointsList.Count - 1]); } if (isLineCollide()) { isMousePressed = false; line.SetColors(Color.red, Color.red); } } } } // ----------------------------------- // Following method checks is currentLine(line drawn by last two points) collided with line // ----------------------------------- private bool isLineCollide() { if (pointsList.Count < 2) return false; int TotalLines = pointsList.Count - 1; myLine[] lines = new myLine[TotalLines]; if (TotalLines > 1) { for (int i = 0; i < TotalLines; i++) { lines[i].StartPoint = (Vector3)pointsList[i]; lines[i].EndPoint = (Vector3)pointsList[i + 1]; } } for (int i = 0; i < TotalLines - 1; i++) { myLine currentLine; currentLine.StartPoint = (Vector3)pointsList[pointsList.Count - 2]; currentLine.EndPoint = (Vector3)pointsList[pointsList.Count - 1]; if (isLinesIntersect(lines[i], currentLine)) return true; } return false; } // ----------------------------------- // Following method checks whether given two points are same or not // ----------------------------------- private bool checkPoints(Vector3 pointA, Vector3 pointB) { return (pointA.x == pointB.x && pointA.y == pointB.y); } // ----------------------------------- // Following method checks whether given two line intersect or not // ----------------------------------- private bool isLinesIntersect(myLine L1, myLine L2) { if (checkPoints(L1.StartPoint, L2.StartPoint) || checkPoints(L1.StartPoint, L2.EndPoint) || checkPoints(L1.EndPoint, L2.StartPoint) || checkPoints(L1.EndPoint, L2.EndPoint)) return false; return ((Mathf.Max(L1.StartPoint.x, L1.EndPoint.x) >= Mathf.Min(L2.StartPoint.x, L2.EndPoint.x)) && (Mathf.Max(L2.StartPoint.x, L2.EndPoint.x) >= Mathf.Min(L1.StartPoint.x, L1.EndPoint.x)) && (Mathf.Max(L1.StartPoint.y, L1.EndPoint.y) >= Mathf.Min(L2.StartPoint.y, L2.EndPoint.y)) && (Mathf.Max(L2.StartPoint.y, L2.EndPoint.y) >= Mathf.Min(L1.StartPoint.y, L1.EndPoint.y)) ); } }
以上是“Unity3d如何使用LineRenderer畫線”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。