@aethervtt/shard-mesh 0.1.0 → 0.2.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.
@@ -0,0 +1,146 @@
1
+ import type { MeshData } from './mesh.js';
2
+ /**
3
+ * A procedural modelling toolkit (spec 0045): append shapes, deform them, and finish into
4
+ * `MeshData`. Data lives in growable TypedArrays (no per-vertex objects), in f64 until `finish`.
5
+ *
6
+ * Deterministic: only +, −, ×, ÷, and sqrt (trig through `sinCos`), so the same calls give the same
7
+ * bytes in every JavaScript engine. That's what generators (0042) need.
8
+ */
9
+ export declare class MeshBuilder {
10
+ /** xyz per vertex. */
11
+ positions: Float64Array;
12
+ /** xyz per vertex (zero until normals are computed or a shape writes them). */
13
+ normalData: Float64Array;
14
+ /** uv per vertex. */
15
+ uvData: Float64Array;
16
+ /** rgba per vertex, or undefined until something sets a color. */
17
+ colorData: Float64Array | undefined;
18
+ /** Second uv set per vertex, or undefined until something sets one. */
19
+ uv1Data: Float64Array | undefined;
20
+ /** xyzw per vertex, after `tangents()`. */
21
+ tangentData: Float64Array | undefined;
22
+ indices: Uint32Array;
23
+ vertexCount: number;
24
+ /** Index count (three per triangle). */
25
+ indexCount: number;
26
+ /** Whether `normals` hold real normals for every vertex. */
27
+ private hasNormals;
28
+ static create(): MeshBuilder;
29
+ /** A builder holding a copy of `mesh`. */
30
+ static from(mesh: MeshData): MeshBuilder;
31
+ get triangleCount(): number;
32
+ private reserve;
33
+ /** Appends a vertex and returns its index. A zero normal means "compute it in `finish`". */
34
+ vertex(x: number, y: number, z: number, nx?: number, ny?: number, nz?: number, u?: number, v?: number): number;
35
+ triangle(a: number, b: number, c: number): this;
36
+ /** Two triangles, a-b-c and a-c-d (counter-clockwise front faces). */
37
+ quad(a: number, b: number, c: number, d: number): this;
38
+ /** Sets one vertex's color (linear rgba). Other vertices default to white. */
39
+ color(vertex: number, r: number, g: number, b: number, a?: number): this;
40
+ private ensureColors;
41
+ /**
42
+ * Sets one vertex's second uv set. Engine generators (0045) put a part code and a shade there
43
+ * (0 bark or base, 1 leaf or tip; a per-vertex variation), which vegetation materials tint by.
44
+ */
45
+ uv1(vertex: number, u: number, v: number): this;
46
+ /** Sets the second uv set of every vertex from `first` on. */
47
+ uv1All(u: number, v: number, first?: number): this;
48
+ /** Colors every vertex from `first` on (default: all of them). */
49
+ colorAll(r: number, g: number, b: number, a?: number, first?: number): this;
50
+ /** A sphere of `radius` subdivided `detail` times from an icosahedron (10 · 4^detail + 2 vertices). */
51
+ icosphere(detail: number, radius?: number): this;
52
+ /** An axis-aligned box centered on the origin, with flat faces. */
53
+ box(options?: {
54
+ x?: number;
55
+ y?: number;
56
+ z?: number;
57
+ }): this;
58
+ /**
59
+ * A surface of revolution around +Y: `profile` is [radius, y] pairs from bottom to top, swept
60
+ * through `sides` steps. A radius of 0 closes that end to a point.
61
+ */
62
+ lathe(profile: ArrayLike<number>, options?: {
63
+ sides?: number;
64
+ caps?: boolean;
65
+ }): this;
66
+ private cap;
67
+ /** A cylinder (or a cone, with `radiusTop` 0) on +Y, centered on the origin. */
68
+ cylinder(options?: {
69
+ radius?: number;
70
+ radiusTop?: number;
71
+ height?: number;
72
+ sides?: number;
73
+ caps?: boolean;
74
+ }): this;
75
+ /**
76
+ * A tube along a curve (xyz points): rings of `sides` vertices, oriented by parallel transport
77
+ * so they don't twist. `radius` is one number or one per point (0 at an end makes a point).
78
+ */
79
+ tube(curve: ArrayLike<number>, radius: number | ArrayLike<number>, options?: {
80
+ sides?: number;
81
+ caps?: boolean;
82
+ v0?: number;
83
+ vScale?: number;
84
+ }): this;
85
+ /**
86
+ * Moves every vertex along its normal by `fn(position, normal, index)` metres (normals are
87
+ * computed first where missing). Synchronous: for generator code, which runs off the main thread.
88
+ */
89
+ displace(fn: (position: Float64Array, normal: Float64Array, index: number) => number, first?: number): this;
90
+ /**
91
+ * Moves every vertex along its normal by `amount × noise(position)`, sampling all positions in one
92
+ * batch: `sample(points, out)` is a batch noise call, such as a generator's
93
+ * `(p, out) => ctx.noise.sample(graph, seed, p, out)`.
94
+ */
95
+ displaceNoise(sample: (points: Float32Array, out: Float32Array) => void, amount: number, first?: number): this;
96
+ /** Scales positions per axis about the origin (normals follow the inverse transpose). */
97
+ scale(x: number, y?: number, z?: number, first?: number): this;
98
+ /** Moves vertices from `first` on. */
99
+ translate(x: number, y: number, z: number, first?: number): this;
100
+ /**
101
+ * Transforms vertices from `first` on by a column-major 4×4 affine matrix (normals by its
102
+ * inverse transpose, renormalized).
103
+ */
104
+ transform(m: ArrayLike<number>, first?: number): this;
105
+ /**
106
+ * Extrudes a set of triangles (indices of triangles, i.e. index / 3) by `distance` along their
107
+ * average normal per vertex: the set moves out, and its boundary gets side walls.
108
+ */
109
+ extrude(faceSet: ArrayLike<number>, distance: number): this;
110
+ private copyVertex;
111
+ /**
112
+ * Appends another mesh (a builder or MeshData), optionally transformed by a column-major 4×4
113
+ * affine matrix.
114
+ */
115
+ merge(other: MeshBuilder | MeshData, transform?: ArrayLike<number>): this;
116
+ /**
117
+ * Merges vertices closer than `epsilon` (positions only; the first vertex's attributes win) and
118
+ * drops triangles that collapse.
119
+ */
120
+ weld(epsilon?: number): this;
121
+ /** Keeps vertices `keep[0..count)` (in that order) and rewrites indices through `remap`. */
122
+ private compact;
123
+ /**
124
+ * Recomputes normals from the triangles (area-weighted). Faces meeting at more than `angle`
125
+ * degrees keep a hard edge (their shared vertices are split); `angle: 180` smooths everything.
126
+ */
127
+ normals(options?: {
128
+ angle?: number;
129
+ }): this;
130
+ private computeNormals;
131
+ private ensureNormals;
132
+ /**
133
+ * Per-vertex tangents (xyzw, w the handedness) from the UV gradient (Lengyel's method, averaged
134
+ * per vertex), or any direction perpendicular to the normal where there's no gradient.
135
+ */
136
+ tangents(): this;
137
+ /**
138
+ * UVs projected along each vertex's dominant normal axis, `scale` metres per repeat: cheap
139
+ * texturing for rocks and crystals, with seams where the axis changes.
140
+ */
141
+ uvsTriplanar(scale?: number): this;
142
+ /** The mesh as `MeshData` (f32 attributes; u16 indices when they fit). Normals are filled in. */
143
+ finish(): MeshData;
144
+ }
145
+ /** Any unit vector perpendicular to (x, y, z) (a unit vector), written into `out`. */
146
+ export declare function perpendicular(x: number, y: number, z: number, out: Float64Array): Float64Array;