ThreeKingdomsSlingshot/Tools/make_aim_arrow.py

163 lines
6.6 KiB
Python
Raw Normal View History

"""조준 화살표 그림(PD 2026-11-11) — IB skill-arrow_b(2단 촉) → 흰 그림 skill-arrow_b_filled(진영 색을 곱함).
모양 = 위 촉 ~ 아래 미늘 끝 사이·바깥 윤곽을 채움(볼록 껍질) · 아래 미늘 밑 안쪽 홈은 비움 · 머리~꼬리 하나의 매끈한 외곽선(8배 다각형) ·
색 = 원본 좌우 두 톤(밝은 왼쪽 1 : 오른쪽 ≈0.86 · 가운데 세로줄 65) + 꼬리 행별 밝기 그라데이션(원본 안쪽 중앙값 · 9행 평균) · 불투명도 = 원본 몸통 222.
실행: python -I -X utf8 Tools/make_aim_arrow.py (원본 IB 그림 무수정)
"""
from PIL import Image, ImageDraw
import numpy as np
SRC = "Assets/VFX/IB/FGResources/Game/Arrow/skill-arrow_b.png"
DST = "Assets/VFX/IB/_TKS/skill-arrow_b_filled.png"
SPLIT, TIPY, SPLIT_COL = 106, 152, 65 # 아래 미늘이 갈라지는 행 · 아래 미늘 끝 행 · 좌우 톤 경계 세로줄(원본 실측)
o = np.asarray(Image.open(SRC).convert("RGBA")).astype(np.float32)
H, W = o.shape[:2]
oa = o[:, :, 3]
op = oa > 100
def segs(y):
xs = np.nonzero(op[y])[0]
out, s, p = [], None, None
for x in xs:
if p is None or x != p + 1:
if s is not None:
out.append((int(s), int(p)))
s = x
p = x
if s is not None:
out.append((int(s), int(p)))
return out
def center(y):
c = [s for s in segs(y) if s[0] <= W // 2 <= s[1]]
return c[0] if c else None
def fit(pts): # x = a*y + b
ys = np.array([p[1] for p in pts], float)
xs = np.array([p[0] for p in pts], float)
return tuple(np.polyfit(ys, xs, 1))
def X(f, y):
return f[0] * y + f[1]
def meet(f1, f2):
y = (f2[1] - f1[1]) / (f1[0] - f2[0])
return (X(f1, y), y)
def cross(a, b, c):
return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])
L_ = 0.299 * 8 + 0.587 * 254 + 0.114 * 254
g = np.clip((0.299 * o[:, :, 0] + 0.587 * o[:, :, 1] + 0.114 * o[:, :, 2]) / L_, 0, 1)
core = oa >= 200
tail_rows = [y for y in range(TIPY + 1, H) if center(y) is not None]
yend = tail_rows[-1] + 1.0
bl, br, sl, sr = [], [], [], []
for y in range(SPLIT + 2, TIPY):
s = segs(y)
if len(s) >= 3:
bl.append((s[0][1] + 1.0, y)); sl.append((s[1][0] + 0.0, y)); sr.append((s[1][1] + 1.0, y)); br.append((s[2][0] + 0.0, y))
tl = [(center(y)[0] + 0.0, y) for y in tail_rows[:-8]]
tr = [(center(y)[1] + 1.0, y) for y in tail_rows[:-8]]
fbl, fbr, fsl, fsr, ftl, ftr = fit(bl), fit(br), fit(sl), fit(sr), fit(tl), fit(tr)
apexL, apexR = meet(fbl, fsl), meet(fbr, fsr)
kneeL, kneeR = meet(fsl, ftl), meet(fsr, ftr)
tipy = meet(ftl, ftr)[1]
l = segs(TIPY)
Lt, Rt = (l[0][0] + 0.5, TIPY + 0.5), (l[-1][1] + 0.5, TIPY + 0.5)
pts = sorted(set([(int(x), y) for y in range(SPLIT) for x in np.nonzero(op[y])[0]] + [(int(Lt[0]), TIPY), (int(Rt[0]), TIPY)]))
lo, up = [], []
for p in pts:
while len(lo) >= 2 and cross(lo[-2], lo[-1], p) <= 0:
lo.pop()
lo.append(p)
for p in reversed(pts):
while len(up) >= 2 and cross(up[-2], up[-1], p) <= 0:
up.pop()
up.append(p)
hull = [(x + 0.5, y + 0.5) for x, y in lo[:-1] + up[:-1]]
top = min(hull, key=lambda p: (p[1], abs(p[0] - W / 2)))
left_chain = sorted([p for p in hull if p[0] < W / 2 and top[1] < p[1] < Lt[1] - 0.5], key=lambda p: p[1])
right_chain = sorted([p for p in hull if p[0] > W / 2 and top[1] < p[1] < Rt[1] - 0.5], key=lambda p: -p[1])
yb = min(tipy, yend + 2)
poly = [top] + left_chain + [Lt, apexL]
if kneeL[1] > apexL[1]:
poly.append(kneeL)
poly += [(X(ftl, yb), yb), (X(ftr, yb), yb)]
if kneeR[1] > apexR[1]:
poly.append(kneeR)
poly += [apexR, Rt] + right_chain
S = 8
m = Image.new("L", (W * S, H * S), 0)
ImageDraw.Draw(m).polygon([(x * S, y * S) for x, y in poly], fill=255)
cov = np.asarray(m.resize((W, H), Image.LANCZOS)).astype(np.float32) / 255.0
rows0 = range(TIPY + 2, TIPY + 18)
lv = float(np.median([g[y, x] for y in rows0 for x in range(W) if core[y, x] and x < SPLIT_COL]))
rv = float(np.median([g[y, x] for y in rows0 for x in range(W) if core[y, x] and x >= SPLIT_COL]))
aRow, lRow, rRow = np.full(H, 222.0), np.ones(H), np.ones(H)
for y in tail_rows:
c = center(y)
xs = np.arange(c[0] + 1, c[1])
if len(xs) == 0:
continue
aRow[y] = float(np.median(oa[y, xs]))
lx, rx = xs[xs < SPLIT_COL], xs[xs >= SPLIT_COL]
if len(lx):
lRow[y] = float(np.median(g[y, lx])) / lv
if len(rx):
rRow[y] = float(np.median(g[y, rx])) / rv
def smooth(a, k=9):
p = np.pad(a, k // 2, mode="edge")
return np.convolve(p, np.ones(k) / k, mode="valid")
aRow[: TIPY + 2], lRow[: TIPY + 2], rRow[: TIPY + 2] = 222.0, 1.0, 1.0
aRow, lRow, rRow = smooth(aRow), smooth(lRow), smooth(rRow)
cols = np.arange(W)
v = np.zeros((H, W))
A = np.zeros((H, W))
for y in range(H):
v[y] = np.where(cols < SPLIT_COL, lv * lRow[y], rv * rRow[y])
A[y] = aRow[y] * cov[y]
# ── 테두리(PD 2026-11-11 「새 화살표는 아웃라인이 사라짐」) = 원본 실측 세 겹 ──
# 바깥 2px 검은 그림자(불투명도 ≈36 · 10) · 가장자리 1px 어두움(밝기 × 0.73) · 안쪽 3px 하얀 띠(흰색 섞임 ≈0.47 · 0.26 · 0.14 → 별도 겹그림 DST_RIM · 색을 곱하지 않음)
DST_RIM = "Assets/VFX/IB/_TKS/skill-arrow_b_rim.png"
from scipy import ndimage
# 8배 해상도 다각형에서 정확한 거리(픽셀 단위) → 띠마다 덮임 비율을 내려 받아 부드러운 테두리(계단 없음)
big = np.asarray(m).astype(np.float32) / 255.0 > 0.5
din_big = ndimage.distance_transform_edt(big) / S # 안쪽: 가장자리에서의 거리(px)
dout_big = ndimage.distance_transform_edt(~big) / S # 바깥: 모양에서의 거리(px)
def band(lo_, hi_, d):
b = ((d > lo_) & (d <= hi_)).astype(np.float32)
return np.asarray(Image.fromarray((b * 255).astype(np.uint8)).resize((W, H), Image.BOX)).astype(np.float32) / 255.0
edge1 = band(0.0, 1.0, din_big) # 가장자리 1px = 어두움
v = v * (1.0 - 0.27 * np.clip(edge1 / np.maximum(cov, 1e-3), 0, 1))
sh = 36.0 * band(0.0, 1.0, dout_big) + 10.0 * band(1.0, 2.0, dout_big) # 바깥 그림자(검정)
cA = A.copy()
A = cA + sh * (1.0 - cA / 255.0)
v = np.where(A > 0, v * cA / np.maximum(A, 1e-3), 0.0) # 그림자와 겹치는 가장자리 = 검정 쪽으로 섞임
Image.fromarray(np.dstack([v * 255, v * 255, v * 255, A]).clip(0, 255).astype(np.uint8), "RGBA").save(DST)
ra = (0.47 * band(1.0, 2.0, din_big) + 0.26 * band(2.0, 3.0, din_big) + 0.14 * band(3.0, 4.0, din_big)) * 255.0 * np.clip(cA / 222.0, 0, 1)
white = np.full((H, W), 255.0)
Image.fromarray(np.dstack([white, white, white, ra]).clip(0, 255).astype(np.uint8), "RGBA").save(DST_RIM)
print("ok", DST, DST_RIM, "좌", round(lv, 3), "우", round(rv, 3))