import*asTHREEfrom'three';// ── АДАПТЕР (наша правка): земля арены заменена рельефом мира Ultra-Water. ──import{ islandHeight as groundHeight }from'../../uw/scene/islandHeight.js';// The shared world every spell, sim and material sees. One file so the GLSL and its JS mirror// stay side by side:// - ARENA: the fixed layout (floor, rim wall, pillars, dummies, pool, caster focus)// - U: uniforms shared by reference across every material ({ ...U, own uniforms })// - the world texture: lights, forces, impacts and capsule occluders packed into one 64x4// RGBA32F DataTexture, uploaded once per frame and read with texelFetch (no uniform-array// limits, no per-material uploads)// - GLSL chunks to read all of the above, and arenaSDF()/arenaRaycast() mirrored in JS//// Conventions: metres, y up, floor at y = 0, linear HDR. Spells never touch the texture;// they hold handles from lights/forces/impacts/occluders and set plain fields.// --- arena layout -------------------------------------------------------------------const pillarAngles =Array.from({length:8}, i
worldData
worldTex worldData
worldTex worldTex
worldTex
placeholder3D
placeholder3D
placeholder2D
placeholder2D
worldTex
placeholder3D
placeholder3D
placeholder2D
placeholder2D
placeholder2D
x
items n i
items
it items x
it
it
it
lights
forces
impactList
impacts
it impactList
x impactList x x it it it x
itpos it radius it type type type
it amount it life it
itcolor
it
x impactList x
occluderList
occluders
occluderList
occluderList
o a b radius kind i occluderListo i occluderListi
occluderListo
o
lightOrder
d worldData
d
lightOrder
l lights l l lightOrderl
lightOrder b b a a
nl lightOrder maxLights
shadowed
i i nl i
l lightOrderi o i
sh l shadowed maxShadowed shadowed
dl l l l l l l l l l l
l l l sh l l l l o
nf
fo forces
fo
o nf
dfo fo fo fo fo fo fo fo fo fo fo fo o
nf
ni
it impactList
it dt
it it
o ni
fade it it
dit it it it it it it fade fade it it it o
ni
no occluderList
i i no i
c occluderListi o i
dc c c c c c c c o
nl nf ni no
worldTex
_pa _ba
_pap a _bab a
h _pa_ba _ba_ba
_pa_ba h r
p p p
d p
c occluderList d d p c c c
d
_rp
t
i i t maxDist i
d _rporigindir t
d t
t d
`
vec3 forceAccel(vec3 p, vec3 v, inout float kill) {
vec3 acc = vec3(0.0);
int n = int(uWorldCounts.y);
for (int i = 0; i < ${MAX_FORCES}; i++) {
if (i >= n) break;
vec4 a = worldTexel(1, i * 4), k = worldTexel(1, i * 4 + 1), b = worldTexel(1, i * 4 + 2);
int type = int(a.w);
vec3 d = p - a.xyz;
if (type == 0) {
float gust = 1.0 + k.y * sin(uTime * 1.7 + dot(p, vec3(0.13, 0.05, 0.11))) * sin(uTime * 0.53 + p.x * 0.07);
acc += (b.xyz * k.x * gust - v) * k.z;
} else if (type == 1) {
vec3 ax = b.xyz;
float h = dot(d, ax);
vec3 radial = d - ax * h;
float r = length(radial);
if (h > -0.5 && h < b.w && r < k.x * 6.0) {
vec3 er = radial / max(r, 1e-3), et = cross(ax, er);
float vt = r < k.x ? k.y / (2.0 * PI) * r / (k.x * k.x) : k.y / (2.0 * PI * r);
float up = k.z * exp(-r * r / (k.x * k.x * 2.0));
vec3 target = et * vt + ax * up - er * k.w * smoothstep(k.x * 6.0, k.x, r) * (1.0 - smoothstep(0.0, 2.0, h) * 0.6);
acc += (target - v) * 3.0 * smoothstep(k.x * 6.0, k.x * 3.0, r);
}
} else if (type == 2) {
float r2 = dot(d, d);
if (r2 < k.w * k.w) {
acc -= normalize(d) * k.x / (r2 + k.y * k.y);
if (r2 < k.z * k.z) kill = 1.0;
}
} else if (type == 3) {
vec3 ax = b.xyz;
float h = dot(d, ax);
vec3 radial = d - ax * h;
float r = length(radial);
if (r > k.y * 0.5 && r < k.z) {
vec3 et = cross(ax, radial / r);
vec3 target = et * sqrt(k.x / r) - ax * h * 4.0;
acc += (target - v) * k.w;
}
if (r < k.y) kill = max(kill, step(abs(h), 0.5));
} else if (type == 4) {
float r = length(d);
float shell = 1.0 - clamp(abs(r - k.x) / max(k.y, 1e-3), 0.0, 1.0);
acc += d / max(r, 1e-3) * k.z * shell;
} else if (type == 5) {
float along = dot(d, b.xyz);
float off = length(d - b.xyz * along);
if (along > -0.5 && along < k.z && off < k.y) acc += (b.xyz * k.x - v) * k.w * (1.0 - off / k.y);
}
}
return acc;
}
`
;
// Spherical "volume decals" for surfaces: weights per impact type at p (amount x falloff x fade).
// A: x scorch/heat, y frost, z wet, w magma glow. B: x acid, y holy glow, z corruption, w electric.
// Each impact also carries a colour (t2.rgb) for the glow types.
export
const
IMPACTS_GLSL
=
/* glsl */
`
void impactWeights(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);
for (int i = 0; i < ${MAX_IMPACTS}; i++) {
if (i >= n) break;
vec4 a = worldTexel(2, i * 4), b = worldTexel(2, i * 4 + 1), c = worldTexel(2, i * 4 + 2);
float w = b.z * smoothstep(1.0, 0.55, length(p - a.xyz) / a.w);
if (w <= 0.0) continue;
int t = int(b.x);
if (t < 4) wa[t] += w; else wb[t - 4] += w;
glowColor += c.rgb * w;
}
}
`
// needs COMMON_GLSL. viewZ positive distance along the view axis.
export
const
FX_OUT_GLSL
=
/* glsl */
`
// full-res scene depth under this half-res fragment (nearest of the 2x2 would be ideal; the
// prepass already made the FX depth the farthest, this is the soft-fade reference)
// farthest depth of this half-res pixel's full-res block, the same value the depth prepass
// wrote: soft fade and volume clipping must agree with the depth test, or smoke gaps open
// along every silhouette when the composite picks the far tap there
float sceneViewZ() {
ivec2 b = ivec2(floor(floor(gl_FragCoord.xy) * uResolution / uFxResolution));
ivec2 m = ivec2(uResolution) - 1;
float d = texelFetch(uSceneDepth, min(b, m), 0).r;
d = max(d, texelFetch(uSceneDepth, min(b + ivec2(1, 0), m), 0).r);
d = max(d, texelFetch(uSceneDepth, min(b + ivec2(0, 1), m), 0).r);
d = max(d, texelFetch(uSceneDepth, min(b + ivec2(1, 1), m), 0).r);
return linearDepth(d);
}
float softFade(float viewZ, float dist) { return clamp((sceneViewZ() - viewZ) / dist, 0.0, 1.0); }
float wboitWeight(float viewZ, float a) {
return a * clamp(10.0 / (1e-5 + pow(viewZ / 10.0, 2.0) + pow(viewZ / 200.0, 6.0)), 0.01, 30.0);
}
// col: lit media colour, a: coverage, emit: additive HDR emission, dist: screen-space UV offset
void fxOut(vec3 col, float a, vec3 emit, vec2 dist, float viewZ) {
a = clamp(a, 0.0, 0.999); // 0.995 let a bright HDR sky print through opaque soot
if (uDebug > 0.5 && uDebug < 1.5) { fxMedia = vec4(0.0, 0.0, 0.0, 0.0); fxWeight = vec4(0.0); fxEmit = vec4(0.04, 0.012, 0.004, 0.0); return; }
float w = wboitWeight(viewZ, a);
fxMedia = vec4(col * a * w, a);
fxWeight = vec4(a * w, dist, a);
fxEmit = vec4(emit, 0.0);
}
`
;
// full-res additive emissive (sparks, motes, bolt cores, sigils) into the HDR target. Pure
// ONE/ONE on rgb whatever alpha the shader writes (three's AdditiveBlending scales by src alpha),
// destination alpha kept (the planar reflection uses alpha as its 'something drawn' mask).
export
const
LINES_BLENDING
=
{
transparent
:
true
,
depthWrite
:
false
,
depthTest
:
true
,
blending
:
THREE
.
CustomBlending
,
blendEquation
:
THREE
.
AddEquation
,
blendSrc
:
THREE
.
OneFactor
,
blendDst
:
THREE
.
OneFactor
,
blendEquationAlpha
:
THREE
.
AddEquation
,
blendSrcAlpha
:
THREE
.
ZeroFactor
,
blendDstAlpha
:
THREE
.
OneFactor
,
}
;
// --- JS side: pools packed into the world texture -------------------------------------
function
makePool
(
n, init
)
{
const
=
Array
.
from
(
{
length
:
}
,
(
_, i
)
=>
(
{
index
:
,
active
:
false
,
...
init
(
)
}
)
)
;
return
{
,
acquire
(
)
{
const
=
.
find
(
(
x
)
=>
!
.
active
)
;
if
(
!
)
return
null
;
Object
.
assign
(
,
init
(
)
,
{
active
:
true
}
)
;
return
;
}
,
}
;
}
// light: pos, range, color (linear), intensity, type 0 point / 1 line / 2 cookie, end (line),