Created
June 26, 2017 08:07
-
-
Save miguel12345/1968d5d5f2d92c09fc022fa55692c77c to your computer and use it in GitHub Desktop.
Tangent space basis visualizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class TangentSpaceBasisVisualizer : MonoBehaviour | |
{ | |
void GetTangentSpaceBasis(Mesh mesh, int triangleIndex, Vector3 barycentricCoordinates,out Vector3 tangent,out Vector3 binormal, out Vector3 normal) { | |
Vector3[] normals = mesh.normals; | |
Vector4[] tangents = mesh.tangents; | |
int[] triangles = mesh.triangles; | |
Vector3 n0 = normals [triangles [triangleIndex * 3 + 0]]; | |
Vector3 n1 = normals [triangles [triangleIndex * 3 + 1]]; | |
Vector3 n2 = normals [triangles [triangleIndex * 3 + 2]]; | |
Vector4 t0 = tangents [triangles [triangleIndex * 3 + 0]]; | |
Vector4 t1 = tangents [triangles [triangleIndex * 3 + 1]]; | |
Vector4 t2 = tangents [triangles [triangleIndex * 3 + 2]]; | |
Vector3 interpolatedNormal = n0 * barycentricCoordinates.x + n1 * barycentricCoordinates.y + n2 * barycentricCoordinates.z; | |
interpolatedNormal = interpolatedNormal.normalized; | |
Vector3 interpolatedTangent = t0 * barycentricCoordinates.x + t1 * barycentricCoordinates.y + t2 * barycentricCoordinates.z; | |
interpolatedTangent = interpolatedTangent.normalized; | |
Vector3 interpolatedBinormal = Vector3.Cross (interpolatedNormal, interpolatedTangent) * t0.w; | |
interpolatedBinormal = interpolatedBinormal.normalized; | |
normal = interpolatedNormal; | |
tangent = interpolatedTangent; | |
binormal = interpolatedBinormal; | |
return; | |
} | |
void Update() { | |
ShowTangentSpaceBasis (); | |
} | |
void ShowTangentSpaceBasis() { | |
RaycastHit hit; | |
if (!Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) { | |
return ; | |
} | |
if (! (hit.collider is MeshCollider)) { | |
Debug.LogWarning ("Can't calculate tangent space in " + hit.collider.gameObject + " because it doesn't contain a mesh collider"); | |
return ; | |
} | |
Mesh mesh = hit.collider.GetComponent<MeshFilter>().sharedMesh; | |
Vector3 tangent; | |
Vector3 normal; | |
Vector3 binormal; | |
GetTangentSpaceBasis (mesh,hit.triangleIndex,hit.barycentricCoordinate, out tangent, out binormal, out normal); | |
Transform hitTransform = hit.collider.transform; | |
normal = hitTransform.TransformDirection (normal); | |
tangent = hitTransform.TransformDirection (tangent); | |
binormal = hitTransform.TransformDirection (binormal); | |
Debug.DrawRay (hit.point, normal, Color.blue); | |
Debug.DrawRay (hit.point, tangent, Color.red); | |
Debug.DrawRay (hit.point, binormal, Color.green); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment