@n3rdw1z4rd/core 0.24.0 → 0.25.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 (38) hide show
  1. package/dist/astar.js +3 -3
  2. package/dist/dungeon-generator.js +4 -4
  3. package/dist/graph/index.d.ts +21 -0
  4. package/dist/graph/subgraph.js +1 -1
  5. package/dist/graph.cjs +2 -2
  6. package/dist/graph.d.cts +21 -0
  7. package/dist/index.cjs +11 -11
  8. package/dist/index.d.cts +11 -0
  9. package/dist/index.d.ts +11 -0
  10. package/dist/map2d.js +2 -2
  11. package/dist/math.js +2 -2
  12. package/dist/{rng-edL6atS3.cjs → rng-BtvhTJmM.cjs} +2 -2
  13. package/dist/{spatial-partition-3d-D1uyJVhM.cjs → spatial-partition-3d-qhjqIpcy.cjs} +1 -1
  14. package/dist/three/adaptive-grid.d.ts +1 -1
  15. package/dist/three/adaptive-grid.js +1 -1
  16. package/dist/three/asteroid-mesh.d.ts +1 -1
  17. package/dist/three/camera-rig.js +1 -1
  18. package/dist/three/cube-sphere-geometry.d.ts +1 -1
  19. package/dist/three/index.d.ts +13 -2
  20. package/dist/three/particle-system-3d.js +1 -1
  21. package/dist/three/picking.d.ts +1 -1
  22. package/dist/three/picking.js +1 -1
  23. package/dist/three/starfield.js +4 -4
  24. package/dist/three/texture-atlas.d.ts +1 -1
  25. package/dist/three/texture-atlas.js +1 -1
  26. package/dist/three/texture-utils.js +2 -4
  27. package/dist/three/voxel-geometry.d.ts +44 -10
  28. package/dist/three/voxel-geometry.js +282 -77
  29. package/dist/three/voxel-mesh.d.ts +1 -1
  30. package/dist/three/voxel-mesh.js +1 -1
  31. package/dist/three/world.d.ts +1 -1
  32. package/dist/three/world.js +1 -1
  33. package/dist/three.cjs +290 -87
  34. package/dist/three.d.cts +55 -10
  35. package/dist/webgl.js +2 -2
  36. package/package.json +1 -1
  37. package/dist/input/_input.d.ts +0 -23
  38. package/dist/input/_input.js +0 -96
package/dist/astar.js CHANGED
@@ -1,5 +1,5 @@
1
- import { SQRT2 } from "./math.js";
2
1
  import { Heap } from "heap-js";
2
+ import { SQRT2 } from "./math.js";
3
3
  // src/astar.ts
4
4
  const DEFAULT_MAX_NODES = 1e4;
