@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.
- package/README.md +45 -0
- package/dist/astar.js +3 -3
- package/dist/desktop.d.ts +37 -0
- package/dist/desktop.js +14 -0
- package/dist/dungeon-generator.js +4 -4
- package/dist/electron/after-pack.cjs +17 -0
- package/dist/electron/index.d.ts +110 -0
- package/dist/electron/index.js +257 -0
- package/dist/electron/preload.cjs +12 -0
- package/dist/electron/updater.d.ts +81 -0
- package/dist/electron/updater.js +133 -0
- package/dist/electron.cjs +416 -0
- package/dist/electron.d.cts +189 -0
- package/dist/graph/index.d.ts +21 -0
- package/dist/graph/subgraph.js +1 -1
- package/dist/graph.cjs +2 -2
- package/dist/graph.d.cts +21 -0
- package/dist/index.cjs +25 -11
- package/dist/index.d.cts +48 -0
- package/dist/index.d.ts +13 -1
- package/dist/index.js +2 -1
- package/dist/map2d.js +2 -2
- package/dist/math.js +2 -2
- package/dist/{rng-edL6atS3.cjs → rng-BtvhTJmM.cjs} +2 -2
- package/dist/{spatial-partition-3d-D1uyJVhM.cjs → spatial-partition-3d-qhjqIpcy.cjs} +1 -1
- package/dist/three/adaptive-grid.d.ts +1 -1
- package/dist/three/adaptive-grid.js +1 -1
- package/dist/three/asteroid-mesh.d.ts +1 -1
- package/dist/three/camera-rig.js +1 -1
- package/dist/three/cube-sphere-geometry.d.ts +1 -1
- package/dist/three/index.d.ts +13 -2
- package/dist/three/particle-system-3d.js +1 -1
- package/dist/three/picking.d.ts +1 -1
- package/dist/three/picking.js +1 -1
- package/dist/three/starfield.js +4 -4
- package/dist/three/texture-atlas.d.ts +1 -1
- package/dist/three/texture-atlas.js +1 -1
- package/dist/three/texture-utils.js +2 -4
- package/dist/three/voxel-geometry.d.ts +44 -10
- package/dist/three/voxel-geometry.js +282 -77
- package/dist/three/voxel-mesh.d.ts +1 -1
- package/dist/three/voxel-mesh.js +1 -1
- package/dist/three/world.d.ts +1 -1
- package/dist/three/world.js +1 -1
- package/dist/three.cjs +290 -87
- package/dist/three.d.cts +55 -10
- package/dist/vite.cjs +124 -0
- package/dist/vite.d.cts +53 -2
- package/dist/vite.d.ts +53 -2
- package/dist/vite.js +122 -1
- package/dist/webgl.js +2 -2
- package/package.json +23 -3
- package/dist/input/_input.d.ts +0 -23
- package/dist/input/_input.js +0 -96
package/dist/three.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 three = require("three");
|
|
3
|
+
const require_rng = require("./rng-BtvhTJmM.cjs");
|
|
4
|
+
const require_spatial_partition_3d = require("./spatial-partition-3d-qhjqIpcy.cjs");
|
|
5
5
|
// src/three/adaptive-grid.ts
|
|
6
6
|
/**
|
|
7
7
|
* An infinite-looking grid on the XY plane for editor views: each {@link AdaptiveGrid.update}
|
|
@@ -56,7 +56,7 @@ var AdaptiveGrid = class extends three.LineSegments {
|
|
|
56
56
|
[view.width / 2, view.height / 2]
|
|
57
57
|
].map(([x, y]) => view.screenToWorld(x, y, z)).filter((p) => p !== null);
|
|
58
58
|
const far = step * this.maxLines / 2;
|
|
59
|
-
let x0 =
|
|
59
|
+
let x0 = 1e400, x1 = -1e400, y0 = 1e400, y1 = -1e400;
|
|
60
60
|
for (const p of pts) {
|
|
61
61
|
x0 = Math.min(x0, p.x);
|
|
62
62
|
x1 = Math.max(x1, p.x);
|
|
@@ -1058,7 +1058,7 @@ function pickEntity(world, view, sx, sy, options = {}) {
|
|
|
1058
1058
|
const direct = castAt(world, view, sx, sy, options.filter);
|
|
1059
1059
|
if (direct || !options.slopPx) return direct;
|
|
1060
1060
|
let best = null;
|
|
1061
|
-
let bestD =
|
|
1061
|
+
let bestD = 1e400;
|
|
1062
1062
|
for (const r of [options.slopPx / 2, options.slopPx]) {
|
|
1063
1063
|
for (let k = 0; k < 8; k++) {
|
|
1064
1064
|
const a = k / 8 * Math.PI * 2;
|
|
@@ -1381,7 +1381,7 @@ const components = new ComponentRegistry();
|
|
|
1381
1381
|
* @param seed seeds a private {@link Rng} for a reproducible starfield
|
|
1382
1382
|
* @category Geometry & Meshes
|
|
1383
1383
|
*/
|
|
1384
|
-
function CreateStarfield(radius = 1e3, count = 5e3, color =
|
|
1384
|
+
function CreateStarfield(radius = 1e3, count = 5e3, color = 0xffffff, seed) {
|
|
1385
1385
|
const random = seed === void 0 ? require_rng.rng : new require_rng.Rng(seed);
|
|
1386
1386
|
const geometry = new three.BufferGeometry();
|
|
1387
1387
|
const positions = new Float32Array(count * 3);
|
|
@@ -1420,11 +1420,11 @@ function CreateSystemStar(position) {
|
|
|
1420
1420
|
const star = new three.Group();
|
|
1421
1421
|
const starPos = position ?? new three.Vector3();
|
|
1422
1422
|
const starGeo = new three.SphereGeometry(50, 32, 32);
|
|
1423
|
-
const starMat = new three.MeshBasicMaterial({ color:
|
|
1423
|
+
const starMat = new three.MeshBasicMaterial({ color: 0xffddaa });
|
|
1424
1424
|
const starMesh = new three.Mesh(starGeo, starMat);
|
|
1425
1425
|
starMesh.position.copy(starPos);
|
|
1426
1426
|
star.add(starMesh);
|
|
1427
|
-
const starLight = new three.PointLight(
|
|
1427
|
+
const starLight = new three.PointLight(0xffffff, 10, 0, 0);
|
|
1428
1428
|
starLight.position.copy(starPos);
|
|
1429
1429
|
star.add(starLight);
|
|
1430
1430
|
return star;
|
|
@@ -1439,11 +1439,9 @@ function CreateSystemStar(position) {
|
|
|
1439
1439
|
function LoadTexture(url) {
|
|
1440
1440
|
return new Promise((res, rej) => {
|
|
1441
1441
|
new three.TextureLoader().load(url, (texture) => {
|
|
1442
|
-
const width = texture.source.data.width;
|
|
1443
|
-
const height = texture.source.data.height;
|
|
1444
1442
|
res({
|
|
1445
|
-
width,
|
|
1446
|
-
height,
|
|
1443
|
+
width: texture.source.data.width,
|
|
1444
|
+
height: texture.source.data.height,
|
|
1447
1445
|
texture
|
|
1448
1446
|
});
|
|
1449
1447
|
}, (_ev) => {}, (err) => rej(err));
|
|
@@ -1556,11 +1554,12 @@ const planeKey = (d, side, slice) => (slice + 33554432) * 8 + d * 2 + (side > 0
|
|
|
1556
1554
|
/**
|
|
1557
1555
|
* Builds a mesh for the voxels in `voxels`: one quad per visible face, merged into larger quads
|
|
1558
1556
|
* (`greedy`) and shaded with per-vertex ambient occlusion. A voxel's value is its palette index;
|
|
1559
|
-
* the
|
|
1557
|
+
* the source's default value means empty. `voxels` is a {@link Map3D} or any other
|
|
1558
|
+
* {@link VoxelSource}.
|
|
1560
1559
|
*
|
|
1561
|
-
* Positions are in the
|
|
1560
|
+
* Positions are in the source's own coordinates, so a chunk stored in chunk-local coordinates is
|
|
1562
1561
|
* placed by positioning its mesh. Pass `neighbor` so faces against neighbouring chunks are culled
|
|
1563
|
-
* and their voxels count for ambient occlusion.
|
|
1562
|
+
* and their voxels count for ambient occlusion (or give a source whose `get` already sees them).
|
|
1564
1563
|
*
|
|
1565
1564
|
* The geometry has position, normal and color attributes and an index; use a material with
|
|
1566
1565
|
* `vertexColors: true`. The output is deterministic: the same voxels and options always produce
|
|
@@ -1579,31 +1578,290 @@ const planeKey = (d, side, slice) => (slice + 33554432) * 8 + d * 2 + (side > 0
|
|
|
1579
1578
|
* @category Voxels
|
|
1580
1579
|
*/
|
|
1581
1580
|
function buildVoxelGeometry(voxels, options = {}) {
|
|
1581
|
+
const settings = resolveOptions(voxels, options);
|
|
1582
|
+
const strategy = options.strategy ?? "auto";
|
|
1583
|
+
const xs = [], ys = [], zs = [], values = [];
|
|
1584
|
+
const min = [
|
|
1585
|
+
1e400,
|
|
1586
|
+
1e400,
|
|
1587
|
+
1e400
|
|
1588
|
+
], max = [
|
|
1589
|
+
-1e400,
|
|
1590
|
+
-1e400,
|
|
1591
|
+
-1e400
|
|
1592
|
+
];
|
|
1593
|
+
voxels.forEach((x, y, z, value) => {
|
|
1594
|
+
xs.push(x);
|
|
1595
|
+
ys.push(y);
|
|
1596
|
+
zs.push(z);
|
|
1597
|
+
values.push(value);
|
|
1598
|
+
if (x < min[0]) min[0] = x;
|
|
1599
|
+
if (x > max[0]) max[0] = x;
|
|
1600
|
+
if (y < min[1]) min[1] = y;
|
|
1601
|
+
if (y > max[1]) max[1] = y;
|
|
1602
|
+
if (z < min[2]) min[2] = z;
|
|
1603
|
+
if (z > max[2]) max[2] = z;
|
|
1604
|
+
});
|
|
1605
|
+
const cells = xs.length ? (max[0] - min[0] + 3) * (max[1] - min[1] + 3) * (max[2] - min[2] + 3) : 0;
|
|
1606
|
+
const dense = strategy === "dense" || strategy === "auto" && cells <= Math.max(DENSE_MIN_CELLS, xs.length * DENSE_CELLS_PER_VOXEL);
|
|
1607
|
+
const mesh = !xs.length ? new MeshData() : dense ? meshDense(voxels, settings, xs, ys, zs, values, min, max) : meshSparse(voxels, settings, xs, ys, zs, values);
|
|
1608
|
+
const geometry = options.target ?? new three.BufferGeometry();
|
|
1609
|
+
geometry.setAttribute("position", new three.Float32BufferAttribute(mesh.positions.subarray(0, mesh.vertices * 3), 3));
|
|
1610
|
+
geometry.setAttribute("normal", new three.Float32BufferAttribute(mesh.normals.subarray(0, mesh.vertices * 3), 3));
|
|
1611
|
+
geometry.setAttribute("color", new three.Float32BufferAttribute(mesh.colors.subarray(0, mesh.vertices * 3), 3));
|
|
1612
|
+
const indices = mesh.indices.subarray(0, mesh.indexCount);
|
|
1613
|
+
geometry.setIndex(mesh.vertices > 65535 ? new three.Uint32BufferAttribute(indices.slice(), 1) : new three.Uint16BufferAttribute(Uint16Array.from(indices), 1));
|
|
1614
|
+
geometry.computeBoundingBox();
|
|
1615
|
+
geometry.computeBoundingSphere();
|
|
1616
|
+
return geometry;
|
|
1617
|
+
}
|
|
1618
|
+
const DENSE_CELLS_PER_VOXEL = 64;
|
|
1619
|
+
const DENSE_MIN_CELLS = 262144;
|
|
1620
|
+
function resolveOptions(voxels, options) {
|
|
1582
1621
|
const palette = options.palette ?? [];
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1622
|
+
return {
|
|
1623
|
+
empty: voxels.defaultValue,
|
|
1624
|
+
neighbor: options.neighbor,
|
|
1625
|
+
includeHiddenFaces: options.includeHiddenFaces ?? false,
|
|
1626
|
+
greedy: options.greedy ?? true,
|
|
1627
|
+
ambientOcclusion: options.ambientOcclusion ?? true,
|
|
1628
|
+
aoStrength: options.aoStrength ?? .5,
|
|
1629
|
+
colorOf: (value) => palette[value] ?? options.missingColor ?? require_rng.Random.uint(value, 1) & 0xffffff
|
|
1630
|
+
};
|
|
1631
|
+
}
|
|
1632
|
+
/** growable output buffers; one quad at a time */
|
|
1633
|
+
var MeshData = class {
|
|
1634
|
+
positions = /* @__PURE__ */ new Float32Array(3072);
|
|
1635
|
+
normals = /* @__PURE__ */ new Float32Array(3072);
|
|
1636
|
+
colors = /* @__PURE__ */ new Float32Array(3072);
|
|
1637
|
+
indices = /* @__PURE__ */ new Uint32Array(1536);
|
|
1638
|
+
vertices = 0;
|
|
1639
|
+
indexCount = 0;
|
|
1640
|
+
corner = [
|
|
1641
|
+
0,
|
|
1642
|
+
0,
|
|
1643
|
+
0
|
|
1644
|
+
];
|
|
1645
|
+
linear = new three.Color();
|
|
1646
|
+
linearOf = /* @__PURE__ */ new Map();
|
|
1647
|
+
/**
|
|
1648
|
+
* One quad on plane (d, side, slice), from (u, v) to (u + width, v + height); `ao` per corner
|
|
1649
|
+
* (0,0) (1,0) (1,1) (0,1), 0 = most occluded.
|
|
1650
|
+
*/
|
|
1651
|
+
quad(d, side, slice, u, v, width, height, color, ao, aoStrength) {
|
|
1652
|
+
if ((this.vertices + 4) * 3 > this.positions.length) {
|
|
1653
|
+
this.positions = grow(this.positions);
|
|
1654
|
+
this.normals = grow(this.normals);
|
|
1655
|
+
this.colors = grow(this.colors);
|
|
1656
|
+
}
|
|
1657
|
+
if (this.indexCount + 6 > this.indices.length) this.indices = grow(this.indices);
|
|
1658
|
+
let rgb = this.linearOf.get(color);
|
|
1659
|
+
if (!rgb) {
|
|
1660
|
+
this.linear.setHex(color);
|
|
1661
|
+
rgb = [
|
|
1662
|
+
this.linear.r,
|
|
1663
|
+
this.linear.g,
|
|
1664
|
+
this.linear.b
|
|
1665
|
+
];
|
|
1666
|
+
this.linearOf.set(color, rgb);
|
|
1667
|
+
}
|
|
1668
|
+
const ud = (d + 1) % 3;
|
|
1669
|
+
const vd = (d + 2) % 3;
|
|
1670
|
+
const base = this.vertices;
|
|
1671
|
+
const corner = this.corner;
|
|
1672
|
+
for (let c = 0; c < 4; c++) {
|
|
1673
|
+
corner[d] = slice;
|
|
1674
|
+
corner[ud] = u + (c === 1 || c === 2 ? width : 0);
|
|
1675
|
+
corner[vd] = v + (c >= 2 ? height : 0);
|
|
1676
|
+
const i = (base + c) * 3;
|
|
1677
|
+
const shade = 1 - aoStrength * (3 - ao[c]) / 3;
|
|
1678
|
+
this.positions[i] = corner[0];
|
|
1679
|
+
this.positions[i + 1] = corner[1];
|
|
1680
|
+
this.positions[i + 2] = corner[2];
|
|
1681
|
+
this.normals[i] = d === 0 ? side : 0;
|
|
1682
|
+
this.normals[i + 1] = d === 1 ? side : 0;
|
|
1683
|
+
this.normals[i + 2] = d === 2 ? side : 0;
|
|
1684
|
+
this.colors[i] = rgb[0] * shade;
|
|
1685
|
+
this.colors[i + 1] = rgb[1] * shade;
|
|
1686
|
+
this.colors[i + 2] = rgb[2] * shade;
|
|
1687
|
+
}
|
|
1688
|
+
this.vertices += 4;
|
|
1689
|
+
const tris = ao[0] + ao[2] >= ao[1] + ao[3] ? TRIS_A : TRIS_B;
|
|
1690
|
+
for (let t = 0; t < 6; t += 3) {
|
|
1691
|
+
this.indices[this.indexCount++] = base + tris[t];
|
|
1692
|
+
this.indices[this.indexCount++] = base + tris[side > 0 ? t + 1 : t + 2];
|
|
1693
|
+
this.indices[this.indexCount++] = base + tris[side > 0 ? t + 2 : t + 1];
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
};
|
|
1697
|
+
const TRIS_A = [
|
|
1698
|
+
0,
|
|
1699
|
+
1,
|
|
1700
|
+
2,
|
|
1701
|
+
0,
|
|
1702
|
+
2,
|
|
1703
|
+
3
|
|
1704
|
+
];
|
|
1705
|
+
const TRIS_B = [
|
|
1706
|
+
0,
|
|
1707
|
+
1,
|
|
1708
|
+
3,
|
|
1709
|
+
1,
|
|
1710
|
+
2,
|
|
1711
|
+
3
|
|
1712
|
+
];
|
|
1713
|
+
function grow(array) {
|
|
1714
|
+
const bigger = new array.constructor(array.length * 2);
|
|
1715
|
+
bigger.set(array);
|
|
1716
|
+
return bigger;
|
|
1717
|
+
}
|
|
1718
|
+
/**
|
|
1719
|
+
* The bounding box (plus a 1-cell border) copied into flat arrays once: which cells are solid,
|
|
1720
|
+
* then each plane's faces as a grid of signatures, merged by scanning it.
|
|
1721
|
+
*/
|
|
1722
|
+
function meshDense(voxels, settings, xs, ys, zs, values, min, max) {
|
|
1723
|
+
const { empty, neighbor, includeHiddenFaces, greedy, ambientOcclusion, aoStrength, colorOf } = settings;
|
|
1724
|
+
const W = max[0] - min[0] + 3, H = max[1] - min[1] + 3, D = max[2] - min[2] + 3;
|
|
1725
|
+
const X0 = min[0] - 1, Y0 = min[1] - 1, Z0 = min[2] - 1;
|
|
1726
|
+
const stride = [
|
|
1727
|
+
1,
|
|
1728
|
+
W,
|
|
1729
|
+
W * H
|
|
1730
|
+
];
|
|
1731
|
+
const solid = new Uint8Array(W * H * D);
|
|
1732
|
+
const listed = new Uint8Array(W * H * D);
|
|
1733
|
+
for (let n = 0; n < xs.length; n++) {
|
|
1734
|
+
const i = xs[n] - X0 + (ys[n] - Y0) * W + (zs[n] - Z0) * W * H;
|
|
1735
|
+
listed[i] = 1;
|
|
1736
|
+
if (values[n] !== empty) solid[i] = 1;
|
|
1737
|
+
}
|
|
1738
|
+
const complete = voxels instanceof require_spatial_partition_3d.Map3D;
|
|
1739
|
+
if (!complete || neighbor) {
|
|
1740
|
+
let i = 0;
|
|
1741
|
+
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++) {
|
|
1742
|
+
if (solid[i]) continue;
|
|
1743
|
+
if (listed[i] && complete) {
|
|
1744
|
+
if (neighbor && neighbor(x, y, z) !== empty) solid[i] = 1;
|
|
1745
|
+
continue;
|
|
1746
|
+
}
|
|
1747
|
+
let value = complete ? empty : voxels.get(x, y, z);
|
|
1748
|
+
if (value === empty && neighbor) value = neighbor(x, y, z);
|
|
1749
|
+
if (value !== empty) solid[i] = 1;
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
const planes = /* @__PURE__ */ new Map();
|
|
1753
|
+
const size = [
|
|
1754
|
+
W - 2,
|
|
1755
|
+
H - 2,
|
|
1756
|
+
D - 2
|
|
1757
|
+
];
|
|
1758
|
+
const p = [
|
|
1759
|
+
0,
|
|
1760
|
+
0,
|
|
1761
|
+
0
|
|
1762
|
+
];
|
|
1763
|
+
const ao = [
|
|
1764
|
+
3,
|
|
1765
|
+
3,
|
|
1766
|
+
3,
|
|
1767
|
+
3
|
|
1768
|
+
];
|
|
1769
|
+
for (let n = 0; n < xs.length; n++) {
|
|
1770
|
+
p[0] = xs[n];
|
|
1771
|
+
p[1] = ys[n];
|
|
1772
|
+
p[2] = zs[n];
|
|
1773
|
+
const i = p[0] - X0 + (p[1] - Y0) * W + (p[2] - Z0) * W * H;
|
|
1774
|
+
const color = colorOf(values[n]);
|
|
1775
|
+
for (let d = 0; d < 3; d++) {
|
|
1776
|
+
const ud = (d + 1) % 3, vd = (d + 2) % 3;
|
|
1777
|
+
const su = stride[ud], sv = stride[vd];
|
|
1778
|
+
for (const side of SIDES) {
|
|
1779
|
+
const f = i + side * stride[d];
|
|
1780
|
+
if (!includeHiddenFaces && solid[f]) continue;
|
|
1781
|
+
if (ambientOcclusion) {
|
|
1782
|
+
const um = solid[f - su], up = solid[f + su], vm = solid[f - sv], vp = solid[f + sv];
|
|
1783
|
+
ao[0] = um && vm ? 0 : 3 - (um + vm + solid[f - su - sv]);
|
|
1784
|
+
ao[1] = up && vm ? 0 : 3 - (up + vm + solid[f + su - sv]);
|
|
1785
|
+
ao[2] = up && vp ? 0 : 3 - (up + vp + solid[f + su + sv]);
|
|
1786
|
+
ao[3] = um && vp ? 0 : 3 - (um + vp + solid[f - su + sv]);
|
|
1787
|
+
}
|
|
1788
|
+
const slice = p[d] + (side > 0 ? 1 : 0);
|
|
1789
|
+
const key = planeKey(d, side, slice);
|
|
1790
|
+
let plane = planes.get(key);
|
|
1791
|
+
if (!plane) {
|
|
1792
|
+
const nu = size[ud], nv = size[vd];
|
|
1793
|
+
plane = {
|
|
1794
|
+
d,
|
|
1795
|
+
side,
|
|
1796
|
+
slice,
|
|
1797
|
+
nu,
|
|
1798
|
+
nv,
|
|
1799
|
+
sig: new Float64Array(nu * nv).fill(-1)
|
|
1800
|
+
};
|
|
1801
|
+
planes.set(key, plane);
|
|
1802
|
+
}
|
|
1803
|
+
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];
|
|
1804
|
+
}
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
const mesh = new MeshData();
|
|
1808
|
+
const cornerAo = [
|
|
1809
|
+
0,
|
|
1810
|
+
0,
|
|
1811
|
+
0,
|
|
1812
|
+
0
|
|
1813
|
+
];
|
|
1814
|
+
for (const key of [...planes.keys()].sort((a, b) => a - b)) {
|
|
1815
|
+
const { d, side, slice, nu, nv, sig } = planes.get(key);
|
|
1816
|
+
const ud = (d + 1) % 3, vd = (d + 2) % 3;
|
|
1817
|
+
const used = new Uint8Array(nu * nv);
|
|
1818
|
+
for (let v = 0; v < nv; v++) for (let u = 0; u < nu; u++) {
|
|
1819
|
+
const k = v * nu + u, s = sig[k];
|
|
1820
|
+
if (s < 0 || used[k]) continue;
|
|
1821
|
+
let width = 1, height = 1;
|
|
1822
|
+
if (greedy) {
|
|
1823
|
+
while (u + width < nu && sig[k + width] === s && !used[k + width]) width++;
|
|
1824
|
+
grow: while (v + height < nv) {
|
|
1825
|
+
for (let du = 0; du < width; du++) {
|
|
1826
|
+
const kk = (v + height) * nu + u + du;
|
|
1827
|
+
if (sig[kk] !== s || used[kk]) break grow;
|
|
1828
|
+
}
|
|
1829
|
+
height++;
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
for (let dv = 0; dv < height; dv++) used.fill(1, (v + dv) * nu + u, (v + dv) * nu + u + width);
|
|
1833
|
+
const bits = s % 256;
|
|
1834
|
+
cornerAo[0] = bits >> 6;
|
|
1835
|
+
cornerAo[1] = bits >> 4 & 3;
|
|
1836
|
+
cornerAo[2] = bits >> 2 & 3;
|
|
1837
|
+
cornerAo[3] = bits & 3;
|
|
1838
|
+
mesh.quad(d, side, slice, min[ud] + u, min[vd] + v, width, height, (s - bits) / 256, cornerAo, aoStrength);
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
return mesh;
|
|
1842
|
+
}
|
|
1843
|
+
const SIDES = [1, -1];
|
|
1844
|
+
/** Faces kept in hash maps: for voxels spread so far apart that their bounding box would be huge. */
|
|
1845
|
+
function meshSparse(voxels, settings, xs, ys, zs, values) {
|
|
1846
|
+
const { empty, neighbor, includeHiddenFaces, greedy, ambientOcclusion, aoStrength, colorOf } = settings;
|
|
1589
1847
|
const solid = (x, y, z) => {
|
|
1590
1848
|
let value = voxels.get(x, y, z);
|
|
1591
1849
|
if (value === empty && neighbor) value = neighbor(x, y, z);
|
|
1592
1850
|
return value !== empty;
|
|
1593
1851
|
};
|
|
1594
|
-
const colorOf = (value) => palette[value] ?? options.missingColor ?? require_rng.Random.uint(value, 1) & 16777215;
|
|
1595
1852
|
const planes = /* @__PURE__ */ new Map();
|
|
1596
1853
|
const p = [
|
|
1597
1854
|
0,
|
|
1598
1855
|
0,
|
|
1599
1856
|
0
|
|
1600
1857
|
];
|
|
1601
|
-
|
|
1602
|
-
const
|
|
1858
|
+
for (let n = 0; n < xs.length; n++) {
|
|
1859
|
+
const x = xs[n], y = ys[n], z = zs[n];
|
|
1860
|
+
const color = colorOf(values[n]);
|
|
1603
1861
|
for (let d = 0; d < 3; d++) {
|
|
1604
1862
|
const ud = (d + 1) % 3;
|
|
1605
1863
|
const vd = (d + 2) % 3;
|
|
1606
|
-
for (const side of
|
|
1864
|
+
for (const side of SIDES) {
|
|
1607
1865
|
p[0] = x;
|
|
1608
1866
|
p[1] = y;
|
|
1609
1867
|
p[2] = z;
|
|
@@ -1634,56 +1892,8 @@ function buildVoxelGeometry(voxels, options = {}) {
|
|
|
1634
1892
|
});
|
|
1635
1893
|
}
|
|
1636
1894
|
}
|
|
1637
|
-
}
|
|
1638
|
-
const
|
|
1639
|
-
const normals = [];
|
|
1640
|
-
const colors = [];
|
|
1641
|
-
const indices = [];
|
|
1642
|
-
const linear = new three.Color();
|
|
1643
|
-
const corner = [
|
|
1644
|
-
0,
|
|
1645
|
-
0,
|
|
1646
|
-
0
|
|
1647
|
-
];
|
|
1648
|
-
const emitQuad = (d, side, slice, face, width, height) => {
|
|
1649
|
-
const ud = (d + 1) % 3;
|
|
1650
|
-
const vd = (d + 2) % 3;
|
|
1651
|
-
const base = positions.length / 3;
|
|
1652
|
-
linear.setHex(face.color);
|
|
1653
|
-
const cornerOffsets = [
|
|
1654
|
-
[0, 0],
|
|
1655
|
-
[width, 0],
|
|
1656
|
-
[width, height],
|
|
1657
|
-
[0, height]
|
|
1658
|
-
];
|
|
1659
|
-
for (let c = 0; c < 4; c++) {
|
|
1660
|
-
corner[d] = slice;
|
|
1661
|
-
corner[ud] = face.u + cornerOffsets[c][0];
|
|
1662
|
-
corner[vd] = face.v + cornerOffsets[c][1];
|
|
1663
|
-
positions.push(corner[0], corner[1], corner[2]);
|
|
1664
|
-
normals.push(d === 0 ? side : 0, d === 1 ? side : 0, d === 2 ? side : 0);
|
|
1665
|
-
const shade = 1 - aoStrength * (3 - face.ao[c]) / 3;
|
|
1666
|
-
colors.push(linear.r * shade, linear.g * shade, linear.b * shade);
|
|
1667
|
-
}
|
|
1668
|
-
const ao = face.ao;
|
|
1669
|
-
const tris = ao[0] + ao[2] >= ao[1] + ao[3] ? [
|
|
1670
|
-
0,
|
|
1671
|
-
1,
|
|
1672
|
-
2,
|
|
1673
|
-
0,
|
|
1674
|
-
2,
|
|
1675
|
-
3
|
|
1676
|
-
] : [
|
|
1677
|
-
0,
|
|
1678
|
-
1,
|
|
1679
|
-
3,
|
|
1680
|
-
1,
|
|
1681
|
-
2,
|
|
1682
|
-
3
|
|
1683
|
-
];
|
|
1684
|
-
for (let t = 0; t < 6; t += 3) if (side > 0) indices.push(base + tris[t], base + tris[t + 1], base + tris[t + 2]);
|
|
1685
|
-
else indices.push(base + tris[t], base + tris[t + 2], base + tris[t + 1]);
|
|
1686
|
-
};
|
|
1895
|
+
}
|
|
1896
|
+
const mesh = new MeshData();
|
|
1687
1897
|
const keys = [...planes.keys()].sort((a, b) => a - b);
|
|
1688
1898
|
for (const key of keys) {
|
|
1689
1899
|
const d = Math.floor(key / 2) % 4;
|
|
@@ -1708,17 +1918,10 @@ function buildVoxelGeometry(voxels, options = {}) {
|
|
|
1708
1918
|
}
|
|
1709
1919
|
}
|
|
1710
1920
|
for (let dv = 0; dv < height; dv++) for (let du = 0; du < width; du++) used.add(cellKey(face.u + du, face.v + dv));
|
|
1711
|
-
|
|
1921
|
+
mesh.quad(d, side, slice, face.u, face.v, width, height, face.color, face.ao, aoStrength);
|
|
1712
1922
|
}
|
|
1713
1923
|
}
|
|
1714
|
-
|
|
1715
|
-
geometry.setAttribute("position", new three.Float32BufferAttribute(positions, 3));
|
|
1716
|
-
geometry.setAttribute("normal", new three.Float32BufferAttribute(normals, 3));
|
|
1717
|
-
geometry.setAttribute("color", new three.Float32BufferAttribute(colors, 3));
|
|
1718
|
-
geometry.setIndex(indices);
|
|
1719
|
-
geometry.computeBoundingBox();
|
|
1720
|
-
geometry.computeBoundingSphere();
|
|
1721
|
-
return geometry;
|
|
1924
|
+
return mesh;
|
|
1722
1925
|
}
|
|
1723
1926
|
function cornerOcclusion(solid, front, ud, vd) {
|
|
1724
1927
|
const q = [
|
|
@@ -1756,14 +1959,14 @@ function cornerOcclusion(solid, front, ud, vd) {
|
|
|
1756
1959
|
*/
|
|
1757
1960
|
var VoxelGeometry = class extends three.BufferGeometry {
|
|
1758
1961
|
type = "VoxelGeometry";
|
|
1759
|
-
/** voxel data (palette indices); the
|
|
1962
|
+
/** voxel data (palette indices); the source's default value is empty */
|
|
1760
1963
|
voxels;
|
|
1761
1964
|
/** meshing options used by every `update()` */
|
|
1762
1965
|
options;
|
|
1763
|
-
/** Builds the geometry right away. */
|
|
1764
|
-
constructor(voxels
|
|
1966
|
+
/** Builds the geometry right away. Without `voxels`, starts from an empty {@link Map3D}. */
|
|
1967
|
+
constructor(voxels, options = {}) {
|
|
1765
1968
|
super();
|
|
1766
|
-
this.voxels = voxels;
|
|
1969
|
+
this.voxels = voxels ?? new require_spatial_partition_3d.Map3D();
|
|
1767
1970
|
this.options = options;
|
|
1768
1971
|
this.update();
|
|
1769
1972
|
}
|
package/dist/three.d.cts
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
|
-
|
|
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
|
+
*/
|
|
2
12
|
import { BufferGeometry, ColorRepresentation, Group, LineBasicMaterial, LineSegments, Material, Mesh, MeshLambertMaterial, MeshLambertMaterialParameters, Object3D, OrthographicCamera, PerspectiveCamera, Points, Raycaster, Texture, Vector3, WebGLRenderer, WebGLRendererParameters } from "three";
|
|
13
|
+
import { d as ColorPalette, h as NoiseFunction3D, i as Map3D } from "./map3d-DRHhbrFn.cjs";
|
|
3
14
|
/** Screen edges covered by UI, CSS px. */
|
|
4
15
|
export interface ViewInsets {
|
|
5
16
|
l: number;
|
|
@@ -983,6 +994,32 @@ export declare class TextureAtlas {
|
|
|
983
994
|
/** a Lambert material using this atlas, set up for pixel-art tiles with transparency */
|
|
984
995
|
createMaterial(params?: MeshLambertMaterialParameters): MeshLambertMaterial;
|
|
985
996
|
}
|
|
997
|
+
/**
|
|
998
|
+
* Voxel data {@link buildVoxelGeometry} can mesh: anything that can look up a voxel and list the
|
|
999
|
+
* solid ones. A {@link Map3D} is one as it is; a dense volume (e.g. a `Uint8Array` with 0 = empty,
|
|
1000
|
+
* as in MagicVoxel's .vox files) or a view onto part of a bigger volume (one chunk of it) can be
|
|
1001
|
+
* one too, without copying its voxels into a `Map3D` first.
|
|
1002
|
+
*
|
|
1003
|
+
* ```ts
|
|
1004
|
+
* // a 32^3 chunk of a dense 256^3 volume: faces are listed for the chunk's voxels only, but
|
|
1005
|
+
* // `get` reads the whole volume, so faces against neighbouring chunks are culled (no `neighbor` needed)
|
|
1006
|
+
* const chunk: VoxelSource = {
|
|
1007
|
+
* defaultValue: 0,
|
|
1008
|
+
* get: (x, y, z) => volume.get(x, y, z), // 0 outside the volume
|
|
1009
|
+
* forEach: (fn) => volume.forEachIn(box, fn), // only the solid voxels inside the chunk
|
|
1010
|
+
* };
|
|
1011
|
+
* ```
|
|
1012
|
+
*
|
|
1013
|
+
* @category Voxels
|
|
1014
|
+
*/
|
|
1015
|
+
export interface VoxelSource {
|
|
1016
|
+
/** the value that means empty: `get()` returns it for empty cells, and `forEach()` never visits it */
|
|
1017
|
+
readonly defaultValue: number;
|
|
1018
|
+
/** value at integer (x, y, z); `defaultValue` where empty, including outside the data */
|
|
1019
|
+
get(x: number, y: number, z: number): number;
|
|
1020
|
+
/** calls `callback` once for each solid voxel to mesh (in any order: the output is sorted) */
|
|
1021
|
+
forEach(callback: (x: number, y: number, z: number, value: number) => void): void;
|
|
1022
|
+
}
|
|
986
1023
|
/**
|
|
987
1024
|
* Options for {@link buildVoxelGeometry} and {@link VoxelGeometry}.
|
|
988
1025
|
*
|
|
@@ -1005,15 +1042,23 @@ export interface BuildVoxelGeometryOptions {
|
|
|
1005
1042
|
aoStrength?: number;
|
|
1006
1043
|
/** rebuild into this geometry instead of creating one */
|
|
1007
1044
|
target?: BufferGeometry;
|
|
1045
|
+
/**
|
|
1046
|
+
* How to mesh; every strategy gives exactly the same geometry. `'dense'` copies the voxels'
|
|
1047
|
+
* bounding box into flat arrays first (fast); `'sparse'` works from hash maps (for voxels
|
|
1048
|
+
* spread far apart, where that box would be huge). Default `'auto'`: dense unless the box is
|
|
1049
|
+
* much bigger than the voxels need.
|
|
1050
|
+
*/
|
|
1051
|
+
strategy?: 'auto' | 'dense' | 'sparse';
|
|
1008
1052
|
}
|
|
1009
1053
|
/**
|
|
1010
1054
|
* Builds a mesh for the voxels in `voxels`: one quad per visible face, merged into larger quads
|
|
1011
1055
|
* (`greedy`) and shaded with per-vertex ambient occlusion. A voxel's value is its palette index;
|
|
1012
|
-
* the
|
|
1056
|
+
* the source's default value means empty. `voxels` is a {@link Map3D} or any other
|
|
1057
|
+
* {@link VoxelSource}.
|
|
1013
1058
|
*
|
|
1014
|
-
* Positions are in the
|
|
1059
|
+
* Positions are in the source's own coordinates, so a chunk stored in chunk-local coordinates is
|
|
1015
1060
|
* placed by positioning its mesh. Pass `neighbor` so faces against neighbouring chunks are culled
|
|
1016
|
-
* and their voxels count for ambient occlusion.
|
|
1061
|
+
* and their voxels count for ambient occlusion (or give a source whose `get` already sees them).
|
|
1017
1062
|
*
|
|
1018
1063
|
* The geometry has position, normal and color attributes and an index; use a material with
|
|
1019
1064
|
* `vertexColors: true`. The output is deterministic: the same voxels and options always produce
|
|
@@ -1031,7 +1076,7 @@ export interface BuildVoxelGeometryOptions {
|
|
|
1031
1076
|
* @returns `options.target` if given, otherwise a new geometry
|
|
1032
1077
|
* @category Voxels
|
|
1033
1078
|
*/
|
|
1034
|
-
export declare function buildVoxelGeometry(voxels:
|
|
1079
|
+
export declare function buildVoxelGeometry(voxels: VoxelSource, options?: BuildVoxelGeometryOptions): BufferGeometry;
|
|
1035
1080
|
/**
|
|
1036
1081
|
* Geometry that owns its voxels; call `update()` after changing them. See
|
|
1037
1082
|
* {@link buildVoxelGeometry} for how voxels are meshed, and {@link VoxelMesh} for an editable mesh
|
|
@@ -1039,14 +1084,14 @@ export declare function buildVoxelGeometry(voxels: Map3D, options?: BuildVoxelGe
|
|
|
1039
1084
|
*
|
|
1040
1085
|
* @category Voxels
|
|
1041
1086
|
*/
|
|
1042
|
-
export declare class VoxelGeometry extends BufferGeometry {
|
|
1087
|
+
export declare class VoxelGeometry<V extends VoxelSource = Map3D> extends BufferGeometry {
|
|
1043
1088
|
type: string;
|
|
1044
|
-
/** voxel data (palette indices); the
|
|
1045
|
-
voxels:
|
|
1089
|
+
/** voxel data (palette indices); the source's default value is empty */
|
|
1090
|
+
voxels: V;
|
|
1046
1091
|
/** meshing options used by every `update()` */
|
|
1047
1092
|
options: Omit<BuildVoxelGeometryOptions, 'target'>;
|
|
1048
|
-
/** Builds the geometry right away. */
|
|
1049
|
-
constructor(voxels?:
|
|
1093
|
+
/** Builds the geometry right away. Without `voxels`, starts from an empty {@link Map3D}. */
|
|
1094
|
+
constructor(voxels?: V, options?: Omit<BuildVoxelGeometryOptions, 'target'>);
|
|
1050
1095
|
/** Rebuilds the geometry from `voxels` and `options`. */
|
|
1051
1096
|
update(): this;
|
|
1052
1097
|
}
|