summaryrefslogtreecommitdiff
path: root/ch.sycoforge.Decal.Demo/LineUtil.cs
blob: 864ee1986cc4573b3333d56c9358f8c6f64955b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Collections.Generic;
using UnityEngine;

namespace ch.sycoforge.Decal.Demo;

public static class LineUtil
{
	public static void DrawPath(float thickness, Material material, List<Vector3> path)
	{
		if (path != null && (path == null || path.Count >= 2))
		{
			if (thickness <= Mathf.Epsilon)
			{
				GL.Begin(1);
			}
			else
			{
				GL.Begin(7);
			}
			material.SetPass(0);
			GL.Color(Color.blue);
			Vector3 start = path[0];
			for (int i = 1; i < path.Count; i++)
			{
				Vector3 vector = path[i];
				DrawLine(thickness, start, vector);
				start = vector;
			}
			GL.End();
		}
	}

	private static void DrawLine(float thickness, Vector3 start, Vector3 end)
	{
		if (thickness <= Mathf.Epsilon)
		{
			GL.Vertex(start);
			GL.Vertex(end);
			return;
		}
		Camera main = Camera.main;
		Vector3 normalized = (end - start).normalized;
		Vector3 normalized2 = (start - main.transform.position).normalized;
		Vector3 vector = Vector3.Cross(normalized2, normalized) * (thickness / 2f);
		GL.Vertex(start - vector);
		GL.Vertex(start + vector);
		GL.Vertex(end + vector);
		GL.Vertex(end - vector);
	}
}