-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGizmoHelper.cs
More file actions
169 lines (166 loc) · 6.74 KB
/
GizmoHelper.cs
File metadata and controls
169 lines (166 loc) · 6.74 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
namespace Emptybraces
{
[AddComponentMenu("")]
public class GizmoHelper : MonoBehaviour
{
Color DefaultColor = Color.white;
static GizmoHelper Instance
{
get
{
if (_instance == null)
{
var g = new GameObject(nameof(GizmoHelper));
DontDestroyOnLoad(g);
_instance = g.AddComponent<GizmoHelper>();
}
return _instance;
}
}
static GizmoHelper _instance;
List<Func<bool>> _renderList = new();
void OnDrawGizmos()
{
for (int i = 0; i < _renderList.Count; ++i)
if (_renderList[i]())
_renderList.RemoveAt(i--);
}
[Conditional("UNITY_EDITOR")] public static void DrawRay(Vector3 p, Vector3 dir, float span = 1) => DrawRay(p, dir, Instance.DefaultColor, span);
[Conditional("UNITY_EDITOR")]
public static void DrawRay(Vector3 p, Vector3 dir, Color c, float span = 1, bool unscaled = false)
{
var end_time = (unscaled ? Time.unscaledTime : Time.time) + span;
Instance._renderList.Add(() =>
{
Gizmos.color = c;
Gizmos.DrawRay(p, dir);
return end_time < (unscaled ? Time.unscaledTime : Time.time);
});
}
[Conditional("UNITY_EDITOR")] public static void DrawMesh(Mesh mesh, Vector3 p, Quaternion r, Vector3 s, int submeshIndex = 0, float span = 1) => DrawMesh(mesh, p, r, s, submeshIndex, Instance.DefaultColor, span);
[Conditional("UNITY_EDITOR")]
public static void DrawMesh(Mesh mesh, Vector3 p, Quaternion r, Vector3 s, int submeshIndex, Color c, float span = 1, bool unscaled = false)
{
var end_time = (unscaled ? Time.unscaledTime : Time.time) + span;
Instance._renderList.Add(() =>
{
Gizmos.color = c;
Gizmos.DrawMesh(mesh, submeshIndex, p, r, s);
return end_time < (unscaled ? Time.unscaledTime : Time.time);
});
}
[Conditional("UNITY_EDITOR")] public static void DrawWireMesh(Mesh mesh, Vector3 p, Quaternion r, Vector3 s, int submeshIndex = 0, float span = 1) => DrawWireMesh(mesh, p, r, s, submeshIndex, Instance.DefaultColor, span);
[Conditional("UNITY_EDITOR")]
public static void DrawWireMesh(Mesh mesh, Vector3 p, Quaternion r, Vector3 s, int submeshIndex, Color c, float span = 1, bool unscaled = false)
{
var end_time = (unscaled ? Time.unscaledTime : Time.time) + span;
Instance._renderList.Add(() =>
{
Gizmos.color = c;
Gizmos.DrawWireMesh(mesh, submeshIndex, p, r, s);
return end_time < (unscaled ? Time.unscaledTime : Time.time);
});
}
[Conditional("UNITY_EDITOR")] public static void DrawSphere(Vector3 p, float radius = 1, float span = 1) => DrawSphere(p, radius, Instance.DefaultColor, span);
[Conditional("UNITY_EDITOR")]
public static void DrawSphere(Vector3 p, float radius, Color c, float span = 1, bool unscaled = false)
{
var end_time = (unscaled ? Time.unscaledTime : Time.time) + span;
Instance._renderList.Add(() =>
{
Gizmos.color = c;
Gizmos.DrawSphere(p, radius);
return end_time < (unscaled ? Time.unscaledTime : Time.time);
});
}
[Conditional("UNITY_EDITOR")] public static void DrawWireSphere(Vector3 p, float radius = 1, float span = 1) => DrawWireSphere(p, radius, Instance.DefaultColor, span);
[Conditional("UNITY_EDITOR")]
public static void DrawWireSphere(Vector3 p, float radius, Color c, float span = 1, bool unscaled = false)
{
var end_time = (unscaled ? Time.unscaledTime : Time.time) + span;
Instance._renderList.Add(() =>
{
Gizmos.color = c;
Gizmos.DrawWireSphere(p, radius);
return end_time < (unscaled ? Time.unscaledTime : Time.time);
});
}
[Conditional("UNITY_EDITOR")] public static void DrawCube(Vector3 p, Vector3 size, Quaternion r, float span = 1) => DrawCube(p, size, r, Instance.DefaultColor, span);
[Conditional("UNITY_EDITOR")]
public static void DrawCube(Vector3 p, Vector3 size, Quaternion r, Color c, float span = 1, bool unscaled = false)
{
var end_time = (unscaled ? Time.unscaledTime : Time.time) + span;
Instance._renderList.Add(() =>
{
Gizmos.color = c;
Gizmos.matrix = Matrix4x4.TRS(p, r, size);
Gizmos.DrawCube(Vector3.zero, Vector3.one);
Gizmos.matrix = Matrix4x4.identity;
return end_time < (unscaled ? Time.unscaledTime : Time.time);
});
}
[Conditional("UNITY_EDITOR")] public static void DrawWireCube(Vector3 p, Vector3 size, Quaternion r, float span = 1) => DrawWireCube(p, size, r, Instance.DefaultColor, span);
[Conditional("UNITY_EDITOR")]
public static void DrawWireCube(Vector3 p, Vector3 size, Quaternion r, Color c, float span = 1, bool unscaled = false)
{
var end_time = (unscaled ? Time.unscaledTime : Time.time) + span;
Instance._renderList.Add(() =>
{
Gizmos.color = c;
Gizmos.matrix = Matrix4x4.TRS(p, r, size);
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
Gizmos.matrix = Matrix4x4.identity;
return end_time < (unscaled ? Time.unscaledTime : Time.time);
});
}
[Conditional("UNITY_EDITOR")] public static void DrawLine(Vector3 p1, Vector3 p2, float span = 1) => DrawLine(p1, p2, Instance.DefaultColor, span);
[Conditional("UNITY_EDITOR")]
public static void DrawLine(Vector3 p1, Vector3 p2, Color c, float span = 1, bool unscaled = false)
{
var end_time = (unscaled ? Time.unscaledTime : Time.time) + span;
Instance._renderList.Add(() =>
{
Gizmos.color = c;
Gizmos.DrawLine(p1, p2);
return end_time < (unscaled ? Time.unscaledTime : Time.time);
});
}
[Conditional("UNITY_EDITOR")] public static void DrawBezier(Vector3 p1, Vector3 p2, Vector3 p3, float span = 1) => DrawBezier(p1, p2, p3, Instance.DefaultColor, span);
[Conditional("UNITY_EDITOR")]
public static void DrawBezier(Vector3 p1, Vector3 p2, Vector3 p3, Color c, float span = 1, bool unscaled = false)
{
var end_time = (unscaled ? Time.unscaledTime : Time.time) + span;
Instance._renderList.Add(() =>
{
Gizmos.color = c;
var prev = p1;
const int cnt = 8;
float f = 1 / (float)cnt;
for (int i = 1; i <= cnt; ++i)
{
var next = __Bezier2d(p1, p2, p3, i * f);
Gizmos.DrawLine(prev, next);
prev = next;
}
return end_time < (unscaled ? Time.unscaledTime : Time.time);
});
static Vector3 __Bezier2d(Vector3 p0, Vector3 p1, Vector3 p2, float t) => (1.0f - t) * (1.0f - t) * p0 + 2.0f * (1.0f - t) * t * p1 + t * t * p2;
}
[Conditional("UNITY_EDITOR")]
public static void RemoveAllGizmos()
{
Instance._renderList.Clear();
}
[Conditional("UNITY_EDITOR")] public static void DrawDirection(Vector3 p, Vector3 dir, float span = 1) => DrawDirection(p, dir, Instance.DefaultColor, span);
[Conditional("UNITY_EDITOR")]
public static void DrawDirection(Vector3 p, Vector3 dir, Color c, float span = 1, bool unscaled = false)
{
DrawLine(p, p + dir, c, span, unscaled);
DrawSphere(p + dir, 0.1f, c, span, unscaled);
}
}
}