# VectorLine

(Difference between revisions)

Author: Eric Haines (Eric5h5)

## Description

Draws a vector line on the screen based on normalized viewport coordinates. The line can have an arbitrary width, color, and number of segments, and is defined by an array of Vector2s. Requires Unity Pro, since it uses the GL class.

## Usage

Put this script on a camera. The public variables control the number of points in the line, its color, its thickness, and whether the line should be drawn or not (because disabling the script doesn't stop the OnPostRender function from running). The linePoints array is an array of Vector2s in normalized viewport coordinates (i.e. (0, 0) is the lower-left corner of the screen and (1,1) is the upper-right). Currently only one contiguous line is supported, but it would be easy enough to modify it to use an array of Vector2s in order to draw multiple separate lines, if so inclined. A sample routine, which results in the image above:

<javascript>private var origPoints : Vector2[];

function Start () { linePoints = new Vector2[numberOfPoints]; origPoints = new Vector2[numberOfPoints];

// Plot points on a circle var radians : float = 360.0/(numberOfPoints-1)*Mathf.Deg2Rad; var p = 0.0; for (i = 0; i < numberOfPoints; i++) { linePoints[i] = Vector2(.5 + .25*Mathf.Cos(p), .5 + .25*Mathf.Sin(p)); origPoints[i] = linePoints[i]; p += radians; } }

function Update () { for (i = 0; i < linePoints.Length; i++) { if (i%2 == 0) {var m = .4; var t = 1.0;} else {m = .5; t = .5;} linePoints[i] = (origPoints[i]-Vector2(.5, .5))*(Mathf.Sin(Time.time*t)+Mathf.PI*m)+Vector2(.5, .5); } }</javascript>

This should be in the same script with the VectorLine code, below.

## VectorLine.js

<javascript>var numberOfPoints = 31; var lineColor = Color.white; var lineWidth = 3; var drawLines = true; private var lineMaterial : Material; private var linePoints : Vector2[];

function Awake () { lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" + "SubShader { Pass {" + " BindChannels { Bind \"Color\",color }" + " Blend SrcAlpha OneMinusSrcAlpha" + " ZWrite Off Cull Off Fog { Mode Off }" + "} } }"); lineMaterial.hideFlags = HideFlags.HideAndDontSave; lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave; }

function OnPostRender () { if (!drawLines || !linePoints || linePoints.Length < 2) {return;}

var cam = camera; // Cache GetComponent call in local variable for speed var nearClip = cam.nearClipPlane+.01; // Add a bit, else there's flickering when the camera moves var pWidth = (1.0/Screen.width)*((Screen.width+0.0)/Screen.height)*.22; var end = linePoints.Length-1; var widthEnd = lineWidth-1;

lineMaterial.SetPass(0); GL.Begin(GL.LINES); GL.Color(lineColor);

for (i = 0; i < end; i++) { var v1 = cam.ViewportToWorldPoint(Vector3(linePoints[i].x, linePoints[i].y, nearClip)); var v2 = cam.ViewportToWorldPoint(Vector3(linePoints[i+1].x, linePoints[i+1].y, nearClip)); GL.Vertex(v1); GL.Vertex(v2);

// Draw more lines on either side of the main line, if drawing lines wider than 1 pixel if (lineWidth > 1) { var perpendicular = Vector3.Cross(v1, v2).normalized; var sign = 1; for (j = 0; j < widthEnd; j++) { var addWidth = perpendicular * pWidth * (1 + j>>1) * sign; GL.Vertex(v1 + addWidth); GL.Vertex(v2 + addWidth); sign = -sign; } } } GL.End(); }

function OnApplicationQuit () { DestroyImmediate(lineMaterial); }

@script RequireComponent(Camera)</javascript>