Skip to content

Instantly share code, notes, and snippets.

@mandarinx
Last active January 12, 2024 23:08
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mandarinx/ed733369fbb2eea6c7fa9e3da65a0e17 to your computer and use it in GitHub Desktop.
Save mandarinx/ed733369fbb2eea6c7fa9e3da65a0e17 to your computer and use it in GitHub Desktop.
Visualize mesh normals in Unity3D

Visualize the normals of a mesh

It's meant to be simple and easy to use, and therefore it has been made as a custom editor of MeshFilter. It needs no preparations.

How to use

Drop the file in an Editor folder.

Updates

  • store the normals length i editor prefs
  • adjust the normals length in the mesh filter inspector
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(MeshFilter))]
public class NormalsVisualizer : Editor {
private const string EDITOR_PREF_KEY = "_normals_length";
private Mesh mesh;
private MeshFilter mf;
private Vector3[] verts;
private Vector3[] normals;
private float normalsLength = 1f;
private void OnEnable() {
mf = target as MeshFilter;
if (mf != null) {
mesh = mf.sharedMesh;
}
normalsLength = EditorPrefs.GetFloat(EDITOR_PREF_KEY);
}
private void OnSceneGUI() {
if (mesh == null) {
return;
}
Handles.matrix = mf.transform.localToWorldMatrix;
Handles.color = Color.yellow;
verts = mesh.vertices;
normals = mesh.normals;
int len = mesh.vertexCount;
for (int i = 0; i < len; i++) {
Handles.DrawLine(verts[i], verts[i] + normals[i] * normalsLength);
}
}
public override void OnInspectorGUI() {
base.OnInspectorGUI();
EditorGUI.BeginChangeCheck();
normalsLength = EditorGUILayout.FloatField("Normals length", normalsLength);
if (EditorGUI.EndChangeCheck()) {
EditorPrefs.SetFloat(EDITOR_PREF_KEY, normalsLength);
}
}
}
@PooperPig
Copy link

Thanks for sharing - this was just what I needed to sort out a niggly bug!

@madtowngaming
Copy link

madtowngaming commented Jan 11, 2024

Warning ... code can pose problems on large meshes ...

Sorry ... the code works but .. I used it on a large mesh and it crashed Unity. I lost about a day's worth of coding.  The problem however is not in code per se but in Unity, or rather both.  It will not work on large meshes. I am not completely sure why Unity crashed but I am working on a custom mesh with a lot of vertices, about 350,000.  As someone pointed out above there should be some way to limit the updating for large meshes.  Also, if the length of the normal in the inspector is set to zero, to hide the normal, then when it is set back to a non zero value the code will not work.  This has to do with some weird Unity editor updating behavior.  In addition there should be a way to turn the method off and on in an easy manner.

@Tunied
Copy link

Tunied commented Jan 11, 2024

Does not work .. garbage code ...

first : it's working.

second: it's not garbage. even if it's not working.

third: maybe something else is garbage?

@madtowngaming
Copy link

madtowngaming commented Jan 12, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment