@pinkaphant/kolmo 0.5.2 → 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.
Files changed (41) hide show
  1. package/README.md +92 -14
  2. package/dist/engine/actor.d.ts +1 -1
  3. package/dist/engine/index.d.ts +3 -0
  4. package/dist/engine/index.js +44 -34
  5. package/dist/engine/index.js.map +1 -1
  6. package/dist/engine/surface.d.ts +5 -6
  7. package/dist/engine/traits.d.ts +27 -0
  8. package/dist/engine/world.d.ts +5 -3
  9. package/dist/index.d.ts +2 -2
  10. package/dist/index.js +9 -5
  11. package/dist/physics/index.d.ts +2 -1
  12. package/dist/physics/index.js +3 -1
  13. package/dist/physics-KYWAXctg.js +233 -0
  14. package/dist/physics-KYWAXctg.js.map +1 -0
  15. package/dist/replay/index.d.ts +84 -0
  16. package/dist/replay/index.js +380 -0
  17. package/dist/replay/index.js.map +1 -0
  18. package/dist/terrain/index.d.ts +119 -0
  19. package/dist/terrain/index.js +495 -0
  20. package/dist/terrain/index.js.map +1 -0
  21. package/dist/toolgun/Toolgun.d.ts +11 -4
  22. package/dist/toolgun/index.d.ts +2 -2
  23. package/dist/toolgun/index.js +1 -1
  24. package/dist/{toolgun-CFCNO00q.js → toolgun-COu_5SxS.js} +202 -542
  25. package/dist/toolgun-COu_5SxS.js.map +1 -0
  26. package/dist/traits-Dp1ey_4w.js +267 -0
  27. package/dist/traits-Dp1ey_4w.js.map +1 -0
  28. package/dist/vite/index.js +1 -0
  29. package/dist/vite/index.js.map +1 -1
  30. package/dist/water/index.d.ts +71 -0
  31. package/dist/water/index.js +442 -0
  32. package/dist/water/index.js.map +1 -0
  33. package/dist/{world-BljtS6Fb.js → world-BDhBihjW.js} +114 -139
  34. package/dist/world-BDhBihjW.js.map +1 -0
  35. package/docs/api-reference.md +201 -0
  36. package/package.json +23 -6
  37. package/dist/physics-CA1abDCY.js +0 -476
  38. package/dist/physics-CA1abDCY.js.map +0 -1
  39. package/dist/toolgun/c12Outlets.d.ts +0 -36
  40. package/dist/toolgun-CFCNO00q.js.map +0 -1
  41. package/dist/world-BljtS6Fb.js.map +0 -1
