@margoengine/voxel 0.0.0-stage → 1.0.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 (90) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1670 -2
  4. package/dist/bevel.d.ts +79 -0
  5. package/dist/bevel.js +453 -0
  6. package/dist/blocks-BabtpQT-.js +179 -0
  7. package/dist/blocks.d.ts +220 -0
  8. package/dist/border.d.ts +9 -0
  9. package/dist/chunk-BCoaKSn4.js +158 -0
  10. package/dist/chunk.d.ts +112 -0
  11. package/dist/codec-BmXhcsFt.js +412 -0
  12. package/dist/codec.d.ts +125 -0
  13. package/dist/codec.js +1 -0
  14. package/dist/debris.d.ts +114 -0
  15. package/dist/debris.js +387 -0
  16. package/dist/faces-DUR7LhrN.js +146 -0
  17. package/dist/faces.d.ts +7 -0
  18. package/dist/fluids.d.ts +74 -0
  19. package/dist/fluids.js +226 -0
  20. package/dist/generate.d.ts +78 -0
  21. package/dist/generate.js +99 -0
  22. package/dist/gi.d.ts +76 -0
  23. package/dist/gi.js +264 -0
  24. package/dist/gpu-mesher.d.ts +77 -0
  25. package/dist/gpu-mesher.js +461 -0
  26. package/dist/granular-mpm.d.ts +40 -0
  27. package/dist/granular-mpm.js +46 -0
  28. package/dist/index.d.ts +6 -0
  29. package/dist/index.js +4 -0
  30. package/dist/light-rgb.d.ts +71 -0
  31. package/dist/light-rgb.js +287 -0
  32. package/dist/light-worker.d.ts +84 -0
  33. package/dist/light-worker.js +110 -0
  34. package/dist/light.d.ts +108 -0
  35. package/dist/light.js +332 -0
  36. package/dist/light.worker.d.ts +1 -0
  37. package/dist/lightmap.d.ts +50 -0
  38. package/dist/lightmap.js +68 -0
  39. package/dist/lod.d.ts +87 -0
  40. package/dist/lod.js +182 -0
  41. package/dist/mesher.d.ts +214 -0
  42. package/dist/mesher.js +539 -0
  43. package/dist/mesher.worker.d.ts +1 -0
  44. package/dist/model-data-D_rIKJCQ.js +41 -0
  45. package/dist/model-data.d.ts +21 -0
  46. package/dist/model-mesh-C4Gi6bMM.js +122 -0
  47. package/dist/model-mesh.d.ts +21 -0
  48. package/dist/model-surface.d.ts +5 -0
  49. package/dist/models.d.ts +74 -0
  50. package/dist/models.js +308 -0
  51. package/dist/objects.d.ts +73 -0
  52. package/dist/objects.js +152 -0
  53. package/dist/octants-C_Ic7gdn.js +10 -0
  54. package/dist/octants.d.ts +16 -0
  55. package/dist/origin-DsMelUxs.js +23 -0
  56. package/dist/origin.d.ts +7 -0
  57. package/dist/physics.d.ts +147 -0
  58. package/dist/physics.js +486 -0
  59. package/dist/query.d.ts +166 -0
  60. package/dist/query.js +305 -0
  61. package/dist/render-post.d.ts +6 -0
  62. package/dist/render.d.ts +296 -0
  63. package/dist/render.js +1616 -0
  64. package/dist/shared/borrowed.d.ts +44 -0
  65. package/dist/smooth-CEwHkXli.js +968 -0
  66. package/dist/smooth-clip.d.ts +24 -0
  67. package/dist/smooth-lod.d.ts +118 -0
  68. package/dist/smooth-lod.js +244 -0
  69. package/dist/smooth.d.ts +171 -0
  70. package/dist/smooth.js +1 -0
  71. package/dist/stream.d.ts +201 -0
  72. package/dist/stream.js +402 -0
  73. package/dist/terrain.worker.d.ts +1 -0
  74. package/dist/textures.d.ts +86 -0
  75. package/dist/ticks.d.ts +126 -0
  76. package/dist/ticks.js +239 -0
  77. package/dist/unit-light-Ch7roJgB.js +7 -0
  78. package/dist/vox.d.ts +237 -0
  79. package/dist/vox.js +780 -0
  80. package/dist/wind-data.d.ts +13 -0
  81. package/dist/wind.d.ts +68 -0
  82. package/dist/wind.js +170 -0
  83. package/dist/worker-DlGRxlc4.js +44 -0
  84. package/dist/worker-pool-sPyC6jCE.js +93 -0
  85. package/dist/workers/light.js +1 -0
  86. package/dist/workers/mesher.js +1 -0
  87. package/dist/workers/terrain.js +1 -0
  88. package/dist/world-CelTlft2.js +153 -0
  89. package/dist/world.d.ts +117 -0
  90. package/package.json +173 -4
