Skip to content

Instantly share code, notes, and snippets.

@craigmjohnston
Last active August 29, 2015 14:05
Show Gist options
  • Save craigmjohnston/e3c014cfb7b8deeea034 to your computer and use it in GitHub Desktop.
Save craigmjohnston/e3c014cfb7b8deeea034 to your computer and use it in GitHub Desktop.
A little script that draws the direction of mesh normals into the scene window in Unity3D, and also draws little number labels for each of the mesh's vertices. Just add the component to the object containing the mesh. Works with MeshFilter and MeshCollider meshes.
using UnityEditor;
using UnityEngine;
public class VisualiseMeshNormals : MonoBehaviour {
public enum MeshType { Filter, Collider }
public MeshType meshType = MeshType.Filter;
protected MeshFilter meshFilter;
protected MeshCollider meshCollider;
protected Mesh mesh {
get {
if (meshType == MeshType.Filter && meshFilter == null) {
meshFilter = GetComponent<MeshFilter>();
} else if (meshType == MeshType.Collider && meshCollider == null) {
meshCollider = GetComponent<MeshCollider>();
}
return meshType == MeshType.Filter ?
meshFilter.mesh : meshCollider.sharedMesh;
}
}
void OnDrawGizmos() {
for (int i = 0; i < mesh.vertexCount; i++) {
Gizmos.color = Color.yellow;
Handles.Label(transform.position + mesh.vertices[i], i.ToString());
Gizmos.DrawLine(transform.position + mesh.vertices[i],
transform.position + mesh.vertices[i]
+ mesh.normals[i].normalized * 100);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment