""" The store page's showcase: slanted animated panels cut from the trailer's rolls, as WEBM with alpha for Steam's 780 px column. py -I showcase.py [name ...] all of them, or only the named ones. Writes to Docs/Steam/showcase Steam takes up to 12 seconds of WEBM in the description, and VP9 with alpha is what keeps the corners clear. """ import os import subprocess import sys import cv2 import numpy as np from PIL import Image, ImageDraw, ImageFont PROJECT = r"D:\PROJECTS\LOCAL\GrassCutter" ROLLS = os.path.join(PROJECT, r"Builds\Trailer\rolls") OUT = os.path.join(PROJECT, r"Docs\Steam\showcase") FONT = os.path.join(PROJECT, r"Assets\Fonts\LilitaOne-Regular.ttf") LOGO = os.path.join(PROJECT, r"Assets\Art\UI\logo_grasscutter.png") WHITE, EDGE = (255, 255, 255), (22, 58, 24) # the store page behind the description: what shows through wherever Steam drops the alpha on the way PAGE = np.array([27, 40, 56], np.float32) WIDTH = 780 SS = 3 # masks are drawn this much bigger, then shrunk def frames_of(take, start, seconds, fps, rate=60): folder = os.path.join(ROLLS, take) files = sorted(f for f in os.listdir(folder) if f.endswith(".jpg")) return [os.path.join(folder, files[min(len(files) - 1, int((start + i / fps) * rate))]) for i in range(int(seconds * fps))] def look(img, sat=1.1, contrast=1.06): f = img.astype(np.float32) / 255 lum = f.mean(axis=2, keepdims=True) f = lum + (f - lum) * sat f = 0.5 + (f - 0.5) * contrast return (np.clip(f, 0, 1) * 255).astype(np.uint8) def shot(path, w, h, focus=(0.5, 0.5), zoom=1.0): """The frame cropped to w:h around focus, zoom times tighter than the widest crop that fits. RGB float.""" img = cv2.imread(path, cv2.IMREAD_COLOR) H, W = img.shape[:2] cw = min(W, H * w / h) / zoom ch = cw * h / w x = int(np.clip(focus[0] * W - cw / 2, 0, W - cw)) y = int(np.clip(focus[1] * H - ch / 2, 0, H - ch)) img = cv2.resize(img[y:int(y + ch), x:int(x + cw)], (w, h), interpolation=cv2.INTER_AREA) return look(img)[..., ::-1].astype(np.float32) def slant(w, h, lean, m=8): """The panel's outline, top edge pushed right, inset by a margin for the frame.""" return [(m + lean, m), (w - m, m), (w - m - lean, h - m), (m, h - m)] def plate(w, h, lean): """Mask of the picture, and the frame drawn over it as RGBA.""" pts = [(x * SS, y * SS) for x, y in slant(w, h, lean)] size = (w * SS, h * SS) outer = Image.new("L", size, 0) d = ImageDraw.Draw(outer) d.polygon(pts, fill=255) d.line(pts + pts[:2], fill=255, width=8 * SS, joint="curve") frame = Image.new("RGBA", size, (0, 0, 0, 0)) d = ImageDraw.Draw(frame) d.line(pts + pts[:2], fill=EDGE + (255,), width=8 * SS, joint="curve") d.line(pts + pts[:2], fill=WHITE + (255,), width=4 * SS, joint="curve") mask = np.asarray(outer.resize((w, h), Image.LANCZOS), np.float32) / 255 return mask, np.asarray(frame.resize((w, h), Image.LANCZOS), np.float32) def caption(text, size, w, h): """Text on a clear layer, white on a dark green edge.""" font = ImageFont.truetype(FONT, size * SS) im = Image.new("RGBA", (w * SS, h * SS), (0, 0, 0, 0)) d = ImageDraw.Draw(im) stroke = max(2, size // 9) * SS box = d.textbbox((0, 0), text, font=font, stroke_width=stroke) x, y = (w * SS - box[2] - box[0]) // 2, (h * SS - box[3] - box[1]) // 2 d.text((x, y + 2 * SS), text, font=font, fill=(0, 0, 0, 150), stroke_width=stroke, stroke_fill=(0, 0, 0, 150)) d.text((x, y), text, font=font, fill=WHITE + (255,), stroke_width=stroke, stroke_fill=EDGE + (255,)) return np.asarray(im.resize((w, h), Image.LANCZOS), np.float32) def over(rgb, lay, x=0, y=0): h, w = lay.shape[:2] a = lay[..., 3:] / 255 part = rgb[y:y + h, x:x + w] part[...] = part * (1 - a) + lay[..., :3] * a def encode(frames, alpha, name, fps, crf=40): """frames are float RGB, alpha 0..1 is one array or one per frame.""" h, w = frames[0].shape[:2] path = os.path.join(OUT, name + "_english.webm") cmd = ["ffmpeg", "-y", "-v", "error", "-f", "rawvideo", "-pix_fmt", "rgba", "-s", f"{w}x{h}", "-r", str(fps), "-i", "-", "-an", "-c:v", "libvpx-vp9", "-pix_fmt", "yuva420p", "-crf", str(crf), "-b:v", "0", "-deadline", "good", "-cpu-used", "1", "-row-mt", "1", "-auto-alt-ref", "0", "-g", "240", path] pipe = subprocess.Popen(cmd, stdin=subprocess.PIPE) for i, f in enumerate(frames): a = alpha[i] if isinstance(alpha, list) else alpha rgb = np.clip(f, 0, 255) * a[..., None] + PAGE * (1 - a[..., None]) rgba = np.dstack((rgb, a * 255)).astype(np.uint8) pipe.stdin.write(rgba.tobytes()) pipe.stdin.close() pipe.wait() print(os.path.basename(path), f"{w}x{h}", len(frames), "frames", f"{os.path.getsize(path) / 1024:.0f} KB", flush=True) # ───────────────────────── the wide panels ───────────────────────── def wide(name, take, start, seconds, text, fps=24, rate=60, focus=(0.5, 0.5), zoom=1.0, h=316, hold=0, crf=40): w, lean = WIDTH, 33 mask, frame = plate(w, h, lean) cap = caption(text, 44, w, 70) files = frames_of(take, start, seconds, fps, rate) files += [files[-1]] * hold rows = [] for p in files: f = shot(p, w, h, focus, zoom) over(f, frame) over(f, cap, 0, h - 80) rows.append(f) encode(rows, mask, name, fps, crf) def row(name, cells, fps=24, seconds=2.6, crf=40): """Three small panels side by side, each with its word under it.""" w, cw, ch, lean, gap = WIDTH, 252, 150, 15, 12 h = ch + 44 mask, frame = plate(cw, ch, lean) alpha = np.zeros((h, w), np.float32) caps = np.zeros((h, w, 4), np.float32) for k, cell in enumerate(cells): x = k * (cw + gap) alpha[0:ch, x:x + cw] = np.maximum(alpha[0:ch, x:x + cw], mask) lay = caption(cell[0], 27, cw, 42) part = caps[ch:ch + 42, x:x + cw] part[...] = np.maximum(part, lay) alpha = np.maximum(alpha, caps[..., 3] / 255) rolls = [frames_of(c[1], c[2], seconds, fps, c[3] if len(c) > 3 else 60) for c in cells] rows = [] for i in range(int(seconds * fps)): canvas = np.zeros((h, w, 3), np.float32) for k, cell in enumerate(cells): x = k * (cw + gap) pic = shot(rolls[k][i], cw, ch, *(cell[4:6] if len(cell) > 4 else ((0.5, 0.5), 1.0))) over(pic, frame) a = mask[..., None] canvas[0:ch, x:x + cw] = canvas[0:ch, x:x + cw] * (1 - a) + pic * a over(canvas, caps) rows.append(canvas) encode(rows, alpha, name, fps, crf) def logo(name="logo"): """The logo with its grass in the wind.""" src = cv2.imread(LOGO, cv2.IMREAD_UNCHANGED) x, y, bw, bh = cv2.boundingRect((src[..., 3] > 8).astype(np.uint8)) src = src[y:y + bh, x:x + bw] w = 600 h = int(bh * w / bw) // 2 * 2 base = cv2.resize(src, (w, h), interpolation=cv2.INTER_AREA).astype(np.float32) # premultiplied, so the warp doesn't drag black in at the rims base[..., :3] *= base[..., 3:] / 255 pad = 8 canvas_w, canvas_h = WIDTH, h + pad * 2 gy, gx = np.mgrid[0:h, 0:w].astype(np.float32) # the tufts sit in the top quarter; only they sway, more towards their tips reach = np.clip(1 - gy / (h * 0.26), 0, 1) ** 1.6 fps, n = 24, 48 rows, alphas = [], [] x0 = (canvas_w - w) // 2 for i in range(n): t = i / n * 2 * np.pi dx = reach * (6.0 * np.sin(t + gx / 46) + 2.0 * np.sin(2 * t + gx / 19)) pic = cv2.remap(base, gx + dx, gy, cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)) rgb = np.zeros((canvas_h, canvas_w, 3), np.float32) a = np.zeros((canvas_h, canvas_w), np.float32) al = pic[..., 3] / 255 a[pad:pad + h, x0:x0 + w] = al rgb[pad:pad + h, x0:x0 + w] = pic[..., 2::-1] / np.maximum(al[..., None], 1e-3) rows.append(rgb) alphas.append(a) encode(rows, alphas, name, fps, 34) def banner(name, text, size=50, h=88): """A static plate that opens a section of the text.""" w, lean = WIDTH, 20 im = Image.new("RGBA", (w * SS, h * SS), (0, 0, 0, 0)) pts = [(x * SS, y * SS) for x, y in slant(w, h, lean)] d = ImageDraw.Draw(im) d.polygon(pts, fill=(38, 92, 40, 255)) d.line(pts + pts[:2], fill=EDGE + (255,), width=8 * SS, joint="curve") d.line(pts + pts[:2], fill=WHITE + (255,), width=4 * SS, joint="curve") cap = Image.fromarray(caption(text, size, w, h).astype(np.uint8), "RGBA").resize((w * SS, h * SS), Image.LANCZOS) im.alpha_composite(cap) im = im.resize((w, h), Image.LANCZOS) # full RGBA, not a palette: Steam turned the palette's clear corners black. Under the alpha sits the page's own colour. page = Image.new("RGBA", (w, h), tuple(int(c) for c in PAGE) + (255,)) page.alpha_composite(im) page.putalpha(im.getchannel("A")) path = os.path.join(OUT, name + "_english.png") page.save(path, optimize=True) print(os.path.basename(path), f"{os.path.getsize(path) / 1024:.0f} KB", flush=True) JOBS = { "logo": logo, "planet": lambda: wide("planet", "e_cut_k", 1.2, 6.8, "MOW THE WHOLE PLANET", fps=24, h=390, hold=24), "crew": lambda: wide("crew", "c_line", 0.8, 3.0, "UP TO 16 PLAYERS"), "robots": lambda: wide("robots", "k_robots", 1.0, 3.2, "THEN LET THE ROBOTS DO IT"), "fleet": lambda: wide("fleet", "f_side", 0.3, 3.0, "BRING THE WHOLE FLEET"), "row_jobs": lambda: row("row_jobs", [("MOW", "c_swing_s", 1.3, 120, (0.42, 0.55), 1.2), ("DRIVE", "c_rider_side", 0.2, 60), ("BUILD", "k_hammer", 0.4, 60, (0.55, 0.56), 1.55)]), "row_life": lambda: row("row_life", [("NIGHT SHIFT", "n_tractor", 0.8, 60), ("PHONE BREAK", "v_phone", 0.8, 60, (0.42, 0.45), 1.3), ("BELTS", "k_belt", 0.3, 60)]), "h_mow": lambda: banner("h_mow", "START WITH NAIL SCISSORS"), "h_build": lambda: banner("h_build", "BUILD THE FACTORY"), "h_planet": lambda: banner("h_planet", "EVERY CUT STAYS CUT"), "h_together": lambda: banner("h_together", "BETTER TOGETHER"), "h_features": lambda: banner("h_features", "WHAT YOU GET"), } if __name__ == "__main__": os.makedirs(OUT, exist_ok=True) for name in sys.argv[1:] or JOBS: JOBS[name]()