Hyluss icon

[Unity] TangentSpaceVisualizer

Hyluss | PRO | 09/06/19 06:49:30 AM UTC | 0 ⭐ | 537 👁️ | Never ⏰ | []
C# |

1.09 KB

|

None

|

0 👍

/

0 👎

using System.Collections;
using System.Collections.Generic;
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;
        for (int i = 0; i < vertices.Length; i++)
        {
            ShowTangentSpace(
                             transform.TransformPoint(vertices[i]),
                             transform.TransformDirection(normals[i])
                            );
        }
    }
 
    void ShowTangentSpace(Vector3 vertex, Vector3 normal)
    {
        vertex += normal * offset;
        Gizmos.color = Color.green;
        Gizmos.DrawLine(vertex, vertex + normal * scale);
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