Files
HDRobotics/Assets/Scripts/View/PointManagerView.cs

107 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
public class PointManagerView : MonoBehaviour
{
[SerializeField] private GameObject pointPrefab;
private List<GameObject> activePoints = new List<GameObject>();
public Transform robotBaseTransform; // 로봇좌표의 기준이 되는 축 위치
[SerializeField] public GameObject movingAlert;
[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);
}
private Quaternion ConvertRobotDataToQuaternion(RobotData pose)
{
float rx = pose.rx;
float ry = pose.z;
float rz = pose.y;
return Quaternion.Euler(pose.rx, -pose.rz, pose.ry);
}
public void CreatePoint(RobotData pose, int index)
{
if(pointPrefab == null)
{
Debug.Log("마커 프리팹이 할당되지 않았습니다.");
return;
}
Vector3 position = ConvertRobotDataToVector3(pose);
//Quaternion localRot = ConvertRobotDataToQuaternion(pose);
//Vector3 toolOffset = new Vector3(0.681004044f, -0.221942063f, -0.447616011f);
//Vector3 rotatedOffset = localRot * toolOffset;
//Vector3 finalLocalPos = localPos + rotatedOffset;
//Vector3 worldPos = localPos;
////Quaternion worldRot = localRot;
//// 로봇 Base가 있다면, 로컬 좌표를 월드 좌표로 변환
//if (robotBaseTransform != null)
//{
// worldPos = robotBaseTransform.TransformPoint(localPos);
// //worldRot = robotBaseTransform.rotation * localRot;
//}
//else
//{
// Debug.LogWarning("PointManagerView: RobotBaseTransform이 할당되지 않아 월드 원점 기준으로 생성됩니다.");
//}
// 변환된 월드 좌표에 생성
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스크립트가 없음");
}
}
public void UpdatePointPosition(int index, Vector3 pose)
{
if (index < 0 || index >= activePoints.Count) return;
activePoints[index].transform.position = pose;
}
public void DeletePoint(int index)
{
if (index < 0 || index >= activePoints.Count) return;
Destroy(activePoints[index]);
activePoints.RemoveAt(index);
}
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);
}
}