-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDraw.cs
491 lines (399 loc) · 23 KB
/
Draw.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
using UnityEngine;
namespace IMGUIDebugDraw
{
public class Draw
{
public static GUIStyle StringStyle { get; set; } = new GUIStyle(GUI.skin.label);
public static void Label(Vector2 position, string label, Color color, bool centered = true)
{
StringStyle.normal.textColor = color;
Label(position, label, centered);
}
public static void Label(Vector2 position, string label, bool centered = true)
{
GUIContent content = new GUIContent(label);
Vector2 size = StringStyle.CalcSize(content);
GUI.Label(new Rect(centered ? (position - size / 2f) : position, size), content, StringStyle);
}
public static void Label(Camera cam, Vector3 worldPos, string label, Color color)
{
if (!IsVisible(cam, worldPos)) return;
Vector2 screenPos = WorldToScreen(cam, worldPos);
Label(screenPos, label, color);
}
public static void Label(Camera cam, Vector3 worldPos, string label)
{
if (!IsVisible(cam, worldPos)) return;
Vector2 screenPos = WorldToScreen(cam, worldPos);
Label(screenPos, label);
}
public static void Crosshair(Vector2 position, float size, Color color, float thickness = 1f)
{
GUI.color = color;
Texture2D whiteTexture = Texture2D.whiteTexture;
GUI.DrawTexture(new Rect(position.x - size / 2f, position.y - thickness / 2f, size, thickness), whiteTexture);
GUI.DrawTexture(new Rect(position.x - thickness / 2f, position.y - size / 2f, thickness, size), whiteTexture);
}
public static void Crosshair(float size, Color color, float thickness = 1f)
{
Crosshair(new Vector2(Screen.width / 2f, Screen.height / 2f), size, color, thickness);
}
public static void Box(float x, float y, float w, float h, float thickness, Color color)
{
GUI.color = color;
Texture2D whiteTexture = Texture2D.whiteTexture;
GUI.DrawTexture(new Rect(x, y, w + thickness, thickness), whiteTexture);
GUI.DrawTexture(new Rect(x, y + h, w + thickness, thickness), whiteTexture);
GUI.DrawTexture(new Rect(x, y + thickness, thickness, h - thickness), whiteTexture);
GUI.DrawTexture(new Rect(x + w, y + thickness, thickness, h - thickness), whiteTexture);
}
public static void Line(Vector2 lineStart, Vector2 lineEnd, float thickness, Color color)
{
if (lineStart == lineEnd) return;
GUI.color = color;
Vector2 vector = lineEnd - lineStart;
float angle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
if (thickness < 1f)
{
thickness = 1f;
}
float halfThickness = thickness / 2f;
Matrix4x4 matrix = GUI.matrix;
GUIUtility.RotateAroundPivot(angle, lineStart);
GUI.DrawTexture(new Rect(lineStart.x, lineStart.y - halfThickness, vector.magnitude, thickness), Texture2D.whiteTexture, ScaleMode.StretchToFill);
GUI.matrix = matrix;
}
public static void Circle(Vector2 center, float radius, Color color, float width, int segmentsPerQuarter = 10)
{
segmentsPerQuarter = Mathf.Max(1, segmentsPerQuarter);
Vector2 top = new Vector2(center.x, center.y - radius);
Vector2 right = new Vector2(center.x + radius, center.y);
Vector2 bottom = new Vector2(center.x, center.y + radius);
Vector2 left = new Vector2(center.x - radius, center.y);
float tangentLength = radius * 0.55228f;
Draw.BezierLine(top, top + Vector2.right * tangentLength, right, right + Vector2.down * tangentLength, color, width, segmentsPerQuarter);
Draw.BezierLine(right, right + Vector2.up * tangentLength, bottom, bottom + Vector2.left * tangentLength, color, width, segmentsPerQuarter);
Draw.BezierLine(bottom, bottom + Vector2.right * tangentLength, left, left + Vector2.down * tangentLength, color, width, segmentsPerQuarter);
Draw.BezierLine(left, left + Vector2.up * tangentLength, top, top + Vector2.left * tangentLength, color, width, segmentsPerQuarter);
}
public static void BezierLine(Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color color, float width, int segments)
{
Vector2 lineStart = Draw.EvaluateCubicBezier(start, startTangent, end, endTangent, 0f);
for (int i = 1; i <= segments; i++)
{
float t = (float)i / segments;
Vector2 lineEnd = Draw.EvaluateCubicBezier(start, startTangent, end, endTangent, t);
Draw.Line(lineStart, lineEnd, width, color);
lineStart = lineEnd;
}
}
private static Vector2 EvaluateCubicBezier(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t)
{
float u = 1f - t;
float tt = t * t;
float uu = u * u;
float uuu = uu * u;
float ttt = tt * t;
Vector2 p = uuu * p0; // (1-t)^3 * p0
p += 3f * uu * t * p1; // 3 * (1-t)^2 * t * p1
p += 3f * u * tt * p2; // 3 * (1-t) * t^2 * p2
p += ttt * p3; // t^3 * p3
return p;
}
private static bool IsVisible(Camera camera, Vector3 worldPos)
{
if (camera == null)
{
Debug.LogError("IMGUIDebugDraw: Camera is null!");
return false;
}
Vector3 viewportPoint = camera.WorldToViewportPoint(worldPos);
return viewportPoint.z > camera.nearClipPlane &&
viewportPoint.x >= 0 && viewportPoint.x <= 1 &&
viewportPoint.y >= 0 && viewportPoint.y <= 1;
}
private static Vector2 WorldToScreen(Camera camera, Vector3 worldPos)
{
if (camera == null)
{
Debug.LogError("IMGUIDebugDraw: Camera is null! Cannot convert WorldToScreen.");
return Vector2.zero;
}
Vector3 screenPoint = camera.WorldToScreenPoint(worldPos);
screenPoint.y = Screen.height - screenPoint.y;
return new Vector2(screenPoint.x, screenPoint.y);
}
public static void Bounds(Camera cam, Bounds bounds, Color color)
{
Vector3 center = bounds.center;
Vector3 extents = bounds.extents;
Vector3[] corners = new Vector3[8];
corners[0] = center + new Vector3(extents.x, extents.y, extents.z);
corners[1] = center + new Vector3(extents.x, extents.y, -extents.z);
corners[2] = center + new Vector3(extents.x, -extents.y, -extents.z);
corners[3] = center + new Vector3(extents.x, -extents.y, extents.z);
corners[4] = center + new Vector3(-extents.x, extents.y, extents.z);
corners[5] = center + new Vector3(-extents.x, extents.y, -extents.z);
corners[6] = center + new Vector3(-extents.x, -extents.y, -extents.z);
corners[7] = center + new Vector3(-extents.x, -extents.y, extents.z);
float thickness = 1.5f;
DrawEdge(cam, corners[0], corners[1], thickness, color);
DrawEdge(cam, corners[1], corners[2], thickness, color);
DrawEdge(cam, corners[2], corners[3], thickness, color);
DrawEdge(cam, corners[3], corners[0], thickness, color);
DrawEdge(cam, corners[4], corners[5], thickness, color);
DrawEdge(cam, corners[5], corners[6], thickness, color);
DrawEdge(cam, corners[6], corners[7], thickness, color);
DrawEdge(cam, corners[7], corners[4], thickness, color);
DrawEdge(cam, corners[0], corners[4], thickness, color);
DrawEdge(cam, corners[1], corners[5], thickness, color);
DrawEdge(cam, corners[2], corners[6], thickness, color);
DrawEdge(cam, corners[3], corners[7], thickness, color);
}
public static void BoxCollider(Camera cam, BoxCollider boxCollider, Color color)
{
if (boxCollider == null) return;
Transform transform = boxCollider.transform;
Vector3 center = boxCollider.center;
Vector3 size = boxCollider.size;
Vector3 halfExtents = size / 2f;
Vector3 scale = transform.lossyScale;
halfExtents.x *= scale.x;
halfExtents.y *= scale.y;
halfExtents.z *= scale.z;
Vector3 worldCenter = transform.TransformPoint(center);
Quaternion worldRotation = transform.rotation;
Vector3[] corners = new Vector3[8];
corners[0] = worldCenter + worldRotation * new Vector3(halfExtents.x, halfExtents.y, halfExtents.z);
corners[1] = worldCenter + worldRotation * new Vector3(halfExtents.x, halfExtents.y, -halfExtents.z);
corners[2] = worldCenter + worldRotation * new Vector3(halfExtents.x, -halfExtents.y, -halfExtents.z);
corners[3] = worldCenter + worldRotation * new Vector3(halfExtents.x, -halfExtents.y, halfExtents.z);
corners[4] = worldCenter + worldRotation * new Vector3(-halfExtents.x, halfExtents.y, halfExtents.z);
corners[5] = worldCenter + worldRotation * new Vector3(-halfExtents.x, halfExtents.y, -halfExtents.z);
corners[6] = worldCenter + worldRotation * new Vector3(-halfExtents.x, -halfExtents.y, -halfExtents.z);
corners[7] = worldCenter + worldRotation * new Vector3(-halfExtents.x, -halfExtents.y, halfExtents.z);
float thickness = 1.5f;
DrawEdge(cam, corners[0], corners[1], thickness, color);
DrawEdge(cam, corners[1], corners[2], thickness, color);
DrawEdge(cam, corners[2], corners[3], thickness, color);
DrawEdge(cam, corners[3], corners[0], thickness, color);
DrawEdge(cam, corners[4], corners[5], thickness, color);
DrawEdge(cam, corners[5], corners[6], thickness, color);
DrawEdge(cam, corners[6], corners[7], thickness, color);
DrawEdge(cam, corners[7], corners[4], thickness, color);
DrawEdge(cam, corners[0], corners[4], thickness, color);
DrawEdge(cam, corners[1], corners[5], thickness, color);
DrawEdge(cam, corners[2], corners[6], thickness, color);
DrawEdge(cam, corners[3], corners[7], thickness, color);
}
private static void DrawEdge(Camera cam, Vector3 worldStart, Vector3 worldEnd, float thickness, Color color)
{
if (cam == null) return;
Vector3 viewStart = cam.WorldToViewportPoint(worldStart);
Vector3 viewEnd = cam.WorldToViewportPoint(worldEnd);
if (viewStart.z > cam.nearClipPlane - 0.01f && viewEnd.z > cam.nearClipPlane - 0.01f)
{
bool startIn = viewStart.x >= -0.1f && viewStart.x <= 1.1f && viewStart.y >= -0.1f && viewStart.y <= 1.1f;
bool endIn = viewEnd.x >= -0.1f && viewEnd.x <= 1.1f && viewEnd.y >= -0.1f && viewEnd.y <= 1.1f;
if (startIn || endIn)
{
Vector2 screenStart = WorldToScreen(cam, worldStart);
Vector2 screenEnd = WorldToScreen(cam, worldEnd);
Line(screenStart, screenEnd, thickness, color);
}
}
}
public static void PlainAxes(Camera camera, Vector3 worldPos, Color color)
{
// draws 3d axis lines on world point:
// |
// |/
// -----·-----
// /|
// |
float size = 0.2f;
float thickness = 1.5f;
Vector2 letterOffset = new Vector2(0, -10);
float labelVisibilityThreshold = 0.1f;
Vector3 xEnd = worldPos + Vector3.right * size;
Vector3 yEnd = worldPos + Vector3.up * size;
Vector3 zEnd = worldPos + Vector3.forward * size;
DrawEdge(camera, worldPos - Vector3.right * size, xEnd, thickness, color);
DrawEdge(camera, worldPos - Vector3.up * size, yEnd, thickness, color);
DrawEdge(camera, worldPos - Vector3.forward * size, zEnd, thickness, color);
// Draw Labels only if endpoint is visible and not too close to screen edge
if (camera != null)
{
Vector3 viewX = camera.WorldToViewportPoint(xEnd);
if (viewX.z > camera.nearClipPlane && viewX.x > labelVisibilityThreshold && viewX.x < 1 - labelVisibilityThreshold && viewX.y > labelVisibilityThreshold && viewX.y < 1 - labelVisibilityThreshold)
{
Label(WorldToScreen(camera, xEnd) + letterOffset, "x", color);
}
Vector3 viewY = camera.WorldToViewportPoint(yEnd);
if (viewY.z > camera.nearClipPlane && viewY.x > labelVisibilityThreshold && viewY.x < 1 - labelVisibilityThreshold && viewY.y > labelVisibilityThreshold && viewY.y < 1 - labelVisibilityThreshold)
{
Label(WorldToScreen(camera, yEnd) + letterOffset, "y", color);
}
Vector3 viewZ = camera.WorldToViewportPoint(zEnd);
if (viewZ.z > camera.nearClipPlane && viewZ.x > labelVisibilityThreshold && viewZ.x < 1 - labelVisibilityThreshold && viewZ.y > labelVisibilityThreshold && viewZ.y < 1 - labelVisibilityThreshold)
{
Label(WorldToScreen(camera, zEnd) + letterOffset, "z", color);
}
}
}
public static void SphereCollider(Camera cam, SphereCollider sphereCollider, Color color, int segments = 24)
{
if (sphereCollider == null || cam == null) return;
Transform transform = sphereCollider.transform;
Vector3 center = transform.TransformPoint(sphereCollider.center);
Vector3 scale = transform.lossyScale;
float maxScale = Mathf.Max(Mathf.Abs(scale.x), Mathf.Abs(scale.y), Mathf.Abs(scale.z));
float radius = sphereCollider.radius * maxScale;
float thickness = 1.5f;
DrawWorldCircle(cam, center, transform.right, transform.up, radius, color, thickness, segments);
DrawWorldCircle(cam, center, transform.up, transform.forward, radius, color, thickness, segments);
DrawWorldCircle(cam, center, transform.forward, transform.right, radius, color, thickness, segments);
}
public static void CapsuleCollider(Camera cam, CapsuleCollider capsuleCollider, Color color, int segments = 24)
{
if (capsuleCollider == null || cam == null) return;
if (segments < 4) segments = 4; // Need at least 4 for minimal shape
Transform transform = capsuleCollider.transform;
Vector3 localCenter = capsuleCollider.center;
float localRadius = capsuleCollider.radius;
float localHeight = capsuleCollider.height;
int direction = capsuleCollider.direction; // 0: X, 1: Y, 2: Z
Vector3 worldCenter = transform.TransformPoint(localCenter);
Vector3 lossyScale = transform.lossyScale;
Quaternion worldRotation = transform.rotation;
// Determine world axes based on direction
Vector3 worldUpAxis, worldRightAxis, worldForwardAxis;
float scaleAlongAxis, radiusScaleX, radiusScaleZ;
switch (direction)
{
case 0: // X-axis
worldUpAxis = worldRotation * Vector3.right;
worldRightAxis = worldRotation * Vector3.up; // Perpendicular 1
worldForwardAxis = worldRotation * Vector3.forward; // Perpendicular 2
scaleAlongAxis = Mathf.Abs(lossyScale.x);
radiusScaleX = Mathf.Abs(lossyScale.y);
radiusScaleZ = Mathf.Abs(lossyScale.z);
break;
case 2: // Z-axis
worldUpAxis = worldRotation * Vector3.forward;
worldRightAxis = worldRotation * Vector3.right; // Perpendicular 1
worldForwardAxis = worldRotation * Vector3.up; // Perpendicular 2
scaleAlongAxis = Mathf.Abs(lossyScale.z);
radiusScaleX = Mathf.Abs(lossyScale.x);
radiusScaleZ = Mathf.Abs(lossyScale.y);
break;
default: // 1: Y-axis (Default)
worldUpAxis = worldRotation * Vector3.up;
worldRightAxis = worldRotation * Vector3.right; // Perpendicular 1
worldForwardAxis = worldRotation * Vector3.forward; // Perpendicular 2
scaleAlongAxis = Mathf.Abs(lossyScale.y);
radiusScaleX = Mathf.Abs(lossyScale.x);
radiusScaleZ = Mathf.Abs(lossyScale.z);
break;
}
// Calculate scaled radius and height
float radius = localRadius * Mathf.Max(radiusScaleX, radiusScaleZ); // Use max perpendicular scale for radius
float height = localHeight * scaleAlongAxis;
// Clamp height to be at least 2 * radius (a sphere)
height = Mathf.Max(height, radius * 2f);
float cylinderHeight = height - 2f * radius; // Height of the cylinder part
float halfCylinderHeight = cylinderHeight * 0.5f;
// Calculate centers of the two hemispheres
Vector3 topSphereCenter = worldCenter + worldUpAxis * halfCylinderHeight;
Vector3 bottomSphereCenter = worldCenter - worldUpAxis * halfCylinderHeight;
float thickness = 1.5f;
// Draw Hemispheres (Top and Bottom Caps)
// Draw equator circles
DrawWorldCircle(cam, topSphereCenter, worldRightAxis, worldForwardAxis, radius, color, thickness, segments);
DrawWorldCircle(cam, bottomSphereCenter, worldRightAxis, worldForwardAxis, radius, color, thickness, segments);
// Draw arcs for the caps (half circles along the main axis)
int arcSegments = segments / 2; // Half the segments for a half circle
DrawWorldArc(cam, topSphereCenter, worldRightAxis, worldUpAxis, radius, 0f, Mathf.PI, color, thickness, arcSegments); // Top Arc 1 (Right/Up plane)
DrawWorldArc(cam, topSphereCenter, worldForwardAxis, worldUpAxis, radius, 0f, Mathf.PI, color, thickness, arcSegments); // Top Arc 2 (Forward/Up plane)
DrawWorldArc(cam, bottomSphereCenter, worldRightAxis, -worldUpAxis, radius, 0f, Mathf.PI, color, thickness, arcSegments); // Bottom Arc 1 (Right/Down plane)
DrawWorldArc(cam, bottomSphereCenter, worldForwardAxis, -worldUpAxis, radius, 0f, Mathf.PI, color, thickness, arcSegments); // Bottom Arc 2 (Forward/Down plane)
// Draw Cylinder Sides (Connecting Lines)
if (cylinderHeight > 0.001f) // Only draw if there's a cylindrical part
{
Vector3 topPointR = topSphereCenter + worldRightAxis * radius;
Vector3 topPointL = topSphereCenter - worldRightAxis * radius;
Vector3 topPointF = topSphereCenter + worldForwardAxis * radius;
Vector3 topPointB = topSphereCenter - worldForwardAxis * radius;
Vector3 bottomPointR = bottomSphereCenter + worldRightAxis * radius;
Vector3 bottomPointL = bottomSphereCenter - worldRightAxis * radius;
Vector3 bottomPointF = bottomSphereCenter + worldForwardAxis * radius;
Vector3 bottomPointB = bottomSphereCenter - worldForwardAxis * radius;
DrawEdge(cam, topPointR, bottomPointR, thickness, color);
DrawEdge(cam, topPointL, bottomPointL, thickness, color);
DrawEdge(cam, topPointF, bottomPointF, thickness, color);
DrawEdge(cam, topPointB, bottomPointB, thickness, color);
}
}
public static void Mesh(Camera cam, Transform transform, Mesh mesh, Color color)
{
if (mesh == null || !mesh.isReadable || transform == null || cam == null)
return;
Vector3[] vertices = mesh.vertices;
int[] triangles = mesh.triangles;
if (vertices == null || triangles == null || vertices.Length == 0 || triangles.Length == 0)
{
return;
}
float thickness = 1.0f;
for (int i = 0; i < triangles.Length; i += 3)
{
if (i + 2 >= triangles.Length)
break;
int index1 = triangles[i];
int index2 = triangles[i + 1];
int index3 = triangles[i + 2];
if (index1 >= vertices.Length || index2 >= vertices.Length || index3 >= vertices.Length)
{
continue;
}
Vector3 localP1 = vertices[index1];
Vector3 localP2 = vertices[index2];
Vector3 localP3 = vertices[index3];
Vector3 worldP1 = transform.TransformPoint(localP1);
Vector3 worldP2 = transform.TransformPoint(localP2);
Vector3 worldP3 = transform.TransformPoint(localP3);
DrawEdge(cam, worldP1, worldP2, thickness, color);
DrawEdge(cam, worldP2, worldP3, thickness, color);
DrawEdge(cam, worldP3, worldP1, thickness, color);
}
}
private static void DrawWorldCircle(Camera cam, Vector3 center, Vector3 axis1, Vector3 axis2, float radius, Color color, float thickness, int segments)
{
if (segments <= 0) return;
Vector3 lastWorldPos = center + axis1 * radius;
for (int i = 1; i <= segments; i++)
{
float angle = (float)i / segments * 2f * Mathf.PI;
Vector3 currentWorldPos = center + (Mathf.Cos(angle) * axis1 + Mathf.Sin(angle) * axis2) * radius;
DrawEdge(cam, lastWorldPos, currentWorldPos, thickness, color);
lastWorldPos = currentWorldPos;
}
}
private static void DrawWorldArc(Camera cam, Vector3 center, Vector3 axis1, Vector3 axis2, float radius, float startAngle, float endAngle, Color color, float thickness, int segments)
{
if (segments <= 0) return;
if (radius <= 0) return;
float angleRange = endAngle - startAngle;
if (Mathf.Approximately(angleRange, 0f)) return;
Vector3 lastWorldPos = center + (Mathf.Cos(startAngle) * axis1 + Mathf.Sin(startAngle) * axis2) * radius;
float angleStep = angleRange / segments;
for (int i = 1; i <= segments; i++)
{
float angle = startAngle + i * angleStep;
Vector3 currentWorldPos = center + (Mathf.Cos(angle) * axis1 + Mathf.Sin(angle) * axis2) * radius;
DrawEdge(cam, lastWorldPos, currentWorldPos, thickness, color);
lastWorldPos = currentWorldPos;
}
}
}
}