Skip to content

Commit 9bfc443

Browse files
committed
Add New Feature: Debug Surface
Plus tooltip improvements
1 parent 41ca695 commit 9bfc443

2 files changed

Lines changed: 157 additions & 18 deletions

File tree

Assets/Plugins/MeshDebugger/Editor/MeshDebugger.cs

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using UnityEditor;
34
using UnityEngine;
45
using UnityEngine.Rendering;
@@ -28,10 +29,16 @@ public partial class MeshDebugger : EditorWindow, IHasCustomMenu
2829

2930
public enum DebugTriangle { None, Index, Area, Submesh }
3031
public enum DebugVertice { None, Index, Shared, Duplicates }
32+
public enum DebugSurface { None, Color, Facing, UV, Tangents }
33+
public enum DebugSurfaceUV { UV = 1, UV2 = 2, UV3 = 3, UV4 = 4 }
34+
public enum DebugSurfaceTangents { Normal = 1, Tangent = 2, Bitangent = 3, WorldNormal = 4, WorldTangent = 5, WorldBitangent = 6 }
3135

3236
[Space]
3337
public DebugTriangle m_DebugTris;
3438
public DebugVertice m_DebugVert;
39+
public DebugSurface m_DebugSurface;
40+
public DebugSurfaceUV m_DebugSurfaceUV = DebugSurfaceUV.UV;
41+
public DebugSurfaceTangents m_DebugSurfaceTangents = DebugSurfaceTangents.Normal;
3542
public bool m_UseHeatmap;
3643
public float m_HeatSize = .1f;
3744

@@ -41,6 +48,7 @@ public enum DebugVertice { None, Index, Shared, Duplicates }
4148
private Matrix4x4 m_matrix;
4249
private MeshInfo m_cpu = new MeshInfo();
4350
private Mesh m_tempMesh;
51+
private Material m_tempMat;
4452

4553
private bool m_hasUpdated = false;
4654

@@ -73,26 +81,79 @@ void OnDisable()
7381
}
7482
}
7583

