mirror of
https://github.com/tetreum/brickcraft.git
synced 2026-02-13 21:48:26 -06:00
44 lines
1.1 KiB
C#
44 lines
1.1 KiB
C#
using UnityEngine;
|
|
|
|
public class TangentSpaceVisualizer : MonoBehaviour {
|
|
|
|
public float offset = 0.01f;
|
|
public float scale = 0.1f;
|
|
|
|
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;
|
|
Gizmos.color = Color.green;
|
|
Gizmos.DrawLine(vertex, vertex + normal * scale);
|
|
Gizmos.color = Color.red;
|
|
Gizmos.DrawLine(vertex, vertex + tangent * scale);
|
|
Vector3 binormal = Vector3.Cross(normal, tangent) * binormalSign;
|
|
Gizmos.color = Color.blue;
|
|
Gizmos.DrawLine(vertex, vertex + binormal * scale);
|
|
}
|
|
} |