forge
Explore
Log in

Don't you dare go Hollow

courier

Clone
★ 0

0 watching0 forksRustAGPL-3.05.3 MiB6d9eeb9Tahinli21 days ago3 commits

art_env.py785 lines, 22.4 KiBHistoryRawDownload
1import functools
2import math
3import os
4import sys
5
6import numpy as np
7from PIL import Image
8
9sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
10import vox
11from vox import P, hash3
12
13G = 'env'
14PREVIEW = '/tmp/claude-1000/-home-tahinli-Documents-Code-Rust/edd994b8-9b3e-47f3-95c7-9404536bd326/scratchpad/env'
15FIRE = P['fire']
16SLAG = [(28, 22, 22), (52, 40, 36), (78, 60, 50), (116, 76, 56)]
17EMBER = [(60, 20, 14), (110, 34, 20), (150, 50, 26), (180, 70, 34)]
18BIRCH = [(70, 66, 60), (150, 144, 136), (200, 194, 182), (232, 228, 216)]
19BG = (34, 32, 38, 255)
20RES = {}
21MC = {}
22
23
24def mt(ramp, pat=None, em=False):
25 if isinstance(ramp, str):
26 ramp = P[ramp]
27 key = (tuple(ramp), em, pat)
28 if key not in MC:
29 assert len(vox._MATS) < 255, 'vox material ids exhausted'
30 MC[key] = vox.mat(list(ramp), pat, em)
31 return MC[key]
32
33
34@functools.lru_cache(None)
35def const(k):
36 return lambda cx, cy, cz, f: k
37
38
39@functools.lru_cache(None)
40def sprinkle(s, p, a):
41 return lambda cx, cy, cz, f: np.where(hash3(cx, cy, 0, s) < p, a, 0)
42
43
44def ring(pat=None):
45 return pat
46
47
48R0 = ring()
49
50
51def gh(s, n=32):
52 gx, gy = np.indices((n, n))
53 return hash3(gx, gy, 0, s)
54
55
56def wrap_disc(gx, gy, centers, shrink=0.0):
57 out = np.zeros(gx.shape, bool)
58 for px, py, r in centers:
59 for ox in (-32, 0, 32):
60 for oy in (-32, 0, 32):
61 out |= (gx - px - ox) ** 2 + (gy - py - oy) ** 2 <= (r - shrink) ** 2
62 return out
63
64
65def top(f):
66 return (f == 2).astype(np.int64)
67
68
69class Cv:
70 def __init__(self, sx, sy, sz, off=(0, 0)):
71 self.m = vox.Model(sx, sy, sz)
72 self.o = off
73
74 def p(self, a):
75 return (a[0] + self.o[0], a[1] + self.o[1], a[2])
76
77 def box(self, a, b, m):
78 pa, pb = self.p(a), self.p(b)
79 self.m.box(pa[0], pa[1], pa[2], pb[0], pb[1], pb[2], m)
80
81 def ell(self, c, r, m):
82 pc = self.p(c)
83 self.m.ellipsoid(pc[0], pc[1], pc[2], r[0], r[1], r[2], m)
84
85 def cap(self, a, b, r, m):
86 self.m.capsule(self.p(a), self.p(b), r, m)
87
88 def cyl(self, a, b, r, m):
89 self.m.cylinder(self.p(a), self.p(b), r, m)
90
91 def gable(self, x0, y0, x1, y1, z0, z1, axis, m, ov=0):
92 ox, oy = self.o
93 self.m.gable(x0 + ox, y0 + oy, x1 + ox, y1 + oy, z0, z1, axis, m, ov)
94
95
96def finish(name, cv, outline=True, edges=True):
97 img, em, org = vox.render(cv.m, outline, edges)
98 anchor = [int(round(a)) for a in vox.project(cv.o[0], cv.o[1], 0, org)]
99 if not edges:
100 bb = img.getbbox()
101 img = img.crop(bb)
102 anchor = [anchor[0] - bb[0], anchor[1] - bb[1]]
103 anchor = tuple(anchor)
104 vox.save(name, img, anchor, emissive=em, group=G)
105 RES[name] = (img, anchor)
106
107
108def add(name, img, origin):
109 vox.save(name, img, origin, group=G)
110 RES[name] = (img, origin)
111
112
113def tile(name, base, fix=None, h=3):
114 cv = Cv(32, 32, h)
115 cv.m.v[:, :, 0] = base
116 if fix:
117 fix(cv.m.v)
118 finish(name, cv, outline=False, edges=False)
119
120
121def brick(cx, cy, w, h, stag):
122 row = cy // h
123 x = cx + (row * stag) % w
124 return x % w, cy % h, (x // w) % (32 // w), row % (32 // h)
125
126
127def paving(name, ramp, joint, w, h, stag, s, var):
128 def pat(cx, cy, cz, f):
129 lx, ly, sx, row = brick(cx, cy, w, h, stag)
130 sid = hash3(sx, row, 0, s)
131 v = np.floor(sid * (2 * var + 1)).astype(np.int64) - var
132 return v + ((lx == 1) | (ly == 1)) - ((lx == w - 1) | (ly == h - 1))
133 gx, gy = np.indices((32, 32))
134 lx, ly, _, _ = brick(gx, gy, w, h, stag)
135 jm = (lx == 0) | (ly == 0)
136 jmat = mt(joint, ring(sprinkle(s + 3, 0.12, -1)))
137
138 def fix(v):
139 v[:, :, 0][jm] = jmat
140 tile(name, mt(ramp, ring(pat)), fix)
141
142
143def grass(i):
144 def pat(cx, cy, cz, f):
145 return sprinkle(20 + i, 0.03, -1)(cx, cy, cz, f) + sprinkle(30 + i, 0.02, 1)(cx, cy, cz, f)
146 tuft = mt('grass', const(1))
147 cream = mt('cloth_cream')
148 wine = mt('cloth_wine')
149
150 def fix(v):
151 m = gh(40 + i) < 0.012
152 v[:, :, 1][m] = tuft
153 if i == 1:
154 v[:, :, 2][m & (gh(49) < 0.5)] = tuft
155 if i == 3:
156 fl = gh(41) < 0.012
157 c = gh(42) < 0.5
158 v[:, :, 1][fl & c] = cream
159 v[:, :, 1][fl & ~c] = wine
160 tile(f'grass{i}', mt('grass', ring(pat)), fix)
161
162
163def road(i):
164 def pat(cx, cy, cz, f):
165 return np.where(hash3(cx // 4, cy // 4, 0, 30 + i) < 0.25, -1, 0) + sprinkle(40 + i, 0.03, 1)(cx, cy, cz, f)
166 stone = mt('stone')
167
168 def fix(v):
169 v[:, :, 1][gh(50 + i) < 0.01] = stone
170 tile(f'road{i}', mt('dirt', pat), fix)
171
172
173def soot(i):
174 def pat(cx, cy, cz, f):
175 p = sprinkle(40 + i, 0.05, 1)(cx, cy, cz, f)
176 if i == 1:
177 p = p + np.where(hash3(cx // 8, cy // 8, 0, 44) < 0.4, -1, 0)
178 return p
179 mud = mt('mud', R0)
180 ember = mt(EMBER, R0)
181 lump = mt('coal', const(2))
182
183 def fix(v):
184 gx, gy = np.indices((32, 32))
185 if i == 2:
186 v[:, :, 0][hash3(gx // 4, gy // 4, 0, 45) < 0.2] = mud
187 v[:, :, 0][gh(50 + i) < 0.004] = ember
188 if i == 3:
189 v[:, :, 1][gh(60) < 0.012] = lump
190 tile(f'soot{i}', mt('coal', ring(pat)), fix)
191
192
193def mud_pat(s):
194 def f(cx, cy, cz, face):
195 return np.where(hash3(cx // 4, cy // 4, 0, s) < 0.35, -1, 0) + sprinkle(s + 9, 0.05, 1)(cx, cy, cz, face)
196 return f
197
198
199def mud(i):
200 cen = [(6, 8, 4), (22, 20, 5), (12, 27, 3)] if i == 0 else [(20, 6, 3), (6, 21, 4)]
201 pud = mt('water', ring(const(-1)))
202 rim = mt('water', R0)
203 base = mt('mud', ring(mud_pat(50 + i)))
204
205 def fix(v):
206 gx, gy = np.indices((32, 32)) + 0.5
207 v[:, :, 0][wrap_disc(gx, gy, cen)] = rim
208 v[:, :, 0][wrap_disc(gx, gy, cen, 1.2)] = pud
209 tile(f'mud{i}', base, fix)
210
211
212def farm(i):
213 gx, gy = np.indices((32, 32))
214 along = gy
215 per = 4 if i == 0 else 8
216 ridge = (along % per) < per // 2
217 rmat = mt('dirt', R0)
218 sprout = mt('grass', const(1))
219
220 def fix(v):
221 v[:, :, 1][ridge] = rmat
222 v[:, :, 1][(~ridge) & (gh(90 + i) < 0.08 + 0.08 * i)] = sprout
223 tile(f'farmland{i}', mt('dirt', ring(const(-1))), fix)
224
225
226def water(i):
227 def pat(cx, cy, cz, f):
228 rip = (cx % 4 != 3) & (hash3(cx // 4, cy, 0, 70 + i) < 0.1)
229 return rip.astype(np.int64) + sprinkle(80 + i, 0.04, -1)(cx, cy, cz, f)
230 tile(f'water{i}', mt('water', ring(pat)))
231
232
233def rail():
234 gx, gy = np.indices((32, 32))
235 cv = Cv(32, 32, 3)
236 cv.m.v[:, :, 0] = mt('coal', ring(sprinkle(95, 0.1, 1)))
237 cv.m.v[:, :, 0][(gx % 8 < 2) & (gy >= 7) & (gy < 25)] = mt('wood_dark', R0)
238 cv.m.v[:, 11, 1] = mt('metal')
239 cv.m.v[:, 20, 1] = mt('metal')
240 finish('rail_x', cv, outline=False, edges=False)
241 cv2 = Cv(32, 32, 3)
242 cv2.m.v = np.ascontiguousarray(cv.m.v.transpose(1, 0, 2))
243 finish('rail_y', cv2, outline=False, edges=False)
244
245
246def tiles():
247 for i in range(4):
248 grass(i)
249 road(i)
250 soot(i)
251 paving('cobble0', 'cobble', 'stone_dark', 8, 4, 4, 11, 1)
252 paving('cobble1', 'cobble', 'stone_dark', 8, 8, 4, 12, 1)
253 paving('cobble2', 'cobble', 'stone_dark', 4, 4, 2, 13, 1)
254 paving('cobble3', 'cobble', 'stone_dark', 8, 4, 0, 14, 1)
255 paving('flag0', 'stone', 'stone_dark', 8, 8, 0, 21, 0)
256 paving('flag1', 'stone', 'stone_dark', 16, 8, 8, 22, 1)
257 paving('flag2', 'stone', 'stone_dark', 8, 8, 4, 23, 1)
258 paving('flag3', 'stone', 'stone_dark', 16, 16, 8, 24, 1)
259 mud(0)
260 mud(1)
261 farm(0)
262 farm(1)
263 water(0)
264 water(1)
265 tile('planks', mt('wood', ring(vox.planks(4, 0))))
266 rail()
267
268
269def weave(cx, cy, cz, f):
270 return np.where(((cx + cz) % 4 == 0) | ((cy + cz) % 4 == 0), -1, 0)
271
272
273def leaf_pat(s):
274 def f(cx, cy, cz, face):
275 blob = np.where(hash3(cx // 3, cy // 3, cz // 3, s) < 0.3, -1, 0)
276 return blob + top(face)
277 return f
278
279
280def bark_pat(s):
281 def f(cx, cy, cz, face):
282 return np.where(hash3(cx // 2, cy // 2, cz // 3, s) < 0.4, -1, 0) + top(face)
283 return f
284
285
286def rock_pat(s):
287 def f(cx, cy, cz, face):
288 return sprinkle(s, 0.06, -1)(cx, cy, cz, face) + sprinkle(s + 1, 0.05, 1)(cx, cy, cz, face) + top(face)
289 return f
290
291
292def crate_pat(x0, y0, w, d):
293 def f(cx, cy, cz, face):
294 edge = (cx == x0) | (cx == x0 + w - 1) | (cy == y0) | (cy == y0 + d - 1)
295 return edge.astype(np.int64) - (cz % 6 == 0).astype(np.int64)
296 return f
297
298
299def crate(cv, x0, y0, z0, w, d, h):
300 cv.box((x0, y0, z0), (x0 + w, y0 + d, z0 + h), mt('wood', crate_pat(x0, y0, w, d)))
301
302
303def barrel(cv, cx, cy, h=22, r=6.5):
304 cv.ell((cx, cy, h / 2), (r, r, h / 2), mt('wood', vox.rows(4)))
305 for z in (4, h - 6):
306 cv.cyl((cx, cy, z), (cx, cy, z + 1), r + 0.7, mt('metal'))
307 cv.cyl((cx, cy, h - 1), (cx, cy, h), r * 0.6, mt('wood_dark'))
308
309
310def sack(cv, cx, cy, sc):
311 m = mt('canvas', weave)
312 cv.ell((cx, cy, 7 * sc), (8 * sc, 6.5 * sc, 7 * sc), m)
313 cv.cyl((cx, cy, 13 * sc), (cx, cy, 15 * sc), 2.4 * sc, m)
314
315
316def bale_pat(cx, cy, cz, f):
317 rope = (cx == 9) | (cx == 22) | (cz == 4) | (cz == 10)
318 return np.where(rope, -2, 0) + sprinkle(7, 0.08, 1)(cx, cy, cz, f)
319
320
321def coal_pat(cx, cy, cz, f):
322 return sprinkle(111, 0.08, 1)(cx, cy, cz, f) + sprinkle(112, 0.08, -1)(cx, cy, cz, f) + top(f)
323
324
325def slag_pat(cx, cy, cz, f):
326 return sprinkle(120, 0.02, 1)(cx, cy, cz, f) + sprinkle(121, 0.1, -1)(cx, cy, cz, f) + top(f)
327
328
329def cross_pat(cx, cy, cz, f):
330 c = ((cx == 15) & (cz >= 7) & (cz <= 11)) | ((cz == 9) & (cx >= 14) & (cx <= 17))
331 return np.where(c, -1, 0) + top(f)
332
333
334def birch_pat(cx, cy, cz, f):
335 return np.where(hash3(cx, cy, cz // 2, 311) < 0.15, -2, 0) + top(f)
336
337
338def p_crate():
339 cv = Cv(32, 32, 16)
340 crate(cv, 5, 5, 0, 22, 22, 14)
341 finish('crate', cv)
342
343
344def p_crates():
345 cv = Cv(32, 32, 28)
346 crate(cv, 2, 4, 0, 14, 14, 12)
347 crate(cv, 16, 14, 0, 14, 14, 12)
348 crate(cv, 4, 6, 12, 11, 11, 10)
349 finish('crates', cv)
350
351
352def p_barrel():
353 cv = Cv(32, 32, 24)
354 barrel(cv, 16, 16)
355 finish('barrel', cv)
356
357
358def p_barrels():
359 cv = Cv(32, 32, 24)
360 barrel(cv, 9, 9)
361 barrel(cv, 23, 10)
362 barrel(cv, 16, 23)
363 finish('barrels', cv)
364
365
366def p_sack():
367 cv = Cv(32, 32, 18)
368 sack(cv, 16, 16, 1.0)
369 finish('sack', cv)
370
371
372def p_sacks():
373 cv = Cv(32, 32, 18)
374 sack(cv, 8, 9, 1.0)
375 sack(cv, 22, 10, 1.0)
376 sack(cv, 15, 22, 0.8)
377 finish('sacks', cv)
378
379
380def p_bale():
381 cv = Cv(32, 32, 20)
382 cv.box((3, 7, 0), (29, 25, 15), mt('cloth_cream', bale_pat))
383 finish('bale', cv)
384
385
386def p_haystack():
387 cv = Cv(32, 32, 28)
388 cv.ell((16, 16, 0), (14, 14, 13), mt('thatch', lambda cx, cy, cz, f: np.where((cx + 2 * cz) % 5 == 0, -1, 0) + sprinkle(8, 0.06, 1)(cx, cy, cz, f)))
389 finish('haystack', cv)
390
391
392def p_logs():
393 cv = Cv(32, 32, 22)
394 bark = mt('bark', bark_pat(600))
395 cut = mt('wood', R0)
396 for y, z in ((7, 3.5), (15, 3.5), (23, 3.5), (11, 9.6), (19, 9.6), (27, 9.6), (15, 15.7)):
397 cv.cyl((2, y, z), (30, y, z), 3.5, bark)
398 cv.cyl((2, y, z), (3.6, y, z), 2.6, cut)
399 cv.cyl((28.4, y, z), (30.5, y, z), 2.6, cut)
400 finish('logs', cv)
401
402
403def fence_cv():
404 cv = Cv(32, 32, 22)
405 cv.box((0, 27, 0), (2, 31, 20), mt('wood_dark'))
406 cv.box((0, 28, 6), (32, 30, 9), mt('wood', vox.rows(4)))
407 cv.box((0, 28, 13), (32, 30, 16), mt('wood', vox.rows(4)))
408 return cv
409
410
411def p_fences():
412 cv = fence_cv()
413 finish('fence_x', cv)
414 cv2 = Cv(32, 32, 22)
415 cv2.m.v = np.ascontiguousarray(cv.m.v.transpose(1, 0, 2))
416 finish('fence_y', cv2)
417
418
419def p_lamp():
420 cv = Cv(32, 32, 94)
421 iron = mt('slate')
422 cv.cyl((16, 16, 0), (16, 16, 4), 3.6, iron)
423 cv.cyl((16, 16, 4), (16, 16, 8), 2.4, iron)
424 cv.cyl((16, 16, 8), (16, 16, 76), 1.2, iron)
425 cv.cyl((16, 16, 76), (16, 16, 78), 2.2, iron)
426 cv.box((11, 11, 78), (21, 21, 80), iron)
427 cv.box((12, 12, 80), (20, 20, 88), mt('window', None, True))
428 for x, y in ((12, 12), (20, 12), (12, 20), (20, 20)):
429 cv.cyl((x, y, 80), (x, y, 88), 0.7, iron)
430 cv.box((9, 9, 88), (23, 23, 90), iron)
431 cv.box((12, 12, 90), (20, 20, 92), iron)
432 cv.cyl((16, 16, 92), (16, 16, 94), 0.9, iron)
433 finish('lamp', cv)
434
435
436def p_well():
437 cv = Cv(32, 32, 46)
438 cv.cyl((16, 16, 0), (16, 16, 12), 9.5, mt('stone', vox.bricks(4, 8)))
439 cv.cyl((16, 16, 8), (16, 16, 13), 7.0, 0)
440 cv.cyl((16, 16, 7), (16, 16, 8), 6.5, mt('water'))
441 cv.box((6, 15, 12), (8, 17, 32), mt('wood'))
442 cv.box((24, 15, 12), (26, 17, 32), mt('wood'))
443 cv.cyl((6, 16, 24), (26, 16, 24), 1.0, mt('wood_dark'))
444 cv.gable(4, 6, 28, 26, 30, 38, 'x', mt('wood_dark', vox.rows(3)), 1)
445 finish('well', cv)
446
447
448def p_bench():
449 cv = Cv(32, 32, 26)
450 cv.box((4, 11, 11), (28, 21, 13), mt('wood', vox.planks(3, 2)))
451 cv.box((4, 11, 0), (6, 21, 11), mt('wood_dark'))
452 cv.box((26, 11, 0), (28, 21, 11), mt('wood_dark'))
453 cv.box((4, 9, 13), (28, 11, 22), mt('wood', vox.rows(4)))
454 finish('bench', cv)
455
456
457def p_table():
458 cv = Cv(32, 32, 24)
459 cv.box((7, 9, 17), (25, 23, 19), mt('wood', vox.planks(4, 2)))
460 cv.cyl((16, 16, 0), (16, 16, 17), 1.6, mt('wood_dark'))
461 cv.cyl((16, 16, 0), (16, 16, 1), 6.0, mt('wood_dark'))
462 for x in (5, 27):
463 cv.cyl((x, 16, 0), (x, 16, 9), 3.0, mt('wood'))
464 cv.cyl((x, 16, 9), (x, 16, 10), 3.4, mt('wood_dark'))
465 finish('table', cv)
466
467
468def p_signpost():
469 cv = Cv(32, 32, 34)
470 cv.box((15, 15, 0), (17, 17, 34), mt('wood_dark'))
471 cv.box((17, 14, 22), (28, 18, 27), mt('wood', vox.rows(4)))
472 cv.box((4, 14, 12), (15, 18, 17), mt('wood', vox.rows(4)))
473 finish('signpost', cv)
474
475
476def p_stall():
477 cv = Cv(64, 32, 42)
478 for x in (2, 59):
479 for y in (2, 26):
480 cv.box((x, y, 0), (x + 3, y + 3, 33), mt('wood_dark'))
481 cv.box((5, 6, 10), (59, 24, 15), mt('wood', vox.planks(4, 2)))
482 cv.box((12, 10, 15), (20, 20, 20), mt('cloth_cream', weave))
483 cv.box((0, 0, 33), (64, 30, 36), mt('cloth_wine'))
484 cream = mt('cloth_cream')
485 for x in range(64):
486 if (x // 8) % 2:
487 cv.m.v[x, 0:30, 33:36] = cream
488 finish('stall', cv)
489
490
491def p_coalpile():
492 cv = Cv(32, 32, 20)
493 coal = mt('coal', coal_pat)
494 cv.ell((16, 16, 0), (14, 12, 9), coal)
495 cv.ell((9, 20, 0), (7, 6, 6), coal)
496 cv.ell((23, 10, 0), (6, 6, 5), coal)
497 finish('coalpile', cv)
498
499
500def p_slag():
501 cv = Cv(64, 64, 34)
502 m = mt(SLAG, slag_pat)
503 cv.ell((32, 32, 0), (28, 25, 15), m)
504 cv.ell((14, 44, 0), (10, 9, 8), m)
505 cv.ell((50, 14, 0), (9, 10, 8), m)
506 finish('slag', cv)
507
508
509def p_tub():
510 cv = Cv(32, 32, 26)
511 cv.box((5, 9, 6), (27, 23, 18), mt('slate'))
512 cv.box((5, 9, 17), (27, 23, 19), mt('metal'))
513 cv.box((7, 11, 19), (25, 21, 22), mt('coal', coal_pat))
514 for x in (8, 24):
515 for y in (11, 20):
516 cv.cyl((x, y - 0.5, 5), (x, y + 1.5, 5), 3.0, mt('metal'))
517 finish('tub', cv)
518
519
520def p_flowers(i):
521 rng = np.random.default_rng(3 + i)
522 cv = Cv(32, 32, 6)
523 cv.box((2, 2, 0), (30, 30, 4), mt('dirt', sprinkle(70 + i, 0.1, 1)))
524 cols = [mt('cloth_wine'), mt('cloth_cream'), mt('window')] if i == 0 else [mt('cloth_navy'), mt('cloth_cream'), mt('cloth_wine')]
525 leaf = mt('grass')
526 for _ in range(70):
527 x, y = rng.integers(3, 29, 2)
528 cv.m.v[x, y, 4] = leaf
529 for _ in range(26):
530 x, y = rng.integers(3, 29, 2)
531 cv.m.v[x, y, 4] = leaf
532 cv.m.v[x, y, 5] = cols[rng.integers(3)]
533 finish(f'flowers{i}', cv)
534
535
536def p_bush(i):
537 cv = Cv(32, 32, 24)
538 leaf = mt('foliage' if i == 0 else 'foliage_dark', leaf_pat(130 + i))
539 for x, y, z, r in ((16, 16, 9, 9), (10, 14, 7, 6), (22, 17, 7, 6), (15, 22, 6, 5)):
540 cv.ell((x, y, z), (r, r, r * 0.8), leaf)
541 finish(f'bush{i}', cv)
542
543
544def p_rock(i):
545 cv = Cv(32, 32, 16)
546 m = mt('stone', rock_pat(140 + i))
547 if i == 0:
548 cv.ell((16, 16, 6), (10, 8, 6.5), m)
549 else:
550 cv.ell((12, 14, 5), (8, 7, 5.5), m)
551 cv.ell((22, 20, 3.5), (6, 5, 3.5), m)
552 finish(f'rock{i}', cv)
553
554
555def p_grave():
556 cv = Cv(32, 32, 22)
557 cv.ell((16, 16, 0), (12, 9, 2.5), mt('mud', mud_pat(160)))
558 cv.box((12, 13, 0), (20, 19, 4), mt('stone_dark'))
559 head = mt('stone', cross_pat)
560 cv.box((12, 14, 4), (20, 18, 15), head)
561 cv.ell((16, 16, 15), (4, 2, 3.5), head)
562 finish('grave', cv)
563
564
565def p_campfire():
566 cv = Cv(32, 32, 8)
567 stone = mt('stone', rock_pat(150))
568 cv.ell((16, 16, 0.5), (4, 4, 1), mt('coal'))
569 for k in range(12):
570 a = 2 * math.pi * k / 12
571 cv.ell((16 + 9 * math.cos(a), 16 + 9 * math.sin(a), 1.5), (3.2, 2.8, 2.0), stone)
572 cv.cyl((8, 12, 2.5), (24, 20, 2.5), 1.6, mt('bark'))
573 cv.cyl((8, 20, 2.5), (24, 12, 2.5), 1.6, mt('bark'))
574 finish('campfire', cv)
575
576
577def p_flame(i):
578 cv = Cv(32, 32, 22)
579 ms = [mt([c] * 4, None, True) for c in ((255, 120, 30), (255, 170, 50), (255, 226, 120), (255, 250, 210))]
580 gx, gy, gz = np.meshgrid(np.arange(32) + 0.5, np.arange(32) + 0.5, np.arange(22) + 0.5, indexing='ij')
581 ph = 1.9 * i
582 cxf = 16 + 0.7 * np.sin(gz * 0.5 + ph) * gz / 22
583 cyf = 16 + 0.5 * np.cos(gz * 0.4 + ph) * gz / 22
584 r = 5.6 * np.clip(1 - gz / 22, 0, 1) ** 0.55 * (1 + 0.2 * np.sin(gz * 1.1 + 2 * ph)) + 0.4
585 d = np.hypot(gx - cxf, gy - cyf) / r
586 v = cv.m.v
587 v[d < 1] = ms[0]
588 v[d < 0.65] = ms[1]
589 v[d < 0.35] = ms[2]
590 v[(d < 0.5) & (gz > 13)] = ms[3]
591 finish(f'flame{i}', cv, outline=False)
592
593
594def p_tent():
595 cv = Cv(64, 32, 44)
596 canvas = mt('canvas', vox.rows(6))
597 cv.gable(4, 3, 60, 29, 0, 40, 'x', canvas)
598 cv.m.v[26:38, 18:29, 0:15] = 0
599 cv.m.v[26:38, 12:18, 0:15] = mt('cloth_black')
600 cv.cap((4, 16, 41), (60, 16, 41), 0.9, mt('wood'))
601 leather = mt('leather')
602 cv.cap((20, 16, 40), (14, 1, 0), 0.5, leather)
603 cv.cap((44, 16, 40), (50, 1, 0), 0.5, leather)
604 finish('tent', cv)
605
606
607def tree_cv():
608 return Cv(128, 128, 176, (48, 48))
609
610
611def p_oak(i):
612 rng = np.random.default_rng(200 + i)
613 cv = tree_cv()
614 bark = mt('bark', bark_pat(210 + i))
615 cv.cap((16, 16, 0), (16, 16, 62), 3.4, bark)
616 for _ in range(5):
617 a = rng.uniform(0, 2 * math.pi)
618 cv.cap((16, 16, 48 + rng.uniform(0, 10)), (16 + 24 * math.cos(a), 16 + 24 * math.sin(a), 92 + rng.uniform(0, 14)), 2.0, bark)
619 light = mt('foliage', leaf_pat(220 + i))
620 dark = mt('foliage_dark', leaf_pat(230 + i))
621 for _ in range(14 + 4 * i):
622 a = rng.uniform(0, 2 * math.pi)
623 r = 28 * math.sqrt(rng.uniform())
624 rad = rng.uniform(12, 18)
625 c = (16 + r * math.cos(a), 16 + r * math.sin(a), 102 + rng.normal(0, 12))
626 cv.ell(c, (rad, rad, rad * 0.85), light if rng.random() < 0.6 else dark)
627 finish(f'oak{i}', cv)
628
629
630def p_birch():
631 rng = np.random.default_rng(301)
632 cv = tree_cv()
633 cv.cap((16, 16, 0), (16, 16, 95), 2.3, mt(BIRCH, birch_pat))
634 light = mt('foliage')
635 dark = mt('foliage_dark')
636 for _ in range(34):
637 a = rng.uniform(0, 2 * math.pi)
638 r = 22 * math.sqrt(rng.uniform())
639 rad = rng.uniform(6, 10)
640 c = (16 + r * math.cos(a), 16 + r * math.sin(a), 110 + rng.normal(0, 14))
641 cv.ell(c, (rad, rad, rad * 1.1), light if rng.random() < 0.7 else dark)
642 finish('birch', cv)
643
644
645def p_pine():
646 cv = tree_cv()
647 cv.cap((16, 16, 0), (16, 16, 110), 2.6, mt('bark'))
648 dark = mt('foliage_dark', leaf_pat(402))
649 for t in range(8):
650 r = 36 - 4.6 * t
651 cv.ell((16, 16, 24 + 15 * t), (r, r, 5.5 + 0.3 * (7 - t)), dark)
652 cv.cap((16, 16, 120), (16, 16, 148), 1.8, dark)
653 finish('pine', cv)
654
655
656def p_dead():
657 rng = np.random.default_rng(501)
658 cv = tree_cv()
659 bark = mt('bark', bark_pat(502))
660
661 def br(p, d, L, r, depth):
662 q = p + d * L
663 cv.cap(p, q, r, bark)
664 if depth > 0:
665 for _ in range(int(rng.integers(2, 4))):
666 nd = d + rng.normal(0, 0.8, 3)
667 nd /= np.linalg.norm(nd)
668 br(q, nd, L * 0.68, r * 0.7, depth - 1)
669 br(np.array([16.0, 16.0, 0.0]), np.array([0.0, 0.0, 1.0]), 46, 3.0, 4)
670 finish('dead', cv)
671
672
673def small():
674 n = 12
675 yy, xx = np.mgrid[0:n, 0:n] + 0.5
676 a = np.zeros((n, n))
677 for dx, dy, r in ((-1.8, 0.8, 4.2), (1.6, -0.6, 4.6), (0.0, 2.2, 3.6), (0.0, -1.8, 3.2)):
678 d = np.hypot(xx - n / 2 - dx, yy - n / 2 - dy) / r
679 a = np.maximum(a, np.clip(1 - d, 0, 1) ** 0.6)
680 g = (120 + 60 * a).astype(np.uint8)
681 add('puff', Image.fromarray(np.dstack([g, g, g, (a * 230).astype(np.uint8)])), (n // 2, n // 2))
682
683 W, H = 96, 48
684 yy, xx = np.mgrid[0:H, 0:W] + 0.5
685 d = np.hypot((xx - W / 2) / (W / 2), (yy - H / 2) / (H / 2))
686 al = np.clip(1 - d, 0, 1) ** 2
687 rgba = np.dstack([np.full((H, W), 255.0), np.full((H, W), 236.0), np.full((H, W), 190.0), al * 255]).astype(np.uint8)
688 add('light', Image.fromarray(rgba), (W // 2, H // 2))
689
690 e = np.zeros((1, 5, 4), np.uint8)
691 e[0, 0] = e[0, 3] = (255, 214, 40, 255)
692 add('eyes', Image.fromarray(e), (2, 0))
693
694
695def props():
696 p_crate()
697 p_crates()
698 p_barrel()
699 p_barrels()
700 p_sack()
701 p_sacks()
702 p_bale()
703 p_haystack()
704 p_logs()
705 p_fences()
706 p_lamp()
707 p_well()
708 p_bench()
709 p_table()
710 p_signpost()
711 p_stall()
712 p_coalpile()
713 p_slag()
714 p_tub()
715 p_flowers(0)
716 p_flowers(1)
717 p_bush(0)
718 p_bush(1)
719 p_rock(0)
720 p_rock(1)
721 p_grave()
722 p_campfire()
723 for i in range(4):
724 p_flame(i)
725 p_tent()
726 for i in range(3):
727 p_oak(i)
728 p_birch()
729 p_pine()
730 p_dead()
731 small()
732
733
734def save_scaled(img, path, k=2):
735 bg = Image.new('RGBA', img.size, BG)
736 bg.alpha_composite(img.convert('RGBA'))
737 bg.resize((bg.width * k, bg.height * k), Image.NEAREST).save(path)
738
739
740def tile_block(names):
741 cv = Image.new('RGBA', (260, 150), BG)
742 for s in range(7):
743 for i in range(4):
744 j = s - i
745 if not 0 <= j < 4:
746 continue
747 img, org = RES[names[(i + j) % len(names)]]
748 cv.alpha_composite(img, (130 + 32 * (i - j) - org[0], 8 + 16 * (i + j) - org[1]))
749 return cv
750
751
752def preview():
753 os.makedirs(PREVIEW, exist_ok=True)
754 items = list(RES.values())
755 rows = [items[k:k + 9] for k in range(0, len(items), 9)]
756 out, _, _, _ = vox.atlas(rows)
757 save_scaled(out, os.path.join(PREVIEW, 'sheet.png'))
758 fams = [
759 ['grass0', 'grass1', 'grass2', 'grass3'],
760 ['road0', 'road1', 'road2', 'road3'],
761 ['cobble0', 'cobble1', 'cobble2', 'cobble3'],
762 ['flag0', 'flag1', 'flag2', 'flag3'],
763 ['soot0', 'soot1', 'soot2', 'soot3'],
764 ['mud0', 'mud1'],
765 ['farmland0', 'farmland1'],
766 ['water0', 'water1'],
767 ['planks'],
768 ['rail_x'],
769 ['rail_y'],
770 ]
771 per = 6
772 sheet = Image.new('RGBA', (260 * per, 150 * ((len(fams) + per - 1) // per)), BG)
773 for k, names in enumerate(fams):
774 sheet.paste(tile_block(names), ((k % per) * 260, (k // per) * 150))
775 save_scaled(sheet, os.path.join(PREVIEW, 'tiles4.png'))
776
777
778def main():
779 tiles()
780 props()
781 preview()
782
783
784if __name__ == '__main__':
785 main()