Skip to content

Release/1.5.0 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.5.0] - 2023/06/18

### Added

- New `ExitApplication` script
- New `PauseApplication` script
- New editor mesh debugger: Window > Analysis > Mesh Debugger
- Behavior help URLs

### Changed

- Refactored the `Compare<T>` class
- Changed the `Compare.Test` function to two distinct functions: `Compare.Equal` and `Compare.NotEqual`
- The generics are now applied to the functions instead of the class, i.e., `Compare.Equal<int>` instead of `Compare<int>.Equal`

## [1.4.0] - 2022/05/20

### Added
Expand Down
6 changes: 3 additions & 3 deletions Documentation~/articles/benchmarking.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Benchmark.Run(1000, Foo, Bar, Baz);

## 🎏 Compare Equality

Alternatively, sometimes it is useful to test if the results of two functions are equal. The **Debug Tools** package comes with another static class [Compare](/api/Zigurous.Debug/Compare-1) to handle these tests. The class uses generics to know the type of value you are looking to compare, and it returns the percentage of results that are equal.
Sometimes it is useful to test if multiple functions return the same results. The **Debug Tools** package comes with another static class [Compare](/api/Zigurous.Debug/Compare-1) to handle these tests. The class uses generics to know the type of value you are looking to compare, and it returns the percentage of results that are equal (or not equal) for a given amount of iterations.

```csharp
Compare<bool>.Test(Foo, Bar, 1000);
Compare<float>.Test(Foo, Bar, 1000);
Compare.Equal(Foo, Bar, 1000);
Compare.NotEqual(Foo, Bar, 1000);
```
2 changes: 2 additions & 0 deletions Documentation~/articles/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ The **Debug Tools** package contains assets and scripts for debugging Unity proj

#### 🎨 [Debug Shaders](/manual/shaders)

#### 🖇️ [Mesh Debugger](/manual/mesh-debugger)

#### ✏️ [Drawing](/manual/drawing)
11 changes: 11 additions & 0 deletions Documentation~/articles/mesh-debugger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
slug: "/manual/mesh-debugger"
---

# Mesh Debugger

The **Debug Tools** package includes a custom editor window for debugging and visualizing mesh data. To open this debugger, use the menu `Window > Analysis > Mesh Debugger`.

When a mesh is selected in the hierarchy while the window is open, a wireframe will be drawn around the current face of the mesh, and vertex and UV information will be shown in the editor. The window provides buttons to move to the next or previous face.

