Created
July 25, 2020 09:05
-
-
Save BenChung/63699452d07549dfc288cfc73ac6e76d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Lines | |
{ | |
public class VectorLine | |
{ | |
private LineRenderer renderer; | |
public VectorLine(LineRenderer renderer) | |
{ | |
this.renderer = renderer; | |
} | |
public void Remove() | |
{ | |
UnityEngine.Object.Destroy(this.renderer.gameObject); | |
} | |
} | |
public static VectorLine DrawLine(Vector3 start, Vector3 end, Color color) | |
{ | |
GameObject go = new GameObject("LineDrawer"); | |
var renderer = go.AddComponent<LineRenderer>(); | |
renderer.material = new Material(Shader.Find("Legacy Shaders/Particles/Additive")); | |
renderer.useWorldSpace = true; | |
renderer.SetVertexCount(2); | |
renderer.SetPosition(0, start); | |
renderer.SetPosition(1, end); | |
renderer.SetColors(color, color); | |
renderer.SetWidth(0.1f, 0.1f); | |
renderer.enabled = true; | |
return new VectorLine(renderer); | |
} | |
public static VectorLine DrawCurve(BezierCurve curve, Vector3 offset, Color colorStart, Color colorEnd) | |
{ | |
GameObject go = new GameObject("LineDrawer"); | |
var renderer = go.AddComponent<LineRenderer>(); | |
renderer.material = new Material(Shader.Find("Legacy Shaders/Particles/Additive")); | |
renderer.useWorldSpace = true; | |
var eps = EquiPointSet.FromBezierEquidistant(curve, 0.1f); | |
renderer.SetVertexCount(eps.points.Length); | |
int npoint = 0; | |
foreach (var point in eps.points) | |
{ | |
var posd = point.position; | |
renderer.SetPosition(npoint++, new Vector3((float)posd.x, (float)posd.y, (float)posd.z) + offset); | |
} | |
renderer.SetColors(colorStart, colorEnd); | |
renderer.SetWidth(0.1f, 0.1f); | |
renderer.enabled = true; | |
return new VectorLine(renderer); | |
} | |
private static List<Lines.VectorLine> lastLines = new List<Lines.VectorLine>(); | |
public static void RegisterLine(VectorLine line) | |
{ | |
lastLines.Add(line); | |
} | |
public static void ClearLines() | |
{ | |
lastLines.ForEach(l => l.Remove()); | |
lastLines.Clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment