sourceextensions/scene/demos/voxels-world.xtl
1⍝# Voxels, 4: a world of chunks. An island 64 by 32 by 64 -- 32 chunks of
2⍝# 16 cubed -- from value noise made by linear algebra (random heights on
3⍝# grids every 16, 8 and 4 blocks, each interpolated as M G M'), shaped
4⍝# by the chunk's rules over the whole world at once, with trees planted
5⍝# by folding one tree over a list of places. Its faces are found over
6⍝# the whole array (so faces between chunks hide each other), then sent
7⍝# to scene chunk by chunk, each chunk's kinds of block under their own
8⍝# ids -- the ids a later demo patches when a block changes.
9⍝# Run: just demo scene voxels-world (drag to turn it; q or close: quit)
10
11ᵛˣ⁼u̲se< "Voxels"
12ˢᶜ⁼u̲se< "Scene"
13
14h ← ᵛˣh̲eights 64
15"heights: lowest, highest, mean"
16('m̲in r̲/ r̲avel h) c̲at ('m̲ax r̲/ r̲avel h) c̲at (f̲loor 0.5 + ('+ r̲/ f̲loat r̲avel h) ÷ 4096.0)
17b ← ᵛˣt̲errain h
18ts ← 12 2 r̲eshape 3 + r̲oll! 24 r̲eshape 58
19b ← ts ᵛˣf̲orest b
20"the world, and its blocks (air stone dirt grass sand water wood leaves):"
21s̲hape b
22ᵛˣc̲ounts b
23
24f ← ᵛˣw̲orldFaces b
25"faces by direction, all of them, and 6 per solid block if none were hidden:"
26ᵛˣc̲ount f
27t̲ally f
286 × '+ r̲/ r̲avel ᵛˣs̲olid b
29c ← ᵛˣc̲hunkOf f
30"faces per chunk, two layers of 4 by 4 (bottom, then top):"
312 4 4 r̲eshape '+ r̲/₂ (r̲ange 32) '= t̲able c
32
33w ← "Voxels 4: a world -- X_eTaL" ˢᶜo̲pen! 640 560
34k ← w ˢᶜc̲amera! 0.75 0.55 3.0 0.25
35g ← w ˢᶜf̲og! 2.6 5.0
36
37⍝# one object for each chunk and kind of block: id 10 * chunk + block
38keys ← u̲nique (10 × c) + 5 s̲elect₂ f
39ᵘp̲ut ← { i →
40 key ← i s̲elect keys
41 sel ← key = (10 × c) + 5 s̲elect₂ f
42 head ← (f̲loat w c̲at key) c̲at (1 + key m̲od 10) s̲elect ᵛˣcolors
43 q ← ᵛˣc̲orners sel r̲eplicate f
44 x ← head ˢᶜq̲uads! (q − (s̲hape q) r̲eshape 32.0 12.0 32.0) ÷ 32.0
45 i + 1
46}
47put ← (t̲ally keys) 'ᵘp̲ut p̲ower 1
48"objects sent:"
49put − 1
50
51ᵘq̲uit ← { n →
52 z ← ˢᶜc̲lose! w
53 0
54}
55ᵘf̲rame ← { n →
56 n = 0 ? 0
57 v ← ˢᶜn̲ext! w
58 ((v m̲atch "close") + v m̲atch "key q") > 0 ? ᵘq̲uit n
59 n
60}
61last ← 100000 'ᵘf̲rame p̲ower 1
62"bye"