nucleation 0.3.16 → 0.3.18

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/Brush.d.ts CHANGED
@@ -74,4 +74,17 @@ export class Brush {
74
74
  * (`colors.len() == stops.len() * 3`, `stops` non-empty).
75
75
  */
76
76
  static curveGradient(stops: Array<number>, colors: Array<number>, space: InterpolationSpace): Brush;
77
+
78
+ /**
79
+ * A field brush: color every voxel by evaluating a scalar field at its
80
+ * center, remapping `[lo, hi]` to `[0, 1]`, and reading a multi-stop
81
+ * gradient (`stops` in `[0, 1]`, `colors` as flat RGB triples). The
82
+ * field is any SDF JSON — the same language that builds geometry — so a
83
+ * `cells` node paints a Voronoi mosaic (`mode: value`) or cracks
84
+ * (`mode: f2MinusF1`), an fbm field a marble, a coordinate expression a
85
+ * stripe. Call `set_palette` to choose the block set. Errors with
86
+ * `Parse` on bad field JSON and `InvalidArgument` on a stops/colors
87
+ * length mismatch.
88
+ */
89
+ static field(fieldJson: string, stops: Array<number>, colors: Array<number>, lo: number, hi: number, space: InterpolationSpace): Brush;
77
90
  }
package/Brush.mjs CHANGED
@@ -234,6 +234,44 @@ export class Brush {
234
234
  }
235
235
  }
236
236
 
237
+ /**
238
+ * A field brush: color every voxel by evaluating a scalar field at its
239
+ * center, remapping `[lo, hi]` to `[0, 1]`, and reading a multi-stop
240
+ * gradient (`stops` in `[0, 1]`, `colors` as flat RGB triples). The
241
+ * field is any SDF JSON — the same language that builds geometry — so a
242
+ * `cells` node paints a Voronoi mosaic (`mode: value`) or cracks
243
+ * (`mode: f2MinusF1`), an fbm field a marble, a coordinate expression a
244
+ * stripe. Call `set_palette` to choose the block set. Errors with
245
+ * `Parse` on bad field JSON and `InvalidArgument` on a stops/colors
246
+ * length mismatch.
247
+ */
248
+ static field(fieldJson, stops, colors, lo, hi, space) {
249
+ let functionCleanupArena = new diplomatRuntime.CleanupArena();
250
+
251
+ const fieldJsonSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.str8(wasm, fieldJson)));
252
+ const stopsSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.slice(wasm, stops, "f32")));
253
+ const colorsSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.sliceWrapper(wasm, diplomatRuntime.DiplomatBuf.slice(wasm, colors, "u8")));
254
+ const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
255
+
256
+
257
+ const result = wasm.Brush_field(diplomatReceive.buffer, fieldJsonSlice.ptr, stopsSlice.ptr, colorsSlice.ptr, lo, hi, space.ffiValue);
258
+
259
+ try {
260
+ if (!diplomatReceive.resultFlag) {
261
+ const cause = new NucleationError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
262
+ throw new globalThis.Error('NucleationError.' + cause.value, { cause });
263
+ }
264
+ return new Brush(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
265
+ }
266
+
267
+ finally {
268
+ diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
269
+ functionCleanupArena.free();
270
+
271
+ diplomatReceive.free();
272
+ }
273
+ }
274
+
237
275
  constructor(symbol, ptr, selfEdge) {
238
276
  return this.#internalConstructor(...arguments)
239
277
  }
