@yaks/voxel 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +79 -2
- package/_dist/boxes.d.ts +80 -0
- package/_dist/boxes.d.ts.map +1 -0
- package/_dist/chartbook.d.ts +14 -0
- package/_dist/chartbook.d.ts.map +1 -0
- package/_dist/chartsheet.d.ts +8 -0
- package/_dist/chartsheet.d.ts.map +1 -0
- package/_dist/light.d.ts +18 -0
- package/_dist/light.d.ts.map +1 -0
- package/_dist/mesh.d.ts +120 -0
- package/_dist/mesh.d.ts.map +1 -0
- package/_dist/mod.d.ts +7 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/stream.d.ts +33 -0
- package/_dist/stream.d.ts.map +1 -0
- package/_dist/three/mod.d.ts +5 -0
- package/_dist/three/mod.d.ts.map +1 -0
- package/_dist/three/parts.d.ts +59 -0
- package/_dist/three/parts.d.ts.map +1 -0
- package/_dist/three/puppet.d.ts +93 -0
- package/_dist/three/puppet.d.ts.map +1 -0
- package/_dist/three/soft.d.ts +27 -0
- package/_dist/three/soft.d.ts.map +1 -0
- package/boxes.js +265 -0
- package/boxes.js.map +1 -0
- package/boxes.ts +266 -0
- package/chartbook.js +63 -0
- package/chartbook.js.map +1 -0
- package/chartbook.ts +88 -0
- package/chartsheet.js +31 -0
- package/chartsheet.js.map +1 -0
- package/chartsheet.ts +32 -0
- package/deno.json +1 -0
- package/light.js +45 -0
- package/light.js.map +1 -0
- package/light.ts +49 -0
- package/mesh.js +577 -0
- package/mesh.js.map +1 -0
- package/mesh.ts +624 -0
- package/mod.js +7 -0
- package/mod.js.map +1 -0
- package/mod.ts +7 -0
- package/package.json +45 -4
- package/stream.js +80 -0
- package/stream.js.map +1 -0
- package/stream.ts +86 -0
- package/three/mod.js +7 -0
- package/three/mod.js.map +1 -0
- package/three/mod.ts +7 -0
- package/three/parts.js +181 -0
- package/three/parts.js.map +1 -0
- package/three/parts.ts +185 -0
- package/three/puppet.js +311 -0
- package/three/puppet.js.map +1 -0
- package/three/puppet.ts +471 -0
- package/three/soft.js +338 -0
- package/three/soft.js.map +1 -0
- package/three/soft.ts +382 -0
package/boxes.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
// Models built of boxes: the parts a figure is made of (parts.ts), the look of
|
|
2
|
+
// a thing (items.ts), a stack of logs (nodes.ts). A model's boxes are meshed
|
|
3
|
+
// as one solid, so no two of its faces are ever drawn in one plane, which is
|
|
4
|
+
// what makes the depth buffer flicker between them (z-fighting):
|
|
5
|
+
//
|
|
6
|
+
// Each box is worn over the boxes before it. Where one of its sides lies
|
|
7
|
+
// within a step (mesh.ts `STEP`) of the same side of an earlier box it
|
|
8
|
+
// overlaps, it stands a step outside it: a belt over a tunic, plate over
|
|
9
|
+
// the tunic, a helm over hair, a band round a shield. Of the same stuff,
|
|
10
|
+
// colour and material, the two are one surface instead and meet flush.
|
|
11
|
+
//
|
|
12
|
+
// A face is drawn only where it shows: not where another box lies against
|
|
13
|
+
// it or over it, nor where a later box of the same surface lies in its
|
|
14
|
+
// plane. What is cut away leaves its edge unrounded, as a joint is.
|
|
15
|
+
//
|
|
16
|
+
// So a model is written as it is imagined, inside out, a layer at a time, and
|
|
17
|
+
// never nudged a few millimetres to keep two faces apart.
|
|
18
|
+
import {
|
|
19
|
+
axes,
|
|
20
|
+
materialOf,
|
|
21
|
+
type Out,
|
|
22
|
+
quad,
|
|
23
|
+
rgb,
|
|
24
|
+
STEP,
|
|
25
|
+
type Vec,
|
|
26
|
+
} from './mesh.js'
|
|
27
|
+
|
|
28
|
+
/** A box: its low corner and its size, in metres, its colour, which says what
|
|
29
|
+
* it is made of too (mesh.ts `metal`), and how wide the rounding of its edges
|
|
30
|
+
* is. */
|
|
31
|
+
export type Box = [Vec, Vec, number, number?]
|
|
32
|
+
|
|
33
|
+
/** A box as its two corners, worn over those before it: what `drawn` draws. */
|
|
34
|
+
export type Solid = { lo: Vec; hi: Vec; hex: number; round: number }
|
|
35
|
+
|
|
36
|
+
// A face's extent in its plane: from (u0, v0) to (u1, v1).
|
|
37
|
+
type Rect = [number, number, number, number]
|
|
38
|
+
|
|
39
|
+
// How near two numbers of metres are the same.
|
|
40
|
+
let E = 1e-6
|
|
41
|
+
|
|
42
|
+
// Where a solid's side lies along `axis`: its low face or its high one.
|
|
43
|
+
let sideOf = (s: Solid, axis: number, sign: number) =>
|
|
44
|
+
sign > 0 ? s.hi[axis] : s.lo[axis]
|
|
45
|
+
|
|
46
|
+
// Whether two solids overlap in the plane of a face on `axis`.
|
|
47
|
+
let across = (a: Solid, b: Solid, axis: number) =>
|
|
48
|
+
axes(axis).every((k) =>
|
|
49
|
+
Math.min(a.hi[k], b.hi[k]) - Math.max(a.lo[k], b.lo[k]) > E
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
/** The boxes as solids, each worn over those before it and over `under`,
|
|
53
|
+
* solids drawn apart that it rests against (parts.ts `knit`): a side within
|
|
54
|
+
* a step of the same side of one it overlaps stands a step outside it, or
|
|
55
|
+
* flush with it where the two are of the same stuff, and so one surface;
|
|
56
|
+
* never so with what is drawn apart, since nothing cuts the two apart.
|
|
57
|
+
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { assertEquals } from '@std/assert'
|
|
60
|
+
* import { STEP } from './mesh.ts'
|
|
61
|
+
* let tunic: Box = [[-0.2, 0, -0.15], [0.4, 0.5, 0.3], 0x4a7ab8]
|
|
62
|
+
* // A belt drawn round the tunic, in its planes, is worn over it.
|
|
63
|
+
* let [, belt] = worn([tunic, [[-0.2, 0.1, -0.15], [0.4, 0.06, 0.3], 0x3d2c20]])
|
|
64
|
+
* assertEquals(belt.hi[0], 0.2 + STEP)
|
|
65
|
+
* // A second piece of the tunic lies flush, a hair inside or not.
|
|
66
|
+
* let [, more] = worn([tunic, [[-0.198, 0.4, -0.1], [0.3, 0.2, 0.2], 0x4a7ab8]])
|
|
67
|
+
* assertEquals(more.lo[0], -0.2)
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export let worn = (
|
|
71
|
+
boxes: Box[],
|
|
72
|
+
round = 0.035,
|
|
73
|
+
under: Solid[] = [],
|
|
74
|
+
): Solid[] => {
|
|
75
|
+
let done: Solid[] = []
|
|
76
|
+
for (let [min, size, hex, r = round] of boxes) {
|
|
77
|
+
let s: Solid = {
|
|
78
|
+
lo: [min[0], min[1], min[2]],
|
|
79
|
+
hi: [min[0] + size[0], min[1] + size[1], min[2] + size[2]],
|
|
80
|
+
hex,
|
|
81
|
+
round: r,
|
|
82
|
+
}
|
|
83
|
+
for (let moved = true; moved;) {
|
|
84
|
+
moved = false
|
|
85
|
+
for (let t of [...under, ...done]) {
|
|
86
|
+
let one = t.hex == s.hex && !under.includes(t)
|
|
87
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
88
|
+
if (!across(t, s, axis)) continue
|
|
89
|
+
for (let sign of [-1, 1]) {
|
|
90
|
+
let p = sideOf(s, axis, sign), q = sideOf(t, axis, sign)
|
|
91
|
+
let d = sign * (p - q)
|
|
92
|
+
if (d <= E - STEP || d >= STEP - E) continue
|
|
93
|
+
let to = one && d <= E ? q : q + sign * STEP
|
|
94
|
+
if (sign * (to - p) <= E) continue
|
|
95
|
+
;(sign > 0 ? s.hi : s.lo)[axis] = to
|
|
96
|
+
moved = true
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
done.push(s)
|
|
102
|
+
}
|
|
103
|
+
return done
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Whether `s` fills the space just outside a face on `axis` at `p`, looking
|
|
107
|
+
// `sign` way: a box lying against the face, or over it.
|
|
108
|
+
let covers = (s: Solid, axis: number, sign: number, p: number) =>
|
|
109
|
+
sign > 0
|
|
110
|
+
? s.lo[axis] <= p + E && s.hi[axis] > p + E
|
|
111
|
+
: s.hi[axis] >= p - E && s.lo[axis] < p - E
|
|
112
|
+
|
|
113
|
+
// Every distinct coordinate, in order.
|
|
114
|
+
let marks = (xs: number[]) =>
|
|
115
|
+
xs.sort((a, b) => a - b).filter((x, i, all) => !i || x - all[i - 1] > E)
|
|
116
|
+
|
|
117
|
+
/** What shows of face `f` with `hidden` drawn over it: as few rectangles as
|
|
118
|
+
* cover the rest, runs along u stacked along v.
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* import { assertEquals } from '@std/assert'
|
|
122
|
+
* assertEquals(shown([0, 0, 1, 1], []), [[0, 0, 1, 1]])
|
|
123
|
+
* // A post standing in the middle of a floor leaves a frame round it.
|
|
124
|
+
* assertEquals(shown([0, 0, 3, 3], [[1, 1, 2, 2]]), [
|
|
125
|
+
* [0, 0, 3, 1],
|
|
126
|
+
* [0, 1, 1, 2],
|
|
127
|
+
* [2, 1, 3, 2],
|
|
128
|
+
* [0, 2, 3, 3],
|
|
129
|
+
* ])
|
|
130
|
+
* assertEquals(shown([0, 0, 1, 1], [[-1, -1, 2, 2]]), [])
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
export let shown = (f: Rect, hidden: Rect[]): Rect[] => {
|
|
134
|
+
let cut = hidden
|
|
135
|
+
.map(([a, b, c, d]): Rect => [
|
|
136
|
+
Math.max(a, f[0]),
|
|
137
|
+
Math.max(b, f[1]),
|
|
138
|
+
Math.min(c, f[2]),
|
|
139
|
+
Math.min(d, f[3]),
|
|
140
|
+
])
|
|
141
|
+
.filter(([a, b, c, d]) => c - a > E && d - b > E)
|
|
142
|
+
if (!cut.length) return [f]
|
|
143
|
+
let us = marks([f[0], f[2], ...cut.flatMap((r) => [r[0], r[2]])])
|
|
144
|
+
let vs = marks([f[1], f[3], ...cut.flatMap((r) => [r[1], r[3]])])
|
|
145
|
+
let open = (i: number, j: number) => {
|
|
146
|
+
let u = (us[i] + us[i + 1]) / 2, v = (vs[j] + vs[j + 1]) / 2
|
|
147
|
+
return !cut.some((r) => r[0] < u && u < r[2] && r[1] < v && v < r[3])
|
|
148
|
+
}
|
|
149
|
+
let out: Rect[] = []
|
|
150
|
+
let last = new Map<string, Rect>()
|
|
151
|
+
for (let j = 0; j + 1 < vs.length; j++) {
|
|
152
|
+
let row = new Map<string, Rect>()
|
|
153
|
+
for (let i = 0; i + 1 < us.length; i++) {
|
|
154
|
+
if (!open(i, j)) continue
|
|
155
|
+
let k = i
|
|
156
|
+
while (k + 2 < us.length && open(k + 1, j)) k++
|
|
157
|
+
let run = `${i} ${k}`
|
|
158
|
+
let r = last.get(run)
|
|
159
|
+
if (r) r[3] = vs[j + 1]
|
|
160
|
+
else out.push(r = [us[i], vs[j], us[k + 1], vs[j + 1]])
|
|
161
|
+
row.set(run, r)
|
|
162
|
+
i = k
|
|
163
|
+
}
|
|
164
|
+
last = row
|
|
165
|
+
}
|
|
166
|
+
return out
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Solids moved by `f`, a turn by quarters, a scale and a shift that takes
|
|
170
|
+
* each corner where it goes.
|
|
171
|
+
*
|
|
172
|
+
* ```ts
|
|
173
|
+
* import { assertEquals } from '@std/assert'
|
|
174
|
+
* let [s] = worn([[[0, 0, 0], [1, 2, 3], 0xffffff]])
|
|
175
|
+
* let [m] = moved([s], ([x, y, z]) => [2 - x, y + 1, z])
|
|
176
|
+
* assertEquals([m.lo, m.hi], [[1, 1, 0], [2, 3, 3]])
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
export let moved = (solids: Solid[], f: (v: Vec) => Vec): Solid[] =>
|
|
180
|
+
solids.map((s) => {
|
|
181
|
+
let [a, b] = [f(s.lo), f(s.hi)]
|
|
182
|
+
let lo: Vec = [
|
|
183
|
+
Math.min(a[0], b[0]),
|
|
184
|
+
Math.min(a[1], b[1]),
|
|
185
|
+
Math.min(a[2], b[2]),
|
|
186
|
+
]
|
|
187
|
+
let hi: Vec = [
|
|
188
|
+
Math.max(a[0], b[0]),
|
|
189
|
+
Math.max(a[1], b[1]),
|
|
190
|
+
Math.max(a[2], b[2]),
|
|
191
|
+
]
|
|
192
|
+
return { ...s, lo, hi }
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
/** Solids drawn as one, soft on every edge, as if built of voxels `cell`
|
|
196
|
+
* across, of whatever each one's colour says it is made of (mesh.ts
|
|
197
|
+
* `metal`): each face only where it shows. */
|
|
198
|
+
export let drawn = (o: Out, solids: Solid[], cell = 0.1): Out => {
|
|
199
|
+
for (let [i, s] of solids.entries()) {
|
|
200
|
+
let c = rgb(s.hex)
|
|
201
|
+
let cs = [...c, ...c, ...c, ...c]
|
|
202
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
203
|
+
let [ua, va] = axes(axis)
|
|
204
|
+
for (let sign of [-1, 1]) {
|
|
205
|
+
let p = sideOf(s, axis, sign)
|
|
206
|
+
let face: Rect = [s.lo[ua], s.lo[va], s.hi[ua], s.hi[va]]
|
|
207
|
+
let hidden = solids.flatMap((t, k): Rect[] =>
|
|
208
|
+
k != i && (covers(t, axis, sign, p) ||
|
|
209
|
+
k > i && Math.abs(sideOf(t, axis, sign) - p) <= E)
|
|
210
|
+
? [[t.lo[ua], t.lo[va], t.hi[ua], t.hi[va]]]
|
|
211
|
+
: []
|
|
212
|
+
)
|
|
213
|
+
for (let [u0, v0, u1, v1] of shown(face, hidden)) {
|
|
214
|
+
let at: Vec = [0, 0, 0]
|
|
215
|
+
at[axis] = p
|
|
216
|
+
at[ua] = u0
|
|
217
|
+
at[va] = v0
|
|
218
|
+
quad(
|
|
219
|
+
o,
|
|
220
|
+
at,
|
|
221
|
+
axis,
|
|
222
|
+
u1 - u0,
|
|
223
|
+
v1 - v0,
|
|
224
|
+
sign,
|
|
225
|
+
cs,
|
|
226
|
+
[
|
|
227
|
+
u0 - face[0] <= E ? 1 : 0,
|
|
228
|
+
face[2] - u1 <= E ? 1 : 0,
|
|
229
|
+
v0 - face[1] <= E ? 1 : 0,
|
|
230
|
+
face[3] - v1 <= E ? 1 : 0,
|
|
231
|
+
],
|
|
232
|
+
s.round,
|
|
233
|
+
undefined,
|
|
234
|
+
cell,
|
|
235
|
+
materialOf(s.hex),
|
|
236
|
+
)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return o
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Boxes as one solid, each worn over those before it, soft on every edge
|
|
246
|
+
* (`round` metres wide, or as a box says) and drawn as if built of voxels
|
|
247
|
+
* `cell` across.
|
|
248
|
+
*
|
|
249
|
+
* ```ts
|
|
250
|
+
* import { assertEquals } from '@std/assert'
|
|
251
|
+
* import { metal, METAL, out } from './mesh.ts'
|
|
252
|
+
* let faces = (o: ReturnType<typeof out>) => o.pos.length / 12
|
|
253
|
+
* let blade = cuboids(out(), [[[0, 0, 0], [1, 1, 1], metal(0xdfe6ee)]])
|
|
254
|
+
* assertEquals(faces(blade), 6)
|
|
255
|
+
* // each vertex's edge ends in what it is made of
|
|
256
|
+
* assertEquals(blade.edge[3], METAL)
|
|
257
|
+
* // Two boxes side by side: the faces between them are not drawn.
|
|
258
|
+
* let pair = cuboids(out(), [
|
|
259
|
+
* [[0, 0, 0], [1, 1, 1], 0xffffff],
|
|
260
|
+
* [[1, 0, 0], [1, 1, 1], 0xff0000],
|
|
261
|
+
* ])
|
|
262
|
+
* assertEquals(faces(pair), 10)
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
export let cuboids = (o: Out, boxes: Box[], cell = 0.1, round = 0.035): Out =>
|
|
266
|
+
drawn(o, worn(boxes, round), cell)
|
package/chartbook.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// A page's fixed-detail charts. Growth supplies cells before the map opens;
|
|
2
|
+
// missing explored cells are requested once, independently of its viewport.
|
|
3
|
+
/** Chunk cells intersecting a square, including negative coordinates. */ export let cellsOf = ([x, z, side], chunkSize)=>{
|
|
4
|
+
let cells = [];
|
|
5
|
+
for(let ck = Math.floor(z / chunkSize); ck < Math.ceil((z + side) / chunkSize); ck++){
|
|
6
|
+
for(let ci = Math.floor(x / chunkSize); ci < Math.ceil((x + side) / chunkSize); ci++){
|
|
7
|
+
cells.push([
|
|
8
|
+
ci,
|
|
9
|
+
ck
|
|
10
|
+
]);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return cells;
|
|
14
|
+
};
|
|
15
|
+
/** Keep both completed charts and concurrent requests until designs change. */ export let chartbook = (chunkSize)=>{
|
|
16
|
+
let kept = new Map();
|
|
17
|
+
let keep = ([ci, ck], make)=>{
|
|
18
|
+
let key = `${ci},${ck}`;
|
|
19
|
+
let got = kept.get(key);
|
|
20
|
+
if (!got) {
|
|
21
|
+
let hold = {
|
|
22
|
+
ready: false,
|
|
23
|
+
pending: Promise.resolve().then(make).then((image)=>{
|
|
24
|
+
hold.image = image;
|
|
25
|
+
hold.ready = true;
|
|
26
|
+
return image;
|
|
27
|
+
})
|
|
28
|
+
};
|
|
29
|
+
got = hold;
|
|
30
|
+
kept.set(key, hold);
|
|
31
|
+
hold.pending.catch(()=>{
|
|
32
|
+
if (kept.get(key) == hold) kept.delete(key);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return got.pending;
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
keep,
|
|
39
|
+
clear: ()=>kept.clear(),
|
|
40
|
+
read: async (box, visible, load)=>{
|
|
41
|
+
let cells = cellsOf(box, chunkSize).filter(visible);
|
|
42
|
+
let charts = new Array(cells.length), pending = [];
|
|
43
|
+
for (let [i, cell] of cells.entries()){
|
|
44
|
+
let hold = kept.get(`${cell[0]},${cell[1]}`);
|
|
45
|
+
let put = (image)=>{
|
|
46
|
+
charts[i] = {
|
|
47
|
+
image,
|
|
48
|
+
box: [
|
|
49
|
+
cell[0] * chunkSize,
|
|
50
|
+
cell[1] * chunkSize,
|
|
51
|
+
chunkSize
|
|
52
|
+
]
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
if (hold?.ready) put(hold.image);
|
|
56
|
+
else pending.push(keep(cell, ()=>load(cell)).then(put));
|
|
57
|
+
}
|
|
58
|
+
await Promise.all(pending);
|
|
59
|
+
return charts;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=chartbook.js.map
|
package/chartbook.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chartbook.js","sources":["./chartbook.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,4EAA4E;AAM5E,uEAAuE,GACvE,OAAO,IAAI,UAAU,CAAC,CAAC,GAAG,GAAG,KAAe,EAAE;EAC5C,IAAI,QAAgB,EAAE;EACtB,IACE,IAAI,KAAK,KAAK,KAAK,CAAC,IAAI,YACxB,KAAK,KAAK,IAAI,CAAC,CAAC,IAAI,IAAI,IAAI,YAC5B,KACA;IACA,IACE,IAAI,KAAK,KAAK,KAAK,CAAC,IAAI,YACxB,KAAK,KAAK,IAAI,CAAC,CAAC,IAAI,IAAI,IAAI,YAC5B,KACA;MACA,MAAM,IAAI,CAAC;QAAC;QAAI;OAAG;IACrB;EACF;EACA,OAAO;AACT,EAAC;AAaD,6EAA6E,GAC7E,OAAO,IAAI,YAAY,CAAI;EAEzB,IAAI,OAAO,IAAI;EACf,IAAI,OAAO,CAAC,CAAC,IAAI,GAAS,EAAE;IAC1B,IAAI,MAAM,GAAG,GAAG,CAAC,EAAE,IAAI;IACvB,IAAI,MAAM,KAAK,GAAG,CAAC;IACnB,IAAI,CAAC,KAAK;MACR,IAAI,OAAa;QACf,OAAO;QACP,SAAS,QAAQ,OAAO,GAAG,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC;UAC1C,KAAK,KAAK,GAAG;UACb,KAAK,KAAK,GAAG;UACb,OAAO;QACT;MACF;MACA,MAAM;MACN,KAAK,GAAG,CAAC,KAAK;MACd,KAAK,OAAO,CAAC,KAAK,CAAC;QACjB,IAAI,KAAK,GAAG,CAAC,QAAQ,MAAM,KAAK,MAAM,CAAC;MACzC;IACF;IACA,OAAO,IAAI,OAAO;EACpB;EACA,OAAO;IACL;IACA,OAAO,IAAM,KAAK,KAAK;IACvB,MAAM,OACJ,KACA,SACA;MAEA,IAAI,QAAQ,QAAQ,KAAK,WAAW,MAAM,CAAC;MAC3C,IAAI,SAAS,IAAI,MAAgB,MAAM,MAAM,GAC3C,UAA2B,EAAE;MAC/B,KAAK,IAAI,CAAC,GAAG,KAAK,IAAI,MAAM,OAAO,GAAI;QACrC,IAAI,OAAO,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,EAAE;QAC3C,IAAI,MAAM,CAAC;UACT,MAAM,CAAC,EAAE,GAAG;YACV;YACA,KAAK;cAAC,IAAI,CAAC,EAAE,GAAG;cAAW,IAAI,CAAC,EAAE,GAAG;cAAW;aAAU;UAC5D;QACF;QACA,IAAI,MAAM,OAAO,IAAI,KAAK,KAAK;aAC1B,QAAQ,IAAI,CAAC,KAAK,MAAM,IAAM,KAAK,OAAO,IAAI,CAAC;MACtD;MACA,MAAM,QAAQ,GAAG,CAAC;MAClB,OAAO;IACT;EACF;AACF,EAAC"}
|
package/chartbook.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// A page's fixed-detail charts. Growth supplies cells before the map opens;
|
|
2
|
+
// missing explored cells are requested once, independently of its viewport.
|
|
3
|
+
export type Cell = [number, number]
|
|
4
|
+
export type ChartBox = [number, number, number]
|
|
5
|
+
|
|
6
|
+
export type Chart<T> = { image: T; box: ChartBox }
|
|
7
|
+
|
|
8
|
+
/** Chunk cells intersecting a square, including negative coordinates. */
|
|
9
|
+
export let cellsOf = ([x, z, side]: ChartBox, chunkSize: number): Cell[] => {
|
|
10
|
+
let cells: Cell[] = []
|
|
11
|
+
for (
|
|
12
|
+
let ck = Math.floor(z / chunkSize);
|
|
13
|
+
ck < Math.ceil((z + side) / chunkSize);
|
|
14
|
+
ck++
|
|
15
|
+
) {
|
|
16
|
+
for (
|
|
17
|
+
let ci = Math.floor(x / chunkSize);
|
|
18
|
+
ci < Math.ceil((x + side) / chunkSize);
|
|
19
|
+
ci++
|
|
20
|
+
) {
|
|
21
|
+
cells.push([ci, ck])
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return cells
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A cache of completed and concurrent chunk charts. */
|
|
28
|
+
export type Chartbook<T> = {
|
|
29
|
+
keep: (cell: Cell, make: () => T | Promise<T>) => Promise<T>
|
|
30
|
+
clear: () => void
|
|
31
|
+
read: (
|
|
32
|
+
box: ChartBox,
|
|
33
|
+
visible: (cell: Cell) => boolean,
|
|
34
|
+
load: (cell: Cell) => Promise<T>,
|
|
35
|
+
) => Promise<Chart<T>[]>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Keep both completed charts and concurrent requests until designs change. */
|
|
39
|
+
export let chartbook = <T>(chunkSize: number): Chartbook<T> => {
|
|
40
|
+
type Held = { ready: boolean; image?: T; pending: Promise<T> }
|
|
41
|
+
let kept = new Map<string, Held>()
|
|
42
|
+
let keep = ([ci, ck]: Cell, make: () => T | Promise<T>) => {
|
|
43
|
+
let key = `${ci},${ck}`
|
|
44
|
+
let got = kept.get(key)
|
|
45
|
+
if (!got) {
|
|
46
|
+
let hold: Held = {
|
|
47
|
+
ready: false,
|
|
48
|
+
pending: Promise.resolve().then(make).then((image) => {
|
|
49
|
+
hold.image = image
|
|
50
|
+
hold.ready = true
|
|
51
|
+
return image
|
|
52
|
+
}),
|
|
53
|
+
}
|
|
54
|
+
got = hold
|
|
55
|
+
kept.set(key, hold)
|
|
56
|
+
hold.pending.catch(() => {
|
|
57
|
+
if (kept.get(key) == hold) kept.delete(key)
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
return got.pending
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
keep,
|
|
64
|
+
clear: () => kept.clear(),
|
|
65
|
+
read: async (
|
|
66
|
+
box: ChartBox,
|
|
67
|
+
visible: (cell: Cell) => boolean,
|
|
68
|
+
load: (cell: Cell) => Promise<T>,
|
|
69
|
+
): Promise<Chart<T>[]> => {
|
|
70
|
+
let cells = cellsOf(box, chunkSize).filter(visible)
|
|
71
|
+
let charts = new Array<Chart<T>>(cells.length),
|
|
72
|
+
pending: Promise<void>[] = []
|
|
73
|
+
for (let [i, cell] of cells.entries()) {
|
|
74
|
+
let hold = kept.get(`${cell[0]},${cell[1]}`)
|
|
75
|
+
let put = (image: T) => {
|
|
76
|
+
charts[i] = {
|
|
77
|
+
image,
|
|
78
|
+
box: [cell[0] * chunkSize, cell[1] * chunkSize, chunkSize],
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (hold?.ready) put(hold.image!)
|
|
82
|
+
else pending.push(keep(cell, () => load(cell)).then(put))
|
|
83
|
+
}
|
|
84
|
+
await Promise.all(pending)
|
|
85
|
+
return charts
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
}
|
package/chartsheet.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Place each chunk's pixels into a larger canvas once, so a view draws only
|
|
2
|
+
// a handful of sheets. Sheets keep the same detail at every scale.
|
|
3
|
+
export let chartsheet = (size, make)=>{
|
|
4
|
+
let kept = new Map();
|
|
5
|
+
let placed = new Map();
|
|
6
|
+
return (charts)=>{
|
|
7
|
+
let visible = new Map();
|
|
8
|
+
for (let { image: px, box: [x, z] } of charts){
|
|
9
|
+
let gx = Math.floor(x / size), gz = Math.floor(z / size);
|
|
10
|
+
let key = `${gx},${gz}`, cell = `${x},${z}`;
|
|
11
|
+
let canvas = kept.get(key);
|
|
12
|
+
if (!canvas) kept.set(key, canvas = make());
|
|
13
|
+
if (placed.get(cell) != px) {
|
|
14
|
+
canvas.put(px, x - gx * size, z - gz * size);
|
|
15
|
+
placed.set(cell, px);
|
|
16
|
+
}
|
|
17
|
+
visible.set(key, {
|
|
18
|
+
image: canvas.image,
|
|
19
|
+
box: [
|
|
20
|
+
gx * size,
|
|
21
|
+
gz * size,
|
|
22
|
+
size
|
|
23
|
+
]
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return [
|
|
27
|
+
...visible.values()
|
|
28
|
+
];
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=chartsheet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chartsheet.js","sources":["./chartsheet.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,mEAAmE;AAMnE,OAAO,IAAI,aAAa,CACtB,MACA;EAEA,IAAI,OAAO,IAAI;EACf,IAAI,SAAS,IAAI;EACjB,OAAO,CAAC;IACN,IAAI,UAAU,IAAI;IAClB,KAAK,IAAI,EAAE,OAAO,EAAE,EAAE,KAAK,CAAC,GAAG,EAAE,EAAE,IAAI,OAAQ;MAC7C,IAAI,KAAK,KAAK,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,KAAK,CAAC,IAAI;MACnD,IAAI,MAAM,GAAG,GAAG,CAAC,EAAE,IAAI,EAAE,OAAO,GAAG,EAAE,CAAC,EAAE,GAAG;MAC3C,IAAI,SAAS,KAAK,GAAG,CAAC;MACtB,IAAI,CAAC,QAAQ,KAAK,GAAG,CAAC,KAAK,SAAS;MACpC,IAAI,OAAO,GAAG,CAAC,SAAS,IAAI;QAC1B,OAAO,GAAG,CAAC,IAAI,IAAI,KAAK,MAAM,IAAI,KAAK;QACvC,OAAO,GAAG,CAAC,MAAM;MACnB;MACA,QAAQ,GAAG,CAAC,KAAK;QACf,OAAO,OAAO,KAAK;QACnB,KAAK;UAAC,KAAK;UAAM,KAAK;UAAM;SAAK;MACnC;IACF;IACA,OAAO;SAAI,QAAQ,MAAM;KAAG;EAC9B;AACF,EAAC"}
|
package/chartsheet.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Place each chunk's pixels into a larger canvas once, so a view draws only
|
|
2
|
+
// a handful of sheets. Sheets keep the same detail at every scale.
|
|
3
|
+
import type { Chart } from './chartbook.ts'
|
|
4
|
+
|
|
5
|
+
type Pixels = Uint8ClampedArray<ArrayBuffer>
|
|
6
|
+
type Canvas<T> = { image: T; put: (px: Pixels, x: number, z: number) => void }
|
|
7
|
+
|
|
8
|
+
export let chartsheet = <T>(
|
|
9
|
+
size: number,
|
|
10
|
+
make: () => Canvas<T>,
|
|
11
|
+
): (charts: Chart<Pixels>[]) => Chart<T>[] => {
|
|
12
|
+
let kept = new Map<string, Canvas<T>>()
|
|
13
|
+
let placed = new Map<string, Pixels>()
|
|
14
|
+
return (charts: Chart<Pixels>[]): Chart<T>[] => {
|
|
15
|
+
let visible = new Map<string, Chart<T>>()
|
|
16
|
+
for (let { image: px, box: [x, z] } of charts) {
|
|
17
|
+
let gx = Math.floor(x / size), gz = Math.floor(z / size)
|
|
18
|
+
let key = `${gx},${gz}`, cell = `${x},${z}`
|
|
19
|
+
let canvas = kept.get(key)
|
|
20
|
+
if (!canvas) kept.set(key, canvas = make())
|
|
21
|
+
if (placed.get(cell) != px) {
|
|
22
|
+
canvas.put(px, x - gx * size, z - gz * size)
|
|
23
|
+
placed.set(cell, px)
|
|
24
|
+
}
|
|
25
|
+
visible.set(key, {
|
|
26
|
+
image: canvas.image,
|
|
27
|
+
box: [gx * size, gz * size, size],
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
return [...visible.values()]
|
|
31
|
+
}
|
|
32
|
+
}
|
package/deno.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/voxel","version":"0.2.36","license":"Apache-2.0","description":"Worker-safe voxel geometry, baked lamp light, chunk detail and heightfield charts, with Three.js materials and box rigs at /three.","exports":{".":"./mod.ts","./mesh":"./mesh.ts","./boxes":"./boxes.ts","./light":"./light.ts","./stream":"./stream.ts","./chartbook":"./chartbook.ts","./chartsheet":"./chartsheet.ts","./three":"./three/mod.ts"},"imports":{"@yaks/voxel":"./mod.ts","@yaks/voxel/mesh":"./mesh.ts","@yaks/voxel/boxes":"./boxes.ts","@yaks/voxel/light":"./light.ts","@yaks/voxel/stream":"./stream.ts","@yaks/voxel/chartbook":"./chartbook.ts","@yaks/voxel/chartsheet":"./chartsheet.ts","@yaks/voxel/three":"./three/mod.ts"}}
|
package/light.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// A building's lamps and fire light its own mesh. The light at each vertex is
|
|
2
|
+
// baked once per shape and turn into the unused high bits of its edge flags;
|
|
3
|
+
// three/soft.ts brings it up at dusk. Its facing keeps light inside the walls
|
|
4
|
+
// and under the ceiling, while the distance makes each source brightest nearby.
|
|
5
|
+
// Plain arrays in and out, so a worker meshing a building bakes it too.
|
|
6
|
+
/** How much of the nearby lamps a face sees, before the day's light changes. */ export let warmth = (at, normal, glows)=>{
|
|
7
|
+
let light = 0;
|
|
8
|
+
for (let g of glows){
|
|
9
|
+
let dx = g.at[0] - at[0], dy = g.at[1] - at[1], dz = g.at[2] - at[2];
|
|
10
|
+
let d = Math.hypot(dx, dy, dz);
|
|
11
|
+
let reach = 1.5 + g.size * 1.5;
|
|
12
|
+
if (d >= reach) continue;
|
|
13
|
+
let face = Math.max(0, (dx * normal[0] + dy * normal[1] + dz * normal[2]) / Math.max(d, 0.01));
|
|
14
|
+
light += (1 - d / reach) ** 2 * face * 2.6;
|
|
15
|
+
}
|
|
16
|
+
return Math.min(1, light);
|
|
17
|
+
};
|
|
18
|
+
/** A lamp lights the face toward it, strongest near its flame.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { assert, assertEquals } from '@std/assert'
|
|
22
|
+
* assert(warmth([0, 0, 0], [1, 0, 0], [{ at: [1, 0, 0], size: 2 }]) >
|
|
23
|
+
* warmth([-2, 0, 0], [1, 0, 0], [{ at: [1, 0, 0], size: 2 }]))
|
|
24
|
+
* assertEquals(warmth([0, 0, 0], [-1, 0, 0], [
|
|
25
|
+
* { at: [1, 0, 0], size: 2 },
|
|
26
|
+
* ]), 0)
|
|
27
|
+
* ```
|
|
28
|
+
*/ /** Store a building's warm light in each vertex's edge-flag high nibble. */ export let light = (mesh, glows)=>{
|
|
29
|
+
for(let i = 0; i < mesh.pos.length / 3; i++){
|
|
30
|
+
let p = i * 3, n = i * 4;
|
|
31
|
+
let at = [
|
|
32
|
+
mesh.pos[p],
|
|
33
|
+
mesh.pos[p + 1],
|
|
34
|
+
mesh.pos[p + 2]
|
|
35
|
+
];
|
|
36
|
+
let face = [
|
|
37
|
+
mesh.nrm[n],
|
|
38
|
+
mesh.nrm[n + 1],
|
|
39
|
+
mesh.nrm[n + 2]
|
|
40
|
+
];
|
|
41
|
+
mesh.edge[n + 2] |= Math.round(warmth(at, face, glows) * 15) << 4;
|
|
42
|
+
}
|
|
43
|
+
return mesh;
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=light.js.map
|
package/light.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"light.js","sources":["./light.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,6EAA6E;AAC7E,8EAA8E;AAC9E,gFAAgF;AAChF,wEAAwE;AAKxE,8EAA8E,GAC9E,OAAO,IAAI,SAAS,CAAC,IAAS,QAAa;EACzC,IAAI,QAAQ;EACZ,KAAK,IAAI,KAAK,MAAO;IACnB,IAAI,KAAK,EAAE,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,KAAK,EAAE,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,KAAK,EAAE,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE;IACpE,IAAI,IAAI,KAAK,KAAK,CAAC,IAAI,IAAI;IAC3B,IAAI,QAAQ,MAAM,EAAE,IAAI,GAAG;IAC3B,IAAI,KAAK,OAAO;IAChB,IAAI,OAAO,KAAK,GAAG,CACjB,GACA,CAAC,KAAK,MAAM,CAAC,EAAE,GAAG,KAAK,MAAM,CAAC,EAAE,GAC9B,KAAK,MAAM,CAAC,EAAE,IAAI,KAAK,GAAG,CAAC,GAAG;IAElC,SAAS,CAAC,IAAI,IAAI,KAAK,KAAK,IAAI,OAAO;EACzC;EACA,OAAO,KAAK,GAAG,CAAC,GAAG;AACrB,EAAC;AAED;;;;;;;;;;CAUC,GAED,0EAA0E,GAC1E,OAAO,IAAI,QAAQ,CAAC,MAAW;EAC7B,IAAK,IAAI,IAAI,GAAG,IAAI,KAAK,GAAG,CAAC,MAAM,GAAG,GAAG,IAAK;IAC5C,IAAI,IAAI,IAAI,GAAG,IAAI,IAAI;IACvB,IAAI,KAAU;MAAC,KAAK,GAAG,CAAC,EAAE;MAAE,KAAK,GAAG,CAAC,IAAI,EAAE;MAAE,KAAK,GAAG,CAAC,IAAI,EAAE;KAAC;IAC7D,IAAI,OAAY;MAAC,KAAK,GAAG,CAAC,EAAE;MAAE,KAAK,GAAG,CAAC,IAAI,EAAE;MAAE,KAAK,GAAG,CAAC,IAAI,EAAE;KAAC;IAC/D,KAAK,IAAI,CAAC,IAAI,EAAE,IAAI,KAAK,KAAK,CAAC,OAAO,IAAI,MAAM,SAAS,OAAO;EAClE;EACA,OAAO;AACT,EAAC"}
|
package/light.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// A building's lamps and fire light its own mesh. The light at each vertex is
|
|
2
|
+
// baked once per shape and turn into the unused high bits of its edge flags;
|
|
3
|
+
// three/soft.ts brings it up at dusk. Its facing keeps light inside the walls
|
|
4
|
+
// and under the ceiling, while the distance makes each source brightest nearby.
|
|
5
|
+
// Plain arrays in and out, so a worker meshing a building bakes it too.
|
|
6
|
+
import { type Out, type Vec } from './mesh.js'
|
|
7
|
+
|
|
8
|
+
export type LightSource = { at: Vec; size: number }
|
|
9
|
+
|
|
10
|
+
/** How much of the nearby lamps a face sees, before the day's light changes. */
|
|
11
|
+
export let warmth = (at: Vec, normal: Vec, glows: LightSource[]): number => {
|
|
12
|
+
let light = 0
|
|
13
|
+
for (let g of glows) {
|
|
14
|
+
let dx = g.at[0] - at[0], dy = g.at[1] - at[1], dz = g.at[2] - at[2]
|
|
15
|
+
let d = Math.hypot(dx, dy, dz)
|
|
16
|
+
let reach = 1.5 + g.size * 1.5
|
|
17
|
+
if (d >= reach) continue
|
|
18
|
+
let face = Math.max(
|
|
19
|
+
0,
|
|
20
|
+
(dx * normal[0] + dy * normal[1] +
|
|
21
|
+
dz * normal[2]) / Math.max(d, 0.01),
|
|
22
|
+
)
|
|
23
|
+
light += (1 - d / reach) ** 2 * face * 2.6
|
|
24
|
+
}
|
|
25
|
+
return Math.min(1, light)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A lamp lights the face toward it, strongest near its flame.
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { assert, assertEquals } from '@std/assert'
|
|
32
|
+
* assert(warmth([0, 0, 0], [1, 0, 0], [{ at: [1, 0, 0], size: 2 }]) >
|
|
33
|
+
* warmth([-2, 0, 0], [1, 0, 0], [{ at: [1, 0, 0], size: 2 }]))
|
|
34
|
+
* assertEquals(warmth([0, 0, 0], [-1, 0, 0], [
|
|
35
|
+
* { at: [1, 0, 0], size: 2 },
|
|
36
|
+
* ]), 0)
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/** Store a building's warm light in each vertex's edge-flag high nibble. */
|
|
41
|
+
export let light = (mesh: Out, glows: LightSource[]): Out => {
|
|
42
|
+
for (let i = 0; i < mesh.pos.length / 3; i++) {
|
|
43
|
+
let p = i * 3, n = i * 4
|
|
44
|
+
let at: Vec = [mesh.pos[p], mesh.pos[p + 1], mesh.pos[p + 2]]
|
|
45
|
+
let face: Vec = [mesh.nrm[n], mesh.nrm[n + 1], mesh.nrm[n + 2]]
|
|
46
|
+
mesh.edge[n + 2] |= Math.round(warmth(at, face, glows) * 15) << 4
|
|
47
|
+
}
|
|
48
|
+
return mesh
|
|
49
|
+
}
|