@khr3/roads 0.1.0 → 0.1.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 +26 -11
- package/dist/terrain.d.ts +18 -2
- package/dist/terrain.js +62 -6
- package/docs/api-reference.md +84 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,21 +1,36 @@
|
|
|
1
1
|
# @khr3/roads
|
|
2
2
|
|
|
3
|
-
Spline-first roads and playable terrain for khr3 worlds. Zero dependencies, deterministic from a seed.
|
|
3
|
+
Spline-first roads and playable terrain for `@khr3/core` and `@pinkaphant/kolmo` worlds. Zero dependencies, deterministic from a seed.
|
|
4
4
|
|
|
5
|
-
- **`growRoad(seed, opts)`**: a seeded
|
|
6
|
-
- **`makeTerrain(seed, road, opts)`**:
|
|
7
|
-
- **`
|
|
8
|
-
- **`
|
|
5
|
+
- **`growRoad(seed, opts)`**: a seeded centripetal Catmull-Rom road resampled by arc length with bounded curvature (`maxTurn`) and grade (`maxGrade`).
|
|
6
|
+
- **`makeTerrain(seed, road, opts)`**: continuous terrain that equals **exact** road elevation across the carriageway and verge (`halfWidth + verge`) and blends smoothly into fractal FBM wild land with slope bounded by `maxSlope`.
|
|
7
|
+
- **`sampleRapierHeightfield(terrain, origin, size, n)`**: `(n + 1) × (n + 1)` **column-major** `Float32Array` (`out[xCol * (n + 1) + zRow]`) ready for Rapier WASM `ColliderDesc.heightfield`, `@khr3/core` `<KhrHeightfield>`, and `@pinkaphant/kolmo` `<Terrain>`.
|
|
8
|
+
- **`sampleHeightfield(terrain, origin, size, n)`**: `(n + 1) × (n + 1)` **row-major** `Float32Array` (`out[zRow * (n + 1) + xCol]`) matching Three.js `PlaneGeometry` vertex order (use `transposeHeightfield(grid, n)` to convert).
|
|
9
|
+
- **`frontage(road, { spacing, setback })`**: road-facing building lots on both sides of the road for `@khr3/c12` settlements.
|
|
10
|
+
|
|
11
|
+
Full API reference: [`docs/api-reference.md`](./docs/api-reference.md)
|
|
9
12
|
|
|
10
13
|
```ts
|
|
11
|
-
import { growRoad, makeTerrain,
|
|
14
|
+
import { growRoad, makeTerrain, sampleRapierHeightfield, frontage } from "@khr3/roads";
|
|
12
15
|
|
|
13
|
-
const road = growRoad(42, {
|
|
16
|
+
const road = growRoad(42, {
|
|
17
|
+
length: 2000,
|
|
18
|
+
halfWidth: 4,
|
|
19
|
+
controlSpacing: 40,
|
|
20
|
+
maxTurn: 0.3,
|
|
21
|
+
maxGrade: 0.08,
|
|
22
|
+
hilliness: 30,
|
|
23
|
+
});
|
|
14
24
|
const land = makeTerrain(42, road, {
|
|
15
|
-
amplitude: 18,
|
|
16
|
-
|
|
25
|
+
amplitude: 18,
|
|
26
|
+
noise: { wavelength: 160, octaves: 4, gain: 0.5, lacunarity: 2 },
|
|
27
|
+
maxSlope: 0.6,
|
|
28
|
+
minShoulder: 6,
|
|
29
|
+
verge: 1.5,
|
|
17
30
|
});
|
|
18
|
-
|
|
31
|
+
|
|
32
|
+
const heights = sampleRapierHeightfield(land, [-200, 0, -200], 400, 128);
|
|
33
|
+
const lots = frontage(road, { spacing: 15, setback: 2 });
|
|
19
34
|
```
|
|
20
35
|
|
|
21
|
-
Frame: SI metres,
|
|
36
|
+
Frame: SI metres, `+Y` up, `+Z` forward. Heading `0` faces `+Z`.
|
package/dist/terrain.d.ts
CHANGED
|
@@ -34,5 +34,21 @@ export interface Terrain {
|
|
|
34
34
|
export declare const SHOULDER_K = 1.6;
|
|
35
35
|
/** Build terrain for a road. Shoulder width adapts per point so the blend stays walkable. */
|
|
36
36
|
export declare function makeTerrain(seed: number, road: Road, o: TerrainOptions): Terrain;
|
|
37
|
-
/**
|
|
38
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Row-major heightfield (`(n + 1) * (n + 1)` vertices: `out[i * (n + 1) + j]` where `i` steps along +Z and `j` steps along +X)
|
|
39
|
+
* matching Three.js `PlaneGeometry` vertex order.
|
|
40
|
+
* For Rapier `ColliderDesc.heightfield` (column-major), use `sampleRapierHeightfield`, `transposeHeightfield`,
|
|
41
|
+
* or pass `layout="row-major"` to `<KhrHeightfield>` / `<KolmoTerrain>`.
|
|
42
|
+
*/
|
|
43
|
+
export declare function sampleHeightfield(t: Terrain, origin: V3, size: number, n: number, out?: Float32Array): Float32Array;
|
|
44
|
+
/**
|
|
45
|
+
* Column-major heightfield (`(n + 1) * (n + 1)` vertices: `out[j * (n + 1) + i]` where `j` steps along +X and `i` steps along +Z)
|
|
46
|
+
* ready for Rapier WASM `ColliderDesc.heightfield(n, n, heights, { x: size, y: 1, z: size })`,
|
|
47
|
+
* `@khr3/core` `<KhrHeightfield>`, and `@pinkaphant/kolmo` `<Terrain>`.
|
|
48
|
+
*/
|
|
49
|
+
export declare function sampleRapierHeightfield(t: Terrain, origin: V3, size: number, n: number, out?: Float32Array): Float32Array;
|
|
50
|
+
/**
|
|
51
|
+
* Transposes an `(n + 1) * (n + 1)` square height grid in O(N^2) between row-major (`sampleHeightfield`)
|
|
52
|
+
* and Rapier column-major (`sampleRapierHeightfield`).
|
|
53
|
+
*/
|
|
54
|
+
export declare function transposeHeightfield(grid: ArrayLike<number>, n: number, out?: Float32Array): Float32Array;
|
package/dist/terrain.js
CHANGED
|
@@ -65,12 +65,68 @@ export function makeTerrain(seed, road, o) {
|
|
|
65
65
|
};
|
|
66
66
|
return { road, height, wild, nearest };
|
|
67
67
|
}
|
|
68
|
-
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
/**
|
|
69
|
+
* Row-major heightfield (`(n + 1) * (n + 1)` vertices: `out[i * (n + 1) + j]` where `i` steps along +Z and `j` steps along +X)
|
|
70
|
+
* matching Three.js `PlaneGeometry` vertex order.
|
|
71
|
+
* For Rapier `ColliderDesc.heightfield` (column-major), use `sampleRapierHeightfield`, `transposeHeightfield`,
|
|
72
|
+
* or pass `layout="row-major"` to `<KhrHeightfield>` / `<KolmoTerrain>`.
|
|
73
|
+
*/
|
|
74
|
+
export function sampleHeightfield(t, origin, size, n, out) {
|
|
75
|
+
if (!Number.isInteger(n) || n < 1)
|
|
76
|
+
throw new RangeError("sampleHeightfield: n must be an integer >= 1");
|
|
77
|
+
if (!(size > 0))
|
|
78
|
+
throw new RangeError("sampleHeightfield: size must be > 0");
|
|
79
|
+
const total = (n + 1) * (n + 1);
|
|
80
|
+
const dst = out && out.length === total ? out : new Float32Array(total);
|
|
81
|
+
for (let i = 0; i <= n; i++) {
|
|
82
|
+
const z = origin[2] + (i / n) * size;
|
|
83
|
+
const rowBase = i * (n + 1);
|
|
84
|
+
for (let j = 0; j <= n; j++) {
|
|
85
|
+
dst[rowBase + j] = t.height(origin[0] + (j / n) * size, z);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return dst;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Column-major heightfield (`(n + 1) * (n + 1)` vertices: `out[j * (n + 1) + i]` where `j` steps along +X and `i` steps along +Z)
|
|
92
|
+
* ready for Rapier WASM `ColliderDesc.heightfield(n, n, heights, { x: size, y: 1, z: size })`,
|
|
93
|
+
* `@khr3/core` `<KhrHeightfield>`, and `@pinkaphant/kolmo` `<Terrain>`.
|
|
94
|
+
*/
|
|
95
|
+
export function sampleRapierHeightfield(t, origin, size, n, out) {
|
|
96
|
+
if (!Number.isInteger(n) || n < 1)
|
|
97
|
+
throw new RangeError("sampleRapierHeightfield: n must be an integer >= 1");
|
|
98
|
+
if (!(size > 0))
|
|
99
|
+
throw new RangeError("sampleRapierHeightfield: size must be > 0");
|
|
100
|
+
const strideZ = n + 1;
|
|
101
|
+
const total = strideZ * strideZ;
|
|
102
|
+
const dst = out && out.length === total ? out : new Float32Array(total);
|
|
103
|
+
for (let j = 0; j <= n; j++) {
|
|
104
|
+
const x = origin[0] + (j / n) * size;
|
|
105
|
+
const colBase = j * strideZ;
|
|
106
|
+
for (let i = 0; i <= n; i++) {
|
|
107
|
+
dst[colBase + i] = t.height(x, origin[2] + (i / n) * size);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return dst;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Transposes an `(n + 1) * (n + 1)` square height grid in O(N^2) between row-major (`sampleHeightfield`)
|
|
114
|
+
* and Rapier column-major (`sampleRapierHeightfield`).
|
|
115
|
+
*/
|
|
116
|
+
export function transposeHeightfield(grid, n, out) {
|
|
117
|
+
if (!Number.isInteger(n) || n < 1)
|
|
118
|
+
throw new RangeError("transposeHeightfield: n must be an integer >= 1");
|
|
119
|
+
const stride = n + 1;
|
|
120
|
+
const expected = stride * stride;
|
|
121
|
+
if (grid.length !== expected) {
|
|
122
|
+
throw new RangeError(`transposeHeightfield: expected (${n} + 1)^2 = ${expected} samples, got ${grid.length}`);
|
|
123
|
+
}
|
|
124
|
+
const dst = out && out.length === expected ? out : new Float32Array(expected);
|
|
125
|
+
for (let i = 0; i <= n; i++) {
|
|
126
|
+
const rowBase = i * stride;
|
|
72
127
|
for (let j = 0; j <= n; j++) {
|
|
73
|
-
|
|
128
|
+
dst[j * stride + i] = grid[rowBase + j];
|
|
74
129
|
}
|
|
75
|
-
|
|
130
|
+
}
|
|
131
|
+
return dst;
|
|
76
132
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# `@khr3/roads` API Reference
|
|
2
|
+
|
|
3
|
+
`@khr3/roads` provides zero-dependency, deterministic spline-first road generation, road-conformed terrain heightfields with adaptive slope shoulders, and frontage lot layout for `@khr3/c12` settlements, `@khr3/core`, and `@pinkaphant/kolmo`.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Import Paths
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import {
|
|
11
|
+
// Splines & roads
|
|
12
|
+
growRoad,
|
|
13
|
+
roadFromPoints,
|
|
14
|
+
maxGrade,
|
|
15
|
+
|
|
16
|
+
// Terrain & heightfields
|
|
17
|
+
makeTerrain,
|
|
18
|
+
sampleRapierHeightfield,
|
|
19
|
+
sampleHeightfield,
|
|
20
|
+
transposeHeightfield,
|
|
21
|
+
SHOULDER_K,
|
|
22
|
+
|
|
23
|
+
// Frontage lots
|
|
24
|
+
frontage,
|
|
25
|
+
|
|
26
|
+
// Deterministic PRNG & noise
|
|
27
|
+
mulberry32,
|
|
28
|
+
seedFrom,
|
|
29
|
+
childSeed,
|
|
30
|
+
valueNoise2,
|
|
31
|
+
fbm2,
|
|
32
|
+
|
|
33
|
+
// Types
|
|
34
|
+
type V3,
|
|
35
|
+
type Road,
|
|
36
|
+
type RoadSample,
|
|
37
|
+
type GrowRoadOptions,
|
|
38
|
+
type Terrain,
|
|
39
|
+
type TerrainOptions,
|
|
40
|
+
type NearestOnRoad,
|
|
41
|
+
type FrontageLot,
|
|
42
|
+
type FrontageOptions,
|
|
43
|
+
type FbmOptions,
|
|
44
|
+
type Rng,
|
|
45
|
+
} from "@khr3/roads";
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Core Functions
|
|
51
|
+
|
|
52
|
+
| Function | Signature | Description |
|
|
53
|
+
| --- | --- | --- |
|
|
54
|
+
| `growRoad` | `(seed: number, opts: GrowRoadOptions) => Road` | Grows a seeded centripetal Catmull-Rom road resampled by arc length with bounded horizontal curvature (`maxTurn`) and vertical grade (`maxGrade`). |
|
|
55
|
+
| `roadFromPoints` | `(points: readonly V3[], halfWidth: number, step?: number) => Road` | Constructs an arc-length-resampled centripetal Catmull-Rom `Road` through authored 3D control points. |
|
|
56
|
+
| `maxGrade` | `(road: Road) => number` | Computes the maximum $|\Delta y / \Delta s|$ grade across consecutive samples. |
|
|
57
|
+
| `makeTerrain` | `(seed: number, road: Road, opts: TerrainOptions) => Terrain` | Builds a continuous terrain surface whose height equals exact road height across `halfWidth + verge` and blends into fractal FBM wild land with shoulder slope bounded by `maxSlope`. |
|
|
58
|
+
| `sampleRapierHeightfield` | `(t: Terrain, origin: V3, size: number, n: number, out?: Float32Array) => Float32Array` | Samples `(n + 1) * (n + 1)` elevations over `[origin[0], origin[0] + size] × [origin[2], origin[2] + size]` in **Rapier column-major** order (`out[xCol * (n + 1) + zRow]`). Pass directly to `@khr3/core` `<KhrHeightfield>`, `@pinkaphant/kolmo` `<Terrain>`, or `RAPIER.ColliderDesc.heightfield`. |
|
|
59
|
+
| `sampleHeightfield` | `(t: Terrain, origin: V3, size: number, n: number, out?: Float32Array) => Float32Array` | Samples `(n + 1) * (n + 1)` elevations in **row-major** order (`out[zRow * (n + 1) + xCol]`), matching Three.js `PlaneGeometry` vertex order. |
|
|
60
|
+
| `transposeHeightfield` | `(grid: ArrayLike<number>, n: number, out?: Float32Array) => Float32Array` | Transposes an `(n + 1) * (n + 1)` grid between row-major and Rapier column-major order. |
|
|
61
|
+
| `frontage` | `(road: Road, opts: FrontageOptions) => FrontageLot[]` | Emits road-facing building lots along `"left"` and/or `"right"` sides of `road` at `halfWidth + setback`. |
|
|
62
|
+
| `seedFrom` | `(text: string \| number) => number` | Hashes a string or number into an unsigned 32-bit seed (FNV-1a). |
|
|
63
|
+
| `childSeed` | `(seed: number, salt: string) => number` | Derives an independent deterministic child seed per subsystem. |
|
|
64
|
+
| `mulberry32` | `(seed: number) => () => number` | Fast deterministic 32-bit PRNG yielding floats in `[0, 1)`. |
|
|
65
|
+
| `valueNoise2` / `fbm2` | `(seed, x, z, opts?) => number` | $C^2$-continuous 2D value noise and multi-octave FBM in `[-1, 1]`. |
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Heightfield Layout Note (Rapier vs Three.js `PlaneGeometry`)
|
|
70
|
+
|
|
71
|
+
Rapier 0.19 `ColliderDesc.heightfield(nrows, ncols, heights, { x: size, y: 1, z: size })` indexes `heights` **column-major** (`xCol * (nrows + 1) + zRow`) and centers the collider at the rigid body's `[cx, 0, cz]` (`origin = [cx - size / 2, 0, cz - size / 2]`).
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
// Centered at world [0, 0, 0] spanning [-200, +200] x [-200, +200]:
|
|
75
|
+
const heights = sampleRapierHeightfield(terrain, [-200, 0, -200], 400, 128);
|
|
76
|
+
// Use directly with @pinkaphant/kolmo <Terrain> or @khr3/core <KhrHeightfield>:
|
|
77
|
+
<Terrain heights={heights} segments={128} size={[400, 400]} position={[0, 0, 0]} />
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Frame & Units
|
|
83
|
+
|
|
84
|
+
- **SI Right-Handed Frame**: `1.0 unit === 1.0 metre`, `+Y` up, `+Z` forward (heading `0` faces `+Z`; positive heading turns `+Z` toward `+X`).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@khr3/roads",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Seeded spline-first roads and playable terrain that meets the road edge exactly, for khr3 worlds. Zero dependencies.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
18
18
|
"dist",
|
|
19
|
+
"docs",
|
|
19
20
|
"LICENSE",
|
|
20
21
|
"README.md"
|
|
21
22
|
],
|