Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tf1379
Created January 9, 2018 11:05
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 tf1379/a523b3cae997ce17e874d432fb7fa3b5 to your computer and use it in GitHub Desktop.
Save tf1379/a523b3cae997ce17e874d432fb7fa3b5 to your computer and use it in GitHub Desktop.
A script to show normal, tangent, binormal in Unity Scene View
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace HEAP
{
[ExecuteInEditMode]
public class MeshVertexInfoVisualizer : MonoBehaviour
{
[System.Flags]
public enum InfoType
{
None = 0x00,
Normal = (0x01 << 1),
Tangent = (0x01 << 2),
Binormal = (0x01 << 3)
}
static Color NormalColor = Color.magenta;
static Color TangentColor = Color.green;
static Color BinormalColor = Color.blue;
public Mesh mesh;
public InfoType infoType = InfoType.Normal;
[Range(0f, 1f)]
public float scale = 0.05f;
void OnEnable()
{
FetchMesh();
}
void OnValidate()
{
FetchMesh();
}
void FetchMesh()
{
if (mesh != null) { return; }
SkinnedMeshRenderer smr = GetComponent<SkinnedMeshRenderer>();
if (smr != null)
{
mesh = smr.sharedMesh;
}
if (mesh != null) { return; }
MeshFilter filter = GetComponent<MeshFilter>();
if (filter != null)
{
mesh = filter.sharedMesh;
}
}
void OnDrawGizmos()
{
if (mesh == null) { return; }
scale = Mathf.Abs(scale);
ShowVertexInfo(mesh);
}
bool EnableInfoType(InfoType it)
{
return (infoType & it) != 0;
}
void ShowVertexInfo(Mesh mesh)
{
Vector3[] vertices = mesh.vertices;
Vector3[] normals = (EnableInfoType(InfoType.Normal) || EnableInfoType(InfoType.Binormal)) ? mesh.normals : null;
Vector4[] tangents = (EnableInfoType(InfoType.Tangent) || EnableInfoType(InfoType.Binormal)) ? mesh.tangents : null;
for (int i = 0; i < vertices.Length; i++)
{
Vector3 vertex = transform.TransformPoint(vertices[i]);
Vector3 normal = (normals != null && i < normals.Length) ? transform.TransformDirection(normals[i]) : Vector3.zero;
Vector4 tangent4 = Vector4.zero;
Vector3 tangnet3 = Vector3.zero;
if (tangents != null && i < tangents.Length) {
tangent4 = tangents[i];
tangnet3 = transform.TransformDirection(tangent4.x, tangent4.y, tangent4.z);
}
DrawVertexInfo(vertex, normal, tangnet3, tangent4.w);
}
}
void DrawVertexInfo(Vector3 vertex, Vector3 normal, Vector3 tangnet, float binormalSign)
{
if (EnableInfoType(InfoType.Normal)) {
Gizmos.color = NormalColor;
Gizmos.DrawLine(vertex, vertex + normal * scale);
Gizmos.color = Color.white;
}
if (EnableInfoType(InfoType.Tangent)) {
Gizmos.color = TangentColor;
Gizmos.DrawLine(vertex, vertex + tangnet * scale);
Gizmos.color = Color.white;
}
if (EnableInfoType(InfoType.Binormal)){
Gizmos.color = BinormalColor;
Vector3 binormal = Vector3.Cross(normal, tangnet) * Mathf.Sign(binormalSign);
Gizmos.DrawLine(vertex, vertex + binormal * scale);
Gizmos.color = Color.white;
}
}
}
}
@tony-topper
Copy link

Thanks so much for this gist. Very helpful for me as I work through some shader development.

@ouerkakaChango
Copy link

Thanks.Time saver man!

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