"""Render the original Studio launch projects with Python, Pillow and FFmpeg.""" import argparse import math import random import subprocess from pathlib import Path from PIL import Image, ImageDraw, ImageFont W, H = 960, 600 TAU = math.tau def background(top, bottom): image = Image.new("RGB", (W, H)) draw = ImageDraw.Draw(image) for y in range(H): f = y / H draw.line((0, y, W, y), fill=tuple(round(a + (b - a) * f) for a, b in zip(top, bottom))) return image, draw def font(size, mono=False): # Pillow's built-in DejaVu lookup works on machines with the font installed. name = "DejaVuSansMono.ttf" if mono else "DejaVuSans-Bold.ttf" try: return ImageFont.truetype(name, size) except OSError: path = "/System/Library/Fonts/Menlo.ttc" if mono else "/System/Library/Fonts/Avenir Next.ttc" return ImageFont.truetype(path, size, index=0 if mono else 2) def stars(draw, seed, height, color, count=85): rng = random.Random(seed) for _ in range(count): x, y = rng.randrange(W), rng.randrange(height) radius = rng.choices([1, 2, 3], [80, 17, 3])[0] draw.ellipse((x, y, x + radius, y + radius), fill=color) def after_hours(t): image, d = background((17, 20, 43), (167, 71, 58)) stars(d, 4, 390, "#b496a0") cx, cy, radius = 480, 294, 136 + int(4 * math.sin(t * TAU)) for y in range(cy - radius, cy + radius): half = math.sqrt(max(0, radius ** 2 - (y - cy) ** 2)) f = (y - cy + radius) / (2 * radius) if y < cy - 12 or (y + round(12 * t)) % 16 < 9: d.line((cx - half, y, cx + half, y), fill=(255, round(206 - 107 * f), round(127 - 41 * f)), width=2) d.polygon([(0, 400), (124, 310), (231, 374), (286, 334), (400, 423), (556, 409), (649, 345), (720, 380), (841, 305), (960, 382), (960, 600), (0, 600)], fill="#44304a") d.polygon([(0, 449), (148, 387), (232, 432), (358, 408), (474, 456), (592, 406), (745, 432), (853, 379), (960, 450), (960, 600), (0, 600)], fill="#261e39") d.polygon([(0, 491), (230, 459), (430, 486), (601, 455), (799, 475), (960, 443), (960, 600), (0, 600)], fill="#181729") d.polygon([(451, 600), (502, 458), (512, 458), (606, 600)], fill="#35283e") for j in range(9): q = ((j + t) / 9) ** 2 y = 458 + q * 142 x = 505 + q * 25 d.line((x, y, x + 3 + 6 * q, y + 2 + 5 * q), fill="#d87d69", width=2) d.text((480, 270), "AFTER HOURS", font=font(92), anchor="mm", fill="#fff0db", stroke_width=1, stroke_fill="#fff0db") d.text((480, 352), "A D E S E R T T I T L E S T U D Y", font=font(15, mono=True), anchor="mm", fill="#ffe0c6") return image OUTPOST_TERRACES = [ (-170, -180, -44, 365, 352, 43, ("#7bae91", "#345e60", "#4b7770")), (-151, -160, -1, 322, 299, 25, ("#a0bea0", "#426b61", "#709080")), (-112, -124, 24, 222, 222, 20, ("#c5ca9e", "#5a8270", "#8da382")), ] OUTPOST_TOWER = (-65, -98, 44, 124, 104, 76, ("#e5d7ae", "#a0a484", "#eee3be")) def observatory(_): image, d = background((15, 48, 54), (47, 103, 100)) stars(d, 16, 320, "#97c5ab", 65) rng = random.Random(76) for _ in range(45): x, y = rng.randrange(W), rng.randrange(170, 560) d.line((x, y, x + rng.randrange(4, 30), y - 6), fill="#467d76") # Isometric scene, drawn back to front. The terraces are layered solids. def iso(x, y, z): return 462 + 1.05 * (x - y), 408 + 0.50 * (x + y) - z def face(points, fill): d.polygon([iso(*point) for point in points], fill=fill) def box(x, y, z, w, h, height, shades): x1, y1, top = x + w, y + h, z + height face([(x1, y, z), (x1, y1, z), (x1, y1, top), (x1, y, top)], shades[1]) face([(x, y1, z), (x1, y1, z), (x1, y1, top), (x, y1, top)], shades[2]) face([(x, y, top), (x1, y, top), (x1, y1, top), (x, y1, top)], shades[0]) for terrace in OUTPOST_TERRACES: box(*terrace) for x, y in [(30, -130), (92, -98), (-114, 12), (-116, 82), (124, 37), (108, 95), (40, 106), (-144, -69)]: for tall, width in [(44, 27), (59, 19), (75, 12)]: t = iso(x, y, 24 + tall) d.polygon([t, (t[0] - width, t[1] + 35), (t[0], t[1] + 45), (t[0] + width, t[1] + 35)], fill="#28645d") d.polygon([t, (t[0] - width, t[1] + 35), (t[0], t[1] + 45)], fill="#4f8970") box(*OUTPOST_TOWER) for x in [-43, -12, 19]: face([(x, 7, 61), (x + 18, 7, 61), (x + 18, 7, 93), (x, 7, 93)], "#214c56") face([(x + 2, 8, 65), (x + 16, 8, 65), (x + 16, 8, 88), (x + 2, 8, 88)], "#ffce76") center = (-4, -46) for i in range(12): a, b = i * TAU / 12, (i + 1) * TAU / 12 p = [(center[0] + 64 * math.cos(a), center[1] + 64 * math.sin(a), 120), (center[0] + 64 * math.cos(b), center[1] + 64 * math.sin(b), 120), (*center, 214)] face(p, ("#b65a50", "#db7965", "#f18f72", "#f7a386", "#edd0a3")[min(4, (i + 2) % 12 // 2)]) x, y = iso(-4, -46, 214) d.line((x, y, x + 12, y - 36), fill="#ffe7a5", width=4) d.ellipse((x + 7, y - 42, x + 17, y - 32), fill="#ffe7a5") return image def moth(t): image, d = background((31, 24, 60), (78, 40, 92)) stars(d, 19, H, "#9c80b3", 110) cx, cy = 480, 298 + round(13 * math.sin(TAU * t)) for r in [80, 129, 175, 226]: d.ellipse((480-r, 298-r, 480+r, 298+r), outline="#775789", width=2) for i in range(12): a = i * TAU / 12 + t * TAU x, y = 480 + 226 * math.cos(a), 298 + 226 * math.sin(a) d.polygon([(x, y-6), (x+4, y), (x, y+6), (x-4, y)], fill="#f5b2ac") spread = .82 + .18 * math.cos(2 * TAU * t) for side in [-1, 1]: def p(x, y): return cx + side * x * spread, cy + y d.polygon([p(8, 7), p(60, -83), p(214, -166), p(236, -101), p(205, -29), p(128, 10), p(188, 64), p(141, 119), p(79, 118), p(13, 45)], fill="#d68bb2", outline="#ffdab9", width=4) d.polygon([p(12, 9), p(64, -77), p(206, -151), p(219, -97), p(175, -42), p(110, -1)], fill="#eea899") d.polygon([p(14, 29), p(113, 16), p(176, 64), p(138, 106), p(84, 111)], fill="#a170a4") for x, y, r in [(145, -70, 24), (104, 61, 16)]: a, b = p(x, y) d.ellipse((a-r, b-r, a+r, b+r), fill="#663f77", outline="#fff0ba", width=4) d.ellipse((a-6, b-6, a+6, b+6), fill="#fff0ba") for x in range(60, 201, 20): d.line([p(12, 14), p(x, -35 - x // 3)], fill="#8b557e", width=2) d.line([p(4, -21), p(15, -50), p(41, -69)], fill="#ffe5be", width=3) d.ellipse((cx-13, cy-39, cx+13, cy+57), fill="#f8d3b4", outline="#895779", width=5) d.ellipse((cx-9, cy-45, cx+9, cy-26), fill="#ffeac3") return image SPRITE = [ ".......ccccc....", ".....ccrrrrcc...", "....crrrrrrrrc..", "....crrrrrrrrc..", "...ccyyyyyyycc..", "...cyybyybyyc...", "....cyyyyyyc....", ".....cccccc.....", "....cgccggc.....", "...cggcggggc....", "...cggcggggcc...", "....cccccccc....", ".....cgc.cgc....", ".....cgc.cgc....", "....ccc..ccc....", ] PALETTE = {"c": "#201a38", "r": "#f17368", "y": "#ffd798", "b": "#3b2843", "g": "#76d3bd"} def paint_sprite(d, x, y, scale, phase): for j, line in enumerate(SPRITE): for i, color in enumerate(line): if color == ".": continue stride = int(phase * (1 if i < 8 else -1)) if j >= 12 else 0 d.rectangle((x + i*scale, y + (j+stride)*scale, x + (i+1)*scale-1, y + (j+stride+1)*scale-1), fill=PALETTE[color]) def courier(t): small = Image.new("RGB", (160, 100), "#231f43") d = ImageDraw.Draw(small) rng = random.Random(32) for _ in range(68): x, y = rng.randrange(160), rng.randrange(72) d.point((x, y), fill=rng.choice(["#8d71a2", "#d6a0ad", "#ecc4aa"])) d.ellipse((108, 13, 130, 35), fill="#e9a68e") for y in [25, 29, 33]: d.line((106, y, 132, y), fill="#734875") d.polygon([(0, 68), (19, 48), (42, 63), (67, 45), (100, 67), (131, 49), (160, 67), (160, 100), (0, 100)], fill="#3e3763") d.polygon([(0, 76), (31, 61), (47, 70), (80, 62), (107, 74), (143, 61), (160, 74), (160, 100), (0, 100)], fill="#554364") d.rectangle((0, 86, 159, 99), fill="#302944") d.line((0, 86, 159, 86), fill="#b67782") for x in range(0, 160, 13): d.line((x, 93, x+7, 93), fill="#786082") d.ellipse((66, 84, 94, 90), fill="#241d39") paint_sprite(d, 57, 41 - int(2 * abs(math.sin(TAU * t))), 3, round(math.sin(TAU * t))) return small.resize((W, H), Image.Resampling.NEAREST) PROJECTS = { "after-hours": (after_hours, True), "cloudbreak-outpost": (observatory, False), "moth-signal": (moth, True), "cinder-courier": (courier, True), } def observatory_mesh(out): """Write terrace and tower blockout in metres for import into Blender.""" lines = ["# Cloudbreak Outpost blockout", "# Metres, Z up"] count = 0 def shape(name, vertices, faces): nonlocal count lines.append("o " + name) for x, y, z in vertices: lines.append(f"v {x / 100:.4f} {y / 100:.4f} {z / 100:.4f}") for face in faces: lines.append("f " + " ".join(str(i + count + 1) for i in face)) count += len(vertices) for i, (x, y, z, w, h, height, _) in enumerate([*OUTPOST_TERRACES, OUTPOST_TOWER]): vertices = [(x + dx, y + dy, z + dz) for dz in (0, height) for dx, dy in ((0, 0), (w, 0), (w, h), (0, h))] shape("Terrace_" + str(i + 1) if i < 3 else "Observatory", vertices, [(3, 2, 1, 0), (4, 5, 6, 7), (0, 1, 5, 4), (1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)]) ring = [(-4 + 64 * math.cos(i * TAU / 12), -46 + 64 * math.sin(i * TAU / 12), 120) for i in range(12)] shape("Faceted_Roof", [*ring, (-4, -46, 214)], [tuple(reversed(range(12))), *((i, (i + 1) % 12, 12) for i in range(12))]) (out / "cloudbreak-blockout.obj").write_text("\n".join(lines) + "\n", encoding="utf-8") def render(name, out): frame, motion = PROJECTS[name] out.mkdir(parents=True, exist_ok=True) frame(0).save(out / (name + ".webp"), quality=86, method=6) if not motion: observatory_mesh(out) return command = ["ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-f", "rawvideo", "-pix_fmt", "rgb24", "-s", f"{W}x{H}", "-r", "24", "-i", "-", "-an", "-c:v", "libx264", "-threads", "2", "-crf", "24", "-pix_fmt", "yuv420p", "-movflags", "+faststart", str(out / (name + ".mp4"))] with subprocess.Popen(command, stdin=subprocess.PIPE) as process: for index in range(72): process.stdin.write(frame(index / 72).tobytes()) process.stdin.close() if process.wait(): raise RuntimeError(f"FFmpeg failed for {name}") if name == "cinder-courier": sheet = Image.new("RGBA", (64, 16)) d = ImageDraw.Draw(sheet) for i, phase in enumerate((0, 1, 0, -1)): paint_sprite(d, i * 16, 0, 1, phase) sheet.save(out / "cinder-courier-sprites.png") if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("project", choices=["all", *PROJECTS]) parser.add_argument("--out", type=Path, default=Path("studio-output")) args = parser.parse_args() for project in PROJECTS if args.project == "all" else [args.project]: render(project, args.out)