84 lines
3.0 KiB
C#
84 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 로봇의 이동 경로(Path)를 3D 공간 상에 시각적으로 그려주는 뷰(View) 클래스
|
|
/// Unity의 LineRenderer 컴포넌트를 사용하여 점과 점 사이를 선으로 연결
|
|
/// </summary>
|
|
|
|
// 자동으로 LineRenderer 컴포넌트 추가
|
|
[RequireComponent(typeof(LineRenderer))]
|
|
public class PathLineView : MonoBehaviour
|
|
{
|
|
private LineRenderer lineRenderer;
|
|
|
|
void Awake()
|
|
{
|
|
lineRenderer = GetComponent<LineRenderer>();
|
|
// Line Renderer 기본 설정
|
|
lineRenderer.startWidth = 0.01f;
|
|
lineRenderer.endWidth = 0.01f;
|
|
lineRenderer.material = new Material(Shader.Find("Legacy Shaders/Particles/Alpha Blended Premultiply"));
|
|
lineRenderer.startColor = Color.cyan;
|
|
lineRenderer.endColor = Color.cyan;
|
|
}
|
|
|
|
private Vector3 ConvertRobotDataToVector3(RobotData pose)
|
|
{
|
|
float x = Convert.ToSingle(pose.x / -1000.0); // mm -> 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>
|
|
/// 프로그램의 모든 포인트를 순서대로 잇는 선을 그림
|
|
/// </summary>
|
|
public void DrawPath(List<RobotData> poses)
|
|
{
|
|
if (poses == null || poses.Count < 2)
|
|
{
|
|
lineRenderer.positionCount = 0; // 포인트가 2개 미만이면 선을 지움
|
|
return;
|
|
}
|
|
|
|
// LINQ의 Select를 사용하여 리스트의 모든 RobotData를 Vector3 배열로 변환
|
|
Vector3[] positions = poses.Select(ConvertRobotDataToVector3).ToArray();
|
|
|
|
// LineRenderer에 정점 개수와 위치 배열 전달
|
|
lineRenderer.positionCount = positions.Length;
|
|
lineRenderer.SetPositions(positions);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 특정 포인트를 드래그하여 수정 중일 때, 경로를 실시간으로 갱신하여 그림
|
|
/// (동적 경로 미리보기용)
|
|
/// </summary>
|
|
/// <param name="poses">기존 포인트 리스트</param>
|
|
/// <param name="modifiedIndex">현재 수정(드래그) 중인 포인트의 인덱스</param>
|
|
/// <param name="tempPose">수정 중인 포인트의 실시간 임시 좌표</param>
|
|
public void DrawPath(List<RobotData> poses, int modifiedIndex, RobotData tempPose)
|
|
{
|
|
// 유효성 검사: 인덱스가 범위를 벗어나면 기존 경로대로 그리기
|
|
if (poses == null || poses.Count < 2 ||
|
|
modifiedIndex < 0 || modifiedIndex >= poses.Count)
|
|
{
|
|
DrawPath(poses);
|
|
return;
|
|
}
|
|
|
|
// 1. 기존 포인트들을 모두 Vector3 배열로 변환
|
|
Vector3[] positions = poses.Select(ConvertRobotDataToVector3).ToArray();
|
|
|
|
// 2. 수정 중인 인덱스의 좌표만 드래그 중인 임시 좌표(tempPose)로 덮어쓰기
|
|
// -> 원본 데이터(List)를 건드리지 않고도 시각적으로만 경로가 바뀐 것을 보여줄 수 있음
|
|
positions[modifiedIndex] = ConvertRobotDataToVector3(tempPose);
|
|
|
|
// 3. 갱신된 좌표 배열로 선 그리기
|
|
lineRenderer.positionCount = positions.Length;
|
|
lineRenderer.SetPositions(positions);
|
|
}
|
|
}
|