Files
tidewall/src/client/terrain/terrain.gdshader
T
claudeandClaude Opus 5.5 8b74485168 W3: Chunked terrain mesh, collision and LOD
TerrainView draws the island as 16 x 16 chunks of 64 m, each at one of
four detail levels (2, 4, 8 and 16 m spacing) chosen by distance to the
camera. TerrainChunkBuilder builds chunk geometry on WorkerThreadPool
threads; the main thread only uploads finished meshes, within a 2 ms
per-frame budget, nearest chunks first.

- Seams: every chunk hangs skirts deeper than twice the most any level
  strays from the full-resolution edge, so neighbours at any two levels
  meet without a crack. Normals come from the full-resolution heightfield
  at every level, and back faces light like the ground above them.
- Splat material: a procedural shader blends sand, marsh, grass and rock
  with weights from the island's bands, averaged over 4 m so borders
  blend. Steep ground turns to rock and summits weather pale.
- Collision stays SimGround's heightfield, with no renderer involved.
- Capture scenarios terrain, terrain_lod (tinted by level) and
  terrain_cracks (magenta sky). tools/crack_check.gd counts sky pixels
  seen through the ground; make terrain-capture requires cracks in a
  skirtless control and none in the real terrain.

Tests: chunk vertices sit on the heightfield; skirts cover every pairing
of levels on sampled shared edges; detail falls with distance; flying
across the island at 30 m/s for 2,037 frames streams 661 chunk rebuilds
with the worst main-thread frame at 0.8 to 1.4 ms (headless, CPU only);
24 players over 900 ticks on two generated islands never sink into the
ground, and a player sprints up towards a summit unharmed.
make terrain-capture: control 437 crack pixels, terrain 0.

Closes #10

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 14:28:30 -05:00

82 lines
3.2 KiB
Plaintext

// Tidewall terrain: four procedural layers (sand, marsh, grass, rock) blended by the
// splat weights in the vertex colour, with rock taking over on steep ground and weathered
// pale stone on the summits. No textures: every pattern is generated here.
shader_type spatial;
render_mode cull_disabled;
uniform float summit_height = 50.0;
uniform float max_slope_cos = 0.69;
uniform vec3 sand_colour : source_color = vec3(0.80, 0.71, 0.52);
uniform vec3 marsh_colour : source_color = vec3(0.37, 0.39, 0.25);
uniform vec3 grass_colour : source_color = vec3(0.31, 0.42, 0.22);
uniform vec3 rock_colour : source_color = vec3(0.47, 0.44, 0.39);
uniform vec3 summit_colour : source_color = vec3(0.74, 0.72, 0.67);
// Debug: tint each chunk by its level of detail (set per instance by TerrainView).
instance uniform float lod_tint = -1.0;
varying vec3 world_position;
varying vec3 world_normal;
float hash(vec2 p) {
p = fract(p * vec2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float value_noise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), u.x),
mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), u.x), u.y);
}
float fbm(vec2 p) {
float total = 0.0;
float amplitude = 0.5;
for (int octave = 0; octave < 4; octave++) {
total += amplitude * value_noise(p);
p *= 2.03;
amplitude *= 0.5;
}
return total;
}
void vertex() {
world_position = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
world_normal = normalize((MODEL_MATRIX * vec4(NORMAL, 0.0)).xyz);
}
void fragment() {
vec2 p = world_position.xz;
float broad = fbm(p * 0.035);
float fine = fbm(p * 0.6);
float ripple = 0.5 + 0.5 * sin(dot(p, vec2(0.55, 0.32)) + broad * 9.0);
vec3 sand = sand_colour * (0.9 + 0.12 * fine) * (0.97 + 0.04 * ripple);
vec3 marsh = mix(marsh_colour * 0.75, marsh_colour * 1.1, smoothstep(0.35, 0.65, broad + 0.2 * fine));
vec3 grass = grass_colour * (0.78 + 0.35 * broad) * (0.9 + 0.15 * fine);
grass = mix(grass, grass * vec3(1.15, 1.05, 0.7), smoothstep(0.55, 0.75, broad));
float strata = 0.5 + 0.5 * sin(world_position.y * 2.1 + broad * 5.0);
vec3 rock = rock_colour * (0.82 + 0.22 * fine + 0.1 * strata);
vec3 summit = summit_colour * (0.88 + 0.14 * fine);
rock = mix(rock, summit, smoothstep(summit_height - 4.0, summit_height + 4.0, world_position.y + 3.0 * broad));
vec4 weights = max(COLOR, vec4(0.0));
float steep = 1.0 - smoothstep(max_slope_cos - 0.08, max_slope_cos + 0.12, world_normal.y);
weights = mix(weights, vec4(0.0, 0.0, 0.0, 1.0), steep);
weights /= max(weights.r + weights.g + weights.b + weights.a, 0.0001);
vec3 albedo = sand * weights.r + marsh * weights.g + grass * weights.b + rock * weights.a;
if (lod_tint >= 0.0) {
vec3 tints[4] = vec3[](vec3(1.0, 0.5, 0.5), vec3(0.5, 1.0, 0.5), vec3(0.5, 0.6, 1.0), vec3(1.0, 1.0, 0.4));
albedo *= tints[int(lod_tint) % 4];
}
ALBEDO = albedo;
// Skirts and any face seen from below must light like the ground above them, so
// use the terrain normal for both faces instead of the flipped back-face normal.
NORMAL = normalize((VIEW_MATRIX * vec4(world_normal, 0.0)).xyz);
ROUGHNESS = mix(0.95, 0.6, weights.g);
SPECULAR = 0.2;
}