"""Compose actual Blender renders, with per-atom triangle counts and a PBR shelf."""
from pathlib import Path
import json
from PIL import Image,ImageDraw,ImageFont
ROOT=Path(__file__).resolve().parent
font='/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'
def f(n):return ImageFont.truetype(font,n)
k=json.loads((ROOT/'kit.json').read_text());atoms=k['atoms'];W=2400;cellW=400;cellH=410;header=165
out=Image.new('RGB',(W,header+5*cellH+35),'#dedfdd');d=ImageDraw.Draw(out)
d.text((35,24),'NORTHERN EUROPE · LATE MEDIEVAL',font=f(42),fill='#252522')
d.text((38,84),f"29 construction atoms · {k['total_triangles']:,} triangles · 14 shared materials · metres / Y-up",font=f(25),fill='#43453f')
d.text((38,123),'Actual GLB renders • Each cell fitted independently, not shown at common scale',font=f(18),fill='#656660')
for i,a in enumerate(atoms):
 x=i%6*cellW;y=header+i//6*cellH;im=Image.open(ROOT/'renders'/f"{a['node_name']}.png").convert('RGB');im.thumbnail((cellW-12,333))
 out.paste(im,(x+(cellW-im.width)//2,y));d.text((x+16,y+337),a['node_name'],font=f(22),fill='#252522');d.text((x+16,y+369),f"{a['triangles']:,} / {a['triangle_budget']:,} tris",font=f(18),fill='#656660')
out.save(ROOT/'kit_sheet.png')
shelf=json.loads((ROOT/'materials.json').read_text())['materials'];out=Image.new('RGB',(2100,1240),'#dedfdd');d=ImageDraw.Draw(out)
d.text((30,22),'SHARED MATERIAL SHELF',font=f(36),fill='#252522');d.text((30,74),'BaseColor previews · registered Normal / ORM maps · 16-bit Height on stone and lime plaster',font=f(19),fill='#55574f')
for i,(name,a) in enumerate(shelf.items()):
 x=i%7*300;y=120+i//7*540;im=Image.open(ROOT/a['maps']['BaseColor']).convert('RGB');im=im.resize((280,350));out.paste(im,(x+10,y));d.text((x+10,y+368),name,font=f(20),fill='#252522');d.text((x+10,y+400),f"{a['tile_size_metres']} m repeat · {a['resolution']} px",font=f(17),fill='#656660');d.text((x+10,y+430),'BaseColor / Normal / ORM'+(' / Height' if 'height_map' in a else ''),font=f(13),fill='#656660')
out.save(ROOT/'material_sheet.png')
