@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/mesh.ts
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
// Voxels into triangles. Every face in the vale, the ground's and a boar's
|
|
2
|
+
// alike, is one quad written by `quad`, carrying what the soft material
|
|
3
|
+
// (soft.ts) needs to round it: where on the face each corner sits and which of
|
|
4
|
+
// its four edges are convex (`edge`), and how wide the rounding is across the
|
|
5
|
+
// face and how big its voxels are (`bw`). A face's two in-plane axes follow
|
|
6
|
+
// one convention (`axes`), shared with the shader, so a rim flag names the
|
|
7
|
+
// same edge on both sides.
|
|
8
|
+
//
|
|
9
|
+
// A vertex is written as narrow as the GPU will take it, 36 bytes, since a
|
|
10
|
+
// level is millions of them and a phone holds them all: every attribute is 4
|
|
11
|
+
// bytes a vertex or a multiple, the one stride Metal (Safari's WebGL) takes
|
|
12
|
+
// without converting. What a face is made of, matte or metal, rides in the
|
|
13
|
+
// last byte of its `edge`, so saying it costs no byte more (`METAL`).
|
|
14
|
+
//
|
|
15
|
+
// `blob` meshes a small voxel model: a tree, a rock, a house. Faces alike in
|
|
16
|
+
// colour, rounding and shade merge into larger quads; the shader still tints
|
|
17
|
+
// each voxel of a merged face on its own, so the merge cannot be seen. The
|
|
18
|
+
// ground has its own mesher (ground.ts) because it is a heightfield, and a
|
|
19
|
+
// model built of boxes its own (boxes.ts).
|
|
20
|
+
//
|
|
21
|
+
// No mesher writes two faces the depth buffer cannot tell apart (`fights`):
|
|
22
|
+
// none in one plane, and none facing the same way closer than a step
|
|
23
|
+
// (`STEP`).
|
|
24
|
+
|
|
25
|
+
export type Vec = [number, number, number]
|
|
26
|
+
|
|
27
|
+
/** The triangles being written, a vertex at a time: where it is (3 numbers,
|
|
28
|
+
* metres); which way its face looks (4: the normal and a 0); its colour (4:
|
|
29
|
+
* sRGB bytes and 255); where on its face it sits, which of the face's edges
|
|
30
|
+
* are rounded and what it is made of (4: u and v, 0 or 1, the edge flags as
|
|
31
|
+
* bits, and its material); and the rounding's widths across the face and the
|
|
32
|
+
* voxel edge (3). */
|
|
33
|
+
export type Out = {
|
|
34
|
+
pos: number[]
|
|
35
|
+
nrm: number[]
|
|
36
|
+
col: number[]
|
|
37
|
+
edge: number[]
|
|
38
|
+
bw: number[]
|
|
39
|
+
idx: number[]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export let out = (): Out => ({
|
|
43
|
+
pos: [],
|
|
44
|
+
nrm: [],
|
|
45
|
+
col: [],
|
|
46
|
+
edge: [],
|
|
47
|
+
bw: [],
|
|
48
|
+
idx: [],
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
// What a vertex carries besides where it is: copied as it stands.
|
|
52
|
+
let COPIED: ('nrm' | 'col' | 'edge' | 'bw')[] = ['nrm', 'col', 'edge', 'bw']
|
|
53
|
+
|
|
54
|
+
/** Copy triangles into `into`, moved by `at`. */
|
|
55
|
+
export let place = (into: Out, from: Out, at: Vec): void => {
|
|
56
|
+
let base = into.pos.length / 3
|
|
57
|
+
for (let i = 0; i < from.pos.length; i += 3) {
|
|
58
|
+
into.pos.push(
|
|
59
|
+
from.pos[i] + at[0],
|
|
60
|
+
from.pos[i + 1] + at[1],
|
|
61
|
+
from.pos[i + 2] + at[2],
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
for (let name of COPIED) {
|
|
65
|
+
let src = from[name], dst = into[name]
|
|
66
|
+
for (let i = 0; i < src.length; i++) dst.push(src[i])
|
|
67
|
+
}
|
|
68
|
+
for (let i of from.idx) into.idx.push(i + base)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The compact bounds and face planes of a mesh, before it is placed. */
|
|
72
|
+
export type Profile = {
|
|
73
|
+
room: [Vec, Vec]
|
|
74
|
+
planes: [number[], number[], number[]]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export let profileOf = (o: Out): Profile => {
|
|
78
|
+
let lo: Vec = [Infinity, Infinity, Infinity]
|
|
79
|
+
let hi: Vec = [-Infinity, -Infinity, -Infinity]
|
|
80
|
+
let planes: [Set<number>, Set<number>, Set<number>] = [
|
|
81
|
+
new Set(),
|
|
82
|
+
new Set(),
|
|
83
|
+
new Set(),
|
|
84
|
+
]
|
|
85
|
+
for (let i = 0; i < o.pos.length; i++) {
|
|
86
|
+
let k = i % 3, m = o.pos[i]
|
|
87
|
+
lo[k] = Math.min(lo[k], m)
|
|
88
|
+
hi[k] = Math.max(hi[k], m)
|
|
89
|
+
}
|
|
90
|
+
for (let q = 0; q < o.pos.length; q += 12) {
|
|
91
|
+
for (let k = 0; k < 3; k++) {
|
|
92
|
+
let m = o.pos[q + k]
|
|
93
|
+
if (
|
|
94
|
+
Math.abs(o.pos[q + 3 + k] - m) < 1e-6 &&
|
|
95
|
+
Math.abs(o.pos[q + 6 + k] - m) < 1e-6 &&
|
|
96
|
+
Math.abs(o.pos[q + 9 + k] - m) < 1e-6
|
|
97
|
+
) {
|
|
98
|
+
planes[k].add(m)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
room: [lo, hi],
|
|
104
|
+
planes: [[...planes[0]], [...planes[1]], [...planes[2]]],
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** What a mesher wrote, in the typed arrays a GPU takes: what a worker hands
|
|
109
|
+
* the page without a copy (grow.ts). */
|
|
110
|
+
export type Packed = {
|
|
111
|
+
pos: Float32Array<ArrayBuffer>
|
|
112
|
+
nrm: Int8Array<ArrayBuffer>
|
|
113
|
+
col: Uint8Array<ArrayBuffer>
|
|
114
|
+
edge: Uint8Array<ArrayBuffer>
|
|
115
|
+
bw: Float32Array<ArrayBuffer>
|
|
116
|
+
idx: Uint16Array<ArrayBuffer> | Uint32Array<ArrayBuffer>
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Triangles packed, with indices as narrow as the vertex count allows. A
|
|
120
|
+
* colour written as sRGB hex comes back as the same bytes.
|
|
121
|
+
*
|
|
122
|
+
* ```ts
|
|
123
|
+
* import { assertEquals } from '@std/assert'
|
|
124
|
+
* import { cuboids } from './boxes.ts'
|
|
125
|
+
* let p = pack(cuboids(out(), [[[0, 0, 0], [1, 1, 1], 0x80c020]]))
|
|
126
|
+
* assertEquals([p.pos.length / 3, p.idx.length / 3], [24, 12])
|
|
127
|
+
* assertEquals(p.idx instanceof Uint16Array, true)
|
|
128
|
+
* assertEquals([...p.col.slice(0, 4)], [0x80, 0xc0, 0x20, 255])
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export let pack = (o: Out): Packed => ({
|
|
132
|
+
pos: new Float32Array(o.pos),
|
|
133
|
+
nrm: new Int8Array(o.nrm),
|
|
134
|
+
col: new Uint8Array(o.col),
|
|
135
|
+
edge: new Uint8Array(o.edge),
|
|
136
|
+
bw: new Float32Array(o.bw),
|
|
137
|
+
idx: o.pos.length / 3 > 65535
|
|
138
|
+
? new Uint32Array(o.idx)
|
|
139
|
+
: new Uint16Array(o.idx),
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
/** The buffers a packed mesh is made of, to hand over rather than copy. */
|
|
143
|
+
export let buffers = (p: Packed): ArrayBuffer[] =>
|
|
144
|
+
[p.pos, p.nrm, p.col, p.edge, p.bw, p.idx].map((a) => a.buffer)
|
|
145
|
+
|
|
146
|
+
/** The least distance two surfaces facing the same way keep apart, in metres,
|
|
147
|
+
* so the depth buffer always tells which is in front: a worn layer over what
|
|
148
|
+
* it covers (boxes.ts), a thing standing on the ground or in another
|
|
149
|
+
* (chunks.ts). A 24-bit depth buffer parts two surfaces d metres off when
|
|
150
|
+
* they are about d² / (near · 2²⁴) apart, near being the camera's near plane
|
|
151
|
+
* (cam.ts `NEAR`): a millimetre at 100 m, so a step holds out to the fog. */
|
|
152
|
+
export let STEP = 0.005
|
|
153
|
+
|
|
154
|
+
// A face as `fights` measures it: the axis it looks along and which way, the
|
|
155
|
+
// plane it lies in, and its extent in that plane.
|
|
156
|
+
type Flat = {
|
|
157
|
+
axis: number
|
|
158
|
+
sign: number
|
|
159
|
+
at: number
|
|
160
|
+
u0: number
|
|
161
|
+
v0: number
|
|
162
|
+
u1: number
|
|
163
|
+
v1: number
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Every face of a packed mesh: each quad `quad` wrote, four vertices and two
|
|
167
|
+
// triangles, looking the way its first triangle winds.
|
|
168
|
+
let facesOf = ({ pos, idx }: Packed): Flat[] => {
|
|
169
|
+
let faces: Flat[] = []
|
|
170
|
+
for (let q = 0; q < idx.length; q += 6) {
|
|
171
|
+
let a = idx[q] * 3, b = idx[q + 1] * 3, c = idx[q + 2] * 3
|
|
172
|
+
let ex = pos[b] - pos[a], ey = pos[b + 1] - pos[a + 1]
|
|
173
|
+
let ez = pos[b + 2] - pos[a + 2]
|
|
174
|
+
let fx = pos[c] - pos[a], fy = pos[c + 1] - pos[a + 1]
|
|
175
|
+
let fz = pos[c + 2] - pos[a + 2]
|
|
176
|
+
let n = [ey * fz - ez * fy, ez * fx - ex * fz, ex * fy - ey * fx]
|
|
177
|
+
let axis = 0
|
|
178
|
+
if (Math.abs(n[1]) > Math.abs(n[axis])) axis = 1
|
|
179
|
+
if (Math.abs(n[2]) > Math.abs(n[axis])) axis = 2
|
|
180
|
+
let [ua, va] = axes(axis)
|
|
181
|
+
let base = idx[q]
|
|
182
|
+
for (let i = 1; i < 6; i++) base = Math.min(base, idx[q + i])
|
|
183
|
+
let u0 = Infinity, v0 = Infinity, u1 = -Infinity, v1 = -Infinity
|
|
184
|
+
for (let i = base * 3; i < base * 3 + 12; i += 3) {
|
|
185
|
+
u0 = Math.min(u0, pos[i + ua]), u1 = Math.max(u1, pos[i + ua])
|
|
186
|
+
v0 = Math.min(v0, pos[i + va]), v1 = Math.max(v1, pos[i + va])
|
|
187
|
+
}
|
|
188
|
+
faces.push({
|
|
189
|
+
axis,
|
|
190
|
+
sign: Math.sign(n[axis]),
|
|
191
|
+
at: pos[base * 3 + axis],
|
|
192
|
+
u0,
|
|
193
|
+
v0,
|
|
194
|
+
u1,
|
|
195
|
+
v1,
|
|
196
|
+
})
|
|
197
|
+
}
|
|
198
|
+
return faces
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// How near, in metres, is as good as touching, for a packed mesh's 32-bit
|
|
202
|
+
// numbers.
|
|
203
|
+
let TOUCH = 1e-5
|
|
204
|
+
|
|
205
|
+
/** Every pair of faces in `p` the depth buffer could not tell apart, by their
|
|
206
|
+
* order in it: where they overlap, two looking the same way less than a step
|
|
207
|
+
* apart, or two back to back in one plane, where two solids meet and neither
|
|
208
|
+
* shows. Nothing a mesher writes ever does (boxes.ts, `blob`, ground.ts).
|
|
209
|
+
*
|
|
210
|
+
* ```ts
|
|
211
|
+
* import { assertEquals } from '@std/assert'
|
|
212
|
+
* let white = Array(12).fill(1), plain: [0, 0, 0, 0] = [0, 0, 0, 0]
|
|
213
|
+
* let floor = (y: number, sign = 1, x = 0) => {
|
|
214
|
+
* let o = out()
|
|
215
|
+
* quad(o, [x, y, 0], 1, 1, 1, sign, white, plain, 0)
|
|
216
|
+
* return o
|
|
217
|
+
* }
|
|
218
|
+
* let both = (a: Out, b: Out) => {
|
|
219
|
+
* let o = out()
|
|
220
|
+
* place(o, a, [0, 0, 0])
|
|
221
|
+
* place(o, b, [0, 0, 0])
|
|
222
|
+
* return fights(pack(o))
|
|
223
|
+
* }
|
|
224
|
+
* assertEquals(both(floor(0), floor(0.002)), [[0, 1]])
|
|
225
|
+
* assertEquals(both(floor(0), floor(STEP)), [])
|
|
226
|
+
* assertEquals(both(floor(0), floor(0, -1)), [[0, 1]])
|
|
227
|
+
* assertEquals(both(floor(0), floor(0, 1, 1)), []) // side by side
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
export let fights = (p: Packed): [number, number][] => {
|
|
231
|
+
let faces = facesOf(p)
|
|
232
|
+
// Faces by axis and slab of planes a step deep: a pair closer than a step
|
|
233
|
+
// lies in one slab or two neighbouring.
|
|
234
|
+
let slabs = new Map<number, number[]>()
|
|
235
|
+
for (let [i, f] of faces.entries()) {
|
|
236
|
+
let k = Math.floor(f.at / STEP) * 3 + f.axis
|
|
237
|
+
let slab = slabs.get(k)
|
|
238
|
+
if (slab) slab.push(i)
|
|
239
|
+
else slabs.set(k, [i])
|
|
240
|
+
}
|
|
241
|
+
let got: [number, number][] = []
|
|
242
|
+
for (let [k, own] of slabs) {
|
|
243
|
+
let next = slabs.get(k + 3) ?? []
|
|
244
|
+
let near = [...own, ...next].sort((a, b) => faces[a].u0 - faces[b].u0)
|
|
245
|
+
let mine = new Set(own)
|
|
246
|
+
for (let x = 0; x < near.length; x++) {
|
|
247
|
+
let a = faces[near[x]]
|
|
248
|
+
for (let y = x + 1; y < near.length; y++) {
|
|
249
|
+
let b = faces[near[y]]
|
|
250
|
+
if (b.u0 > a.u1 - TOUCH) break
|
|
251
|
+
if (!mine.has(near[x]) && !mine.has(near[y])) continue
|
|
252
|
+
if (Math.min(a.u1, b.u1) - Math.max(a.u0, b.u0) < TOUCH) continue
|
|
253
|
+
if (Math.min(a.v1, b.v1) - Math.max(a.v0, b.v0) < TOUCH) continue
|
|
254
|
+
let d = Math.abs(a.at - b.at)
|
|
255
|
+
if (a.sign == b.sign ? d < STEP - TOUCH : d < TOUCH) {
|
|
256
|
+
got.push([Math.min(near[x], near[y]), Math.max(near[x], near[y])])
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return got.sort((a, b) => a[0] - b[0] || a[1] - b[1])
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** What a face is made of, besides its colour: matte, the soft look of
|
|
265
|
+
* everything in the vale, or metal, which mirrors the sky and catches the sun
|
|
266
|
+
* (soft.ts). A colour carries its material in the byte above its 24 bits of
|
|
267
|
+
* sRGB, so a model says what each box or voxel is made of where it says its
|
|
268
|
+
* colour; the byte has room for more materials than these. */
|
|
269
|
+
export let MATTE = 0
|
|
270
|
+
export let METAL = 1
|
|
271
|
+
|
|
272
|
+
/** A colour, in metal.
|
|
273
|
+
*
|
|
274
|
+
* ```ts
|
|
275
|
+
* import { assertEquals } from '@std/assert'
|
|
276
|
+
* assertEquals([materialOf(metal(0x9aa2aa)), materialOf(0x9aa2aa)], [
|
|
277
|
+
* METAL,
|
|
278
|
+
* MATTE,
|
|
279
|
+
* ])
|
|
280
|
+
* // the colour itself is the same
|
|
281
|
+
* assertEquals(rgb(metal(0x9aa2aa)), rgb(0x9aa2aa))
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
export let metal = (hex: number) => hex % 0x1000000 + METAL * 0x1000000
|
|
285
|
+
|
|
286
|
+
/** What a colour is made of: `MATTE` or `METAL`. */
|
|
287
|
+
export let materialOf = (hex: number): number => Math.floor(hex / 0x1000000)
|
|
288
|
+
|
|
289
|
+
/** A face's two in-plane axes, by the axis its normal lies on: x → (z, y),
|
|
290
|
+
* y → (x, z), z → (x, y). soft.ts derives the same pair from the normal. */
|
|
291
|
+
export let axes = (axis: number): [number, number] =>
|
|
292
|
+
axis == 0 ? [2, 1] : axis == 1 ? [0, 2] : [0, 1]
|
|
293
|
+
|
|
294
|
+
let unit = (a: number, s = 1): Vec => {
|
|
295
|
+
let v: Vec = [0, 0, 0]
|
|
296
|
+
v[a] = s
|
|
297
|
+
return v
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// How much light a corner keeps, by how many of its three neighbours are open.
|
|
301
|
+
let AO = [0.5, 0.68, 0.84, 1]
|
|
302
|
+
|
|
303
|
+
// The sign of (u × v) along the normal's axis: which way round is outward.
|
|
304
|
+
let WINDING = [0, 1, 2].map((axis) => {
|
|
305
|
+
let [ua, va] = axes(axis)
|
|
306
|
+
let u = unit(ua), v = unit(va)
|
|
307
|
+
return [
|
|
308
|
+
u[1] * v[2] - u[2] * v[1],
|
|
309
|
+
u[2] * v[0] - u[0] * v[2],
|
|
310
|
+
u[0] * v[1] - u[1] * v[0],
|
|
311
|
+
][axis]
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* One face: `at` its minimum corner, `du` and `dv` its extent along its two
|
|
316
|
+
* axes (in metres), `sign` which way along `axis` it faces. `c` holds a colour
|
|
317
|
+
* per corner (r, g, b, in corner order: at, +u, +u+v, +v). `rim` flags its
|
|
318
|
+
* edges (−u, +u, −v, +v) for rounding, `round` is the rounding's width in
|
|
319
|
+
* metres, `ao` darkens the corners (0 dark to 3 open), `cell` is the edge of
|
|
320
|
+
* the voxels it is made of, and `material` what they are made of (`METAL`).
|
|
321
|
+
* The diagonal is flipped where that keeps the darkening smooth.
|
|
322
|
+
*/
|
|
323
|
+
export let quad = (
|
|
324
|
+
o: Out,
|
|
325
|
+
at: Vec,
|
|
326
|
+
axis: number,
|
|
327
|
+
du: number,
|
|
328
|
+
dv: number,
|
|
329
|
+
sign: number,
|
|
330
|
+
c: number[],
|
|
331
|
+
rim: [number, number, number, number],
|
|
332
|
+
round: number,
|
|
333
|
+
ao: [number, number, number, number] = [3, 3, 3, 3],
|
|
334
|
+
cell = 0.5,
|
|
335
|
+
material = MATTE,
|
|
336
|
+
): void => {
|
|
337
|
+
let [ua, va] = axes(axis)
|
|
338
|
+
let base = o.pos.length / 3
|
|
339
|
+
let bu = Math.min(0.5, round / du), bv = Math.min(0.5, round / dv)
|
|
340
|
+
let flags = rim[0] | rim[1] << 1 | rim[2] << 2 | rim[3] << 3
|
|
341
|
+
for (let i = 0; i < 4; i++) {
|
|
342
|
+
let cu = i == 1 || i == 2 ? 1 : 0, cv = i >= 2 ? 1 : 0
|
|
343
|
+
let p: Vec = [at[0], at[1], at[2]]
|
|
344
|
+
p[ua] += cu * du
|
|
345
|
+
p[va] += cv * dv
|
|
346
|
+
o.pos.push(p[0], p[1], p[2])
|
|
347
|
+
o.nrm.push(
|
|
348
|
+
axis == 0 ? sign : 0,
|
|
349
|
+
axis == 1 ? sign : 0,
|
|
350
|
+
axis == 2 ? sign : 0,
|
|
351
|
+
0,
|
|
352
|
+
)
|
|
353
|
+
let shade = AO[ao[i]]
|
|
354
|
+
o.col.push(
|
|
355
|
+
srgb(c[i * 3] * shade),
|
|
356
|
+
srgb(c[i * 3 + 1] * shade),
|
|
357
|
+
srgb(c[i * 3 + 2] * shade),
|
|
358
|
+
255,
|
|
359
|
+
)
|
|
360
|
+
o.edge.push(cu, cv, flags, material)
|
|
361
|
+
o.bw.push(bu, bv, cell)
|
|
362
|
+
}
|
|
363
|
+
let flip = ao[0] + ao[2] < ao[1] + ao[3]
|
|
364
|
+
let t = flip ? [[0, 1, 3], [1, 2, 3]] : [[0, 1, 2], [0, 2, 3]]
|
|
365
|
+
for (let [a, b, d] of t) {
|
|
366
|
+
if (WINDING[axis] * sign > 0) o.idx.push(base + a, base + b, base + d)
|
|
367
|
+
else o.idx.push(base + a, base + d, base + b)
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// One sRGB byte as the linear light three.js blends vertex colours in, and
|
|
372
|
+
// back: linear light as an sRGB byte, each looked up, since a level writes
|
|
373
|
+
// millions (the soft shader decodes it again).
|
|
374
|
+
let LINEAR = Float64Array.from({ length: 256 }, (_, i) => {
|
|
375
|
+
let c = i / 255
|
|
376
|
+
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4
|
|
377
|
+
})
|
|
378
|
+
let STEPS = 4096
|
|
379
|
+
let SRGB = Uint8Array.from({ length: STEPS + 1 }, (_, i) => {
|
|
380
|
+
let c = i / STEPS
|
|
381
|
+
return Math.round(
|
|
382
|
+
255 * (c <= 0.0031308 ? c * 12.92 : 1.055 * c ** (1 / 2.4) - 0.055),
|
|
383
|
+
)
|
|
384
|
+
})
|
|
385
|
+
let srgb = (c: number) =>
|
|
386
|
+
SRGB[Math.max(0, Math.min(STEPS, Math.round(c * STEPS)))]
|
|
387
|
+
|
|
388
|
+
/** A colour, written as sRGB hex, as three linear numbers in [0, 1]. */
|
|
389
|
+
export let rgb = (hex: number): Vec => [
|
|
390
|
+
LINEAR[(hex >> 16) & 255],
|
|
391
|
+
LINEAR[(hex >> 8) & 255],
|
|
392
|
+
LINEAR[hex & 255],
|
|
393
|
+
]
|
|
394
|
+
|
|
395
|
+
/** A small voxel model: voxel coordinates, packed, to a colour, and what the
|
|
396
|
+
* voxel is made of (`metal`). */
|
|
397
|
+
export type Vox = Map<number, number>
|
|
398
|
+
|
|
399
|
+
let B = 512
|
|
400
|
+
/** Coordinates in [−512, 512) as one key. */
|
|
401
|
+
export let key = (x: number, y: number, z: number) =>
|
|
402
|
+
((x + B) * 1024 + (y + B)) * 1024 + (z + B)
|
|
403
|
+
|
|
404
|
+
/** A key's coordinates. */
|
|
405
|
+
export let unkey = (k: number): Vec => [
|
|
406
|
+
Math.floor(k / 1048576) - B,
|
|
407
|
+
(Math.floor(k / 1024) % 1024) - B,
|
|
408
|
+
(k % 1024) - B,
|
|
409
|
+
]
|
|
410
|
+
|
|
411
|
+
/** Fill a box of voxels, corners inclusive. */
|
|
412
|
+
export let box = (
|
|
413
|
+
v: Vox,
|
|
414
|
+
[x0, y0, z0]: Vec,
|
|
415
|
+
[x1, y1, z1]: Vec,
|
|
416
|
+
color: number,
|
|
417
|
+
): Vox => {
|
|
418
|
+
for (let x = x0; x <= x1; x++) {
|
|
419
|
+
for (let y = y0; y <= y1; y++) {
|
|
420
|
+
for (let z = z0; z <= z1; z++) v.set(key(x, y, z), color)
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return v
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** Fill a ball of voxels, with `pick` choosing each one's colour (or none),
|
|
427
|
+
* given its coordinates. */
|
|
428
|
+
export let ball = (
|
|
429
|
+
v: Vox,
|
|
430
|
+
[cx, cy, cz]: Vec,
|
|
431
|
+
r: number,
|
|
432
|
+
pick: (x: number, y: number, z: number) => number | null,
|
|
433
|
+
): Vox => {
|
|
434
|
+
let R = Math.ceil(r)
|
|
435
|
+
for (let x = -R; x <= R; x++) {
|
|
436
|
+
for (let y = -R; y <= R; y++) {
|
|
437
|
+
for (let z = -R; z <= R; z++) {
|
|
438
|
+
if (x * x + y * y + z * z > r * r) continue
|
|
439
|
+
let c = pick(cx + x, cy + y, cz + z)
|
|
440
|
+
if (c != null) v.set(key(cx + x, cy + y, cz + z), c)
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return v
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// One exposed voxel face, before merging: where it lies in its plane, and
|
|
448
|
+
// everything that must match for two faces to merge.
|
|
449
|
+
type Face = { u: number; v: number; color: number; rim: number; ao: number }
|
|
450
|
+
// The exposed faces in one plane: the axis it looks along, which way, and
|
|
451
|
+
// its depth, in voxels.
|
|
452
|
+
type Plane = {
|
|
453
|
+
axis: number
|
|
454
|
+
sign: number
|
|
455
|
+
depth: number
|
|
456
|
+
faces: Map<number, Face>
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Mesh a voxel model into `o`: voxel `size`, placed with its origin at `at`.
|
|
461
|
+
* Every exposed face is rounded along each edge that is convex; faces alike
|
|
462
|
+
* in colour, rounding and shade merge into rectangles. `cell` controls the
|
|
463
|
+
* visible shade grid on those faces without changing their outline.
|
|
464
|
+
*/
|
|
465
|
+
export let blob = (
|
|
466
|
+
o: Out,
|
|
467
|
+
v: Vox,
|
|
468
|
+
size: number,
|
|
469
|
+
at: Vec = [0, 0, 0],
|
|
470
|
+
round = 0.22,
|
|
471
|
+
cell = size,
|
|
472
|
+
): Out => {
|
|
473
|
+
// The voxels as a grid over their bounds and one more all round, 1 where
|
|
474
|
+
// solid: each face asks a score of its neighbours, and a grid answers
|
|
475
|
+
// sooner than the map. A neighbour's cell is a step along its axis.
|
|
476
|
+
let ps = [...v.keys()].map(unkey)
|
|
477
|
+
let lo = [Infinity, Infinity, Infinity],
|
|
478
|
+
hi = [-Infinity, -Infinity, -Infinity]
|
|
479
|
+
for (let p of ps) {
|
|
480
|
+
for (let a = 0; a < 3; a++) {
|
|
481
|
+
lo[a] = Math.min(lo[a], p[a])
|
|
482
|
+
hi[a] = Math.max(hi[a], p[a])
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
let H = hi[1] - lo[1] + 3, D = hi[2] - lo[2] + 3, along = [H * D, D, 1]
|
|
486
|
+
let cells = new Uint8Array(ps.length ? (hi[0] - lo[0] + 3) * H * D : 0)
|
|
487
|
+
let cellOf = (p: Vec) =>
|
|
488
|
+
(p[0] - lo[0] + 1) * H * D + (p[1] - lo[1] + 1) * D + p[2] - lo[2] + 1
|
|
489
|
+
for (let p of ps) cells[cellOf(p)] = 1
|
|
490
|
+
let solid = (c: number) => cells[c] == 1
|
|
491
|
+
// Faces by plane: axis, sign and depth.
|
|
492
|
+
let planes = new Map<number, Plane>()
|
|
493
|
+
let n = 0
|
|
494
|
+
for (let color of v.values()) {
|
|
495
|
+
let p = ps[n++], k = cellOf(p)
|
|
496
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
497
|
+
let [ua, va] = axes(axis), du = along[ua], dv = along[va]
|
|
498
|
+
for (let sign = -1; sign <= 1; sign += 2) {
|
|
499
|
+
let q = k + sign * along[axis]
|
|
500
|
+
if (solid(q)) continue
|
|
501
|
+
// What stands on the ground never shows its underside.
|
|
502
|
+
if (axis == 1 && sign < 0 && p[1] == 0) continue
|
|
503
|
+
// An edge is convex where the voxel beyond it, and the one diagonally
|
|
504
|
+
// out past it, are both empty.
|
|
505
|
+
let open = (d: number) => !solid(k + d) && !solid(q + d) ? 1 : 0
|
|
506
|
+
let rim = open(-du) | open(du) << 1 | open(-dv) << 2 | open(dv) << 3
|
|
507
|
+
// Ambient occlusion from the voxels just outside the face.
|
|
508
|
+
let occ = (su: number, sv: number) => {
|
|
509
|
+
let a = q + su * du, b = q + sv * dv
|
|
510
|
+
let s1 = solid(a) ? 1 : 0, s2 = solid(b) ? 1 : 0
|
|
511
|
+
return s1 && s2 ? 0 : 3 - (s1 + s2 + (solid(a + sv * dv) ? 1 : 0))
|
|
512
|
+
}
|
|
513
|
+
let ao = occ(-1, -1) | occ(1, -1) << 2 | occ(1, 1) << 4 |
|
|
514
|
+
occ(-1, 1) << 6
|
|
515
|
+
let id = (axis * 2 + (sign + 1) / 2) * 1024 + p[axis] + B
|
|
516
|
+
let plane = planes.get(id)
|
|
517
|
+
if (!plane) {
|
|
518
|
+
planes.set(
|
|
519
|
+
id,
|
|
520
|
+
plane = { axis, sign, depth: p[axis], faces: new Map() },
|
|
521
|
+
)
|
|
522
|
+
}
|
|
523
|
+
plane.faces.set(p[ua] * 4096 + p[va], {
|
|
524
|
+
u: p[ua],
|
|
525
|
+
v: p[va],
|
|
526
|
+
color,
|
|
527
|
+
rim,
|
|
528
|
+
ao,
|
|
529
|
+
})
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
for (let { axis, sign, depth, faces } of planes.values()) {
|
|
534
|
+
let [ua, va] = axes(axis)
|
|
535
|
+
// Runs along u: neighbouring faces alike in colour, shade and their
|
|
536
|
+
// rounding across the run. Inside a run no edge is convex (each face's
|
|
537
|
+
// neighbour is there), so a run's own ends say its rounding along u.
|
|
538
|
+
let runs = new Map<number, Run[]>()
|
|
539
|
+
let rows = new Map<number, Face[]>()
|
|
540
|
+
for (let f of faces.values()) {
|
|
541
|
+
if (!rows.has(f.v)) rows.set(f.v, [])
|
|
542
|
+
rows.get(f.v)!.push(f)
|
|
543
|
+
}
|
|
544
|
+
for (let [row, cells] of rows) {
|
|
545
|
+
cells.sort((a, b) => a.u - b.u)
|
|
546
|
+
let out: Run[] = []
|
|
547
|
+
for (let f of cells) {
|
|
548
|
+
let last = out[out.length - 1]
|
|
549
|
+
if (
|
|
550
|
+
last && last.u + last.w == f.u && last.color == f.color &&
|
|
551
|
+
last.ao == f.ao && last.across == (f.rim & 12)
|
|
552
|
+
) {
|
|
553
|
+
last.w++
|
|
554
|
+
last.end = (f.rim >> 1) & 1
|
|
555
|
+
} else {
|
|
556
|
+
out.push({
|
|
557
|
+
u: f.u,
|
|
558
|
+
w: 1,
|
|
559
|
+
color: f.color,
|
|
560
|
+
ao: f.ao,
|
|
561
|
+
across: f.rim & 12,
|
|
562
|
+
start: f.rim & 1,
|
|
563
|
+
end: (f.rim >> 1) & 1,
|
|
564
|
+
used: false,
|
|
565
|
+
})
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
runs.set(row, out)
|
|
569
|
+
}
|
|
570
|
+
// Stack runs of one span into rectangles along v.
|
|
571
|
+
let order = [...runs.keys()].sort((a, b) => a - b)
|
|
572
|
+
for (let row of order) {
|
|
573
|
+
for (let r of runs.get(row)!) {
|
|
574
|
+
if (r.used) continue
|
|
575
|
+
r.used = true
|
|
576
|
+
let h = 1, top = r
|
|
577
|
+
for (;;) {
|
|
578
|
+
let next = runs.get(row + h)?.find((n) =>
|
|
579
|
+
!n.used && n.u == r.u && n.w == r.w && n.color == r.color &&
|
|
580
|
+
n.ao == r.ao && n.start == r.start && n.end == r.end
|
|
581
|
+
)
|
|
582
|
+
if (!next) break
|
|
583
|
+
next.used = true
|
|
584
|
+
top = next
|
|
585
|
+
h++
|
|
586
|
+
}
|
|
587
|
+
let min: Vec = [0, 0, 0]
|
|
588
|
+
min[axis] = depth + (sign > 0 ? 1 : 0)
|
|
589
|
+
min[ua] = r.u
|
|
590
|
+
min[va] = row
|
|
591
|
+
let c = rgb(r.color)
|
|
592
|
+
quad(
|
|
593
|
+
o,
|
|
594
|
+
[at[0] + min[0] * size, at[1] + min[1] * size, at[2] + min[2] * size],
|
|
595
|
+
axis,
|
|
596
|
+
r.w * size,
|
|
597
|
+
h * size,
|
|
598
|
+
sign,
|
|
599
|
+
[...c, ...c, ...c, ...c],
|
|
600
|
+
[r.start, r.end, (r.across >> 2) & 1, (top.across >> 3) & 1],
|
|
601
|
+
size * round,
|
|
602
|
+
[r.ao & 3, (r.ao >> 2) & 3, (r.ao >> 4) & 3, (r.ao >> 6) & 3],
|
|
603
|
+
cell,
|
|
604
|
+
materialOf(r.color),
|
|
605
|
+
)
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
return o
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// A run of alike faces along u, and how it is rounded: `across` holds the
|
|
613
|
+
// rounding of its edges along v (the −v and +v bits), `start` and `end` its
|
|
614
|
+
// two ends'.
|
|
615
|
+
type Run = {
|
|
616
|
+
u: number
|
|
617
|
+
w: number
|
|
618
|
+
color: number
|
|
619
|
+
ao: number
|
|
620
|
+
across: number
|
|
621
|
+
start: number
|
|
622
|
+
end: number
|
|
623
|
+
used: boolean
|
|
624
|
+
}
|
package/mod.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Voxel geometry and chunk charts over plain values, without a renderer or a graph. */ export * from "./mesh.js";
|
|
2
|
+
export * from "./boxes.js";
|
|
3
|
+
export * from "./light.js";
|
|
4
|
+
export * from "./stream.js";
|
|
5
|
+
export * from "./chartbook.js";
|
|
6
|
+
export * from "./chartsheet.js";
|
|
7
|
+
//# sourceMappingURL=mod.js.map
|
package/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA,sFAAsF,GACtF,0BAAyB;AACzB,2BAA0B;AAC1B,2BAA0B;AAC1B,4BAA2B;AAC3B,+BAA8B;AAC9B,gCAA+B"}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Voxel geometry and chunk charts over plain values, without a renderer or a graph. */
|
|
2
|
+
export * from './mesh.js'
|
|
3
|
+
export * from './boxes.js'
|
|
4
|
+
export * from './light.js'
|
|
5
|
+
export * from './stream.js'
|
|
6
|
+
export * from './chartbook.js'
|
|
7
|
+
export * from './chartsheet.js'
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaks/voxel",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.36",
|
|
4
|
+
"homepage": "https://jsr.io/@yaks/voxel",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./_dist/mod.d.ts",
|
|
9
|
+
"default": "./mod.js"
|
|
10
|
+
},
|
|
11
|
+
"./mesh": {
|
|
12
|
+
"types": "./_dist/mesh.d.ts",
|
|
13
|
+
"default": "./mesh.js"
|
|
14
|
+
},
|
|
15
|
+
"./boxes": {
|
|
16
|
+
"types": "./_dist/boxes.d.ts",
|
|
17
|
+
"default": "./boxes.js"
|
|
18
|
+
},
|
|
19
|
+
"./light": {
|
|
20
|
+
"types": "./_dist/light.d.ts",
|
|
21
|
+
"default": "./light.js"
|
|
22
|
+
},
|
|
23
|
+
"./stream": {
|
|
24
|
+
"types": "./_dist/stream.d.ts",
|
|
25
|
+
"default": "./stream.js"
|
|
26
|
+
},
|
|
27
|
+
"./chartbook": {
|
|
28
|
+
"types": "./_dist/chartbook.d.ts",
|
|
29
|
+
"default": "./chartbook.js"
|
|
30
|
+
},
|
|
31
|
+
"./chartsheet": {
|
|
32
|
+
"types": "./_dist/chartsheet.d.ts",
|
|
33
|
+
"default": "./chartsheet.js"
|
|
34
|
+
},
|
|
35
|
+
"./three": {
|
|
36
|
+
"types": "./_dist/three/mod.d.ts",
|
|
37
|
+
"default": "./three/mod.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"license": "Apache-2.0",
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@types/three": "^0.186.0",
|
|
43
|
+
"three": "^0.186.1"
|
|
44
|
+
},
|
|
45
|
+
"description": "Worker-safe voxel geometry, baked lamp light, chunk detail and heightfield charts, with Three.js materials and box rigs at /three.",
|
|
46
|
+
"repository": "https://github.com/yak-sh/yak.git"
|
|
47
|
+
}
|