5
5
  const DIRECTIONS = [
@@ -50,7 +50,7 @@ function findPath(start, goal, cost, options = {}) {
50
50
  const dy = Math.abs(y - goal.y);
51
51
  return diagonal ? Math.max(dx, dy) === 1 : dx + dy === 1;
52
52
  };
53
- const passable = (x, y) => cost(x, y) < Infinity;
53
+ const passable = (x, y) => cost(x, y) < 1e400;
54
54
  if (!adjacent && !partial && !passable(goal.x, goal.y) && !(start.x === goal.x && start.y === goal.y)) return [];
55
55
  let seq = 0;
56
56
  const open = new Heap((a, b) => a.f - b.f || a.h - b.h || a.seq - b.seq);
@@ -84,7 +84,7 @@ function findPath(start, goal, cost, options = {}) {
84
84
  const k = key(x, y);
85
85
  if (closed.has(k)) continue;
86
86
  const stepCost = cost(x, y);
87
- if (!(stepCost < Infinity)) continue;
87
+ if (!(stepCost < 1e400)) continue;
88
88
  const isDiagonal = dx !== 0 && dy !== 0;
89
89
  if (isDiagonal && !cutCorners && (!passable(current.x + dx, current.y) || !passable(current.x, current.y + dy))) continue;
90
90
  const g = current.g + (isDiagonal ? stepCost * SQRT2 : stepCost);
@@ -1,10 +1,10 @@
1
1
  import { findPath } from "./astar.js";
2
+ import { Heap } from "heap-js";
2
3
  import { Map2D } from "./map2d.js";
3
4
  import { rng } from "./rng.js";
4
5
  import { log } from "./log.js";
5
6
  import { distance2d, squaredDistance } from "./math.js";
6
7
  import { Rectangle } from "./rectangle.js";
7
- import { Heap } from "heap-js";
8
8
  // src/dungeon-generator.ts
9
9
  /**
10
10
  * Tile value for empty (unused) space.
@@ -178,7 +178,7 @@ function _getShortestPathBetweenRects(rectA, rectB) {
178
178
  let paths = {
179
179
  a: edgeA[0],
180
180
  b: edgeB[0],
181
- distance: Infinity
181
+ distance: 1e400
182
182
  };
183
183
  for (const a of edgeA) for (const b of edgeB) {
184
184
  const d = distance2d(a.x, a.y, b.x, b.y);
@@ -195,13 +195,13 @@ function _getShortestPathBetweenRects(rectA, rectB) {
195
195
  }
196
196
  function _generatePaths(map, rooms, params = {}) {
197
197
  const walkable = params.walkableValues ?? [map.defaultValue];
198
- const cost = (x, y) => walkable.includes(map.get(x, y)) ? 1 : Infinity;
198
+ const cost = (x, y) => walkable.includes(map.get(x, y)) ? 1 : 1e400;
199
199
  const paths = [];
200
200
  _generateMST(rooms, params).forEach((rects) => {
201
201
  const { a, b } = _getShortestPathBetweenRects(rects.a, rects.b);
202
202
  paths.push(findPath(a, b, cost, {
203
203
  adjacent: true,
204
- maxNodes: Infinity
204
+ maxNodes: 1e400
205
205
  }));
206
206
  });
207
207
  return paths;
@@ -1,3 +1,24 @@
1
+ /**
2
+ * Node graphs: a graph document format, typed pins, node types (built-in or written as
3
+ * scripts), and a lazy, memoized evaluator with list broadcasting. No DOM: runs in a page,
4
+ * a worker or Node.
5
+ *
6
+ * ```ts
7
+ * import { createGraph, createNodeRegistry, addNode, connect, GraphEvaluator } from '@n3rdw1z4rd/core/graph';
8
+ *
9
+ * const nodes = createNodeRegistry();
10
+ * const graph = createGraph();
11
+ * const grid = addNode(graph, nodes.get('list/grid2d')!, [0, 0], { cols: 3, rows: 2 });
12
+ * const count = addNode(graph, nodes.get('list/count')!, [240, 0]);
13
+ * connect(graph, nodes, { node: grid.id, port: 'points' }, { node: count.id, port: 'list' });
14
+ *
15
+ * const { results } = await new GraphEvaluator(nodes).evaluate(graph);
16
+ * results.get(count.id)!.outputs.count; // 6
17
+ * ```
18
+ *
19
+ * @packageDocumentation
20
+ * @module @n3rdw1z4rd/core/graph
21
+ */
1
22
  import { ConnectCheck, GRAPH_FORMAT, GRAPH_VERSION, GraphDoc, GraphGroup, GraphLink, GraphNode, PortRef, SubgraphDef, addNode, canConnect, connect, createGraph, disconnect, getNode, graphId, linkInto, linksInto, normalizeGraph, removeNode } from "./doc.js";
2
23
  import { EvaluateOptions, EvaluateResult, FrameInfo, GraphEvaluator, NodeResult, OUTPUT_LISTS } from "./evaluate.js";
3
24
  import { NodeDef, NodeRegistry, NodeSpec, Param, ParamSpec, Port, PortSpec, RunContext, RunFn, WidgetSpec, defineNode } from "./node.js";
@@ -168,7 +168,7 @@ function collapseToSubgraph(doc, registry, nodeIds, title = "Subgraph") {
168
168
  from: { ...l.from }
169
169
  });
170
170
  }
