65 lines
2.0 KiB
C#
65 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
[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;
|
|
}
|
|
|
|
Vector3[] positions = poses.Select(ConvertRobotDataToVector3).ToArray();
|
|
|
|
lineRenderer.positionCount = positions.Length;
|
|
lineRenderer.SetPositions(positions);
|
|
}
|
|
|
|
// 실시간 경로 그리기
|
|
public void DrawPath(List<RobotData> poses, int modifiedIndex, RobotData tempPose)
|
|
{
|
|
if (poses == null || poses.Count < 2 ||
|
|
modifiedIndex < 0 || modifiedIndex >= poses.Count)
|
|
{
|
|
DrawPath(poses);
|
|
return;
|
|
}
|
|
|
|
Vector3[] positions = poses.Select(ConvertRobotDataToVector3).ToArray();
|
|
|
|
positions[modifiedIndex] = ConvertRobotDataToVector3(tempPose);
|
|
|
|
lineRenderer.positionCount = positions.Length;
|
|
lineRenderer.SetPositions(positions);
|
|
}
|
|
}
|