![](../images/mesh-debugger.jpg)
4 changes: 4 additions & 0 deletions Documentation~/data/sidenav.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
"name": "Debug Shaders",
"path": "/manual/shaders"
},
{
"name": "Mesh Debugger",
"path": "/manual/mesh-debugger"
},
{
"name": "Drawing",
"path": "/manual/drawing"
Expand Down
Binary file added Documentation~/images/mesh-debugger.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

175 changes: 175 additions & 0 deletions Editor/MeshDebugger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using UnityEditor;
using UnityEngine;

namespace Zigurous.Debug.Editor
{
public sealed class MeshDebugger : EditorWindow
{
private Mesh currentMesh;
private MeshFilter currentMeshFilter;
private SkinnedMeshRenderer currentSkinnedMeshRenderer;
private int currentFace = 0, numFaces = 0, newFace = 0;
private GUIStyle labelStyle = null;
private Vector3 p1, p2, p3;

[MenuItem("Window/Analysis/Mesh Debugger")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(MeshDebugger), true, "Mesh Debugger");
}

private void OnEnable()
{
SceneView.duringSceneGui -= OnSceneGUI;
SceneView.duringSceneGui += OnSceneGUI;

OnSelectionChange();
}

private void OnDestroy()
{
SceneView.duringSceneGui -= OnSceneGUI;
}

private void OnSelectionChange()
{
currentMesh = null;
currentSkinnedMeshRenderer = null;
numFaces = 0;

if (Selection.activeGameObject)
{
currentMeshFilter = Selection.activeGameObject.GetComponentInChildren<MeshFilter>();

if (currentMeshFilter != null)
{
currentMesh = currentMeshFilter.sharedMesh;
numFaces = currentMesh.triangles.Length / 3;
}
else
{
currentSkinnedMeshRenderer = Selection.activeGameObject.GetComponentInChildren<SkinnedMeshRenderer>();

if (currentSkinnedMeshRenderer != null)
{
currentMesh = currentSkinnedMeshRenderer.sharedMesh;
numFaces = currentMesh.triangles.Length / 3;
}
}
}

currentFace = 0;
newFace = currentFace;

Repaint();
}

private void OnGUI()
{
if (currentMesh == null)
{
EditorGUILayout.LabelField("Current selection contains no mesh");
return;
}

EditorGUILayout.LabelField("Number of faces", numFaces.ToString());
EditorGUILayout.LabelField("Current face", currentFace.ToString());

newFace = EditorGUILayout.IntField("Jump to face", currentFace);

if (newFace != currentFace)
{
if (newFace >= 0 && newFace < numFaces) {
currentFace = newFace;
}
}

EditorGUILayout.BeginHorizontal();

if (GUILayout.Button("Previous Face"))
{
currentFace = (currentFace - 1) % numFaces;

if (currentFace < 0) {
currentFace = currentFace + numFaces;
}
}

if (GUILayout.Button("Next Face")) {
currentFace = (currentFace + 1) % numFaces;
}

EditorGUILayout.EndHorizontal();

EditorGUILayout.Space();

int redIndex = currentMesh.triangles[currentFace * 3];
int greenIndex = currentMesh.triangles[currentFace * 3 + 1];
int blueIndex = currentMesh.triangles[currentFace * 3 + 2];

EditorGUILayout.LabelField("Red vertex", $"Index: {redIndex}, UV: ({currentMesh.uv[redIndex].x}, {currentMesh.uv[redIndex].y})");
EditorGUILayout.LabelField("Green vertex", $"Index: {greenIndex}, UV: ({currentMesh.uv[greenIndex].x}, {currentMesh.uv[greenIndex].y})");
EditorGUILayout.LabelField("Blue vertex", $"Index: {blueIndex}, UV: ({currentMesh.uv[blueIndex].x}, {currentMesh.uv[blueIndex].y})");
}

private void OnSceneGUI(SceneView sceneView)
{
if (currentMesh == null) {
return;
}

int index1 = currentMesh.triangles[currentFace * 3];
int index2 = currentMesh.triangles[currentFace * 3 + 1];
int index3 = currentMesh.triangles[currentFace * 3 + 2];

if (currentMeshFilter != null)
{
p1 = currentMeshFilter.transform.TransformPoint(currentMesh.vertices[index1]);
p2 = currentMeshFilter.transform.TransformPoint(currentMesh.vertices[index2]);
p3 = currentMeshFilter.transform.TransformPoint(currentMesh.vertices[index3]);
}
else if (currentSkinnedMeshRenderer != null)
{
p1 = currentSkinnedMeshRenderer.transform.TransformPoint(currentMesh.vertices[index1]);
p2 = currentSkinnedMeshRenderer.transform.TransformPoint(currentMesh.vertices[index2]);
p3 = currentSkinnedMeshRenderer.transform.TransformPoint(currentMesh.vertices[index3]);
}

Handles.color = Color.red;
Handles.SphereHandleCap(0, p1, Quaternion.identity, 0.2f * HandleUtility.GetHandleSize(p1), EventType.Repaint);
Handles.color = Color.green;
Handles.SphereHandleCap(0, p2, Quaternion.identity, 0.2f * HandleUtility.GetHandleSize(p2), EventType.Repaint);
Handles.color = Color.blue;
Handles.SphereHandleCap(0, p3, Quaternion.identity, 0.2f * HandleUtility.GetHandleSize(p3), EventType.Repaint);

Handles.color = Color.white;
Handles.DrawDottedLine(p1, p2, 5f);
Handles.DrawDottedLine(p2, p3, 5f);
Handles.DrawDottedLine(p3, p1, 5f);

if (labelStyle == null)
{
labelStyle = new GUIStyle(GUI.skin.label);
labelStyle.normal.textColor = Color.white;
labelStyle.fixedWidth = 40;
labelStyle.fixedHeight = 20;
labelStyle.alignment = TextAnchor.MiddleCenter;
labelStyle.fontSize = 12;
labelStyle.clipping = TextClipping.Overflow;
}

Handles.Label(p1, index1.ToString(), labelStyle);
Handles.Label(p2, index2.ToString(), labelStyle);
Handles.Label(p3, index3.ToString(), labelStyle);

sceneView.Repaint();
}

private void OnInspectorUpdate()
{
Repaint();
}

}

}
11 changes: 11 additions & 0 deletions Editor/MeshDebugger.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Editor/Zigurous.Debug.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "Zigurous.Debug.Editor",
"references": [
"GUID:0186cdaa7ccbaeb4f91d3907506784d7"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Editor/Zigurous.Debug.Editor.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The **Debug Tools** package contains assets and scripts for debugging Unity proj
- [Benchmarking](https://docs.zigurous.com/com.zigurous.debug/manual/benchmarking)
- [Framerate Display](https://docs.zigurous.com/com.zigurous.debug/manual/framerate)
- [Debug Shaders](https://docs.zigurous.com/com.zigurous.debug/manual/shaders)
- [Mesh Debugger](https://docs.zigurous.com/com.zigurous.debug/manual/mesh-debugger)
- [Drawing](https://docs.zigurous.com/com.zigurous.debug/manual/drawing)

## Installation

Expand Down
59 changes: 53 additions & 6 deletions Runtime/Compare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace Zigurous.Debug
{
/// <summary>
/// Compares how many results are equal between two functions.
/// Compares the results of multiple functions for equality.
/// </summary>
/// <typeparam name="T">The type of value to compare.</typeparam>
public static class Compare<T> where T: IEquatable<T>
public static class Compare
{
/// <summary>
/// Tests the equality of the results of two functions with a given
/// Compares how many results of the two functions are equal for a given
/// amount of iterations.
/// </summary>
/// <param name="foo">The first function to execute.</param>
Expand All @@ -18,7 +17,8 @@ public static class Compare<T> where T: IEquatable<T>
/// <param name="log">Logs the final comparison result.</param>
/// <param name="logIndividual">Logs the result of each iteration of the functions.</param>
/// <returns>The percentage of equal results.</returns>
public static float Test(Func<T> foo, Func<T> bar, int iterations, bool log = true, bool logIndividual = false)
public static float Equal<T>(Func<T> foo, Func<T> bar, int iterations, bool log = true, bool logIndividual = false)
where T : IEquatable<T>
{
#if UNITY_EDITOR || DEVELOPMENT_BUILD
int amountEqual = 0;
Expand All @@ -29,7 +29,10 @@ public static float Test(Func<T> foo, Func<T> bar, int iterations, bool log = tr
T resultBar = bar();

bool equal = resultFoo.Equals(resultBar);
if (equal) amountEqual++;

if (equal) {
amountEqual++;
}

if (log && logIndividual) {
UnityEngine.Debug.Log($"[Compare] {resultFoo.ToString()} vs {resultBar.ToString()} | {(equal ? "Equal" : "Not Equal")}");
Expand All @@ -48,6 +51,50 @@ public static float Test(Func<T> foo, Func<T> bar, int iterations, bool log = tr
#endif
}

/// <summary>
/// Compares how many results of the two functions are not equal for a
/// given amount of iterations.
/// </summary>
/// <param name="foo">The first function to execute.</param>
/// <param name="bar">The second function to execute.</param>
/// <param name="iterations">The amount of times each function is executed.</param>
/// <param name="log">Logs the final comparison result.</param>
/// <param name="logIndividual">Logs the result of each iteration of the functions.</param>
/// <returns>The percentage of equal results.</returns>
public static float NotEqual<T>(Func<T> foo, Func<T> bar, int iterations, bool log = true, bool logIndividual = false)
where T : IEquatable<T>
{
#if UNITY_EDITOR || DEVELOPMENT_BUILD
int amountNotEqual = 0;

for (int i = 0; i < iterations; i++)
{
T resultFoo = foo();
T resultBar = bar();

bool equal = resultFoo.Equals(resultBar);

if (!equal) {
amountNotEqual++;
}

if (log && logIndividual) {
UnityEngine.Debug.Log($"[Compare] {resultFoo.ToString()} vs {resultBar.ToString()} | {(equal ? "Equal" : "Not Equal")}");
}
}

float percentNotEqual = (float)amountNotEqual / (float)iterations;

if (log) {
UnityEngine.Debug.Log($"[Compare] {amountNotEqual.ToString()}/{iterations.ToString()} ({(percentNotEqual * 100f).ToString()}%) not equal results");
}

return percentNotEqual;
#else
return float.NaN;
#endif
}

}

}
Loading