Skip to content

Commit 41ca695

Browse files
committed
Bug Fix for Missing Normals and Big GC Allocation on GUI
Plus new mesh info channels: position, normal, tangents. Plus bug fix regression from 1f5db8b
1 parent 1f5db8b commit 41ca695

2 files changed

Lines changed: 54 additions & 23 deletions

File tree

Assets/Plugins/MeshDebugger/Editor/MeshDebugger.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ void OnDisable()
7575

7676
void OnDestroy() {
7777
m_Gizmo.Dispose();
78-
if (m_Mesh)
79-
DestroyImmediate(m_Mesh);
78+
if (m_tempMesh)
79+
DestroyImmediate(m_tempMesh);
8080
}
8181

8282
void OnSelectionChange()
@@ -177,11 +177,11 @@ void OnSceneGUI(SceneView view)
177177
Color blue = Color.blue, green = Color.green, red = Color.red, cyan = Color.cyan;
178178
EachVert((i, vert) =>
179179
{
180-
if (m_DebugNormalVerts)
180+
if (m_DebugNormalVerts && m_cpu.m_NormalChannels >= 1)
181181
m_Gizmo.AddRay(vert, m_cpu.m_Normals[0][i] * m_RaySize, blue);
182-
if (m_DebugTangentVerts)
182+
if (m_DebugTangentVerts && m_cpu.m_NormalChannels >= 2)
183183
m_Gizmo.AddRay(vert, m_cpu.m_Normals[1][i] * m_RaySize, green);
184-
if (m_DebugBinormalVerts)
184+
if (m_DebugBinormalVerts && m_cpu.m_NormalChannels >= 3)
185185
m_Gizmo.AddRay(vert, m_cpu.m_Normals[2][i] * m_RaySize, red);
186186
if (m_DebugVertsToIndice)
187187
{
@@ -270,7 +270,7 @@ private bool IsSafeToDrawGUI()
270270
{
271271
return ((m_DebugTris == DebugTriangle.None ? 0 : m_cpu.m_IndiceCountNormalized) +
272272
(m_DebugVert == DebugVertice.None ? 0 : m_cpu.m_VertCount)) *
273-
(m_PartialDebug ? (m_PartialDebugEnd - m_PartialDebugStart) : 1) < 2500;
273+
(m_PartialDebug ? (m_PartialDebugEnd - m_PartialDebugStart) : 1) < Styles.GUILimit;
274274
}
275275

276276
private void DrawGUILabels()
@@ -281,7 +281,7 @@ private void DrawGUILabels()
281281
{
282282
case DebugTriangle.Index:
283283
EachIndice((i, j, vert) =>
284-
DrawLabel(vert, m_cpu.m_IndiceNormals[i][j], (j + m_cpu.m_IndiceOffsets[i]).ToString())
284+
DrawLabel(vert, m_cpu.m_IndiceNormals[i][j], (j + m_cpu.m_IndiceOffsets[i]))
285285
);
286286
break;
287287
case DebugTriangle.Area:
@@ -300,17 +300,17 @@ private void DrawGUILabels()
300300
{
301301
case DebugVertice.Index:
302302
EachVert((i, vert) =>
303-
DrawLabel(vert, m_cpu.m_Normals[0][i], i.ToString())
303+
DrawLabel(vert, m_cpu.m_Normals[0][i], i)
304304
);
305305
break;
306306
case DebugVertice.Shared:
307307
EachVert((i, vert) =>
308-
DrawLabel(vert, m_cpu.m_Normals[0][i], m_cpu.m_VertUsedCounts[i].ToString())
308+
DrawLabel(vert, m_cpu.m_Normals[0][i], m_cpu.m_VertUsedCounts[i])
309309
);
310310
break;
311311
case DebugVertice.Duplicates:
312312
foreach (var item in m_cpu.m_VertSimilars)
313-
DrawLabel(item.Key, item.Key, item.Value.ToString());
313+
DrawLabel(item.Key, item.Key, item.Value);
314314
break;
315315
default:
316316
break;
@@ -369,6 +369,11 @@ private bool IsFacingCamera(Vector3 pos, Vector3 normal)
369369

370370
private static GUIContent m_gui = new GUIContent();
371371

372+
private void DrawLabel(Vector3 pos, Vector3 normal, int number)
373+
{
374+
DrawLabel(pos, normal, number < Styles.GUILimit ? Styles.numbers[number] : number.ToString());
375+
}
376+
372377
private void DrawLabel(Vector3 pos, Vector3 normal, string text)
373378
{
374379
if (!m_DepthCulling || (IsFacingCamera(pos, normal)))
@@ -386,8 +391,17 @@ static public class Styles
386391
{
387392
static public GUIStyle blockLabel = new GUIStyle(EditorStyles.boldLabel);
388393

394+
static public string[] numbers;
395+
396+
public const int GUILimit = 2500;
397+
389398
static Styles()
390399
{
400+
numbers = new string[GUILimit];
401+
for (int i = 0; i < numbers.Length; i++)
402+
numbers[i] = i.ToString();
403+
404+
blockLabel.normal.textColor = Color.black;
391405
blockLabel.normal.background = EditorGUIUtility.whiteTexture;
392406
blockLabel.margin = new RectOffset();//2, 2, 1, 1);
393407
blockLabel.padding = new RectOffset();

Assets/Plugins/MeshDebugger/Editor/MeshInfo.cs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class MeshInfo
4545
// Normal = 0, Tangent = 1, Bitangent = 2
4646
public List<Vector3>[] m_Normals;
4747

48+
/// <summary>
49+
/// 3 = Normal + Tangents, 1 = Normal, 0 = None,
50+
/// </summary>
51+
public int m_NormalChannels = 0;
52+
4853
public List<float> m_NormalFlips;
4954

5055
public string m_Features;
@@ -83,12 +88,20 @@ public void Update()
8388
Set(ref m_Normals[0], m_Mesh.normals);
8489
Reset(ref m_NormalFlips);
8590
var tan = m_Mesh.tangents;
86-
for (int i = 0; i < m_Normals[0].Count; i++)
91+
if (tan.Length > 0)
8792
{
88-
m_Normals[1].Add(tan[i]);
89-
m_NormalFlips.Add(tan[i].w);
90-
m_Normals[2].Add(Vector3.Cross(m_Normals[0][i], m_Normals[1][i]) * m_NormalFlips[i]);
93+
for (int i = 0; i < m_Normals[0].Count; i++)
94+
{
95+
m_Normals[1].Add(tan[i]);
96+
m_NormalFlips.Add(tan[i].w);
97+
m_Normals[2].Add(Vector3.Cross(m_Normals[0][i], m_Normals[1][i]) * m_NormalFlips[i]);
98+
}
99+
m_NormalChannels = 3;
91100
}
101+
else if (m_Normals[0].Count > 0)
102+
m_NormalChannels = 1;
103+
else
104+
m_NormalChannels = 0;
92105
}
93106
{
94107
m_IndiceAreaMax = 0;
@@ -119,22 +132,26 @@ public void Update()
119132
case 1:
120133
a = indice[m];
121134
m_IndiceMedians[i].Add(m_Verts[a]);
122-
m_IndiceNormals[i].Add(normal[a]);
135+
if (m_NormalChannels > 0)
136+
m_IndiceNormals[i].Add(normal[a]);
123137
m_IndiceAreas[i].Add(0); break;
124138
case 2:
125139
a = indice[m]; b = indice[m + 1];
126140
m_IndiceMedians[i].Add((m_Verts[a] + m_Verts[b]) / 2);
127-
m_IndiceNormals[i].Add((normal[a] + normal[b]).normalized);
141+
if (m_NormalChannels > 0)
142+
m_IndiceNormals[i].Add((normal[a] + normal[b]).normalized);
128143
m_IndiceAreas[i].Add((m_Verts[a] + m_Verts[b]).magnitude); break;
129144
case 3:
130145
a = indice[m]; b = indice[m + 1]; c = indice[m + 2];
131146
m_IndiceMedians[i].Add((m_Verts[a] + m_Verts[b] + m_Verts[c]) / 3);
132-
m_IndiceNormals[i].Add((normal[a] + normal[b] + normal[c]).normalized);
147+
if (m_NormalChannels > 0)
148+
m_IndiceNormals[i].Add((normal[a] + normal[b] + normal[c]).normalized);
133149
m_IndiceAreas[i].Add(GetTriArea(m_Verts[a], m_Verts[b], m_Verts[c])); break;
134150
case 4:
135151
a = indice[m]; b = indice[m + 1]; c = indice[m + 2]; d = indice[m + 3];
136152
m_IndiceMedians[i].Add((m_Verts[a] + m_Verts[b] + m_Verts[c] + m_Verts[d]) / 4);
137-
m_IndiceNormals[i].Add((normal[a] + normal[b] + normal[c] + normal[d]).normalized);
153+
if (m_NormalChannels > 0)
154+
m_IndiceNormals[i].Add((normal[a] + normal[b] + normal[c] + normal[d]).normalized);
138155
m_IndiceAreas[i].Add(GetTriArea(m_Verts[a], m_Verts[b], m_Verts[c]) +
139156
GetTriArea(m_Verts[d], m_Verts[b], m_Verts[c])); break;
140157
}
@@ -180,16 +197,16 @@ public void Update()
180197
m_VertToIndicesDir[idx] = new List<int>();
181198
}
182199
m_VertToIndicesDir[idx].Add(m_IndiceOffsets[i] + j);
183-
m_VertUsedCountMax = Mathf.Max(m_VertUsedCountMax, ++m_VertUsedCounts[idx]);
200+
m_VertUsedCountMax = Mathf.Max(m_VertUsedCountMax, ++m_VertUsedCounts[idx]);
184201
}
185202
}
186203
}
187204
{
188205
m_Features =
189206
"Vertices: " + m_VertCount + " total, " + (m_VertOrphan > 0 ? m_VertOrphan + " orphan, " : "") + (m_VertDuplicates > 0 ? m_VertDuplicates + " duplicates, " : "") +
190-
"\nIndices: " + m_IndiceCountNormalized + " total, " + m_IndiceCount + " buffer capacity, " + m_IndiceAreaTotal.ToString("0.##") + " unit surface area, " +
191-
(m_MeshSubmeshCount > 1 ? m_MeshSubmeshCount + " submeshes, " : "") + (m_IndiceInvalidArea > 0 ? m_IndiceInvalidArea + " invalid, " : "") +
192-
"\nChannels: " + InternalMeshUtil.GetVertexFormat(m_Mesh) +
207+
"\nIndices: " + m_IndiceCountNormalized + " total, " + m_IndiceCount + " buffer capacity, " + m_IndiceAreaTotal.ToString("0.##") + " unit surface area, " +
208+
(m_MeshSubmeshCount > 1 ? m_MeshSubmeshCount + " submeshes, " : "") + (m_IndiceInvalidArea > 0 ? m_IndiceInvalidArea + " invalid, " : "") +
209+
"\nChannels: position," + (m_NormalChannels >= 1 ? "normals," + ( m_NormalChannels >= 3 ? "tangents," : "") : "") + InternalMeshUtil.GetVertexFormat(m_Mesh) +
193210
"\nSize: " + m_MeshBounds.size.ToString("0.00");
194211
}
195212
m_lastMeshId = m_Mesh.GetInstanceID();
@@ -276,7 +293,7 @@ private static void Set<T>(ref List<T> list, T[] array)
276293

277294
public void UnpackTriangleIdx(int src, out int submesh, out int localidx)
278295
{
279-
for (int i = m_MeshSubmeshCount; i --> 0;)
296+
for (int i = m_MeshSubmeshCount; i-- > 0;)
280297
{
281298
if (i > 0 && m_IndiceOffsets[i] > src)
282299
continue;

0 commit comments

Comments
 (0)