@@ -0,0 +1,380 @@
1
+ import * as THREE from "three";
2
+ import { createQuery, unpackEntity } from "koota";
3
+ import { s as TransformTrait, u as VelocityTrait } from "../traits-Dp1ey_4w.js";
4
+ // src/replay/index.ts
5
+ var INPUT_MAGIC = 0x4c50524b;
6
+ var TRAJ_MAGIC = 0x5041544b;
7
+ var FORMAT_VERSION = 1;
8
+ /**
9
+ * Deterministic fixed-timestep accumulator (`dtFixed = 1 / tickHz`) preventing Rapier solver drift across monitor refresh rates.
10
+ */
11
+ var FixedStepAccumulator = class {
12
+ dtFixed;
13
+ maxSubSteps;
14
+ accumulator = 0;
15
+ tickCount = 0;
16
+ constructor(tickHz = 60, maxSubSteps = 5) {
17
+ if (!(tickHz > 0) || !Number.isFinite(tickHz)) throw new RangeError(`FixedStepAccumulator: tickHz must be positive finite, got ${tickHz}`);
18
+ this.dtFixed = 1 / tickHz;
19
+ this.maxSubSteps = Math.max(1, Math.trunc(maxSubSteps));
20
+ }
21
+ get tick() {
22
+ return this.tickCount;
23
+ }
24
+ /** Normalized interpolation remainder in `[0, 1)` after stepping. */
25
+ get alpha() {
26
+ return this.accumulator / this.dtFixed;
27
+ }
28
+ reset() {
29
+ this.accumulator = 0;
30
+ this.tickCount = 0;
31
+ }
32
+ advance(frameDeltaSec, onFixedStep) {
33
+ if (!(frameDeltaSec > 0) || !Number.isFinite(frameDeltaSec)) return 0;
34
+ this.accumulator = Math.min(this.accumulator + frameDeltaSec, this.dtFixed * this.maxSubSteps);
35
+ let steps = 0;
36
+ while (this.accumulator >= this.dtFixed - 1e-9 && steps < this.maxSubSteps) {
37
+ this.accumulator = Math.max(0, this.accumulator - this.dtFixed);
38
+ onFixedStep(this.dtFixed, this.tickCount++);
39
+ steps++;
40
+ }
41
+ return steps;
42
+ }
43
+ };
44
+ function packInputMask(inp) {
45
+ return (inp.forward ? 1 : 0) | (inp.backward ? 2 : 0) | (inp.left ? 4 : 0) | (inp.right ? 8 : 0) | (inp.brake ? 16 : 0) | (inp.descend ? 32 : 0) | (inp.boost ? 64 : 0) | (inp.interact ? 128 : 0) | (inp.cameraView ? 256 : 0) | (inp.reset ? 512 : 0) | (inp.modeToggle ? 1024 : 0) | (inp.turnLeft ? 2048 : 0) | (inp.turnRight ? 4096 : 0);
46
+ }
47
+ function unpackInputMask(mask, stickX, stickY, out) {
48
+ out.forward = (mask & 1) !== 0;
49
+ out.backward = (mask & 2) !== 0;
50
+ out.left = (mask & 4) !== 0;
51
+ out.right = (mask & 8) !== 0;
52
+ out.brake = (mask & 16) !== 0;
53
+ out.descend = (mask & 32) !== 0;
54
+ out.boost = (mask & 64) !== 0;
55
+ out.interact = (mask & 128) !== 0;
56
+ out.cameraView = (mask & 256) !== 0;
57
+ out.reset = (mask & 512) !== 0;
58
+ out.modeToggle = (mask & 1024) !== 0;
59
+ out.turnLeft = (mask & 2048) !== 0;
60
+ out.turnRight = (mask & 4096) !== 0;
61
+ out.stickX = stickX;
62
+ out.stickY = stickY;
63
+ return out;
64
+ }
65
+ var quantizeAxis = (v) => Math.round(Math.max(-1, Math.min(1, v)) * 32767);
66
+ var dequantizeAxis = (q) => Math.max(-1, Math.min(1, q / 32767));
67
+ /**
68
+ * Compact 6-byte-per-tick deterministic input tape recorder (`uint16` button bitmask + `int16` stickX + `int16` stickY).
69
+ */
70
+ var InputTapeRecorder = class {
71
+ tickHz;
72
+ masks;
73
+ sticks;
74
+ length = 0;
75
+ constructor(tickHz = 60, initialCapacityTicks = 3600) {
76
+ this.tickHz = tickHz;
77
+ const cap = Math.max(16, initialCapacityTicks);
78
+ this.masks = new Uint16Array(cap);
79
+ this.sticks = new Int16Array(cap * 2);
80
+ }
81
+ get frameCount() {
82
+ return this.length;
83
+ }
84
+ clear() {
85
+ this.length = 0;
86
+ }
87
+ recordTick(inp) {
88
+ if (this.length >= this.masks.length) {
89
+ const nextCap = this.masks.length * 2;
90
+ const nextMasks = new Uint16Array(nextCap);
91
+ nextMasks.set(this.masks);
92
+ this.masks = nextMasks;
93
+ const nextSticks = new Int16Array(nextCap * 2);
94
+ nextSticks.set(this.sticks);
95
+ this.sticks = nextSticks;
96
+ }
97
+ const i = this.length++;
98
+ this.masks[i] = packInputMask(inp);
99
+ this.sticks[i * 2 + 0] = quantizeAxis(inp.stickX);
100
+ this.sticks[i * 2 + 1] = quantizeAxis(inp.stickY);
101
+ }
102
+ encode() {
103
+ const headerBytes = 16;
104
+ const out = new Uint8Array(headerBytes + this.length * 6);
105
+ const dv = new DataView(out.buffer, out.byteOffset, out.byteLength);
106
+ dv.setUint32(0, INPUT_MAGIC, true);
107
+ dv.setUint16(4, FORMAT_VERSION, true);
108
+ dv.setUint16(6, Math.round(this.tickHz), true);
109
+ dv.setUint32(8, this.length, true);
110
+ dv.setUint32(12, 0, true);
111
+ let off = headerBytes;
112
+ for (let i = 0; i < this.length; i++) {
113
+ dv.setUint16(off, this.masks[i], true);
114
+ dv.setInt16(off + 2, this.sticks[i * 2 + 0], true);
115
+ dv.setInt16(off + 4, this.sticks[i * 2 + 1], true);
116
+ off += 6;
117
+ }
118
+ return out;
119
+ }
120
+ };
121
+ var InputTapePlayer = class InputTapePlayer {
122
+ tickHz;
123
+ frameCount;
124
+ masks;
125
+ sticks;
126
+ cursor = 0;
127
+ scratch = {
128
+ forward: false,
129
+ backward: false,
130
+ left: false,
131
+ right: false,
132
+ turnLeft: false,
133
+ turnRight: false,
134
+ brake: false,
135
+ descend: false,
136
+ boost: false,
137
+ interact: false,
138
+ cameraView: false,
139
+ reset: false,
140
+ modeToggle: false,
141
+ stickX: 0,
142
+ stickY: 0
143
+ };
144
+ constructor(tickHz, masks, sticks) {
145
+ this.tickHz = tickHz;
146
+ this.frameCount = masks.length;
147
+ this.masks = masks;
148
+ this.sticks = sticks;
149
+ }
150
+ static fromBytes(bytes) {
151
+ if (bytes.byteLength < 16) throw new RangeError("InputTapePlayer: truncated KRPL header");
152
+ const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
153
+ if (dv.getUint32(0, true) !== INPUT_MAGIC) throw new Error("InputTapePlayer: invalid KRPL magic");
154
+ const ver = dv.getUint16(4, true);
155
+ if (ver !== FORMAT_VERSION) throw new Error(`InputTapePlayer: unsupported version ${ver}`);
156
+ const tickHz = dv.getUint16(6, true);
157
+ const count = dv.getUint32(8, true);
158
+ if (bytes.byteLength < 16 + count * 6) throw new RangeError("InputTapePlayer: truncated KRPL frame payload");
159
+ const masks = new Uint16Array(count);
160
+ const sticks = new Int16Array(count * 2);
161
+ let off = 16;
162
+ for (let i = 0; i < count; i++) {
163
+ masks[i] = dv.getUint16(off, true);
164
+ sticks[i * 2 + 0] = dv.getInt16(off + 2, true);
165
+ sticks[i * 2 + 1] = dv.getInt16(off + 4, true);
166
+ off += 6;
167
+ }
168
+ return new InputTapePlayer(tickHz, masks, sticks);
169
+ }
170
+ get done() {
171
+ return this.cursor >= this.frameCount;
172
+ }
173
+ get currentTick() {
174
+ return this.cursor;
175
+ }
176
+ seekTick(tick) {
177
+ this.cursor = Math.max(0, Math.min(this.frameCount, Math.trunc(tick)));
178
+ }
179
+ /** Compatible directly with `<KolmoActor getInput={player.readTick} />` / `<KolmoVehicle getInput={...} />`. */
180
+ readTick = () => {
181
+ if (this.cursor >= this.frameCount) return unpackInputMask(0, 0, 0, this.scratch);
182
+ const i = this.cursor++;
183
+ return unpackInputMask(this.masks[i], dequantizeAxis(this.sticks[i * 2 + 0]), dequantizeAxis(this.sticks[i * 2 + 1]), this.scratch);
184
+ };
185
+ };
186
+ var kinematicsQuery = createQuery(TransformTrait, VelocityTrait);
187
+ var STRIDE_FLOATS = 11;
188
+ var _q0 = new THREE.Quaternion();
189
+ var _q1 = new THREE.Quaternion();
190
+ /**
191
+ * Zero-allocation Koota SoA trajectory recorder and slerp keyframe scrubber (`KTAP` binary serialization).
192
+ * Reads Koota typed/array stores directly via `query.useStores(([xf, vel], entities) => ...)`.
193
+ */
194
+ var KootaTrajectoryTape = class KootaTrajectoryTape {
195
+ sampleHz;
196
+ tracks = /* @__PURE__ */ new Map();
197
+ capacityFrames;
198
+ frames = 0;
199
+ constructor(sampleHz = 60, initialCapacityFrames = 1800) {
200
+ this.sampleHz = sampleHz;
201
+ this.capacityFrames = Math.max(8, initialCapacityFrames);
202
+ }
203
+ get frameCount() {
204
+ return this.frames;
205
+ }
206
+ get durationSec() {
207
+ return this.frames <= 1 ? 0 : (this.frames - 1) / this.sampleHz;
208
+ }
209
+ get trackedEntityIds() {
210
+ return [...this.tracks.keys()];
211
+ }
212
+ clear() {
213
+ this.tracks.clear();
214
+ this.frames = 0;
215
+ }
216
+ ensureTrack(entity) {
217
+ let buf = this.tracks.get(entity);
218
+ if (!buf) {
219
+ buf = new Float32Array(this.capacityFrames * STRIDE_FLOATS);
220
+ for (let f = 0; f < this.frames; f++) buf[f * STRIDE_FLOATS + 6] = 1;
221
+ this.tracks.set(entity, buf);
222
+ }
223
+ return buf;
224
+ }
225
+ growAll() {
226
+ this.capacityFrames *= 2;
227
+ for (const [ent, old] of this.tracks) {
228
+ const next = new Float32Array(this.capacityFrames * STRIDE_FLOATS);
229
+ next.set(old);
230
+ this.tracks.set(ent, next);
231
+ }
232
+ }
233
+ /** Zero-allocation capture of all Koota entities matching `(TransformTrait, VelocityTrait)`. */
234
+ captureWorld(world) {
235
+ if (this.frames >= this.capacityFrames) this.growAll();
236
+ const base = this.frames++ * STRIDE_FLOATS;
237
+ world.query(kinematicsQuery).useStores(([xf, vel], entities) => {
238
+ for (let i = 0; i < entities.length; i++) {
239
+ const ent = entities[i];
240
+ const { entityId } = unpackEntity(ent);
241
+ const buf = this.ensureTrack(ent);
242
+ buf[base + 0] = xf.x[entityId];
243
+ buf[base + 1] = xf.y[entityId];
244
+ buf[base + 2] = xf.z[entityId];
245
+ buf[base + 3] = xf.qx[entityId];
246
+ buf[base + 4] = xf.qy[entityId];
247
+ buf[base + 5] = xf.qz[entityId];
248
+ buf[base + 6] = xf.qw[entityId];
249
+ buf[base + 7] = vel.vx[entityId];
250
+ buf[base + 8] = vel.vy[entityId];
251
+ buf[base + 9] = vel.vz[entityId];
252
+ buf[base + 10] = vel.speedKmh[entityId];
253
+ }
254
+ });
255
+ }
256
+ sampleEntityAt(entity, timeSec, out = {
257
+ x: 0,
258
+ y: 0,
259
+ z: 0,
260
+ qx: 0,
261
+ qy: 0,
262
+ qz: 0,
263
+ qw: 1,
264
+ vx: 0,
265
+ vy: 0,
266
+ vz: 0,
267
+ speedKmh: 0
268
+ }) {
269
+ const buf = this.tracks.get(entity);
270
+ if (!buf || this.frames === 0) return null;
271
+ const posF = Math.max(0, Math.min(this.frames - 1, timeSec * this.sampleHz));
272
+ const f0 = Math.floor(posF);
273
+ const f1 = Math.min(this.frames - 1, f0 + 1);
274
+ const t = posF - f0;
275
+ const b0 = f0 * STRIDE_FLOATS;
276
+ const b1 = f1 * STRIDE_FLOATS;
277
+ out.x = buf[b0 + 0] + (buf[b1 + 0] - buf[b0 + 0]) * t;
278
+ out.y = buf[b0 + 1] + (buf[b1 + 1] - buf[b0 + 1]) * t;
279
+ out.z = buf[b0 + 2] + (buf[b1 + 2] - buf[b0 + 2]) * t;
280
+ _q0.set(buf[b0 + 3], buf[b0 + 4], buf[b0 + 5], buf[b0 + 6]);
281
+ _q1.set(buf[b1 + 3], buf[b1 + 4], buf[b1 + 5], buf[b1 + 6]);
282
+ _q0.slerp(_q1, t);
283
+ out.qx = _q0.x;
284
+ out.qy = _q0.y;
285
+ out.qz = _q0.z;
286
+ out.qw = _q0.w;
287
+ out.vx = buf[b0 + 7] + (buf[b1 + 7] - buf[b0 + 7]) * t;
288
+ out.vy = buf[b0 + 8] + (buf[b1 + 8] - buf[b0 + 8]) * t;
289
+ out.vz = buf[b0 + 9] + (buf[b1 + 9] - buf[b0 + 9]) * t;
290
+ out.speedKmh = buf[b0 + 10] + (buf[b1 + 10] - buf[b0 + 10]) * t;
291
+ return out;
292
+ }
293
+ /** Scrubs Koota SoA stores directly to `timeSec` with zero allocations. */
294
+ scrubWorld(world, timeSec) {
295
+ if (this.frames === 0) return;
296
+ const scratch = {
297
+ x: 0,
298
+ y: 0,
299
+ z: 0,
300
+ qx: 0,
301
+ qy: 0,
302
+ qz: 0,
303
+ qw: 1,
304
+ vx: 0,
305
+ vy: 0,
306
+ vz: 0,
307
+ speedKmh: 0
308
+ };
309
+ world.query(kinematicsQuery).useStores(([xf, vel], entities) => {
310
+ for (let i = 0; i < entities.length; i++) {
311
+ const ent = entities[i];
312
+ if (!this.sampleEntityAt(ent, timeSec, scratch)) continue;
313
+ const { entityId } = unpackEntity(ent);
314
+ xf.x[entityId] = scratch.x;
315
+ xf.y[entityId] = scratch.y;
316
+ xf.z[entityId] = scratch.z;
317
+ xf.qx[entityId] = scratch.qx;
318
+ xf.qy[entityId] = scratch.qy;
319
+ xf.qz[entityId] = scratch.qz;
320
+ xf.qw[entityId] = scratch.qw;
321
+ vel.vx[entityId] = scratch.vx;
322
+ vel.vy[entityId] = scratch.vy;
323
+ vel.vz[entityId] = scratch.vz;
324
+ vel.speedKmh[entityId] = scratch.speedKmh;
325
+ }
326
+ });
327
+ }
328
+ encode() {
329
+ const trackCount = this.tracks.size;
330
+ const floatsPerTrack = this.frames * STRIDE_FLOATS;
331
+ const byteLen = 16 + trackCount * (4 + floatsPerTrack * 4);
332
+ const out = new Uint8Array(byteLen);
333
+ const dv = new DataView(out.buffer, out.byteOffset, out.byteLength);
334
+ dv.setUint32(0, TRAJ_MAGIC, true);
335
+ dv.setUint16(4, FORMAT_VERSION, true);
336
+ dv.setUint16(6, Math.round(this.sampleHz), true);
337
+ dv.setUint32(8, this.frames, true);
338
+ dv.setUint32(12, trackCount, true);
339
+ let off = 16;
340
+ for (const [entKey, buf] of this.tracks) {
341
+ dv.setUint32(off, entKey >>> 0, true);
342
+ off += 4;
343
+ for (let j = 0; j < floatsPerTrack; j++) {
344
+ dv.setFloat32(off, buf[j], true);
345
+ off += 4;
346
+ }
347
+ }
348
+ return out;
349
+ }
350
+ static fromBytes(bytes) {
351
+ if (bytes.byteLength < 16) throw new RangeError("KootaTrajectoryTape: truncated KTAP header");
352
+ const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
353
+ if (dv.getUint32(0, true) !== TRAJ_MAGIC) throw new Error("KootaTrajectoryTape: invalid KTAP magic");
354
+ const ver = dv.getUint16(4, true);
355
+ if (ver !== FORMAT_VERSION) throw new Error(`KootaTrajectoryTape: unsupported version ${ver}`);
356
+ const sampleHz = dv.getUint16(6, true);
357
+ const frames = dv.getUint32(8, true);
358
+ const trackCount = dv.getUint32(12, true);
359
+ const floatsPerTrack = frames * STRIDE_FLOATS;
360
+ const expected = 16 + trackCount * (4 + floatsPerTrack * 4);
361
+ if (bytes.byteLength < expected) throw new RangeError("KootaTrajectoryTape: truncated KTAP track payload");
362
+ const tape = new KootaTrajectoryTape(sampleHz, Math.max(8, frames));
363
+ tape.frames = frames;
364
+ let off = 16;
365
+ for (let t = 0; t < trackCount; t++) {
366
+ const entKey = dv.getUint32(off, true);
367
+ off += 4;
368
+ const buf = new Float32Array(Math.max(8, frames) * STRIDE_FLOATS);
369
+ for (let j = 0; j < floatsPerTrack; j++) {
370
+ buf[j] = dv.getFloat32(off, true);
371
+ off += 4;
372
+ }
373
+ tape.tracks.set(entKey, buf);
374
+ }
375
+ return tape;
376
+ }
377
+ };
378
+ export { FixedStepAccumulator, InputTapePlayer, InputTapeRecorder, KootaTrajectoryTape };
379
+
380
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/replay/index.ts"],"sourcesContent":["import * as THREE from \"three\";\nimport { createQuery, unpackEntity, type Entity, type World } from \"koota\";\nimport type { KolmoInputState } from \"../engine/input.js\";\nimport { TransformTrait, VelocityTrait } from \"../engine/traits.js\";\n\nexport type KolmoInputSnapshot = KolmoInputState;\n\nconst INPUT_MAGIC = 0x4c50524b; // \"KRPL\" little-endian\nconst TRAJ_MAGIC = 0x5041544b; // \"KTAP\" little-endian\nconst FORMAT_VERSION = 1;\n\n/**\n * Deterministic fixed-timestep accumulator (`dtFixed = 1 / tickHz`) preventing Rapier solver drift across monitor refresh rates.\n */\nexport class FixedStepAccumulator {\n readonly dtFixed: number;\n readonly maxSubSteps: number;\n private accumulator = 0;\n private tickCount = 0;\n\n constructor(tickHz = 60, maxSubSteps = 5) {\n if (!(tickHz > 0) || !Number.isFinite(tickHz)) {\n throw new RangeError(`FixedStepAccumulator: tickHz must be positive finite, got ${tickHz}`);\n }\n this.dtFixed = 1 / tickHz;\n this.maxSubSteps = Math.max(1, Math.trunc(maxSubSteps));\n }\n\n get tick(): number {\n return this.tickCount;\n }\n\n /** Normalized interpolation remainder in `[0, 1)` after stepping. */\n get alpha(): number {\n return this.accumulator / this.dtFixed;\n }\n\n reset(): void {\n this.accumulator = 0;\n this.tickCount = 0;\n }\n\n advance(frameDeltaSec: number, onFixedStep: (dtFixed: number, tick: number) => void): number {\n if (!(frameDeltaSec > 0) || !Number.isFinite(frameDeltaSec))\n return 0;\n this.accumulator = Math.min(this.accumulator + frameDeltaSec, this.dtFixed * this.maxSubSteps);\n let steps = 0;\n while (this.accumulator >= this.dtFixed - 1e-9 && steps < this.maxSubSteps) {\n this.accumulator = Math.max(0, this.accumulator - this.dtFixed);\n onFixedStep(this.dtFixed, this.tickCount++);\n steps++;\n }\n return steps;\n }\n}\n\nfunction packInputMask(inp: Readonly<KolmoInputSnapshot>): number {\n return (\n (inp.forward ? 1 : 0) |\n (inp.backward ? 2 : 0) |\n (inp.left ? 4 : 0) |\n (inp.right ? 8 : 0) |\n (inp.brake ? 16 : 0) |\n (inp.descend ? 32 : 0) |\n (inp.boost ? 64 : 0) |\n (inp.interact ? 128 : 0) |\n (inp.cameraView ? 256 : 0) |\n (inp.reset ? 512 : 0) |\n (inp.modeToggle ? 1024 : 0) |\n (inp.turnLeft ? 2048 : 0) |\n (inp.turnRight ? 4096 : 0)\n );\n}\n\nfunction unpackInputMask(mask: number, stickX: number, stickY: number, out: KolmoInputSnapshot): KolmoInputSnapshot {\n out.forward = (mask & 1) !== 0;\n out.backward = (mask & 2) !== 0;\n out.left = (mask & 4) !== 0;\n out.right = (mask & 8) !== 0;\n out.brake = (mask & 16) !== 0;\n out.descend = (mask & 32) !== 0;\n out.boost = (mask & 64) !== 0;\n out.interact = (mask & 128) !== 0;\n out.cameraView = (mask & 256) !== 0;\n out.reset = (mask & 512) !== 0;\n out.modeToggle = (mask & 1024) !== 0;\n out.turnLeft = (mask & 2048) !== 0;\n out.turnRight = (mask & 4096) !== 0;\n out.stickX = stickX;\n out.stickY = stickY;\n return out;\n}\n\nconst quantizeAxis = (v: number): number => Math.round(Math.max(-1, Math.min(1, v)) * 32767);\nconst dequantizeAxis = (q: number): number => Math.max(-1, Math.min(1, q / 32767));\n\n/**\n * Compact 6-byte-per-tick deterministic input tape recorder (`uint16` button bitmask + `int16` stickX + `int16` stickY).\n */\nexport class InputTapeRecorder {\n readonly tickHz: number;\n private masks: Uint16Array;\n private sticks: Int16Array;\n private length = 0;\n\n constructor(tickHz = 60, initialCapacityTicks = 3600) {\n this.tickHz = tickHz;\n const cap = Math.max(16, initialCapacityTicks);\n this.masks = new Uint16Array(cap);\n this.sticks = new Int16Array(cap * 2);\n }\n\n get frameCount(): number {\n return this.length;\n }\n\n clear(): void {\n this.length = 0;\n }\n\n recordTick(inp: Readonly<KolmoInputSnapshot>): void {\n if (this.length >= this.masks.length) {\n const nextCap = this.masks.length * 2;\n const nextMasks = new Uint16Array(nextCap);\n nextMasks.set(this.masks);\n this.masks = nextMasks;\n\n const nextSticks = new Int16Array(nextCap * 2);\n nextSticks.set(this.sticks);\n this.sticks = nextSticks;\n }\n const i = this.length++;\n this.masks[i] = packInputMask(inp);\n this.sticks[i * 2 + 0] = quantizeAxis(inp.stickX);\n this.sticks[i * 2 + 1] = quantizeAxis(inp.stickY);\n }\n\n encode(): Uint8Array {\n const headerBytes = 16;\n const out = new Uint8Array(headerBytes + this.length * 6);\n const dv = new DataView(out.buffer, out.byteOffset, out.byteLength);\n dv.setUint32(0, INPUT_MAGIC, true);\n dv.setUint16(4, FORMAT_VERSION, true);\n dv.setUint16(6, Math.round(this.tickHz), true);\n dv.setUint32(8, this.length, true);\n dv.setUint32(12, 0, true);\n\n let off = headerBytes;\n for (let i = 0; i < this.length; i++) {\n dv.setUint16(off, this.masks[i], true);\n dv.setInt16(off + 2, this.sticks[i * 2 + 0], true);\n dv.setInt16(off + 4, this.sticks[i * 2 + 1], true);\n off += 6;\n }\n return out;\n }\n}\n\nexport class InputTapePlayer {\n readonly tickHz: number;\n readonly frameCount: number;\n private readonly masks: Uint16Array;\n private readonly sticks: Int16Array;\n private cursor = 0;\n private readonly scratch: KolmoInputSnapshot = {\n forward: false, backward: false, left: false, right: false,\n turnLeft: false, turnRight: false, brake: false, descend: false,\n boost: false, interact: false, cameraView: false, reset: false,\n modeToggle: false, stickX: 0, stickY: 0,\n };\n\n private constructor(tickHz: number, masks: Uint16Array, sticks: Int16Array) {\n this.tickHz = tickHz;\n this.frameCount = masks.length;\n this.masks = masks;\n this.sticks = sticks;\n }\n\n static fromBytes(bytes: Uint8Array): InputTapePlayer {\n if (bytes.byteLength < 16) {\n throw new RangeError(\"InputTapePlayer: truncated KRPL header\");\n }\n const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n if (dv.getUint32(0, true) !== INPUT_MAGIC) {\n throw new Error(\"InputTapePlayer: invalid KRPL magic\");\n }\n const ver = dv.getUint16(4, true);\n if (ver !== FORMAT_VERSION) {\n throw new Error(`InputTapePlayer: unsupported version ${ver}`);\n }\n const tickHz = dv.getUint16(6, true);\n const count = dv.getUint32(8, true);\n if (bytes.byteLength < 16 + count * 6) {\n throw new RangeError(\"InputTapePlayer: truncated KRPL frame payload\");\n }\n const masks = new Uint16Array(count);\n const sticks = new Int16Array(count * 2);\n let off = 16;\n for (let i = 0; i < count; i++) {\n masks[i] = dv.getUint16(off, true);\n sticks[i * 2 + 0] = dv.getInt16(off + 2, true);\n sticks[i * 2 + 1] = dv.getInt16(off + 4, true);\n off += 6;\n }\n return new InputTapePlayer(tickHz, masks, sticks);\n }\n\n get done(): boolean {\n return this.cursor >= this.frameCount;\n }\n\n get currentTick(): number {\n return this.cursor;\n }\n\n seekTick(tick: number): void {\n this.cursor = Math.max(0, Math.min(this.frameCount, Math.trunc(tick)));\n }\n\n /** Compatible directly with `<KolmoActor getInput={player.readTick} />` / `<KolmoVehicle getInput={...} />`. */\n readonly readTick = (): KolmoInputSnapshot => {\n if (this.cursor >= this.frameCount) {\n return unpackInputMask(0, 0, 0, this.scratch);\n }\n const i = this.cursor++;\n return unpackInputMask(\n this.masks[i],\n dequantizeAxis(this.sticks[i * 2 + 0]),\n dequantizeAxis(this.sticks[i * 2 + 1]),\n this.scratch,\n );\n };\n}\n\nconst kinematicsQuery = createQuery(TransformTrait, VelocityTrait);\nconst STRIDE_FLOATS = 11; // x, y, z, qx, qy, qz, qw, vx, vy, vz, speedKmh\nconst _q0 = new THREE.Quaternion();\nconst _q1 = new THREE.Quaternion();\n\nexport interface TrajectoryPoseSample {\n x: number;\n y: number;\n z: number;\n qx: number;\n qy: number;\n qz: number;\n qw: number;\n vx: number;\n vy: number;\n vz: number;\n speedKmh: number;\n}\n\n/**\n * Zero-allocation Koota SoA trajectory recorder and slerp keyframe scrubber (`KTAP` binary serialization).\n * Reads Koota typed/array stores directly via `query.useStores(([xf, vel], entities) => ...)`.\n */\nexport class KootaTrajectoryTape {\n readonly sampleHz: number;\n private readonly tracks = new Map<number, Float32Array>();\n private capacityFrames: number;\n private frames = 0;\n\n constructor(sampleHz = 60, initialCapacityFrames = 1800) {\n this.sampleHz = sampleHz;\n this.capacityFrames = Math.max(8, initialCapacityFrames);\n }\n\n get frameCount(): number {\n return this.frames;\n }\n\n get durationSec(): number {\n return this.frames <= 1 ? 0 : (this.frames - 1) / this.sampleHz;\n }\n\n get trackedEntityIds(): number[] {\n return [...this.tracks.keys()];\n }\n\n clear(): void {\n this.tracks.clear();\n this.frames = 0;\n }\n\n private ensureTrack(entity: number): Float32Array {\n let buf = this.tracks.get(entity);\n if (!buf) {\n buf = new Float32Array(this.capacityFrames * STRIDE_FLOATS);\n // Fill skipped earlier frames with identity quaternion qw = 1\n for (let f = 0; f < this.frames; f++) {\n buf[f * STRIDE_FLOATS + 6] = 1;\n }\n this.tracks.set(entity, buf);\n }\n return buf;\n }\n\n private growAll(): void {\n this.capacityFrames *= 2;\n for (const [ent, old] of this.tracks) {\n const next = new Float32Array(this.capacityFrames * STRIDE_FLOATS);\n next.set(old);\n this.tracks.set(ent, next);\n }\n }\n\n /** Zero-allocation capture of all Koota entities matching `(TransformTrait, VelocityTrait)`. */\n captureWorld(world: World): void {\n if (this.frames >= this.capacityFrames) {\n this.growAll();\n }\n const f = this.frames++;\n const base = f * STRIDE_FLOATS;\n\n world.query(kinematicsQuery).useStores(([xf, vel], entities) => {\n for (let i = 0; i < entities.length; i++) {\n const ent = entities[i] as Entity;\n const { entityId } = unpackEntity(ent);\n const buf = this.ensureTrack(ent as unknown as number);\n buf[base + 0] = xf.x[entityId];\n buf[base + 1] = xf.y[entityId];\n buf[base + 2] = xf.z[entityId];\n buf[base + 3] = xf.qx[entityId];\n buf[base + 4] = xf.qy[entityId];\n buf[base + 5] = xf.qz[entityId];\n buf[base + 6] = xf.qw[entityId];\n buf[base + 7] = vel.vx[entityId];\n buf[base + 8] = vel.vy[entityId];\n buf[base + 9] = vel.vz[entityId];\n buf[base + 10] = vel.speedKmh[entityId];\n }\n });\n }\n\n sampleEntityAt(\n entity: Entity | number,\n timeSec: number,\n out: TrajectoryPoseSample = { x: 0, y: 0, z: 0, qx: 0, qy: 0, qz: 0, qw: 1, vx: 0, vy: 0, vz: 0, speedKmh: 0 },\n ): TrajectoryPoseSample | null {\n const buf = this.tracks.get(entity as number);\n if (!buf || this.frames === 0)\n return null;\n\n const posF = Math.max(0, Math.min(this.frames - 1, timeSec * this.sampleHz));\n const f0 = Math.floor(posF);\n const f1 = Math.min(this.frames - 1, f0 + 1);\n const t = posF - f0;\n const b0 = f0 * STRIDE_FLOATS;\n const b1 = f1 * STRIDE_FLOATS;\n\n out.x = buf[b0 + 0] + (buf[b1 + 0] - buf[b0 + 0]) * t;\n out.y = buf[b0 + 1] + (buf[b1 + 1] - buf[b0 + 1]) * t;\n out.z = buf[b0 + 2] + (buf[b1 + 2] - buf[b0 + 2]) * t;\n\n _q0.set(buf[b0 + 3], buf[b0 + 4], buf[b0 + 5], buf[b0 + 6]);\n _q1.set(buf[b1 + 3], buf[b1 + 4], buf[b1 + 5], buf[b1 + 6]);\n _q0.slerp(_q1, t);\n out.qx = _q0.x;\n out.qy = _q0.y;\n out.qz = _q0.z;\n out.qw = _q0.w;\n\n out.vx = buf[b0 + 7] + (buf[b1 + 7] - buf[b0 + 7]) * t;\n out.vy = buf[b0 + 8] + (buf[b1 + 8] - buf[b0 + 8]) * t;\n out.vz = buf[b0 + 9] + (buf[b1 + 9] - buf[b0 + 9]) * t;\n out.speedKmh = buf[b0 + 10] + (buf[b1 + 10] - buf[b0 + 10]) * t;\n return out;\n }\n\n /** Scrubs Koota SoA stores directly to `timeSec` with zero allocations. */\n scrubWorld(world: World, timeSec: number): void {\n if (this.frames === 0)\n return;\n const scratch: TrajectoryPoseSample = { x: 0, y: 0, z: 0, qx: 0, qy: 0, qz: 0, qw: 1, vx: 0, vy: 0, vz: 0, speedKmh: 0 };\n world.query(kinematicsQuery).useStores(([xf, vel], entities) => {\n for (let i = 0; i < entities.length; i++) {\n const ent = entities[i] as Entity;\n if (!this.sampleEntityAt(ent, timeSec, scratch))\n continue;\n const { entityId } = unpackEntity(ent);\n xf.x[entityId] = scratch.x;\n xf.y[entityId] = scratch.y;\n xf.z[entityId] = scratch.z;\n xf.qx[entityId] = scratch.qx;\n xf.qy[entityId] = scratch.qy;\n xf.qz[entityId] = scratch.qz;\n xf.qw[entityId] = scratch.qw;\n vel.vx[entityId] = scratch.vx;\n vel.vy[entityId] = scratch.vy;\n vel.vz[entityId] = scratch.vz;\n vel.speedKmh[entityId] = scratch.speedKmh;\n }\n });\n }\n\n encode(): Uint8Array {\n const trackCount = this.tracks.size;\n const floatsPerTrack = this.frames * STRIDE_FLOATS;\n const byteLen = 16 + trackCount * (4 + floatsPerTrack * 4);\n const out = new Uint8Array(byteLen);\n const dv = new DataView(out.buffer, out.byteOffset, out.byteLength);\n dv.setUint32(0, TRAJ_MAGIC, true);\n dv.setUint16(4, FORMAT_VERSION, true);\n dv.setUint16(6, Math.round(this.sampleHz), true);\n dv.setUint32(8, this.frames, true);\n dv.setUint32(12, trackCount, true);\n\n let off = 16;\n for (const [entKey, buf] of this.tracks) {\n dv.setUint32(off, entKey >>> 0, true);\n off += 4;\n for (let j = 0; j < floatsPerTrack; j++) {\n dv.setFloat32(off, buf[j], true);\n off += 4;\n }\n }\n return out;\n }\n\n static fromBytes(bytes: Uint8Array): KootaTrajectoryTape {\n if (bytes.byteLength < 16) {\n throw new RangeError(\"KootaTrajectoryTape: truncated KTAP header\");\n }\n const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n if (dv.getUint32(0, true) !== TRAJ_MAGIC) {\n throw new Error(\"KootaTrajectoryTape: invalid KTAP magic\");\n }\n const ver = dv.getUint16(4, true);\n if (ver !== FORMAT_VERSION) {\n throw new Error(`KootaTrajectoryTape: unsupported version ${ver}`);\n }\n const sampleHz = dv.getUint16(6, true);\n const frames = dv.getUint32(8, true);\n const trackCount = dv.getUint32(12, true);\n const floatsPerTrack = frames * STRIDE_FLOATS;\n const expected = 16 + trackCount * (4 + floatsPerTrack * 4);\n if (bytes.byteLength < expected) {\n throw new RangeError(\"KootaTrajectoryTape: truncated KTAP track payload\");\n }\n\n const tape = new KootaTrajectoryTape(sampleHz, Math.max(8, frames));\n tape.frames = frames;\n let off = 16;\n for (let t = 0; t < trackCount; t++) {\n const entKey = dv.getUint32(off, true);\n off += 4;\n const buf = new Float32Array(Math.max(8, frames) * STRIDE_FLOATS);\n for (let j = 0; j < floatsPerTrack; j++) {\n buf[j] = dv.getFloat32(off, true);\n off += 4;\n }\n tape.tracks.set(entKey, buf);\n }\n return tape;\n }\n}\n"],"mappings":";;;;AAOA,IAAM,cAAc;AACpB,IAAM,aAAa;AACnB,IAAM,iBAAiB;;;;AAKvB,IAAa,uBAAb,MAAkC;CACrB;CACA;CACD,cAAc;CACd,YAAY;CAEpB,YAAY,SAAS,IAAI,cAAc,GAAG;EACtC,IAAI,EAAE,SAAS,MAAM,CAAC,OAAO,SAAS,MAAM,GACxC,MAAM,IAAI,WAAW,6DAA6D,QAAQ;EAE9F,KAAK,UAAU,IAAI;EACnB,KAAK,cAAc,KAAK,IAAI,GAAG,KAAK,MAAM,WAAW,CAAC;CAC1D;CAEA,IAAI,OAAe;EACf,OAAO,KAAK;CAChB;;CAGA,IAAI,QAAgB;EAChB,OAAO,KAAK,cAAc,KAAK;CACnC;CAEA,QAAc;EACV,KAAK,cAAc;EACnB,KAAK,YAAY;CACrB;CAEA,QAAQ,eAAuB,aAA8D;EACzF,IAAI,EAAE,gBAAgB,MAAM,CAAC,OAAO,SAAS,aAAa,GACtD,OAAO;EACX,KAAK,cAAc,KAAK,IAAI,KAAK,cAAc,eAAe,KAAK,UAAU,KAAK,WAAW;EAC7F,IAAI,QAAQ;EACZ,OAAO,KAAK,eAAe,KAAK,UAAU,QAAQ,QAAQ,KAAK,aAAa;GACxE,KAAK,cAAc,KAAK,IAAI,GAAG,KAAK,cAAc,KAAK,OAAO;GAC9D,YAAY,KAAK,SAAS,KAAK,WAAW;GAC1C;EACJ;EACA,OAAO;CACX;AACJ;AAEA,SAAS,cAAc,KAA2C;CAC9D,QACK,IAAI,UAAU,IAAI,MAClB,IAAI,WAAW,IAAI,MACnB,IAAI,OAAO,IAAI,MACf,IAAI,QAAQ,IAAI,MAChB,IAAI,QAAQ,KAAK,MACjB,IAAI,UAAU,KAAK,MACnB,IAAI,QAA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GAAG,IAAI,gBAAgB,KAAK;IACrC,GAAG,WAAW,KAAK,IAAI,IAAI,IAAI;IAC/B,OAAO;GACX;EACJ;EACA,OAAO;CACX;CAEA,OAAO,UAAU,OAAwC;EACrD,IAAI,MAAM,aAAa,IACnB,MAAM,IAAI,WAAW,4CAA4C;EAErE,MAAM,KAAK,IAAI,SAAS,MAAM,QAAQ,MAAM,YAAY,MAAM,UAAU;EACxE,IAAI,GAAG,UAAU,GAAG,IAAI,MAAM,YAC1B,MAAM,IAAI,MAAM,yCAAyC;EAE7D,MAAM,MAAM,GAAG,UAAU,GAAG,IAAI;EAChC,IAAI,QAAQ,gBACR,MAAM,IAAI,MAAM,4CAA4C,KAAK;EAErE,MAAM,WAAW,GAAG,UAAU,GAAG,IAAI;EACrC,MAAM,SAAS,GAAG,UAAU,GAAG,IAAI;EACnC,MAAM,aAAa,GAAG,UAAU,IAAI,IAAI;EACxC,MAAM,iBAAiB,SAAS;EAChC,MAAM,WAAW,KAAK,cAAc,IAAI,iBAAiB;EACzD,IAAI,MAAM,aAAa,UACnB,MAAM,IAAI,WAAW,mDAAmD;EAG5E,MAAM,OAAO,IAAI,oBAAoB,UAAU,KAAK,IAAI,GAAG,MAAM,CAAC;EAClE,KAAK,SAAS;EACd,IAAI,MAAM;EACV,KAAK,IAAI,IAAI,GAAG,IAAI,YAAY,KAAK;GACjC,MAAM,SAAS,GAAG,UAAU,KAAK,IAAI;GACrC,OAAO;GACP,MAAM,MAAM,IAAI,aAAa,KAAK,IAAI,GAAG,MAAM,IAAI,aAAa;GAChE,KAAK,IAAI,IAAI,GAAG,IAAI,gBAAgB,KAAK;IACrC,IAAI,KAAK,GAAG,WAAW,KAAK,IAAI;IAChC,OAAO;GACX;GACA,KAAK,OAAO,IAAI,QAAQ,GAAG;EAC/B;EACA,OAAO;CACX;AACJ"}
@@ -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;