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);
}
}
}
@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