Unity Draw Line on Plane
Using GL Lines:
I would recommend using the the GL API for drawing lines. The line thickness will always exist 1px on screen and there is no option to change it. There volition besides be no shadows.
The GL method calls are executed immediately and so yous need to make sure to call them after the camera has already rendered.
Attaching the script to the camera and using Camera.OnPostRender() works adept for rendering in the game window. To get them to show in the editor, you tin can utilize MonoBehaviour.OnDrawGizmos().
Here is the barebones code to draw a line with the GL API (note, this code does non work in modern Unity):
public Textile lineMat = new Material("Shader \"Lines/Colored Blended\" {" + "SubShader { Laissez passer { " + " Blend SrcAlpha OneMinusSrcAlpha " + " ZWrite Off Choose Off Fog { Mode Off } " + " BindChannels {" + " Bind \"vertex\", vertex Bind \"color\", color }" + "} } }"); void OnPostRender() { GL.Begin(GL.LINES); lineMat.SetPass(0); GL.Color(new Colour(0f, 0f, 0f, 1f)); GL.Vertex3(0f, 0f, 0f); GL.Vertex3(1f, 1f, 1f); GL.End(); } Here is a full script that attaches all of the given points to the primary point. There are some instructions in the comments of the code to get it set correct and nearly what is going on.
If you are having problems changing the colour of the connecting lines, brand sure to use a shader on your line textile that takes into account the vertex color such as Unlit/Color.
using UnityEngine; using System.Collections; // Put this script on a Photographic camera public form DrawLines : MonoBehaviour { // Fill/drag these in from the editor // Cull the Unlit/Colour shader in the Fabric Settings // You can alter that color, to change the color of the connecting lines public Material lineMat; public GameObject mainPoint; public GameObject[] points; // Connect all of the `points` to the `mainPoint` void DrawConnectingLines() { if(mainPoint && points.Length > 0) { // Loop through each betoken to connect to the mainPoint foreach(GameObject point in points) { Vector3 mainPointPos = mainPoint.transform.position; Vector3 pointPos = point.transform.position; GL.Brainstorm(GL.LINES); lineMat.SetPass(0); GL.Color(new Colour(lineMat.colour.r, lineMat.color.g, lineMat.color.b, lineMat.color.a)); GL.Vertex3(mainPointPos.10, mainPointPos.y, mainPointPos.z); GL.Vertex3(pointPos.10, pointPos.y, pointPos.z); GL.End(); } } } // To show the lines in the game window whne it is running void OnPostRender() { DrawConnectingLines(); } // To show the lines in the editor void OnDrawGizmos() { DrawConnectingLines(); } }
Further note on shadows: I explored using a geometry shader to make shadows but since the GL calls run immediately, they are not in the normal rendering pipeline and AutoLight.cginc and Lighting.cginc won't option up the ShadowCaster laissez passer.
Lines with Shadows and Radius
If y'all need to change the line thickness and desire to accept realistic shadows. Just apply a cylinder mesh and scale the height.
Hither is a script that will make a cylinder to connect each point to the main point. Identify it on a empty game object and fill up in the parameters. Information technology will hold all of the extra connecting objects.
using UnityEngine; using Arrangement.Collections; public class ConnectPointsWithCylinderMesh : MonoBehaviour { // Textile used for the connecting lines public Textile lineMat; public float radius = 0.05f; // Connect all of the `points` to the `mainPoint` public GameObject mainPoint; public GameObject[] points; // Fill in this with the default Unity Cylinder mesh // We will account for the cylinder pivot/origin existence in the middle. public Mesh cylinderMesh; GameObject[] ringGameObjects; // Employ this for initialization void Start () { this.ringGameObjects = new GameObject[points.Length]; //this.connectingRings = new ProceduralRing[points.Length]; for(int i = 0; i < points.Length; i++) { // Brand a gameobject that nosotros will put the ring on // And then put it as a kid on the gameobject that has this Command and Control script this.ringGameObjects[i] = new GameObject(); this.ringGameObjects[i].name = "Connecting ring #" + i; this.ringGameObjects[i].transform.parent = this.gameObject.transform; // Nosotros brand a first gameobject to counteract the default cylindermesh pin/origin existence in the middle GameObject ringOffsetCylinderMeshObject = new GameObject(); ringOffsetCylinderMeshObject.transform.parent = this.ringGameObjects[i].transform; // Beginning the cylinder so that the pivot/origin is at the bottom in relation to the outer ring gameobject. ringOffsetCylinderMeshObject.transform.localPosition = new Vector3(0f, 1f, 0f); // Set the radius ringOffsetCylinderMeshObject.transform.localScale = new Vector3(radius, 1f, radius); // Create the the Mesh and renderer to show the connecting band MeshFilter ringMesh = ringOffsetCylinderMeshObject.AddComponent<MeshFilter>(); ringMesh.mesh = this.cylinderMesh; MeshRenderer ringRenderer = ringOffsetCylinderMeshObject.AddComponent<MeshRenderer>(); ringRenderer.textile = lineMat; } } // Update is called once per frame void Update () { for(int i = 0; i < points.Length; i++) { // Move the band to the point this.ringGameObjects[i].transform.position = this.points[i].transform.position; // Friction match the calibration to the distance float cylinderDistance = 0.5f*Vector3.Distance(this.points[i].transform.position, this.mainPoint.transform.position); this.ringGameObjects[i].transform.localScale = new Vector3(this.ringGameObjects[i].transform.localScale.x, cylinderDistance, this.ringGameObjects[i].transform.localScale.z); // Make the cylinder look at the master indicate. // Since the cylinder is pointing up(y) and the forward is z, nosotros demand to offset by 90 degrees. this.ringGameObjects[i].transform.LookAt(this.mainPoint.transform, Vector3.up); this.ringGameObjects[i].transform.rotation *= Quaternion.Euler(ninety, 0, 0); } } }
Source: https://gamedev.stackexchange.com/questions/96964/how-to-correctly-draw-a-line-in-unity
Postar um comentário for "Unity Draw Line on Plane"