// 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; }