import*asTHREEfrom'three';import{ARENA,U,WORLD_W,MAX_LIGHTS,MAX_IMPACTS,MAX_OCCLUDERS,COMMON_GLSL,NOISE_GLSL,WORLD_GLSL,LIGHTS_GLSL,GROUND_GLSL}from'../vfx/common.js';import{CRACK_SOURCES,FISSURE_A,FISSURE_B}from'../vfx/GroundField.js';// Every lit arena surface (floor, stone, dummies) is one lean ShaderMaterial pipeline, so they all// react to spells the same way and the idle arena stays cheap (the spells are the product):// 1. state: impacts (spherical volume decals) + the ground field (full strength on the floor,// creeping up the foot of stone)// 2. the surface itself (slabs, stone, wood...): one or two noise taps
h x y s
h h h
h h
data
xi x yi y fx x xi fy y yi
ux fx fx fx uy fy fy fy
x0 xi y0 yi x1 x0 y1 y0
a x0 y0 s b x1 y0 s c x0 y1 s d x1 y1 s
a b a ux c a uy a b c d ux uy
sum amp norm
o o oct o f base o sum amp u f v f f s o norm amp amp
sum norm
pts
j j j i i i ptsi i j j i j
y y y x x x
u x v y o y x
cx u cy v ix cx iy cy
f1 f2
dj dj dj di di di
gi ix di gj iy dj wi gi wj gj
px ptswj wi gi wi py ptswj wi gj wj
d px cx py cy
d f1 f2 f1 f1 d d f2 f2 d
datao u v
datao u v
datao f2 f1
datao u v
t data
t t
t
t
t
t
t
t
arenaUniforms
t
t
arenaUniforms t
worldData
worldUniforms
worldData
worldData
worldData
worldInUniforms
worldInUniforms renderer
worldInUniforms
x
pars
mode kind uniforms surface mode parsGLSL vertexParsGLSL vertexGLSL
mode
arenaUniforms worldUniforms kind uniforms
vertexParsGLSL parsGLSL vertexGLSL
surface parsGLSL
geometry geometry geometryvalue
geometry
kind
// needs COMMON_GLSL + WORLD_GLSL + LIGHTS_GLSL (the dummy ice shell and the pool use it too)
export
const
LIGHT_FN_GLSL
=
/* glsl */
`
#ifdef ARENA_WORLD_UNIFORMS
uniform vec4 uWL[${WORLD_W}];
uniform vec4 uWI[${WORLD_W}];
uniform vec4 uWO[${WORLD_W}];
#define WL(i) uWL[i]
#define WI(i) uWI[i]
#define WO(i) uWO[i]
#else
#define WL(i) worldTexel(0, i)
#define WI(i) worldTexel(2, i)
#define WO(i) worldTexel(3, i)
#endif
// All three loops below are split in two: a cheap pass sets one bit per item that can matter at
// this pixel, then the expensive body runs once per set bit. With the body behind a plain
// "if (culled) continue;" the D3D compiler (ANGLE) predicates it and every pixel paid for every
// light; a loop whose trip count is the bit count cannot be flattened.
#define NEXT_BIT(mask, i) { uint bit_ = mask & (~mask + 1u); mask ^= bit_; i = int(log2(float(bit_)) + 0.5); }
// LIGHTS_GLSL's capsuleShadow, culled: an occluder only matters if its capsule (plus the widest
// penumbra) reaches the bounding sphere of the shadow ray; stops once fully shadowed.
float arenaCapsuleShadow(vec3 p, vec3 lp) {
vec3 dir = lp - p;
float len = length(dir);
p += dir * min(0.15 / max(len, 1e-3), 0.5);
vec3 c = 0.5 * (p + lp);
float R = 0.5 * length(lp - p) + 0.04 + 0.18 * len;
int n = int(uWorldCounts.w);
uint mask = 0u;
for (int i = 0; i < ${MAX_OCCLUDERS}; i++) {
if (i >= n) break;
vec4 a = WO(i * 2);
vec3 q = closestOnSegment(a.xyz, WO(i * 2 + 1).xyz, c) - c;
float reach = R + a.w;
if (dot(q, q) <= reach * reach) mask |= 1u << uint(i);
}
float vis = 1.0;
while (mask != 0u) {
int i; NEXT_BIT(mask, i)
vec4 a = WO(i * 2), b = WO(i * 2 + 1);
float s, d = segSegDist(p, lp, a.xyz, b.xyz, s);
float pen = 0.04 + 0.18 * s * len;
vis *= smoothstep(a.w - pen * 0.5, a.w + pen, d);
if (vis < 0.003) return 0.0;
}
return vis;
}
// A light hovering over the pedestal (the focus orb, a spell charging at the focus) throws the
// pedestal's shadow as a soft disc on the floor around its foot: analytic, so the orb light needs
// no capsule shadow (a capsule's round top would also shade far too wide a disc).
float pedestalShadow(vec3 p, vec3 lp) {
vec2 c = vec2(${f(ARENA.pedestal.pos.x)}, ${f(ARENA.pedestal.pos.z)});
float hl = lp.y - p.y;
if (length(lp.xz - c) > 0.3 || hl < 1.2) return 1.0;
float d0 = 0.45 * hl / (hl - 1.15); // umbra edge: the ray grazing the capital's rim
return smoothstep(d0 * 0.8, d0 * 1.25, length(p.xz - c));
}
// Spell lights (LIGHTS_GLSL's spellLightPBR, restructured for the hot path): lights are culled
// on their window (range) before any shadow or BRDF work; a surface facing away skips the capsule
// shadow, and the shadow is traced once (at the diffuse point) and shared with the line light's
// specular point. base: luminance of the pixel's other lighting; a light adding less than 4% of it
// skips its capsule shadow (invisible there, and a wide dim light shadow-tracing 20 occluders over
// the whole screen cost ~0.4 ms at 3.3 MP). The pool and the ice shell pass 0: always traced.
void arenaSpellLights(vec3 p, vec3 n, vec3 v, float rough, vec3 f0, vec3 diffColor, float base, out vec3 diffuse, out vec3 specular) {
diffuse = vec3(0.0); specular = vec3(0.0);
int count = int(uWorldCounts.x);
uint mask = 0u;
for (int i = 0; i < ${MAX_LIGHTS}; i++) {
if (i >= count) break;
vec4 la = WL(i * 4);
vec3 lp = WL(i * 4 + 1).a == 1.0 ? closestOnSegment(la.xyz, WL(i * 4 + 2).xyz, p) : la.xyz;
vec3 d = lp - p;
if (dot(d, d) < la.w * la.w) mask |= 1u << uint(i);
}
if (mask == 0u) return;
float a = max(rough * rough, 0.002);
float nv = max(dot(n, v), 1e-4);
vec3 r = reflect(-v, n);
while (mask != 0u) {
int i; NEXT_BIT(mask, i)
vec4 la = WL(i * 4), lb = WL(i * 4 + 1), lc = WL(i * 4 + 2);
int type = int(lb.a);
vec3 lp = type == 1 ? closestOnSegment(la.xyz, lc.xyz, p) : la.xyz;
vec3 d = lp - p;
float dist = length(d);
vec3 l = d / max(dist, 1e-4), ls = l;
float dists = dist;
if (type == 1) { vec3 sp = closestOnSegmentToRay(la.xyz, lc.xyz, p, r) - p; dists = length(sp); ls = sp / max(dists, 1e-4); }
float nl = dot(n, l), nls = dot(n, ls);
if (nl <= 0.0 && nls <= 0.0) continue;
vec3 col = lb.rgb;
if (type == 2) col *= lightCookie(SpellLight(la.xyz, la.w, lb.rgb, type, lc.xyz, lc.w, WL(i * 4 + 3)), p);
#ifdef ARENA_FLOOR
col *= pedestalShadow(p, lp);
#endif
vec3 dl = nl > 0.0 ? col * (lightFalloff(dist, la.w) * nl / PI) * diffColor : vec3(0.0), sl = vec3(0.0);
if (nls > 0.0) {
vec3 h = normalize(ls + v);
float nh = max(dot(n, h), 0.0), vh = max(dot(v, h), 0.0);
vec3 F = f0 + (1.0 - f0) * pow(1.0 - vh, 5.0);
sl = col * F * (lightFalloff(dists, la.w) * spellD_GGX(nh, a) * spellV_Smith(nv, nls, a) * nls);
// line lights: Karis's energy normalization, or a long segment's highlight clips into a white bar
if (type == 1) sl *= a / min(a + length(lc.xyz - la.xyz) / (2.0 * dists), 1.0);
}
if (lc.w > 0.5 && !gLightNoShadow && dot(dl + sl, vec3(0.2126, 0.7152, 0.0722)) > 0.04 * base) {
float sh = arenaCapsuleShadow(p, lp);
dl *= sh; sl *= sh;
}
diffuse += dl; specular += sl;
}
}
void arenaSpellLights(vec3 p, vec3 n, vec3 v, float rough, vec3 f0, vec3 diffColor, out vec3 diffuse, out vec3 specular) {
arenaSpellLights(p, n, v, rough, f0, diffColor, 0.0, diffuse, specular);
}
`
;
// IMPACTS_GLSL's impactWeights, culled the same way; the type is routed with a mask (dynamic vector
// indexing is emulated on D3D, and slow).
// Same world-texture layout as common.js (row 2: pos+radius, type/age/amount, colour).
const
IMPACT_FN_GLSL
=
/* glsl */
`
void arenaImpacts(vec3 p, out vec4 wa, out vec4 wb, out vec3 glowColor) {
wa = vec4(0.0); wb = vec4(0.0); glowColor = vec3(0.0);
int n = int(uWorldCounts.z);
uint mask = 0u;
for (int i = 0; i < ${MAX_IMPACTS}; i++) {
if (i >= n) break;
vec4 a = WI(i * 4);
vec3 d = p - a.xyz;
if (dot(d, d) < a.w * a.w) mask |= 1u << uint(i);
}
while (mask != 0u) {
int i; NEXT_BIT(mask, i)
vec4 a = WI(i * 4), b = WI(i * 4 + 1);
float w = b.z * smoothstep(1.0, 0.55, length(p - a.xyz) / a.w);
wa += w * vec4(equal(vec4(b.x), vec4(0.0, 1.0, 2.0, 3.0)));
wb += w * vec4(equal(vec4(b.x), vec4(4.0, 5.0, 6.0, 7.0)));
glowColor += WI(i * 4 + 2).rgb * w;
}
}
`
;
export
const
SURFACE_PARS
=
COMMON_GLSL
+
NOISE_GLSL
+
WORLD_GLSL
+
LIGHTS_GLSL
+
LIGHT_FN_GLSL
+
IMPACT_FN_GLSL
+
GROUND_GLSL
+
/* glsl */
`
uniform sampler2D uStoneNoise;
uniform samplerCube uSkyCube;
uniform highp sampler2DShadow uShadowMap;
uniform mat4 uShadowMatrix;
uniform float uShadowOn;
uniform vec3 uSunDir;
uniform vec3 uSunColor;
uniform vec3 uAmbient;
uniform vec3 uRuneColor;
uniform vec3 uFocusColor;
uniform float uEnvScale;
uniform float uKind;
varying vec3 vWPos;
varying vec3 vNrm;
varying vec3 vLocal;
varying float vAO;
flat varying float vSeed;
float gAO = 1.0; // cavity occlusion of ambient
float gSpecAO = 1.0; // ... and of reflections
float gGroove = 0.0; // in a narrow groove (floor grout): spell lights low over the floor can't reach in
float gHeatOpen = -1.0; // where heat glows through (>= 0 overrides crevice: the floor's grout barely glows)
float gFrostGlint = 0.0;// hoarfrost micro-facet glint, x sun
float stFrostImp = 0.0; // frost from impacts alone (the floor re-reads the field's own)
uniform vec4 uCrackSrc[4];
uniform vec4 uFissA[4];
uniform vec4 uFissB[4];
// metres from p to fissure i's line, and where along it (u) and across it (v)
float fissDist(int i, vec2 p, out float u, out float v, out float L) {
vec2 ab = uFissA[i].zw - uFissA[i].xy;
L = max(length(ab), 1e-3);
vec2 T = ab / L, q = p - uFissA[i].xy;
u = dot(q, T); v = dot(q, vec2(-T.y, T.x));
return length(q - T * clamp(u, 0.0, L));
}
float gVent = 0.0; // craterNear() of this floor pixel (computed once by the floor)
// how near a crater or a fissure vent this is (their own fractures carry the glow there, not the
// generic thermal crackle or the field's cell network)
float craterNear(vec2 p) {
float c = 0.0, u, v, L;
if (dot(vec4(uCrackSrc[0].w, uCrackSrc[1].w, uCrackSrc[2].w, uCrackSrc[3].w) + vec4(uFissB[0].y, uFissB[1].y, uFissB[2].y, uFissB[3].y), vec4(1.0)) <= 0.0) return 0.0;
for (int i = 0; i < 4; i++) {
c = max(c, smoothstep(2.6, 1.9, length(p - uCrackSrc[i].xy) / max(uCrackSrc[i].z, 0.1)) * min(uCrackSrc[i].w, 1.0));
c = max(c, smoothstep(2.6, 1.8, fissDist(i, p, u, v, L)) * min(uFissB[i].y, 1.0));
}
return c;
}
// coverage of a crack d metres away, half-width w, pixel footprint px (a crack narrower than a pixel
// dims instead of aliasing)
float crackCov(float d, float w, float px) { return (1.0 - smoothstep(w * 0.3, w + px, d)) * w / (w + 0.75 * px); }
// F1, F2, F3 to jittered feature points (3x3 cells), and the nearest cell
vec3 vor3(vec2 x, float seed, out vec2 id) {
vec2 c = floor(x), fr = x - c;
vec3 d = vec3(8.0);
id = c;
for (int j = -1; j <= 1; j++) for (int i = -1; i <= 1; i++) {
vec2 g = vec2(float(i), float(j));
float e = length(g + hash33(vec3(c + g, seed)).xy * 0.8 + 0.1 - fr);
if (e < d.x) { d = vec3(e, d.xy); id = c + g; } else if (e < d.y) d = vec3(d.x, e, d.y); else d.z = min(d.z, e);
}
return d;
}
// Desiccation / lava-crust network: ~2.6 m primary plates, each broken into its own secondary cells
// (0.35-1.1 m; a fifth of the plates stay whole), so cell size varies several times over and the
// secondaries end on the primaries; every line tapers into its junctions (F3 - F2 -> 0 there).
// x: primary coverage, y: secondary coverage, z: metres to a primary
vec3 crackNet(vec2 p, float px, float wP, float wS) {
vec2 idP, idS;
vec3 a = vor3(p * ${f(1/2.6)}, 11.0, idP);
vec3 hc = hash33(vec3(idP, 5.0));
float sS = mix(0.35, 1.1, hc.x);
vec3 b = vor3(p / sS, 12.0 + floor(hc.y * 8.0), idS);
float eP = (a.y - a.x) * 1.3, eS = (b.y - b.x) * 0.5 * sS;
float tP = mix(0.2, 1.0, smoothstep(0.0, 0.25, (a.z - a.y) * 2.6)), tS = mix(0.15, 1.0, smoothstep(0.0, 0.3, b.z - b.y));
return vec3(crackCov(eP, wP * tP, px), crackCov(eS, wS * tS, px) * step(0.2, hc.z), eP);
}
#ifdef ARENA_FLOOR
// The rim wall's sun shadow, exact: where the wall meets the floor the shadow map's bias leaks a
// lit sliver a few cm wide, and on the polished floor facing a low sun it glints as a row of
// white dashes (one per shadow texel) along the far wall. A ray towards the sun leaves the arena
// through the wall's inner face (r = 17.98); it is clear only if it is above the coping by then.
float rimWallSun(vec2 p, vec3 s) {
float dd = dot(s.xz, s.xz);
if (dd < 1e-6) return 1.0;
float b = dot(p, s.xz), c = dot(p, p) - ${f((ARENA.radius-0.02)**2)};
float t = (-b + sqrt(max(b * b - dd * c, 0.0))) / dd;
return smoothstep(${f(ARENA.wallHeight-0.04)}, ${f(ARENA.wallHeight+0.04)}, t * s.y);
}
#endif
// running-bond blocks: distance to the nearest joint (uv units), id
float blocks(vec2 uv, vec2 size, float stagger, out vec2 id) {
vec2 q = uv / size;
q.x += floor(q.y) * stagger;
id = floor(q);
vec2 d = (0.5 - abs(fract(q) - 0.5)) * size;
return min(d.x, d.y);
}
float segDist(vec2 p, vec2 a, vec2 b) { vec2 pa = p - a, ba = b - a; return length(pa - ba * clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0)); }
// carved rune in a unit cell: 4 strokes between the nodes of a 3x3 lattice, picked by hash
float rune(vec2 q, float seed) {
float d = 1e3;
for (int k = 0; k < 4; k++) {
vec4 h = hash41(seed * 13.7 + float(k) * 3.1);
vec2 a = floor(h.xy * 2.999) * 0.5, b = floor(h.zw * 2.999) * 0.5;
if (all(equal(a, b))) b = vec2(1.0) - a;
d = min(d, segDist(q, a, b));
}
return d;
}
// split-sum environment BRDF, analytic (Karis 2014, mobile)
vec2 envBRDF(float rough, float nv) {
vec4 r = rough * vec4(-1.0, -0.0275, -0.572, 0.022) + vec4(1.0, 0.0425, 1.04, -0.04);
float a004 = min(r.x * r.x, exp2(-9.28 * nv)) * r.x + r.y;
return vec2(-1.04, 1.04) * a004 + r.zw;
}
`