Files
tidewall/src/client/terrain/terrain.gdshader
T
claudeandClaude Opus 5.5 923876ed1f A5: World art for points of interest, build pieces, vessels and terrain
- PoiMesher dresses every point of interest from its parts: lapped tarred
  weatherboards, plank boards, stone in a running bond with lichen, tiled
  roofs with a ridge cap, salt-white trim on paint, crusted salt pans, and a
  tarred frame round the lighthouse's glowing glass. One mesh per POI.
- Huts get a shuttered window in each side wall (non-solid parts, no rng).
- Build pieces show their state: building in quarter steps, worn (a third of
  health gone) and battered (two thirds gone, strips knocked out).
  StructureView rebuilds a piece's mesh only when its state changes.
- VesselModels builds the barge (lofted hull, curved rails, mooring lines,
  gored envelope) and the sea chests in code; the primitive stand-ins are gone.
- Terrain: tussocks, wind-dried patches, shingle on sand, lichen on rock.
- test_world_art (9) fails on any primitive stand-in mesh or placeholder
  resource under src/ or assets/, and covers POI bounds and determinism,
  windows, the lamp surface, wear and build stages, view rebuilds and the
  vessel models.
- make poi-captures and make build-captures (state card, vessels).

Closes #45

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 20:39:24 -05:00

95 lines
4.0 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.60, 0.58, 0.53);
// 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 clumps = fbm(p * 0.16 + vec2(17.0, 3.0));
float grain = value_noise(p * 3.1);
// Detail fades with distance so it doesn't shimmer far off.
float near = 1.0 - smoothstep(40.0, 160.0, length(world_position - CAMERA_POSITION_WORLD));
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);
// Shingle: dark pebbles scattered on the sand.
sand = mix(sand, rock_colour * 0.75, near * 0.6 * step(0.86, grain) * step(0.45, clumps));
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));
// Tussocks: darker clumps of rough grass, and paler wind-dried patches between them.
grass *= mix(1.0, 0.8, smoothstep(0.5, 0.68, clumps));
grass = mix(grass, grass * vec3(1.12, 1.08, 0.82), 0.6 * smoothstep(0.3, 0.18, clumps));
grass *= 1.0 - near * 0.12 * smoothstep(0.55, 0.9, grain);
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);
// Lichen crusts in blotches.
rock = mix(rock, vec3(0.62, 0.61, 0.45) * (0.9 + 0.2 * fine), 0.45 * smoothstep(0.6, 0.72, clumps));
vec3 summit = summit_colour * (0.88 + 0.14 * fine);
rock = mix(rock, summit, 0.8 * smoothstep(summit_height - 2.0, summit_height + 6.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;
}