154 lines
9.5 KiB
Python
154 lines
9.5 KiB
Python
"""Paint the Ljoskjarne's pyramid face (Alfar Nexus) and render a preview.
|
|
Usage: py -3.14 -I paint_nexus.py <pylib> <scripts dir> <work dir>
|
|
<work dir> must hold runes/RuneTile_e.png (Valheim's glyphs, the Ashlands rune tile's glow mask),
|
|
metal/Ironbeam_d.png, both from bundle c4210710. Writes
|
|
AlfarNexus_face.png (one face, u 0..1 along the base, v 1 at the apex), AlfarNexus_glow.png (its glow
|
|
map: the cyan seam down the middle), AlfarNexus_n.png (its normal map), AlfarNexus_icon.png and
|
|
prev_nexus.png; copy the face, glow, normal map and icon to
|
|
JotunnModStub/Assets/AlfarNexus/."""
|
|
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, ImageChops
|
|
from render_obj import render
|
|
d = sys.argv[3]
|
|
S = 512
|
|
|
|
# --- the glyphs: every blob of the rune tile's glow mask, cut out with its bounding box
|
|
mask = np.asarray(Image.open(os.path.join(d,"runes","RuneTile_e.png")).convert("L"), dtype=np.float32)/255
|
|
grown = np.asarray(Image.fromarray((mask>0.15).astype(np.uint8)*255).filter(ImageFilter.MaxFilter(15)))>0
|
|
seen = np.zeros_like(grown); blobs = []
|
|
H, W = grown.shape
|
|
for y in range(H):
|
|
for x in range(W):
|
|
if grown[y,x] and not seen[y,x]:
|
|
stack=[(y,x)]; seen[y,x]=True; ys=[];xs=[]
|
|
while stack:
|
|
cy,cx=stack.pop(); ys.append(cy); xs.append(cx)
|
|
for ny,nx in ((cy+1,cx),(cy-1,cx),(cy,cx+1),(cy,cx-1)):
|
|
if 0<=ny<H and 0<=nx<W and grown[ny,nx] and not seen[ny,nx]: seen[ny,nx]=True; stack.append((ny,nx))
|
|
y0,y1,x0,x1=min(ys)+7,max(ys)-7,min(xs)+7,max(xs)-7
|
|
if (y1-y0)*(x1-x0) > 150: blobs.append((x0,y0,x1+1,y1+1))
|
|
tall = [b for b in blobs if (b[3]-b[1]) > 2.5*(b[2]-b[0])]
|
|
wide = [b for b in blobs if b not in tall and (b[2]-b[0]) < 200]
|
|
glyph = lambda b: Image.fromarray((mask[b[1]:b[3], b[0]:b[2]]*255).astype(np.uint8))
|
|
print("glyphs: tall", len(tall), "short", len(wide))
|
|
|
|
# --- the plate: three riveted steel panels per face, split by two straight seams and framed by a
|
|
# seam along the sloping edges, each brushed in its own streaks toward the apex. Straight lines and
|
|
# streaks read as metal; the iron beam's blotches stay only as a faint hint of wear.
|
|
def norm(a): return (a - a.mean())/(a.std() + 1e-6)
|
|
Y, X = np.mgrid[0:S, 0:S].astype(np.float32) # Y from the apex down
|
|
edge = Y/2 - np.abs(X - (S-1)/2) # pixels inside the sloping edges
|
|
SEAMS_Y = (int(S*0.31), int(S*0.635)) # above the tall runes, between them and the rows
|
|
FRAME = 12 # the edge seam, this far inside the edge
|
|
band = (Y >= SEAMS_Y[0]).astype(int) + (Y >= SEAMS_Y[1]).astype(int)
|
|
|
|
def brushed(seed):
|
|
n = np.random.default_rng(seed).normal(size=(S + 120, S)).astype(np.float32)
|
|
c = np.cumsum(n, axis=0); n = (c[120:] - c[:-120])/120 # long streaks down the face
|
|
return norm(n + 0.5*np.roll(n, 1, axis=1))
|
|
streaks = np.choose(band, [brushed(1), brushed(2), brushed(3)])
|
|
iron = Image.open(os.path.join(d,"metal","Ironbeam_d.png")).convert("L").crop((176,90,256,250))
|
|
mottle = norm(np.asarray(iron.resize((S,S), Image.BICUBIC).filter(ImageFilter.GaussianBlur(10)), dtype=np.float32))
|
|
|
|
seam_lines = np.zeros((S,S), np.float32) # 1 in the groove, soft at its sides
|
|
for y in SEAMS_Y: seam_lines = np.maximum(seam_lines, np.clip(2 - np.abs(Y - y), 0, 1))
|
|
seam_lines = np.maximum(seam_lines, np.clip(2 - np.abs(edge - FRAME), 0, 1)*(Y > FRAME*2))
|
|
|
|
rivets = np.zeros((S,S), np.float32) # domes, 2 cm across
|
|
def rivet(cx, cy, r=5):
|
|
if edge[int(cy), int(cx)] < FRAME + 2 or abs(cx - (S-1)/2) < 30: return
|
|
rivets[:] = np.maximum(rivets, np.sqrt(np.clip(1 - ((X-cx)**2 + (Y-cy)**2)/r**2, 0, 1)))
|
|
for y, below in zip(SEAMS_Y, (True, False)): # the rune rows start just under the second seam
|
|
for cy in ((y - 8, y + 8) if below else (y - 8,)):
|
|
for cx in np.arange(S/2 % 30, S, 30): rivet(cx, cy)
|
|
for cy in np.arange(FRAME*2 + 20, S - 12, 30): # down both sloping edges
|
|
for side in (-1, 1): rivet((S-1)/2 + side*(cy/2 - FRAME - 9), cy)
|
|
|
|
tone = 0.30 + np.choose(band, [0.015, -0.01, 0.005]) + 0.022*streaks + 0.012*mottle
|
|
tone = tone*(1 - 0.5*seam_lines) + 0.08*rivets
|
|
stone = tone[...,None]*np.array([0.95,1.0,1.08])
|
|
|
|
# --- lay the glyphs out inside the face's triangle (in image space the apex is at the top middle)
|
|
ink = Image.new("L", (S,S), 0)
|
|
inside = lambda x, y: abs(x - S/2) < (y/S)*S/2 - 14 # y measured from the apex
|
|
GAP = 26 # half-width kept clear for the seam
|
|
# the tall columns flank the seam
|
|
for i, b in enumerate(tall[:3]):
|
|
g = glyph(b); h = int(S*0.28); w = max(1, int(g.width*h/g.height))
|
|
g = g.resize((w,h), Image.LANCZOS)
|
|
for side in (-1, 1):
|
|
x = int(S/2 + side*(GAP + 8 + i*(w+10)) - (w if side < 0 else 0)); y = int(S*0.34)
|
|
if inside(x, y) and inside(x+w, y): ink.paste(g, (x,y), g)
|
|
# short clusters in rows toward the base
|
|
k = 0
|
|
for row, y in enumerate(range(int(S*0.66), S-40, 48)):
|
|
half = (y/S)*S/2 - 36 # clear of the edge rivets
|
|
x = S/2 - half
|
|
while x < S/2 + half:
|
|
g = glyph(wide[k % len(wide)]); k += 1
|
|
h = 34; w = max(1, int(g.width*h/g.height)); w = min(w, 80)
|
|
g = g.resize((w,h), Image.LANCZOS)
|
|
if abs(x + w/2 - S/2) < GAP + w/2: x = S/2 + GAP; continue
|
|
if inside(x, y) and inside(x+w, y+h): ink.paste(g, (int(x), int(y)), g)
|
|
x += w + 14
|
|
ink = ink.filter(ImageFilter.MaxFilter(3)) # thicker strokes: they are read from metres away
|
|
inkf = np.clip(np.asarray(ink, dtype=np.float32)/255*1.4, 0, 1)
|
|
# carved: a dark rim just below each line, the line itself light grey
|
|
shadow = np.asarray(ink.filter(ImageFilter.GaussianBlur(2)).transform((S,S), Image.AFFINE, (1,0,-1,0,1,-2)), dtype=np.float32)/255
|
|
face = stone*(1 - 0.45*shadow[...,None])
|
|
face = face*(1 - inkf[...,None]) + np.array([0.72,0.73,0.76])*inkf[...,None]
|
|
out = Image.fromarray((np.clip(face,0,1)*255).astype(np.uint8)) # top row is v = 1, the apex, as Unity reads a PNG
|
|
out.save(os.path.join(d,"AlfarNexus_face.png"))
|
|
|
|
# --- the glow map: one line down the middle. u runs 0.5 per metre at every height, so a band of
|
|
# constant width in u is 9 cm wide on the pyramid from base to tip.
|
|
SEAM_HALF = 11 # pixels; GAP keeps the runes clear of it
|
|
xs = np.abs(np.arange(S) - (S-1)/2)
|
|
line = np.clip((SEAM_HALF + 1 - xs)/2, 0, 1) # two pixels of soft edge
|
|
glow = np.tile(line, (S,1))
|
|
Image.fromarray((glow*255).astype(np.uint8)).convert("RGB").save(os.path.join(d,"AlfarNexus_glow.png"))
|
|
|
|
# --- the normal map, from a height field: faint streaks and wear, the panel seams, runes and the
|
|
# glowing seam's channel cut in, the rivets raised. DXT5nm, as Valheim packs it: X in alpha, Y in
|
|
# green and blue, red 255.
|
|
height = 0.12*streaks + 0.25*mottle - 1.6*seam_lines + 2.5*rivets \
|
|
- 2.2*np.asarray(ink.filter(ImageFilter.GaussianBlur(1)), dtype=np.float32)/255 \
|
|
- 1.5*np.clip((SEAM_HALF + 4 - xs)/3, 0, 1)[None,:]
|
|
gx = np.zeros_like(height); gy = np.zeros_like(height)
|
|
gx[:,1:-1] = height[:,2:] - height[:,:-2]; gy[1:-1,:] = height[2:,:] - height[:-2,:]
|
|
K = 0.35
|
|
nm = np.stack([-gx*K, gy*K, np.ones_like(height)], -1) # image y runs down, texture v up
|
|
nm /= np.linalg.norm(nm, axis=-1, keepdims=True)
|
|
packed = np.zeros((S,S,4), np.float32)
|
|
packed[...,0] = 1; packed[...,1] = packed[...,2] = nm[...,1]*0.5 + 0.5; packed[...,3] = nm[...,0]*0.5 + 0.5
|
|
Image.fromarray((packed*255).astype(np.uint8), "RGBA").save(os.path.join(d,"AlfarNexus_n.png"))
|
|
# a lit check of the normal map, light from the upper left
|
|
shade = np.clip(nm @ np.array([-0.5,0.5,0.7])/np.linalg.norm([-0.5,0.5,0.7]), 0, 1)
|
|
Image.fromarray((np.clip(face*shade[...,None]*1.6,0,1)*255).astype(np.uint8)).save(os.path.join(d,"prev_nexus_bump.png"))
|
|
|
|
# --- preview: the pyramid as AlfarNexus builds it
|
|
HGT, B, LIFT = 2.0, 1.0, 0.8 # AlfarNexusLook: 2 m tall, 2 m base, floating 0.8 m up
|
|
apex = np.array([0, LIFT+HGT, 0]); c = [np.array(p) for p in [(-B,LIFT,-B),(B,LIFT,-B),(B,LIFT,B),(-B,LIFT,B)]]
|
|
lit = np.asarray(out, dtype=np.float32)/255
|
|
lit = lit*(1-glow[...,None]) + np.array([0.35,0.75,1.0])*glow[...,None]
|
|
shown = Image.fromarray((lit*255).astype(np.uint8))
|
|
L = []; vi = 1
|
|
for i in range(4):
|
|
a, b = c[i], c[(i+1)%4]
|
|
for p in (a, b, apex): L.append("v %f %f %f" % (-p[0], p[1], p[2]))
|
|
L += ["vt 0 0", "vt 1 0", "vt 0.5 1", f"f {vi}/{vi} {vi+1}/{vi+1} {vi+2}/{vi+2}"]; vi += 3
|
|
for p in c: L.append("v %f %f %f" % (-p[0], p[1], p[2]))
|
|
L += ["vt 0 0", f"f {vi}/{3*4+1} {vi+3}/{3*4+1} {vi+2}/{3*4+1} {vi+1}/{3*4+1}"]
|
|
open(os.path.join(d,"pyramid.obj"),"w").write("\n".join(L)+"\n")
|
|
open(os.path.join(d,"ground.obj"),"w").write("v -2 0 -2\nv 2 0 -2\nv 2 0 2\nv -2 0 2\nvt 0 0\nf 1/1 2/1 3/1 4/1\n")
|
|
sh = B*0.85
|
|
open(os.path.join(d,"shadow.obj"),"w").write(
|
|
f"v {-sh} 0.005 {-sh}\nv {sh} 0.005 {-sh}\nv {sh} 0.005 {sh}\nv {-sh} 0.005 {sh}\nvt 0 0\nf 1/1 2/1 3/1 4/1\n")
|
|
parts = [{'obj':os.path.join(d,'pyramid.obj'),'tex':shown},
|
|
{'obj':os.path.join(d,'ground.obj'),'color':(0.33,0.36,0.3)},
|
|
{'obj':os.path.join(d,'shadow.obj'),'color':(0.18,0.2,0.17)}]
|
|
render(parts, os.path.join(d,'prev_nexus.png'), size=640, yaw=25, pitch=-12, light=(0.4,0.8,0.6), fit=(np.array([0,1.4,0]), 2.5))
|
|
# the hammer icon: the pyramid alone, nearly side-on so two faces show
|
|
render(parts[:1], os.path.join(d,'AlfarNexus_icon.png'), size=128, yaw=35, pitch=-12, bg=(0,0,0,0), light=(0.3,0.8,0.7))
|