0.0) { vec3 d = L.xyz - p; float d2 = dot(d, d); float att = clamp(1.0 - d2 / (L.w * L.w), 0.0, 1.0); att *= att; float nd = max(dot(n, d * inversesqrt(d2 + 0.0001)), 0.0) * 0.75 + 0.25; c += u_lightCol * (att * nd * 1.9 * (0.5 + 0.5 * ao)); } } return c; } vec3 applyFog(vec3 c, vec3 p) { vec3 d = p - u_eye; float dist = length(d); float h = exp(-max(p.y - u_fog.z, 0.0) * u_fog.y); float f = 1.0 - exp(-dist * u_fog.x * (0.35 + 0.65 * h)); f = max(f, smoothstep(u_fog.w * 0.8, u_fog.w, dist)); float s = max(dot(d / max(dist, 0.001), u_sunDir), 0.0); vec3 fc = mix(u_fogCol, u_fogSun, s * s * s); return mix(c, fc, clamp(f, 0.0, 1.0)); } vec3 tonemap(vec3 x) { x *= u_exposure; // a touch less saturated, like film: greens and skies stop being candy x = mix(vec3(dot(x, vec3(0.3, 0.59, 0.11))), x, 0.86); vec3 c = (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14); return sqrt(clamp(c, 0.0, 1.0)); } GLSL; // ---- terrain ------------------------------------------------------------------------------------ public const TERRAIN_VS = <<<'GLSL' attribute vec3 a_pos; attribute vec3 a_nrm; attribute vec4 a_wa; attribute vec4 a_wb; attribute vec3 a_tint; attribute vec4 a_wc; uniform mat4 u_vp; uniform vec2 u_origin; varying vec3 v_pos; varying vec3 v_nrm; varying vec4 v_wa; varying vec4 v_wb; varying vec3 v_tint; varying vec2 v_uv; varying vec4 v_wc; void main() { v_pos = a_pos; v_nrm = a_nrm; v_wa = a_wa; v_wb = a_wb; v_tint = a_tint; v_wc = a_wc; v_uv = a_pos.xz - u_origin; gl_Position = u_vp * vec4(a_pos, 1.0); } GLSL; /** * The ground: six textures blended by height (their alpha) and by the biome weights of the vertices, a detail * normal map near the eye (pebbles, clumps, cracks lit by the sun), rock outcrops where the slope and a noise * say so, macro variation (no tiling), wet darkening by the water and in the rain, ash where it burnt. */ public const TERRAIN_FS = <<<'GLSL' uniform sampler2D t_grass; uniform sampler2D t_jungle; uniform sampler2D t_mud; uniform sampler2D t_sand; uniform sampler2D t_rock; uniform sampler2D t_snow; uniform sampler2D t_noise; uniform sampler2D t_detail; uniform vec4 u_scale; // near repeats / m, far repeats / m, macro, far blend distance uniform vec4 u_ground; // detail strength, outcrops, grass hue (0 lush .. 1 dry), 0 uniform vec3 u_burnt[8]; // x, z, radius of burnt ground (radius 0: none) varying vec3 v_pos; varying vec3 v_nrm; varying vec4 v_wa; // grass jungle mud sand varying vec4 v_wb; // rock snow ice ao varying vec3 v_tint; varying vec2 v_uv; varying vec4 v_wc; // wet (shore) ash lush 0 vec4 layer(sampler2D t, vec2 a, vec2 b, float fd) { #if QUALITY > 0 return mix(texture2D(t, a), texture2D(t, b), fd); #else return texture2D(t, a); #endif } float hgt(vec4 s) { return clamp((1.0 - s.a * 2.0078 - 0.25) * 1.3333, 0.0, 1.0); } void main() { float dist = distance(v_pos, u_eye); float fd = smoothstep(5.0, u_scale.w, dist); vec2 a = v_uv * u_scale.x; vec2 b = v_uv * u_scale.y; vec3 nrm = normalize(v_nrm); float slope = 1.0 - nrm.y; vec4 mac = texture2D(t_noise, v_uv * u_scale.z); #if QUALITY < 0 vec4 mac2 = mac.gbra; vec4 mac3 = mac.brga; #else vec4 mac2 = texture2D(t_noise, v_uv * u_scale.z * 0.23 + 0.37); vec4 mac3 = texture2D(t_noise, v_uv * u_scale.z * 3.1 + 0.71); #endif vec4 sg = vec4(0.0); vec4 sj = vec4(0.0); vec4 sm = vec4(0.0); vec4 ss = vec4(0.0); vec4 sr = vec4(0.0); vec4 sn = vec4(0.0); float hg = -9.0; float hj = -9.0; float hm = -9.0; float hs = -9.0; float hr = -9.0; float hn = -9.0; float snowW = v_wb.y + v_wb.z; // outcrops: rock breaks through on slopes where a large noise says so float outcrop = smoothstep(0.55, 0.78, mac.b * 0.55 + mac2.r * 0.55 + slope * 1.4 - 0.15) * smoothstep(0.05, 0.22, slope) * u_ground.y; float rockW = max(v_wb.x, outcrop * (1.0 - v_wb.z)); if (v_wa.x > 0.01) { sg = layer(t_grass, a, b, fd); hg = v_wa.x + hgt(sg) * 0.45; } if (v_wa.y > 0.01) { sj = layer(t_jungle, a, b, fd); hj = v_wa.y + hgt(sj) * 0.45; } if (v_wa.z > 0.01) { sm = layer(t_mud, a, b, fd); hm = v_wa.z + hgt(sm) * 0.45; } if (v_wa.w > 0.01) { ss = layer(t_sand, a, b, fd); hs = v_wa.w + hgt(ss) * 0.45; } if (rockW > 0.01) { sr = layer(t_rock, a * 0.5, b * 0.6, fd); hr = rockW + hgt(sr) * 0.6; } if (snowW > 0.01) { sn = layer(t_snow, a * 0.7, b, fd); hn = snowW + hgt(sn) * 0.3; } float best = max(max(max(hg, hj), max(hm, hs)), max(hr, hn)); float k = 0.2; float wg = max(hg - best + k, 0.0); float wj = max(hj - best + k, 0.0); float wm = max(hm - best + k, 0.0); float ws = max(hs - best + k, 0.0); float wr = max(hr - best + k, 0.0); float wn = max(hn - best + k, 0.0); float sum = wg + wj + wm + ws + wr + wn + 0.0001; // per-layer colour variation by large patches (no visible tiling); grass drier or greener by zone vec3 dry = mix(vec3(1.0, 0.95, 0.72), vec3(1.12, 0.98, 0.68), u_ground.z); vec3 grass = sg.rgb * mix(dry, vec3(0.82, 0.96, 0.84), mac.r) * vec3(0.9, 0.86, 0.78); grass = mix(vec3(dot(grass, vec3(0.3, 0.59, 0.11))), grass, 0.78); vec3 jungle = sj.rgb * mix(vec3(1.08, 0.95, 0.85), vec3(0.85, 1.0, 0.8), mac.g); vec3 rock = sr.rgb * v_tint * (0.8 + 0.35 * mac2.r) * mix(vec3(1.0), vec3(1.06, 1.0, 0.92), mac3.g); vec3 sand = ss.rgb * mix(vec3(1.0), v_tint * 1.15, 0.6) * (0.92 + 0.16 * mac3.r); // dark volcanic sand: the ripples flatten float tl = dot(v_tint, vec3(0.33)); sand = mix(vec3(dot(sand, vec3(0.33))) * vec3(1.0, 0.98, 0.97), sand, 0.4 + 0.6 * smoothstep(0.4, 0.9, tl)); float ice = v_wb.z / max(snowW, 0.001); vec3 snow = mix(sn.rgb, sn.rgb * vec3(0.62, 0.8, 0.95) * 0.9 + vec3(0.02, 0.04, 0.07), ice); vec3 col = (grass * wg + jungle * wj + sm.rgb * wm + sand * ws + rock * wr + snow * wn) / sum; col *= 0.82 + 0.36 * mac2.g; // the relief of the dominant texture shades its hollows float hmix = (hgt(sg) * wg + hgt(sj) * wj + hgt(sm) * wm + hgt(ss) * ws + hgt(sr) * wr + hgt(sn) * wn) / sum; float near = 1.0 - smoothstep(12.0, 60.0, dist); col *= mix(1.0, 0.78 + 0.42 * hmix, near * (1.0 - wn / sum)); // the forest floor: darker, greener col = mix(col, col * vec3(0.68, 0.86, 0.6), v_wc.z * 0.55); #if QUALITY > 0 vec4 d1 = texture2D(t_detail, v_uv * 0.8); vec4 d2 = texture2D(t_detail, v_uv * 3.1 + 0.31); vec2 dn = (d1.rg - 0.5) * 1.1 + (d2.rg - 0.5) * 0.8; float strength = (0.6 * wg + 0.7 * wj + 1.1 * wm + 0.9 * ws + 1.4 * wr + 0.4 * wn) / sum * near * u_ground.x; nrm = normalize(nrm + vec3(dn.x, 0.0, dn.y) * strength); col *= mix(1.0, 0.82 + 0.3 * hgt(d1), near * strength * 0.6); #endif // wet: rain, mud, the shores of the water (and darker sand where waves reach) float wetness = clamp(u_wet + v_wc.x * 0.9 + (wm / sum) * 0.25, 0.0, 1.0) * (1.0 - wn / sum); col *= 1.0 - 0.42 * wetness; // ash: burnt ground (the biome, and fresh burns of the game) float ash = v_wc.y; for (int i = 0; i < 8; i++) { vec3 bz = u_burnt[i]; if (bz.z > 0.0) { float d = distance(v_pos.xz, bz.xy) + (mac3.r - 0.5) * bz.z * 0.6; ash = max(ash, smoothstep(bz.z, bz.z * 0.5, d)); } } if (ash > 0.01) { vec4 sp = texture2D(t_noise, v_uv * 0.19 + 0.17); vec3 ashCol = mix(vec3(0.07, 0.065, 0.06), vec3(0.34, 0.33, 0.31), smoothstep(0.3, 0.75, sp.g * 0.5 + mac3.r * 0.6)); ashCol *= 0.75 + 0.5 * hmix; col = mix(col, ashCol, clamp(ash, 0.0, 1.0) * 0.94); } vec3 alb = col * col; float ao = v_wb.w; vec3 c = alb * lightAt(v_pos, nrm, ao, 0.0); #if QUALITY >= 0 // wet ground and ice shine in the sun vec3 v = normalize(u_eye - v_pos); vec3 hv = normalize(u_sunDir + v); float fres = 0.04 + 0.96 * pow(1.0 - max(dot(nrm, v), 0.0), 5.0); float gl = wetness * 0.55 + ice * (wn / sum) * 0.8; float sp2 = pow(max(dot(nrm, hv), 0.0), 60.0 + 200.0 * wetness) * gl; c += u_sunCol * sp2 * 0.8; c += u_skyCol * fres * gl * 0.35; #endif c = applyFog(c, v_pos); gl_FragColor = vec4(tonemap(c), 1.0); } GLSL; // ---- props and bodies --------------------------------------------------------------------------- /** * Things and bodies. Things: a_extra.y = the wind's push, a_bone = gloss. Bodies (SKIN): a_extra.y = gloss, * a_bone = b1 + 32 * b2 + 1024 * round(w * 32), each bone a 3x4 matrix (rows) in u_bones. */ public const PROPS_VS = <<<'GLSL' attribute vec3 a_pos; attribute vec3 a_nrm; attribute vec2 a_uv; attribute vec3 a_col; attribute vec2 a_extra; // ao, sway (things) / gloss (bodies) attribute float a_bone; uniform mat4 u_vp; uniform mat4 u_model; uniform float u_time; uniform vec3 u_wind; uniform vec3 u_push; // the walker: grass bends away from him #ifdef GRASS uniform vec2 u_grassFar; // ground cover: where the blades start to sink, where they are gone #endif #ifdef SKIN uniform vec4 u_bones[60]; vec3 bx(int b, vec4 p) { return vec3(dot(u_bones[b * 3], p), dot(u_bones[b * 3 + 1], p), dot(u_bones[b * 3 + 2], p)); } #endif varying vec3 v_pos; varying vec3 v_nrm; varying vec2 v_uv; varying vec3 v_col; varying vec2 v_extra; // ao, foliage (1) or not varying float v_gloss; void main() { vec3 p = a_pos; vec3 n = a_nrm; float sway = 0.0; #ifdef SKIN float wq = floor(a_bone / 1024.0 + 0.0001); float rest = a_bone - wq * 1024.0; float b2 = floor(rest / 32.0 + 0.0001); float b1 = rest - b2 * 32.0; float w = wq / 32.0; vec4 p4 = vec4(a_pos, 1.0); vec4 n4 = vec4(a_nrm, 0.0); int i1 = int(b1 + 0.5); p = bx(i1, p4); n = bx(i1, n4); if (w > 0.0) { int i2 = int(b2 + 0.5); p = mix(p, bx(i2, p4), w); n = mix(n, bx(i2, n4), w); } v_gloss = a_extra.y; #else sway = a_extra.y; v_gloss = a_bone; #endif vec4 wp = u_model * vec4(p, 1.0); #ifdef GRASS // ground cover: the blades sink into the ground toward the end of their reach (a_bone: the blade's height) float gd = distance(wp.xz, u_push.xz); if (sway > 0.0) { wp.y -= a_bone * smoothstep(u_grassFar.x, u_grassFar.y, gd); } #endif if (sway > 0.0) { float ph = wp.x * 0.35 + wp.z * 0.27; float gust = 0.65 + 0.35 * sin(u_time * 0.4 + wp.x * 0.02 + wp.z * 0.013); float s = sin(u_time * 1.5 + ph) * 0.65 + sin(u_time * 3.7 + ph * 2.3) * 0.25; wp.xz += u_wind.xz * (s * gust + u_wind.y) * sway * 0.12; float flutter = sin(u_time * 7.0 + dot(a_pos, vec3(3.1, 2.3, 4.7))) * 0.025 * sway * (0.3 + length(u_wind.xz)); wp.y += flutter; wp.x += flutter * 0.5; vec2 dd = wp.xz - u_push.xz; float d = length(dd); if (d < 1.1 && sway < 1.0 && wp.y < u_push.y) { float k = (1.1 - d) * sway; wp.xz += dd / max(d, 0.05) * k * 0.6; wp.y -= k * 0.25; } } v_pos = wp.xyz; v_nrm = (u_model * vec4(n, 0.0)).xyz; v_uv = a_uv; v_col = a_col; v_extra = vec2(a_extra.x, sway > 0.0 ? 1.0 : 0.0); gl_Position = u_vp * wp; } GLSL; public const PROPS_FS = <<<'GLSL' uniform sampler2D t_atlas; uniform vec3 u_tint; varying vec3 v_pos; varying vec3 v_nrm; varying vec2 v_uv; varying vec3 v_col; varying vec2 v_extra; varying float v_gloss; void main() { vec4 t = texture2D(t_atlas, v_uv); float op = 1.0 - t.a * 2.0078; #if QUALITY > 0 if (op < 0.3 + 0.35 * dither(gl_FragCoord.xy)) discard; #else if (op < 0.45) discard; #endif vec3 tint = v_col * u_tint; vec3 base = t.rgb * min(tint, vec3(1.5)); // foliage: deeper, less yellow greens base = mix(base, base * vec3(0.78, 0.84, 0.72), v_extra.y); vec3 glow = max(tint - vec3(1.5), vec3(0.0)) * (0.6 + 0.6 * t.rgb); base *= 1.0 - 0.3 * u_wet * (1.0 - v_gloss); vec3 alb = base * base; vec3 n = normalize(v_nrm); float fol = v_extra.y; vec3 c = alb * lightAt(v_pos, n, v_extra.x, fol * 0.45); #if QUALITY >= 0 vec3 v = normalize(u_eye - v_pos); // leaves let the sun through when seen against it c += alb * u_sunCol * fol * pow(max(dot(-v, u_sunDir), 0.0), 4.0) * 0.7 * v_extra.x; // glossy surfaces: metal, wet eyes, chitin, a wet hide float g = clamp(v_gloss + u_wet * 0.35, 0.0, 1.0); float ndv = max(dot(n, v), 0.0); float fres = 0.04 + 0.96 * pow(1.0 - ndv, 5.0); vec3 hv = normalize(u_sunDir + v); c += u_sunCol * pow(max(dot(n, hv), 0.0), 12.0 + 120.0 * g) * g * (0.25 + fres) * v_extra.x; c += u_skyCol * fres * g * 0.45; #ifdef SKIN // fur and skin catch the light at their edges c += (u_skyCol * 0.5 + u_sunCol * 0.18) * alb * pow(1.0 - ndv, 3.0) * 0.9 * v_extra.x; #endif #endif c += glow * glow * 2.5; c = applyFog(c, v_pos); gl_FragColor = vec4(tonemap(c), 1.0); } GLSL; /** * Ground cover (PROPS_VS with GRASS): the blades take the colour of the ground they grow from (the same textures, * tints and large patches as TERRAIN_FS, sampled soft where the blade stands), greener and lighter toward their * tips, the atlas giving only their shape and strands; toward the end of their reach they turn the ground's colour * as they sink into it. v_col: jungle weight, mud weight, the tip's brightness; v_extra.x: 0.55 at the root .. 1 at the tip. */ public const GRASS_FS = <<<'GLSL' uniform sampler2D t_atlas; uniform sampler2D t_grass; uniform sampler2D t_jungle; uniform sampler2D t_mud; uniform sampler2D t_noise; uniform vec4 u_scale; uniform vec4 u_ground; uniform vec2 u_grassFar; varying vec3 v_pos; varying vec3 v_nrm; varying vec2 v_uv; varying vec3 v_col; varying vec2 v_extra; varying float v_gloss; void main() { vec4 t = texture2D(t_atlas, v_uv); float op = 1.0 - t.a * 2.0078; if (op < 0.3 + 0.35 * dither(gl_FragCoord.xy)) discard; vec2 g = v_pos.xz; vec4 mac = texture2D(t_noise, g * u_scale.z); vec4 mac2 = texture2D(t_noise, g * u_scale.z * 0.23 + 0.37); vec3 sg = texture2D(t_grass, g * u_scale.y, 2.5).rgb; vec3 sj = texture2D(t_jungle, g * u_scale.y, 2.5).rgb; vec3 sm = texture2D(t_mud, g * u_scale.y, 2.5).rgb; vec3 dry = mix(vec3(1.0, 0.95, 0.72), vec3(1.12, 0.98, 0.68), u_ground.z); vec3 grass = sg * mix(dry, vec3(0.82, 0.96, 0.84), mac.r) * vec3(0.9, 0.86, 0.78); grass = mix(vec3(dot(grass, vec3(0.3, 0.59, 0.11))), grass, 0.78); vec3 jungle = sj * mix(vec3(1.08, 0.95, 0.85), vec3(0.85, 1.0, 0.8), mac.g); vec3 ground = mix(mix(grass, jungle, v_col.x), sm, v_col.y) * (0.82 + 0.36 * mac2.g); float h = clamp((v_extra.x - 0.55) / 0.45, 0.0, 1.0); float lum = dot(t.rgb, vec3(0.3, 0.59, 0.11)); vec3 blade = ground * mix(vec3(0.62, 0.66, 0.58), vec3(0.98, 1.14, 0.8) * v_col.z, h) * (0.55 + 1.5 * lum); // the jungle's floor is leaf litter: what grows from it is still green blade *= mix(vec3(1.0), vec3(0.72, 1.12, 0.55), v_col.x * (0.35 + 0.5 * h)); float dist = distance(v_pos, u_eye); blade = mix(blade, ground * 0.92, smoothstep(u_grassFar.x * 0.7, u_grassFar.y, dist)); blade *= 1.0 - 0.3 * u_wet; vec3 alb = blade * blade; vec3 c = alb * lightAt(v_pos, normalize(v_nrm), 0.6 + 0.4 * h, 0.45); c = applyFog(c, v_pos); gl_FragColor = vec4(tonemap(c), 1.0); } GLSL; // ---- sky ------------------------------------------------------------------------------------------ public const SKY_VS = <<<'GLSL' attribute vec3 a_pos; uniform mat4 u_vp; uniform vec3 u_eye; varying vec3 v_dir; void main() { v_dir = a_pos; gl_Position = u_vp * vec4(u_eye + a_pos * 50.0, 1.0); } GLSL; public const SKY_FS = <<<'GLSL' uniform sampler2D t_noise; uniform vec3 u_zenith; uniform vec3 u_horizon; uniform vec3 u_glow; uniform vec3 u_sunDisc; uniform vec3 u_moonDir; uniform vec4 u_cloud; // cover 0..1, darkness 0..1, wind offset x, z uniform vec3 u_cloudCol; uniform float u_stars; uniform float u_haze; // weather: the sky melts into the fog uniform float u_flash; uniform vec4 u_ridge; // far mountains: height (0 none), seed, snow, 0 varying vec3 v_dir; void main() { vec3 d = normalize(v_dir); float e = d.y; vec3 c = mix(u_horizon, u_zenith, pow(clamp(e, 0.0, 1.0), 0.55)); if (e < 0.0) { c = mix(u_horizon, u_fogCol, clamp(-e * 4.0, 0.0, 1.0)); } float s = max(dot(d, u_sunDir), 0.0); c += u_glow * (pow(s, 5.0) * 0.6 + pow(s, 32.0) * 0.8) * (1.0 - u_haze * 0.7); c += u_sunDisc * (smoothstep(0.99935, 0.99965, s) * 30.0 + pow(s, 900.0) * 3.0) * (1.0 - u_cloud.x * 0.5); // moon: a disc with dark seas float m = dot(d, u_moonDir); if (m > 0.995) { vec3 side = normalize(cross(u_moonDir, vec3(0.0, 1.0, 0.0))); vec3 up = cross(side, u_moonDir); vec2 mp = vec2(dot(d, side), dot(d, up)) * 70.0; float seas = texture2D(t_noise, mp * 0.35 + 0.5).r; float disc = smoothstep(1.0, 0.92, length(mp)); c += vec3(0.75, 0.78, 0.85) * disc * (0.65 + 0.35 * seas) * (0.5 + 0.5 * u_stars) * 1.6; } c += vec3(0.25, 0.3, 0.4) * pow(max(m, 0.0), 120.0) * 0.15 * u_stars; // stars: points of a cellular noise, two scales if (u_stars > 0.01 && e > 0.0) { vec2 sp = d.xz / (e + 0.9); float st = smoothstep(0.03, 0.0, texture2D(t_noise, sp * 2.3).b); st += smoothstep(0.022, 0.0, texture2D(t_noise, sp * 5.1 + 0.3).b) * 0.6; float tw = 0.6 + 0.4 * texture2D(t_noise, sp * 9.0 + u_cloud.zw * 4.0).g; c += vec3(0.9, 0.92, 1.0) * st * tw * u_stars * smoothstep(0.0, 0.25, e) * (1.0 - u_cloud.x); } // clouds on a plane far above if (e > -0.02) { vec2 cp = d.xz / (e + 0.12) * 0.12 + u_cloud.zw; float n = texture2D(t_noise, cp).r * 0.62 + texture2D(t_noise, cp * 2.7 + 0.21).g * 0.28 + texture2D(t_noise, cp * 7.0).a * 0.1; float cov = u_cloud.x; float cl = smoothstep(1.0 - cov - 0.08, 1.0 - cov + 0.32, n) * smoothstep(-0.02, 0.18, e); float thick = smoothstep(1.0 - cov, 1.0 - cov + 0.6, n); vec3 cc = u_cloudCol * mix(1.15, 0.55, thick * (0.4 + 0.6 * u_cloud.y)); cc += u_glow * pow(s, 6.0) * (1.0 - thick) * 1.4; c = mix(c, cc, cl * 0.95); } // far mountain ranges on the horizon: two layers, the farther one paler (aerial perspective) if (u_ridge.x > 0.0 && e < u_ridge.x * 1.6 + 0.02) { vec2 q = d.xz / max(length(d.xz), 0.0001); for (int k = 0; k < 2; k++) { float fk = float(k); float r1 = texture2D(t_noise, q * (0.16 + 0.1 * fk) + u_ridge.y + fk * 0.37).r; float r2 = texture2D(t_noise, q * (0.55 + 0.2 * fk) + u_ridge.y + 0.5).g; float r3 = texture2D(t_noise, q * 1.7 + u_ridge.y + fk).a; float ridge = u_ridge.x * (1.0 - 0.45 * fk) * (0.15 + 1.1 * r1 * r1 + 0.3 * r2 + 0.06 * r3) - 0.01; if (e < ridge) { float lit = 0.75 + 0.35 * dot(normalize(vec3(q.x, 0.4, q.y)), u_sunDir); vec3 mc = mix(u_horizon * 0.72 + u_zenith * 0.12, u_horizon * 0.95, 0.35 + 0.45 * (1.0 - fk)) * lit; float snowy = u_ridge.z * smoothstep(ridge - 0.03, ridge - 0.005, e) * smoothstep(0.35, 0.6, r1); mc = mix(mc, u_horizon * 1.15 + u_glow * 0.2, snowy); c = mix(c, mc, smoothstep(ridge, ridge - 0.004, e) * (0.9 - 0.3 * fk)); } } } c = mix(c, u_fogCol, u_haze * (1.0 - smoothstep(0.0, 0.9, e) * 0.35)); c += vec3(0.8, 0.85, 1.0) * u_flash; gl_FragColor = vec4(tonemap(c), 1.0); } GLSL; // ---- water -------------------------------------------------------------------------------------- public const WATER_VS = <<<'GLSL' attribute vec3 a_pos; attribute float a_depth; attribute vec2 a_flow; uniform mat4 u_vp; varying vec3 v_pos; varying float v_depth; varying vec2 v_flow; void main() { v_pos = a_pos; v_depth = a_depth; v_flow = a_flow; gl_Position = u_vp * vec4(a_pos, 1.0); } GLSL; /** * Water: ripples of three noise octaves (wind), a flow map where a river runs (two phases blended, no seam), * Fresnel sky reflection and sun glint, light absorbed with depth (the bed shows through shallows, murky or * clear by zone), foam lines along the shores and in fast water, rings of rain drops. */ public const WATER_FS = <<<'GLSL' uniform sampler2D t_noise; uniform vec3 u_zenith; uniform vec3 u_horizon; uniform vec4 u_flow; // two scrolling offsets (fract of time: the noise repeats) uniform vec4 u_phase; // flow phases 0..1 (two, half a cycle apart), time (wrapped), rain uniform vec3 u_deep; // the colour light scatters to in deep water uniform vec3 u_absorb; // absorption per metre (r, g, b) uniform vec2 u_origin; uniform float u_rough; uniform vec3 u_bed; uniform vec4 u_banks; // the forest reflected low on the water: colour, amount varying vec3 v_pos; varying float v_depth; varying vec2 v_flow; void main() { vec2 p = v_pos.xz - u_origin; float speed = length(v_flow); // a flow map: the texture slides along the current, two phases fading in and out vec2 o0 = v_flow * u_phase.x * 2.5; vec2 o1 = v_flow * u_phase.y * 2.5; float w0 = 1.0 - abs(1.0 - 2.0 * u_phase.x); vec4 f0 = texture2D(t_noise, (p - o0) * 0.09 + 0.13); vec4 f1 = texture2D(t_noise, (p - o1) * 0.09 + 0.61); vec4 fl = mix(f1, f0, w0); vec4 n1 = texture2D(t_noise, p * 0.045 + u_flow.xy); #if QUALITY < 0 vec4 n2 = n1.gbar; vec4 n3 = texture2D(t_noise, p * 0.31 - u_flow.yx * 1.7); #else vec4 n2 = texture2D(t_noise, p * 0.11 + u_flow.zw); vec4 n3 = texture2D(t_noise, p * 0.31 - u_flow.yx * 1.7); #endif vec2 slope = (n1.rg - 0.5) * 0.9 + (n2.gr - 0.5) * 0.6 + (n3.rg - 0.5) * 0.35; float fk = clamp(speed * 1.5, 0.0, 1.0); slope = mix(slope, (fl.rg - 0.5) * 1.6 + (n3.rg - 0.5) * 0.3, fk); #if QUALITY > 0 // fine ripples near the eye float eyeD = distance(v_pos, u_eye); vec4 n4 = texture2D(t_noise, (p - o0 * fk) * 0.9 + u_flow.wz * 3.0); vec4 n5 = texture2D(t_noise, p * 2.3 - u_flow.xy * 5.0); slope += ((n4.gr - 0.5) * 0.55 + (n5.rg - 0.5) * 0.3) * (1.0 - smoothstep(8.0, 40.0, eyeD)); #endif // rain: rings spreading from the drops if (u_phase.w > 0.0) { vec4 rc = texture2D(t_noise, p * 0.7); float age = fract(rc.g * 5.0 + u_phase.z * 1.1); float ring = sin(rc.b * 90.0 - age * 22.0) * (1.0 - age) * smoothstep(0.0, 0.1, age); slope += vec2(ring) * 0.6 * u_phase.w * (rc.r - 0.4); } vec3 n = normalize(vec3(slope.x * u_rough, 1.0, slope.y * u_rough)); vec3 toEye = u_eye - v_pos; float dist = length(toEye); vec3 v = toEye / dist; float fres = 0.02 + 0.98 * pow(1.0 - max(dot(n, v), 0.0), 5.0); vec3 r = reflect(-v, n); vec3 sky = mix(u_horizon, u_zenith, pow(clamp(r.y, 0.0, 1.0), 0.55)); sky = mix(sky, u_banks.rgb, u_banks.a * (1.0 - smoothstep(0.015, 0.1 + 0.08 * n2.r, r.y))); float sp = pow(max(dot(r, u_sunDir), 0.0), 380.0); vec3 refl = sky + u_sunCol * sp * 7.0; vec3 pl = vec3(0.0); for (int i = 0; i < 4; i++) { vec4 L = u_lights[i]; if (L.w > 0.0) { vec3 d = L.xyz - v_pos; float att = clamp(1.0 - dot(d, d) / (L.w * L.w), 0.0, 1.0); pl += u_lightCol * att * att * 1.6; } } refl += pl * 0.4; vec3 light = u_sunCol * max(u_sunDir.y, 0.0) * 0.7 + u_skyCol + pl; // what is under the surface: the bed, absorbed with depth, and the light scattered by the water float depth = max(v_depth, 0.0) * (1.0 + (1.0 - v.y) * 1.5); vec3 tr = exp(-u_absorb * depth); vec3 bed = u_bed * u_bed * light * (0.85 + 0.3 * n1.b); vec3 under = bed * tr + u_deep * light * (1.0 - tr); vec3 c = mix(under, refl, fres); // foam: lines along the shore moving with the waves, white water in the current float shore = smoothstep(0.45, 0.0, v_depth); float lines = smoothstep(0.55, 0.95, sin(v_depth * 28.0 - u_phase.z * 1.7 + n2.b * 5.0) * 0.5 + 0.5) * shore; float foam = max(smoothstep(0.12, 0.0, v_depth) * (0.5 + 0.5 * n2.b), lines * 0.7 * smoothstep(0.35, 0.65, n1.a)); foam = max(foam, smoothstep(0.78, 0.95, fl.b * 0.6 + fl.a * 0.5) * fk * 0.45); c = mix(c, light * 0.55, foam * 0.5); if (v_depth < 0.03 && v_depth / 0.03 < dither(gl_FragCoord.xy)) discard; c = applyFog(c, v_pos); gl_FragColor = vec4(tonemap(c), 1.0); } GLSL; // ---- flames, glows --------------------------------------------------------------------------------- public const FLAME_VS = <<<'GLSL' attribute vec3 a_pos; attribute vec2 a_uv; uniform mat4 u_vp; uniform mat4 u_model; varying vec2 v_uv; varying vec3 v_pos; void main() { vec4 w = u_model * vec4(a_pos, 1.0); v_pos = w.xyz; v_uv = a_uv; gl_Position = u_vp * w; } GLSL; public const FLAME_FS = <<<'GLSL' uniform sampler2D t_noise; uniform vec4 u_flame; // flow, seed, alpha, mode (0 flame, 1 glow, 2 steam / spray: dithered, 3 smoke: blended) uniform vec3 u_glowCol; uniform vec3 u_behind; // steam: a guess of what lies behind (the haze): dots of a thin veil are taken toward it varying vec2 v_uv; varying vec3 v_pos; void main() { vec2 uv = v_uv; if (u_flame.w > 1.5) { // a soft round puff, its edge broken by two octaves of drifting noise; lighter toward its top vec2 q = uv - 0.5; float r = length(q) * 2.0; float n = texture2D(t_noise, uv * 0.55 + vec2(u_flame.y, -u_flame.x * 0.3)).r; float n2 = texture2D(t_noise, uv * 1.4 + vec2(-u_flame.x * 0.5, u_flame.y)).g; float body = 1.0 - smoothstep(0.15, 1.0, r + (n - 0.5) * 0.55 + (n2 - 0.5) * 0.25); float a = body * body * (0.7 + 0.3 * n2) * u_flame.z; vec3 col = u_glowCol * (0.82 + 0.3 * uv.y + 0.12 * (n - 0.5)); if (u_flame.w > 2.5) { // blended smoke (WebGl::draw blend): the layer's alpha drops to a*a + (1 - a) under it and the // composite multiplies by it, so a and the colour are chosen for the transmittance asked (1 - A) float A = clamp(a, 0.0, 0.92); if (A < 0.004) discard; float b = A * 0.5; for (int i = 0; i < 3; i++) { float f = A - 2.0 * b + 2.0 * b * b - b * b * b; b -= f / (-2.0 + 4.0 * b - 3.0 * b * b); } b = clamp(b, 0.001, 0.999); float k = 1.0 - b + b * b; gl_FragColor = vec4(min(col * A / (b * k), vec3(1.0)), b); return; } // dithered steam: more dots than the opacity asks (a^0.42), each taken toward the haze behind, so that the // average is right and every dot stands out less float keep = pow(clamp(a, 0.0, 1.0), 0.42); if (keep < ditherT(gl_FragCoord.xy) * 0.98 + 0.02) discard; gl_FragColor = vec4(mix(u_behind, col, clamp(a / max(keep, 0.001), 0.0, 1.0)), 1.0); return; } if (u_flame.w > 0.5) { float r = length(uv - 0.5) * 2.0; float a = pow(clamp(1.0 - r, 0.0, 1.0), 2.2) * u_flame.z; if (a < ditherT(gl_FragCoord.xy) * 0.98 + 0.01) discard; gl_FragColor = vec4(u_glowCol, 1.0); return; } float n = texture2D(t_noise, vec2(uv.x * 0.5 + u_flame.y, uv.y * 0.55 - u_flame.x)).r; float n2 = texture2D(t_noise, vec2(uv.x * 1.3 - u_flame.y, uv.y * 1.1 - u_flame.x * 1.7)).g; float n3 = texture2D(t_noise, vec2(uv.x * 2.9 + u_flame.y * 2.0, uv.y * 2.2 - u_flame.x * 2.9)).a; // tongues: the column sways and splits as it rises float x = (uv.x - 0.5) * 2.0 + (n2 - 0.5) * 0.9 * uv.y; float tongues = 0.5 + 0.5 * sin(x * 7.0 + n * 4.0); float width = (1.0 - uv.y * 0.85) * (0.75 + 0.25 * tongues); float shape = width - abs(x) + (n * 0.6 + n3 * 0.4 - 0.5) * (0.25 + uv.y * 0.9) - uv.y * 0.35; shape *= smoothstep(0.0, 0.12, uv.y + 0.04); float f = smoothstep(0.0, 0.22, shape); vec3 c = mix(vec3(0.75, 0.1, 0.02), vec3(1.0, 0.45, 0.06), smoothstep(0.05, 0.35, shape)); c = mix(c, vec3(1.0, 0.86, 0.5), smoothstep(0.35, 0.7, shape)); if (f * u_flame.z < ditherT(gl_FragCoord.xy) * 0.9 + 0.05) discard; gl_FragColor = vec4(c, 1.0); } GLSL; /** A puff of smoke or steam: a quad turned to the eye (u_right, u_up), centred on u_centre, u_size metres. */ public const PUFF_VS = <<<'GLSL' attribute vec3 a_pos; attribute vec2 a_uv; uniform mat4 u_vp; uniform vec3 u_centre; uniform vec2 u_size; uniform vec3 u_right; uniform vec3 u_up; varying vec2 v_uv; varying vec3 v_pos; void main() { vec3 w = u_centre + u_right * a_pos.x * u_size.x + u_up * a_pos.y * u_size.y; v_pos = w; v_uv = a_uv; gl_Position = u_vp * vec4(w, 1.0); } GLSL; /** * A waterfall's sheet: streaks of noise flowing down (u_fall.x), screen-door translucency thinning at both edges, * lit like the rest (sun or moon, sky, torch and fires). */ public const FALL_FS = <<<'GLSL' uniform sampler2D t_noise; uniform vec4 u_fall; // flow, opacity, seed, 0 uniform vec3 u_nrm; varying vec2 v_uv; varying vec3 v_pos; void main() { float n1 = texture2D(t_noise, vec2(v_uv.x * 1.3 + u_fall.z, v_uv.y * 0.22 - u_fall.x)).r; float n2 = texture2D(t_noise, vec2(v_uv.x * 3.7 - u_fall.z, v_uv.y * 0.55 - u_fall.x * 1.7)).g; float streak = smoothstep(0.45, 0.85, n1 * 0.6 + n2 * 0.5); float edge = smoothstep(0.0, 0.16, v_uv.x) * smoothstep(1.0, 0.84, v_uv.x); float a = (0.3 + 0.7 * streak) * edge * u_fall.y; vec3 alb = mix(vec3(0.22, 0.33, 0.38), vec3(0.88, 0.93, 0.97), streak); alb *= alb; vec3 c = alb * lightAt(v_pos, normalize(u_nrm), 1.0, 0.6); c = applyFog(c, v_pos); if (a < ditherT(gl_FragCoord.xy) * 0.98 + 0.01) discard; gl_FragColor = vec4(tonemap(c), 1.0); } GLSL; /** A lightning bolt: a ribbon of hot white-violet light, fading with its age (u_bolt.x 0..1). */ public const BOLT_FS = <<<'GLSL' uniform vec4 u_bolt; // fade 0..1, flicker, 0, 0 varying vec2 v_uv; varying vec3 v_pos; void main() { float core = 1.0 - abs(v_uv.x * 2.0 - 1.0); float a = pow(core, 1.5) * u_bolt.x * (0.6 + 0.4 * u_bolt.y) * (0.5 + 0.5 * v_uv.y); if (a < ditherT(gl_FragCoord.xy) * 0.9 + 0.05) discard; gl_FragColor = vec4(mix(vec3(0.7, 0.65, 1.0), vec3(1.0), smoothstep(0.5, 0.9, core)), 1.0); } GLSL; // ---- rain and snow ---------------------------------------------------------------------------------- public const PRECIP_VS = <<<'GLSL' attribute vec4 a_seed; // xyz 0..1 in the box, w: corner 0..3 of the quad uniform mat4 u_vp; uniform vec3 u_eye; uniform vec4 u_box; // size, fall offset, streak length, width uniform vec3 u_drift; // wind push per metre of fall uniform vec3 u_right; uniform vec3 u_up; uniform float u_snow; varying float v_a; varying vec2 v_c; void main() { float size = u_box.x; vec3 base = a_seed.xyz * size; base.y -= u_box.y; base.xz += u_drift.xz * u_box.y; vec3 rel = mod(base - u_eye + size * 0.5, size) - size * 0.5; vec3 w = u_eye + rel; float cx = step(0.5, a_seed.w) * step(a_seed.w, 2.5); float cy = step(1.5, a_seed.w); v_c = vec2(cx, cy); if (u_snow > 0.5) { w.x += sin(a_seed.y * 40.0 + u_box.y * 0.7) * 0.4; w.z += cos(a_seed.x * 37.0 + u_box.y * 0.6) * 0.4; w += u_right * (cx - 0.5) * u_box.w + u_up * (cy - 0.5) * u_box.w; } else { w += u_right * (cx - 0.5) * u_box.w + vec3(u_drift.x, -1.0, u_drift.z) * u_box.z * cy; } float d = length(rel); v_a = clamp(1.0 - d / (size * 0.5), 0.0, 1.0) * smoothstep(0.8, 2.5, d); gl_Position = u_vp * vec4(w, 1.0); } GLSL; public const PRECIP_FS = <<<'GLSL' uniform vec4 u_color; uniform float u_snow; varying float v_a; varying vec2 v_c; void main() { float a = u_color.a * v_a; vec2 q = v_c - 0.5; if (u_snow > 0.5) { a *= smoothstep(0.25, 0.08, dot(q, q)); } else { a *= smoothstep(0.5, 0.0, abs(q.x)) * (0.3 + 0.7 * v_c.y); } if (a < ditherT(gl_FragCoord.xy) * 0.98 + 0.01) discard; gl_FragColor = vec4(u_color.rgb, 1.0); } GLSL; // ---- post --------------------------------------------------------------------------------------- public const POST = <<<'GLSL' uniform float u_hurt; uniform float u_vignette; uniform float u_flashP; void main() { vec3 c = texture2D(u_frame, v_uv).rgb; vec2 q = v_uv - 0.5; float r2 = dot(q, q); c *= 1.0 - r2 * u_vignette; c = mix(c, vec3(0.7, 0.04, 0.02), u_hurt * clamp(0.18 + r2 * 2.4, 0.0, 1.0)); c += vec3(0.9, 0.95, 1.0) * u_flashP * 0.25; gl_FragColor = vec4(c, 1.0); } GLSL; public static function quality(int $q, string $defines = ''): string { return "#define QUALITY $q\n" . $defines; } }