@n3rdw1z4rd/core 0.24.0 → 0.26.0

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.
Files changed (54) hide show
  1. package/README.md +45 -0
  2. package/dist/astar.js +3 -3
  3. package/dist/desktop.d.ts +37 -0
  4. package/dist/desktop.js +14 -0
  5. package/dist/dungeon-generator.js +4 -4
  6. package/dist/electron/after-pack.cjs +17 -0
  7. package/dist/electron/index.d.ts +110 -0
  8. package/dist/electron/index.js +257 -0
  9. package/dist/electron/preload.cjs +12 -0
  10. package/dist/electron/updater.d.ts +81 -0
  11. package/dist/electron/updater.js +133 -0
  12. package/dist/electron.cjs +416 -0
  13. package/dist/electron.d.cts +189 -0
  14. package/dist/graph/index.d.ts +21 -0
  15. package/dist/graph/subgraph.js +1 -1
  16. package/dist/graph.cjs +2 -2
  17. package/dist/graph.d.cts +21 -0
  18. package/dist/index.cjs +25 -11
  19. package/dist/index.d.cts +48 -0
  20. package/dist/index.d.ts +13 -1
  21. package/dist/index.js +2 -1
  22. package/dist/map2d.js +2 -2
  23. package/dist/math.js +2 -2
  24. package/dist/{rng-edL6atS3.cjs → rng-BtvhTJmM.cjs} +2 -2
  25. package/dist/{spatial-partition-3d-D1uyJVhM.cjs → spatial-partition-3d-qhjqIpcy.cjs} +1 -1
  26. package/dist/three/adaptive-grid.d.ts +1 -1
  27. package/dist/three/adaptive-grid.js +1 -1
  28. package/dist/three/asteroid-mesh.d.ts +1 -1
  29. package/dist/three/camera-rig.js +1 -1
  30. package/dist/three/cube-sphere-geometry.d.ts +1 -1
  31. package/dist/three/index.d.ts +13 -2
  32. package/dist/three/particle-system-3d.js +1 -1
  33. package/dist/three/picking.d.ts +1 -1
  34. package/dist/three/picking.js +1 -1
  35. package/dist/three/starfield.js +4 -4
  36. package/dist/three/texture-atlas.d.ts +1 -1
  37. package/dist/three/texture-atlas.js +1 -1
  38. package/dist/three/texture-utils.js +2 -4
  39. package/dist/three/voxel-geometry.d.ts +44 -10
  40. package/dist/three/voxel-geometry.js +282 -77
  41. package/dist/three/voxel-mesh.d.ts +1 -1
  42. package/dist/three/voxel-mesh.js +1 -1
  43. package/dist/three/world.d.ts +1 -1
  44. package/dist/three/world.js +1 -1
  45. package/dist/three.cjs +290 -87
  46. package/dist/three.d.cts +55 -10
  47. package/dist/vite.cjs +124 -0
  48. package/dist/vite.d.cts +53 -2
  49. package/dist/vite.d.ts +53 -2
  50. package/dist/vite.js +122 -1
  51. package/dist/webgl.js +2 -2
  52. package/package.json +23 -3
  53. package/dist/input/_input.d.ts +0 -23
  54. package/dist/input/_input.js +0 -96
