-
Notifications
You must be signed in to change notification settings - Fork 0
/
Visualizer.cs
58 lines (55 loc) · 1.59 KB
/
Visualizer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Copy pasted from catlikecodng.com
//USE ONLY FOR DEBUGGING -- hide in inspector -> OnDrawGizmos will not be called
public class Visualizer : MonoBehaviour
{
public float offset = 0.01f;
public float scale = 0.1f;
public bool drawNormals = false;
public bool drawTangent = false;
public bool drawBiTangent = false;
void OnDrawGizmos()
{
MeshFilter filter = GetComponent<MeshFilter>();
if (filter) {
Mesh mesh = filter.sharedMesh;
if (mesh) {
ShowTangentSpace(mesh);
}
}
}
void ShowTangentSpace (Mesh mesh) {
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
Vector4[] tangents = mesh.tangents;
for (int i = 0; i < vertices.Length; i++) {
ShowTangentSpace(
transform.TransformPoint(vertices[i]),
transform.TransformDirection(normals[i]),
transform.TransformDirection(tangents[i]),
tangents[i].w
);
}
}
void ShowTangentSpace (Vector3 vertex, Vector3 normal, Vector3 tangent, float binormalSign) {
vertex += normal * offset;
if (drawNormals)
{
Gizmos.color = Color.green;
Gizmos.DrawLine(vertex, vertex + normal * scale);
}
if (drawTangent)
{
Gizmos.color = Color.red;
Gizmos.DrawLine(vertex, vertex + tangent * scale);
}
if (drawBiTangent)
{
Vector3 binormal = Vector3.Cross(normal, tangent) * binormalSign;
Gizmos.color = Color.blue;
Gizmos.DrawLine(vertex, vertex + binormal * scale);
}
}
}