forked from XINCGer/UnityToolchainsTrick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GizmosExtensions.cs
236 lines (209 loc) · 10 KB
/
GizmosExtensions.cs
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using UnityEditor;
using UnityEngine;
namespace ToolKits
{
public static class GizmosExtensions
{
#region 进一步封装的绘制方法
[DrawGizmo(GizmoType.Selected | GizmoType.NonSelected)]
public static void DrawWireCube(BoxCollider boxCollider, GizmoType gizmosType)
{
var originColor = GUI.color;
GUI.color = Color.white;
DrawWireCube(boxCollider.center, boxCollider.size, boxCollider.transform.rotation);
GUI.color = originColor;
}
[DrawGizmo(GizmoType.Selected | GizmoType.NonSelected)]
public static void DrawWireSphere(SphereCollider sphereCollider, GizmoType gizmosType)
{
var originColor = GUI.color;
GUI.color = Color.white;
DrawWireSphere(sphereCollider.center, sphereCollider.radius, sphereCollider.transform.rotation);
GUI.color = originColor;
}
#endregion
#region 基本绘制方法
/// <summary>
/// Draws a wire cube with a given rotation
/// </summary>
/// <param name="center"></param>
/// <param name="size"></param>
/// <param name="rotation"></param>
public static void DrawWireCube(Vector3 center, Vector3 size, Quaternion rotation = default(Quaternion))
{
var old = Gizmos.matrix;
if (rotation.Equals(default(Quaternion)))
rotation = Quaternion.identity;
Gizmos.matrix = Matrix4x4.TRS(center, rotation, size);
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
Gizmos.matrix = old;
}
public static void DrawArrow(Vector3 from, Vector3 to, float arrowHeadLength = 0.25f,
float arrowHeadAngle = 20.0f)
{
Gizmos.DrawLine(from, to);
var direction = to - from;
var right = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 + arrowHeadAngle, 0) *
new Vector3(0, 0, 1);
var left = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 - arrowHeadAngle, 0) *
new Vector3(0, 0, 1);
Gizmos.DrawLine(to, to + right * arrowHeadLength);
Gizmos.DrawLine(to, to + left * arrowHeadLength);
}
public static void DrawWireSphere(Vector3 center, float radius, Quaternion rotation = default(Quaternion))
{
var old = Gizmos.matrix;
if (rotation.Equals(default(Quaternion)))
rotation = Quaternion.identity;
Gizmos.matrix = Matrix4x4.TRS(center, rotation, Vector3.one);
Gizmos.DrawWireSphere(Vector3.zero, radius);
Gizmos.matrix = old;
}
/// <summary>
/// Draws a flat wire circle (up)
/// </summary>
/// <param name="center"></param>
/// <param name="radius"></param>
/// <param name="segments"></param>
/// <param name="rotation"></param>
public static void DrawWireCircle(Vector3 center, float radius, int segments = 20,
Quaternion rotation = default(Quaternion))
{
DrawWireArc(center, radius, 360, segments, rotation);
}
/// <summary>
/// Draws an arc with a rotation around the center
/// </summary>
/// <param name="center">center point</param>
/// <param name="radius">radiu</param>
/// <param name="angle">angle in degrees</param>
/// <param name="segments">number of segments</param>
/// <param name="rotation">rotation around the center</param>
public static void DrawWireArc(Vector3 center, float radius, float angle, int segments = 20,
Quaternion rotation = default(Quaternion))
{
var old = Gizmos.matrix;
Gizmos.matrix = Matrix4x4.TRS(center, rotation, Vector3.one);
Vector3 from = Vector3.forward * radius;
var step = Mathf.RoundToInt(angle / segments);
for (int i = 0; i <= angle; i += step)
{
var to = new Vector3(radius * Mathf.Sin(i * Mathf.Deg2Rad), 0, radius * Mathf.Cos(i * Mathf.Deg2Rad));
Gizmos.DrawLine(from, to);
from = to;
}
Gizmos.matrix = old;
}
/// <summary>
/// Draws an arc with a rotation around an arbitraty center of rotation
/// </summary>
/// <param name="center">the circle's center point</param>
/// <param name="radius">radius</param>
/// <param name="angle">angle in degrees</param>
/// <param name="segments">number of segments</param>
/// <param name="rotation">rotation around the centerOfRotation</param>
/// <param name="centerOfRotation">center of rotation</param>
public static void DrawWireArc(Vector3 center, float radius, float angle, int segments, Quaternion rotation,
Vector3 centerOfRotation)
{
var old = Gizmos.matrix;
if (rotation.Equals(default(Quaternion)))
rotation = Quaternion.identity;
Gizmos.matrix = Matrix4x4.TRS(centerOfRotation, rotation, Vector3.one);
var deltaTranslation = centerOfRotation - center;
Vector3 from = deltaTranslation + Vector3.forward * radius;
var step = Mathf.RoundToInt(angle / segments);
for (int i = 0; i <= angle; i += step)
{
var to = new Vector3(radius * Mathf.Sin(i * Mathf.Deg2Rad), 0, radius * Mathf.Cos(i * Mathf.Deg2Rad)) +
deltaTranslation;
Gizmos.DrawLine(from, to);
from = to;
}
Gizmos.matrix = old;
}
/// <summary>
/// Draws an arc with a rotation around an arbitraty center of rotation
/// </summary>
/// <param name="matrix">Gizmo matrix applied before drawing</param>
/// <param name="radius">radius</param>
/// <param name="angle">angle in degrees</param>
/// <param name="segments">number of segments</param>
public static void DrawWireArc(Matrix4x4 matrix, float radius, float angle, int segments)
{
var old = Gizmos.matrix;
Gizmos.matrix = matrix;
Vector3 from = Vector3.forward * radius;
var step = Mathf.RoundToInt(angle / segments);
for (int i = 0; i <= angle; i += step)
{
var to = new Vector3(radius * Mathf.Sin(i * Mathf.Deg2Rad), 0, radius * Mathf.Cos(i * Mathf.Deg2Rad));
Gizmos.DrawLine(from, to);
from = to;
}
Gizmos.matrix = old;
}
/// <summary>
/// Draws a wire cylinder face up with a rotation around the center
/// </summary>
/// <param name="center"></param>
/// <param name="radius"></param>
/// <param name="height"></param>
/// <param name="rotation"></param>
public static void DrawWireCylinder(Vector3 center, float radius, float height,
Quaternion rotation = default(Quaternion))
{
var old = Gizmos.matrix;
if (rotation.Equals(default(Quaternion)))
rotation = Quaternion.identity;
Gizmos.matrix = Matrix4x4.TRS(center, rotation, Vector3.one);
var half = height / 2;
//draw the 4 outer lines
Gizmos.DrawLine(Vector3.right * radius - Vector3.up * half, Vector3.right * radius + Vector3.up * half);
Gizmos.DrawLine(-Vector3.right * radius - Vector3.up * half, -Vector3.right * radius + Vector3.up * half);
Gizmos.DrawLine(Vector3.forward * radius - Vector3.up * half, Vector3.forward * radius + Vector3.up * half);
Gizmos.DrawLine(-Vector3.forward * radius - Vector3.up * half,
-Vector3.forward * radius + Vector3.up * half);
//draw the 2 cricles with the center of rotation being the center of the cylinder, not the center of the circle itself
DrawWireArc(center + Vector3.up * half, radius, 360, 20, rotation, center);
DrawWireArc(center + Vector3.down * half, radius, 360, 20, rotation, center);
Gizmos.matrix = old;
}
/// <summary>
/// Draws a wire capsule face up
/// </summary>
/// <param name="center"></param>
/// <param name="radius"></param>
/// <param name="height"></param>
/// <param name="rotation"></param>
public static void DrawWireCapsule(Vector3 center, float radius, float height,
Quaternion rotation = default(Quaternion))
{
if (rotation.Equals(default(Quaternion)))
rotation = Quaternion.identity;
var old = Gizmos.matrix;
Gizmos.matrix = Matrix4x4.TRS(center, rotation, Vector3.one);
var half = height / 2 - radius;
//draw cylinder base
DrawWireCylinder(center, radius, height - radius * 2, rotation);
//draw upper cap
//do some cool stuff with orthogonal matrices
var mat = Matrix4x4.Translate(center + rotation * Vector3.up * half) *
Matrix4x4.Rotate(rotation * Quaternion.AngleAxis(90, Vector3.forward));
DrawWireArc(mat, radius, 180, 20);
mat = Matrix4x4.Translate(center + rotation * Vector3.up * half) * Matrix4x4.Rotate(
rotation * Quaternion.AngleAxis(90, Vector3.up) * Quaternion.AngleAxis(90, Vector3.forward));
DrawWireArc(mat, radius, 180, 20);
//draw lower cap
mat = Matrix4x4.Translate(center + rotation * Vector3.down * half) * Matrix4x4.Rotate(
rotation * Quaternion.AngleAxis(90, Vector3.up) * Quaternion.AngleAxis(-90, Vector3.forward));
DrawWireArc(mat, radius, 180, 20);
mat = Matrix4x4.Translate(center + rotation * Vector3.down * half) *
Matrix4x4.Rotate(rotation * Quaternion.AngleAxis(-90, Vector3.forward));
DrawWireArc(mat, radius, 180, 20);
Gizmos.matrix = old;
}
#endregion
}
}