Skip to content

Instantly share code, notes, and snippets.

@cjhodge
Forked from mandarinx/NormalsVisualizer.cs
Created February 7, 2020 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjhodge/dd1f72e0a787f6d6799403cbef39e30a to your computer and use it in GitHub Desktop.
Save cjhodge/dd1f72e0a787f6d6799403cbef39e30a 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.

using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(MeshFilter))]
public class NormalsVisualizer : Editor {
private Mesh mesh;
void OnEnable() {
MeshFilter mf = target as MeshFilter;
if (mf != null) {
mesh = mf.sharedMesh;
}
}
void OnSceneGUI() {
if (mesh == null) {
return;
}
for (int i = 0; i < mesh.vertexCount; i++) {
Handles.matrix = (target as MeshFilter).transform.localToWorldMatrix;
Handles.color = Color.yellow;
Handles.DrawLine(
mesh.vertices[i],
mesh.vertices[i] + mesh.normals[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment