using System.Collections.Generic; using System.Globalization; using UnityEngine; namespace TKS { /// /// 지형 기믹 그림(PD 2026-11-10 「기획된 지형을 실제 게임 맵에 화풍에 맞게」): 표 `Resources/Gimmicks/gimmick_art.csv` 한 줄 = 기믹 1종의 그리는 법. /// 지대(zone) = 모양(원·사각) 거리장 + 가장자리 흔들림 + 월드 좌표 반복 타일(`Resources/Gimmicks/Tiles/`) + 테두리 띠 · 그늘 · 절벽/앞면 + 외곽선을 전투 시작 때 한 장으로 굽는다 /// (지대 모양·크기가 스테이지마다 달라서 그림 한 장을 늘리면 찌그러짐 · 기획서 「지대 타일 1장(반복)」). /// 물체(object · scatter · strip · gate · banner) = `Resources/Gimmicks/Art/` 스프라이트를 비율 유지로 세움(늘리지 않음). /// 원형 = 조직 작업 폴더 `C:\ArtRnD\samguk_terrain\proto.py`(같은 식 · 파이썬 시안). /// public static class GimmickArt { public const string TablePath = "Gimmicks/gimmick_art"; public const string TileFolder = "Gimmicks/Tiles/"; public const string ArtFolder = "Gimmicks/Art/"; public class Style { public string key, mode, tile, baseKey, baseAlt, sprite, spriteAlt; public float tileT = 2f, alpha = 1f, rimA = 1f, flatK, mul = 1f, rim, shadeTop, shadeBottom, cliff, face, faceMul = 0.7f; public float outline = 0.045f, outlineA = 1f, wobble, wfreq = 1.6f, scallop, corner = 0.4f, post, ppu = 100f; public float objW = 1f, objDy, cellW = 1.5f, cellH = 1f, period = 0.5f; public float objA = 1f, jitter, extB, extT, extW; // 물체 불투명도 · 크기 흔들림(±비율) · patch 가 모양 밖 아래/위로 더 그리는 길이(두께 띠·절벽) · 좌우 합 여유 public bool faceTex, rimLR, flip, objGround; // flip = 흩뿌린 물체 좌우 무작위 반전 · objGround = 흩뿌린 물체를 바닥 그림과 같은 층(물결 · 다리보다 아래) public Vector4 border; // patch 9-slice 테두리 px(왼 · 아래 · 오른 · 위) public string spriteV = ""; // strip 세로(높이 > 폭) 마디 그림(; 3개 = 위끝·가운데·아래끝 · 폭 = 모양 폭 · 이음 없이 세로로 이어 붙임 · 성벽 세로 구간 · 2026-11-11) public string spriteRound = "", spriteOval = "", spriteLong = "", edge = "", edgeSprite = ""; // 원형 전용 그림 · 타원 통째 그림 · 가장자리 소품(top|bottom|both) · 그 그림(; 여러 개) public float ovalMax = 3f; // 가로세로비가 이 이하면 sprite_oval 통째 · 넘으면 sprite(9-slice) public float keepAspect, circleFill = 0.8f; // circleFill = 원형 흩뿌림에서 중심 거리 상한(반지름 배율) // 통째 그림 원래 가로세로비 유지 정도(0 = 모양에 꽉 · 1 = 그림 비율) public bool objOver; // obj_layer over = 흩뿌린 물체를 유닛 몸 위(정렬 1 · 체력 바 아래)에 · 안개가 캐릭터를 덮음(PD 2026-11-10) public float edgeGap = 1.2f, edgeW = 0.6f, shadow; // 가장자리 소품 간격·폭 · strip 바닥 그림자 불투명도 public Color flat = Color.white, rimCol = Color.white, shadeCol = new Color(0, 0, 0, 0), shadebCol = Color.white, faceCol = Color.gray; public Color outlineCol = new Color(0.19f, 0.13f, 0.09f), postCol = Color.gray, outTint = Color.gray, debris = Color.gray; public float Drop { get { return Mathf.Max(cliff, face); } } } static Dictionary styles; static readonly Dictionary sprites = new Dictionary(); static readonly Dictionary tiles = new Dictionary(); static readonly Dictionary baked = new Dictionary(); static readonly HashSet used = new HashSet(); class TileData { public Color32[] px; public int w, h; } public static void Reload() { styles = null; sprites.Clear(); tiles.Clear(); foreach (var kv in patches) { if (kv.Value != null) Object.Destroy(kv.Value); } patches.Clear(); } public static Style Get(string key) { Ensure(); Style s; return key != null && styles.TryGetValue(key, out s) ? s : null; } /// 전투 시작 = 이번 전장에서 다시 쓰는 구운 그림만 남기고 나머지 텍스처를 놓는다(기믹 레이어 Build 앞뒤). public static void BeginBuild() { used.Clear(); } public static void EndBuild() { var drop = new List(); foreach (var kv in baked) { if (!used.Contains(kv.Key)) drop.Add(kv.Key); } foreach (var k in drop) { var sp = baked[k]; baked.Remove(k); if (sp != null) { if (sp.texture != null) Object.Destroy(sp.texture); Object.Destroy(sp); } } } public static Sprite Art(string name) { if (string.IsNullOrEmpty(name)) return null; Sprite s; if (!sprites.TryGetValue(name, out s)) { s = Resources.Load(ArtFolder + name); sprites[name] = s; } return s; } static void Ensure() { if (styles != null) return; styles = new Dictionary(); var ta = Resources.Load(TablePath); if (ta == null) return; string[] head = null; foreach (var raw in ta.text.Replace("\r", "").Split('\n')) { var line = raw.TrimStart('\uFEFF'); if (line.Trim().Length == 0 || line.TrimStart().StartsWith("#")) continue; var c = SkillVfx.SplitCsv(line); if (head == null) { head = c; continue; } var row = new Dictionary(); for (int i = 0; i < head.Length && i < c.Length; i++) row[head[i].Trim()] = c[i].Trim(); var st = Parse(row); if (st != null) styles[st.key] = st; } } static Style Parse(Dictionary r) { string key = S(r, "key"); if (key.Length == 0) return null; var s = new Style { key = key, mode = S(r, "mode"), tile = S(r, "tile"), baseKey = S(r, "base"), baseAlt = S(r, "base_alt"), sprite = S(r, "sprite"), spriteAlt = S(r, "sprite_alt") }; s.tileT = F(r, "tile_t", s.tileT); s.alpha = F(r, "alpha", s.alpha); s.flat = C(r, "flat", s.flat); s.flatK = F(r, "flat_k", 0f); s.mul = F(r, "mul", 1f); s.rim = F(r, "rim", 0f); s.rimCol = C(r, "rim_col", s.rimCol); s.rimA = F(r, "rim_a", s.alpha); // 테두리 띠 불투명도(비우면 채움과 같음 · 군기 원 = 진한 띠) s.rimLR = S(r, "rim_sides") == "lr"; s.shadeTop = F(r, "shade_top", 0f); s.shadeCol = C(r, "shade_col", s.shadeCol); s.shadeBottom = F(r, "shade_bottom", 0f); s.shadebCol = C(r, "shadeb_col", s.shadebCol); s.cliff = F(r, "cliff", 0f); s.face = F(r, "face", 0f); s.faceCol = C(r, "face_col", s.faceCol); s.faceMul = F(r, "face_mul", s.faceMul); s.faceTex = S(r, "face_tex") == "1"; s.outline = F(r, "outline", s.outline); s.outlineCol = C(r, "outline_col", s.outlineCol); s.outlineA = F(r, "outline_a", 1f); s.wobble = F(r, "wobble", 0f); s.wfreq = F(r, "wfreq", s.wfreq); s.scallop = F(r, "scallop", 0f); s.corner = F(r, "corner", s.corner); s.post = F(r, "post", 0f); s.postCol = C(r, "post_col", s.postCol); s.objW = F(r, "obj_w", 1f); s.objDy = F(r, "obj_dy", 0f); s.cellW = F(r, "cell_w", s.cellW); s.cellH = F(r, "cell_h", s.cellH); s.period = F(r, "period", s.period); s.ppu = F(r, "ppu", s.ppu); s.outTint = C(r, "out_tint", s.outTint); s.debris = C(r, "debris", s.debris); s.objA = F(r, "obj_a", 1f); s.jitter = F(r, "jitter", 0f); s.flip = S(r, "flip") == "1"; s.extB = F(r, "ext_b", 0f); s.extT = F(r, "ext_t", 0f); s.extW = F(r, "ext_w", 0f); s.spriteRound = S(r, "sprite_round"); s.spriteOval = S(r, "sprite_oval"); s.spriteLong = S(r, "sprite_long"); s.spriteV = S(r, "sprite_v"); s.keepAspect = F(r, "keep_aspect", 0f); s.circleFill = F(r, "circle_fill", s.circleFill); s.objOver = S(r, "obj_layer") == "over"; s.ovalMax = F(r, "oval_max", s.ovalMax); s.edge = S(r, "edge"); s.edgeSprite = S(r, "edge_sprite"); s.edgeGap = F(r, "edge_gap", s.edgeGap); s.edgeW = F(r, "edge_w", s.edgeW); s.shadow = F(r, "shadow", 0f); s.objGround = S(r, "obj_layer") == "ground"; var b = S(r, "border").Split(';'); if (b.Length == 4) { float l, bt, rt, t; if (float.TryParse(b[0], NumberStyles.Float, CultureInfo.InvariantCulture, out l) && float.TryParse(b[1], NumberStyles.Float, CultureInfo.InvariantCulture, out bt) && float.TryParse(b[2], NumberStyles.Float, CultureInfo.InvariantCulture, out rt) && float.TryParse(b[3], NumberStyles.Float, CultureInfo.InvariantCulture, out t)) s.border = new Vector4(l, bt, rt, t); } return s; } static readonly Dictionary patches = new Dictionary(); /// patch(9-slice) 스프라이트 = 그림(Art) 텍스처 + 표의 테두리 px · ppu 로 실행 중 만든다(임포트 설정 없이 표만 고치면 바뀜 · 크기가 달라도 외곽선 두께 고정). public static Sprite PatchSprite(Style st, string whole = null) { if (st == null) return null; string name = string.IsNullOrEmpty(whole) ? st.sprite : whole; var border = string.IsNullOrEmpty(whole) ? st.border : Vector4.zero; // 통째 그림 = 테두리 없이 모양 크기로 늘림 string key = st.key + "|" + name + "|" + border + "|" + st.ppu; Sprite sp; if (patches.TryGetValue(key, out sp) && sp != null) return sp; var src = Art(name); if (src == null) return null; sp = Sprite.Create(src.texture, src.rect, new Vector2(0.5f, 0.5f), Mathf.Max(1f, st.ppu), 0, SpriteMeshType.FullRect, border); sp.name = "Patch_" + st.key; patches[key] = sp; return sp; } static string S(Dictionary r, string k) { string v; return r.TryGetValue(k, out v) ? v : ""; } static float F(Dictionary r, string k, float def) { float f; return float.TryParse(S(r, k), NumberStyles.Float, CultureInfo.InvariantCulture, out f) ? f : def; } static Color C(Dictionary r, string k, Color def) { Color c; string v = S(r, k); return v.Length > 0 && ColorUtility.TryParseHtmlString(v, out c) ? c : def; } static TileData Tile(string name) { if (string.IsNullOrEmpty(name)) return null; TileData t; if (tiles.TryGetValue(name, out t)) return t; var tex = Resources.Load(TileFolder + name); t = null; if (tex != null && tex.isReadable) t = new TileData { px = tex.GetPixels32(), w = tex.width, h = tex.height }; else if (tex != null) Debug.LogWarning("[TKS] 기믹 타일 읽기 불가(Read/Write 꺼짐): " + name); tiles[name] = t; return t; } // ---------- 지대 굽기 ---------- /// 지대 한 장(중심 = 모양 중심 · 축 정렬 · 회전은 렌더러가). 같은 모양·위치면 캐시(전투마다 BeginBuild/EndBuild 로 정리). public static Sprite BakeZone(Style st, GimmickShape s) { if (st == null || s.Type == GimmickShapeType.Poly) return null; bool circle = s.Type == GimmickShapeType.Circle; float hw = circle ? s.R : Mathf.Abs(s.Size.x) * 0.5f; float hh = circle ? s.R : Mathf.Abs(s.Size.y) * 0.5f; string key = st.key + "|" + (circle ? "c" : "r") + "|" + hw.ToString("0.###", CultureInfo.InvariantCulture) + "|" + hh.ToString("0.###", CultureInfo.InvariantCulture) + "|" + s.Center.x.ToString("0.##", CultureInfo.InvariantCulture) + "|" + s.Center.y.ToString("0.##", CultureInfo.InvariantCulture); Sprite cached; if (baked.TryGetValue(key, out cached) && cached != null) { used.Add(key); return cached; } var tile = Tile(st.tile); if (tile == null) return null; float ppu = Mathf.Max(16f, st.ppu), px = 1f / ppu; float padT = 0.12f + st.wobble + st.outline, padB = padT + st.Drop; float x0 = -hw - padT, x1 = hw + padT, y0 = -hh - padB, y1 = hh + padT; int W = Mathf.CeilToInt((x1 - x0) * ppu), H = Mathf.CeilToInt((y1 - y0) * ppu); if (W <= 0 || H <= 0 || W > 4096 || H > 4096) return null; float cx = s.Center.x, cy = s.Center.y; float seed = Mathf.Abs(cx * 7.31f + cy * 3.17f) % 9.7f + 1f; var shape = new ShapeEval { circle = circle, hw = hw, hh = hh, corner = Mathf.Min(st.corner, hw, hh), st = st, seed = seed, cx = cx, cy = cy }; var d = new float[W * H]; var inside = new bool[W * H]; for (int y = 0; y < H; y++) { float ly = y1 - (y + 0.5f) * px; for (int x = 0; x < W; x++) { float lx = x0 + (x + 0.5f) * px; float v = shape.D(lx, ly); d[y * W + x] = v; inside[y * W + x] = v < 0f; } } // 아래로 내려선 면(고지 절벽 · 성벽 앞면) = 열마다 위쪽 마지막 안쪽 칸까지 거리 ≤ drop int hp = Mathf.RoundToInt(st.Drop * ppu); var faceDist = hp > 0 ? new int[W * H] : null; if (hp > 0) { for (int x = 0; x < W; x++) { int last = -1000000; for (int y = 0; y < H; y++) { int i = y * W + x; if (inside[i]) last = y; faceDist[i] = inside[i] ? 0 : y - last; } } } int k = Mathf.Max(1, Mathf.RoundToInt(st.outline * ppu)); int band = Mathf.Max(2, Mathf.RoundToInt(0.07f * ppu)); var outPx = new Color32[W * H]; var rimC = st.rimCol; for (int y = 0; y < H; y++) { float ly = y1 - (y + 0.5f) * px; for (int x = 0; x < W; x++) { int i = y * W + x; float lx = x0 + (x + 0.5f) * px; float dv = d[i]; Color acc = new Color(0, 0, 0, 0); // 1) 앞면 bool isFace = hp > 0 && !inside[i] && faceDist[i] > 0 && faceDist[i] <= hp; if (isFace) { Color fc = st.faceCol; if (st.faceTex) { Color t = Sample(tile, lx + cx, ly + cy - st.Drop * 0.5f, st.tileT); fc = new Color(t.r * st.faceMul * 0.5f + fc.r * 0.5f, t.g * st.faceMul * 0.5f + fc.g * 0.5f, t.b * st.faceMul * 0.5f + fc.b * 0.5f, 1f); } if (faceDist[i] > hp - band) fc = new Color(fc.r * 0.75f, fc.g * 0.75f, fc.b * 0.75f, 1f); acc = fc; // 앞면 둘레 외곽선(좌우 이웃 · 아래가 밖) if (!InU(inside, faceDist, hp, W, H, x - k, y) || !InU(inside, faceDist, hp, W, H, x + k, y) || !InU(inside, faceDist, hp, W, H, x, y + k)) acc = Over(acc, st.outlineCol, st.outlineA); } // 2) 윗면 float cov = Mathf.Clamp01(0.5f - dv / px); if (cov > 0f) { Color t = Sample(tile, lx + cx, ly + cy, st.tileT); Color rgb = new Color(t.r * st.mul, t.g * st.mul, t.b * st.mul, 1f); if (st.flatK > 0f) rgb = Color.Lerp(rgb, st.flat, st.flatK); if (st.shadeTop > 0f && shape.D(lx, ly + st.shadeTop) >= 0f) rgb = st.shadeCol.a > 0f ? new Color(st.shadeCol.r, st.shadeCol.g, st.shadeCol.b, 1f) : new Color(rgb.r * 0.68f, rgb.g * 0.68f, rgb.b * 0.68f, 1f); if (st.shadeBottom > 0f && shape.D(lx, ly - st.shadeBottom) >= 0f) rgb = new Color(st.shadebCol.r, st.shadebCol.g, st.shadebCol.b, 1f); bool onRim = false; if (st.rim > 0f) { if (st.rimLR && !circle) { if (Mathf.Abs(lx) > hw - st.rim - st.outline) rgb = new Color(rimC.r, rimC.g, rimC.b, 1f); if (st.post > 0f) { for (int sx = -1; sx <= 1; sx += 2) { for (int fy = -1; fy <= 1; fy++) { float pcx = sx * (hw - st.rim * 0.5f - st.outline * 0.5f), pcy = fy * 0.84f * hh; // 난간 기둥 = 좌우 × 위·가운데·아래 float dd = Mathf.Sqrt((lx - pcx) * (lx - pcx) + (ly - pcy) * (ly - pcy)) - st.post; if (dd < 0f) rgb = new Color(st.postCol.r, st.postCol.g, st.postCol.b, 1f); if (Mathf.Abs(dd + st.outline * 0.3f) < st.outline * 0.45f) rgb = new Color(st.outlineCol.r, st.outlineCol.g, st.outlineCol.b, 1f); } } } } else if (dv > -st.rim - st.outline) { rgb = new Color(rimC.r, rimC.g, rimC.b, 1f); onRim = true; } } rgb.a = cov * (onRim ? st.rimA : st.alpha); acc = Over(acc, rgb, 1f); } // 3) 외곽선(윗면 둘레) float ow = st.outline; float line = Mathf.Clamp01((ow * 0.5f - Mathf.Abs(dv + ow * 0.2f)) / px + 0.5f); if (line > 0f) acc = Over(acc, st.outlineCol, line * st.outlineA); int ty = H - 1 - y; // 텍스처 행 0 = 아래 outPx[ty * W + x] = acc; } } var tex = new Texture2D(W, H, TextureFormat.RGBA32, false) { wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Bilinear, name = "Gim_" + st.key }; tex.SetPixels32(outPx); tex.Apply(false, true); var pivot = new Vector2((0f - x0) / (x1 - x0), (0f - y0) / (y1 - y0)); var sp = Sprite.Create(tex, new Rect(0, 0, W, H), pivot, ppu, 0, SpriteMeshType.FullRect); sp.name = tex.name; baked[key] = sp; used.Add(key); return sp; } static bool InU(bool[] inside, int[] faceDist, int hp, int W, int H, int x, int y) { if (x < 0 || y < 0 || x >= W || y >= H) return false; int i = y * W + x; return inside[i] || (faceDist[i] > 0 && faceDist[i] <= hp); } static Color Over(Color dst, Color src, float a) { float sa = Mathf.Clamp01(src.a * a); float oa = sa + dst.a * (1f - sa); if (oa <= 1e-5f) return new Color(0, 0, 0, 0); float k = dst.a * (1f - sa); return new Color((src.r * sa + dst.r * k) / oa, (src.g * sa + dst.g * k) / oa, (src.b * sa + dst.b * k) / oa, oa); } /// 타일 반복 샘플(월드 좌표 · T = 타일 한 장의 월드 크기 · 선형 보간). static Color Sample(TileData t, float wx, float wy, float T) { float u = Frac(wx / T) * t.w - 0.5f, v = Frac(wy / T) * t.h - 0.5f; int xa = Mathf.FloorToInt(u), ya = Mathf.FloorToInt(v); float fx = u - xa, fy = v - ya; Color32 c00 = P(t, xa, ya), c10 = P(t, xa + 1, ya), c01 = P(t, xa, ya + 1), c11 = P(t, xa + 1, ya + 1); float w00 = (1 - fx) * (1 - fy), w10 = fx * (1 - fy), w01 = (1 - fx) * fy, w11 = fx * fy; const float n = 1f / 255f; return new Color((c00.r * w00 + c10.r * w10 + c01.r * w01 + c11.r * w11) * n, (c00.g * w00 + c10.g * w10 + c01.g * w01 + c11.g * w11) * n, (c00.b * w00 + c10.b * w10 + c01.b * w01 + c11.b * w11) * n, 1f); } static Color32 P(TileData t, int x, int y) { x %= t.w; if (x < 0) x += t.w; y %= t.h; if (y < 0) y += t.h; return t.px[y * t.w + x]; } static float Frac(float v) { return v - Mathf.Floor(v); } /// 모양 거리장(음수 = 안) + 가장자리 흔들림(월드 좌표 · 같은 자리면 같은 모양). struct ShapeEval { public bool circle; public float hw, hh, corner, seed, cx, cy; public Style st; public float D(float lx, float ly) { float d; if (circle) d = Mathf.Sqrt(lx * lx + ly * ly) - hw; else { float qx = Mathf.Abs(lx) - (hw - corner), qy = Mathf.Abs(ly) - (hh - corner); float ox = Mathf.Max(qx, 0f), oy = Mathf.Max(qy, 0f); d = Mathf.Sqrt(ox * ox + oy * oy) + Mathf.Min(Mathf.Max(qx, qy), 0f) - corner; } return d + Wob(lx + cx, ly + cy); } float Wob(float wx, float wy) { if (st.wobble <= 0f) return 0f; if (st.scallop > 0f) { float lam = st.scallop; return st.wobble * (1f - 0.5f * (Mathf.Abs(Mathf.Sin(Mathf.PI * (wx + seed * 0.37f) / lam)) + Mathf.Abs(Mathf.Sin(Mathf.PI * (wy + seed * 0.61f) / lam)))); } float a = seed * 1.618f, f = st.wfreq; return st.wobble * (0.55f * Mathf.Sin(f * wx + 0.7f * f * wy + a) + 0.30f * Mathf.Sin(f * 2.13f * wy - 0.9f * f * wx + 2.1f * a) + 0.15f * Mathf.Sin(f * 4.7f * (wx + wy) + 3.3f * a)); } } } }