Skip to content

Instantly share code, notes, and snippets.

@Siccity
Last active April 1, 2019 20:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Siccity/5cbfd8dd931f03cd088df491ae24d0a8 to your computer and use it in GitHub Desktop.
Save Siccity/5cbfd8dd931f03cd088df491ae24d0a8 to your computer and use it in GitHub Desktop.
Unity3D: Debug Mesh - Shows mesh normals and tangents.
using UnityEngine;
public class DebugMesh : MonoBehaviour {
private MeshFilter filter { get { return _filter != null ? _filter : _filter = GetComponent<MeshFilter>(); } }
private MeshFilter _filter;
public bool showNormals = true;
public bool showTangents = true;
public float renderDistance = 10f;
[ContextMenu("Print mesh info")]
public void PrintMeshInfo() {
Debug.Log("Verts: " + filter.sharedMesh.vertexCount + ", Norms: " + filter.sharedMesh.normals.Length + " Uvs: " + filter.sharedMesh.uv.Length + " Tris: " + filter.sharedMesh.triangles.Length + " Submeshes: " + filter.sharedMesh.subMeshCount);
}
// Update is called once per frame
void OnDrawGizmos() {
if (!filter) return;
Vector3[] verts = filter.sharedMesh.vertices;
Vector4[] tangents = filter.sharedMesh.tangents;
Vector3[] norms = filter.sharedMesh.normals;
for (int i = 0; i < verts.Length; i++) {
verts[i] = transform.TransformPoint(verts[i]);
if (Vector3.Distance(verts[i], Camera.current.transform.position) > renderDistance) continue;
if (showNormals) {
Gizmos.color = Color.blue;
Gizmos.DrawLine(verts[i], verts[i] + transform.rotation * norms[i] * 0.1f);
}
if (showTangents) {
if (tangents[i].w > 0) {
Gizmos.color = Color.yellow;
Gizmos.DrawLine(verts[i], verts[i] + transform.rotation * tangents[i] * 0.1f);
} else {
Gizmos.color = Color.red;
Gizmos.DrawLine(verts[i], verts[i] + transform.rotation * -tangents[i] * 0.1f);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment