Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
263
ArtSource/Textures/finalize_ocean.py
Normal file
263
ArtSource/Textures/finalize_ocean.py
Normal file
@ -0,0 +1,263 @@
|
||||
"""OCEAN SWEEPERS: oc_raw_*.png -> Assets/Art/Textures + Assets/Art/UI.
|
||||
Tileable textures: pick the best-matching wrap point (crop), resize to 1024, then cross-fade the wrap seam
|
||||
with a half-offset copy so the result is truly seamless. Decals keep aspect. UI images get real alpha.
|
||||
Usage: python -I finalize_ocean.py [name...]"""
|
||||
import os, sys
|
||||
import numpy as np
|
||||
from PIL import Image, ImageFilter
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
ROOT = os.path.normpath(os.path.join(HERE, "..", ".."))
|
||||
TEX = os.path.join(ROOT, "Assets", "Art", "Textures")
|
||||
UI = os.path.join(ROOT, "Assets", "Art", "UI")
|
||||
|
||||
# name -> (blend band fraction, do_x, do_y). The raws come out nearly tileable already (measured edge
|
||||
# mismatch ~1.5x the interior pixel delta), so only a narrow cross-fade at the wrap is used; wide bands ghost.
|
||||
# T_Deck is an exact 3x3 plate grid with half-gaps on every edge -> kept as is (blending ghosted the stripes).
|
||||
# T_Fabric has exactly 12 stripes across 1024 px -> X untouched, Y gets a thin blend for the weave.
|
||||
TILING = {
|
||||
"T_HullPanel": (0.05, True, True),
|
||||
"T_Deck": (0.0, False, False),
|
||||
"T_WoodDeck": (0.04, True, True),
|
||||
"T_Rust": (0.06, True, True),
|
||||
"T_Rock": (0.06, True, True),
|
||||
"T_Sand": (0.06, True, True),
|
||||
"T_Ice": (0.06, True, True),
|
||||
"T_Fabric": (0.03, False, True),
|
||||
}
|
||||
USE_WRAP_CROP = False # best_wrap() crop search; not needed for these raws
|
||||
|
||||
|
||||
def raw(n):
|
||||
return Image.open(os.path.join(HERE, f"oc_raw_{n}.png"))
|
||||
|
||||
|
||||
def best_wrap(a, axis, lo=0.80, strip=6):
|
||||
"""Length L (lo*N..N) such that wrapping at L matches best: compares the first `strip` lines with lines L..L+strip."""
|
||||
a = a.astype(np.float32)
|
||||
if axis == 1:
|
||||
a = a.transpose(1, 0, 2)
|
||||
n = a.shape[0]
|
||||
head = a[:strip]
|
||||
best, bl = 1e18, n
|
||||
for L in range(int(n * lo), n - strip + 1):
|
||||
d = np.mean(np.abs(a[L:L + strip] - head))
|
||||
if d < best:
|
||||
best, bl = d, L
|
||||
return bl
|
||||
|
||||
|
||||
def seamless(a, band=0.12, do_x=True, do_y=True):
|
||||
a = a.astype(np.float32)
|
||||
h, w = a.shape[:2]
|
||||
|
||||
def ramp(n):
|
||||
t = np.ones(n, np.float32)
|
||||
b = max(2, int(n * band))
|
||||
x = np.arange(b, dtype=np.float32) / b
|
||||
s = x * x * (3 - 2 * x)
|
||||
t[:b] = s
|
||||
t[n - b:] = s[::-1]
|
||||
return t
|
||||
if do_y:
|
||||
off = np.roll(a, h // 2, axis=0)
|
||||
m = ramp(h)[:, None, None]
|
||||
a = a * m + off * (1 - m)
|
||||
if do_x:
|
||||
off = np.roll(a, w // 2, axis=1)
|
||||
m = ramp(w)[None, :, None]
|
||||
a = a * m + off * (1 - m)
|
||||
return np.clip(a, 0, 255).astype(np.uint8)
|
||||
|
||||
|
||||
def _cut_mask(err):
|
||||
"""err: (band, W) cost. DP for a connected min-cost path (one row per column, +-1 step).
|
||||
Returns mask (band, W): 1 above/at-cut rows -> use the offset copy, 0 below."""
|
||||
b, W = err.shape
|
||||
cost = err.copy()
|
||||
back = np.zeros((b, W), np.int32)
|
||||
for x in range(1, W):
|
||||
prev = cost[:, x - 1]
|
||||
cand = np.stack([np.r_[np.inf, prev[:-1]], prev, np.r_[prev[1:], np.inf]])
|
||||
k = cand.argmin(0)
|
||||
back[:, x] = k - 1
|
||||
cost[:, x] += cand.min(0)
|
||||
r = np.zeros(W, np.int32)
|
||||
r[-1] = cost[:, -1].argmin()
|
||||
for x in range(W - 1, 0, -1):
|
||||
r[x - 1] = r[x] + back[r[x], x]
|
||||
return (np.arange(b)[:, None] < r[None, :]).astype(np.float32)
|
||||
|
||||
|
||||
def seamless_cut(a, band=0.10, feather=1.5):
|
||||
"""Image-quilting style wrap: half-offset copy fills the edge bands, joined to the original along a
|
||||
minimum-error path (hard chipped edges stay crisp instead of ghosting)."""
|
||||
a = a.astype(np.float32)
|
||||
for axis in (0, 1):
|
||||
if axis == 1:
|
||||
a = a.transpose(1, 0, 2)
|
||||
h = a.shape[0]
|
||||
b = int(h * band)
|
||||
off = np.roll(a, h // 2, axis=0)
|
||||
m = np.zeros(a.shape[:2], np.float32)
|
||||
# top band: rows 0..b, use off above the cut; keep a margin so row 0 is always off
|
||||
err = np.abs(a[2:b] - off[2:b]).sum(-1)
|
||||
m[:2] = 1; m[2:b] = _cut_mask(err)
|
||||
err = np.abs(a[h - b:h - 2] - off[h - b:h - 2]).sum(-1)[::-1]
|
||||
m[h - 2:] = 1; m[h - b:h - 2] = _cut_mask(err)[::-1]
|
||||
mi = Image.fromarray((m * 255).astype(np.uint8)).filter(ImageFilter.GaussianBlur(feather))
|
||||
m = np.array(mi).astype(np.float32)[..., None] / 255.0
|
||||
m[:1] = 1; m[-1:] = 1
|
||||
a = off * m + a * (1 - m)
|
||||
if axis == 1:
|
||||
a = a.transpose(1, 0, 2)
|
||||
return np.clip(a, 0, 255).astype(np.uint8)
|
||||
|
||||
|
||||
CUT = {"T_Rust"} # use the min-error cut instead of a cross-fade
|
||||
|
||||
|
||||
def tile(n):
|
||||
band, dx, dy = TILING[n]
|
||||
im = raw(n).convert("RGB")
|
||||
a = np.array(im)
|
||||
if n == "T_HullPanel": # keep seams/rivets faint: halve contrast around the paint colour
|
||||
f = a.astype(np.float32)
|
||||
a = np.clip(np.median(f.reshape(-1, 3), 0) + (f - np.median(f.reshape(-1, 3), 0)) * 0.5, 0, 255).astype(np.uint8)
|
||||
h = best_wrap(a, 0) if (dy and USE_WRAP_CROP) else a.shape[0]
|
||||
w = best_wrap(a, 1) if (dx and USE_WRAP_CROP) else a.shape[1]
|
||||
im = Image.fromarray(a[:h, :w])
|
||||
if im.size != (1024, 1024):
|
||||
im = im.resize((1024, 1024), Image.LANCZOS)
|
||||
if n in CUT:
|
||||
return Image.fromarray(seamless_cut(np.array(im), max(band, 0.08)))
|
||||
return Image.fromarray(seamless(np.array(im), band, dx, dy)) if (dx or dy) else im
|
||||
|
||||
|
||||
def fit(im, w, h):
|
||||
W, H = im.size
|
||||
if W / H > w / h:
|
||||
nw = int(H * w / h); x0 = (W - nw) // 2; im = im.crop((x0, 0, x0 + nw, H))
|
||||
else:
|
||||
nh = int(W * h / w); y0 = (H - nh) // 2; im = im.crop((0, y0, W, y0 + nh))
|
||||
return im.resize((w, h), Image.LANCZOS)
|
||||
|
||||
|
||||
def has_alpha(im):
|
||||
"""True only if the model returned a real cut-out (a good share of fully transparent pixels)."""
|
||||
return im.mode == "RGBA" and (np.array(im)[..., 3] < 10).mean() > 0.05
|
||||
|
||||
|
||||
def key_flat(im, tol=28, soft=18):
|
||||
"""Key out a flat / near-flat background connected to the image border (flood from edges)."""
|
||||
rgb = np.array(im.convert("RGB")).astype(np.float32)
|
||||
H, W = rgb.shape[:2]
|
||||
border = np.concatenate([rgb[0], rgb[-1], rgb[:, 0], rgb[:, -1]])
|
||||
bg = np.median(border, axis=0)
|
||||
dist = np.sqrt(((rgb - bg) ** 2).sum(-1))
|
||||
cand = dist < tol + soft
|
||||
# flood fill from border over candidate pixels
|
||||
from collections import deque
|
||||
seen = np.zeros((H, W), bool)
|
||||
q = deque()
|
||||
for x in range(W):
|
||||
for y in (0, H - 1):
|
||||
if cand[y, x] and not seen[y, x]:
|
||||
seen[y, x] = True; q.append((y, x))
|
||||
for y in range(H):
|
||||
for x in (0, W - 1):
|
||||
if cand[y, x] and not seen[y, x]:
|
||||
seen[y, x] = True; q.append((y, x))
|
||||
while q:
|
||||
y, x = q.popleft()
|
||||
for yy, xx in ((y - 1, x), (y + 1, x), (y, x - 1), (y, x + 1)):
|
||||
if 0 <= yy < H and 0 <= xx < W and cand[yy, xx] and not seen[yy, xx]:
|
||||
seen[yy, xx] = True; q.append((yy, xx))
|
||||
alpha = np.ones((H, W), np.float32)
|
||||
ramp = np.clip((dist - tol) / soft, 0, 1)
|
||||
alpha[seen] = ramp[seen]
|
||||
a8 = Image.fromarray((alpha * 255).astype(np.uint8)).filter(ImageFilter.GaussianBlur(0.6))
|
||||
# decontaminate edge colours from the background
|
||||
al = np.array(a8).astype(np.float32)[..., None] / 255.0
|
||||
rgb2 = np.where(al > 0.02, (rgb - bg * (1 - al)) / np.maximum(al, 0.02), rgb)
|
||||
out = np.dstack([np.clip(rgb2, 0, 255), np.array(a8)]).astype(np.uint8)
|
||||
print(f" keyed bg {bg.round()}")
|
||||
return Image.fromarray(out, "RGBA")
|
||||
|
||||
|
||||
def black_to_alpha(im):
|
||||
"""Unmix a pure black background: alpha from brightness, un-premultiply colour."""
|
||||
rgb = np.array(im.convert("RGB")).astype(np.float32) / 255.0
|
||||
a = rgb.max(-1)
|
||||
a = np.clip((a - 0.025) / 0.16, 0, 1) # bg is ~0.00; darkest planet pixels ~0.25 -> stay opaque
|
||||
a = a * a * (3 - 2 * a)
|
||||
col = np.where(a[..., None] > 0.01, np.clip(rgb / np.maximum(a[..., None], 0.01), 0, 1), 0)
|
||||
out = np.dstack([col * 255, a * 255]).clip(0, 255).astype(np.uint8)
|
||||
return Image.fromarray(out, "RGBA")
|
||||
|
||||
|
||||
def trim_pad(im, pad=24):
|
||||
a = np.array(im)[..., 3]
|
||||
ys, xs = np.where(a > 8)
|
||||
if len(xs) == 0:
|
||||
return im
|
||||
x0, x1, y0, y1 = max(0, xs.min() - pad), min(im.width, xs.max() + pad + 1), max(0, ys.min() - pad), min(im.height, ys.max() + pad + 1)
|
||||
return im.crop((x0, y0, x1, y1))
|
||||
|
||||
|
||||
def alpha_ui(n, keyfn=None):
|
||||
im = raw(n)
|
||||
if has_alpha(im):
|
||||
print(" real alpha from model")
|
||||
return im.convert("RGBA")
|
||||
return (keyfn or key_flat)(im)
|
||||
|
||||
|
||||
def save(im, folder, n):
|
||||
os.makedirs(folder, exist_ok=True)
|
||||
im.save(os.path.join(folder, n + ".png"), optimize=True)
|
||||
print("ok", n, im.size, im.mode)
|
||||
|
||||
|
||||
def build(n):
|
||||
if n in TILING:
|
||||
save(tile(n), TEX, n)
|
||||
elif n == "T_Label": # crop to the outer navy stripes (rows 154..868 of the 1536x1024 raw) so the band fills the image
|
||||
im = raw(n).convert("RGB")
|
||||
a = np.array(im).min(-1) < 200
|
||||
rows = np.where(a.mean(1) > 0.5)[0]
|
||||
save(im.crop((0, rows.min(), im.width, rows.max() + 1)).resize((1024, 512), Image.LANCZOS), TEX, n)
|
||||
elif n == "T_Logo":
|
||||
save(raw(n).convert("RGB").resize((1024, 1024), Image.LANCZOS), TEX, n)
|
||||
elif n == "T_RocketDecal": # 2:1 crop centred on the artwork (no squash), then 1024x512
|
||||
im = raw(n).convert("RGB")
|
||||
a = np.array(im).min(-1) < 200
|
||||
r = np.where(a.any(1))[0]
|
||||
cy, hh = (r.min() + r.max()) // 2, im.width // 4
|
||||
y0 = int(np.clip(cy - hh, 0, im.height - 2 * hh))
|
||||
save(im.crop((0, y0, im.width, y0 + 2 * hh)).resize((1024, 512), Image.LANCZOS), TEX, n)
|
||||
elif n == "logo_oceansweepers":
|
||||
im = alpha_ui(n)
|
||||
save(im.resize((1536, 1024), Image.LANCZOS) if im.size != (1536, 1024) else im, UI, n)
|
||||
elif n == "ui_stamp_hired":
|
||||
im = alpha_ui(n)
|
||||
save(im.resize((1024, 1024), Image.LANCZOS) if im.size != (1024, 1024) else im, UI, n)
|
||||
elif n == "sky_planet":
|
||||
im = raw(n)
|
||||
im = im.convert("RGBA") if has_alpha(im) else black_to_alpha(im)
|
||||
save(im.resize((1024, 1024), Image.LANCZOS), UI, n)
|
||||
elif n == "ui_paper":
|
||||
save(fit(raw(n).convert("RGB"), 1024, 1536), UI, n)
|
||||
elif n == "menu_bg":
|
||||
save(fit(raw(n).convert("RGB"), 1536, 1024), UI, n)
|
||||
else:
|
||||
print("unknown", n)
|
||||
|
||||
|
||||
ALL = list(TILING) + ["T_Label", "T_Logo", "T_RocketDecal", "logo_oceansweepers", "ui_paper", "ui_stamp_hired", "sky_planet", "menu_bg"]
|
||||
|
||||
if __name__ == "__main__":
|
||||
for n in (sys.argv[1:] or ALL):
|
||||
if not os.path.exists(os.path.join(HERE, f"oc_raw_{n}.png")):
|
||||
print("missing", n); continue
|
||||
build(n)
|
||||
Reference in New Issue
Block a user