cntrl/assets/shaders/concrete.fs

110 lines
4.3 KiB
GLSL

#version 330
// One tileable concrete material for floor, walls, pillars.
// The mesh carries one planar UV per axis-aligned face, so each map costs a
// single texture fetch instead of triplanar's three. Slots follow raylib's
// material map binding: 0 albedo, 1 roughness, 2 normal.
in vec3 vWorld;
in vec3 vNormal;
in vec4 vColor;
in vec2 vUV;
out vec4 finalColor;
uniform sampler2D texture0; // albedo
uniform sampler2D texture1; // roughness
uniform sampler2D texture2; // tangent normal
uniform vec4 colDiffuse;
uniform vec3 uCameraPos;
uniform float uScale = 0.42;
uniform float uCorruption = 0.0;
uniform mat4 uLightVP;
uniform sampler2D uShadowMap;
uniform float uShadowDarkness = 0.0;
float shadow_factor(vec3 n, vec3 light_dir)
{
if (uShadowDarkness <= 0.0) return 1.0;
vec4 q = uLightVP * vec4(vWorld, 1.0);
vec3 p = q.xyz / q.w * 0.5 + 0.5;
if (p.x < 0.0 || p.x > 1.0 || p.y < 0.0 || p.y > 1.0 || p.z >= 1.0)
return 1.0;
float bias = max(0.00034 * (1.0 - dot(n, light_dir)), 0.00007);
return texture(uShadowMap, p.xy).r < p.z - bias
? 1.0 - uShadowDarkness : 1.0;
}
// Floor 01 wayfinding: a low-strength tint per room, applied to up-facing
// concrete only. The PBR detail stays; this replaces the old opaque flat plates.
#define MAX_ROOM_TINTS 10
uniform vec4 uRoomRect[MAX_ROOM_TINTS]; // x0, z0, x1, z1 in world metres
uniform vec3 uRoomTint[MAX_ROOM_TINTS]; // multiplicative tint, max channel 1
uniform int uRoomCount = 0;
void main()
{
vec3 n = normalize(vNormal);
vec2 uv = vUV * uScale;
vec3 albedo = texture(texture0, uv).rgb;
vec3 tangent_normal = texture(texture2, uv).rgb * 2.0 - 1.0;
// Project the tangent slopes onto the surface's own frame.
vec3 up = abs(n.y) < 0.9 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0);
vec3 bitan = normalize(cross(n, up));
vec3 tan = normalize(cross(bitan, n));
vec3 detailed = normalize(tan * tangent_normal.x
+ bitan * tangent_normal.y
+ n * max(tangent_normal.z, 0.2));
n = normalize(mix(n, detailed, 0.32));
float rough = clamp(texture(texture1, uv).r * vColor.a, 0.08, 1.0);
vec3 key_dir = normalize(vec3(-0.42, 0.86, 0.30));
vec3 fill_dir = normalize(vec3(0.55, 0.25, -0.70));
// Stylised half-Lambert ramp — the analytic twin of the Blender-generated
// actor ramp, so the room and its actors share one shading language.
float ndl = dot(n, key_dir) * 0.5 + 0.5;
float ramp = mix(0.62, 1.30, smoothstep(0.06, 0.94, ndl));
float fill = max(dot(n, fill_dir), 0.0);
float hemi = 0.5 + 0.5 * n.y;
float shade = shadow_factor(n, key_dir);
vec3 lit = albedo * (ramp * shade * (0.92 + 0.08 * hemi) + 0.16 * fill);
float rim = pow(1.0 - max(dot(n, normalize(vec3(0.0, 0.6, 0.8))), 0.0), 3.0);
lit += vec3(0.05, 0.06, 0.07) * rim;
// Sealed-floor sheen: tight highlight on the polished patches.
vec3 view_dir = normalize(uCameraPos - vWorld);
vec3 half_dir = normalize(key_dir + view_dir);
float gloss = mix(48.0, 7.0, rough);
float spec = pow(max(dot(n, half_dir), 0.0), gloss) * (1.0 - rough) * 0.30;
lit += vec3(0.92, 0.90, 0.82) * spec * shade;
// Hiss bleeding into the unlit side of the room as containment fails.
lit += vec3(0.12, 0.008, 0.012) * (1.0 - ndl) * uCorruption * 0.22;
// Room identity: pick the nearest containing rect, softened at its edges.
// Only the horizontal floor gets tinted, never walls or pillars.
if (uRoomCount > 0 && normalize(vNormal).y > 0.5) {
vec3 tint = vec3(1.0);
float best = 0.0;
for (int i = 0; i < MAX_ROOM_TINTS; ++i) {
if (i >= uRoomCount) break;
vec4 r = uRoomRect[i];
float inside = min(min(vWorld.x - r.x, r.z - vWorld.x),
min(vWorld.z - r.y, r.w - vWorld.z));
float w = smoothstep(-0.5, 1.5, inside);
if (w > best) { best = w; tint = uRoomTint[i]; }
}
lit *= mix(vec3(1.0), tint, best);
}
// Volumetric haze: distant concrete settles into the Oldest House gloom.
float dist = length(uCameraPos - vWorld);
float fog = (1.0 - exp(-dist * 0.014)) * 0.50;
lit = mix(lit, vec3(0.055, 0.058, 0.070), fog);
finalColor = vec4(lit * colDiffuse.rgb * vColor.rgb, 1.0);
}