@@ -0,0 +1,152 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const a=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
+ import { i as e } from "./blocks-BabtpQT-.js";
5
+ import { n as t } from "./world-CelTlft2.js";
6
+ import { n, t as r } from "./origin-DsMelUxs.js";
7
+ import { MargoError as i } from "@margoengine/math/errors";
8
+ import { engineRuntime as __margoRuntime, meshRuntime as o } from "@margoengine/core/extension";
9
+ //#region src/objects.ts
10
+ function s(t, n = 0) {
11
+ if (!Number.isInteger(n) || n < 0 || n > 3) throw RangeError("Object turns must be 0..3; pass positive Y quarter turns");
12
+ return e(t, n);
13
+ }
14
+ var c = (e) => [
15
+ e.x,
16
+ e.y,
17
+ e.z
18
+ ], l = class e {
19
+ static attach(t, n, r = {}) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
20
+ return new e(t, n, r);
21
+ }
22
+ constructor(e, n, i = {}) {
23
+ this.world = n, this.placed = [
24
+ 0,
25
+ 0,
26
+ 0
27
+ ], this.templates = /* @__PURE__ */ new Map(), this.pending = /* @__PURE__ */ new Set(), this.chunks = /* @__PURE__ */ new Map(), this.disposed = !1, this.engine = a(e), this.origin = r(i?.origin, "VoxelObjects"), this.off = [n.onChunk((e, n, r) => this.pending.add(t(e, n, r))), n.onChange((e, n, r) => this.pending.add(t(e >> 5, n >> 5, r >> 5)))];
28
+ }
29
+ check() {
30
+ if (this.disposed) throw new i("disposed", "VoxelObjects is disposed; create a new VoxelObjects");
31
+ }
32
+ define(e, t, n = {}) {
33
+ this.check();
34
+ let r = "root" in t ? t : void 0, i = o(r ? r.root : t), a = [], s = /* @__PURE__ */ new Map();
35
+ i.check();
36
+ let l = (e) => {
37
+ e.check();
38
+ let t = s.get(e);
39
+ if (t !== void 0) return t;
40
+ let n = -1;
41
+ if (r && e !== i) {
42
+ let t = e.engine.meshes.get(e.engine.store.parents[e.id]);
43
+ if (!t) throw TypeError("Object source meshes must descend from their model root; pass one glTF Model");
44
+ n = l(t);
45
+ }
46
+ let o = a.length;
47
+ return s.set(e, o), a.push({
48
+ parent: n,
49
+ geometry: e.geometry,
50
+ material: e.material,
51
+ position: c(e.position),
52
+ rotation: c(e.rotation),
53
+ scale: c(e.scale),
54
+ receiveShadow: e.receiveShadow,
55
+ name: e.name
56
+ }), o;
57
+ };
58
+ for (let e of r?.meshes ?? [i]) l(o(e));
59
+ if (!a.some((e) => e.geometry)) throw RangeError("Object source has no geometry; pass a core mesh or loaded glTF Model");
60
+ let u = (e) => e.parent < 0 ? 0 : 1 + u(a[e.parent]);
61
+ if (a.some((e) => u(e) >= 63)) throw RangeError("Object source nests 64 or more meshes deep; placements add one level and core allows 63, so flatten the graph");
62
+ let d = this.world.blocks.define(e, {
63
+ solid: !1,
64
+ opacity: 0,
65
+ ...n,
66
+ shape: "object"
67
+ });
68
+ return this.templates.set(d, a), d;
69
+ }
70
+ at(e, n, r) {
71
+ return this.check(), this.world.getBlock(e, n, r), this.chunks.get(t(e >> 5, n >> 5, r >> 5))?.get(e & 31 | (r & 31) << 5 | (n & 31) << 10)?.meshes ?? [];
72
+ }
73
+ get stats() {
74
+ let e = 0, t = 0;
75
+ for (let n of this.chunks.values()) for (let r of n.values()) e++, t += r.meshes.length;
76
+ return {
77
+ chunks: this.chunks.size,
78
+ cells: e,
79
+ meshes: t
80
+ };
81
+ }
82
+ update() {
83
+ this.check();
84
+ let e = n(this.origin, "VoxelObjects");
85
+ if (e.some((e, t) => e !== this.placed[t])) {
86
+ this.placed = e;
87
+ for (let [t, n] of this.chunks) {
88
+ let [r, i, a] = t.split(",").map(Number);
89
+ for (let [t, o] of n) o.meshes[0].setPosition(this.cellPosition(r, i, a, t, e));
90
+ }
91
+ }
92
+ for (let t of this.pending) {
93
+ let [n, r, i] = t.split(",").map(Number), a = this.world.getChunk(n, r, i), o = this.chunks.get(t) ?? /* @__PURE__ */ new Map();
94
+ if (a) try {
95
+ for (let t = 0; t < 32768; t++) {
96
+ let s = t & 31, c = t >> 10, l = t >> 5 & 31, u = a.get(s, c, l), d = this.templates.get(u & 65535), f = o.get(t);
97
+ if (f?.state === u || (f && (this.remove(f), o.delete(t)), !d)) continue;
98
+ let p = this.engine.createMesh().setPosition(this.cellPosition(n, r, i, t, e)).setRotation([
99
+ 0,
100
+ (u >>> 16 & 3) * Math.PI / 2,
101
+ 0
102
+ ]), m = [p], h = [];
103
+ try {
104
+ for (let e of d) {
105
+ let t = this.engine.createMesh(e.parent < 0 ? p : h[e.parent]).setGeometry(e.geometry).setMaterial(e.material).setPosition(e.position).setRotation(e.rotation).setScale(e.scale);
106
+ t.receiveShadow = e.receiveShadow, t.name = e.name, h.push(t), m.push(t);
107
+ }
108
+ } catch (e) {
109
+ throw this.remove({
110
+ state: u,
111
+ meshes: m
112
+ }), e;
113
+ }
114
+ o.set(t, {
115
+ state: u,
116
+ meshes: m
117
+ });
118
+ }
119
+ } finally {
120
+ o.size ? this.chunks.set(t, o) : this.chunks.delete(t);
121
+ }
122
+ else {
123
+ for (let e of o.values()) this.remove(e);
124
+ this.chunks.delete(t);
125
+ }
126
+ }
127
+ this.pending.clear();
128
+ }
129
+ cellPosition(e, t, n, r, i) {
130
+ return [
131
+ e * 32 + (r & 31) + .5 - i[0],
132
+ t * 32 + (r >> 10) - i[1],
133
+ n * 32 + (r >> 5 & 31) + .5 - i[2]
134
+ ];
135
+ }
136
+ remove(e) {
137
+ for (let t = e.meshes.length - 1; t >= 0; t--) e.meshes[t].dispose();
138
+ }
139
+ dispose() {
140
+ if (!this.disposed) {
141
+ this.disposed = !0;
142
+ for (let e of this.off) e();
143
+ for (let e of this.chunks.values()) for (let t of e.values()) this.remove(t);
144
+ this.chunks.clear(), this.templates.clear(), this.pending.clear();
145
+ }
146
+ }
147
+ [Symbol.dispose]() {
148
+ this.dispose();
149
+ }
150
+ };
151
+ //#endregion
152
+ export { l as VoxelObjects, s as objectState };
@@ -0,0 +1,10 @@
1
+ var e = 32768, t = Uint8Array.from({ length: 16 }, (e, t) => t & 8 ? 255 : t & 4 ? 204 : 51), n = Uint8Array.from({ length: 64 }, (e, t) => {
2
+ let n = t & 2 ? 2 : 0, r = 2 - n, i = t & 1 ? 0 : 1, a = t & 4 ? 1 : 0, o = t >> 3 & 3, s = t >> 5 & 1, c = 0;
3
+ for (let e = 0; e < 8; e++) {
4
+ let t = e >> n & 1, l = e >> r & 1;
5
+ ((e >> 1 & 1) === a || (o === 2 ? t === i && l === s : o === 1 ? t === i || l === s : t === i)) && (c |= 1 << e);
6
+ }
7
+ return c;
8
+ }), r = (e) => t[e & 15], i = (e) => n[e & 63], a = (e, r) => e === "cube" ? 255 : e === "slab" ? t[r & 15] : e === "stair" ? n[r & 63] : 0;
9
+ //#endregion
10
+ export { i, a as n, r, e as t };
@@ -0,0 +1,16 @@
1
+ import type { BlockShape } from './blocks.js';
2
+ /** Slab data bits: the upper half, both halves. */
3
+ export declare const SLAB_TOP = 4, SLAB_DOUBLE = 8,
4
+ /** Stair data: the corner bits (3-5) and the hand-set corner bit. */
5
+ STAIR_CORNER = 56, STAIR_FIXED = 64,
6
+ /** Waterlogged: the block type's `waterlog` liquid fills the rest of its cell. */
7
+ WATERLOGGED = 32768;
8
+ /** Octant mask of a slab with state data `data`. */
9
+ export declare const slabOctants: (data: number) => number;
10
+ /** Octant mask of a stair with state data `data`. */
11
+ export declare const stairOctants: (data: number) => number;
12
+ /**
13
+ * Octant mask of a block shape with state data `data`: 255 for a cube, 0 for shapes without a box (cross, liquid),
14
+ * else the half cells a slab or stair fills (see `shapeOctants`).
15
+ */
16
+ export declare const octants: (shape: BlockShape, data: number) => number;
@@ -0,0 +1,23 @@
1
+ //#region src/origin.ts
2
+ function e(e, t) {
3
+ if (e == null) return null;
4
+ if (typeof e?.origin?.length != "number") throw TypeError(`${t} origin must be null or an object with origin [x, y, z], such as a FloatingOrigin`);
5
+ return e;
6
+ }
7
+ function t(e, t) {
8
+ let n = e?.origin, r = [
9
+ 0,
10
+ 0,
11
+ 0
12
+ ];
13
+ if (!n) return r;
14
+ for (let e = 0; e < 3; e++) {
15
+ let i = +n[e];
16
+ if (!Number.isFinite(i)) throw RangeError(`${t} origin[${e}] ${n[e]} is not a finite number; pass three finite numbers`);
17
+ r[e] = i;
18
+ }
19
+ return r;
20
+ }
21
+ var n = (e, t) => (e % t + t) % t;
22
+ //#endregion
23
+ export { t as n, n as r, e as t };
@@ -0,0 +1,7 @@
1
+ import type { VoxelOrigin } from './render.js';
2
+ /** Checks an `origin` option: null, undefined or an object with an `origin` array. */
3
+ export declare function checkOrigin(origin: unknown, who: string): VoxelOrigin | null;
4
+ /** The origin's point in float64, [0, 0, 0] for none. @throws RangeError for a value that is not a finite number. */
5
+ export declare function originPoint(origin: VoxelOrigin | null | undefined, who: string): [number, number, number];
6
+ /** `v` mod `n` in [0, n), in float64: where a world value falls in a wrapping window of `n` cells. */
7
+ export declare const wrap: (v: number, n: number) => number;
@@ -0,0 +1,147 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ // @ts-ignore optional peer: its types apply when it is installed
3
+ import type { Physics, PhysicsBody, PhysicsCollider } from '@margoengine/physics';
4
+ import { VoxelWorld } from './world.js';
5
+ /** Options of {@link VoxelPhysics}. */
6
+ export interface VoxelPhysicsOptions {
7
+ /**
8
+ * Chunks per axis around each moving body's chunk that get colliders: 1 (default) covers the body's chunk and its
9
+ * 26 neighbours, so a body always has at least one chunk (32 blocks) of terrain ahead of it. They are kept until no
10
+ * body is within `radius + 1`. Integer >= 1.
11
+ */
12
+ radius?: number;
13
+ /**
14
+ * Milliseconds of collider building per physics step, default 2 (see {@link VoxelPhysics.budget}). `Infinity`
15
+ * builds everything in the step that needs it. A number >= 0.
16
+ */
17
+ budget?: number;
18
+ }
19
+ /**
20
+ * Colliders for a {@link VoxelWorld}, so dynamic bodies (mobs, item drops, debris, balls) and kinematic
21
+ * character controllers of a `@margoengine/physics` world collide with its blocks. Before each physics step, every
22
+ * loaded chunk within `radius` chunks of any non-fixed body (dynamic or kinematic, awake or asleep: a sleeping body
23
+ * keeps its floor) gets a collider in cell coordinates, built from the chunk's surface: its solid cells (block types
24
+ * with `solid`) with at least one non-solid face neighbour, reading neighbour chunks (an unloaded one counts as air).
25
+ * Solid slabs and stairs collide as their half-block boxes instead: each chunk with any gets one more triangle mesh of
26
+ * their open faces (`fixInternalEdges`), rebuilt when a cell in or beside one changes, so bodies rest on a slab's
27
+ * top and climb stairs.
28
+ * A chunk keeps it until no body is within `radius + 1` (so a body on a chunk border does not make chunks come and go
29
+ * every step) or the chunk unloads.
30
+ *
31
+ * That is a `voxels` collider: neighbouring ones are coupled with `combineVoxelStates`, and edits
32
+ * (`VoxelWorld.onChange`) update them in place with `setVoxel` and `propagateVoxelChange`, so bodies cross chunk
33
+ * borders without snagging and a block removed under a body lets it fall. The solver panics when a ball collider
34
+ * meets a voxel edge, so a chunk within `radius` of a body with a `'sphere'` collider
35
+ * also gets a triangle mesh of the same open faces (merged into rectangles, `fixInternalEdges` so balls roll over
36
+ * its edges and seams smoothly), split into pieces of at most 2,048 triangles, and its voxel collider is disabled,
37
+ * still updated and coupled so the voxel chunks beside it stay seamless; that lasts until no ball body is within
38
+ * `radius + 1`. An edit rebuilds only the pieces holding the changed cell and its neighbours.
39
+ *
40
+ * Building is spread over steps by {@link VoxelPhysics.budget}: what a body can reach before the next step is built
41
+ * at once, the rest nearest first within the budget. A chunk keeps its voxel collider until its mesh is complete, a
42
+ * ball arriving early switches it at once (with the pieces around every body built in that step), and an edited
43
+ * piece keeps its previous mesh until the rebuild replaces it.
44
+ *
45
+ * Every collider belongs to {@link VoxelPhysics.body}, one fixed `PhysicsBody`, so `Physics` collision events
46
+ * (`contactstart`, `contactend`, and sensor intersections) and queries report that body for contacts with blocks;
47
+ * {@link VoxelPhysics.chunkOf} names the chunk of a collider.
48
+ */
49
+ export declare class VoxelPhysics {
50
+ /** Chunks per axis around each moving body's chunk that get colliders. */
51
+ readonly radius: number;
52
+ /**
53
+ * The fixed body that owns every voxel collider and mesh piece. Collision events between a body and the blocks
54
+ * name it as `bodyA` or `bodyB` (`event.bodyB === voxels.body`); its `userData` is this VoxelPhysics. Removing it
55
+ * disposes this VoxelPhysics.
56
+ */
57
+ readonly body: PhysicsBody;
58
+ private readonly physics;
59
+ private readonly world;
60
+ private limit;
61
+ private readonly entries;
62
+ private readonly work;
63
+ private readonly owners;
64
+ private readonly retired;
65
+ private centres;
66
+ private near;
67
+ private needed;
68
+ private readonly cost;
69
+ private readonly spheres;
70
+ private readonly cells;
71
+ private readonly mask;
72
+ private readonly faces;
73
+ private readonly plane;
74
+ private readonly vertices;
75
+ private readonly indices;
76
+ private solid;
77
+ private part;
78
+ private disposed;
79
+ private readonly offStep;
80
+ private readonly offChange;
81
+ /**
82
+ * Keeps colliders for `world`'s chunks near the bodies of `physics`, from the next physics step on, on a new fixed
83
+ * body ({@link VoxelPhysics.body}).
84
+ * @throws TypeError when `physics` is not a `@margoengine/physics` Physics or `world` is not a {@link VoxelWorld}.
85
+ * @throws RangeError (TypeError for a non-number) unless `options.radius` is an integer >= 1 and `options.budget`
86
+ * a number >= 0.
87
+ * @throws MargoError `'disposed'` when `physics` is disposed, `'unsupported'` when its solver has no `voxels` shape.
88
+ */
89
+ constructor(physics: Physics, world: VoxelWorld, options?: VoxelPhysicsOptions);
90
+ /**
91
+ * Milliseconds of collider building per physics step (default 2; live). Work a body can reach before the next step
92
+ * (its own surroundings, the mesh pieces around a ball, an edited piece under a body) is always done in that step.
93
+ * The rest (chunks entering the radius, meshes still away from balls, stale borders) is built nearest first while
94
+ * the step's estimated cost stays within the budget; a step with nothing urgent still does one unit (one chunk's
95
+ * voxel collider, one scan or one mesh piece), so building always progresses. `Infinity` builds everything at once.
96
+ * @throws RangeError for a negative or NaN value, TypeError for a non-number.
97
+ */
98
+ get budget(): number;
99
+ set budget(value: number);
100
+ /** Chunks with collider work left: a voxel collider or mesh pieces still to build or rebuild. */
101
+ get pending(): number;
102
+ /**
103
+ * A snapshot of the colliders bodies touch, by chunk key ({@link chunkKey}): the chunk's voxel collider, or the
104
+ * triangle-mesh pieces of a chunk near ball bodies. A chunk whose first collider is still pending is missing. A
105
+ * collider is replaced when its chunk is replaced or comes back into range, and a mesh piece also when it is edited,
106
+ * so settings changed on one (friction, groups) do not last.
107
+ */
108
+ get colliders(): ReadonlyMap<string, readonly PhysicsCollider[]>;
109
+ /**
110
+ * The chunk key ({@link chunkKey}) of one of this VoxelPhysics' colliders (voxels or mesh piece), such as the
111
+ * `colliderA` or `colliderB` of a collision event with {@link VoxelPhysics.body}; undefined for any other collider.
112
+ */
113
+ chunkOf(collider: PhysicsCollider): string | undefined;
114
+ /**
115
+ * Removes {@link VoxelPhysics.body} with every collider and stops following the world and the physics steps. Calling
116
+ * it again does nothing.
117
+ */
118
+ dispose(): void;
119
+ [Symbol.dispose]: () => void;
120
+ private update;
121
+ private survey;
122
+ private build;
123
+ private unit;
124
+ private settle;
125
+ private distance;
126
+ private reaches;
127
+ private reach;
128
+ private setMode;
129
+ private activate;
130
+ private attach;
131
+ private retire;
132
+ private remove;
133
+ private solidIds;
134
+ private partIds;
135
+ private octantsAt;
136
+ private shapes;
137
+ private reshape;
138
+ private scan;
139
+ private voxels;
140
+ private piece;
141
+ private triangles;
142
+ private layer;
143
+ private readonly edit;
144
+ private set;
145
+ private propagate;
146
+ private touch;
147
+ }