Guest User

Untitled

a guest
Jan 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace HEAP
  6. {
  7. [ExecuteInEditMode]
  8. public class MeshVertexInfoVisualizer : MonoBehaviour
  9. {
  10. [System.Flags]
  11. public enum InfoType
  12. {
  13. None = 0x00,
  14. Normal = (0x01 << 1),
  15. Tangent = (0x01 << 2),
  16. Binormal = (0x01 << 3)
  17. }
  18.  
  19. static Color NormalColor = Color.magenta;
  20. static Color TangentColor = Color.green;
  21. static Color BinormalColor = Color.blue;
  22.  
  23. public Mesh mesh;
  24.  
  25.  
  26. public InfoType infoType = InfoType.Normal;
  27.  
  28. [Range(0f, 1f)]
  29. public float scale = 0.05f;
  30.  
  31. void OnEnable()
  32. {
  33. FetchMesh();
  34. }
  35.  
  36. void OnValidate()
  37. {
  38. FetchMesh();
  39. }
  40.  
  41. void FetchMesh()
  42. {
  43. if (mesh != null) { return; }
  44.  
  45. SkinnedMeshRenderer smr = GetComponent<SkinnedMeshRenderer>();
  46. if (smr != null)
  47. {
  48. mesh = smr.sharedMesh;
  49. }
  50.  
  51. if (mesh != null) { return; }
  52.  
  53. MeshFilter filter = GetComponent<MeshFilter>();
  54. if (filter != null)
  55. {
  56. mesh = filter.sharedMesh;
  57. }
  58. }
  59.  
  60. void OnDrawGizmos()
  61. {
  62. if (mesh == null) { return; }
  63.  
  64. scale = Mathf.Abs(scale);
  65. ShowVertexInfo(mesh);
  66. }
  67.  
  68. bool EnableInfoType(InfoType it)
  69. {
  70. return (infoType & it) != 0;
  71. }
  72.  
  73. void ShowVertexInfo(Mesh mesh)
  74. {
  75. Vector3[] vertices = mesh.vertices;
  76. Vector3[] normals = (EnableInfoType(InfoType.Normal) || EnableInfoType(InfoType.Binormal)) ? mesh.normals : null;
  77. Vector4[] tangents = (EnableInfoType(InfoType.Tangent) || EnableInfoType(InfoType.Binormal)) ? mesh.tangents : null;
  78.  
  79. for (int i = 0; i < vertices.Length; i++)
  80. {
  81. Vector3 vertex = transform.TransformPoint(vertices[i]);
  82. Vector3 normal = (normals != null && i < normals.Length) ? transform.TransformDirection(normals[i]) : Vector3.zero;
  83. Vector4 tangent4 = Vector4.zero;
  84. Vector3 tangnet3 = Vector3.zero;
  85. if (tangents != null && i < tangents.Length) {
  86. tangent4 = tangents[i];
  87. tangnet3 = transform.TransformDirection(tangent4.x, tangent4.y, tangent4.z);
  88. }
  89. DrawVertexInfo(vertex, normal, tangnet3, tangent4.w);
  90. }
  91. }
  92.  
  93. void DrawVertexInfo(Vector3 vertex, Vector3 normal, Vector3 tangnet, float binormalSign)
  94. {
  95. if (EnableInfoType(InfoType.Normal)) {
  96. Gizmos.color = NormalColor;
  97. Gizmos.DrawLine(vertex, vertex + normal * scale);
  98. Gizmos.color = Color.white;
  99. }
  100.  
  101. if (EnableInfoType(InfoType.Tangent)) {
  102. Gizmos.color = TangentColor;
  103. Gizmos.DrawLine(vertex, vertex + tangnet * scale);
  104. Gizmos.color = Color.white;
  105. }
  106.  
  107. if (EnableInfoType(InfoType.Binormal)){
  108. Gizmos.color = BinormalColor;
  109. Vector3 binormal = Vector3.Cross(normal, tangnet) * Mathf.Sign(binormalSign);
  110. Gizmos.DrawLine(vertex, vertex + binormal * scale);
  111. Gizmos.color = Color.white;
  112. }
  113. }
  114. }
  115. }
Add Comment
Please, Sign In to add comment