@pinkaphant/kolmo 0.5.1 → 0.5.3
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 +92 -14
- package/dist/engine/actor.d.ts +1 -1
- package/dist/engine/index.d.ts +3 -0
- package/dist/engine/index.js +94 -441
- package/dist/engine/index.js.map +1 -1
- package/dist/engine/surface.d.ts +5 -6
- package/dist/engine/traits.d.ts +27 -0
- package/dist/engine/world.d.ts +5 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +10 -6
- package/dist/physics/index.d.ts +2 -1
- package/dist/physics/index.js +3 -1
- package/dist/physics-KYWAXctg.js +233 -0
- package/dist/physics-KYWAXctg.js.map +1 -0
- package/dist/replay/index.d.ts +84 -0
- package/dist/replay/index.js +380 -0
- package/dist/replay/index.js.map +1 -0
- package/dist/terrain/index.d.ts +119 -0
- package/dist/terrain/index.js +495 -0
- package/dist/terrain/index.js.map +1 -0
- package/dist/toolgun/Toolgun.d.ts +17 -6
- package/dist/toolgun/index.d.ts +2 -2
- package/dist/toolgun/index.js +1 -1
- package/dist/toolgun/store.d.ts +25 -75
- package/dist/{toolgun-BfcCIAQx.js → toolgun-COu_5SxS.js} +391 -673
- package/dist/toolgun-COu_5SxS.js.map +1 -0
- package/dist/traits-Dp1ey_4w.js +267 -0
- package/dist/traits-Dp1ey_4w.js.map +1 -0
- package/dist/vite/index.js +1 -0
- package/dist/vite/index.js.map +1 -1
- package/dist/water/index.d.ts +71 -0
- package/dist/water/index.js +442 -0
- package/dist/water/index.js.map +1 -0
- package/dist/{surface-DUol_5ml.js → world-BDhBihjW.js} +442 -57
- package/dist/world-BDhBihjW.js.map +1 -0
- package/docs/api-reference.md +201 -0
- package/package.json +25 -8
- package/dist/physics-ZDFXaCli.js +0 -475
- package/dist/physics-ZDFXaCli.js.map +0 -1
- package/dist/surface-DUol_5ml.js.map +0 -1
- package/dist/toolgun-BfcCIAQx.js.map +0 -1
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import * as THREE from "three";
|
|
3
|
+
import type RAPIER from "@dimforge/rapier3d-compat";
|
|
4
|
+
import { type HeightfieldLayout } from "@khr3/core";
|
|
5
|
+
export type TerrainBrushMode = "add" | "flatten" | "smooth";
|
|
6
|
+
export interface TerrainBrushStamp {
|
|
7
|
+
readonly x: number;
|
|
8
|
+
readonly z: number;
|
|
9
|
+
readonly radius: number;
|
|
10
|
+
readonly strength: number;
|
|
11
|
+
readonly mode?: TerrainBrushMode;
|
|
12
|
+
readonly targetHeight?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface RollingBasinOptions {
|
|
15
|
+
readonly amplitudeM?: number;
|
|
16
|
+
readonly wavelengthM?: number;
|
|
17
|
+
readonly basinRadiusM?: number;
|
|
18
|
+
readonly basinDepthM?: number;
|
|
19
|
+
readonly seed?: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Smooth C^2 analytical height function producing a central lake basin surrounded by driveable banked ridges.
|
|
23
|
+
*/
|
|
24
|
+
export declare function createRollingBasinHeightFn(options?: RollingBasinOptions): (x: number, z: number) => number;
|
|
25
|
+
export interface KolmoTerrainFieldOptions {
|
|
26
|
+
readonly chunksX?: number;
|
|
27
|
+
readonly chunksZ?: number;
|
|
28
|
+
readonly cellsPerChunk?: number;
|
|
29
|
+
readonly cellSizeM?: number;
|
|
30
|
+
readonly originXZ?: readonly [number, number];
|
|
31
|
+
readonly heightFn?: (x: number, z: number) => number;
|
|
32
|
+
readonly friction?: number;
|
|
33
|
+
readonly restitution?: number;
|
|
34
|
+
}
|
|
35
|
+
export interface TerrainChunkBuffers {
|
|
36
|
+
readonly key: number;
|
|
37
|
+
readonly cx: number;
|
|
38
|
+
readonly cz: number;
|
|
39
|
+
readonly centerX: number;
|
|
40
|
+
readonly centerZ: number;
|
|
41
|
+
readonly spanM: number;
|
|
42
|
+
/** Column-major `(N + 1) * (N + 1)` heights (`r + c * (N + 1)`) matching parry3d `ColliderDesc.heightfield`. */
|
|
43
|
+
readonly rapierHeights: Float32Array;
|
|
44
|
+
readonly geometry: THREE.BufferGeometry;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Exact-math chunked heightfield storage whose indexed triangle diagonals (`(c1,r0)-(c0,r1)`)
|
|
48
|
+
* match `parry3d` `ColliderDesc.heightfield` to 0 ulp, with cross-chunk C^1 vertex normals and `.glb` export.
|
|
49
|
+
*/
|
|
50
|
+
export declare class KolmoTerrainField {
|
|
51
|
+
readonly chunksX: number;
|
|
52
|
+
readonly chunksZ: number;
|
|
53
|
+
readonly cellsPerChunk: number;
|
|
54
|
+
readonly cellSizeM: number;
|
|
55
|
+
readonly totalCellsX: number;
|
|
56
|
+
readonly totalCellsZ: number;
|
|
57
|
+
readonly vertsX: number;
|
|
58
|
+
readonly vertsZ: number;
|
|
59
|
+
readonly spanX: number;
|
|
60
|
+
readonly spanZ: number;
|
|
61
|
+
readonly minX: number;
|
|
62
|
+
readonly minZ: number;
|
|
63
|
+
readonly friction: number;
|
|
64
|
+
readonly restitution: number;
|
|
65
|
+
/** Global vertex height grid indexed as `gz * vertsX + gx`. */
|
|
66
|
+
readonly heights: Float32Array;
|
|
67
|
+
private readonly scratchHeights;
|
|
68
|
+
private readonly sharedIndices;
|
|
69
|
+
private readonly chunks;
|
|
70
|
+
private readonly dirtyChunks;
|
|
71
|
+
constructor(options?: KolmoTerrainFieldOptions);
|
|
72
|
+
getChunks(): readonly TerrainChunkBuffers[];
|
|
73
|
+
private syncChunkBuffers;
|
|
74
|
+
/**
|
|
75
|
+
* Evaluates the exact piecewise-linear surface height matching `parry3d` `HeightField`'s anti-diagonal triangles.
|
|
76
|
+
*/
|
|
77
|
+
sampleHeight(x: number, z: number): number;
|
|
78
|
+
/**
|
|
79
|
+
* Evaluates the exact facet normal of the active anti-diagonal heightfield triangle at `(x, z)`.
|
|
80
|
+
*/
|
|
81
|
+
sampleNormal(x: number, z: number, out?: THREE.Vector3): THREE.Vector3;
|
|
82
|
+
/**
|
|
83
|
+
* Sculpts the terrain with a C^2 radial falloff brush and marks only affected chunks (plus 1-vertex normal halo) dirty.
|
|
84
|
+
*/
|
|
85
|
+
applyBrush(stamp: TerrainBrushStamp): number;
|
|
86
|
+
consumeDirtyChunks(): TerrainChunkBuffers[];
|
|
87
|
+
dispose(): void;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Exports a `KolmoTerrainField` as a Khronos-compliant `KHR_physics_rigid_bodies` `.glb` binary
|
|
91
|
+
* with indexed static trimesh colliders (`convexHull: false`), ready to round-trip via `loadKhrGlb`.
|
|
92
|
+
*/
|
|
93
|
+
export declare function exportTerrainKhrGlb(field: KolmoTerrainField, material?: THREE.Material): Promise<Uint8Array>;
|
|
94
|
+
export interface KolmoTerrainHandle {
|
|
95
|
+
readonly field: KolmoTerrainField;
|
|
96
|
+
readonly body: RAPIER.RigidBody | null;
|
|
97
|
+
applyBrush: (stamp: TerrainBrushStamp) => number;
|
|
98
|
+
exportGlb: () => Promise<Uint8Array>;
|
|
99
|
+
}
|
|
100
|
+
export interface KolmoTerrainProps extends KolmoTerrainFieldOptions {
|
|
101
|
+
readonly ref?: React.Ref<KolmoTerrainHandle>;
|
|
102
|
+
readonly field?: KolmoTerrainField;
|
|
103
|
+
/** Optional pre-sampled height array (e.g. `@khr3/roads` `sampleRapierHeightfield` or `sampleHeightfield`). */
|
|
104
|
+
readonly heights?: Float32Array;
|
|
105
|
+
/** Cell subdivisions `n` or `[nrows (Z), ncols (X)]` when `heights` is provided (default `128`). */
|
|
106
|
+
readonly segments?: number | readonly [nrows: number, ncols: number];
|
|
107
|
+
/** World footprint `sizeM` or `[sizeX, sizeZ]` when `heights` is provided (default `[200, 200]`). */
|
|
108
|
+
readonly size?: number | readonly [sizeX: number, sizeZ: number];
|
|
109
|
+
/** World position `[x, y, z]` when `heights` is provided (default `[originXZ[0], 0, originXZ[1]]`). */
|
|
110
|
+
readonly position?: readonly [number, number, number];
|
|
111
|
+
/** Array layout when `heights` is provided (`"rapier-column-major"` default, or `"row-major"`). */
|
|
112
|
+
readonly layout?: HeightfieldLayout;
|
|
113
|
+
readonly color?: string;
|
|
114
|
+
readonly material?: THREE.Material;
|
|
115
|
+
readonly wireframe?: boolean;
|
|
116
|
+
readonly receiveShadow?: boolean;
|
|
117
|
+
readonly castShadow?: boolean;
|
|
118
|
+
}
|
|
119
|
+
export declare function KolmoTerrain(props: KolmoTerrainProps): React.JSX.Element;
|
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import { useEffect, useImperativeHandle, useMemo, useRef } from "react";
|
|
2
|
+
import * as THREE from "three";
|
|
3
|
+
import { useFrame } from "@react-three/fiber";
|
|
4
|
+
import { useRapier } from "@react-three/rapier";
|
|
5
|
+
import { KHR_HEIGHTFIELD_FLAGS, KhrHeightfield, createHeightfieldGeometry, exportKhrGlb } from "@khr3/core";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
// src/terrain/index.tsx
|
|
8
|
+
/**
|
|
9
|
+
* Smooth C^2 analytical height function producing a central lake basin surrounded by driveable banked ridges.
|
|
10
|
+
*/
|
|
11
|
+
function createRollingBasinHeightFn(options = {}) {
|
|
12
|
+
const amp = options.amplitudeM ?? 3.6;
|
|
13
|
+
const wl = Math.max(4, options.wavelengthM ?? 38);
|
|
14
|
+
const basinR = Math.max(4, options.basinRadiusM ?? 22);
|
|
15
|
+
const basinDepth = options.basinDepthM ?? 2.8;
|
|
16
|
+
const seed = options.seed ?? 1337;
|
|
17
|
+
const k1 = 2 * Math.PI / wl;
|
|
18
|
+
const k2 = k1 * 2.07;
|
|
19
|
+
const p1 = seed * .173 % (2 * Math.PI);
|
|
20
|
+
const p2 = seed * .619 % (2 * Math.PI);
|
|
21
|
+
return (x, z) => {
|
|
22
|
+
const u = Math.min(1, Math.hypot(x, z) / basinR);
|
|
23
|
+
const s5 = u * u * u * (u * (u * 6 - 15) + 10);
|
|
24
|
+
const hills = amp * (.62 * Math.sin(x * k1 + p1) * Math.cos(z * k1 - p2) + .26 * Math.cos((x * .8 + z * .6) * k2 + p2) + .12 * Math.sin((z * .85 - x * .52) * k2 * 1.6 - p1));
|
|
25
|
+
return -basinDepth * (1 - s5) + hills * (.18 + .82 * s5);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Exact-math chunked heightfield storage whose indexed triangle diagonals (`(c1,r0)-(c0,r1)`)
|
|
30
|
+
* match `parry3d` `ColliderDesc.heightfield` to 0 ulp, with cross-chunk C^1 vertex normals and `.glb` export.
|
|
31
|
+
*/
|
|
32
|
+
var KolmoTerrainField = class {
|
|
33
|
+
chunksX;
|
|
34
|
+
chunksZ;
|
|
35
|
+
cellsPerChunk;
|
|
36
|
+
cellSizeM;
|
|
37
|
+
totalCellsX;
|
|
38
|
+
totalCellsZ;
|
|
39
|
+
vertsX;
|
|
40
|
+
vertsZ;
|
|
41
|
+
spanX;
|
|
42
|
+
spanZ;
|
|
43
|
+
minX;
|
|
44
|
+
minZ;
|
|
45
|
+
friction;
|
|
46
|
+
restitution;
|
|
47
|
+
/** Global vertex height grid indexed as `gz * vertsX + gx`. */
|
|
48
|
+
heights;
|
|
49
|
+
scratchHeights;
|
|
50
|
+
sharedIndices;
|
|
51
|
+
chunks = [];
|
|
52
|
+
dirtyChunks = /* @__PURE__ */ new Set();
|
|
53
|
+
constructor(options = {}) {
|
|
54
|
+
const chunksX = Math.max(1, Math.trunc(options.chunksX ?? 4));
|
|
55
|
+
const chunksZ = Math.max(1, Math.trunc(options.chunksZ ?? 4));
|
|
56
|
+
const cellsPerChunk = Math.max(2, Math.trunc(options.cellsPerChunk ?? 24));
|
|
57
|
+
const cellSizeM = options.cellSizeM ?? 1.5;
|
|
58
|
+
if (!(cellSizeM > 0) || !Number.isFinite(cellSizeM)) throw new RangeError(`KolmoTerrainField: cellSizeM must be positive finite, got ${cellSizeM}`);
|
|
59
|
+
this.chunksX = chunksX;
|
|
60
|
+
this.chunksZ = chunksZ;
|
|
61
|
+
this.cellsPerChunk = cellsPerChunk;
|
|
62
|
+
this.cellSizeM = cellSizeM;
|
|
63
|
+
this.totalCellsX = chunksX * cellsPerChunk;
|
|
64
|
+
this.totalCellsZ = chunksZ * cellsPerChunk;
|
|
65
|
+
this.vertsX = this.totalCellsX + 1;
|
|
66
|
+
this.vertsZ = this.totalCellsZ + 1;
|
|
67
|
+
this.spanX = this.totalCellsX * cellSizeM;
|
|
68
|
+
this.spanZ = this.totalCellsZ * cellSizeM;
|
|
69
|
+
const ox = options.originXZ?.[0] ?? 0;
|
|
70
|
+
const oz = options.originXZ?.[1] ?? 0;
|
|
71
|
+
this.minX = ox - this.spanX * .5;
|
|
72
|
+
this.minZ = oz - this.spanZ * .5;
|
|
73
|
+
this.friction = options.friction ?? .85;
|
|
74
|
+
this.restitution = options.restitution ?? .04;
|
|
75
|
+
this.heights = new Float32Array(this.vertsX * this.vertsZ);
|
|
76
|
+
this.scratchHeights = new Float32Array(this.vertsX * this.vertsZ);
|
|
77
|
+
const fn = options.heightFn ?? createRollingBasinHeightFn();
|
|
78
|
+
for (let gz = 0; gz < this.vertsZ; gz++) {
|
|
79
|
+
const z = this.minZ + gz * cellSizeM;
|
|
80
|
+
const rowOff = gz * this.vertsX;
|
|
81
|
+
for (let gx = 0; gx < this.vertsX; gx++) {
|
|
82
|
+
const x = this.minX + gx * cellSizeM;
|
|
83
|
+
const y = fn(x, z);
|
|
84
|
+
if (!Number.isFinite(y)) throw new RangeError(`KolmoTerrainField: non-finite height at (${x}, ${z})`);
|
|
85
|
+
this.heights[rowOff + gx] = y;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const n = cellsPerChunk;
|
|
89
|
+
const stride = n + 1;
|
|
90
|
+
const idx = new Uint32Array(n * n * 6);
|
|
91
|
+
let p = 0;
|
|
92
|
+
for (let r = 0; r < n; r++) for (let c = 0; c < n; c++) {
|
|
93
|
+
const i00 = r * stride + c;
|
|
94
|
+
const i10 = i00 + 1;
|
|
95
|
+
const i01 = (r + 1) * stride + c;
|
|
96
|
+
const i11 = i01 + 1;
|
|
97
|
+
idx[p++] = i00;
|
|
98
|
+
idx[p++] = i01;
|
|
99
|
+
idx[p++] = i10;
|
|
100
|
+
idx[p++] = i10;
|
|
101
|
+
idx[p++] = i01;
|
|
102
|
+
idx[p++] = i11;
|
|
103
|
+
}
|
|
104
|
+
this.sharedIndices = idx;
|
|
105
|
+
const chunkSpan = n * cellSizeM;
|
|
106
|
+
const vertCount = stride * stride;
|
|
107
|
+
for (let cz = 0; cz < chunksZ; cz++) for (let cx = 0; cx < chunksX; cx++) {
|
|
108
|
+
const key = cz * chunksX + cx;
|
|
109
|
+
const centerX = this.minX + (cx + .5) * chunkSpan;
|
|
110
|
+
const centerZ = this.minZ + (cz + .5) * chunkSpan;
|
|
111
|
+
const posArr = new Float32Array(vertCount * 3);
|
|
112
|
+
const nrmArr = new Float32Array(vertCount * 3);
|
|
113
|
+
const uvArr = new Float32Array(vertCount * 2);
|
|
114
|
+
for (let r = 0; r <= n; r++) {
|
|
115
|
+
const lz = -chunkSpan * .5 + r * cellSizeM;
|
|
116
|
+
const gz = cz * n + r;
|
|
117
|
+
for (let c = 0; c <= n; c++) {
|
|
118
|
+
const lx = -chunkSpan * .5 + c * cellSizeM;
|
|
119
|
+
const gx = cx * n + c;
|
|
120
|
+
const vi = r * stride + c;
|
|
121
|
+
posArr[vi * 3 + 0] = lx;
|
|
122
|
+
posArr[vi * 3 + 2] = lz;
|
|
123
|
+
uvArr[vi * 2 + 0] = gx / this.totalCellsX;
|
|
124
|
+
uvArr[vi * 2 + 1] = gz / this.totalCellsZ;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const geo = new THREE.BufferGeometry();
|
|
128
|
+
geo.setAttribute("position", new THREE.BufferAttribute(posArr, 3));
|
|
129
|
+
geo.setAttribute("normal", new THREE.BufferAttribute(nrmArr, 3));
|
|
130
|
+
geo.setAttribute("uv", new THREE.BufferAttribute(uvArr, 2));
|
|
131
|
+
geo.setIndex(new THREE.BufferAttribute(this.sharedIndices, 1));
|
|
132
|
+
const chunk = {
|
|
133
|
+
key,
|
|
134
|
+
cx,
|
|
135
|
+
cz,
|
|
136
|
+
centerX,
|
|
137
|
+
centerZ,
|
|
138
|
+
spanM: chunkSpan,
|
|
139
|
+
rapierHeights: new Float32Array(vertCount),
|
|
140
|
+
geometry: geo
|
|
141
|
+
};
|
|
142
|
+
this.chunks.push(chunk);
|
|
143
|
+
this.syncChunkBuffers(chunk);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
getChunks() {
|
|
147
|
+
return this.chunks;
|
|
148
|
+
}
|
|
149
|
+
syncChunkBuffers(chunk) {
|
|
150
|
+
const n = this.cellsPerChunk;
|
|
151
|
+
const stride = n + 1;
|
|
152
|
+
const posAttr = chunk.geometry.getAttribute("position");
|
|
153
|
+
const nrmAttr = chunk.geometry.getAttribute("normal");
|
|
154
|
+
const pos = posAttr.array;
|
|
155
|
+
const nrm = nrmAttr.array;
|
|
156
|
+
const hCol = chunk.rapierHeights;
|
|
157
|
+
const invCell = 1 / this.cellSizeM;
|
|
158
|
+
for (let r = 0; r <= n; r++) {
|
|
159
|
+
const gz = chunk.cz * n + r;
|
|
160
|
+
const gzPrev = gz > 0 ? gz - 1 : 0;
|
|
161
|
+
const gzNext = gz < this.totalCellsZ ? gz + 1 : this.totalCellsZ;
|
|
162
|
+
const dzScale = invCell / Math.max(1, gzNext - gzPrev);
|
|
163
|
+
for (let c = 0; c <= n; c++) {
|
|
164
|
+
const gx = chunk.cx * n + c;
|
|
165
|
+
const gxPrev = gx > 0 ? gx - 1 : 0;
|
|
166
|
+
const gxNext = gx < this.totalCellsX ? gx + 1 : this.totalCellsX;
|
|
167
|
+
const dxScale = invCell / Math.max(1, gxNext - gxPrev);
|
|
168
|
+
const h = this.heights[gz * this.vertsX + gx];
|
|
169
|
+
const vi = r * stride + c;
|
|
170
|
+
pos[vi * 3 + 1] = h;
|
|
171
|
+
hCol[r + c * stride] = h;
|
|
172
|
+
const hx = (this.heights[gz * this.vertsX + gxNext] - this.heights[gz * this.vertsX + gxPrev]) * dxScale;
|
|
173
|
+
const hz = (this.heights[gzNext * this.vertsX + gx] - this.heights[gzPrev * this.vertsX + gx]) * dzScale;
|
|
174
|
+
const invLen = 1 / Math.hypot(hx, 1, hz);
|
|
175
|
+
nrm[vi * 3 + 0] = -hx * invLen;
|
|
176
|
+
nrm[vi * 3 + 1] = invLen;
|
|
177
|
+
nrm[vi * 3 + 2] = -hz * invLen;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
posAttr.needsUpdate = true;
|
|
181
|
+
nrmAttr.needsUpdate = true;
|
|
182
|
+
chunk.geometry.computeBoundingSphere();
|
|
183
|
+
chunk.geometry.computeBoundingBox();
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Evaluates the exact piecewise-linear surface height matching `parry3d` `HeightField`'s anti-diagonal triangles.
|
|
187
|
+
*/
|
|
188
|
+
sampleHeight(x, z) {
|
|
189
|
+
const fx = Math.min(this.totalCellsX, Math.max(0, (x - this.minX) / this.cellSizeM));
|
|
190
|
+
const fz = Math.min(this.totalCellsZ, Math.max(0, (z - this.minZ) / this.cellSizeM));
|
|
191
|
+
const gx = Math.min(this.totalCellsX - 1, Math.floor(fx));
|
|
192
|
+
const gz = Math.min(this.totalCellsZ - 1, Math.floor(fz));
|
|
193
|
+
const u = fx - gx;
|
|
194
|
+
const v = fz - gz;
|
|
195
|
+
const i00 = gz * this.vertsX + gx;
|
|
196
|
+
const h00 = this.heights[i00];
|
|
197
|
+
const h10 = this.heights[i00 + 1];
|
|
198
|
+
const h01 = this.heights[i00 + this.vertsX];
|
|
199
|
+
const h11 = this.heights[i00 + this.vertsX + 1];
|
|
200
|
+
if (u + v <= 1) return h00 * (1 - u - v) + h10 * u + h01 * v;
|
|
201
|
+
return h11 * (u + v - 1) + h10 * (1 - v) + h01 * (1 - u);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Evaluates the exact facet normal of the active anti-diagonal heightfield triangle at `(x, z)`.
|
|
205
|
+
*/
|
|
206
|
+
sampleNormal(x, z, out = new THREE.Vector3()) {
|
|
207
|
+
const fx = Math.min(this.totalCellsX, Math.max(0, (x - this.minX) / this.cellSizeM));
|
|
208
|
+
const fz = Math.min(this.totalCellsZ, Math.max(0, (z - this.minZ) / this.cellSizeM));
|
|
209
|
+
const gx = Math.min(this.totalCellsX - 1, Math.floor(fx));
|
|
210
|
+
const gz = Math.min(this.totalCellsZ - 1, Math.floor(fz));
|
|
211
|
+
const u = fx - gx;
|
|
212
|
+
const v = fz - gz;
|
|
213
|
+
const i00 = gz * this.vertsX + gx;
|
|
214
|
+
const h00 = this.heights[i00];
|
|
215
|
+
const h10 = this.heights[i00 + 1];
|
|
216
|
+
const h01 = this.heights[i00 + this.vertsX];
|
|
217
|
+
const h11 = this.heights[i00 + this.vertsX + 1];
|
|
218
|
+
const inv = 1 / this.cellSizeM;
|
|
219
|
+
const dhdx = (u + v <= 1 ? h10 - h00 : h11 - h01) * inv;
|
|
220
|
+
const dhdz = (u + v <= 1 ? h01 - h00 : h11 - h10) * inv;
|
|
221
|
+
return out.set(-dhdx, 1, -dhdz).normalize();
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Sculpts the terrain with a C^2 radial falloff brush and marks only affected chunks (plus 1-vertex normal halo) dirty.
|
|
225
|
+
*/
|
|
226
|
+
applyBrush(stamp) {
|
|
227
|
+
const radius = Math.max(this.cellSizeM, stamp.radius);
|
|
228
|
+
const mode = stamp.mode ?? "add";
|
|
229
|
+
const gxMin = Math.max(0, Math.floor((stamp.x - radius - this.minX) / this.cellSizeM));
|
|
230
|
+
const gxMax = Math.min(this.totalCellsX, Math.ceil((stamp.x + radius - this.minX) / this.cellSizeM));
|
|
231
|
+
const gzMin = Math.max(0, Math.floor((stamp.z - radius - this.minZ) / this.cellSizeM));
|
|
232
|
+
const gzMax = Math.min(this.totalCellsZ, Math.ceil((stamp.z + radius - this.minZ) / this.cellSizeM));
|
|
233
|
+
if (gxMin > gxMax || gzMin > gzMax) return 0;
|
|
234
|
+
const targetH = stamp.targetHeight ?? this.sampleHeight(stamp.x, stamp.z);
|
|
235
|
+
const r2 = radius * radius;
|
|
236
|
+
if (mode === "smooth") for (let gz = gzMin; gz <= gzMax; gz++) for (let gx = gxMin; gx <= gxMax; gx++) {
|
|
237
|
+
const xm = Math.max(0, gx - 1), xp = Math.min(this.totalCellsX, gx + 1);
|
|
238
|
+
const zm = Math.max(0, gz - 1), zp = Math.min(this.totalCellsZ, gz + 1);
|
|
239
|
+
this.scratchHeights[gz * this.vertsX + gx] = .25 * (this.heights[gz * this.vertsX + xm] + this.heights[gz * this.vertsX + xp] + this.heights[zm * this.vertsX + gx] + this.heights[zp * this.vertsX + gx]);
|
|
240
|
+
}
|
|
241
|
+
let touchedVerts = 0;
|
|
242
|
+
for (let gz = gzMin; gz <= gzMax; gz++) {
|
|
243
|
+
const dz = this.minZ + gz * this.cellSizeM - stamp.z;
|
|
244
|
+
for (let gx = gxMin; gx <= gxMax; gx++) {
|
|
245
|
+
const dx = this.minX + gx * this.cellSizeM - stamp.x;
|
|
246
|
+
const d2 = dx * dx + dz * dz;
|
|
247
|
+
if (d2 >= r2) continue;
|
|
248
|
+
const t = 1 - Math.sqrt(d2) / radius;
|
|
249
|
+
const weight = t * t * (3 - 2 * t);
|
|
250
|
+
const idx = gz * this.vertsX + gx;
|
|
251
|
+
const cur = this.heights[idx];
|
|
252
|
+
if (mode === "add") this.heights[idx] = cur + stamp.strength * weight;
|
|
253
|
+
else if (mode === "flatten") {
|
|
254
|
+
const alpha = Math.min(1, Math.max(0, stamp.strength) * weight);
|
|
255
|
+
this.heights[idx] = cur + (targetH - cur) * alpha;
|
|
256
|
+
} else {
|
|
257
|
+
const alpha = Math.min(1, Math.max(0, stamp.strength) * weight);
|
|
258
|
+
this.heights[idx] = cur + (this.scratchHeights[idx] - cur) * alpha;
|
|
259
|
+
}
|
|
260
|
+
touchedVerts++;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (touchedVerts === 0) return 0;
|
|
264
|
+
const n = this.cellsPerChunk;
|
|
265
|
+
const cx0 = Math.max(0, Math.floor(Math.max(0, gxMin - 1) / n));
|
|
266
|
+
const cx1 = Math.min(this.chunksX - 1, Math.floor(Math.min(this.totalCellsX, gxMax + 1) / n));
|
|
267
|
+
const cz0 = Math.max(0, Math.floor(Math.max(0, gzMin - 1) / n));
|
|
268
|
+
const cz1 = Math.min(this.chunksZ - 1, Math.floor(Math.min(this.totalCellsZ, gzMax + 1) / n));
|
|
269
|
+
for (let cz = cz0; cz <= cz1; cz++) for (let cx = cx0; cx <= cx1; cx++) {
|
|
270
|
+
const key = cz * this.chunksX + cx;
|
|
271
|
+
this.syncChunkBuffers(this.chunks[key]);
|
|
272
|
+
this.dirtyChunks.add(key);
|
|
273
|
+
}
|
|
274
|
+
return touchedVerts;
|
|
275
|
+
}
|
|
276
|
+
consumeDirtyChunks() {
|
|
277
|
+
if (this.dirtyChunks.size === 0) return [];
|
|
278
|
+
const out = [];
|
|
279
|
+
for (const key of this.dirtyChunks) out.push(this.chunks[key]);
|
|
280
|
+
this.dirtyChunks.clear();
|
|
281
|
+
return out;
|
|
282
|
+
}
|
|
283
|
+
dispose() {
|
|
284
|
+
for (const chunk of this.chunks) chunk.geometry.dispose();
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
/**
|
|
288
|
+
* Exports a `KolmoTerrainField` as a Khronos-compliant `KHR_physics_rigid_bodies` `.glb` binary
|
|
289
|
+
* with indexed static trimesh colliders (`convexHull: false`), ready to round-trip via `loadKhrGlb`.
|
|
290
|
+
*/
|
|
291
|
+
async function exportTerrainKhrGlb(field, material) {
|
|
292
|
+
const root = new THREE.Group();
|
|
293
|
+
root.name = "KolmoTerrain";
|
|
294
|
+
const ownMat = !material;
|
|
295
|
+
const mat = material ?? new THREE.MeshStandardMaterial({
|
|
296
|
+
color: "#4d6b53",
|
|
297
|
+
roughness: .88,
|
|
298
|
+
metalness: .04
|
|
299
|
+
});
|
|
300
|
+
const specs = [];
|
|
301
|
+
for (const chunk of field.getChunks()) {
|
|
302
|
+
const nodeName = `TerrainChunk_${chunk.cx}_${chunk.cz}`;
|
|
303
|
+
const mesh = new THREE.Mesh(chunk.geometry, mat);
|
|
304
|
+
mesh.name = nodeName;
|
|
305
|
+
mesh.position.set(chunk.centerX, 0, chunk.centerZ);
|
|
306
|
+
root.add(mesh);
|
|
307
|
+
specs.push({
|
|
308
|
+
nodeName,
|
|
309
|
+
collider: {
|
|
310
|
+
meshNodeName: nodeName,
|
|
311
|
+
convexHull: false,
|
|
312
|
+
friction: field.friction,
|
|
313
|
+
restitution: field.restitution
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
try {
|
|
318
|
+
return new Uint8Array(await exportKhrGlb(root, specs));
|
|
319
|
+
} finally {
|
|
320
|
+
if (ownMat) mat.dispose();
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
function ExplicitHeightfieldTerrain({ heights, segments = 128, size = [200, 200], position, originXZ, layout = "rapier-column-major", friction = .85, restitution = .04, color = "#4b6b50", material: externalMaterial, wireframe = false, receiveShadow = true, castShadow = false }) {
|
|
324
|
+
const nrows = typeof segments === "number" ? segments : segments[0];
|
|
325
|
+
const ncols = typeof segments === "number" ? segments : segments[1];
|
|
326
|
+
const sizeX = typeof size === "number" ? size : size[0];
|
|
327
|
+
const sizeZ = typeof size === "number" ? size : size[1];
|
|
328
|
+
const pos = position ? [
|
|
329
|
+
position[0],
|
|
330
|
+
position[1],
|
|
331
|
+
position[2]
|
|
332
|
+
] : [
|
|
333
|
+
originXZ?.[0] ?? 0,
|
|
334
|
+
0,
|
|
335
|
+
originXZ?.[1] ?? 0
|
|
336
|
+
];
|
|
337
|
+
const geo = useMemo(() => createHeightfieldGeometry(heights, sizeX, sizeZ, nrows, ncols, layout), [
|
|
338
|
+
heights,
|
|
339
|
+
sizeX,
|
|
340
|
+
sizeZ,
|
|
341
|
+
nrows,
|
|
342
|
+
ncols,
|
|
343
|
+
layout
|
|
344
|
+
]);
|
|
345
|
+
useEffect(() => () => geo.dispose(), [geo]);
|
|
346
|
+
const defaultMat = useMemo(() => externalMaterial ? null : new THREE.MeshStandardMaterial({
|
|
347
|
+
color,
|
|
348
|
+
roughness: .88,
|
|
349
|
+
metalness: .04,
|
|
350
|
+
wireframe
|
|
351
|
+
}), [
|
|
352
|
+
externalMaterial,
|
|
353
|
+
color,
|
|
354
|
+
wireframe
|
|
355
|
+
]);
|
|
356
|
+
useEffect(() => () => defaultMat?.dispose(), [defaultMat]);
|
|
357
|
+
return /* @__PURE__ */ jsx(KhrHeightfield, {
|
|
358
|
+
heights,
|
|
359
|
+
segments: [nrows, ncols],
|
|
360
|
+
size: [sizeX, sizeZ],
|
|
361
|
+
position: pos,
|
|
362
|
+
layout,
|
|
363
|
+
friction,
|
|
364
|
+
restitution,
|
|
365
|
+
userData: { name: "KolmoTerrain" },
|
|
366
|
+
children: /* @__PURE__ */ jsx("mesh", {
|
|
367
|
+
geometry: geo,
|
|
368
|
+
material: externalMaterial ?? defaultMat,
|
|
369
|
+
receiveShadow,
|
|
370
|
+
castShadow
|
|
371
|
+
})
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
function KolmoTerrain(props) {
|
|
375
|
+
if (props.heights) return /* @__PURE__ */ jsx(ExplicitHeightfieldTerrain, {
|
|
376
|
+
heights: props.heights,
|
|
377
|
+
segments: props.segments,
|
|
378
|
+
size: props.size,
|
|
379
|
+
position: props.position,
|
|
380
|
+
originXZ: props.originXZ,
|
|
381
|
+
layout: props.layout,
|
|
382
|
+
friction: props.friction,
|
|
383
|
+
restitution: props.restitution,
|
|
384
|
+
color: props.color,
|
|
385
|
+
material: props.material,
|
|
386
|
+
wireframe: props.wireframe,
|
|
387
|
+
receiveShadow: props.receiveShadow,
|
|
388
|
+
castShadow: props.castShadow
|
|
389
|
+
});
|
|
390
|
+
return /* @__PURE__ */ jsx(ChunkedKolmoTerrain, { ...props });
|
|
391
|
+
}
|
|
392
|
+
function ChunkedKolmoTerrain({ ref, field: externalField, chunksX, chunksZ, cellsPerChunk, cellSizeM, originXZ, heightFn, friction, restitution, color = "#4b6b50", material: externalMaterial, wireframe = false, receiveShadow = true, castShadow = false }) {
|
|
393
|
+
const { rapier, world } = useRapier();
|
|
394
|
+
const ownedField = useMemo(() => externalField ?? new KolmoTerrainField({
|
|
395
|
+
chunksX,
|
|
396
|
+
chunksZ,
|
|
397
|
+
cellsPerChunk,
|
|
398
|
+
cellSizeM,
|
|
399
|
+
originXZ,
|
|
400
|
+
heightFn,
|
|
401
|
+
friction,
|
|
402
|
+
restitution
|
|
403
|
+
}), [
|
|
404
|
+
externalField,
|
|
405
|
+
chunksX,
|
|
406
|
+
chunksZ,
|
|
407
|
+
cellsPerChunk,
|
|
408
|
+
cellSizeM,
|
|
409
|
+
originXZ,
|
|
410
|
+
heightFn,
|
|
411
|
+
friction,
|
|
412
|
+
restitution
|
|
413
|
+
]);
|
|
414
|
+
useEffect(() => {
|
|
415
|
+
if (externalField) return;
|
|
416
|
+
return () => ownedField.dispose();
|
|
417
|
+
}, [externalField, ownedField]);
|
|
418
|
+
const defaultMat = useMemo(() => externalMaterial ? null : new THREE.MeshStandardMaterial({
|
|
419
|
+
color,
|
|
420
|
+
roughness: .88,
|
|
421
|
+
metalness: .04,
|
|
422
|
+
wireframe
|
|
423
|
+
}), [
|
|
424
|
+
externalMaterial,
|
|
425
|
+
color,
|
|
426
|
+
wireframe
|
|
427
|
+
]);
|
|
428
|
+
useEffect(() => () => defaultMat?.dispose(), [defaultMat]);
|
|
429
|
+
const mat = externalMaterial ?? defaultMat;
|
|
430
|
+
const bodyRef = useRef(null);
|
|
431
|
+
const collidersRef = useRef(/* @__PURE__ */ new Map());
|
|
432
|
+
const buildChunkCollider = (body, chunk) => {
|
|
433
|
+
const n = ownedField.cellsPerChunk;
|
|
434
|
+
const desc = rapier.ColliderDesc.heightfield(n, n, chunk.rapierHeights, {
|
|
435
|
+
x: chunk.spanM,
|
|
436
|
+
y: 1,
|
|
437
|
+
z: chunk.spanM
|
|
438
|
+
}, KHR_HEIGHTFIELD_FLAGS).setTranslation(chunk.centerX, 0, chunk.centerZ).setFriction(ownedField.friction).setRestitution(ownedField.restitution);
|
|
439
|
+
return world.createCollider(desc, body);
|
|
440
|
+
};
|
|
441
|
+
useEffect(() => {
|
|
442
|
+
const body = world.createRigidBody(rapier.RigidBodyDesc.fixed());
|
|
443
|
+
body.userData = { name: "KolmoTerrain" };
|
|
444
|
+
bodyRef.current = body;
|
|
445
|
+
const map = /* @__PURE__ */ new Map();
|
|
446
|
+
ownedField.consumeDirtyChunks();
|
|
447
|
+
for (const chunk of ownedField.getChunks()) map.set(chunk.key, buildChunkCollider(body, chunk));
|
|
448
|
+
collidersRef.current = map;
|
|
449
|
+
return () => {
|
|
450
|
+
collidersRef.current.clear();
|
|
451
|
+
bodyRef.current = null;
|
|
452
|
+
if (world.getRigidBody(body.handle)) world.removeRigidBody(body);
|
|
453
|
+
};
|
|
454
|
+
}, [
|
|
455
|
+
rapier,
|
|
456
|
+
world,
|
|
457
|
+
ownedField
|
|
458
|
+
]);
|
|
459
|
+
useFrame(() => {
|
|
460
|
+
const body = bodyRef.current;
|
|
461
|
+
if (!body) return;
|
|
462
|
+
const dirty = ownedField.consumeDirtyChunks();
|
|
463
|
+
if (dirty.length === 0) return;
|
|
464
|
+
for (const chunk of dirty) {
|
|
465
|
+
const prev = collidersRef.current.get(chunk.key);
|
|
466
|
+
if (prev && world.getCollider(prev.handle)) world.removeCollider(prev, false);
|
|
467
|
+
collidersRef.current.set(chunk.key, buildChunkCollider(body, chunk));
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
useImperativeHandle(ref, () => ({
|
|
471
|
+
field: ownedField,
|
|
472
|
+
get body() {
|
|
473
|
+
return bodyRef.current;
|
|
474
|
+
},
|
|
475
|
+
applyBrush: (stamp) => ownedField.applyBrush(stamp),
|
|
476
|
+
exportGlb: () => exportTerrainKhrGlb(ownedField, mat)
|
|
477
|
+
}), [ownedField, mat]);
|
|
478
|
+
return /* @__PURE__ */ jsx("group", {
|
|
479
|
+
name: "KolmoTerrain",
|
|
480
|
+
children: ownedField.getChunks().map((chunk) => /* @__PURE__ */ jsx("mesh", {
|
|
481
|
+
position: [
|
|
482
|
+
chunk.centerX,
|
|
483
|
+
0,
|
|
484
|
+
chunk.centerZ
|
|
485
|
+
],
|
|
486
|
+
geometry: chunk.geometry,
|
|
487
|
+
material: mat,
|
|
488
|
+
receiveShadow,
|
|
489
|
+
castShadow
|
|
490
|
+
}, chunk.key))
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
export { KolmoTerrain, KolmoTerrainField, createRollingBasinHeightFn, exportTerrainKhrGlb };
|
|
494
|
+
|
|
495
|
+
//# sourceMappingURL=index.js.map
|