@n3rdw1z4rd/core 0.18.5 → 0.19.1
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 +72 -10
- package/dist/astar.d.ts +44 -0
- package/dist/astar.js +118 -0
- package/dist/canvas-renderer.d.ts +157 -0
- package/dist/canvas-renderer.js +207 -0
- package/dist/clock.d.ts +69 -0
- package/dist/clock.js +108 -0
- package/dist/color-palette.d.ts +25 -0
- package/dist/color-palette.js +112 -0
- package/dist/color.d.ts +90 -0
- package/dist/color.js +224 -0
- package/dist/css/main.css +1 -3
- package/dist/disposable.d.ts +27 -0
- package/dist/disposable.js +32 -0
- package/dist/dungeon-generator.d.ts +142 -0
- package/dist/dungeon-generator.js +288 -0
- package/dist/ecs.d.ts +87 -0
- package/dist/ecs.js +101 -0
- package/dist/emitter.d.ts +40 -0
- package/dist/emitter.js +54 -0
- package/dist/event-queue.d.ts +29 -0
- package/dist/event-queue.js +40 -0
- package/dist/index.cjs +3820 -0
- package/dist/index.d.cts +2657 -0
- package/dist/index.d.ts +35 -873
- package/dist/index.js +34 -3981
- package/dist/input/action-map.d.ts +79 -0
- package/dist/input/action-map.js +131 -0
- package/dist/input/bindings.d.ts +273 -0
- package/dist/input/bindings.js +250 -0
- package/dist/input/gestures.d.ts +108 -0
- package/dist/input/gestures.js +159 -0
- package/dist/input/index.d.ts +5 -0
- package/dist/input/index.js +5 -0
- package/dist/input/input.d.ts +297 -0
- package/dist/input/input.js +695 -0
- package/dist/log.d.ts +8 -0
- package/dist/log.js +9 -0
- package/dist/map2d.d.ts +83 -0
- package/dist/map2d.js +120 -0
- package/dist/map3d-CHDJiq5T.d.cts +236 -0
- package/dist/map3d.d.ts +94 -0
- package/dist/map3d.js +148 -0
- package/dist/math.d.ts +176 -0
- package/dist/math.js +206 -0
- package/dist/misc.d.ts +46 -0
- package/dist/misc.js +71 -0
- package/dist/observable.d.ts +43 -0
- package/dist/observable.js +53 -0
- package/dist/particle-renderer.d.ts +47 -0
- package/dist/particle-renderer.js +82 -0
- package/dist/particle-system-2d.d.ts +60 -0
- package/dist/particle-system-2d.js +147 -0
- package/dist/random.d.ts +46 -0
- package/dist/random.js +74 -0
- package/dist/rectangle.d.ts +44 -0
- package/dist/rectangle.js +69 -0
- package/dist/rng.d.ts +72 -0
- package/dist/rng.js +152 -0
- package/dist/signal.d.ts +34 -0
- package/dist/signal.js +41 -0
- package/dist/spatial-partition-2d.d.ts +42 -0
- package/dist/spatial-partition-2d.js +52 -0
- package/dist/spatial-partition-3d-Ba-VBveM.cjs +1324 -0
- package/dist/spatial-partition-3d.d.ts +44 -0
- package/dist/spatial-partition-3d.js +57 -0
- package/dist/three/asteroid-mesh.d.ts +41 -0
- package/dist/three/asteroid-mesh.js +44 -0
- package/dist/three/camera-rig.d.ts +140 -0
- package/dist/three/camera-rig.js +197 -0
- package/dist/three/cube-sphere-geometry.d.ts +55 -0
- package/dist/three/cube-sphere-geometry.js +239 -0
- package/dist/three/ecs.d.ts +75 -0
- package/dist/three/ecs.js +64 -0
- package/dist/three/index.d.ts +12 -0
- package/dist/three/index.js +12 -0
- package/dist/three/particle-system-3d.d.ts +53 -0
- package/dist/three/particle-system-3d.js +224 -0
- package/dist/three/renderer.d.ts +38 -0
- package/dist/three/renderer.js +57 -0
- package/dist/three/starfield.d.ts +23 -0
- package/dist/three/starfield.js +64 -0
- package/dist/three/texture-atlas.d.ts +76 -0
- package/dist/three/texture-atlas.js +84 -0
- package/dist/three/texture-utils.d.ts +31 -0
- package/dist/three/texture-utils.js +44 -0
- package/dist/three/voxel-geometry.d.ts +72 -0
- package/dist/three/voxel-geometry.js +231 -0
- package/dist/three/voxel-mesh.d.ts +67 -0
- package/dist/three/voxel-mesh.js +71 -0
- package/dist/three.cjs +1307 -0
- package/dist/three.d.cts +655 -0
- package/dist/tilemap.d.ts +17 -0
- package/dist/tilemap.js +26 -0
- package/dist/types.d.ts +34 -0
- package/dist/types.js +0 -0
- package/dist/vector.d.ts +78 -0
- package/dist/vector.js +169 -0
- package/dist/webgl-renderer.d.ts +89 -0
- package/dist/webgl-renderer.js +140 -0
- package/dist/webgl.d.ts +357 -0
- package/dist/webgl.js +628 -0
- package/dist/worker-interface.d.ts +30 -0
- package/dist/worker-interface.js +39 -0
- package/package.json +55 -13
- package/dist/css/index.ts +0 -1
- package/dist/index.css +0 -104
- package/dist/index.d.mts +0 -873
- package/dist/index.mjs +0 -3977
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { Random } from "../random.js";
|
|
2
|
+
import { Map3D } from "../map3d.js";
|
|
3
|
+
import { BufferGeometry, Color, Float32BufferAttribute } from "three";
|
|
4
|
+
//#region src/three/voxel-geometry.ts
|
|
5
|
+
const cellKey = (u, v) => (u + 33554432) * 67108864 + (v + 33554432);
|
|
6
|
+
const planeKey = (d, side, slice) => (slice + 33554432) * 8 + d * 2 + (side > 0 ? 1 : 0);
|
|
7
|
+
/**
|
|
8
|
+
* Builds a mesh for the voxels in `voxels`: one quad per visible face, merged into larger quads
|
|
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.
|
|
11
|
+
*
|
|
12
|
+
* Positions are in the map's own coordinates, so a chunk stored in chunk-local coordinates is
|
|
13
|
+
* placed by positioning its mesh. Pass `neighbor` so faces against neighbouring chunks are culled
|
|
14
|
+
* and their voxels count for ambient occlusion.
|
|
15
|
+
*
|
|
16
|
+
* The geometry has position, normal and color attributes and an index; use a material with
|
|
17
|
+
* `vertexColors: true`. The output is deterministic: the same voxels and options always produce
|
|
18
|
+
* the same buffers.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* const voxels = new Map3D();
|
|
22
|
+
* voxels.setArea(0, 0, 0, 7, 0, 7, 0); // 8x1x8 slab of palette index 0
|
|
23
|
+
* voxels.set(3, 1, 3, 1);
|
|
24
|
+
*
|
|
25
|
+
* const geometry = buildVoxelGeometry(voxels, { palette: [0x55aa33, 0x8b5a2b] });
|
|
26
|
+
* scene.add(new Mesh(geometry, new MeshLambertMaterial({ vertexColors: true })));
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @returns `options.target` if given, otherwise a new geometry
|
|
30
|
+
* @category Voxels
|
|
31
|
+
*/
|
|
32
|
+
function buildVoxelGeometry(voxels, options = {}) {
|
|
33
|
+
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;
|
|
40
|
+
const solid = (x, y, z) => {
|
|
41
|
+
let value = voxels.get(x, y, z);
|
|
42
|
+
if (value === empty && neighbor) value = neighbor(x, y, z);
|
|
43
|
+
return value !== empty;
|
|
44
|
+
};
|
|
45
|
+
const colorOf = (value) => palette[value] ?? options.missingColor ?? Random.uint(value, 1) & 16777215;
|
|
46
|
+
const planes = /* @__PURE__ */ new Map();
|
|
47
|
+
const p = [
|
|
48
|
+
0,
|
|
49
|
+
0,
|
|
50
|
+
0
|
|
51
|
+
];
|
|
52
|
+
voxels.forEach((x, y, z, value) => {
|
|
53
|
+
const color = colorOf(value);
|
|
54
|
+
for (let d = 0; d < 3; d++) {
|
|
55
|
+
const ud = (d + 1) % 3;
|
|
56
|
+
const vd = (d + 2) % 3;
|
|
57
|
+
for (const side of [1, -1]) {
|
|
58
|
+
p[0] = x;
|
|
59
|
+
p[1] = y;
|
|
60
|
+
p[2] = z;
|
|
61
|
+
p[d] += side;
|
|
62
|
+
if (!includeHiddenFaces && solid(p[0], p[1], p[2])) continue;
|
|
63
|
+
const ao = ambientOcclusion ? cornerOcclusion(solid, p, ud, vd) : [
|
|
64
|
+
3,
|
|
65
|
+
3,
|
|
66
|
+
3,
|
|
67
|
+
3
|
|
68
|
+
];
|
|
69
|
+
const slice = (d === 0 ? x : d === 1 ? y : z) + (side > 0 ? 1 : 0);
|
|
70
|
+
const key = planeKey(d, side, slice);
|
|
71
|
+
let plane = planes.get(key);
|
|
72
|
+
if (!plane) {
|
|
73
|
+
plane = /* @__PURE__ */ new Map();
|
|
74
|
+
planes.set(key, plane);
|
|
75
|
+
}
|
|
76
|
+
const u = d === 0 ? y : d === 1 ? z : x;
|
|
77
|
+
const v = d === 0 ? z : d === 1 ? x : y;
|
|
78
|
+
const sig = color * 256 + ao[0] * 64 + ao[1] * 16 + ao[2] * 4 + ao[3];
|
|
79
|
+
plane.set(cellKey(u, v), {
|
|
80
|
+
u,
|
|
81
|
+
v,
|
|
82
|
+
color,
|
|
83
|
+
ao,
|
|
84
|
+
sig
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
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
|
+
};
|
|
138
|
+
const keys = [...planes.keys()].sort((a, b) => a - b);
|
|
139
|
+
for (const key of keys) {
|
|
140
|
+
const d = Math.floor(key / 2) % 4;
|
|
141
|
+
const side = key % 2 ? 1 : -1;
|
|
142
|
+
const slice = Math.floor(key / 8) - 33554432;
|
|
143
|
+
const plane = planes.get(key);
|
|
144
|
+
const faces = [...plane.values()].sort((a, b) => a.v - b.v || a.u - b.u);
|
|
145
|
+
const used = /* @__PURE__ */ new Set();
|
|
146
|
+
const free = (u, v, sig) => {
|
|
147
|
+
const k = cellKey(u, v);
|
|
148
|
+
return !used.has(k) && plane.get(k)?.sig === sig;
|
|
149
|
+
};
|
|
150
|
+
for (const face of faces) {
|
|
151
|
+
if (used.has(cellKey(face.u, face.v))) continue;
|
|
152
|
+
let width = 1;
|
|
153
|
+
let height = 1;
|
|
154
|
+
if (greedy) {
|
|
155
|
+
while (free(face.u + width, face.v, face.sig)) width++;
|
|
156
|
+
grow: while (true) {
|
|
157
|
+
for (let du = 0; du < width; du++) if (!free(face.u + du, face.v + height, face.sig)) break grow;
|
|
158
|
+
height++;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
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);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
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;
|
|
173
|
+
}
|
|
174
|
+
function cornerOcclusion(solid, front, ud, vd) {
|
|
175
|
+
const q = [
|
|
176
|
+
0,
|
|
177
|
+
0,
|
|
178
|
+
0
|
|
179
|
+
];
|
|
180
|
+
const at = (du, dv) => {
|
|
181
|
+
q[0] = front[0];
|
|
182
|
+
q[1] = front[1];
|
|
183
|
+
q[2] = front[2];
|
|
184
|
+
q[ud] += du;
|
|
185
|
+
q[vd] += dv;
|
|
186
|
+
return solid(q[0], q[1], q[2]);
|
|
187
|
+
};
|
|
188
|
+
const occlusion = (su, sv) => {
|
|
189
|
+
const side1 = at(su, 0);
|
|
190
|
+
const side2 = at(0, sv);
|
|
191
|
+
if (side1 && side2) return 0;
|
|
192
|
+
return 3 - (+side1 + +side2 + +at(su, sv));
|
|
193
|
+
};
|
|
194
|
+
return [
|
|
195
|
+
occlusion(-1, -1),
|
|
196
|
+
occlusion(1, -1),
|
|
197
|
+
occlusion(1, 1),
|
|
198
|
+
occlusion(-1, 1)
|
|
199
|
+
];
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Geometry that owns its voxels; call `update()` after changing them. See
|
|
203
|
+
* {@link buildVoxelGeometry} for how voxels are meshed, and {@link VoxelMesh} for an editable mesh
|
|
204
|
+
* that tracks changes for you.
|
|
205
|
+
*
|
|
206
|
+
* @category Voxels
|
|
207
|
+
*/
|
|
208
|
+
var VoxelGeometry = class extends BufferGeometry {
|
|
209
|
+
type = "VoxelGeometry";
|
|
210
|
+
/** voxel data (palette indices); the map's default value is empty */
|
|
211
|
+
voxels;
|
|
212
|
+
/** meshing options used by every `update()` */
|
|
213
|
+
options;
|
|
214
|
+
/** Builds the geometry right away. */
|
|
215
|
+
constructor(voxels = new Map3D(), options = {}) {
|
|
216
|
+
super();
|
|
217
|
+
this.voxels = voxels;
|
|
218
|
+
this.options = options;
|
|
219
|
+
this.update();
|
|
220
|
+
}
|
|
221
|
+
/** Rebuilds the geometry from `voxels` and `options`. */
|
|
222
|
+
update() {
|
|
223
|
+
buildVoxelGeometry(this.voxels, {
|
|
224
|
+
...this.options,
|
|
225
|
+
target: this
|
|
226
|
+
});
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
//#endregion
|
|
231
|
+
export { VoxelGeometry, buildVoxelGeometry };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Map3D } from "../map3d.js";
|
|
2
|
+
import { BuildVoxelGeometryOptions, VoxelGeometry } from "./voxel-geometry.js";
|
|
3
|
+
import { Material, Mesh } from "three";
|
|
4
|
+
//#region src/three/voxel-mesh.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Box of voxel cells a {@link VoxelMesh} accepts, from the origin.
|
|
7
|
+
*
|
|
8
|
+
* @category Voxels
|
|
9
|
+
*/
|
|
10
|
+
export interface VoxelBounds {
|
|
11
|
+
/** x */
|
|
12
|
+
width: number;
|
|
13
|
+
/** y */
|
|
14
|
+
height: number;
|
|
15
|
+
/** z */
|
|
16
|
+
depth: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Options for the {@link VoxelMesh} constructor; the rest are passed on to {@link VoxelGeometry}.
|
|
20
|
+
*
|
|
21
|
+
* @category Voxels
|
|
22
|
+
*/
|
|
23
|
+
export interface VoxelMeshOptions extends Omit<BuildVoxelGeometryOptions, 'target'> {
|
|
24
|
+
/** vertexColors is turned on; default: MeshLambertMaterial */
|
|
25
|
+
material?: Material | Material[];
|
|
26
|
+
/** voxels may only be set inside [0, width) x [0, height) x [0, depth) */
|
|
27
|
+
bounds?: VoxelBounds;
|
|
28
|
+
/** existing voxel data to show */
|
|
29
|
+
voxels?: Map3D;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Editable voxel model: `set()` voxels, then `update()` rebuilds the geometry if anything changed.
|
|
33
|
+
* Voxel values are palette indices; `Map3D`'s default value (-1) means empty.
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* const model = new VoxelMesh({ palette: [0xff0000, 0x00ff00], bounds: { width: 16, height: 16, depth: 16 } });
|
|
37
|
+
* scene.add(model);
|
|
38
|
+
*
|
|
39
|
+
* model.set(0, 0, 0, 1);
|
|
40
|
+
* model.update(); // rebuilds once, however many voxels changed
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @category Voxels
|
|
44
|
+
*/
|
|
45
|
+
export declare class VoxelMesh extends Mesh<VoxelGeometry, Material | Material[]> {
|
|
46
|
+
/** `set()` ignores voxels outside these; undefined = unbounded */
|
|
47
|
+
bounds?: VoxelBounds;
|
|
48
|
+
private _dirty;
|
|
49
|
+
/** the geometry's voxel data; call `invalidate()` after changing it directly */
|
|
50
|
+
get voxels(): Map3D;
|
|
51
|
+
/** geometry is out of date with the voxels */
|
|
52
|
+
get needsUpdate(): boolean;
|
|
53
|
+
constructor(options?: VoxelMeshOptions);
|
|
54
|
+
/** Whether (x, y, z) is inside `bounds` (always true without bounds). */
|
|
55
|
+
inBounds(x: number, y: number, z: number): boolean;
|
|
56
|
+
/** Voxel value at (x, y, z); the map's default value (-1 unless you passed your own map) if empty. */
|
|
57
|
+
get(x: number, y: number, z: number): number;
|
|
58
|
+
/** Sets a voxel; leave out `value` (or pass the default value) to remove it. Ignored outside the bounds. */
|
|
59
|
+
set(x: number, y: number, z: number, value?: number): void;
|
|
60
|
+
/** (not `clear()`: that's Object3D's "remove all children") */
|
|
61
|
+
clearVoxels(): void;
|
|
62
|
+
/** Call after changing `voxels` directly (or the geometry's options) to force a rebuild. */
|
|
63
|
+
invalidate(): void;
|
|
64
|
+
/** Rebuilds the geometry if the voxels changed; returns whether it did. */
|
|
65
|
+
update(): boolean;
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Map3D } from "../map3d.js";
|
|
2
|
+
import { VoxelGeometry } from "./voxel-geometry.js";
|
|
3
|
+
import { Mesh, MeshLambertMaterial } from "three";
|
|
4
|
+
//#region src/three/voxel-mesh.ts
|
|
5
|
+
/**
|
|
6
|
+
* Editable voxel model: `set()` voxels, then `update()` rebuilds the geometry if anything changed.
|
|
7
|
+
* Voxel values are palette indices; `Map3D`'s default value (-1) means empty.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const model = new VoxelMesh({ palette: [0xff0000, 0x00ff00], bounds: { width: 16, height: 16, depth: 16 } });
|
|
11
|
+
* scene.add(model);
|
|
12
|
+
*
|
|
13
|
+
* model.set(0, 0, 0, 1);
|
|
14
|
+
* model.update(); // rebuilds once, however many voxels changed
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @category Voxels
|
|
18
|
+
*/
|
|
19
|
+
var VoxelMesh = class extends Mesh {
|
|
20
|
+
/** `set()` ignores voxels outside these; undefined = unbounded */
|
|
21
|
+
bounds;
|
|
22
|
+
_dirty = false;
|
|
23
|
+
/** the geometry's voxel data; call `invalidate()` after changing it directly */
|
|
24
|
+
get voxels() {
|
|
25
|
+
return this.geometry.voxels;
|
|
26
|
+
}
|
|
27
|
+
/** geometry is out of date with the voxels */
|
|
28
|
+
get needsUpdate() {
|
|
29
|
+
return this._dirty;
|
|
30
|
+
}
|
|
31
|
+
constructor(options = {}) {
|
|
32
|
+
const { material, bounds, voxels, ...buildOptions } = options;
|
|
33
|
+
super(new VoxelGeometry(voxels ?? new Map3D(), buildOptions), material ?? new MeshLambertMaterial());
|
|
34
|
+
for (const m of Array.isArray(this.material) ? this.material : [this.material]) m.vertexColors = true;
|
|
35
|
+
this.bounds = bounds;
|
|
36
|
+
}
|
|
37
|
+
/** Whether (x, y, z) is inside `bounds` (always true without bounds). */
|
|
38
|
+
inBounds(x, y, z) {
|
|
39
|
+
if (!this.bounds) return true;
|
|
40
|
+
return x >= 0 && y >= 0 && z >= 0 && x < this.bounds.width && y < this.bounds.height && z < this.bounds.depth;
|
|
41
|
+
}
|
|
42
|
+
/** Voxel value at (x, y, z); the map's default value (-1 unless you passed your own map) if empty. */
|
|
43
|
+
get(x, y, z) {
|
|
44
|
+
return this.voxels.get(x, y, z);
|
|
45
|
+
}
|
|
46
|
+
/** Sets a voxel; leave out `value` (or pass the default value) to remove it. Ignored outside the bounds. */
|
|
47
|
+
set(x, y, z, value = this.voxels.defaultValue) {
|
|
48
|
+
if (!this.inBounds(x, y, z) || this.voxels.get(x, y, z) === value) return;
|
|
49
|
+
this.voxels.set(x, y, z, value);
|
|
50
|
+
this._dirty = true;
|
|
51
|
+
}
|
|
52
|
+
/** (not `clear()`: that's Object3D's "remove all children") */
|
|
53
|
+
clearVoxels() {
|
|
54
|
+
if (this.voxels.size === 0) return;
|
|
55
|
+
this.voxels.clear();
|
|
56
|
+
this._dirty = true;
|
|
57
|
+
}
|
|
58
|
+
/** Call after changing `voxels` directly (or the geometry's options) to force a rebuild. */
|
|
59
|
+
invalidate() {
|
|
60
|
+
this._dirty = true;
|
|
61
|
+
}
|
|
62
|
+
/** Rebuilds the geometry if the voxels changed; returns whether it did. */
|
|
63
|
+
update() {
|
|
64
|
+
if (!this._dirty) return false;
|
|
65
|
+
this.geometry.update();
|
|
66
|
+
this._dirty = false;
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
//#endregion
|
|
71
|
+
export { VoxelMesh };
|