@@ -10,7 +10,7 @@ function pickEntity(world, view, sx, sy, options = {}) {
10
10
  const direct = castAt(world, view, sx, sy, options.filter);
11
11
  if (direct || !options.slopPx) return direct;
12
12
  let best = null;
13
- let bestD = Infinity;
13
+ let bestD = 1e400;
14
14
  for (const r of [options.slopPx / 2, options.slopPx]) {
15
15
  for (let k = 0; k < 8; k++) {
16
16
  const a = k / 8 * Math.PI * 2;
@@ -1,5 +1,5 @@
1
- import { Rng, rng } from "../rng.js";
2
1
  import { BufferAttribute, BufferGeometry, Group, Mesh, MeshBasicMaterial, PointLight, Points, PointsMaterial, SphereGeometry, Vector3 } from "three";
2
+ import { Rng, rng } from "../rng.js";
3
3
  // src/three/starfield.ts
4
4
  /**
5
5
  * Points spread uniformly over a sphere's surface, for a distant star backdrop (point size 5 with
@@ -12,7 +12,7 @@ import { BufferAttribute, BufferGeometry, Group, Mesh, MeshBasicMaterial, PointL
12
12
  * @param seed seeds a private {@link Rng} for a reproducible starfield
13
13
  * @category Geometry & Meshes
14
14
  */
15
- function CreateStarfield(radius = 1e3, count = 5e3, color = 16777215, seed) {
15
+ function CreateStarfield(radius = 1e3, count = 5e3, color = 0xffffff, seed) {
16
16
  const random = seed === void 0 ? rng : new Rng(seed);
17
17
  const geometry = new BufferGeometry();
18
18
  const positions = new Float32Array(count * 3);
@@ -51,11 +51,11 @@ function CreateSystemStar(position) {
51
51
  const star = new Group();
52
52
  const starPos = position ?? new Vector3();
53
53
  const starGeo = new SphereGeometry(50, 32, 32);
54
- const starMat = new MeshBasicMaterial({ color: 16768426 });
54
+ const starMat = new MeshBasicMaterial({ color: 0xffddaa });
55
55
  const starMesh = new Mesh(starGeo, starMat);
56
56
  starMesh.position.copy(starPos);
57
57
  star.add(starMesh);
58
- const starLight = new PointLight(16777215, 10, 0, 0);
58
+ const starLight = new PointLight(0xffffff, 10, 0, 0);
59
59
  starLight.position.copy(starPos);
60
60
  star.add(starLight);
61
61
  return star;
@@ -1,5 +1,5 @@
1
- import { TextureData } from "./texture-utils.js";
2
1
  import { MeshLambertMaterial, MeshLambertMaterialParameters, Texture } from "three";
2
+ import { TextureData } from "./texture-utils.js";
3
3
  /**
4
4
  * Texture coordinate `[u, v]`, each 0..1, with v = 1 at the image's top (three.js convention).
5
5
  *
@@ -1,5 +1,5 @@
1
- import { LoadTexture } from "./texture-utils.js";
2
1
  import { MeshLambertMaterial, NearestFilter } from "three";
2
+ import { LoadTexture } from "./texture-utils.js";
3
3
  // src/three/texture-atlas.ts
4
4
  /**
5
5
  * Tile layout and UV lookup for a texture atlas (tile sheet). Tiles are numbered left to
@@ -9,11 +9,9 @@ import { NearestFilter, RepeatWrapping, TextureLoader } from "three";
9
9
  function LoadTexture(url) {
10
10
  return new Promise((res, rej) => {
11
11
  new TextureLoader().load(url, (texture) => {
12
- const width = texture.source.data.width;
13
- const height = texture.source.data.height;
14
12
  res({
15
- width,
16
- height,
13
+ width: texture.source.data.width,
14
+ height: texture.source.data.height,
17
15
  texture
18
16
  });
19
17
  }, (_ev) => {}, (err) => rej(err));
@@ -1,6 +1,32 @@
1
+ import { BufferGeometry } from "three";
1
2
  import { Map3D } from "../map3d.js";
2
3
  import { ColorPalette } from "../color-palette.js";
3
- import { BufferGeometry } from "three";
4
+ /**
5
+ * Voxel data {@link buildVoxelGeometry} can mesh: anything that can look up a voxel and list the
6
+ * solid ones. A {@link Map3D} is one as it is; a dense volume (e.g. a `Uint8Array` with 0 = empty,
7
+ * as in MagicVoxel's .vox files) or a view onto part of a bigger volume (one chunk of it) can be
8
+ * one too, without copying its voxels into a `Map3D` first.
9
+ *
10
+ * ```ts
11
+ * // a 32^3 chunk of a dense 256^3 volume: faces are listed for the chunk's voxels only, but
12
+ * // `get` reads the whole volume, so faces against neighbouring chunks are culled (no `neighbor` needed)
13
+ * const chunk: VoxelSource = {
14
+ * defaultValue: 0,
15
+ * get: (x, y, z) => volume.get(x, y, z), // 0 outside the volume
16
+ * forEach: (fn) => volume.forEachIn(box, fn), // only the solid voxels inside the chunk
17
+ * };
18
+ * ```
19
+ *
20
+ * @category Voxels
21
+ */
22
+ export interface VoxelSource {
23
+ /** the value that means empty: `get()` returns it for empty cells, and `forEach()` never visits it */
24
+ readonly defaultValue: number;
25
+ /** value at integer (x, y, z); `defaultValue` where empty, including outside the data */
26
+ get(x: number, y: number, z: number): number;
27
+ /** calls `callback` once for each solid voxel to mesh (in any order: the output is sorted) */
28
+ forEach(callback: (x: number, y: number, z: number, value: number) => void): void;
29
+ }
4
30
  /**
5
31
  * Options for {@link buildVoxelGeometry} and {@link VoxelGeometry}.
6
32
  *
@@ -23,15 +49,23 @@ export interface BuildVoxelGeometryOptions {
23
49
  aoStrength?: number;
24
50
  /** rebuild into this geometry instead of creating one */
25
51
  target?: BufferGeometry;
52
+ /**
53
+ * How to mesh; every strategy gives exactly the same geometry. `'dense'` copies the voxels'
54
+ * bounding box into flat arrays first (fast); `'sparse'` works from hash maps (for voxels
55
+ * spread far apart, where that box would be huge). Default `'auto'`: dense unless the box is
56
+ * much bigger than the voxels need.
57
+ */
58
+ strategy?: 'auto' | 'dense' | 'sparse';
26
59
  }
27
60
  /**
28
61
  * Builds a mesh for the voxels in `voxels`: one quad per visible face, merged into larger quads
29
62
  * (`greedy`) and shaded with per-vertex ambient occlusion. A voxel's value is its palette index;
30
- * the map's default value means empty.
63
+ * the source's default value means empty. `voxels` is a {@link Map3D} or any other
64
+ * {@link VoxelSource}.
31
65
  *
32
- * Positions are in the map's own coordinates, so a chunk stored in chunk-local coordinates is
66
+ * Positions are in the source's own coordinates, so a chunk stored in chunk-local coordinates is
33
67
  * placed by positioning its mesh. Pass `neighbor` so faces against neighbouring chunks are culled
34
- * and their voxels count for ambient occlusion.
68
+ * and their voxels count for ambient occlusion (or give a source whose `get` already sees them).
35
69
  *
36
70
  * The geometry has position, normal and color attributes and an index; use a material with
37
71
  * `vertexColors: true`. The output is deterministic: the same voxels and options always produce
@@ -49,7 +83,7 @@ export interface BuildVoxelGeometryOptions {
49
83
  * @returns `options.target` if given, otherwise a new geometry
50
84
  * @category Voxels
51
85
  */
52
- export declare function buildVoxelGeometry(voxels: Map3D, options?: BuildVoxelGeometryOptions): BufferGeometry;
86
+ export declare function buildVoxelGeometry(voxels: VoxelSource, options?: BuildVoxelGeometryOptions): BufferGeometry;
53
87
  /**
54
88
  * Geometry that owns its voxels; call `update()` after changing them. See
55
89
  * {@link buildVoxelGeometry} for how voxels are meshed, and {@link VoxelMesh} for an editable mesh
@@ -57,14 +91,14 @@ export declare function buildVoxelGeometry(voxels: Map3D, options?: BuildVoxelGe
57
91
  *
58
92
  * @category Voxels
59
93
  */
60
- export declare class VoxelGeometry extends BufferGeometry {
94
+ export declare class VoxelGeometry<V extends VoxelSource = Map3D> extends BufferGeometry {
61
95
  type: string;
62
- /** voxel data (palette indices); the map's default value is empty */
63
- voxels: Map3D;
96
+ /** voxel data (palette indices); the source's default value is empty */
97
+ voxels: V;
64
98
  /** meshing options used by every `update()` */
65
99
  options: Omit<BuildVoxelGeometryOptions, 'target'>;
66
- /** Builds the geometry right away. */
67
- constructor(voxels?: Map3D, options?: Omit<BuildVoxelGeometryOptions, 'target'>);
100
+ /** Builds the geometry right away. Without `voxels`, starts from an empty {@link Map3D}. */
101
+ constructor(voxels?: V, options?: Omit<BuildVoxelGeometryOptions, 'target'>);
68
102
  /** Rebuilds the geometry from `voxels` and `options`. */
69
103
  update(): this;
70
104
  }
@@ -1,17 +1,18 @@
1
+ import { BufferGeometry, Color, Float32BufferAttribute, Uint16BufferAttribute, Uint32BufferAttribute } from "three";
1
2
  import { Map3D } from "../map3d.js";
2
3
  import { Random } from "../random.js";
3
- import { BufferGeometry, Color, Float32BufferAttribute } from "three";
4
4
  // src/three/voxel-geometry.ts
5
5
  const cellKey = (u, v) => (u + 33554432) * 67108864 + (v + 33554432);
6
6
  const planeKey = (d, side, slice) => (slice + 33554432) * 8 + d * 2 + (side > 0 ? 1 : 0);
7
7
  /**
8
8
  * Builds a mesh for the voxels in `voxels`: one quad per visible face, merged into larger quads
9
9
  * (`greedy`) and shaded with per-vertex ambient occlusion. A voxel's value is its palette index;
10
- * the map's default value means empty.
10
+ * the source's default value means empty. `voxels` is a {@link Map3D} or any other
11
+ * {@link VoxelSource}.
11
12
  *
12
- * Positions are in the map's own coordinates, so a chunk stored in chunk-local coordinates is
13
+ * Positions are in the source's own coordinates, so a chunk stored in chunk-local coordinates is
13
14
  * placed by positioning its mesh. Pass `neighbor` so faces against neighbouring chunks are culled
14
- * and their voxels count for ambient occlusion.
15
+ * and their voxels count for ambient occlusion (or give a source whose `get` already sees them).
15
16
  *
16
17
  * The geometry has position, normal and color attributes and an index; use a material with
17
18
  * `vertexColors: true`. The output is deterministic: the same voxels and options always produce
@@ -30,31 +31,290 @@ const planeKey = (d, side, slice) => (slice + 33554432) * 8 + d * 2 + (side > 0
30
31
  * @category Voxels
31
32
  */
32
33
  function buildVoxelGeometry(voxels, options = {}) {
34
+ const settings = resolveOptions(voxels, options);
35
+ const strategy = options.strategy ?? "auto";
36
+ const xs = [], ys = [], zs = [], values = [];
37
+ const min = [
38
+ 1e400,
39
+ 1e400,
40
+ 1e400
41
+ ], max = [
42
+ -1e400,
43
+ -1e400,
44
+ -1e400
45
+ ];
46
+ voxels.forEach((x, y, z, value) => {
47
+ xs.push(x);
48
+ ys.push(y);
49
+ zs.push(z);
50
+ values.push(value);
51
+ if (x < min[0]) min[0] = x;
52
+ if (x > max[0]) max[0] = x;
53
+ if (y < min[1]) min[1] = y;
54
+ if (y > max[1]) max[1] = y;
55
+ if (z < min[2]) min[2] = z;
56
+ if (z > max[2]) max[2] = z;
57
+ });
58
+ const cells = xs.length ? (max[0] - min[0] + 3) * (max[1] - min[1] + 3) * (max[2] - min[2] + 3) : 0;
59
+ const dense = strategy === "dense" || strategy === "auto" && cells <= Math.max(DENSE_MIN_CELLS, xs.length * DENSE_CELLS_PER_VOXEL);
60
+ const mesh = !xs.length ? new MeshData() : dense ? meshDense(voxels, settings, xs, ys, zs, values, min, max) : meshSparse(voxels, settings, xs, ys, zs, values);
61
+ const geometry = options.target ?? new BufferGeometry();
62
+ geometry.setAttribute("position", new Float32BufferAttribute(mesh.positions.subarray(0, mesh.vertices * 3), 3));
63
+ geometry.setAttribute("normal", new Float32BufferAttribute(mesh.normals.subarray(0, mesh.vertices * 3), 3));
64
+ geometry.setAttribute("color", new Float32BufferAttribute(mesh.colors.subarray(0, mesh.vertices * 3), 3));
65
+ const indices = mesh.indices.subarray(0, mesh.indexCount);
66
+ geometry.setIndex(mesh.vertices > 65535 ? new Uint32BufferAttribute(indices.slice(), 1) : new Uint16BufferAttribute(Uint16Array.from(indices), 1));
67
+ geometry.computeBoundingBox();
68
+ geometry.computeBoundingSphere();
69
+ return geometry;
70
+ }
71
+ const DENSE_CELLS_PER_VOXEL = 64;
72
+ const DENSE_MIN_CELLS = 262144;
73
+ function resolveOptions(voxels, options) {
33
74
  const palette = options.palette ?? [];
34
- const neighbor = options.neighbor;
35
- const includeHiddenFaces = options.includeHiddenFaces ?? false;
36
- const greedy = options.greedy ?? true;
37
- const ambientOcclusion = options.ambientOcclusion ?? true;
38
- const aoStrength = options.aoStrength ?? .5;
39
- const empty = voxels.defaultValue;
75
+ return {
76
+ empty: voxels.defaultValue,
77
+ neighbor: options.neighbor,
78
+ includeHiddenFaces: options.includeHiddenFaces ?? false,
79
+ greedy: options.greedy ?? true,
80
+ ambientOcclusion: options.ambientOcclusion ?? true,
81
+ aoStrength: options.aoStrength ?? .5,
82
+ colorOf: (value) => palette[value] ?? options.missingColor ?? Random.uint(value, 1) & 0xffffff
83
+ };
84
+ }
85
+ /** growable output buffers; one quad at a time */
86
+ var MeshData = class {
87
+ positions = /* @__PURE__ */ new Float32Array(3072);
88
+ normals = /* @__PURE__ */ new Float32Array(3072);
89
+ colors = /* @__PURE__ */ new Float32Array(3072);
90
+ indices = /* @__PURE__ */ new Uint32Array(1536);
91
+ vertices = 0;
92
+ indexCount = 0;
93
+ corner = [
94
+ 0,
95
+ 0,
96
+ 0
97
+ ];
98
+ linear = new Color();
99
+ linearOf = /* @__PURE__ */ new Map();
100
+ /**
101
+ * One quad on plane (d, side, slice), from (u, v) to (u + width, v + height); `ao` per corner
102
+ * (0,0) (1,0) (1,1) (0,1), 0 = most occluded.
103
+ */
104
+ quad(d, side, slice, u, v, width, height, color, ao, aoStrength) {
105
+ if ((this.vertices + 4) * 3 > this.positions.length) {
106
+ this.positions = grow(this.positions);
107
+ this.normals = grow(this.normals);
108
+ this.colors = grow(this.colors);
109
+ }
110
+ if (this.indexCount + 6 > this.indices.length) this.indices = grow(this.indices);
111
+ let rgb = this.linearOf.get(color);
112
+ if (!rgb) {
113
+ this.linear.setHex(color);
114
+ rgb = [
115
+ this.linear.r,
116
+ this.linear.g,
117
+ this.linear.b
118
+ ];
119
+ this.linearOf.set(color, rgb);
120
+ }
121
+ const ud = (d + 1) % 3;
122
+ const vd = (d + 2) % 3;
123
+ const base = this.vertices;
124
+ const corner = this.corner;
125
+ for (let c = 0; c < 4; c++) {
126
+ corner[d] = slice;
127
+ corner[ud] = u + (c === 1 || c === 2 ? width : 0);
128
+ corner[vd] = v + (c >= 2 ? height : 0);
129
+ const i = (base + c) * 3;
130
+ const shade = 1 - aoStrength * (3 - ao[c]) / 3;
131
+ this.positions[i] = corner[0];
132
+ this.positions[i + 1] = corner[1];
133
+ this.positions[i + 2] = corner[2];
134
+ this.normals[i] = d === 0 ? side : 0;
135
+ this.normals[i + 1] = d === 1 ? side : 0;
136
+ this.normals[i + 2] = d === 2 ? side : 0;
137
+ this.colors[i] = rgb[0] * shade;
138
+ this.colors[i + 1] = rgb[1] * shade;
139
+ this.colors[i + 2] = rgb[2] * shade;
140
+ }
141
+ this.vertices += 4;
142
+ const tris = ao[0] + ao[2] >= ao[1] + ao[3] ? TRIS_A : TRIS_B;
143
+ for (let t = 0; t < 6; t += 3) {
144
+ this.indices[this.indexCount++] = base + tris[t];
145
+ this.indices[this.indexCount++] = base + tris[side > 0 ? t + 1 : t + 2];
146
+ this.indices[this.indexCount++] = base + tris[side > 0 ? t + 2 : t + 1];
147
+ }
148
+ }
149
+ };
150
+ const TRIS_A = [
151
+ 0,
152
+ 1,
153
+ 2,
154
+ 0,
155
+ 2,
156
+ 3
157
+ ];
158
+ const TRIS_B = [
159
+ 0,
160
+ 1,
161
+ 3,
162
+ 1,
163
+ 2,
164
+ 3
165
+ ];
166
+ function grow(array) {
167
+ const bigger = new array.constructor(array.length * 2);
168
+ bigger.set(array);
169
+ return bigger;
170
+ }
171
+ /**
172
+ * The bounding box (plus a 1-cell border) copied into flat arrays once: which cells are solid,
173
+ * then each plane's faces as a grid of signatures, merged by scanning it.
174
+ */
175
+ function meshDense(voxels, settings, xs, ys, zs, values, min, max) {
176
+ const { empty, neighbor, includeHiddenFaces, greedy, ambientOcclusion, aoStrength, colorOf } = settings;
177
+ const W = max[0] - min[0] + 3, H = max[1] - min[1] + 3, D = max[2] - min[2] + 3;
178
+ const X0 = min[0] - 1, Y0 = min[1] - 1, Z0 = min[2] - 1;
179
+ const stride = [
180
+ 1,
181
+ W,
182
+ W * H
183
+ ];
184
+ const solid = new Uint8Array(W * H * D);
185
+ const listed = new Uint8Array(W * H * D);
186
+ for (let n = 0; n < xs.length; n++) {
187
+ const i = xs[n] - X0 + (ys[n] - Y0) * W + (zs[n] - Z0) * W * H;
188
+ listed[i] = 1;
189
+ if (values[n] !== empty) solid[i] = 1;
190
+ }
191
+ const complete = voxels instanceof Map3D;
192
+ if (!complete || neighbor) {
193
+ let i = 0;
194
+ for (let z = Z0; z < Z0 + D; z++) for (let y = Y0; y < Y0 + H; y++) for (let x = X0; x < X0 + W; x++, i++) {
195
+ if (solid[i]) continue;
196
+ if (listed[i] && complete) {
197
+ if (neighbor && neighbor(x, y, z) !== empty) solid[i] = 1;
198
+ continue;
199
+ }
200
+ let value = complete ? empty : voxels.get(x, y, z);
201
+ if (value === empty && neighbor) value = neighbor(x, y, z);
202
+ if (value !== empty) solid[i] = 1;
203
+ }
204
+ }
205
+ const planes = /* @__PURE__ */ new Map();
206
+ const size = [
207
+ W - 2,
208
+ H - 2,
209
+ D - 2
210
+ ];
211
+ const p = [
212
+ 0,
213
+ 0,
214
+ 0
215
+ ];
216
+ const ao = [
217
+ 3,
218
+ 3,
219
+ 3,
220
+ 3
221
+ ];
222
+ for (let n = 0; n < xs.length; n++) {
223
+ p[0] = xs[n];
224
+ p[1] = ys[n];
225
+ p[2] = zs[n];
226
+ const i = p[0] - X0 + (p[1] - Y0) * W + (p[2] - Z0) * W * H;
227
+ const color = colorOf(values[n]);
228
+ for (let d = 0; d < 3; d++) {
229
+ const ud = (d + 1) % 3, vd = (d + 2) % 3;
230
+ const su = stride[ud], sv = stride[vd];
231
+ for (const side of SIDES) {
232
+ const f = i + side * stride[d];
233
+ if (!includeHiddenFaces && solid[f]) continue;
234
+ if (ambientOcclusion) {
235
+ const um = solid[f - su], up = solid[f + su], vm = solid[f - sv], vp = solid[f + sv];
236
+ ao[0] = um && vm ? 0 : 3 - (um + vm + solid[f - su - sv]);
237
+ ao[1] = up && vm ? 0 : 3 - (up + vm + solid[f + su - sv]);
238
+ ao[2] = up && vp ? 0 : 3 - (up + vp + solid[f + su + sv]);
239
+ ao[3] = um && vp ? 0 : 3 - (um + vp + solid[f - su + sv]);
240
+ }
241
+ const slice = p[d] + (side > 0 ? 1 : 0);
242
+ const key = planeKey(d, side, slice);
243
+ let plane = planes.get(key);
244
+ if (!plane) {
245
+ const nu = size[ud], nv = size[vd];
246
+ plane = {
247
+ d,
248
+ side,
249
+ slice,
250
+ nu,
251
+ nv,
252
+ sig: new Float64Array(nu * nv).fill(-1)
253
+ };
254
+ planes.set(key, plane);
255
+ }
256
+ plane.sig[(p[vd] - min[vd]) * plane.nu + (p[ud] - min[ud])] = color * 256 + ao[0] * 64 + ao[1] * 16 + ao[2] * 4 + ao[3];
257
+ }
258
+ }
259
+ }
260
+ const mesh = new MeshData();
261
+ const cornerAo = [
262
+ 0,
263
+ 0,
264
+ 0,
265
+ 0
266
+ ];
267
+ for (const key of [...planes.keys()].sort((a, b) => a - b)) {
268
+ const { d, side, slice, nu, nv, sig } = planes.get(key);
269
+ const ud = (d + 1) % 3, vd = (d + 2) % 3;
270
+ const used = new Uint8Array(nu * nv);
271
+ for (let v = 0; v < nv; v++) for (let u = 0; u < nu; u++) {
272
+ const k = v * nu + u, s = sig[k];
273
+ if (s < 0 || used[k]) continue;
274
+ let width = 1, height = 1;
275
+ if (greedy) {
276
+ while (u + width < nu && sig[k + width] === s && !used[k + width]) width++;
277
+ grow: while (v + height < nv) {
278
+ for (let du = 0; du < width; du++) {
279
+ const kk = (v + height) * nu + u + du;
280
+ if (sig[kk] !== s || used[kk]) break grow;
281
+ }
282
+ height++;
283
+ }
284
+ }
285
+ for (let dv = 0; dv < height; dv++) used.fill(1, (v + dv) * nu + u, (v + dv) * nu + u + width);
286
+ const bits = s % 256;
287
+ cornerAo[0] = bits >> 6;
288
+ cornerAo[1] = bits >> 4 & 3;
289
+ cornerAo[2] = bits >> 2 & 3;
290
+ cornerAo[3] = bits & 3;
291
+ mesh.quad(d, side, slice, min[ud] + u, min[vd] + v, width, height, (s - bits) / 256, cornerAo, aoStrength);
292
+ }
293
+ }
294
+ return mesh;
295
+ }
296
+ const SIDES = [1, -1];
297
+ /** Faces kept in hash maps: for voxels spread so far apart that their bounding box would be huge. */
298
+ function meshSparse(voxels, settings, xs, ys, zs, values) {
299
+ const { empty, neighbor, includeHiddenFaces, greedy, ambientOcclusion, aoStrength, colorOf } = settings;
40
300
  const solid = (x, y, z) => {
41
301
  let value = voxels.get(x, y, z);
42
302
  if (value === empty && neighbor) value = neighbor(x, y, z);
43
303
  return value !== empty;
44
304
  };
45
- const colorOf = (value) => palette[value] ?? options.missingColor ?? Random.uint(value, 1) & 16777215;
46
305
  const planes = /* @__PURE__ */ new Map();
47
306
  const p = [
48
307
  0,
49
308
  0,
50
309
  0
51
310
  ];
52
- voxels.forEach((x, y, z, value) => {
53
- const color = colorOf(value);
311
+ for (let n = 0; n < xs.length; n++) {
312
+ const x = xs[n], y = ys[n], z = zs[n];
313
+ const color = colorOf(values[n]);
54
314
  for (let d = 0; d < 3; d++) {
55
315
  const ud = (d + 1) % 3;
56
316
  const vd = (d + 2) % 3;
57
- for (const side of [1, -1]) {
317
+ for (const side of SIDES) {
58
318
  p[0] = x;
59
319
  p[1] = y;
60
320
  p[2] = z;
@@ -85,56 +345,8 @@ function buildVoxelGeometry(voxels, options = {}) {
85
345
  });
86
346
  }
87
347
  }
88
- });
89
- const positions = [];
90
- const normals = [];
91
- const colors = [];
92
- const indices = [];
93
- const linear = new Color();
94
- const corner = [
95
- 0,
96
- 0,
97
- 0
98
- ];
99
- const emitQuad = (d, side, slice, face, width, height) => {
100
- const ud = (d + 1) % 3;
101
- const vd = (d + 2) % 3;
102
- const base = positions.length / 3;
103
- linear.setHex(face.color);
104
- const cornerOffsets = [
105
- [0, 0],
106
- [width, 0],
107
- [width, height],
108
- [0, height]
109
- ];
110
- for (let c = 0; c < 4; c++) {
111
- corner[d] = slice;
112
- corner[ud] = face.u + cornerOffsets[c][0];
113
- corner[vd] = face.v + cornerOffsets[c][1];
114
- positions.push(corner[0], corner[1], corner[2]);
115
- normals.push(d === 0 ? side : 0, d === 1 ? side : 0, d === 2 ? side : 0);
116
- const shade = 1 - aoStrength * (3 - face.ao[c]) / 3;
117
- colors.push(linear.r * shade, linear.g * shade, linear.b * shade);
118
- }
119
- const ao = face.ao;
120
- const tris = ao[0] + ao[2] >= ao[1] + ao[3] ? [
121
- 0,
122
- 1,
123
- 2,
124
- 0,
125
- 2,
126
- 3
127
- ] : [
128
- 0,
129
- 1,
130
- 3,
131
- 1,
132
- 2,
133
- 3
134
- ];
135
- for (let t = 0; t < 6; t += 3) if (side > 0) indices.push(base + tris[t], base + tris[t + 1], base + tris[t + 2]);
136
- else indices.push(base + tris[t], base + tris[t + 2], base + tris[t + 1]);
137
- };
348
+ }
349
+ const mesh = new MeshData();
138
350
  const keys = [...planes.keys()].sort((a, b) => a - b);
139
351
  for (const key of keys) {
140
352
  const d = Math.floor(key / 2) % 4;
@@ -159,17 +371,10 @@ function buildVoxelGeometry(voxels, options = {}) {
159
371
  }
160
372
  }
161
373
  for (let dv = 0; dv < height; dv++) for (let du = 0; du < width; du++) used.add(cellKey(face.u + du, face.v + dv));
162
- emitQuad(d, side, slice, face, width, height);
374
+ mesh.quad(d, side, slice, face.u, face.v, width, height, face.color, face.ao, aoStrength);
163
375
  }
164
376
  }
165
- const geometry = options.target ?? new BufferGeometry();
166
- geometry.setAttribute("position", new Float32BufferAttribute(positions, 3));
167
- geometry.setAttribute("normal", new Float32BufferAttribute(normals, 3));
168
- geometry.setAttribute("color", new Float32BufferAttribute(colors, 3));
169
- geometry.setIndex(indices);
170
- geometry.computeBoundingBox();
171
- geometry.computeBoundingSphere();
172
- return geometry;
377
+ return mesh;
173
378
  }
174
379
  function cornerOcclusion(solid, front, ud, vd) {
175
380
  const q = [
@@ -207,14 +412,14 @@ function cornerOcclusion(solid, front, ud, vd) {
207
412
  */
208
413
  var VoxelGeometry = class extends BufferGeometry {
209
414
  type = "VoxelGeometry";
210
- /** voxel data (palette indices); the map's default value is empty */
415
+ /** voxel data (palette indices); the source's default value is empty */
211
416
  voxels;
212
417
  /** meshing options used by every `update()` */
213
418
  options;
214
- /** Builds the geometry right away. */
215
- constructor(voxels = new Map3D(), options = {}) {
419
+ /** Builds the geometry right away. Without `voxels`, starts from an empty {@link Map3D}. */
420
+ constructor(voxels, options = {}) {
216
421
  super();
217
- this.voxels = voxels;
422
+ this.voxels = voxels ?? new Map3D();
218
423
  this.options = options;
219
424
  this.update();
220
425
  }
@@ -1,6 +1,6 @@
1
+ import { Material, Mesh } from "three";
1
2
  import { Map3D } from "../map3d.js";
2
3
  import { BuildVoxelGeometryOptions, VoxelGeometry } from "./voxel-geometry.js";
3
- import { Material, Mesh } from "three";
4
4
  /**
5
5
  * Box of voxel cells a {@link VoxelMesh} accepts, from the origin.
6
6
  *
@@ -1,6 +1,6 @@
1
+ import { Mesh, MeshLambertMaterial } from "three";
1
2
  import { Map3D } from "../map3d.js";
2
3
  import { VoxelGeometry } from "./voxel-geometry.js";
3
- import { Mesh, MeshLambertMaterial } from "three";
4
4
  // src/three/voxel-mesh.ts
5
5
  /**
6
6
  * Editable voxel model: `set()` voxels, then `update()` rebuilds the geometry if anything changed.
@@ -1,6 +1,6 @@
1
+ import { Object3D } from "three";
1
2
  import { ThreeEntity } from "./ecs.js";
2
3
  import { ComponentRegistry, ComponentType } from "./schema.js";
3
- import { Object3D } from "three";
4
4
  /**
5
5
  * A scene of entities: three.js objects with components (see {@link CreateThreeEntity}),
6
6
  * tracked by their `Object3D.uuid`.
@@ -1,6 +1,6 @@
1
+ import { MathUtils, Scene } from "three";
1
2
  import { CreateThreeEntity, IsThreeEntity } from "./ecs.js";
2
3
  import { componentToJson, components } from "./schema.js";
3
- import { MathUtils, Scene } from "three";
4
4
  // src/three/world.ts
5
5
  /**
6
6
  * A scene of entities: three.js objects with components (see {@link CreateThreeEntity}),