|
| 1 | +using UnityEditor; |
| 2 | +using UnityEngine; |
| 3 | + |
| 4 | +namespace Zigurous.Debug.Editor |
| 5 | +{ |
| 6 | + public sealed class MeshDebugger : EditorWindow |
| 7 | + { |
| 8 | + private Mesh currentMesh; |
| 9 | + private MeshFilter currentMeshFilter; |
| 10 | + private SkinnedMeshRenderer currentSkinnedMeshRenderer; |
| 11 | + private int currentFace = 0, numFaces = 0, newFace = 0; |
| 12 | + private GUIStyle labelStyle = null; |
| 13 | + private Vector3 p1, p2, p3; |
| 14 | + |
| 15 | + [MenuItem("Window/Analysis/Mesh Debugger")] |
| 16 | + public static void ShowWindow() |
| 17 | + { |
| 18 | + EditorWindow.GetWindow(typeof(MeshDebugger), true, "Mesh Debugger"); |
| 19 | + } |
| 20 | + |
| 21 | + private void OnEnable() |
| 22 | + { |
| 23 | + SceneView.duringSceneGui -= OnSceneGUI; |
| 24 | + SceneView.duringSceneGui += OnSceneGUI; |
| 25 | + |
| 26 | + OnSelectionChange(); |
| 27 | + } |
| 28 | + |
| 29 | + private void OnDestroy() |
| 30 | + { |
| 31 | + SceneView.duringSceneGui -= OnSceneGUI; |
| 32 | + } |
| 33 | + |
| 34 | + private void OnSelectionChange() |
| 35 | + { |
| 36 | + currentMesh = null; |
| 37 | + currentSkinnedMeshRenderer = null; |
| 38 | + numFaces = 0; |
| 39 | + |
| 40 | + if (Selection.activeGameObject) |
| 41 | + { |
| 42 | + currentMeshFilter = Selection.activeGameObject.GetComponentInChildren<MeshFilter>(); |
| 43 | + |
| 44 | + if (currentMeshFilter != null) |
| 45 | + { |
| 46 | + currentMesh = currentMeshFilter.sharedMesh; |
| 47 | + numFaces = currentMesh.triangles.Length / 3; |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + currentSkinnedMeshRenderer = Selection.activeGameObject.GetComponentInChildren<SkinnedMeshRenderer>(); |
| 52 | + |
| 53 | + if (currentSkinnedMeshRenderer != null) |
| 54 | + { |
| 55 | + currentMesh = currentSkinnedMeshRenderer.sharedMesh; |
| 56 | + numFaces = currentMesh.triangles.Length / 3; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + currentFace = 0; |
| 62 | + newFace = currentFace; |
| 63 | + |
| 64 | + Repaint(); |
| 65 | + } |
| 66 | + |
| 67 | + private void OnGUI() |
| 68 | + { |
| 69 | + if (currentMesh == null) |
| 70 | + { |
| 71 | + EditorGUILayout.LabelField("Current selection contains no mesh"); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + EditorGUILayout.LabelField("Number of faces", numFaces.ToString()); |
| 76 | + EditorGUILayout.LabelField("Current face", currentFace.ToString()); |
| 77 | + |
| 78 | + newFace = EditorGUILayout.IntField("Jump to face", currentFace); |
| 79 | + |
| 80 | + if (newFace != currentFace) |
| 81 | + { |
| 82 | + if (newFace >= 0 && newFace < numFaces) { |
| 83 | + currentFace = newFace; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + EditorGUILayout.BeginHorizontal(); |
| 88 | + |
| 89 | + if (GUILayout.Button("Previous Face")) |
| 90 | + { |
| 91 | + currentFace = (currentFace - 1) % numFaces; |
| 92 | + |
| 93 | + if (currentFace < 0) { |
| 94 | + currentFace = currentFace + numFaces; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (GUILayout.Button("Next Face")) { |
| 99 | + currentFace = (currentFace + 1) % numFaces; |
| 100 | + } |
| 101 | + |
| 102 | + EditorGUILayout.EndHorizontal(); |
| 103 | + |
| 104 | + EditorGUILayout.Space(); |
| 105 | + |
| 106 | + int redIndex = currentMesh.triangles[currentFace * 3]; |
| 107 | + int greenIndex = currentMesh.triangles[currentFace * 3 + 1]; |
| 108 | + int blueIndex = currentMesh.triangles[currentFace * 3 + 2]; |
| 109 | + |
| 110 | + EditorGUILayout.LabelField("Red vertex", $"Index: {redIndex}, UV: ({currentMesh.uv[redIndex].x}, {currentMesh.uv[redIndex].y})"); |
| 111 | + EditorGUILayout.LabelField("Green vertex", $"Index: {greenIndex}, UV: ({currentMesh.uv[greenIndex].x}, {currentMesh.uv[greenIndex].y})"); |
| 112 | + EditorGUILayout.LabelField("Blue vertex", $"Index: {blueIndex}, UV: ({currentMesh.uv[blueIndex].x}, {currentMesh.uv[blueIndex].y})"); |
| 113 | + } |
| 114 | + |
| 115 | + private void OnSceneGUI(SceneView sceneView) |
| 116 | + { |
| 117 | + if (currentMesh == null) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + int index1 = currentMesh.triangles[currentFace * 3]; |
| 122 | + int index2 = currentMesh.triangles[currentFace * 3 + 1]; |
| 123 | + int index3 = currentMesh.triangles[currentFace * 3 + 2]; |
| 124 | + |
| 125 | + if (currentMeshFilter != null) |
| 126 | + { |
| 127 | + p1 = currentMeshFilter.transform.TransformPoint(currentMesh.vertices[index1]); |
| 128 | + p2 = currentMeshFilter.transform.TransformPoint(currentMesh.vertices[index2]); |
| 129 | + p3 = currentMeshFilter.transform.TransformPoint(currentMesh.vertices[index3]); |
| 130 | + } |
| 131 | + else if (currentSkinnedMeshRenderer != null) |
| 132 | + { |
| 133 | + p1 = currentSkinnedMeshRenderer.transform.TransformPoint(currentMesh.vertices[index1]); |
| 134 | + p2 = currentSkinnedMeshRenderer.transform.TransformPoint(currentMesh.vertices[index2]); |
| 135 | + p3 = currentSkinnedMeshRenderer.transform.TransformPoint(currentMesh.vertices[index3]); |
| 136 | + } |
| 137 | + |
| 138 | + Handles.color = Color.red; |
| 139 | + Handles.SphereHandleCap(0, p1, Quaternion.identity, 0.2f * HandleUtility.GetHandleSize(p1), EventType.Repaint); |
| 140 | + Handles.color = Color.green; |
| 141 | + Handles.SphereHandleCap(0, p2, Quaternion.identity, 0.2f * HandleUtility.GetHandleSize(p2), EventType.Repaint); |
| 142 | + Handles.color = Color.blue; |
| 143 | + Handles.SphereHandleCap(0, p3, Quaternion.identity, 0.2f * HandleUtility.GetHandleSize(p3), EventType.Repaint); |
| 144 | + |
| 145 | + Handles.color = Color.white; |
| 146 | + Handles.DrawDottedLine(p1, p2, 5f); |
| 147 | + Handles.DrawDottedLine(p2, p3, 5f); |
| 148 | + Handles.DrawDottedLine(p3, p1, 5f); |
| 149 | + |
| 150 | + if (labelStyle == null) |
| 151 | + { |
| 152 | + labelStyle = new GUIStyle(GUI.skin.label); |
| 153 | + labelStyle.normal.textColor = Color.white; |
| 154 | + labelStyle.fixedWidth = 40; |
| 155 | + labelStyle.fixedHeight = 20; |
| 156 | + labelStyle.alignment = TextAnchor.MiddleCenter; |
| 157 | + labelStyle.fontSize = 12; |
| 158 | + labelStyle.clipping = TextClipping.Overflow; |
| 159 | + } |
| 160 | + |
| 161 | + Handles.Label(p1, index1.ToString(), labelStyle); |
| 162 | + Handles.Label(p2, index2.ToString(), labelStyle); |
| 163 | + Handles.Label(p3, index3.ToString(), labelStyle); |
| 164 | + |
| 165 | + sceneView.Repaint(); |
| 166 | + } |
| 167 | + |
| 168 | + private void OnInspectorUpdate() |
| 169 | + { |
| 170 | + Repaint(); |
| 171 | + } |
| 172 | + |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments