303 lines
13 KiB
C#
303 lines
13 KiB
C#
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
namespace TKS
|
||
{
|
||
/// <summary>아트 리소스 없이 쓰는 도형 스프라이트(원·링·얇은 링·도넛·사각). 프로토타입 전용.</summary>
|
||
public static class ProceduralSprites
|
||
{
|
||
static Sprite circle;
|
||
static Sprite ring;
|
||
static Sprite thinRing;
|
||
static Sprite square;
|
||
static readonly Dictionary<int, Sprite> donuts = new Dictionary<int, Sprite>();
|
||
|
||
/// <summary>바깥 지름 1 월드 단위의 얇은 흰 링(두께 3 %) — 사거리 표시용.</summary>
|
||
public static Sprite ThinRing
|
||
{
|
||
get
|
||
{
|
||
if (thinRing == null) thinRing = MakeRing(256, 0.03f);
|
||
return thinRing;
|
||
}
|
||
}
|
||
|
||
static Sprite softDisc;
|
||
static Sprite chevron;
|
||
static Texture2D dashTex;
|
||
|
||
/// <summary>
|
||
/// 지름 1 의 은은한 원판(가운데 투명 → 가장자리로 갈수록 진해지고 끝은 부드럽게 끊김 · 512 px) — 사거리 원판(표 ib_ui range_style 2).
|
||
/// 부드러운 그라데이션이라 크게 늘려도 계단이 보이지 않는다.
|
||
/// </summary>
|
||
public static Sprite SoftDisc
|
||
{
|
||
get
|
||
{
|
||
if (softDisc != null) return softDisc;
|
||
const int size = 512;
|
||
var tex = new Texture2D(size, size, TextureFormat.RGBA32, false) { wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Bilinear };
|
||
float half = size * 0.5f;
|
||
var px = new Color[size * size];
|
||
for (int y = 0; y < size; y++)
|
||
for (int x = 0; x < size; x++)
|
||
{
|
||
float d = Mathf.Sqrt((x + 0.5f - half) * (x + 0.5f - half) + (y + 0.5f - half) * (y + 0.5f - half)) / half;
|
||
float a = Mathf.SmoothStep(0f, 1f, Mathf.InverseLerp(0.35f, 1f, d)) * Mathf.Clamp01((1f - d) * half);
|
||
px[y * size + x] = new Color(1f, 1f, 1f, a);
|
||
}
|
||
tex.SetPixels(px);
|
||
tex.Apply();
|
||
tex.hideFlags = HideFlags.DontSave;
|
||
softDisc = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f), size);
|
||
softDisc.hideFlags = HideFlags.DontSave;
|
||
return softDisc;
|
||
}
|
||
}
|
||
|
||
static Sprite glowRing;
|
||
static Sprite radialGlow;
|
||
static Sprite reticle;
|
||
|
||
/// <summary>
|
||
/// 지름 1 의 빛나는 고리 띠(공격 인디케이터 바깥 빛 · PD 2026-11-24 예시) — 가장자리(0.97)에서 가장 진하고 안쪽으로 0.18 만큼 부드럽게 옅어짐 · 바깥은 짧게 끊김(512 px).
|
||
/// </summary>
|
||
public static Sprite GlowRing
|
||
{
|
||
get
|
||
{
|
||
if (glowRing != null) return glowRing;
|
||
glowRing = MakeRadial(512, d => d > 1f ? 0f : d >= 0.97f ? Mathf.Clamp01((1f - d) / 0.03f) : Mathf.Pow(Mathf.Clamp01(1f - (0.97f - d) / 0.18f), 2f));
|
||
return glowRing;
|
||
}
|
||
}
|
||
|
||
/// <summary>지름 1 의 가운데 빛(가운데 진함 → 가장자리 0 · 512 px) — 공격 인디케이터 바닥 빛.</summary>
|
||
public static Sprite RadialGlow
|
||
{
|
||
get
|
||
{
|
||
if (radialGlow != null) return radialGlow;
|
||
radialGlow = MakeRadial(512, d => d >= 1f ? 0f : Mathf.Pow(1f - d, 2.2f));
|
||
return radialGlow;
|
||
}
|
||
}
|
||
|
||
/// <summary>중심 표적(네 방향 별 + 고리 둘 + 가운데 점 · 256 px · 크기 1) — 공격 인디케이터 가운데(시전 중심).</summary>
|
||
public static Sprite Reticle
|
||
{
|
||
get
|
||
{
|
||
if (reticle != null) return reticle;
|
||
reticle = MakeRadialXY(256, (x, y) =>
|
||
{
|
||
float d = Mathf.Sqrt(x * x + y * y);
|
||
float a = 0f;
|
||
// 네 방향 별(가로 · 세로 마름모 가시)
|
||
float ax = Mathf.Abs(x), ay = Mathf.Abs(y);
|
||
float spike = Mathf.Max(Mathf.Clamp01((0.11f * (1f - ax / 0.95f) - ay) * 60f), Mathf.Clamp01((0.11f * (1f - ay / 0.95f) - ax) * 60f));
|
||
a = Mathf.Max(a, spike);
|
||
a = Mathf.Max(a, Mathf.Clamp01((0.045f - Mathf.Abs(d - 0.36f)) * 60f)); // 바깥 고리
|
||
a = Mathf.Max(a, Mathf.Clamp01((0.035f - Mathf.Abs(d - 0.25f)) * 60f)); // 안 고리
|
||
a = Mathf.Max(a, Mathf.Clamp01((0.16f - d) * 60f) * 0.85f); // 가운데 점
|
||
return a;
|
||
});
|
||
return reticle;
|
||
}
|
||
}
|
||
|
||
static Sprite MakeRadial(int size, System.Func<float, float> alpha)
|
||
{
|
||
return MakeRadialXY(size, (x, y) => alpha(Mathf.Sqrt(x * x + y * y)));
|
||
}
|
||
|
||
/// <summary>가운데 (0,0) · 가장자리 ±1 좌표의 알파 함수로 흰 스프라이트(지름 1)를 만든다.</summary>
|
||
static Sprite MakeRadialXY(int size, System.Func<float, float, float> alpha)
|
||
{
|
||
var tex = new Texture2D(size, size, TextureFormat.RGBA32, false) { wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Bilinear };
|
||
float half = size * 0.5f;
|
||
var px = new Color[size * size];
|
||
for (int y = 0; y < size; y++)
|
||
for (int x = 0; x < size; x++)
|
||
px[y * size + x] = new Color(1f, 1f, 1f, Mathf.Clamp01(alpha((x + 0.5f - half) / half, (y + 0.5f - half) / half)));
|
||
tex.SetPixels(px);
|
||
tex.Apply();
|
||
tex.hideFlags = HideFlags.DontSave;
|
||
var s = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f), size);
|
||
s.hideFlags = HideFlags.DontSave;
|
||
return s;
|
||
}
|
||
|
||
/// <summary>위(+y)를 가리키는 둥근 꺾쇠(⌃ 모양 · 128 px · 크기 1) — 사거리 원 위 「공격 가능한 적 방향」 표시(range_style 3).</summary>
|
||
public static Sprite Chevron
|
||
{
|
||
get
|
||
{
|
||
if (chevron != null) return chevron;
|
||
const int size = 128;
|
||
var tex = new Texture2D(size, size, TextureFormat.RGBA32, false) { wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Bilinear };
|
||
var px = new Color[size * size];
|
||
Vector2 tip = new Vector2(0.5f, 0.82f), l = new Vector2(0.12f, 0.3f), r = new Vector2(0.88f, 0.3f);
|
||
for (int y = 0; y < size; y++)
|
||
for (int x = 0; x < size; x++)
|
||
{
|
||
var p = new Vector2((x + 0.5f) / size, (y + 0.5f) / size);
|
||
float d = Mathf.Min(SegDist(p, l, tip), SegDist(p, r, tip));
|
||
float a = Mathf.Clamp01((0.11f - d) * size * 0.5f);
|
||
px[y * size + x] = new Color(1f, 1f, 1f, a);
|
||
}
|
||
tex.SetPixels(px);
|
||
tex.Apply();
|
||
tex.hideFlags = HideFlags.DontSave;
|
||
chevron = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f), size);
|
||
chevron.hideFlags = HideFlags.DontSave;
|
||
return chevron;
|
||
}
|
||
}
|
||
|
||
static float SegDist(Vector2 p, Vector2 a, Vector2 b)
|
||
{
|
||
Vector2 ab = b - a;
|
||
float t = Mathf.Clamp01(Vector2.Dot(p - a, ab) / ab.sqrMagnitude);
|
||
return Vector2.Distance(p, a + ab * t);
|
||
}
|
||
|
||
/// <summary>점선 무늬(가로로 반복 · 앞 fill 비율만 흰색 · 끝은 부드럽게) — 선 그리기(LineRenderer Tile)용.</summary>
|
||
public static Texture2D DashTexture(float fill)
|
||
{
|
||
if (dashTex != null) return dashTex;
|
||
const int w = 64;
|
||
dashTex = new Texture2D(w, 4, TextureFormat.RGBA32, false) { wrapMode = TextureWrapMode.Repeat, filterMode = FilterMode.Bilinear };
|
||
var px = new Color[w * 4];
|
||
for (int x = 0; x < w; x++)
|
||
{
|
||
float u = (x + 0.5f) / w;
|
||
float a = Mathf.Clamp01(Mathf.Min(u, fill - u) * w * 0.5f);
|
||
for (int y = 0; y < 4; y++) px[y * w + x] = new Color(1f, 1f, 1f, u < fill ? a : 0f);
|
||
}
|
||
dashTex.SetPixels(px);
|
||
dashTex.Apply();
|
||
dashTex.hideFlags = HideFlags.DontSave;
|
||
return dashTex;
|
||
}
|
||
|
||
/// <summary>바깥 지름 1 · 안쪽 지름 innerFrac(0~1) 의 채워진 도넛 — 궁병 공격 가능 범위 표시용.</summary>
|
||
public static Sprite Donut(float innerFrac)
|
||
{
|
||
int key = Mathf.RoundToInt(Mathf.Clamp01(innerFrac) * 1000f);
|
||
Sprite s;
|
||
if (!donuts.TryGetValue(key, out s))
|
||
{
|
||
s = MakeRing(256, 1f - key / 1000f);
|
||
donuts[key] = s;
|
||
}
|
||
return s;
|
||
}
|
||
|
||
/// <summary>지름 1 월드 단위의 흰 원.</summary>
|
||
public static Sprite Circle
|
||
{
|
||
get
|
||
{
|
||
if (circle == null) circle = MakeCircle(128);
|
||
return circle;
|
||
}
|
||
}
|
||
|
||
/// <summary>바깥 지름 1 월드 단위의 흰 링(두께 12 %).</summary>
|
||
public static Sprite Ring
|
||
{
|
||
get
|
||
{
|
||
if (ring == null) ring = MakeRing(128, 0.12f);
|
||
return ring;
|
||
}
|
||
}
|
||
|
||
/// <summary>1×1 월드 단위의 흰 사각형.</summary>
|
||
public static Sprite Square
|
||
{
|
||
get
|
||
{
|
||
if (square == null)
|
||
{
|
||
var tex = new Texture2D(4, 4, TextureFormat.RGBA32, false);
|
||
var px = new Color[16];
|
||
for (int i = 0; i < px.Length; i++) px[i] = Color.white;
|
||
tex.SetPixels(px);
|
||
tex.Apply();
|
||
tex.hideFlags = HideFlags.DontSave;
|
||
square = Sprite.Create(tex, new Rect(0, 0, 4, 4), new Vector2(0.5f, 0.5f), 4f);
|
||
square.hideFlags = HideFlags.DontSave;
|
||
}
|
||
return square;
|
||
}
|
||
}
|
||
|
||
static Sprite MakeCircle(int size)
|
||
{
|
||
var tex = new Texture2D(size, size, TextureFormat.RGBA32, false);
|
||
tex.wrapMode = TextureWrapMode.Clamp;
|
||
tex.filterMode = FilterMode.Bilinear;
|
||
float half = size * 0.5f;
|
||
var px = new Color[size * size];
|
||
for (int y = 0; y < size; y++)
|
||
{
|
||
for (int x = 0; x < size; x++)
|
||
{
|
||
float dx = x + 0.5f - half;
|
||
float dy = y + 0.5f - half;
|
||
float d = Mathf.Sqrt(dx * dx + dy * dy);
|
||
float a = Mathf.Clamp01(half - d); // 1 px 안티앨리어싱
|
||
px[y * size + x] = new Color(1f, 1f, 1f, a);
|
||
}
|
||
}
|
||
tex.SetPixels(px);
|
||
tex.Apply();
|
||
tex.hideFlags = HideFlags.DontSave;
|
||
var s = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f), size);
|
||
s.hideFlags = HideFlags.DontSave;
|
||
return s;
|
||
}
|
||
|
||
static Sprite MakeRing(int size, float thicknessFrac)
|
||
{
|
||
var tex = new Texture2D(size, size, TextureFormat.RGBA32, false);
|
||
tex.wrapMode = TextureWrapMode.Clamp;
|
||
tex.filterMode = FilterMode.Bilinear;
|
||
float half = size * 0.5f;
|
||
float inner = half * (1f - thicknessFrac);
|
||
var px = new Color[size * size];
|
||
for (int y = 0; y < size; y++)
|
||
{
|
||
for (int x = 0; x < size; x++)
|
||
{
|
||
float dx = x + 0.5f - half;
|
||
float dy = y + 0.5f - half;
|
||
float d = Mathf.Sqrt(dx * dx + dy * dy);
|
||
float a = Mathf.Clamp01(half - d) * Mathf.Clamp01(d - inner);
|
||
px[y * size + x] = new Color(1f, 1f, 1f, a);
|
||
}
|
||
}
|
||
tex.SetPixels(px);
|
||
tex.Apply();
|
||
tex.hideFlags = HideFlags.DontSave;
|
||
var s = Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f), size);
|
||
s.hideFlags = HideFlags.DontSave;
|
||
return s;
|
||
}
|
||
|
||
public static SpriteRenderer NewSprite(Transform parent, string name, Sprite sprite, Color color, float scale, int sortingOrder)
|
||
{
|
||
var go = new GameObject(name);
|
||
go.transform.SetParent(parent, false);
|
||
var sr = go.AddComponent<SpriteRenderer>();
|
||
sr.sprite = sprite;
|
||
sr.color = color;
|
||
sr.sortingOrder = sortingOrder;
|
||
go.transform.localScale = new Vector3(scale, scale, 1f);
|
||
return sr;
|
||
}
|
||
}
|
||
}
|