Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw lines with LineRenderer in Unity3d

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how Unity3d uses LineRenderer to draw lines. The introduction in this article is very detailed and has certain reference value. Interested friends must read it!

LineRenderer is mainly used to render line segments in 3D, although we can also use GL image library to render line segments, but with LineRenderer we can do more operations on line segments, such as: setting color, width, etc. Note here that the two endpoints of the segment rendered by LineRenderer are points in the 3D world, i.e., they belong to the world coordinates (World Point).

LineRenderer exists as a component. First, we create an empty Game Object, and then we select "Component→Effects→Line Renderer" to add LineRenderer components to it.

In fact, we can also add LineRenderer components to it through scripts:

LineRenderer lineRenderer = gameObject.AddComponent();

Get LineRenderer component:

lineRenderer = GetComponent();

[Case] Keep drawing line segments according to the left mouse click position

First we create an empty GameObject in the scene and Reset it. Then add Script1 script to him.

using UnityEngine; using System.Collections; using System.Collections.Generic; //[RequireComponent(typeof(LineRenderer))] public class DrawLine : MonoBehaviour { private LineRenderer line; private bool isMousePressed; private List pointsList; private Vector3 mousePos; //private Vector3 m_DownCamPos; //private Vector3 m_mouseDownStartPos; //Main camera node // 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(); line = gameObject.AddComponent(); 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(); line.sortingLayerName = "Ignore Raycast"; line.sortingOrder = 999; // renderer.material.SetTextureOffset( //m_mainCam = this.GetComponent(); } 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; //将鼠标点击的屏幕坐标转换为世界坐标,然后存储到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)) ); } } The above is all the content of this article "Unity3d how to draw lines using LineRenderer", thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report