ThreeKingdomsSlingshot/Assets/Scripts/OutGame/UI/UiRope.cs

58 lines
2.0 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.UI;
namespace TKS.OutGame
{
/// <summary>
/// 처지는 줄(고삐 등) 한 가닥 — 두 점(이 그래픽 RectTransform 의 로컬 좌표) 사이를 가운데가 sag 만큼 아래로 처지는 곡선(2차 · t(1−t))으로 굵기 width 띠를 그린다.
/// 연무장 허브 시안(mk_mock.py 「말 매는 줄」 · 진한 띠 위에 밝은 띠 두 겹)과 같은 모양. 그림(텍스처) 없음 = 기본 UI 재질 한 묶음.
/// </summary>
public class UiRope : MaskableGraphic
{
public Vector2 a;
public Vector2 b;
public float sag;
public float width = 4f;
public int segments = 16;
public void Set(Vector2 from, Vector2 to, float sagPx, float w)
{
a = from;
b = to;
sag = sagPx;
width = w;
SetVerticesDirty();
}
Vector2 At(float t)
{
var p = Vector2.Lerp(a, b, t);
p.y -= sag * 4f * t * (1f - t); // 로컬 y 위 + → 아래로 처짐 = −
return p;
}
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
int n = Mathf.Max(1, segments);
float hw = width * 0.5f;
var c = (Color32)color;
for (int i = 0; i <= n; i++)
{
float t = i / (float)n;
var p = At(t);
var d = At(Mathf.Min(1f, t + 0.5f / n)) - At(Mathf.Max(0f, t - 0.5f / n));
var nrm = d.sqrMagnitude > 1e-6f ? new Vector2(-d.y, d.x).normalized * hw : Vector2.up * hw;
vh.AddVert(p + nrm, c, Vector2.zero);
vh.AddVert(p - nrm, c, Vector2.zero);
if (i > 0)
{
int k = i * 2;
vh.AddTriangle(k - 2, k - 1, k);
vh.AddTriangle(k - 1, k + 1, k);
}
}
}
}
}