Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(terrain): a ridge's crest is rounded, so ridged ground worn by its noise is continuous

yhhe42a3af63713
diff --git a/sdk/src/terrain/source.ts b/sdk/src/terrain/source.ts
index 8d01eb7da..d96c8c03f 100644
--- a/sdk/src/terrain/source.ts
+++ b/sdk/src/terrain/source.ts
@@ -146,11 +146,13 @@ export class NoiseSource implements TerrainSource {
const [n, nx, nz] = noised(u * freq, v * freq, p.seed + o * 101);
let value = n, ddx = nx * freq, ddz = nz * freq;
if (p.ridged > 0) {
- // Folded about its middle, a sharp crest where it turns.
- const s = n * 2 - 1, sign = s >= 0 ? 1 : -1;
- value = n * (1 - p.ridged) + (1 - Math.abs(s)) * p.ridged;
- ddx = ddx * (1 - p.ridged) - 2 * sign * ddx * p.ridged;
- ddz = ddz * (1 - p.ridged) - 2 * sign * ddz * p.ridged;
+ // Folded about its middle into a crest, rounded over its last few hundredths: a slope that turned there at once would
+ // jump the wear the octaves after it take from it, and with it the ground.
+ const s = n * 2 - 1, root = Math.sqrt(s * s + RIDGE_ROUND * RIDGE_ROUND);
+ const fold = (root - RIDGE_ROUND) / RIDGE_SPAN, turn = (s / root / RIDGE_SPAN) * 2;
+ value = n * (1 - p.ridged) + (1 - fold) * p.ridged;
+ ddx = ddx * (1 - p.ridged) - turn * ddx * p.ridged;
+ ddz = ddz * (1 - p.ridged) - turn * ddz * p.ridged;
}
gx += ddx * amp; gz += ddz * amp;
const damp = wear > 0 ? 1 / (1 + wear * (gx * gx + gz * gz) * 0.25) : 1;
@@ -164,6 +166,10 @@ export class NoiseSource implements TerrainSource {
}
}
+/** How round a ridge's crest is, in the noise's own 0 to 1 (folded, -1 to 1), and what the rounded fold reaches at the ends. */
+const RIDGE_ROUND = 0.06;
+const RIDGE_SPAN = Math.sqrt(1 + RIDGE_ROUND * RIDGE_ROUND) - RIDGE_ROUND;
+
/** A 32-bit integer hash, stable across hosts, so a seed makes the same ground everywhere. */
function hash2(x: number, y: number, seed: number): number {
let h = (Math.imul(x, 374761393) + Math.imul(y, 668265263) + Math.imul(seed | 0, 2147483647)) | 0;
diff --git a/sdk/tests/terrain-surface.test.ts b/sdk/tests/terrain-surface.test.ts
index 95da6b113..3a8f9c596 100644
--- a/sdk/tests/terrain-surface.test.ts
+++ b/sdk/tests/terrain-surface.test.ts
@@ -42,6 +42,19 @@ function onPiece(piece: CollisionPiece, x: number, z: number): number | null {
return null;
}
+describe('ground without end', () => {
+ it('is continuous where its ridges are worn: no step, however finely it is read', () => {
+ const worn = new NoiseSource({ ...params, ridged: 1, erosion: 2, warp: 0.35 });
+ const steepest = (step: number) => {
+ let w = 0;
+ for (let x = -3000; x < 3000; x += step) w = Math.max(w, Math.abs(worn.height(x + step, 300) - worn.height(x, 300)) / step);
+ return w;
+ };
+ // A step would show as a slope that grows as the reading gets finer.
+ expect(steepest(0.02)).toBeLessThan(steepest(0.5) * 1.05);
+ });
+});
+
describe('the surface', () => {
const surface = new TerrainSurface(new NoiseSource(params), 2);