feat: Alfar chests get bodies of their own

Players mistook a plain Wood chest for the Eldividarkista, so neither
Alfar chest looks like a buildable vanilla chest any more.

Eldividarkista: the Mistlands dvergr crate stretched to 1.6 x 0.8 m,
blackened iron with a bronze frame and an iron grate on the front. The
grate glows and throws sparks only while the chest holds wood the drones
may burn.

Inngangskista: the dvergr town loot chest, navy steel with silver trim. A
cyan line in the lid shines while it holds anything but the sigil.

Both have new hammer icons, and their hover names say their role (input,
fuel) in English, Italian and Russian. If a borrowed vanilla model is
missing, the chest falls back to the old tinted wood chest.

scripts/paint_*_chest.py repaint the textures and render previews and
icons offline with scripts/render_obj.py.
This commit is contained in:
kanym 2026-10-30 18:20:13 +01:00
commit b661fa6516
15 changed files with 634 additions and 10 deletions

View file

@ -0,0 +1,89 @@
"""Paint the Eldividarkista (Alfar fire chest) and render its preview and hammer icon.
Usage: py -3.14 -I paint_fire_chest.py <pylib> <scripts dir> <work dir>
<work dir> must hold tex/Crate_d.png and crate_0.obj / crate_2.obj (body, lid) exported from
dvergrprops_crate in bundle c4210710 (see extract_carto.py for the export). Writes
AlfarFireChest_d.png and AlfarFireChest_icon.png there; copy both to
JotunnModStub/Assets/AlfarFireChest/."""
import sys, os, math
sys.path.insert(0, sys.argv[1]); sys.path.insert(0, sys.argv[2])
import numpy as np
from PIL import Image, ImageFilter
from render_obj import render
d = sys.argv[3]
src = Image.open(os.path.join(d,"tex","Crate_d.png")).convert("RGB")
S = 256
up = np.asarray(src.resize((S,S), Image.BICUBIC), dtype=np.float32)/255
lum = up @ np.array([0.3,0.59,0.11])
# normalise the wood's shading into 0..1 so the brace and frame keep their relief
lo, hi = np.percentile(lum, 3), np.percentile(lum, 97)
t = np.clip((lum-lo)/(hi-lo), 0, 1)
rng = np.random.default_rng(7)
grain = np.asarray(Image.fromarray((rng.random((S,S))*255).astype(np.uint8)).filter(ImageFilter.GaussianBlur(0.8)), dtype=np.float32)/255
# the panels: plain brushed plates, the wood's planks and X-brace flattened away
brushed = np.asarray(Image.fromarray((rng.random((S,S))*255).astype(np.uint8)).resize((S//32,S), Image.BICUBIC).resize((S,S), Image.BICUBIC), dtype=np.float32)/255
iron = np.array([0.17,0.18,0.20])[None,None,:] * (0.85 + 0.3*brushed)[...,None] * (0.92 + 0.16*t)[...,None]
# the raised frame and brace become copper: every triangle lying on the outer shell of the body
# or lid (|x|, |z| or y at the extreme) paints its UV area into the copper mask
from PIL import ImageDraw
from render_obj import load_obj
mask = Image.new("L", (S,S), 0); md = ImageDraw.Draw(mask)
for name in ("crate_0.obj","crate_2.obj"):
v, vt, faces = load_obj(os.path.join(d,name))
mn, mx = v.min(0), v.max(0)
for f in faces:
pv = np.array([v[i] for i,_ in f]); c = pv.mean(0)
n = np.cross(pv[1]-pv[0], pv[2]-pv[0]); nl = np.linalg.norm(n)
if nl == 0: continue
n = n/nl; ax = int(np.argmax(np.abs(n)))
outer = (abs(c[ax]-mx[ax]) < 0.012) or (abs(c[ax]-mn[ax]) < 0.012)
if ax != 1 and not outer: continue
if ax == 1 and name == "crate_0.obj": continue # body top/bottom: skip
if ax == 1 and not outer: continue
uv = [(vt[j][0]*S, (1-vt[j][1])*S) for _,j in f]
md.polygon(uv, fill=255)
mask = mask.filter(ImageFilter.GaussianBlur(0.6))
mask.save(os.path.join(d,"copper_mask.png"))
cu = (np.asarray(mask, dtype=np.float32)/255)[...,None]
copper = np.array([0.30,0.17,0.10]) * (0.8 + 0.4*t[...,None])
iron = iron*(1-cu) + copper*cu
iron *= (0.9 + 0.2*grain)[...,None]
# soot: a warm brown tint creeping into the darkest texels
soot = np.clip(0.35 - t, 0, 0.35)/0.35
iron = iron*(1-0.25*soot[...,None]) + np.array([0.12,0.06,0.03])*0.25*soot[...,None]
out = np.clip(iron,0,1)
Image.fromarray((out*255).astype(np.uint8)).save(os.path.join(d,"AlfarFireChest_d.png"))
# a unit cube OBJ for the grate bars and the glow plate
cube = """v -0.5 -0.5 -0.5\nv 0.5 -0.5 -0.5\nv 0.5 0.5 -0.5\nv -0.5 0.5 -0.5\nv -0.5 -0.5 0.5\nv 0.5 -0.5 0.5\nv 0.5 0.5 0.5\nv -0.5 0.5 0.5\nvt 0 0\nf 1/1 2/1 3/1 4/1\nf 5/1 8/1 7/1 6/1\nf 1/1 5/1 6/1 2/1\nf 2/1 6/1 7/1 3/1\nf 3/1 7/1 8/1 4/1\nf 4/1 8/1 5/1 1/1\n"""
open(os.path.join(d,"cube.obj"),"w").write(cube)
tex = Image.open(os.path.join(d,"AlfarFireChest_d.png"))
# the crate stretched into a chest: 1.6x as wide, 0.8x as deep (AlfarFireChest.Stretch)
SX, SZ = 1.6, 0.8
parts=[{'obj':os.path.join(d,'crate_0.obj'),'tex':tex,'scale':(1.5*SX,1.5,1.5*SZ)},
{'obj':os.path.join(d,'crate_2.obj'),'tex':tex,'scale':(1.5*SX,1.5,1.5*SZ),'offset':(0,0.81,0)}]
# grate on the +z face: glow plate, frame, bars (sizes in metres, mirrored like the OBJ x)
FZ = 0.515*SZ; GW, GH, GY = 0.62*SX, 0.36, 0.42
glow = (1.0, 0.45, 0.12)
bar = (0.16,0.15,0.15)
parts.append({'obj':os.path.join(d,'cube.obj'),'color':glow,'emissive':True,'scale':(GW,GH,0.01),'offset':(0,GY,FZ+0.005)})
T = 0.035
for x in (-GW/2, GW/2):
parts.append({'obj':os.path.join(d,'cube.obj'),'color':bar,'scale':(T,GH+T,0.04),'offset':(x,GY,FZ+0.02)})
for y in (GY-GH/2, GY+GH/2):
parts.append({'obj':os.path.join(d,'cube.obj'),'color':bar,'scale':(GW+T,T,0.04),'offset':(0,y,FZ+0.02)})
n = 10
for i in range(1,n):
x = -GW/2 + GW*i/n
parts.append({'obj':os.path.join(d,'cube.obj'),'color':bar,'scale':(0.022,GH,0.03),'offset':(x,GY,FZ+0.02)})
parts.append({'obj':os.path.join(d,'cube.obj'),'color':bar,'scale':(GW,0.022,0.03),'offset':(0,GY,FZ+0.022)})
render(parts, os.path.join(d,'prev_fire.png'), yaw=-30, pitch=-20, light=(0.4,0.8,0.6))
render(parts, os.path.join(d,'prev_fire_side.png'), yaw=-80, pitch=-15, light=(0.4,0.8,0.6))
from PIL import Image as I
tiles=[]
for k,yw in enumerate((-30,60,150,240)):
p=os.path.join(d,f'_q{k}.png'); render(parts,p,size=256,yaw=yw,pitch=-20,light=(0.4,0.8,0.6)); tiles.append(I.open(p))
sheet=I.new('RGBA',(1024,256))
for k,tl in enumerate(tiles): sheet.paste(tl,(k*256,0))
sheet.save(os.path.join(d,'prev_sheet.png'))
render(parts, os.path.join(d,'AlfarFireChest_icon.png'), size=128, yaw=160, pitch=-22, bg=(0,0,0,0), light=(0.3,0.8,0.7))

View file

@ -0,0 +1,45 @@
"""Paint the Inngangskista (Alfar input chest) and render its preview and hammer icon.
Usage: py -3.14 -I paint_input_chest.py <pylib> <scripts dir> <work dir>
<work dir> must hold tex/dvergrchestWorn_d.png, tex/anvil.png and
TreasureChest_dvergrtown__stonechest.obj / __stonechesttop_closed.obj, exported from bundle c4210710.
Writes AlfarInputChest_d.png and AlfarInputChest_icon.png; copy both to
JotunnModStub/Assets/AlfarInputChest/. The trim is recoloured in code (AlfarInputChest.Trim)."""
import sys, os
sys.path.insert(0, sys.argv[1]); sys.path.insert(0, sys.argv[2])
import numpy as np
from PIL import Image, ImageFilter
from render_obj import render
d = sys.argv[3]
S = 128
src = Image.open(os.path.join(d,"tex","dvergrchestWorn_d.png")).convert("RGB")
up = np.asarray(src.resize((S,S), Image.BICUBIC), dtype=np.float32)/255
lum = up @ np.array([0.3,0.59,0.11])
lo, hi = np.percentile(lum, 3), np.percentile(lum, 97)
t = np.clip((lum-lo)/(hi-lo), 0, 1)
rng = np.random.default_rng(11)
brushed = np.asarray(Image.fromarray((rng.random((S,S))*255).astype(np.uint8)).resize((S,S//16), Image.BICUBIC).resize((S,S), Image.BICUBIC), dtype=np.float32)/255
# steel-blue plates: the planks' seams survive as faint plate joints
steel = np.array([0.12,0.17,0.24])[None,None,:] * (0.85 + 0.25*brushed)[...,None] * (0.8 + 0.35*t)[...,None]
Image.fromarray((np.clip(steel,0,1)*255).astype(np.uint8)).save(os.path.join(d,"AlfarInputChest_d.png"))
TRIM = (1.25,1.3,1.35) # AlfarInputChest.Trim
K = 1.5*1.2 # the mesh scale times the chest's 1.2
LIDTOP, HX, HZ = 0.48*K, 0.42*K, 0.24*K
INSET, W = 0.10, 0.03 # AlfarInputChest inlay
cyan = (0.35,0.9,1.0)
panel = Image.open(os.path.join(d,"AlfarInputChest_d.png")); metal = Image.open(os.path.join(d,"tex","anvil.png"))
parts = [{'obj':os.path.join(d,'TreasureChest_dvergrtown__'+n+'.obj'),'texs':[metal,panel],'colors':[TRIM,(1,1,1)],'scale':(K,K,K)}
for n in ('stonechest','stonechesttop_closed')]
cube = os.path.join(d,'cube.obj')
ix, iz = HX-INSET, HZ-INSET
for z in (-iz, iz): parts.append({'obj':cube,'color':cyan,'emissive':True,'scale':(2*ix+W,0.01,W),'offset':(0,LIDTOP+0.004,z)})
for x in (-ix, ix): parts.append({'obj':cube,'color':cyan,'emissive':True,'scale':(W,0.01,2*iz),'offset':(x,LIDTOP+0.004,0)})
tiles = []
for k,yw in enumerate((-30,60,150,240)):
p = os.path.join(d,f'_i{k}.png'); render(parts,p,size=256,yaw=yw,pitch=-30,light=(0.4,0.8,0.6)); tiles.append(Image.open(p))
sheet = Image.new('RGBA',(1024,256))
for k,tl in enumerate(tiles): sheet.paste(tl,(k*256,0))
sheet.save(os.path.join(d,'prev_input.png'))
render(parts, os.path.join(d,'AlfarInputChest_icon.png'), size=128, yaw=160, pitch=-28, bg=(0,0,0,0), light=(0.3,0.8,0.7))

88
scripts/render_obj.py Normal file
View file

@ -0,0 +1,88 @@
"""Tiny software renderer: render(parts, out, size, yaw, pitch, bg)
parts: list of dicts {obj: path, tex: PIL image or None, color:(r,g,b), offset:(x,y,z), scale:(sx,sy,sz), emissive:bool}
OBJ x is mirrored vs Unity: we flip x back."""
import numpy as np
from PIL import Image
import math
def load_obj(path, groups=False):
v=[];vt=[];vn=[];faces=[];gid=[];g=0;gn=0;ids={}
for l in open(path):
p=l.split()
if not p: continue
if p[0]=='g': gn+=1; g=gn
elif p[0]=='v': v.append([float(x) for x in p[1:4]])
elif p[0]=='vt': vt.append([float(x) for x in p[1:3]])
elif p[0]=='f':
idx=[]
for k in p[1:]:
a=k.split('/'); idx.append((int(a[0])-1, int(a[1])-1 if len(a)>1 and a[1] else -1))
# a group counts as a submesh only once it has faces (the export starts with an empty one)
if g not in ids: ids[g]=len(ids)
for i in range(1,len(idx)-1): faces.append((idx[0],idx[i],idx[i+1])); gid.append(ids[g])
v=np.array(v); v[:,0]*=-1
if groups: return v, np.array(vt) if vt else None, faces, gid
return v, np.array(vt) if vt else None, faces
def render(parts, out, size=512, yaw=35, pitch=25, bg=(40,44,52,255), fit=None, light=(0.4,0.8,-0.5)):
tris=[]
for P in parts:
v,vt,faces,gid=load_obj(P['obj'],groups=True)
if 'rotx' in P:
a=math.radians(P['rotx']); c,s=math.cos(a),math.sin(a)
v=v@np.array([[1,0,0],[0,c,-s],[0,s,c]]).T
v=v*np.array(P.get('scale',(1,1,1)))+np.array(P.get('offset',(0,0,0)))
# 'texs'/'colors'/'emissives' per OBJ group (submesh) override 'tex'/'color'/'emissive'
def arr(t): return np.asarray(t.convert('RGBA'),dtype=np.float32)/255 if t is not None else None
texs=[arr(t) for t in P['texs']] if 'texs' in P else None
ta0=arr(P.get('tex'))
for f,gi in zip(faces,gid):
pv=np.array([v[i] for i,_ in f]); puv=np.array([vt[j] if (vt is not None and j>=0) else (0,0) for _,j in f])
ta=texs[min(gi,len(texs)-1)] if texs is not None else ta0
col=P['colors'][min(gi,len(P['colors'])-1)] if 'colors' in P else P.get('color',(1,1,1))
em=P['emissives'][min(gi,len(P['emissives'])-1)] if 'emissives' in P else P.get('emissive',False)
tris.append((pv,puv,ta,col,em))
ay=math.radians(yaw); ap=math.radians(pitch)
Ry=np.array([[math.cos(ay),0,math.sin(ay)],[0,1,0],[-math.sin(ay),0,math.cos(ay)]])
Rx=np.array([[1,0,0],[0,math.cos(ap),-math.sin(ap)],[0,math.sin(ap),math.cos(ap)]])
R=Rx@Ry
allv=np.concatenate([t[0] for t in tris])@R.T
if fit is None:
mn=allv.min(0); mx=allv.max(0); c=(mn+mx)/2; ext=max(mx[0]-mn[0],mx[1]-mn[1])*0.55
else: c,ext=fit
img=np.zeros((size,size,4),np.float32); img[:]=np.array(bg)/255
zb=np.full((size,size),-1e9)
L=np.array(light);L=L/np.linalg.norm(L)
for pv,puv,ta,col,em in tris:
q=pv@R.T
n=np.cross(pv[1]-pv[0],pv[2]-pv[0]); nl=np.linalg.norm(n)
if nl==0: continue
n=n/nl
nv=n@R.T
shade=1.0 if em else 0.35+0.65*abs(float(n@L))
sx=(q[:,0]-c[0])/ext*size/2+size/2; sy=size/2-(q[:,1]-c[1])/ext*size/2
x0=max(int(sx.min()),0);x1=min(int(sx.max())+1,size);y0=max(int(sy.min()),0);y1=min(int(sy.max())+1,size)
if x0>=x1 or y0>=y1: continue
xs,ys=np.meshgrid(np.arange(x0,x1)+0.5,np.arange(y0,y1)+0.5)
d=(sy[1]-sy[2])*(sx[0]-sx[2])+(sx[2]-sx[1])*(sy[0]-sy[2])
if abs(d)<1e-9: continue
w0=((sy[1]-sy[2])*(xs-sx[2])+(sx[2]-sx[1])*(ys-sy[2]))/d
w1=((sy[2]-sy[0])*(xs-sx[2])+(sx[0]-sx[2])*(ys-sy[2]))/d
w2=1-w0-w1
m=(w0>=0)&(w1>=0)&(w2>=0)
if not m.any(): continue
z=w0*q[0,2]+w1*q[1,2]+w2*q[2,2]
z=-z
sub=zb[y0:y1,x0:x1]; m&=z>sub
if not m.any(): continue
if ta is not None:
u=(w0*puv[0,0]+w1*puv[1,0]+w2*puv[2,0])%1; vv=(w0*puv[0,1]+w1*puv[1,1]+w2*puv[2,1])%1
th,tw=ta.shape[:2]
px=np.clip((u*tw).astype(int),0,tw-1); py=np.clip(((1-vv)*th).astype(int),0,th-1)
rgb=ta[py,px,:3]
else: rgb=np.ones(xs.shape+(3,))
rgb=rgb*np.array(col)*shade
sub[m]=z[m]
reg=img[y0:y1,x0:x1]
reg[m,:3]=np.clip(rgb[m],0,1); reg[m,3]=1
Image.fromarray((img*255).astype(np.uint8)).save(out)