Files
HDRobotics/Assets/Scripts/View/PointManagerView.cs
2025-12-03 21:19:07 +09:00

104 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 로봇의 이동 경로 포인트(Point/Marker)를 3D 월드 상에 시각적으로 생성하고 관리하는 뷰 클래스
/// 포인트의 생성(Instantiate), 위치 갱신, 삭제, 전체 다시 그리기 기능을 담당
/// </summary>
public class PointManagerView : MonoBehaviour
{
[SerializeField] private GameObject pointPrefab; // 3D 공간에 생성될 포인트(마커) 프리팹
private List<GameObject> activePoints = new List<GameObject>(); // 현재 활성화된 포인트 게임 오브젝트들을 관리하는 리스트
public Transform robotBaseTransform; // 로봇좌표의 기준이 되는 축 위치
[SerializeField] public GameObject movingAlert; // 로봇이 이동 중일 때 사용자에게 경고를 주는 UI
/// <summary>
/// 드래그 시 보여질 반투명 고스트 로봇의 IK(Inverse Kinematics) 제어 노드
/// </summary>
[SerializeField]
[Tooltip("반투명 로봇 모델의 IK")]
public HybridInverseKinematicsNode kinematicsNode;
void Start()
{
movingAlert.SetActive(false);
}
private Vector3 ConvertRobotDataToVector3(RobotData pose)
{
float x = Convert.ToSingle(pose.x / -1000.0); // Robot X(mm) -> Unity X(m)
float y = Convert.ToSingle(pose.z / 1000.0); // Robot Z(mm) -> Unity Y(m)
float z = Convert.ToSingle(pose.y / -1000.0); // Robot Y(mm) -> Unity Z(m)
return new Vector3(x, y, z);
}
/// <summary>
/// 지정된 로봇 좌표(Pose)에 새로운 포인트(마커)를 생성
/// </summary>
/// <param name="pose">포인트의 위치 데이터</param>
/// <param name="index">리스트 상의 인덱스 (식별용)</param>
public void CreatePoint(RobotData pose, int index)
{
if(pointPrefab == null)
{
Debug.Log("마커 프리팹이 할당되지 않았습니다.");
return;
}
Vector3 position = ConvertRobotDataToVector3(pose);
// 변환된 월드 좌표에 생성
GameObject pointObj = Instantiate(pointPrefab, position, Quaternion.identity, this.transform);
Debug.Log($"마커 프리팹 생성됨, 위치: {position}");
Debug.Log($"로봇 기준점(root 좌표): {robotBaseTransform.position}");
//pointObj.transform.localRotation = Quaternion.identity;
activePoints.Add(pointObj);
RobotPoint pointComponent = pointObj.GetComponent<RobotPoint>();
if (pointComponent != null)
{
pointComponent.pointIndex = activePoints.Count - 1;
}
else
{
Debug.LogError("point프리팹에 RobotPoint스크립트가 없음");
}
}
/// <summary>
/// 특정 인덱스의 포인트 위치를 갱신 (실시간 드래그 등)
/// </summary>
/// <param name="index">갱신할 포인트 인덱스</param>
/// <param name="pose">새로운 Unity 월드 좌표</param>
public void UpdatePointPosition(int index, Vector3 pose)
{
if (index < 0 || index >= activePoints.Count) return;
activePoints[index].transform.position = pose;
}
/// <summary>
/// 특정 인덱스의 포인트를 삭제하고 리스트에서 제거
/// </summary>
public void DeletePoint(int index)
{
if (index < 0 || index >= activePoints.Count) return;
Destroy(activePoints[index]);
activePoints.RemoveAt(index);
}
/// <summary>
/// 기존 포인트들을 모두 지우고, 주어진 데이터 리스트대로 다시 그림
/// (프로그램 로드, 리셋, 대규모 변경 시 호출)
/// </summary>
public void RedrawPoints(List<RobotData> poses)
{
foreach (var point in activePoints) Destroy(point);
activePoints.Clear();
if (poses == null) return;
for (int i= 0; i < poses.Count; i++) CreatePoint(poses[i], i);
}
}