roamwright 0.61.19 → 0.61.20

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.
@@ -203,6 +203,19 @@ export function plainUnitShapes() {
203
203
  };
204
204
  return plain;
205
205
  }
206
+ /**
207
+ * The unit shapes a simple figure is built from, each new on every call: a torso and a skirt widening downward from their
208
+ * foot, a round head about its centre, and an open half-cloak hanging from its top edge, its back toward -z.
209
+ * @returns {{torso: THREE.BufferGeometry, skirt: THREE.BufferGeometry, head: THREE.BufferGeometry, cloak: THREE.BufferGeometry}}
210
+ */
211
+ export function createFigureUnitShapes() {
212
+ return {
213
+ torso: new THREE.CylinderGeometry(0.5, 0.4, 1, 12).translate(0, 0.5, 0),
214
+ skirt: new THREE.CylinderGeometry(0.32, 0.5, 1, 12).translate(0, 0.5, 0),
215
+ head: new THREE.IcosahedronGeometry(0.5, 2),
216
+ cloak: new THREE.CylinderGeometry(0.5, 0.64, 1, 10, 3, true, Math.PI * 0.5, Math.PI).translate(0, -0.5, 0),
217
+ };
218
+ }
206
219
  function C(hex, jitter = 0, random) {
207
220
  // sRGB hex -> linear [r,g,b], optionally brightness-jittered
208
221
  colorScratch.setHex(hex);
package/index.js CHANGED
@@ -29,10 +29,11 @@ export { createNoise } from './math/noise.js';
29
29
  export { mulberry32 } from './math/random.js';
30
30
  export { GB as GeometryBuilder, geometryOf } from './geometry/builder.js';
31
31
  export { stillMesh } from './geometry/still-mesh.js';
32
- export { createUnitShapes } from './geometry/primitives.js';
32
+ export { createUnitShapes, createFigureUnitShapes } from './geometry/primitives.js';
33
33
 
34
34
  // ── Spaces & player motion ───────────────────────────────────────────────
35
35
  export { createSpaces } from './motion/spaces.js';
36
+ export { createTriangleHeightField } from './motion/triangle-height-field.js';
36
37
  export { createPlayer } from './motion/player.js';
37
38
 
38
39
  // ── Terrain & land baking ────────────────────────────────────────────────
@@ -0,0 +1,177 @@
1
+ /**
2
+ * A walkable height field over a triangle soup, for a solid the player can walk on and be stopped by, a moving one
3
+ * included (query it in the solid's own frame). Faces are sorted into floors and cliffs by how steeply they stand, kept
4
+ * packed ten numbers each, and listed by the cells of a square grid they cover, so a query reads only the faces near it.
5
+ */
6
+
7
+ const FACE = 10; // a face's corner, its two edges from it, and 1/area seen from above for a floor (0 for a wall)
8
+
9
+ /**
10
+ * Makes an empty height field to fill with triangles.
11
+ * @param {object} [options]
12
+ * @param {number} [options.cell=8] the grid's cell size, in the soup's units
13
+ * @param {number} [options.firstFaces=4096] faces room is made for at first; the room doubles as it fills
14
+ * @param {number} [options.floorSlope=0.17] a face is a floor where its upward share of the normal is over this
15
+ * @param {number} [options.cliffSlope=0.62] and a cliff where it is under this (a face between the two is both)
16
+ * @param {number} [options.reach=0.31] how far ahead of a step a cliff stops it
17
+ * @param {number[]} [options.rays] the heights over the feet at which a step is tested against cliffs (knee and eye)
18
+ * @returns {{
19
+ * add(positions: ArrayLike<number>, start?: number): void,
20
+ * seal(): void,
21
+ * at(x: number, z: number, limit: number): number,
22
+ * blocked(x: number, feet: number, z: number, toX: number, toZ: number): boolean,
23
+ * }} `add` takes non-indexed triangle positions (nine numbers a face) from `start`; `seal` lets go of unused room once
24
+ * every face is in; `at` is the highest floor under (x, z) no higher than `limit`, or -Infinity; `blocked` says
25
+ * whether a step from (x, z) to (toX, toZ) with the feet at `feet` runs into a cliff facing it.
26
+ */
27
+ export function createTriangleHeightField({
28
+ cell = 8,
29
+ firstFaces = 4096,
30
+ floorSlope = 0.17,
31
+ cliffSlope = 0.62,
32
+ reach = 0.31,
33
+ rays = [0.97, 0.97 + 0.68],
34
+ } = {}) {
35
+ const cells = new Map(),
36
+ cliffs = new Map(),
37
+ key = (x, z) => x * 65536 + z;
38
+ let faces = new Float32Array(firstFaces * FACE),
39
+ count = 0;
40
+ function store(map, k, face) {
41
+ let bucket = map.get(k);
42
+ if (!bucket) {
43
+ bucket = [];
44
+ map.set(k, bucket);
45
+ }
46
+ bucket.push(face);
47
+ }
48
+ function newFace(x, y, z, ux, uy, uz, vx, vy, vz, inv) {
49
+ if ((count + 1) * FACE > faces.length) {
50
+ const more = new Float32Array(faces.length * 2);
51
+ more.set(faces);
52
+ faces = more;
53
+ }
54
+ const o = count * FACE;
55
+ faces[o] = x;
56
+ faces[o + 1] = y;
57
+ faces[o + 2] = z;
58
+ faces[o + 3] = ux;
59
+ faces[o + 4] = uy;
60
+ faces[o + 5] = uz;
61
+ faces[o + 6] = vx;
62
+ faces[o + 7] = vy;
63
+ faces[o + 8] = vz;
64
+ faces[o + 9] = inv;
65
+ return count++;
66
+ }
67
+ return {
68
+ add(p, start = 0) {
69
+ for (let i = start; i < p.length; i += 9) {
70
+ const x = p[i],
71
+ y = p[i + 1],
72
+ z = p[i + 2];
73
+ const ux = p[i + 3] - x,
74
+ uy = p[i + 4] - y,
75
+ uz = p[i + 5] - z,
76
+ vx = p[i + 6] - x,
77
+ vy = p[i + 7] - y,
78
+ vz = p[i + 8] - z;
79
+ const area = uz * vx - ux * vz;
80
+ const normal = Math.hypot(uy * vz - uz * vy, area, ux * vy - uy * vx);
81
+ if (normal < 1e-5) continue;
82
+ const isFloor = area > normal * floorSlope,
83
+ isCliff = area < normal * cliffSlope;
84
+ if (!isFloor && !isCliff) continue;
85
+ const face = newFace(x, y, z, ux, uy, uz, vx, vy, vz, isFloor ? 1 / area : 0);
86
+ const bx = p[i + 3],
87
+ bz = p[i + 5],
88
+ cx = p[i + 6],
89
+ cz = p[i + 8];
90
+ const x0 = Math.floor(Math.min(x, bx, cx) / cell),
91
+ x1 = Math.floor(Math.max(x, bx, cx) / cell);
92
+ const z0 = Math.floor(Math.min(z, bz, cz) / cell),
93
+ z1 = Math.floor(Math.max(z, bz, cz) / cell);
94
+ for (let ix = x0; ix <= x1; ix++)
95
+ for (let iz = z0; iz <= z1; iz++) {
96
+ const k = key(ix, iz);
97
+ if (isFloor) store(cells, k, face);
98
+ if (isCliff) store(cliffs, k, face);
99
+ }
100
+ }
101
+ },
102
+ seal() {
103
+ faces = faces.slice(0, count * FACE);
104
+ },
105
+ at(x, z, limit) {
106
+ const list = cells.get(key(Math.floor(x / cell), Math.floor(z / cell)));
107
+ let best = -Infinity;
108
+ if (!list) return best;
109
+ for (let i = 0; i < list.length; i++) {
110
+ const o = list[i] * FACE,
111
+ dx = x - faces[o],
112
+ dz = z - faces[o + 2],
113
+ ux = faces[o + 3],
114
+ uz = faces[o + 5],
115
+ vx = faces[o + 6],
116
+ vz = faces[o + 8],
117
+ inv = faces[o + 9];
118
+ const u = (dz * vx - dx * vz) * inv,
119
+ v = (uz * dx - ux * dz) * inv;
120
+ if (u < -1e-6 || v < -1e-6 || u + v > 1.000001) continue;
121
+ const y = faces[o + 1] + u * faces[o + 4] + v * faces[o + 7];
122
+ if (y > best && y <= limit) best = y;
123
+ }
124
+ return best;
125
+ },
126
+ blocked(x, feet, z, toX, toZ) {
127
+ const sx = toX - x,
128
+ sz = toZ - z,
129
+ length = Math.hypot(sx, sz);
130
+ if (length < 1e-5) return false;
131
+ const dx = sx * (1 + reach / length),
132
+ dz = sz * (1 + reach / length);
133
+ const x0 = Math.floor(Math.min(x, x + dx) / cell),
134
+ x1 = Math.floor(Math.max(x, x + dx) / cell);
135
+ const z0 = Math.floor(Math.min(z, z + dz) / cell),
136
+ z1 = Math.floor(Math.max(z, z + dz) / cell);
137
+ for (let ix = x0; ix <= x1; ix++)
138
+ for (let iz = z0; iz <= z1; iz++) {
139
+ const list = cliffs.get(key(ix, iz));
140
+ if (!list) continue;
141
+ for (let i = 0; i < list.length; i++) {
142
+ const o = list[i] * FACE,
143
+ ax = faces[o],
144
+ ay = faces[o + 1],
145
+ az = faces[o + 2],
146
+ ux = faces[o + 3],
147
+ uy = faces[o + 4],
148
+ uz = faces[o + 5],
149
+ vx = faces[o + 6],
150
+ vy = faces[o + 7],
151
+ vz = faces[o + 8];
152
+ // level rays at each height, culling back faces so a walker touching a wall can still step away from it
153
+ const hx = -dz * vy,
154
+ hy = dz * vx - dx * vz,
155
+ hz = dx * vy;
156
+ const det = ux * hx + uy * hy + uz * hz;
157
+ if (det <= 1e-6) continue;
158
+ const px = x - ax,
159
+ pz = z - az,
160
+ qy = pz * ux - px * uz;
161
+ for (let r = 0; r < rays.length; r++) {
162
+ const py = feet + rays[r] - ay,
163
+ u = (px * hx + py * hy + pz * hz) / det;
164
+ if (u < 0 || u > 1) continue;
165
+ const qx = py * uz - pz * uy,
166
+ qz = px * uy - py * ux;
167
+ const v = (dx * qx + dz * qz) / det;
168
+ if (v < 0 || u + v > 1) continue;
169
+ const t = (vx * qx + vy * qy + vz * qz) / det;
170
+ if (t > 0 && t <= 1) return true;
171
+ }
172
+ }
173
+ }
174
+ return false;
175
+ },
176
+ };
177
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roamwright",
3
- "version": "0.61.19",
3
+ "version": "0.61.20",
4
4
  "description": "A procedural worldbuilding engine built on top of Three.js. Generate landscapes with rivers and changing weather, animate crowds, and explore on foot or in flight.",
5
5
  "keywords": [
6
6
  "threejs",