171
- let minX = Infinity, minY = Infinity, sumX = 0, sumY = 0;
171
+ let minX = 1e400, minY = 1e400, sumX = 0, sumY = 0;
172
172
  for (const n of inside) {
173
173
  minX = Math.min(minX, n.pos[0]);
174
174
  minY = Math.min(minY, n.pos[1]);
package/dist/graph.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_rng = require("./rng-edL6atS3.cjs");
2
+ const require_rng = require("./rng-BtvhTJmM.cjs");
3
3
  // src/graph/types.ts
4
4
  /** @category Graph */
5
5
  function parseType(type) {
@@ -1528,7 +1528,7 @@ function collapseToSubgraph(doc, registry, nodeIds, title = "Subgraph") {
1528
1528
  from: { ...l.from }
1529
1529
  });
1530
1530
  }
1531
- let minX = Infinity, minY = Infinity, sumX = 0, sumY = 0;
1531
+ let minX = 1e400, minY = 1e400, sumX = 0, sumY = 0;
1532
1532
  for (const n of inside) {
1533
1533
  minX = Math.min(minX, n.pos[0]);
1534
1534
  minY = Math.min(minY, n.pos[1]);
package/dist/graph.d.cts CHANGED
@@ -1,3 +1,24 @@
1
+ /**
2
+ * Node graphs: a graph document format, typed pins, node types (built-in or written as
3
+ * scripts), and a lazy, memoized evaluator with list broadcasting. No DOM: runs in a page,
4
+ * a worker or Node.
5
+ *
6
+ * ```ts
7
+ * import { createGraph, createNodeRegistry, addNode, connect, GraphEvaluator } from '@n3rdw1z4rd/core/graph';
8
+ *
9
+ * const nodes = createNodeRegistry();
10
+ * const graph = createGraph();
11
+ * const grid = addNode(graph, nodes.get('list/grid2d')!, [0, 0], { cols: 3, rows: 2 });
12
+ * const count = addNode(graph, nodes.get('list/count')!, [240, 0]);
13
+ * connect(graph, nodes, { node: grid.id, port: 'points' }, { node: count.id, port: 'list' });
14
+ *
15
+ * const { results } = await new GraphEvaluator(nodes).evaluate(graph);
16
+ * results.get(count.id)!.outputs.count; // 6
17
+ * ```
18
+ *
19
+ * @packageDocumentation
20
+ * @module @n3rdw1z4rd/core/graph
21
+ */
1
22
  /**
2
23
  * Pin types. A type is a name (`number`, `vec2`, your own…) with a color for the editor;
3
24
  * a trailing `[]` in a port declaration (`vec2[]`) makes the port a **list port** — it takes
package/dist/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_rng = require("./rng-edL6atS3.cjs");
3
- const require_spatial_partition_3d = require("./spatial-partition-3d-D1uyJVhM.cjs");
4
2
  let heap_js = require("heap-js");
3
+ const require_rng = require("./rng-BtvhTJmM.cjs");
4
+ const require_spatial_partition_3d = require("./spatial-partition-3d-qhjqIpcy.cjs");
5
5
  // src/astar.ts
6
6
  const DEFAULT_MAX_NODES = 1e4;
7
7
  const DIRECTIONS = [
@@ -52,7 +52,7 @@ function findPath(start, goal, cost, options = {}) {
52
52
  const dy = Math.abs(y - goal.y);
53
53
  return diagonal ? Math.max(dx, dy) === 1 : dx + dy === 1;
54
54
  };
55
- const passable = (x, y) => cost(x, y) < Infinity;
55
+ const passable = (x, y) => cost(x, y) < 1e400;
56
56
  if (!adjacent && !partial && !passable(goal.x, goal.y) && !(start.x === goal.x && start.y === goal.y)) return [];
57
57
  let seq = 0;
58
58
  const open = new heap_js.Heap((a, b) => a.f - b.f || a.h - b.h || a.seq - b.seq);
@@ -86,7 +86,7 @@ function findPath(start, goal, cost, options = {}) {
86
86
  const k = key$1(x, y);
87
87
  if (closed.has(k)) continue;
88
88
  const stepCost = cost(x, y);
89
- if (!(stepCost < Infinity)) continue;
89
+ if (!(stepCost < 1e400)) continue;
90
90
  const isDiagonal = dx !== 0 && dy !== 0;
91
91
  if (isDiagonal && !cutCorners && (!passable(current.x + dx, current.y) || !passable(current.x, current.y + dy))) continue;
92
92
  const g = current.g + (isDiagonal ? stepCost * require_rng.SQRT2 : stepCost);
@@ -587,7 +587,7 @@ const MAP_2D_MIN_COORD = -1048576;
587
587
  *
588
588
  * @category Maps & Spatial
589
589
  */
590
- const MAP_2D_MAX_COORD = 1048575;
590
+ const MAP_2D_MAX_COORD = 0xfffff;
591
591
  /**
592
592
  * Sparse 2D grid of numbers keyed by integer coordinates. Coordinates are floored, so any point
593
593
  * inside a cell addresses that cell. Cells holding `defaultValue` are not stored: setting a cell to
@@ -630,7 +630,7 @@ var Map2D = class {
630
630
  return [Math.floor(x), Math.floor(y)];
631
631
  }
632
632
  _key(x, y) {
633
- if (!this._warnedOutOfRange && (x < -1048576 || x > 1048575 || y < -1048576 || y > 1048575)) {
633
+ if (!this._warnedOutOfRange && (x < -1048576 || x > 0xfffff || y < -1048576 || y > 0xfffff)) {
634
634
  this._warnedOutOfRange = true;
635
635
  console.warn(`Map2D: (${x}, ${y}) is outside the supported range [${MAP_2D_MIN_COORD}, ${MAP_2D_MAX_COORD}] per axis; it wraps around and collides with other coordinates. Further warnings for this map are suppressed.`);
636
636
  }
@@ -1087,7 +1087,7 @@ function _getShortestPathBetweenRects(rectA, rectB) {
1087
1087
  let paths = {
1088
1088
  a: edgeA[0],
1089
1089
  b: edgeB[0],
1090
- distance: Infinity
1090
+ distance: 1e400
1091
1091
  };
1092
1092
  for (const a of edgeA) for (const b of edgeB) {
1093
1093
  const d = require_rng.distance2d(a.x, a.y, b.x, b.y);
@@ -1104,13 +1104,13 @@ function _getShortestPathBetweenRects(rectA, rectB) {
1104
1104
  }
1105
1105
  function _generatePaths(map, rooms, params = {}) {
1106
1106
  const walkable = params.walkableValues ?? [map.defaultValue];
1107
- const cost = (x, y) => walkable.includes(map.get(x, y)) ? 1 : Infinity;
1107
+ const cost = (x, y) => walkable.includes(map.get(x, y)) ? 1 : 1e400;
1108
1108
  const paths = [];
1109
1109
  _generateMST(rooms, params).forEach((rects) => {
1110
1110
  const { a, b } = _getShortestPathBetweenRects(rects.a, rects.b);
1111
1111
  paths.push(findPath(a, b, cost, {
1112
1112
  adjacent: true,
1113
- maxNodes: Infinity
1113
+ maxNodes: 1e400
1114
1114
  }));
1115
1115
  });
1116
1116
  return paths;
@@ -3584,7 +3584,7 @@ function CreateVertexArray(gl, info, attributes, indices) {
3584
3584
  vao,
3585
3585
  buffers: []
3586
3586
  };
3587
- let vertexCount = Infinity;
3587
+ let vertexCount = 1e400;
3588
3588
  gl.bindVertexArray(vao);
3589
3589
  for (const name in attributes) {
3590
3590
  const attribute = info.attributes[name];
@@ -3624,7 +3624,7 @@ function CreateVertexArray(gl, info, attributes, indices) {
3624
3624
  if (!result.buffers.includes(indices.buffer)) result.buffers.push(indices.buffer);
3625
3625
  }
3626
3626
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, result.indexBuffer);
3627
- } else if (vertexCount < Infinity) result.count = vertexCount;
3627
+ } else if (vertexCount < 1e400) result.count = vertexCount;
3628
3628
  gl.bindVertexArray(null);
3629
3629
  gl.bindBuffer(gl.ARRAY_BUFFER, null);
3630
3630
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
package/dist/index.d.cts CHANGED
@@ -1,3 +1,14 @@
1
+ /**
2
+ * The main entry point: math and RNG, input, pathfinding, maps, ECS, events, color, canvas and
3
+ * raw WebGL helpers. Never imports three.js; see {@link "@n3rdw1z4rd/core/three"} for those helpers.
4
+ *
5
+ * ```ts
6
+ * import { rng, findPath, Input } from '@n3rdw1z4rd/core';
7
+ * ```
8
+ *
9
+ * @packageDocumentation
10
+ * @module @n3rdw1z4rd/core
11
+ */
1
12
  import { _ as XYZ, a as Map3DCell, c as MAP_2D_MIN_COORD, d as ColorPalette, f as RGB, g as XY, h as NoiseFunction3D, i as Map3D, l as Map2D, m as KeyValue, n as MAP_3D_MAX_COORD, o as DEFAULT_MAP_2D_VALUE, p as buildColorPalette, r as MAP_3D_MIN_COORD, s as MAP_2D_MAX_COORD, t as DEFAULT_MAP_3D_VALUE, u as Map2DData } from "./map3d-DRHhbrFn.cjs";
2
13
  /**
3
14
  * Cost of stepping onto tile (x, y). Return `Infinity` for blocked tiles. Costs should be >= 1
package/dist/index.d.ts CHANGED
@@ -1,3 +1,14 @@
1
+ /**
2
+ * The main entry point: math and RNG, input, pathfinding, maps, ECS, events, color, canvas and
3
+ * raw WebGL helpers. Never imports three.js; see {@link "@n3rdw1z4rd/core/three"} for those helpers.
4
+ *
5
+ * ```ts
6
+ * import { rng, findPath, Input } from '@n3rdw1z4rd/core';
7
+ * ```
8
+ *
9
+ * @packageDocumentation
10
+ * @module @n3rdw1z4rd/core
11
+ */
1
12
  import { PathOptions, StepCost, findPath } from "./astar.js";
2
13
  import { CanvasColor, CanvasRenderer, DEFAULT_COLOR, DEFAULT_FONT, DEFAULT_FONT_NAME, DEFAULT_FONT_SIZE, DEFAULT_PIXEL_SIZE, DEFAULT_TEXTALIGN, DEFAULT_TEXTBASELINE, DrawParams, SpriteParams } from "./canvas-renderer.js";
3
14
  import { Clock, ClockCallback, ONE_SECOND } from "./clock.js";
package/dist/map2d.js CHANGED
@@ -19,7 +19,7 @@ const MAP_2D_MIN_COORD = -1048576;
19
19
  *
20
20
  * @category Maps & Spatial
21
21
  */
22
- const MAP_2D_MAX_COORD = 1048575;
22
+ const MAP_2D_MAX_COORD = 0xfffff;
23
23
  /**
24
24
  * Sparse 2D grid of numbers keyed by integer coordinates. Coordinates are floored, so any point
25
25
  * inside a cell addresses that cell. Cells holding `defaultValue` are not stored: setting a cell to
@@ -62,7 +62,7 @@ var Map2D = class {
62
62
  return [Math.floor(x), Math.floor(y)];
63
63
  }
64
64
  _key(x, y) {
65
- if (!this._warnedOutOfRange && (x < -1048576 || x > 1048575 || y < -1048576 || y > 1048575)) {
65
+ if (!this._warnedOutOfRange && (x < -1048576 || x > 0xfffff || y < -1048576 || y > 0xfffff)) {
66
66
  this._warnedOutOfRange = true;
67
67
  console.warn(`Map2D: (${x}, ${y}) is outside the supported range [${MAP_2D_MIN_COORD}, ${MAP_2D_MAX_COORD}] per axis; it wraps around and collides with other coordinates. Further warnings for this map are suppressed.`);
68
68
  }
package/dist/math.js CHANGED
@@ -183,9 +183,9 @@ const roundTo = (value, digits = 0) => {
183
183
  const hash32 = (n) => {
184
184
  n |= 0;
185
185
  n ^= n >>> 16;
186
- n = Math.imul(n, 2246822507);
186
+ n = Math.imul(n, 0x85ebca6b);
187
187
  n ^= n >>> 13;
188
- n = Math.imul(n, 3266489909);
188
+ n = Math.imul(n, 0xc2b2ae35);
189
189
  n ^= n >>> 16;
190
190
  return n >>> 0;
191
191
  };
@@ -183,9 +183,9 @@ const roundTo = (value, digits = 0) => {
183
183
  const hash32 = (n) => {
184
184
  n |= 0;
185
185
  n ^= n >>> 16;
186
- n = Math.imul(n, 2246822507);
186
+ n = Math.imul(n, 0x85ebca6b);
187
187
  n ^= n >>> 13;
188
- n = Math.imul(n, 3266489909);
188
+ n = Math.imul(n, 0xc2b2ae35);
189
189
  n ^= n >>> 16;
190
190
  return n >>> 0;
191
191
  };
@@ -1,4 +1,4 @@
1
- const require_rng = require("./rng-edL6atS3.cjs");
1
+ const require_rng = require("./rng-BtvhTJmM.cjs");
2
2
  // src/log.ts
3
3
  /**
4
4
  * `console.debug` with a bold gray `[dev]` prefix; takes the same arguments. Visible only when the
@@ -1,5 +1,5 @@
1
- import { EditorCamera } from "./editor-camera.js";
2
1
  import { BufferGeometry, ColorRepresentation, LineBasicMaterial, LineSegments } from "three";
2
+ import { EditorCamera } from "./editor-camera.js";
3
3
  /**
4
4
  * An infinite-looking grid on the XY plane for editor views: each {@link AdaptiveGrid.update}
5
5
  * rebuilds just the lines in view, doubling the spacing when zoomed out so lines never get
@@ -53,7 +53,7 @@ var AdaptiveGrid = class extends LineSegments {
53
53
  [view.width / 2, view.height / 2]
54
54
  ].map(([x, y]) => view.screenToWorld(x, y, z)).filter((p) => p !== null);
55
55
  const far = step * this.maxLines / 2;
56
- let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
56
+ let x0 = 1e400, x1 = -1e400, y0 = 1e400, y1 = -1e400;
57
57
  for (const p of pts) {
58
58
  x0 = Math.min(x0, p.x);
59
59
  x1 = Math.max(x1, p.x);
@@ -1,5 +1,5 @@
1
- import { NoiseFunction3D } from "../types.js";
2
1
  import { Mesh } from "three";
2
+ import { NoiseFunction3D } from "../types.js";
3
3
  /**
4
4
  * Options for the {@link AsteroidMesh} constructor.
5
5
  *
@@ -1,5 +1,5 @@
1
- import { clamp, lerp, radians, saturate } from "../math.js";
2
1
  import { Group, PerspectiveCamera, Raycaster, Vector3 } from "three";
2
+ import { clamp, lerp, radians, saturate } from "../math.js";
3
3
  // src/three/camera-rig.ts
4
4
  const DEFAULT_DISTANCE = 5;
5
5
  const damp = (current, target, damping, deltaTime) => damping > 0 ? lerp(current, target, 1 - Math.exp(-damping * deltaTime)) : target;
@@ -1,5 +1,5 @@
1
- import { NoiseFunction3D } from "../types.js";
2
1
  import { BufferGeometry } from "three";
2
+ import { NoiseFunction3D } from "../types.js";
3
3
  /**
4
4
  * A sphere built from a subdivided cube, for planets and other displaced spheres.
5
5
  *
@@ -1,3 +1,14 @@
1
+ /**
2
+ * Three.js helpers: a scene `World` (entities, component schemas, save/load), camera rig, voxel meshing, texture atlases, procedural geometry, particles.
3
+ * Requires the `three` package (an optional peer dependency).
4
+ *
5
+ * ```ts
6
+ * import { CameraRig, buildVoxelGeometry } from '@n3rdw1z4rd/core/three';
7
+ * ```
8
+ *
9
+ * @packageDocumentation
10
+ * @module @n3rdw1z4rd/core/three
11
+ */
1
12
  import { AdaptiveGrid } from "./adaptive-grid.js";
2
13
  import { AsteroidMesh, AsteroidOptions } from "./asteroid-mesh.js";
3
14
  import { CameraRig, CameraRigParams, PitchLimits } from "./camera-rig.js";
@@ -11,7 +22,7 @@ import { ComponentRegistry, ComponentSchema, ComponentType, FieldDef, FieldType,
11
22
  import { CreateStarfield, CreateSystemStar } from "./starfield.js";
12
23
  import { AtlasParams, TextureAtlas, UV } from "./texture-atlas.js";
13
24
  import { CreateSubTexture, LoadTexture, TextureData } from "./texture-utils.js";
14
- import { BuildVoxelGeometryOptions, VoxelGeometry, buildVoxelGeometry } from "./voxel-geometry.js";
25
+ import { BuildVoxelGeometryOptions, VoxelGeometry, VoxelSource, buildVoxelGeometry } from "./voxel-geometry.js";
15
26
  import { VoxelBounds, VoxelMesh, VoxelMeshOptions } from "./voxel-mesh.js";
16
27
  import { ComponentRef, EntityDoc, SCENE_FORMAT, SCENE_VERSION, SceneDoc, World, WorldEvents, WorldSystem } from "./world.js";
17
- export { AdaptiveGrid, AddThreeComponent, AsteroidMesh, AsteroidOptions, AtlasParams, BuildVoxelGeometryOptions, CameraRig, CameraRigParams, ComponentRef, ComponentRegistry, ComponentSchema, ComponentType, CreateStarfield, CreateSubTexture, CreateSystemStar, CreateThreeEntity, CubeSphereGeometry, EditorCamera, EntityDoc, EntityHit, FieldDef, FieldType, GenerateEntityId, GetThreeComponent, IsThreeEntity, LoadTexture, ParticleSystem3d, PickOptions, PitchLimits, RemoveThreeComponent, Renderer, SCENE_FORMAT, SCENE_VERSION, SceneDoc, TextureAtlas, TextureData, ThreeComponent, ThreeEntity, ThreeSystem, UV, ViewInsets, VoxelBounds, VoxelGeometry, VoxelMesh, VoxelMeshOptions, World, WorldEvents, WorldSystem, applyFieldValue, buildVoxelGeometry, component, componentToJson, componentType, components, defineComponent, entityOf, field, fieldToJson, inferFieldType, pickEntity };
28
+ export { AdaptiveGrid, AddThreeComponent, AsteroidMesh, AsteroidOptions, AtlasParams, BuildVoxelGeometryOptions, CameraRig, CameraRigParams, ComponentRef, ComponentRegistry, ComponentSchema, ComponentType, CreateStarfield, CreateSubTexture, CreateSystemStar, CreateThreeEntity, CubeSphereGeometry, EditorCamera, EntityDoc, EntityHit, FieldDef, FieldType, GenerateEntityId, GetThreeComponent, IsThreeEntity, LoadTexture, ParticleSystem3d, PickOptions, PitchLimits, RemoveThreeComponent, Renderer, SCENE_FORMAT, SCENE_VERSION, SceneDoc, TextureAtlas, TextureData, ThreeComponent, ThreeEntity, ThreeSystem, UV, ViewInsets, VoxelBounds, VoxelGeometry, VoxelMesh, VoxelMeshOptions, VoxelSource, World, WorldEvents, WorldSystem, applyFieldValue, buildVoxelGeometry, component, componentToJson, componentType, components, defineComponent, entityOf, field, fieldToJson, inferFieldType, pickEntity };
@@ -1,9 +1,9 @@
1
+ import { BufferAttribute, BufferGeometry, DynamicDrawUsage, Group, Points, ShaderMaterial, Sphere, Vector3 } from "three";
1
2
  import { Random } from "../random.js";
2
3
  import { fract } from "../math.js";
3
4
  import { SpatialPartition3d } from "../spatial-partition-3d.js";
4
5
  import { rng } from "../rng.js";
5
6
  import { randomMatrix } from "../particle-system-2d.js";
6
- import { BufferAttribute, BufferGeometry, DynamicDrawUsage, Group, Points, ShaderMaterial, Sphere, Vector3 } from "three";
7
7
  // src/three/particle-system-3d.ts
8
8
  const vertexShader = `
9
9
  uniform float pointSize;
@@ -1,7 +1,7 @@
1
+ import { Object3D, Vector3 } from "three";
1
2
  import { ThreeEntity } from "./ecs.js";
2
3
  import { EditorCamera } from "./editor-camera.js";
3
4
  import { World } from "./world.js";
4
- import { Object3D, Vector3 } from "three";
5
5
  /** @category Three.js */
6
6
  export interface EntityHit {
7
7
  entity: ThreeEntity;
@@ -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
  }