Update
This commit is contained in:
commit
2d6a8a867d
1093 files changed
+2424571
No files matched your search
@@ -0,0 +1,152 @@
|
||||
# Generates voxel ornament .bbmodel files (Blockbench, Generic Model). Usage: python gen_ornaments.py <out_dir>
|
||||
import json, uuid, zlib, struct, base64, math, os, sys
|
||||
from collections import deque
|
||||
|
||||
def png(px, w, h):
|
||||
raw = b''.join(b'\0' + bytes(c for p in px[y*w:(y+1)*w] for c in p + (255,)) for y in range(h))
|
||||
ch = lambda t, d: struct.pack('>I', len(d)) + t + d + struct.pack('>I', zlib.crc32(t + d) & 0xffffffff)
|
||||
return b'\x89PNG\r\n\x1a\n' + ch(b'IHDR', struct.pack('>IIBBBBB', w, h, 8, 6, 0, 0, 0)) + ch(b'IDAT', zlib.compress(raw)) + ch(b'IEND', b'')
|
||||
|
||||
def hx(s): return tuple(int(s[i:i+2], 16) for i in (0, 2, 4))
|
||||
def sh(c, f): # f<1 darken, f>1 lighten
|
||||
if f < 1: return tuple(int(v * f) for v in c)
|
||||
return tuple(int(v + (255 - v) * min(1, f - 1)) for v in c)
|
||||
|
||||
GOLD, GEMC = hx('f2b705'), hx('45e0f0')
|
||||
|
||||
class Model:
|
||||
def __init__(s): s.cols, s.els = [], []
|
||||
def box(s, x0, y0, z0, x1, y1, z1, c, name='v'):
|
||||
q = [round(v / 8) * 8 for v in c] # quantize to keep palette small
|
||||
c = tuple(min(255, v) for v in q)
|
||||
if c not in s.cols: s.cols.append(c)
|
||||
i = s.cols.index(c); u, v = i % 16 + .25, i // 16 + .25
|
||||
f = {d: {'uv': [u, v, u + .5, v + .5], 'texture': 0} for d in ('north', 'south', 'east', 'west', 'up', 'down')}
|
||||
s.els.append({'name': name, 'box_uv': False, 'type': 'cube', 'from': [x0, y0, z0], 'to': [x1, y1, z1],
|
||||
'origin': [0, 0, 0], 'faces': f, 'uuid': str(uuid.uuid4())})
|
||||
def save(s, fn, name):
|
||||
assert len(s.cols) <= 256, name
|
||||
px = (s.cols + [(0, 0, 0)] * 256)[:256]
|
||||
src = 'data:image/png;base64,' + base64.b64encode(png(px, 16, 16)).decode()
|
||||
d = {'meta': {'format_version': '4.5', 'model_format': 'free', 'box_uv': False}, 'name': name,
|
||||
'resolution': {'width': 16, 'height': 16}, 'elements': s.els, 'outliner': [e['uuid'] for e in s.els],
|
||||
'textures': [{'name': name + '.png', 'folder': '', 'namespace': '', 'id': '0', 'width': 16, 'height': 16,
|
||||
'uv_width': 16, 'uv_height': 16, 'particle': False, 'render_mode': 'default', 'visible': True,
|
||||
'saved': False, 'uuid': str(uuid.uuid4()), 'source': src}]}
|
||||
with open(fn, 'w') as fh: json.dump(d, fh)
|
||||
|
||||
def edge_dist(S):
|
||||
dist, q = {}, deque()
|
||||
N = ((1, 0), (-1, 0), (0, 1), (0, -1))
|
||||
for p in S:
|
||||
if any((p[0]+a, p[1]+b) not in S for a, b in N): dist[p] = 0; q.append(p)
|
||||
while q:
|
||||
p = q.popleft()
|
||||
for a, b in N:
|
||||
n = (p[0]+a, p[1]+b)
|
||||
if n in S and n not in dist: dist[n] = dist[p] + 1; q.append(n)
|
||||
return dist
|
||||
|
||||
def shade(base, x, y, cy, R):
|
||||
t = (-(x + .5) + (y + .5 - cy)) / (2 * R) # light from upper-left
|
||||
f = 1 + 0.45 * t + (0.05 if (x + y) % 2 else -0.05) # pixel checker
|
||||
return sh(base, f)
|
||||
|
||||
def body(m, S, hf, colf):
|
||||
dist = edge_dist(S)
|
||||
for (x, y) in S:
|
||||
h = max(.5, round(hf(x, y, dist[(x, y)]) * 2) / 2)
|
||||
m.box(x, y, -h, x + 1, y + 1, h, colf(x, y), 'body')
|
||||
|
||||
def cap_hook(m, top, w=4):
|
||||
x0 = -w // 2
|
||||
m.box(x0, top, -1.5, x0 + w, top + 2, 1.5, sh(GOLD, .9), 'cap')
|
||||
for gx in (x0 + .5, x0 + w - 1.5):
|
||||
m.box(gx, top + .5, -1.75, gx + 1, top + 1.5, 1.75, GEMC, 'cap_gem')
|
||||
hook = ['.###.', '#...#', '....#', '...#.', '..#..', '.#.#.', '..#..']
|
||||
for r, row in enumerate(hook):
|
||||
for cI, ch in enumerate(row):
|
||||
if ch == '#':
|
||||
y = top + 2 + len(hook) - 1 - r
|
||||
m.box(cI - 2, y, -.5, cI - 1, y + 1, .5, sh(GOLD, 1.1 if r < 3 else 1), 'hook')
|
||||
|
||||
def gem(m, x, y, s, z, c):
|
||||
m.box(x - s/2 - .5, y - s/2 - .5, -z, x + s/2 + .5, y + s/2 + .5, z, GOLD, 'frame')
|
||||
m.box(x - s/2, y - s/2, -z - .25, x + s/2, y + s/2, z + .25, sh(c, 1.15), 'gem')
|
||||
|
||||
def bauble(m, base, gemc):
|
||||
rx, ry, rz = 7.5, 8, 4.5
|
||||
S = {(x, y) for x in range(-8, 8) for y in range(-8, 8) if ((x+.5)/rx)**2 + ((y+.5)/ry)**2 <= 1}
|
||||
r2 = lambda x, y: ((x+.5)/rx)**2 + ((y+.5)/ry)**2
|
||||
hf = lambda x, y, d: rz * math.sqrt(max(0, 1 - r2(x, y))) + .5 + (.5 if y in (-1, 0) else 0)
|
||||
colf = lambda x, y: shade(GOLD if y in (-1, 0) else base, x, y, 0, 8)
|
||||
body(m, S, hf, colf)
|
||||
gem(m, 0, 0, 2, rz + 1.25, gemc)
|
||||
for sx in (-5, 5): gem(m, sx, 0, 1, rz * math.sqrt(1 - (sx/rx)**2) + 1, gemc)
|
||||
cap_hook(m, max(y for _, y in S) + 1)
|
||||
|
||||
def in_poly(px, py, P):
|
||||
ins, j = False, len(P) - 1
|
||||
for i in range(len(P)):
|
||||
(xi, yi), (xj, yj) = P[i], P[j]
|
||||
if (yi > py) != (yj > py) and px < (xj - xi) * (py - yi) / (yj - yi) + xi: ins = not ins
|
||||
j = i
|
||||
return ins
|
||||
|
||||
def star(m, base):
|
||||
P = [((8 if k % 2 == 0 else 3.6) * math.cos(math.pi/2 + k*math.pi/5),
|
||||
(8 if k % 2 == 0 else 3.6) * math.sin(math.pi/2 + k*math.pi/5) - .5) for k in range(10)]
|
||||
S = {(x, y) for x in range(-9, 9) for y in range(-10, 9) if in_poly(x + .5, y + .5, P)}
|
||||
body(m, S, lambda x, y, d: min(3, 1 + .7 * d), lambda x, y: shade(base, x, y, 0, 8))
|
||||
cap_hook(m, max(y for _, y in S) + 1, 2)
|
||||
|
||||
def seg_d(px, py, ax, ay, bx, by):
|
||||
dx, dy = bx - ax, by - ay
|
||||
t = max(0, min(1, ((px-ax)*dx + (py-ay)*dy) / (dx*dx + dy*dy)))
|
||||
return math.hypot(px - ax - t*dx, py - ay - t*dy)
|
||||
|
||||
def snowflake(m, base):
|
||||
segs = []
|
||||
for k in range(6):
|
||||
a = math.pi/2 + k*math.pi/3
|
||||
ex, ey = 7.5*math.cos(a), 7.5*math.sin(a); segs.append((0, 0, ex, ey))
|
||||
bx, by = 4.5*math.cos(a), 4.5*math.sin(a)
|
||||
for s in (-1, 1):
|
||||
b = a + s*math.pi/3; segs.append((bx, by, bx + 2.6*math.cos(b), by + 2.6*math.sin(b)))
|
||||
S = {(x, y) for x in range(-9, 9) for y in range(-9, 9) if min(seg_d(x+.5, y+.5, *g) for g in segs) < .85}
|
||||
body(m, S, lambda x, y, d: 1.5, lambda x, y: shade(base, x, y, 0, 8))
|
||||
gem(m, 0, 0, 2, 2.25, hx('7ff6ff'))
|
||||
cap_hook(m, max(y for _, y in S) + 1, 2)
|
||||
|
||||
def bell(m, base):
|
||||
def w(y):
|
||||
if y <= -5: return 8
|
||||
return 2.5 + ((6 - y) / 11) ** 2 * 5
|
||||
S = {(x, y) for y in range(-6, 7) for x in range(-9, 9) if abs(x + .5) <= w(y)}
|
||||
hf = lambda x, y, d: .5 + .65 * math.sqrt(max(0, w(y)**2 - (x + .5)**2))
|
||||
body(m, S, hf, lambda x, y: shade(GOLD if y <= -5 else base, x, y, 0, 8))
|
||||
m.box(-1, -8, -1, 1, -6, 1, sh(base, .8), 'clapper')
|
||||
cap_hook(m, 7)
|
||||
|
||||
CL = hx # alias
|
||||
SHEETS = {
|
||||
'common_01-10': [
|
||||
('01_RedBauble', bauble, hx('e0202a'), hx('ff5060')),
|
||||
('02_GreenBauble', bauble, hx('20b030'), hx('50ff70')),
|
||||
('03_BlueBauble', bauble, hx('1e6ae8'), GEMC),
|
||||
('04_GoldBauble', bauble, hx('f5b800'), hx('fff07a')),
|
||||
('05_SilverBauble', bauble, hx('c9d3e8'), GEMC),
|
||||
('06_GoldStar', star, hx('ffd21a')),
|
||||
('07_RedStar', star, hx('e0202a')),
|
||||
('08_SilverStar', star, hx('c9d3e8')),
|
||||
('09_Snowflake', snowflake, hx('5fd8f5')),
|
||||
('10_GoldBell', bell, hx('f5c000')),
|
||||
]}
|
||||
|
||||
if __name__ == '__main__':
|
||||
out = sys.argv[1]
|
||||
for sheet, items in SHEETS.items():
|
||||
d = os.path.join(out, sheet); os.makedirs(d, exist_ok=True)
|
||||
for name, fn, *args in items:
|
||||
m = Model(); fn(m, *args); m.save(os.path.join(d, name + '.bbmodel'), name)
|
||||
print(sheet, name, len(m.els), 'cubes', len(m.cols), 'colors')
|
||||
Reference in new issue
Block a user