You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.0 KiB

using System.Collections.Generic;
using UnityEngine;
namespace DebugDrawEx
{
public class Line
{
public Material Mat { get; set; }
public Mesh Mesh { get; private set; }
public bool DepthTest { get; set; }
public float Duration { get; set; }
private readonly List<Vector3> _vertices;
private readonly List<Color> _colors;
private readonly List<int> _indices;
private static readonly int ZTest = Shader.PropertyToID("_ZTest");
public Line()
{
_vertices = new List<Vector3>();
_colors = new List<Color>();
_indices = new List<int>();
}
public void AddLine(Vector3 from, Vector3 to, Color color)
{
_vertices.Add(from);
_vertices.Add(to);
_colors.Add(color);
_colors.Add(color);
_indices.Add(_vertices.Count - 2);
_indices.Add(_vertices.Count - 1);
Mesh = CreateMesh();
if (Duration != 0)
CreateGameObjectLine(from, to, color);
}
private void CreateGameObjectLine(Vector3 from, Vector3 to, Color color)
{
var go = new GameObject("line");
go.hideFlags = HideFlags.HideAndDontSave;
var lineMonoBehaviour = go.AddComponent<LineMonoBehaviour>();
lineMonoBehaviour.Point1 = from;
lineMonoBehaviour.Point2 = to;
lineMonoBehaviour.Color = color;
lineMonoBehaviour.Duration = Duration;
lineMonoBehaviour.DepthTest = DepthTest;
}
private Mesh CreateMesh()
{
var newMesh = new Mesh();
newMesh.MarkDynamic();
Mat = new Material(Shader.Find("Hidden/Internal-Colored"));
Mat.SetInt(ZTest, DepthTest ? 4 : 0);
newMesh.SetVertices(_vertices);
newMesh.SetColors(_colors);
newMesh.SetIndices(_indices.ToArray(), MeshTopology.Lines, 0);
return newMesh;
}
}
}