76-
void OnDestroy() {
84+
void OnDestroy()
85+
{
7786
m_Gizmo.Dispose();
7887
if (m_tempMesh)
7988
DestroyImmediate(m_tempMesh);
89+
if (m_tempMat)
90+
DestroyImmediate(m_tempMat);
8091
}
8192

82-
void OnSelectionChange()
93+
Material[] m_backupMats;
94+
bool m_matModificationBreaksPrefab = false;
95+
96+
void ChangeMaterial(Material mat)
8397
{
84-
if (m_Transform && m_Transform.GetComponent<MeshDebuggerProxyUI>())
98+
MeshRenderer r;
99+
if (m_Transform && (r = m_Transform.GetComponent<MeshRenderer>()))
100+
{
101+
if (mat)
102+
{
103+
if (m_backupMats == null)
104+
{
105+
m_backupMats = r.sharedMaterials;
106+
m_matModificationBreaksPrefab = (PrefabUtility.GetPrefabType(r) > PrefabType.ModelPrefab
107+
&& PrefabUtility.GetPropertyModifications(r).FirstOrDefault(x => x.propertyPath.StartsWith("m_Materials")) == null);
108+
r.sharedMaterials = Enumerable.Repeat(mat, m_backupMats.Length).ToArray();
109+
}
110+
mat.SetInt(Styles.UV_Mode, (int)m_DebugSurfaceUV);
111+
mat.SetInt(Styles.Tan_Mode, (int)m_DebugSurfaceTangents);
112+
}
113+
else if (m_backupMats != null)
114+
{
115+
if (m_matModificationBreaksPrefab)
116+
{
117+
var modifs = PrefabUtility.GetPropertyModifications(r);
118+
modifs = modifs.Where(x => !x.propertyPath.StartsWith("m_Materials")).ToArray();
119+
PrefabUtility.SetPropertyModifications(r, modifs);
120+
}
121+
else
122+
r.sharedMaterials = m_backupMats;
123+
m_backupMats = null;
124+
}
125+
}
126+
}
127+
128+
void RestoreDefault()
129+
{
130+
if (m_Transform.GetComponent<MeshDebuggerProxyUI>())
85131
{
86132
DestroyImmediate(m_Transform.GetComponent<MeshDebuggerProxyUI>());
87133
}
134+
if (m_backupMats != null && m_backupMats.Length > 0)
135+
{
136+
ChangeMaterial(null);
137+
}
138+
}
139+
140+
void OnSelectionChange()
141+
{
142+
if (m_Transform)
143+
RestoreDefault();
88144

89145
m_Transform = Selection.activeTransform;
90146
if (m_Transform)
91147
{
92148
var m = m_Transform.GetComponent<MeshFilter>();
93149
var m2 = m_Transform.GetComponent<Graphic>();
94150
var m3 = m_Transform.GetComponent<SkinnedMeshRenderer>();
95-
if (m) m_Mesh = m.sharedMesh;
151+
if (m)
152+
{
153+
m_Mesh = m.sharedMesh;
154+
if (m_DebugSurface != DebugSurface.None)
155+
ChangeMaterial(m_tempMat);
156+
}
96157
else if (m2)
97158
{
98159
var m4 = m_Transform.gameObject.AddComponent<MeshDebuggerProxyUI>();
@@ -114,7 +175,7 @@ void OnSelectionChange()
114175
{
115176
m_tempMesh.Clear();
116177
}
117-
178+
118179
m3.BakeMesh(m_Mesh = m_tempMesh);
119180
m_Mesh.name = m3.sharedMesh.name + " (Snapshot)";
120181
}
@@ -395,6 +456,10 @@ static public class Styles
395456

396457
public const int GUILimit = 2500;
397458

459+
public static int UV_Mode = Shader.PropertyToID("UV_Mode");
460+
461+
public static int Tan_Mode = Shader.PropertyToID("Tan_Mode");
462+
398463
static Styles()
399464
{
400465
numbers = new string[GUILimit];

Assets/Plugins/MeshDebugger/Editor/MeshDebugger_GUI.cs

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static class UI
1515

1616
public static GUIContent Configuration = new GUIContent("Configuration", "Toggleable control");
1717
public static GUIContent Static = new GUIContent("Static", "Turn on to assume that the mesh won't change internally (ie. not procedural)");
18-
public static GUIContent DepthCulling = new GUIContent("Depth Culling", "Turn on to cull cues that behind the object");
18+
public static GUIContent DepthCulling = new GUIContent("Depth Culling", "Turn on to cull cues which behind the object");
1919
public static GUIContent Equalize = new GUIContent("Equalize", "Turn on to keep cues on scale no matter far it is\n(NOTE: does not work correctly if static is on)");
2020
public static GUIContent PartialDebug = new GUIContent("Partial Debug", "");
2121

@@ -26,19 +26,29 @@ public static class UI
2626
public static GUIContent Bitangent = new GUIContent("Bitangent", "Bitangent (cross of normal and tangent) vector of vertices");
2727

2828
public static GUIContent AdditionalRays = new GUIContent("Additional Rays", "");
29-
public static GUIContent VertsToIndice = new GUIContent("Vertex to Indice", "Incomplete");
30-
public static GUIContent TriangleNormal = new GUIContent("Triangle Normal", "");
29+
public static GUIContent VertsToIndice = new GUIContent("Vertex to Indice", "Ray from vertices to each triangles in median");
30+
public static GUIContent TriangleNormal = new GUIContent("Triangle Normal", "Median Normal vector of triangles");
3131

32-
public static GUIContent UseHeatmap = new GUIContent("Use Heatmap", "");
33-
public static GUIContent DebugVertices = new GUIContent("Debug Vertices", "");
34-
public static GUIContent DebugTriangles = new GUIContent("Debug Triangles", "");
32+
public static GUIContent UseHeatmap = new GUIContent("Use Heatmap", "Use Color indicator instead of GUI Labels");
33+
public static GUIContent DebugVertices = new GUIContent("Debug Vertices", "Debug Vertice Modes");
34+
public static GUIContent DebugTriangles = new GUIContent("Debug Triangles", "Debug Triangle Modes");
3535

36-
public static GUIContent None = new GUIContent("None", "");
37-
public static GUIContent Index = new GUIContent("Index", "");
38-
public static GUIContent Shared = new GUIContent("Shared", "");
39-
public static GUIContent Duplicates = new GUIContent("Duplicates", "");
40-
public static GUIContent Area = new GUIContent("Area", "");
41-
public static GUIContent Submesh = new GUIContent("Submesh", "");
36+
public static GUIContent DebugSurface = new GUIContent("Debug Surface", "Debug Surface Modes");
37+
public static GUIContent SurfaceUV = new GUIContent("Surface UV", "Surface UV Mode");
38+
public static GUIContent SurfaceTangent = new GUIContent("Surface Tangent", "Surface Tangent Mode");
39+
40+
public static GUIContent None = new GUIContent("None", "Not Activated");
41+
public static GUIContent Index = new GUIContent("Index", "Debug Index in Buffer");
42+
public static GUIContent Shared = new GUIContent("Shared", "Debug of how many triangles use the vertex");
43+
public static GUIContent Duplicates = new GUIContent("Duplicates", "Debug of how many vertices have the same position");
44+
public static GUIContent Area = new GUIContent("Area", "Debug Calculated area surface of each triangle");
45+
public static GUIContent Submesh = new GUIContent("Submesh", "Debug Submesh index of each triangle");
46+
47+
public static GUIContent Facing = new GUIContent("Facing");
48+
public static GUIContent Color = new GUIContent("Color");
49+
public static GUIContent UV = new GUIContent("UV");
50+
public static GUIContent Tangents = new GUIContent("Tangents");
51+
public static int[] Surfaces = new int[] { 0, 1, 2, 3 };
4252

4353
}
4454

@@ -119,6 +129,33 @@ void OnGUI()
119129
if (GUILayout.Toggle(m_DebugTris == DebugTriangle.Submesh, UI.Submesh, EditorStyles.miniButtonRight)) m_DebugTris = DebugTriangle.Submesh;
120130
EditorGUILayout.EndHorizontal();
121131
}
132+
{
133+
EditorGUI.BeginChangeCheck();
134+
EditorGUILayout.BeginHorizontal();
135+
EditorGUILayout.PrefixLabel(UI.DebugSurface);
136+
if (GUILayout.Toggle(m_DebugSurface == DebugSurface.None, UI.None, EditorStyles.miniButtonLeft)) m_DebugSurface = DebugSurface.None;
137+
if (GUILayout.Toggle(m_DebugSurface == DebugSurface.Facing, UI.Facing, EditorStyles.miniButtonMid)) m_DebugSurface = DebugSurface.Facing;
138+
if (GUILayout.Toggle(m_DebugSurface == DebugSurface.Color, UI.Color, EditorStyles.miniButtonMid)) m_DebugSurface = DebugSurface.Color;
139+
if (GUILayout.Toggle(m_DebugSurface == DebugSurface.UV, UI.UV, EditorStyles.miniButtonMid)) m_DebugSurface = DebugSurface.UV;
140+
if (GUILayout.Toggle(m_DebugSurface == DebugSurface.Tangents, UI.Tangents, EditorStyles.miniButtonRight)) m_DebugSurface = DebugSurface.Tangents;
141+
EditorGUILayout.EndHorizontal();
142+
if (m_DebugSurface == DebugSurface.UV)
143+
{
144+
EditorGUILayout.BeginHorizontal();
145+
EditorGUILayout.PrefixLabel(UI.SurfaceUV);
146+
m_DebugSurfaceUV = (DebugSurfaceUV)EditorGUILayout.EnumPopup(m_DebugSurfaceUV);
147+
EditorGUILayout.EndHorizontal();
148+
}
149+
if (m_DebugSurface == DebugSurface.Tangents)
150+
{
151+
EditorGUILayout.BeginHorizontal();
152+
EditorGUILayout.PrefixLabel(UI.SurfaceTangent);
153+
m_DebugSurfaceTangents = (DebugSurfaceTangents)EditorGUILayout.EnumPopup(m_DebugSurfaceTangents);
154+
EditorGUILayout.EndHorizontal();
155+
}
156+
if (EditorGUI.EndChangeCheck())
157+
UpdateTempMaterial();
158+
}
122159
{
123160
EditorGUILayout.Space();
124161
if (m_Mesh)
@@ -132,4 +169,41 @@ void OnGUI()
132169
SceneView.RepaintAll();
133170
}
134171
}
135-
}
172+
173+
Shader GetShaderForTempMaterial()
174+
{
175+
switch (m_DebugSurface)
176+
{
177+
case DebugSurface.None:
178+
default:
179+
return null;
180+
case DebugSurface.Color:
181+
return Shader.Find("Debug/Color");
182+
case DebugSurface.Facing:
183+
return Shader.Find("Debug/FrontBack");
184+
case DebugSurface.UV:
185+
return Shader.Find("Debug/UV");
186+
case DebugSurface.Tangents:
187+
return Shader.Find("Debug/Tangents");
188+
}
189+
}
190+
191+
void UpdateTempMaterial ()
192+
{
193+
if (m_DebugSurface == DebugSurface.None)
194+
{
195+
ChangeMaterial(null);
196+
return;
197+
}
198+
else if (!m_tempMat)
199+
m_tempMat = new Material(GetShaderForTempMaterial())
200+
{
201+
hideFlags = HideFlags.HideAndDontSave,
202+
name = "MeshDebugger Debug Material"
203+
};
204+
else
205+
m_tempMat.shader = GetShaderForTempMaterial();
206+
207+
ChangeMaterial(m_tempMat);
208+
}
209+
}

0 commit comments

Comments
 (0)