본문으로 바로가기

vertex info debugging script

category Technical Report/Unity 2019. 4. 18. 00:07
반응형

 

VertexInfo.cs


using UnityEngine;

[ExecuteAlways]
[RequireComponent(typeof(MeshFilter))]
public class VertexInfo : MonoBehaviour
{
    [Header("Vertex Selection Settings")]
    [SerializeField] private float selectionRadius = 5.0f;
    [SerializeField] private Color highlightColor = Color.red;

    private Mesh mesh;
    private int selectedVertexIndex = -1;
    private Vector3 selectedVertexWorldPos;

    private void Awake()
    {
        if (mesh == null)
        {
            MeshFilter mf = GetComponent<MeshFilter>();
            mesh = mf != null ? mf.sharedMesh : null;
        }
    }

    public void FindVertex(Vector2 mousePosition, Camera sceneCamera)
    {
        if (mesh == null || sceneCamera == null)
            return;

        selectedVertexIndex = -1;
        float minDistance = selectionRadius;

        for (int i = 0; i < mesh.vertexCount; i++)
        {
            Vector3 worldPos = transform.TransformPoint(mesh.vertices[i]);
            Vector3 screenPos = sceneCamera.WorldToScreenPoint(worldPos);

            // Scene 뷰 y축 뒤집힘 보정
            screenPos.y = Screen.height - screenPos.y;

            float dist = Vector2.Distance(mousePosition, screenPos);
            if (dist < minDistance)
            {
                minDistance = dist;
                selectedVertexIndex = i;
                selectedVertexWorldPos = worldPos;
            }
        }

        if (selectedVertexIndex >= 0)
        {
            Debug.Log($"[VertexInfo] Found Vertex #{selectedVertexIndex}");
            Debug.Log($"Position: {mesh.vertices[selectedVertexIndex]}");
            Debug.Log($"UV: {mesh.uv[selectedVertexIndex]}");
        }
    }

    private void OnDrawGizmos()
    {
        if (selectedVertexIndex >= 0)
        {
            Gizmos.color = highlightColor;
            Gizmos.DrawSphere(selectedVertexWorldPos, 0.05f);
#if UNITY_EDITOR
            UnityEditor.Handles.Label(selectedVertexWorldPos, $"#{selectedVertexIndex}");
#endif
        }
    }
}

 

VertexInfoEditor.cs


using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(VertexInfo))]
public class VertexInfoEditor : Editor
{
    private void OnSceneGUI()
    {
        VertexInfo vertexInfo = (VertexInfo)target;
        Event e = Event.current;

        if (e.type == EventType.KeyDown && e.character == 'f')
        {
            Camera sceneCamera = SceneView.lastActiveSceneView.camera;
            Vector2 mousePos = e.mousePosition;

            // Scene view mousePos는 bottom-left origin이지만
            // Screen space는 top-left origin이므로 변환 필요
            mousePos.y = Screen.height - mousePos.y;

            vertexInfo.FindVertex(mousePos, sceneCamera);

            e.Use(); // 이벤트 소모
        }
    }
}

 

기능 설명
selectionRadius : 마우스 근처 몇 픽셀 이내를 잡을지 조정 가능
highlightColor : 선택된 버텍스의 색상을 설정 가능
선택한 버텍스 인덱스 Label 표시 : Scene 뷰에 직접 인덱스 번호 보여줌
null-check와 컴포넌트 초기화 : 에러 방지
Event.Use() 호출 키 입력 이벤트를 소모해서 중복처리 방지

 

 

반응형

'Technical Report > Unity' 카테고리의 다른 글

Unity Lightweight PBR Sample Shader  (0) 2019.05.24
Unite Seoul 2019 Training Day - Surface Shader for Artist  (0) 2019.05.23
Unity SimpleLit HLSL  (0) 2019.04.08
Unity Surface LightDir  (0) 2019.02.23
Surface Shader Stencil buffer toggle  (0) 2019.02.19