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
|
#include "UnityPrefix.h"
#if UNITY_EDITOR
#include "DebugUtility.h"
#include "Editor/Src/Gizmos/GizmoUtil.h"
#include "Runtime/Math/Color.h"
#include "Runtime/Math/Vector3.h"
#include "Runtime/Input/TimeManager.h"
#include "Runtime/BaseClasses/IsPlaying.h"
#include "Editor/Src/Gizmos/GizmoManager.h"
#include "Editor/Src/Gizmos/GizmoRenderer.h"
#include <vector>
#include "Runtime/Utilities/InitializeAndCleanup.h"
using namespace std;
class DebugLineRenderer
{
public:
void AddLine (const Vector3f& v0, const Vector3f& v1, const ColorRGBAf& color, double durationSeconds, bool depthTest)
{
// We do not support timed debug lines in edit mode
if (!IsWorldPlaying ())
durationSeconds = 0.0;
Line l;
l.start = v0;
l.end = v1;
l.color = color;
l.depthTest = depthTest;
if (durationSeconds > 0.0)
{
// We use game time (CurTime) to be able to pause game and investigate debug lines.
l.removeTime = GetTimeManager ().GetCurTime () + durationSeconds;
m_TimedLines.push_back (l);
}
else
{
l.removeTime = -1.0;
if (GetTimeManager ().IsUsingFixedTimeStep ())
m_FixedStepLines.push_back (l);
else
m_DynamicStepLines.push_back (l);
}
}
void Clear ()
{
if (GetTimeManager ().IsUsingFixedTimeStep ())
{
m_FixedStepLines.clear ();
}
else
{
m_DynamicStepLines.clear ();
if (!m_TimedLines.empty())
{
float curTime = GetTimeManager ().GetCurTime ();
list<Line>::iterator it = m_TimedLines.begin();
while (it != m_TimedLines.end())
{
if (curTime >= it->removeTime)
it = m_TimedLines.erase (it);
else
it++;
}
}
}
}
void ClearAll ()
{
m_DynamicStepLines.clear ();
m_FixedStepLines.clear ();
m_TimedLines.clear ();
}
void Draw () const
{
for (unsigned i=0; i<m_FixedStepLines.size(); ++i)
{
const Line& line = m_FixedStepLines[i];
gizmos::g_GizmoColor = line.color;
DrawLine(line.start, line.end, line.depthTest);
}
for (unsigned i=0; i<m_DynamicStepLines.size(); ++i)
{
const Line& line = m_DynamicStepLines[i];
gizmos::g_GizmoColor = line.color;
DrawLine(line.start, line.end, line.depthTest);
}
for (list<Line>::const_iterator it = m_TimedLines.begin(); it != m_TimedLines.end(); it++)
{
const Line& line = *it;
gizmos::g_GizmoColor = line.color;
DrawLine(line.start, line.end, line.depthTest);
}
}
static DebugLineRenderer& Get ();
static void StaticDestroy();
private:
struct Line
{
Vector3f start;
Vector3f end;
ColorRGBAf color;
double removeTime;
bool depthTest;
};
list<Line> m_TimedLines;
vector<Line> m_FixedStepLines;
vector<Line> m_DynamicStepLines;
};
static RegisterRuntimeInitializeAndCleanup gDebugLineRendererInstance(NULL, DebugLineRenderer::StaticDestroy);
static DebugLineRenderer* g_DebugLineRenderer = NULL;
void DebugLineRenderer::StaticDestroy()
{
if(g_DebugLineRenderer)
UNITY_DELETE(g_DebugLineRenderer, kMemEditorUtility);
}
DebugLineRenderer& DebugLineRenderer::Get ()
{
if(g_DebugLineRenderer == NULL)
g_DebugLineRenderer = UNITY_NEW_AS_ROOT(DebugLineRenderer,kMemEditorUtility, "Manager", "DebugLineRenderer");
return *g_DebugLineRenderer;
}
void ClearLines ()
{
DebugLineRenderer::Get ().Clear ();
}
void ClearAllLines ()
{
DebugLineRenderer::Get ().ClearAll ();
}
void DrawDebugLinesGizmo ()
{
DebugLineRenderer::Get ().Draw ();
}
void DebugDrawLine (const Vector3f& p0, const Vector3f& p1, const ColorRGBAf& color, double durationSeconds /*=0.0*/, bool depthTest /*=true*/)
{
DebugLineRenderer::Get ().AddLine (p0, p1, color, durationSeconds, depthTest);
}
#endif
|