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

3 years ago
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace DebugDrawEx
  4. {
  5. public class Line
  6. {
  7. public Material Mat { get; set; }
  8. public Mesh Mesh { get; private set; }
  9. public bool DepthTest { get; set; }
  10. public float Duration { get; set; }
  11. private readonly List<Vector3> _vertices;
  12. private readonly List<Color> _colors;
  13. private readonly List<int> _indices;
  14. private static readonly int ZTest = Shader.PropertyToID("_ZTest");
  15. public Line()
  16. {
  17. _vertices = new List<Vector3>();
  18. _colors = new List<Color>();
  19. _indices = new List<int>();
  20. }
  21. public void AddLine(Vector3 from, Vector3 to, Color color)
  22. {
  23. _vertices.Add(from);
  24. _vertices.Add(to);
  25. _colors.Add(color);
  26. _colors.Add(color);
  27. _indices.Add(_vertices.Count - 2);
  28. _indices.Add(_vertices.Count - 1);
  29. Mesh = CreateMesh();
  30. if (Duration != 0)
  31. CreateGameObjectLine(from, to, color);
  32. }
  33. private void CreateGameObjectLine(Vector3 from, Vector3 to, Color color)
  34. {
  35. var go = new GameObject("line");
  36. go.hideFlags = HideFlags.HideAndDontSave;
  37. var lineMonoBehaviour = go.AddComponent<LineMonoBehaviour>();
  38. lineMonoBehaviour.Point1 = from;
  39. lineMonoBehaviour.Point2 = to;
  40. lineMonoBehaviour.Color = color;
  41. lineMonoBehaviour.Duration = Duration;
  42. lineMonoBehaviour.DepthTest = DepthTest;
  43. }
  44. private Mesh CreateMesh()
  45. {
  46. var newMesh = new Mesh();
  47. newMesh.MarkDynamic();
  48. Mat = new Material(Shader.Find("Hidden/Internal-Colored"));
  49. Mat.SetInt(ZTest, DepthTest ? 4 : 0);
  50. newMesh.SetVertices(_vertices);
  51. newMesh.SetColors(_colors);
  52. newMesh.SetIndices(_indices.ToArray(), MeshTopology.Lines, 0);
  53. return newMesh;
  54. }
  55. }
  56. }