""" The game's sky stays daylight blue from orbit, so the orbital takes get a real one: the ball is keyed out, lit from one side with a terminator and an atmosphere, and set against stars. py -I space.py rolls take take_k """ import os import sys import cv2 import numpy as np root, src, dst = sys.argv[1:4] front = len(sys.argv) > 4 W, H = 2560, 1440 os.makedirs(os.path.join(root, dst), exist_ok=True) rng = np.random.default_rng(11) # the stars: a field a little bigger than the frame that drifts sky = np.zeros((H + 400, W + 400, 3), np.float32) for n, size, lo, hi in ((9000, 1, 0.12, 0.5), (1300, 2, 0.35, 0.8), (130, 3, 0.7, 1.0)): ys = rng.integers(0, sky.shape[0], n) xs = rng.integers(0, sky.shape[1], n) v = rng.uniform(lo, hi, n) tint = rng.uniform(0.82, 1.0, (n, 3)) for y, x, a, c in zip(ys, xs, v, tint): cv2.circle(sky, (int(x), int(y)), size - 1, (float(a * c[0]), float(a * c[1]), float(a * c[2])), -1, cv2.LINE_AA) sky = cv2.GaussianBlur(sky, (0, 0), 0.8) # a band of faint dust across it y, x = np.mgrid[0:sky.shape[0], 0:sky.shape[1]].astype(np.float32) band = np.exp(-(((x * 0.34 + y - 1200) / 340) ** 2)) noise = cv2.GaussianBlur(rng.random(sky.shape[:2], np.float32), (0, 0), 60) noise = (noise - noise.min()) / (noise.max() - noise.min()) sky += (band * noise * 0.16)[..., None] * np.array([0.9, 0.55, 0.5], np.float32) # what makes the front ragged, as if it were made of many crews ragged = cv2.GaussianBlur(rng.random((H, W), np.float32), (0, 0), 7) + cv2.GaussianBlur(rng.random((H, W), np.float32), (0, 0), 30) * 2.2 ragged = (ragged - ragged.mean()) / ragged.std() blot = cv2.GaussianBlur(rng.random((H, W), np.float32), (0, 0), 16) blot = np.clip((blot - blot.mean()) / blot.std() * 0.5 + 0.5, 0, 1) spot = None light = np.array([-0.62, -0.42, 0.66], np.float32) light /= np.linalg.norm(light) files = sorted(f for f in os.listdir(os.path.join(root, src)) if f.endswith(".jpg")) fit = None for i, name in enumerate(files): img = cv2.imread(os.path.join(root, src, name), cv2.IMREAD_COLOR).astype(np.float32) / 255 b, g, r = cv2.split(img) mask = ((g > b * 1.03) | (r > b * 1.03)).astype(np.uint8) * 255 mask = cv2.morphologyEx(cv2.morphologyEx(mask, cv2.MORPH_OPEN, np.ones((5, 5), np.uint8)), cv2.MORPH_CLOSE, np.ones((9, 9), np.uint8)) n, labels, stats, _ = cv2.connectedComponentsWithStats(mask) if n > 1: mask = (labels == 1 + int(np.argmax(stats[1:, cv2.CC_STAT_AREA]))).astype(np.uint8) * 255 edge = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0] pts = max(edge, key=len)[:, 0, :].astype(np.float64) pts = pts[(pts[:, 0] > 3) & (pts[:, 0] < W - 4) & (pts[:, 1] > 3) & (pts[:, 1] < H - 4)] # a circle through the rim, least squares A = np.c_[2 * pts, np.ones(len(pts))] c = np.linalg.lstsq(A, (pts ** 2).sum(axis=1), rcond=None)[0] cx, cy, rad = c[0], c[1], np.sqrt(c[2] + c[0] ** 2 + c[1] ** 2) now = np.array([cx, cy, rad]) fit = now if fit is None else fit * 0.7 + now * 0.3 cx, cy, rad = fit yy, xx = np.mgrid[0:H, 0:W].astype(np.float32) nx, ny = (xx - cx) / rad, (yy - cy) / rad rr = nx * nx + ny * ny inside = np.clip((1 - np.sqrt(rr)) * rad / 2.0 + 0.5, 0, 1) nz = np.sqrt(np.clip(1 - rr, 0, 1)) lit = np.clip(nx * light[0] + ny * light[1] + nz * light[2], 0, 1) if front: k = i / (len(files) - 1) seen = (r > g * 0.92) & (rr < 0.8) if seen.sum() > 300: ys, xs = np.nonzero(seen) now = np.array([(xs.mean() - cx) / rad, (ys.mean() - cy) / rad]) spot = now if spot is None else spot * 0.85 + now * 0.15 bx, by = spot if spot is not None else (0.0, 0.0) bz = np.sqrt(max(0.0, 1 - bx * bx - by * by)) ang = np.arccos(np.clip(nx * bx + ny * by + nz * bz, -1, 1)) reach = 0.06 + 1.95 * (k * k * (3 - 2 * k)) edge = reach - ang + ragged * 0.022 cover = np.clip(edge / 0.012, 0, 1)[..., None] lum = img.mean(axis=2, keepdims=True) lum = lum * 0.3 + cv2.GaussianBlur(lum, (0, 0), 90)[..., None] * 0.7 tan = lum * (0.96 + blot[..., None] * 0.08) * np.array([0.56, 0.86, 1.3], np.float32) img = img * (1 - cover) + tan * cover # a soft terminator with a warm edge day = np.clip(lit * 1.5, 0, 1) ** 0.75 warm = np.clip(1 - np.abs(lit - 0.1) / 0.16, 0, 1) * 0.5 surface = img * (day[..., None] * 1.28 + 0.012) + warm[..., None] * img * np.array([0.0, 0.25, 0.8], np.float32) if front: surface += (np.exp(-(edge / 0.018) ** 2) * (0.25 + day))[..., None] * np.array([0.25, 0.8, 1.0], np.float32) * 0.9 # the thin air at the rim, inside and out rim = (1 - nz) ** 3.2 * np.clip(lit * 2.2 + 0.05, 0, 1) surface += rim[..., None] * np.array([1.0, 0.72, 0.32], np.float32) * 0.85 out = np.maximum(np.sqrt(rr) - 1, 0) * rad side = np.clip((nx * light[0] + ny * light[1]) / np.maximum(np.sqrt(rr), 1e-3) + 0.35, 0, 1) halo = (np.exp(-out / (rad * 0.016)) * 0.7 + np.exp(-out / (rad * 0.07)) * 0.16) * side dx = int(40 + i * 0.05) back = sky[120:120 + H, dx:dx + W] + halo[..., None] * np.array([1.0, 0.7, 0.34], np.float32) # the sun just out of frame sun = 0.5 / (1 + ((xx + 300) ** 2 + (yy + 150) ** 2) / 420000) back = back + sun[..., None] * np.array([0.8, 0.9, 1.0], np.float32) frame = surface * inside[..., None] + back * (1 - inside[..., None]) cv2.imwrite(os.path.join(root, dst, name), (np.clip(frame, 0, 1) * 255).astype(np.uint8), [cv2.IMWRITE_JPEG_QUALITY, 96]) if i % 60 == 0: print(name, f"centre {cx:.0f},{cy:.0f} radius {rad:.0f}", flush=True)