#!/usr/bin/env python3 """장면 덤프(JSON)를 간단한 3D 그림으로 그립니다. 서버에서 화면을 볼 수 없어 배치 확인용으로 씁니다. 로블록스 렌더링과 다르고(조명·질감·애니메이션 없음), 파트 위치·크기·색만 맞습니다. python3 tools/render.py .cache/scene.json out.png --eye x,y,z --at x,y,z [--size 1280x720] """ import argparse import json import math import numpy as np from PIL import Image def box_mesh(): v = np.array([[x, y, z] for x in (-0.5, 0.5) for y in (-0.5, 0.5) for z in (-0.5, 0.5)]) f = [(0, 1, 3), (0, 3, 2), (4, 6, 7), (4, 7, 5), (0, 4, 5), (0, 5, 1), (2, 3, 7), (2, 7, 6), (0, 2, 6), (0, 6, 4), (1, 5, 7), (1, 7, 3)] return v, f def wedge_mesh(): # 로블록스 WedgePart: 아래 면 전체, 뒤(-Z) 면 전체, 경사면은 위-뒤에서 아래-앞으로 v = np.array([[-0.5, -0.5, -0.5], [0.5, -0.5, -0.5], [-0.5, -0.5, 0.5], [0.5, -0.5, 0.5], [-0.5, 0.5, -0.5], [0.5, 0.5, -0.5]]) f = [(0, 1, 3), (0, 3, 2), (0, 4, 5), (0, 5, 1), (2, 3, 5), (2, 5, 4), (0, 2, 4), (1, 5, 3)] return v, f def cyl_mesh(n=14): # 축은 X v = [] for x in (-0.5, 0.5): for i in range(n): a = 2 * math.pi * i / n v.append([x, 0.5 * math.cos(a), 0.5 * math.sin(a)]) v.append([-0.5, 0, 0]) v.append([0.5, 0, 0]) f = [] for i in range(n): j = (i + 1) % n f += [(i, j, n + j), (i, n + j, n + i), (2 * n, j, i), (2 * n + 1, n + i, n + j)] return np.array(v), f def ball_mesh(lat=7, lon=12): v = [] for i in range(lat + 1): th = math.pi * i / lat for j in range(lon): ph = 2 * math.pi * j / lon v.append([0.5 * math.sin(th) * math.cos(ph), 0.5 * math.cos(th), 0.5 * math.sin(th) * math.sin(ph)]) f = [] for i in range(lat): for j in range(lon): a = i * lon + j b = i * lon + (j + 1) % lon c = (i + 1) * lon + j d = (i + 1) * lon + (j + 1) % lon f += [(a, c, d), (a, d, b)] return np.array(v), f MESHES = {"Block": box_mesh(), "Wedge": wedge_mesh(), "Cylinder": cyl_mesh(), "Ball": ball_mesh()} def triangles(parts): tris, cols = [], [] for p in parts: v, f = MESHES.get(p["s"], MESHES["Block"]) c = p["c"] pos = np.array(c[:3]) rot = np.array(c[3:]).reshape(3, 3) size = np.array(p["z"]) w = (v * size) @ rot.T + pos col = np.array(p["k"], dtype=float) if p["m"] == "Neon": col = np.minimum(col * 1.25 + 30, 255) for a, b, d in f: tris.append(w[[a, b, d]]) cols.append((col, p["t"], p["m"] == "Neon")) return np.array(tris), cols def render(parts, eye, at, w, h, fov=55): tris, cols = triangles(parts) eye, at = np.array(eye, float), np.array(at, float) fwd = at - eye fwd /= np.linalg.norm(fwd) right = np.cross(fwd, [0, 1, 0]) right /= np.linalg.norm(right) up = np.cross(right, fwd) rel = tris - eye cx = rel @ right cy = rel @ up cz = rel @ fwd f = 0.5 * h / math.tan(math.radians(fov) / 2) img = np.zeros((h, w, 3)) # 하늘 for y in range(h): t = y / h img[y] = np.array([150, 200, 250]) * (1 - t) + np.array([210, 230, 250]) * t zbuf = np.full((h, w), np.inf) light = np.array([0.4, 0.85, 0.35]) light /= np.linalg.norm(light) order = np.argsort([1 if c[1] > 0.3 else 0 for c in cols], kind="stable") # 투명은 나중에 for i in order: z = cz[i] if np.any(z < 0.5): continue sx = w / 2 + f * cx[i] / z sy = h / 2 - f * cy[i] / z x0, x1 = int(max(0, math.floor(sx.min()))), int(min(w - 1, math.ceil(sx.max()))) y0, y1 = int(max(0, math.floor(sy.min()))), int(min(h - 1, math.ceil(sy.max()))) if x0 > x1 or y0 > y1: continue t = tris[i] n = np.cross(t[1] - t[0], t[2] - t[0]) nn = np.linalg.norm(n) if nn == 0: continue n /= nn if np.dot(n, eye - t[0]) < 0: n = -n col, tr, neon = cols[i] shade = 1.0 if neon else 0.55 + 0.45 * max(0.0, float(np.dot(n, light))) rgb = np.clip(col * shade, 0, 255) xs, ys = np.meshgrid(np.arange(x0, x1 + 1) + 0.5, np.arange(y0, y1 + 1) + 0.5) (ax, bx, cx_), (ay, by, cy_) = sx, sy den = (by - cy_) * (ax - cx_) + (cx_ - bx) * (ay - cy_) if abs(den) < 1e-9: continue l1 = ((by - cy_) * (xs - cx_) + (cx_ - bx) * (ys - cy_)) / den l2 = ((cy_ - ay) * (xs - cx_) + (ax - cx_) * (ys - cy_)) / den l3 = 1 - l1 - l2 inside = (l1 >= -1e-6) & (l2 >= -1e-6) & (l3 >= -1e-6) if not inside.any(): continue iz = l1 / z[0] + l2 / z[1] + l3 / z[2] depth = 1 / np.where(iz > 0, iz, 1e-9) sub = zbuf[y0:y1 + 1, x0:x1 + 1] m = inside & (depth < sub) if tr > 0.3: a = 1 - tr region = img[y0:y1 + 1, x0:x1 + 1] region[m] = region[m] * (1 - a) + rgb * a else: sub[m] = depth[m] img[y0:y1 + 1, x0:x1 + 1][m] = rgb return Image.fromarray(img.astype(np.uint8)) def main(): ap = argparse.ArgumentParser() ap.add_argument("scene") ap.add_argument("out") ap.add_argument("--eye", default="0,120,160") ap.add_argument("--at", default="0,0,0") ap.add_argument("--size", default="1280x720") ap.add_argument("--fov", type=float, default=55) a = ap.parse_args() with open(a.scene) as f: parts = json.load(f) w, h = map(int, a.size.split("x")) eye = [float(x) for x in a.eye.split(",")] at = [float(x) for x in a.at.split(",")] # 큰 것(바닥)부터 그려도 z-버퍼가 있으니 순서는 상관없음 render(parts, eye, at, w, h, a.fov).save(a.out) print(a.out) if __name__ == "__main__": main()