@@ -0,0 +1,39 @@
1
+ // generated by diplomat-tool
2
+ import type { Schematic } from "./Schematic"
3
+ import type { pointer, codepoint } from "./diplomat-runtime.d.ts";
4
+
5
+
6
+
7
+ export class DistanceField {
8
+ /** @internal */
9
+ get ffiValue(): pointer;
10
+ /** @internal */
11
+ constructor();
12
+
13
+
14
+ /**
15
+ * Distance transform of a build's occupied voxels: every solid block
16
+ * learns how many blocks it sits below the surface, and the gradient of
17
+ * that depth gives the outward normal. Computed once over the
18
+ * schematic's bounding box.
19
+ */
20
+ static fromSchematic(schematic: Schematic): DistanceField;
21
+
22
+ /**
23
+ * Blocks below the surface at a voxel: 0 for empty/outside, 1 at the
24
+ * surface, increasing inward.
25
+ */
26
+ depth(x: number, y: number, z: number): number;
27
+
28
+ /**
29
+ * The upward component of the outward surface normal: 1 on flat ground,
30
+ * 0 on a vertical face, negative under an overhang. The scalar to key
31
+ * slope-based landscaping on (grass on the flats, stone on the steeps).
32
+ */
33
+ slope(x: number, y: number, z: number): number;
34
+
35
+ /**
36
+ * The full outward surface normal as JSON `[nx, ny, nz]`.
37
+ */
38
+ normalJson(x: number, y: number, z: number): string;
39
+ }
@@ -0,0 +1,114 @@
1
+ // generated by diplomat-tool
2
+ import { Schematic } from "./Schematic.mjs"
3
+ import wasm from "./diplomat-wasm.mjs";
4
+ import * as diplomatRuntime from "./diplomat-runtime.mjs";
5
+
6
+ const DistanceField_box_destroy_registry = new FinalizationRegistry((ptr) => {
7
+ wasm.DistanceField_destroy(ptr);
8
+ });
9
+
10
+ export class DistanceField {
11
+ // Internal ptr reference:
12
+ #ptr = null;
13
+
14
+ // Lifetimes are only to keep dependencies alive.
15
+ // Since JS won't garbage collect until there are no incoming edges.
16
+ #selfEdge = [];
17
+
18
+ #internalConstructor(symbol, ptr, selfEdge) {
19
+ if (symbol !== diplomatRuntime.internalConstructor) {
20
+ console.error("DistanceField is an Opaque type. You cannot call its constructor.");
21
+ return;
22
+ }
23
+ this.#ptr = ptr;
24
+ this.#selfEdge = selfEdge;
25
+
26
+ // Are we being borrowed? If not, we can register.
27
+ if (this.#selfEdge.length === 0) {
28
+ DistanceField_box_destroy_registry.register(this, this.#ptr);
29
+ }
30
+
31
+ return this;
32
+ }
33
+ /** @internal */
34
+ get ffiValue() {
35
+ return this.#ptr;
36
+ }
37
+
38
+
39
+ /**
40
+ * Distance transform of a build's occupied voxels: every solid block
41
+ * learns how many blocks it sits below the surface, and the gradient of
42
+ * that depth gives the outward normal. Computed once over the
43
+ * schematic's bounding box.
44
+ */
45
+ static fromSchematic(schematic) {
46
+
47
+ const result = wasm.DistanceField_from_schematic(schematic.ffiValue);
48
+
49
+ try {
50
+ return new DistanceField(diplomatRuntime.internalConstructor, result, []);
51
+ }
52
+
53
+ finally {
54
+ diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Blocks below the surface at a voxel: 0 for empty/outside, 1 at the
60
+ * surface, increasing inward.
61
+ */
62
+ depth(x, y, z) {
63
+
64
+ const result = wasm.DistanceField_depth(this.ffiValue, x, y, z);
65
+
66
+ try {
67
+ return result;
68
+ }
69
+
70
+ finally {
71
+ diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
72
+ }
73
+ }
74
+
75
+ /**
76
+ * The upward component of the outward surface normal: 1 on flat ground,
77
+ * 0 on a vertical face, negative under an overhang. The scalar to key
78
+ * slope-based landscaping on (grass on the flats, stone on the steeps).
79
+ */
80
+ slope(x, y, z) {
81
+
82
+ const result = wasm.DistanceField_slope(this.ffiValue, x, y, z);
83
+
84
+ try {
85
+ return result;
86
+ }
87
+
88
+ finally {
89
+ diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
90
+ }
91
+ }
92
+
93
+ /**
94
+ * The full outward surface normal as JSON `[nx, ny, nz]`.
95
+ */
96
+ normalJson(x, y, z) {
97
+ const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
98
+
99
+ wasm.DistanceField_normal_json(this.ffiValue, x, y, z, write.buffer);
100
+
101
+ try {
102
+ return write.readString8();
103
+ }
104
+
105
+ finally {
106
+ diplomatRuntime.FUNCTION_PARAM_ALLOC.clean();
107
+ write.free();
108
+ }
109
+ }
110
+
111
+ constructor(symbol, ptr, selfEdge) {
112
+ return this.#internalConstructor(...arguments)
113
+ }
114
+ }
package/index.d.ts CHANGED
@@ -38,6 +38,8 @@ export { Diff } from "./Diff"
38
38
 
39
39
  export { Fingerprint } from "./Fingerprint"
40
40
 
41
+ export { DistanceField } from "./DistanceField"
42
+
41
43
  export { Geo } from "./Geo"
42
44
 
43
45
  export { ChunkMeshResult } from "./ChunkMeshResult"
package/index.mjs CHANGED
@@ -36,6 +36,8 @@ export { Diff } from "./Diff.mjs"
36
36
 
37
37
  export { Fingerprint } from "./Fingerprint.mjs"
38
38
 
39
+ export { DistanceField } from "./DistanceField.mjs"
40
+
39
41
  export { Geo } from "./Geo.mjs"
40
42
 
41
43
  export { ChunkMeshResult } from "./ChunkMeshResult.mjs"
package/nucleation.wasm CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nucleation",
3
- "version": "0.3.16",
3
+ "version": "0.3.18",
4
4
  "description": "A high-performance Minecraft schematic parser and utility library",
5
5
  "type": "module",
6
6
  "main": "index.mjs",