@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.
- package/dist/astar.js +3 -3
- package/dist/dungeon-generator.js +4 -4
- 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 +11 -11
- package/dist/index.d.cts +11 -0
- package/dist/index.d.ts +11 -0
- 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/webgl.js +2 -2
- package/package.json +1 -1
- package/dist/input/_input.d.ts +0 -23
- package/dist/input/_input.js +0 -96
|
@@ -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
|
|
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
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
53
|
-
const
|
|
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
|
|
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
|
|
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
|
-
|
|
374
|
+
mesh.quad(d, side, slice, face.u, face.v, width, height, face.color, face.ao, aoStrength);
|
|
163
375
|
}
|
|
164
376
|
}
|
|
165
|
-
|
|
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
|
|
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
|
|
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
|
*
|
package/dist/three/voxel-mesh.js
CHANGED
|
@@ -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.
|
package/dist/three/world.d.ts
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`.
|
package/dist/three/world.js
CHANGED
|
@@ -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}),
|