banner



How To Draw Tie Lines Using Plait Point

Using GL Lines:

I would recommend using the the GL API for drawing lines. The line thickness will e'er be 1px on screen and there is no option to change it. There will likewise be no shadows.

The GL method calls are executed immediately then you lot need to make sure to call them after the camera has already rendered.

Attaching the script to the camera and using Photographic camera.OnPostRender() works good for rendering in the game window. To get them to prove in the editor, you can use MonoBehaviour.OnDrawGizmos().

Here is the barebones code to depict a line with the GL API (notation, this code does non work in modern Unity):

          public Material lineMat = new Material("Shader \"Lines/Colored Blended\" {" + "SubShader { Pass { " + "    Blend SrcAlpha OneMinusSrcAlpha " + "    ZWrite Off Choose Off Fog { Style Off } " + "    BindChannels {" + "      Demark \"vertex\", vertex Bind \"colour\", color }" + "} } }");  void OnPostRender() {     GL.Begin(GL.LINES);     lineMat.SetPass(0);     GL.Colour(new Color(0f, 0f, 0f, 1f));     GL.Vertex3(0f, 0f, 0f);     GL.Vertex3(1f, 1f, 1f);     GL.Finish(); }                  

Hither is a full script that attaches all of the given points to the chief point. In that location are some instructions in the comments of the lawmaking to get it set up right and about what is going on.

If you are having problems irresolute the color of the connecting lines, make sure to use a shader on your line textile that takes into business relationship the vertex colour such as Unlit/Color.

          using UnityEngine; using System.Collections;  // Put this script on a Camera public class DrawLines : MonoBehaviour {      // Fill/drag these in from the editor      // Choose the Unlit/Colour shader in the Material Settings     // Yous can alter that color, to change the color of the connecting lines     public Textile 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 indicate in points) {                 Vector3 mainPointPos = mainPoint.transform.position;                 Vector3 pointPos = point.transform.position;                                  GL.Begin(GL.LINES);                 lineMat.SetPass(0);                 GL.Colour(new Colour(lineMat.color.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 prove 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 pick up the ShadowCaster laissez passer.


Lines with Shadows and Radius

If you need to alter the line thickness and want to accept realistic shadows. Just employ a cylinder mesh and scale the height.

Hither is a script that will make a cylinder to connect each point to the main signal. Place it on a empty game object and fill up in the parameters. It will hold all of the actress connecting objects.

          using UnityEngine; using System.Collections;  public class ConnectPointsWithCylinderMesh : MonoBehaviour {      // Material used for the connecting lines     public Fabric lineMat;      public bladder 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     // Nosotros will business relationship for the cylinder pivot/origin being in the eye.     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++) {             // Make a gameobject that we will put the ring on             // And and then put it equally a child 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;              // We brand a offset gameobject to counteract the default cylindermesh pivot/origin beingness in the middle             GameObject ringOffsetCylinderMeshObject = new GameObject();             ringOffsetCylinderMeshObject.transform.parent = this.ringGameObjects[i].transform;              // Offset 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 ring             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.Altitude(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 expect at the main point.             // Since the cylinder is pointing up(y) and the frontwards is z, we demand to offset by xc degrees.             this.ringGameObjects[i].transform.LookAt(this.mainPoint.transform, Vector3.up);             this.ringGameObjects[i].transform.rotation *= Quaternion.Euler(90, 0, 0);         }     } }                  

Source: https://gamedev.stackexchange.com/questions/96964/how-to-correctly-draw-a-line-in-unity

Posted by: upchurchsucken.blogspot.com

0 Response to "How To Draw Tie Lines Using Plait Point"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel