Agent Harness
Публичная копия обвязки агентной разработки: гейты, journeys, скиллы и шаблоны. Без ключей и личных данных.
Публичная копия обвязки агентной разработки: гейты, journeys, скиллы и шаблоны. Без ключей и личных данных.
Принят владельцем вручную — без оценки ИИ. Очки за вклад не начислены. Оценка восстановлена по объёму правки.
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { FullscreenQuad, FULLSCREEN_VERT, rawPass } from '../../engine/fullscreen.js'; |
| +добавлено | import { |
| +добавлено | U, COMMON_GLSL, NOISE_GLSL, WORLD_GLSL, ARENA_GLSL, FORCES_GLSL, LIGHTS_GLSL, |
| +добавлено | FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL, FX_BLENDING, LINES_BLENDING, |
| +добавлено | } from '../../vfx/common.js'; |
| +добавлено | |
| +добавлено | // A small GPU agent engine for swarms (Plague Swarm, Ember Swarm). Not a spell: no default export. |
| +добавлено | // - State: two RGBA32F MRT targets, ping-ponged by one fullscreen draw: |
| +добавлено | // S0 = (pos.xyz, age) S1 = (vel.xyz, state) |
| +добавлено | // state 0 unborn, 1 flying, 2 landed / clinging, 3 falling, 4 resting, 5 spent (never respawns |
| +добавлено | // until reset). A texel is born when its hash < uSpawnFrac, so a spell grows its swarm by |
| +добавлено | // ramping one uniform: no emission records, no CPU per agent. |
| +добавлено | // - Flocking grid (optional): every flying agent splats (vel, 1) into one 32^3 cell of a 2D atlas |
| +добавлено | // (8 x 4 tiles of 32 x 32, RGBA16F, additive points). The sim reads it back trilinearly |
| +добавлено | // (hardware bilinear in a tile, manual lerp across slices): mean velocity = alignment, density |
| +добавлено | // gradients at two scales = separation (pressure) and cohesion. O(N), no sort, no neighbour |
| +добавлено | // lists: particle-in-cell boids. |
| +добавлено | // - Floor light (optional): agents splat their emission x 1/(h^2 + k) into a 64^2 floor texture |
| +добавлено | // that follows the swarm, blurred twice; a floor decal in the lines pass relights the floor |
| +добавлено | // from it (the resolved opaque colour carries the floor's own texture), so thousands of glowing |
| +добавлено | // agents light the floor beneath them without a light each. |
| +добавлено | // The spell supplies the behaviour (swarmSpawn / swarmStep) and the look (a GLSL chunk shared by |
| +добавлено | // the glow, light and body shaders) as strings, compiled once in create(). |
| +добавлено | |
| +добавлено | export const GN = 32; |
| +добавлено | |
| +добавлено | export const SWARM_STATE_GLSL = (W) => /* glsl */` |
| +добавлено | uniform highp sampler2D uS0; |
| +добавлено | uniform highp sampler2D uS1; |
| +добавлено | const int SW = ${W}; |
| +добавлено | ivec2 swarmTexel(int i) { return ivec2(i % SW, i / SW); } |
| +добавлено | vec4 swarmHash(int i) { return hash41(float(i) * 0.6180339 + 0.37); } |
| +добавлено | vec4 swarmHash2(int i) { return hash41(float(i) * 0.4142136 + 11.3); } |
| +добавлено | vec4 swarmHash3(int i) { return hash41(float(i) * 0.2718281 + 5.1); } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | export const GRID_GLSL = /* glsl */` |
| +добавлено | uniform highp sampler2D uGrid; |
| +добавлено | uniform vec4 uGridBox; // min corner xyz, cell size (m) |
| +добавлено | vec4 gridAt(vec3 p) { |
| +добавлено | vec3 c = (p - uGridBox.xyz) / uGridBox.w - 0.5; |
| +добавлено | if (any(lessThan(c, vec3(-0.5))) || any(greaterThan(c, vec3(${GN}.0 - 0.5)))) return vec4(0.0); |
| +добавлено | c = clamp(c, 0.0, ${GN}.0 - 1.0); |
| +добавлено | float z0 = floor(c.z), fz = c.z - z0, z1 = min(z0 + 1.0, ${GN}.0 - 1.0); |
| +добавлено | vec2 t0 = vec2(mod(z0, 8.0), floor(z0 / 8.0)) * ${GN}.0, t1 = vec2(mod(z1, 8.0), floor(z1 / 8.0)) * ${GN}.0; |
| +добавлено | const vec2 inv = 1.0 / vec2(${GN * 8}.0, ${GN * 4}.0); |
| +добавлено | return mix(texture(uGrid, (t0 + c.xy + 0.5) * inv), texture(uGrid, (t1 + c.xy + 0.5) * inv), fz) * 64.0; |
| +добавлено | } |
| +добавлено | float gridN(vec3 p) { return gridAt(p).w; } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | const SIM_FRAG = (W, body, grid) => /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | precision highp int; |
| +добавлено | precision highp sampler2D; |
| +добавлено | ${COMMON_GLSL} |
| +добавлено | ${NOISE_GLSL} |
| +добавлено | ${ARENA_GLSL} |
| +добавлено | ${FORCES_GLSL} |
| +добавлено | ${SWARM_STATE_GLSL(W)} |
| +добавлено | ${grid ? GRID_GLSL : ''} |
| +добавлено | uniform float uSimDt; |
| +добавлено | uniform float uSpawnFrac; |
| +добавлено | layout(location = 0) out vec4 o0; |
| +добавлено | layout(location = 1) out vec4 o1; |
| +добавлено | ${body} |
| +добавлено | void main() { |
| +добавлено | ivec2 px = ivec2(gl_FragCoord.xy); |
| +добавлено | int idx = px.y * SW + px.x; |
| +добавлено | vec4 s0 = texelFetch(uS0, px, 0), s1 = texelFetch(uS1, px, 0); |
| +добавлено | vec3 p = s0.xyz, v = s1.xyz; |
| +добавлено | float age = s0.w, st = s1.w; |
| +добавлено | vec4 h = swarmHash(idx); |
| +добавлено | if (st < 0.5) { |
| +добавлено | if (h.x >= uSpawnFrac) { o0 = vec4(0.0); o1 = vec4(0.0); return; } |
| +добавлено | st = 1.0; age = 0.0; |
| +добавлено | swarmSpawn(idx, h, p, v); |
| +добавлено | } else if (st > 4.5) { o0 = s0; o1 = s1; return; } |
| +добавлено | else { age += uSimDt; swarmStep(idx, h, p, v, st, age, uSimDt); } |
| +добавлено | if (!(dot(p, p) < 1e12) || !(dot(v, v) < 1e12)) { p = vec3(0.0, -100.0, 0.0); v = vec3(0.0); st = 5.0; } |
| +добавлено | o0 = vec4(p, age); |
| +добавлено | o1 = vec4(v, st); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // splat flying agents into the flocking atlas |
| +добавлено | const GRID_VERT = (W) => /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | precision highp int; |
| +добавлено | ${COMMON_GLSL} |
| +добавлено | ${SWARM_STATE_GLSL(W)} |
| +добавлено | uniform vec4 uGridBox; |
| +добавлено | out vec4 vVal; |
| +добавлено | void main() { |
| +добавлено | ivec2 px = swarmTexel(gl_VertexID); |
| +добавлено | vec4 s0 = texelFetch(uS0, px, 0), s1 = texelFetch(uS1, px, 0); |
| +добавлено | gl_PointSize = 1.0; |
| +добавлено | gl_Position = vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | vVal = vec4(0.0); |
| +добавлено | if (s1.w < 0.5 || s1.w > 1.5) return; |
| +добавлено | vec3 c = floor((s0.xyz - uGridBox.xyz) / uGridBox.w); |
| +добавлено | if (any(lessThan(c, vec3(0.0))) || any(greaterThanEqual(c, vec3(${GN}.0)))) return; |
| +добавлено | vec2 t = vec2(mod(c.z, 8.0), floor(c.z / 8.0)) * ${GN}.0 + c.xy + 0.5; |
| +добавлено | gl_Position = vec4(t / vec2(${GN * 8}.0, ${GN * 4}.0) * 2.0 - 1.0, 0.0, 1.0); |
| +добавлено | vVal = vec4(s1.xyz, 1.0) * (1.0 / 64.0); // half floats: a whole swarm in one cell must not overflow |
| +добавлено | }`; |
| +добавлено | const VAL_FRAG = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | in vec4 vVal; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { fragColor = vVal; }`; |
| +добавлено | |
| +добавлено | // splat agent emission into the floor light texture |
| +добавлено | const LIGHT_VERT = (W, look) => /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | precision highp int; |
| +добавлено | ${COMMON_GLSL} |
| +добавлено | ${NOISE_GLSL} |
| +добавлено | ${SWARM_STATE_GLSL(W)} |
| +добавлено | uniform vec4 uLightBox; // min x, min z, size (m), height softening (m^2) |
| +добавлено | ${look} |
| +добавлено | out vec4 vVal; |
| +добавлено | void main() { |
| +добавлено | int idx = gl_VertexID; |
| +добавлено | ivec2 px = swarmTexel(idx); |
| +добавлено | vec4 s0 = texelFetch(uS0, px, 0), s1 = texelFetch(uS1, px, 0); |
| +добавлено | gl_PointSize = 1.0; |
| +добавлено | gl_Position = vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | vVal = vec4(0.0); |
| +добавлено | if (s1.w < 0.5 || s1.w > 4.5) return; |
| +добавлено | vec3 col = swarmLight(idx, swarmHash(idx), s0.xyz, s1.xyz, s1.w, s0.w); |
| +добавлено | vec2 uv = (s0.xz - uLightBox.xy) / uLightBox.z; |
| +добавлено | if (dot(col, col) <= 0.0 || any(lessThan(uv, vec2(0.0))) || any(greaterThan(uv, vec2(1.0)))) return; |
| +добавлено | float hgt = max(s0.y, 0.0); |
| +добавлено | gl_Position = vec4(uv * 2.0 - 1.0, 0.0, 1.0); |
| +добавлено | // irradiance straight below a point source, I cos / d^2 with d = h (softened) |
| +добавлено | vVal = vec4(min(col / (hgt * hgt + uLightBox.w), vec3(64.0)) * (1.0 / 256.0), 0.0); // scaled: no half overflow |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const BLUR_FRAG = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | uniform sampler2D uTex; |
| +добавлено | uniform vec2 uStep; |
| +добавлено | in vec2 vUv; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | vec4 s = vec4(0.0); |
| +добавлено | for (int k = -4; k <= 4; k++) s += texture(uTex, vUv + uStep * float(k)) * exp(-float(k * k) / 8.0); |
| +добавлено | fragColor = s / 5.0133; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- glow: velocity-stretched additive capsules (lines pass), as Particles' glow ----------------- |
| +добавлено | const GLOW_VERT = (W, look, copies) => /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | precision highp int; |
| +добавлено | precision highp sampler2D; |
| +добавлено | uniform mat4 viewMatrix; |
| +добавлено | uniform mat4 projectionMatrix; |
| +добавлено | uniform vec3 cameraPosition; |
| +добавлено | in vec3 position; |
| +добавлено | ${COMMON_GLSL} |
| +добавлено | ${NOISE_GLSL} |
| +добавлено | ${SWARM_STATE_GLSL(W)} |
| +добавлено | ${look} |
| +добавлено | out vec2 vUv; |
| +добавлено | out vec3 vColor; |
| +добавлено | out float vSoft; |
| +добавлено | void main() { |
| +добавлено | int idx = gl_InstanceID / ${copies}, copy = gl_InstanceID - idx * ${copies}; |
| +добавлено | ivec2 px = swarmTexel(idx); |
| +добавлено | vec4 s0 = texelFetch(uS0, px, 0), s1 = texelFetch(uS1, px, 0); |
| +добавлено | gl_Position = vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | vColor = vec3(0.0); vUv = vec2(0.0); vSoft = 0.0; |
| +добавлено | if (s1.w < 0.5 || s1.w > 4.5) return; |
| +добавлено | vec3 head, tail, col; float size; |
| +добавлено | if (!swarmGlow(idx, copy, swarmHash(idx), s0.xyz, s1.xyz, s1.w, s0.w, head, tail, size, col)) return; |
| +добавлено | vec3 headV = (viewMatrix * vec4(head, 1.0)).xyz, tailV = (viewMatrix * vec4(tail, 1.0)).xyz; |
| +добавлено | headV.z += 0.03; tailV.z += 0.03; // in front of the agent's own opaque body |
| +добавлено | float viewZ = max(-headV.z, 0.05); |
| +добавлено | float pxWorld = 2.0 * viewZ / (projectionMatrix[1][1] * uResolution.y); |
| +добавлено | float w = max(size, 1.5 * pxWorld), aa = size / w; |
| +добавлено | vec2 axis = tailV.xy - headV.xy; |
| +добавлено | float len = length(axis); |
| +добавлено | vec2 dir = len > 1e-5 ? axis / len : vec2(1.0, 0.0), perp = vec2(-dir.y, dir.x); |
| +добавлено | vec2 off = dir * (position.x < 0.0 ? -w : len + w) + perp * position.y * w; |
| +добавлено | gl_Position = projectionMatrix * vec4(headV + vec3(off, 0.0), 1.0); |
| +добавлено | vUv = vec2(position.x < 0.0 ? -1.0 : 1.0, position.y); |
| +добавлено | vSoft = len / (len + 2.0 * w); |
| +добавлено | vColor = col * aa * smoothstep(0.25, 0.8, viewZ) * (2.0 * w / (len + 2.0 * w)); |
| +добавлено | }`; |
| +добавлено | const GLOW_FRAG = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | in vec2 vUv; |
| +добавлено | in vec3 vColor; |
| +добавлено | in float vSoft; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | float x = vUv.x * 0.5 + 0.5, ends = 1.0 - vSoft; |
| +добавлено | float dx = x < ends * 0.5 ? (ends * 0.5 - x) / max(ends * 0.5, 1e-3) : x > 1.0 - ends * 0.5 ? (x - 1.0 + ends * 0.5) / max(ends * 0.5, 1e-3) : 0.0; |
| +добавлено | float d2 = dx * dx + vUv.y * vUv.y; |
| +добавлено | fragColor = vec4(vColor * exp(-d2 * 4.0) * mix(1.0, 0.4, x), 0.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- floor decal: relights the floor from the light texture (lines pass, reads the opaque colour) |
| +добавлено | const FLOOR_VERT = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | uniform mat4 modelMatrix; |
| +добавлено | uniform mat4 viewMatrix; |
| +добавлено | uniform mat4 projectionMatrix; |
| +добавлено | in vec3 position; |
| +добавлено | out vec3 vW; |
| +добавлено | void main() { |
| +добавлено | vec4 w = modelMatrix * vec4(position, 1.0); |
| +добавлено | vW = w.xyz; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * w; |
| +добавлено | }`; |
| +добавлено | const FLOOR_FRAG = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | precision highp sampler2D; |
| +добавлено | uniform sampler2D uLightTex; |
| +добавлено | uniform sampler2D uSceneColor; |
| +добавлено | uniform vec2 uResolution; |
| +добавлено | uniform vec4 uLightBox; |
| +добавлено | uniform vec4 uFloor; // gain, base albedo, relight (scene colour share), - |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | in vec3 vW; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | vec2 uv = (vW.xz - uLightBox.xy) / uLightBox.z; |
| +добавлено | vec2 e = smoothstep(0.0, 0.12, uv) * smoothstep(1.0, 0.88, uv); |
| +добавлено | vec3 E = texture(uLightTex, uv).rgb * (256.0 * e.x * e.y * uFloor.x); |
| +добавлено | if (dot(E, E) < 1e-10) discard; |
| +добавлено | // albedo from what the floor already shows: scene / the irradiance the arena put there |
| +добавлено | vec3 sc = texture(uSceneColor, gl_FragCoord.xy / uResolution).rgb; |
| +добавлено | vec3 Eamb = uAmbient * 3.14159 + uSunColor * max(uSunDir.y, 0.0) * 0.5 + 1e-3; |
| +добавлено | vec3 alb = mix(vec3(uFloor.y), clamp(sc / Eamb, 0.0, 0.25), uFloor.z); |
| +добавлено | fragColor = vec4(E * alb, 0.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const quadGeometry = () => { |
| +добавлено | const g = new THREE.InstancedBufferGeometry(); |
| +добавлено | g.setAttribute('position', new THREE.Float32BufferAttribute([-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0], 3)); |
| +добавлено | g.setIndex([0, 1, 2, 0, 2, 3]); |
| +добавлено | return g; |
| +добавлено | }; |
| +добавлено | const pointCloud = (n, material) => { |
| +добавлено | const g = new THREE.BufferGeometry(); |
| +добавлено | g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(n), 1)); |
| +добавлено | g.boundingSphere = new THREE.Sphere(); |
| +добавлено | const m = new THREE.Points(g, material); |
| +добавлено | m.frustumCulled = false; |
| +добавлено | return m; |
| +добавлено | }; |
| +добавлено | const ADD = { blending: THREE.CustomBlending, blendEquation: THREE.AddEquation, blendSrc: THREE.OneFactor, blendDst: THREE.OneFactor, transparent: true, depthTest: false, depthWrite: false }; |
| +добавлено | |
| +добавлено | export class Swarm { |
| +добавлено | // sim: GLSL defining swarmSpawn / swarmStep. look: GLSL defining swarmGlow / swarmLight. |
| +добавлено | constructor(fx, { W, H, sim, look, uniforms, grid = false, light = null }) { |
| +добавлено | const r = fx.renderer; |
| +добавлено | this.fx = fx; this.W = W; this.H = H; this.N = W * H; |
| +добавлено | this.active = false; |
| +добавлено | const rtOpts = { type: THREE.FloatType, format: THREE.RGBAFormat, minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, depthBuffer: false, generateMipmaps: false, count: 2 }; |
| +добавлено | this.rt = [new THREE.WebGLRenderTarget(W, H, rtOpts), new THREE.WebGLRenderTarget(W, H, rtOpts)]; |
| +добавлено | this.cur = 0; |
| +добавлено | this.stateU = { uS0: { value: null }, uS1: { value: null } }; |
| +добавлено | this.u = uniforms; // the spell's, shared by reference with every material here |
| +добавлено | this.simU = { ...U, ...this.stateU, ...uniforms, uSimDt: { value: 1 / 60 }, uSpawnFrac: { value: 0 } }; |
| +добавлено | this.passes = []; |
| +добавлено | |
| +добавлено | if (grid) { |
| +добавлено | this.gridRT = new THREE.WebGLRenderTarget(GN * 8, GN * 4, { type: THREE.HalfFloatType, minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, depthBuffer: false, generateMipmaps: false }); |
| +добавлено | this.gridBox = new THREE.Vector4(0, 0, 0, 0.25); |
| +добавлено | this.simU.uGrid = { value: this.gridRT.texture }; |
| +добавлено | this.simU.uGridBox = { value: this.gridBox }; |
| +добавлено | this.gridPts = pointCloud(this.N, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: GRID_VERT(W), fragmentShader: VAL_FRAG, |
| +добавлено | uniforms: { ...U, ...this.stateU, uGridBox: this.simU.uGridBox }, ...ADD, |
| +добавлено | })); |
| +добавлено | } |
| +добавлено | this.sim = new FullscreenQuad(new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: FULLSCREEN_VERT, fragmentShader: SIM_FRAG(W, sim, grid), |
| +добавлено | uniforms: this.simU, depthTest: false, depthWrite: false, |
| +добавлено | })); |
| +добавлено | this.lookU = { ...U, ...this.stateU, ...uniforms }; |
| +добавлено | |
| +добавлено | if (light) { |
| +добавлено | const res = light.res ?? 64; |
| +добавлено | const lo = { type: THREE.HalfFloatType, minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, depthBuffer: false, generateMipmaps: false, wrapS: THREE.ClampToEdgeWrapping, wrapT: THREE.ClampToEdgeWrapping }; |
| +добавлено | this.lightRT = [new THREE.WebGLRenderTarget(res, res, lo), new THREE.WebGLRenderTarget(res, res, lo)]; |
| +добавлено | this.lightBox = new THREE.Vector4(-8, -8, light.box ?? 16, light.soft ?? 0.35); |
| +добавлено | this.lightU = { uLightBox: { value: this.lightBox } }; |
| +добавлено | this.lightPts = pointCloud(this.N, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: LIGHT_VERT(W, look), fragmentShader: VAL_FRAG, |
| +добавлено | uniforms: { ...this.lookU, ...this.lightU }, ...ADD, |
| +добавлено | })); |
| +добавлено | this.blur = new FullscreenQuad(rawPass(BLUR_FRAG, { uTex: { value: null }, uStep: { value: new THREE.Vector2() } })); |
| +добавлено | this.floorU = { uFloor: { value: new THREE.Vector4(1, 0.045, 0.6, 0) } }; |
| +добавлено | const plane = new THREE.PlaneGeometry(1, 1).rotateX(-Math.PI / 2); |
| +добавлено | this.floor = new THREE.Mesh(plane, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: FLOOR_VERT, fragmentShader: FLOOR_FRAG, |
| +добавлено | uniforms: { ...U, uLightTex: { value: this.lightRT[0].texture }, uLightBox: this.lightU.uLightBox, ...this.floorU }, ...LINES_BLENDING, |
| +добавлено | })); |
| +добавлено | this.floor.frustumCulled = false; |
| +добавлено | this.floor.visible = false; |
| +добавлено | this.floor.renderOrder = -1; |
| +добавлено | } |
| +добавлено | this.glow = null; |
| +добавлено | this.look = look; |
| +добавлено | this.reset(); |
| +добавлено | // compile the off-scene passes now (programs never grow at cast time) |
| +добавлено | this.simU.uSpawnFrac.value = 0; |
| +добавлено | this.run(1 / 60); |
| +добавлено | this.reset(); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // full-res additive glow mesh for the lines scene; copies = quads per agent (swarmGlow's copy) |
| +добавлено | glowMesh(copies = 1) { |
| +добавлено | const m = new THREE.Mesh(quadGeometry(), new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: GLOW_VERT(this.W, this.look, copies), fragmentShader: GLOW_FRAG, |
| +добавлено | uniforms: this.lookU, ...LINES_BLENDING, |
| +добавлено | })); |
| +добавлено | m.geometry.instanceCount = this.N * copies; |
| +добавлено | m.frustumCulled = false; |
| +добавлено | m.renderOrder = 11; |
| +добавлено | m.layers.enable(1); |
| +добавлено | m.visible = false; |
| +добавлено | return (this.glow = m); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // a material over the state for the spell's own meshes |
| +добавлено | material(vertexShader, fragmentShader, extraUniforms, blending) { |
| +добавлено | return new THREE.RawShaderMaterial({ glslVersion: THREE.GLSL3, vertexShader, fragmentShader, uniforms: { ...this.lookU, ...extraUniforms }, ...blending }); |
| +добавлено | } |
| +добавлено | instancedQuad() { const g = quadGeometry(); g.instanceCount = this.N; return g; } |
| +добавлено | |
| +добавлено | bind() { |
| +добавлено | const t = this.rt[this.cur].textures; |
| +добавлено | this.stateU.uS0.value = t[0]; this.stateU.uS1.value = t[1]; |
| +добавлено | } |
| +добавлено | reset() { |
| +добавлено | const r = this.fx.renderer; |
| +добавлено | for (const rt of this.rt) { r.setRenderTarget(rt); r.setClearColor(0x000000, 0); r.clear(true, false, false); } |
| +добавлено | if (this.lightRT) for (const rt of this.lightRT) { r.setRenderTarget(rt); r.clear(true, false, false); } |
| +добавлено | r.setRenderTarget(null); |
| +добавлено | this.cur = 0; |
| +добавлено | this.bind(); |
| +добавлено | } |
| +добавлено | |
| +добавлено | run(dt) { |
| +добавлено | const r = this.fx.renderer, cam = this.fx.camera; |
| +добавлено | this.simU.uSimDt.value = dt; |
| +добавлено | if (this.gridRT) { |
| +добавлено | r.setRenderTarget(this.gridRT); |
| +добавлено | r.setClearColor(0x000000, 0); |
| +добавлено | r.clear(true, false, false); |
| +добавлено | r.render(this.gridPts, cam); |
| +добавлено | } |
| +добавлено | this.bind(); |
| +добавлено | this.cur ^= 1; |
| +добавлено | this.sim.render(r, this.rt[this.cur]); |
| +добавлено | this.bind(); |
| +добавлено | if (this.lightRT) { |
| +добавлено | const [a, b] = this.lightRT, u = this.blur.material.uniforms, res = a.width; |
| +добавлено | r.setRenderTarget(a); |
| +добавлено | r.setClearColor(0x000000, 0); |
| +добавлено | r.clear(true, false, false); |
| +добавлено | r.render(this.lightPts, cam); |
| +добавлено | u.uTex.value = a.texture; u.uStep.value.set(1 / res, 0); this.blur.render(r, b); |
| +добавлено | u.uTex.value = b.texture; u.uStep.value.set(0, 1 / res); this.blur.render(r, a); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // per frame from fx.systems while active |
| +добавлено | update(dt) { |
| +добавлено | if (!this.active || dt <= 0) return; |
| +добавлено | this.run(Math.min(dt, 1 / 30)); |
| +добавлено | } |
| +добавлено | |
| +добавлено | show(on) { |
| +добавлено | if (this.glow) this.glow.visible = on; |
| +добавлено | if (this.floor) { |
| +добавлено | this.floor.visible = on; |
| +добавлено | const B = this.lightBox; |
| +добавлено | this.floor.position.set(B.x + B.z / 2, 0.012, B.y + B.z / 2); |
| +добавлено | this.floor.scale.set(B.z, 1, B.z); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // ================================================================================================ |
| +добавлено | // Plague Swarm |
| +добавлено | // ================================================================================================ |
| +добавлено | |
| +добавлено | // the training dummy's body as an SDF, in its leaning frame (needs uDummyP, uDummyR, ARENA_GLSL) |
| +добавлено | export const SWARM_DUMMY_GLSL = /* glsl */` |
| +добавлено | float sdEllipsoid(vec3 p, vec3 r) { float k0 = length(p / r), k1 = length(p / (r * r)); return k0 * (k0 - 1.0) / max(k1, 1e-5); } |
| +добавлено | // sack, head, arm beam, post |
| +добавлено | float dummySDF(vec3 w) { |
| +добавлено | vec3 p = transpose(uDummyR) * (w - uDummyP.xyz); |
| +добавлено | float d = sdEllipsoid(p - vec3(0.0, 1.02, 0.0), vec3(0.3, 0.43, 0.25)); |
| +добавлено | d = min(d, sdEllipsoid(p - vec3(0.0, 1.62, 0.0), vec3(0.16, 0.176, 0.152))); |
| +добавлено | d = min(d, sdCapsule(p, vec3(-0.58, 1.27, 0.0), vec3(0.58, 1.27, 0.0), 0.045)); |
| +добавлено | d = min(d, sdCapsule(p, vec3(0.0, 0.1, 0.0), vec3(0.0, 0.62, 0.0), 0.06)); |
| +добавлено | return d; |
| +добавлено | } |
| +добавлено | vec3 dummyNormal(vec3 p) { |
| +добавлено | const vec2 e = vec2(0.01, 0.0); |
| +добавлено | return normalize(vec3(dummySDF(p + e.xyy) - dummySDF(p - e.xyy), dummySDF(p + e.yxy) - dummySDF(p - e.yxy), dummySDF(p + e.yyx) - dummySDF(p - e.yyx))); |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | // boids on the grid: alignment with the local mean velocity, pressure-like separation, cohesion up |
| +добавлено | // the wide-scale density gradient where it is sparse (needs uFlock, uMove and the flocking grid) |
| +добавлено | export const SWARM_BOIDS_GLSL = /* glsl */` |
| +добавлено | vec3 boidAccel(vec3 p, vec3 v) { |
| +добавлено | vec4 g = gridAt(p); |
| +добавлено | float n = g.w, rho0 = uMove.w, c = uGridBox.w; |
| +добавлено | vec3 acc = uFlock.x * (g.xyz / max(n, 1e-3) - v) * smoothstep(0.5, 3.0, n); |
| +добавлено | vec3 ex = vec3(c, 0.0, 0.0), ey = vec3(0.0, c, 0.0), ez = vec3(0.0, 0.0, c); |
| +добавлено | vec3 g1 = vec3(gridN(p + ex) - gridN(p - ex), gridN(p + ey) - gridN(p - ey), gridN(p + ez) - gridN(p - ez)) * 0.5 / rho0; |
| +добавлено | acc -= uFlock.y * g1 * smoothstep(0.25 * rho0, 1.2 * rho0, n); |
| +добавлено | vec3 g2 = vec3(gridN(p + 2.5 * ex) - gridN(p - 2.5 * ex), gridN(p + 2.5 * ey) - gridN(p - 2.5 * ey), gridN(p + 2.5 * ez) - gridN(p - 2.5 * ez)) * 0.5 / rho0; |
| +добавлено | acc += uFlock.z * g2 * (1.0 - smoothstep(0.4 * rho0, 1.5 * rho0, n)); |
| +добавлено | return acc; |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | // behaviour: boids on the grid + a goal field (the leader's cloud, an orbit shell around the target, |
| +добавлено | // a landing on the dummy, dispersal), curl turbulence, world avoidance, other spells' forces |
| +добавлено | export const PLAGUE_SIM = /* glsl */` |
| +добавлено | uniform vec4 uLeader; // cloud centre xyz, radius (m) |
| +добавлено | uniform vec4 uLeaderV; // leader velocity xyz, cloud height / width |
| +добавлено | uniform vec4 uTarget; // envelope centre xyz, radius |
| +добавлено | uniform vec4 uMode; // envelope 0..1, disperse 0..1, landing fraction, death fraction |
| +добавлено | uniform vec4 uFlock; // alignment, separation, cohesion, turbulence |
| +добавлено | uniform vec4 uMove; // cruise m/s, orbit m/s, steering gain, rest density (agents / cell) |
| +добавлено | uniform vec4 uSpawnP; // spawn centre xyz, radius |
| +добавлено | uniform vec4 uDummyP; // target dummy base xyz, 1 = landing allowed |
| +добавлено | uniform mat3 uDummyR; // its lean (local -> world) |
| +добавлено | uniform vec4 uClock; // seconds since dispersal began (-1 before), force response, -, - |
| +добавлено | uniform vec4 uPathA; // the river: swoop start xyz, swoop duration (s) |
| +добавлено | uniform vec4 uPathM; // its Bezier control point xyz, cast clock (s) |
| +добавлено | uniform vec4 uPathB; // its end (the target) xyz, swoop start time (s) |
| +добавлено | uniform vec4 uStream; // delay spread along the river (s), river radius (m), -, - |
| +добавлено | ${SWARM_BOIDS_GLSL} |
| +добавлено | // the swoop: slow off the cloud, diving at the end |
| +добавлено | vec3 pathAt(float s) { |
| +добавлено | s = clamp(s, 0.0, 1.0); |
| +добавлено | float k = s * s * (3.0 - 2.0 * s) * 0.6 + s * s * 0.4, i = 1.0 - k; |
| +добавлено | return i * i * uPathA.xyz + 2.0 * i * k * uPathM.xyz + k * k * uPathB.xyz; |
| +добавлено | } |
| +добавлено | |
| +добавлено | ${SWARM_DUMMY_GLSL} |
| +добавлено | |
| +добавлено | void swarmSpawn(int idx, vec4 h, inout vec3 p, inout vec3 v) { |
| +добавлено | vec3 r = hash31(float(idx) * 1.37 + uTime * 13.1); |
| +добавлено | vec3 d = randomOnSphere(r.xy); |
| +добавлено | p = uSpawnP.xyz + d * uSpawnP.w * (0.4 + 0.6 * r.z); |
| +добавлено | // a rising, turning boil out of the orb |
| +добавлено | vec3 tang = normalize(cross(vec3(0.0, 1.0, 0.0), d) + 1e-4); |
| +добавлено | v = (tang * 3.2 + vec3(0.0, 2.2, 0.0) + d * 1.4 + normalize(uLeader.xyz - p) * 1.5) * mix(0.7, 1.3, h.y); |
| +добавлено | } |
| +добавлено | |
| +добавлено | void swarmStep(int idx, vec4 h, inout vec3 p, inout vec3 v, inout float st, inout float age, float dt) { |
| +добавлено | vec4 h2 = swarmHash2(idx), h3 = swarmHash3(idx); |
| +добавлено | if (st > 3.5) { // dead on the floor: lies still, then it's gone |
| +добавлено | v = vec3(0.0); |
| +добавлено | if (age > 2.6) st = 5.0; |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (st > 2.5) { // dying: wings stop, it tumbles down |
| +добавлено | v += (vec3(0.0, -9.81, 0.0) + curl3(p * 0.9 + h.xyz) * 3.0) * dt; |
| +добавлено | v *= exp(-1.4 * dt); |
| +добавлено | p += v * dt; |
| +добавлено | float d = arenaSDF(p); |
| +добавлено | if (d < 0.006) { p += arenaNormal(p) * (0.006 - d); v = vec3(0.0); st = 4.0; age = 0.0; } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | float lift = uMode.y > 0.05 || uDummyP.w < 0.5 ? 1.0 : 0.0; |
| +добавлено | if (st > 1.5) { // landed on the dummy: crawls, sometimes takes off again |
| +добавлено | vec3 n = dummyNormal(p); |
| +добавлено | vec3 w = curl3(p * 1.7 + h.yzw * 9.0 + uTime * 0.12); |
| +добавлено | v = (w - n * dot(w, n)) * 0.09; |
| +добавлено | p += v * dt; |
| +добавлено | p -= n * (dummySDF(p) - 0.008); |
| +добавлено | float hop = hash12(vec2(float(idx), floor(uTime * 3.0))); |
| +добавлено | if (lift > 0.5 || hop < 0.004 || dummySDF(p) > 0.1) { st = 1.0; v = n * (2.0 + 2.0 * h.z) + w * 0.5; } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | // --- flying --- |
| +добавлено | // each insect runs late along the river by its own delay, so the cloud pours out as a stream |
| +добавлено | float Ti = uPathM.w - h2.z * uStream.x; |
| +добавлено | float sPath = (Ti - uPathB.w) / uPathA.w; |
| +добавлено | float env = smoothstep(0.78, 1.12, sPath) * uMode.x, dis = uMode.y; |
| +добавлено | float cruise = uMove.x * mix(0.75, 1.25, h.y); |
| +добавлено | vec3 acc = vec3(0.0); |
| +добавлено | acc += boidAccel(p, v); |
| +добавлено | |
| +добавлено | // goal 1: a slot on a folding, precessing membrane. A murmuration is a sheet of birds: the sheet |
| +добавлено | // turning edge-on (dark, dense) and face-on (thin) is what makes it read |
| +добавлено | float t = uTime, r0 = sqrt(h3.x), th = h3.y * 6.2832; |
| +добавлено | vec3 o = vec3(r0 * cos(th), (h3.z - 0.5) * 0.2, r0 * sin(th)); |
| +добавлено | o.y += 0.42 * sin(2.1 * o.x + 1.3 * t) + 0.3 * sin(1.7 * o.z - 0.9 * t + 1.7) + 0.3 * o.x * o.z * sin(0.7 * t); |
| +добавлено | float ys = 0.5 * t, tilt = 0.55 * sin(0.42 * t) + 0.3; |
| +добавлено | o.xz = mat2(cos(ys), -sin(ys), sin(ys), cos(ys)) * (o.xz * vec2(1.0 + 0.4 * sin(0.6 * t), 1.0 - 0.3 * sin(0.6 * t))); |
| +добавлено | o.xy = mat2(cos(tilt), -sin(tilt), sin(tilt), cos(tilt)) * o.xy; |
| +добавлено | o += 0.22 * curl3(o * 0.3 + vec3(0.13, t * 0.04, 0.71)); |
| +добавлено | vec3 goal = uLeader.xyz + o * uLeader.w, lv = uLeaderV.xyz; |
| +добавлено | if (sPath > 0.0) { // in the river: a slot around the path point, carried at the path's speed |
| +добавлено | float e = 0.02 / uPathA.w; |
| +добавлено | vec3 P = pathAt(sPath); |
| +добавлено | lv = (P - pathAt(sPath - e)) / 0.02; |
| +добавлено | goal = P + randomInSphere(h3.xzw) * uStream.y * (0.6 + 0.4 * sin(sPath * 9.0 + h.z * 6.0)); |
| +добавлено | } |
| +добавлено | vec3 dc = (goal - p) * 2.0; |
| +добавлено | float dl = length(dc); |
| +добавлено | if (dl > cruise * 1.7) dc *= cruise * 1.7 / dl; |
| +добавлено | vec3 dCloud = dc + lv; |
| +добавлено | // goal 2: a lumpy, breathing ball around the target, each insect on its own tilted orbit, a loose |
| +добавлено | // halo of strays outside it |
| +добавлено | vec3 rr = p - uTarget.xyz; |
| +добавлено | vec3 rs = rr / vec3(1.0, 1.3, 1.0); |
| +добавлено | float dist = max(length(rs), 1e-3); |
| +добавлено | vec3 ax = normalize(vec3(h2.x - 0.5, 0.45 + h.w, h3.w - 0.5) * vec3(2.4, 1.0, 2.4)); |
| +добавлено | ax *= h3.z < 0.12 ? -1.0 : 1.0; // a few fly against the stream |
| +добавлено | float lump = noise3(rs / dist * 0.35 + vec3(t * 0.05, 0.0, 0.31)) - 0.5; |
| +добавлено | bool stray = h2.x > 0.84; |
| +добавлено | float shellR = uTarget.w * (1.0 + 0.1 * sin(t * 1.6 + h.x * 0.8)) * (1.0 + 1.6 * lump) |
| +добавлено | * (stray ? mix(1.5, 2.6, h3.x) : mix(0.42, 1.15, pow(h3.x, 0.7))); |
| +добавлено | vec3 tang = normalize(cross(ax, rr) + 1e-4); |
| +добавлено | float vt = uMove.y * mix(0.6, 1.2, h.w) * (stray ? 0.7 : 1.0); |
| +добавлено | vec3 dEnv = tang * vt + (rs / dist) * (shellR - dist) * 4.0; |
| +добавлено | // centripetal feed-forward: hold the circle about the insect's own axis |
| +добавлено | vec3 q = rr - ax * dot(rr, ax); |
| +добавлено | float qr = max(length(q), 0.25); |
| +добавлено | acc -= q / qr * (vt * vt / qr) * env * (1.0 - dis); |
| +добавлено | // darting: sudden small turns, strongest in the ball |
| +добавлено | acc += curl3(p * 2.2 + h.zxy * 13.0 + vec3(t * 1.3)) * uFlock.w * 7.0 * env; |
| +добавлено | // landers make for the dummy's surface |
| +добавлено | bool lander = h2.w < uMode.z && uDummyP.w > 0.5 && dis < 0.05; |
| +добавлено | float dd = 1e3; |
| +добавлено | if (lander) { |
| +добавлено | dd = dummySDF(p); |
| +добавлено | vec3 nn = dummyNormal(p); |
| +добавлено | dEnv = mix(dEnv, -nn * clamp(dd * 4.0, 0.5, cruise) + tang * 0.6, smoothstep(1.4, 0.35, dd)); |
| +добавлено | if (dd < 0.025 && env > 0.9) { st = 2.0; v *= 0.05; return; } |
| +добавлено | } |
| +добавлено | // goal 3: dispersal, up and away from the target |
| +добавлено | vec3 dOut = normalize(rr * vec3(1.0, 0.3, 1.0) + vec3(0.0, 0.7 + h.x, 0.0) + 1e-3) * cruise * 1.5; |
| +добавлено | vec3 desired = mix(mix(dCloud, dEnv, env), dOut, dis); |
| +добавлено | acc += uMove.z * mix(1.0, 2.3, env) * (desired - v); |
| +добавлено | |
| +добавлено | // turbulence: swarm-scale eddies and each insect's own erratic flight |
| +добавлено | acc += curl3(p * 0.06 + vec3(0.0, uTime * 0.025, 0.0)) * uFlock.w * 3.0; |
| +добавлено | acc += curl3(p * 0.85 + h.xyz * 7.0 + vec3(uTime * 0.37)) * uFlock.w * 4.5; |
| +добавлено | // hold the cruise speed |
| +добавлено | float sp = length(v); |
| +добавлено | acc += (v / max(sp, 1e-3)) * (cruise - sp) * 1.5; |
| +добавлено | // the world: floor, rim, pillars, dummies (not the landing target), other spells' forces |
| +добавлено | float kill = 0.0; |
| +добавлено | bool nearTarget = lander && dd < 1.0; |
| +добавлено | float dw = nearTarget ? p.y : arenaSDF(p); |
| +добавлено | if (dw < 0.7) acc += (nearTarget ? vec3(0.0, 1.0, 0.0) : arenaNormal(p)) * (0.7 - dw) * 28.0; |
| +добавлено | acc += forceAccel(p, v, kill) * uClock.y; |
| +добавлено | v += acc * dt; |
| +добавлено | sp = length(v); |
| +добавлено | if (sp > cruise * 2.3) v *= cruise * 2.3 / sp; |
| +добавлено | p += v * dt; |
| +добавлено | float d2 = nearTarget ? p.y - 0.02 : arenaSDF(p) - 0.02; |
| +добавлено | if (d2 < 0.0) { |
| +добавлено | vec3 nn = nearTarget ? vec3(0.0, 1.0, 0.0) : arenaNormal(p); |
| +добавлено | p -= nn * d2; |
| +добавлено | v -= nn * min(dot(v, nn), 0.0) * 1.3; |
| +добавлено | } |
| +добавлено | // some die as the swarm breaks up |
| +добавлено | if (dis > 0.0 && h2.y < uMode.w && uClock.x > h2.z * 1.5) { st = 3.0; age = 0.0; v *= 0.5; } |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // look: the lantern (bioluminescence), shared by the glow, the floor light and the bodies |
| +добавлено | export const PLAGUE_LOOK = /* glsl */` |
| +добавлено | uniform vec4 uLook; // size, glow, fade (flyers), sync 0..1 |
| +добавлено | uniform vec4 uTarget; |
| +добавлено | uniform vec3 uGlowA; // lantern colour (linear) x intensity |
| +добавлено | uniform vec3 uGlowB; // second hue |
| +добавлено | // 0..1: a firefly flash (40 ms rise, ~150 ms decay) over a dim glow, phase-locked by uLook.w |
| +добавлено | // into waves travelling through the swarm |
| +добавлено | float lantern(int idx, vec3 p, float st, float age) { |
| +добавлено | vec4 h3 = hash41(float(idx) * 0.2718281 + 5.1); |
| +добавлено | float ph = fract(h3.y + uTime * mix(0.55, 1.15, h3.x)); |
| +добавлено | float wave = fract(dot(p - uTarget.xyz, vec3(0.31, 0.62, 0.17)) * 0.55 - uTime * 0.8 + h3.w * 0.08); |
| +добавлено | ph = mix(ph, wave, uLook.w * step(0.25, h3.z)); |
| +добавлено | float flash = smoothstep(0.0, 0.035, ph) * exp(-max(ph - 0.035, 0.0) * 13.0); |
| +добавлено | float l = 0.018 + flash; |
| +добавлено | if (st > 3.5) l *= 0.5 * exp(-age * 1.6); // dead on the floor: the light goes out |
| +добавлено | else if (st > 2.5) l *= exp(-age * 2.2); // dying |
| +добавлено | else l *= uLook.z; |
| +добавлено | return l; |
| +добавлено | } |
| +добавлено | vec3 lanternColor(vec4 h) { return mix(uGlowA, uGlowB, h.w * h.w); } |
| +добавлено | bool swarmGlow(int idx, int copy, vec4 h, vec3 p, vec3 v, float st, float age, out vec3 head, out vec3 tail, out float size, out vec3 col) { |
| +добавлено | float l = lantern(idx, p, st, age); |
| +добавлено | float sp = length(v); |
| +добавлено | vec3 fwd = sp > 0.02 ? v / sp : vec3(0.0, 1.0, 0.0); |
| +добавлено | float L = 0.04 * uLook.x * mix(0.75, 1.25, h.z); |
| +добавлено | head = p - fwd * L * 0.3; |
| +добавлено | tail = head - v * (1.0 / 120.0) - fwd * L * 0.12; |
| +добавлено | size = L * (0.2 + 0.12 * l); |
| +добавлено | col = lanternColor(h) * l * uLook.y; |
| +добавлено | return l > 1e-3; |
| +добавлено | } |
| +добавлено | vec3 swarmLight(int idx, vec4 h, vec3 p, vec3 v, float st, float age) { |
| +добавлено | return lanternColor(h) * lantern(idx, p, st, age) * uLook.y; |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | // bodies: full-res opaque pass with alpha-to-coverage (MSAA): crisp dark chitin silhouettes that |
| +добавлено | // depth-sort with everything, motion-blurred into streaks (coverage = blur), with blurred wing |
| +добавлено | // lobes that transmit the low sun (backlit swarms glow amber at their rims) and glint now and then |
| +добавлено | export const PLAGUE_BODY_VERT = (W) => /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | precision highp int; |
| +добавлено | precision highp sampler2D; |
| +добавлено | uniform mat4 viewMatrix; |
| +добавлено | uniform mat4 projectionMatrix; |
| +добавлено | uniform vec3 cameraPosition; |
| +добавлено | in vec3 position; |
| +добавлено | ${COMMON_GLSL} |
| +добавлено | ${NOISE_GLSL} |
| +добавлено | ${WORLD_GLSL} |
| +добавлено | ${LIGHTS_GLSL} |
| +добавлено | ${SWARM_STATE_GLSL(W)} |
| +добавлено | ${PLAGUE_LOOK} |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | uniform vec3 uChitin; |
| +добавлено | uniform vec4 uBody; // body alpha, wing alpha, glint, wing transmission |
| +добавлено | out vec2 vUv; |
| +добавлено | out vec3 vBody; |
| +добавлено | out vec3 vWing; |
| +добавлено | out vec4 vK; // body alpha, wing alpha, stretched share, - |
| +добавлено | void main() { |
| +добавлено | int idx = gl_InstanceID; |
| +добавлено | ivec2 px = swarmTexel(idx); |
| +добавлено | vec4 s0 = texelFetch(uS0, px, 0), s1 = texelFetch(uS1, px, 0); |
| +добавлено | gl_Position = vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | vK = vec4(0.0); vUv = vec2(0.0); vBody = vWing = vec3(0.0); |
| +добавлено | float st = s1.w; |
| +добавлено | if (st < 0.5 || st > 4.5) return; |
| +добавлено | vec4 h = swarmHash(idx); |
| +добавлено | vec3 p = s0.xyz, v = s1.xyz; |
| +добавлено | float sp = length(v); |
| +добавлено | vec3 fwd = sp > 0.02 ? v / sp : normalize(vec3(cos(h.y * 6.283), 0.0, sin(h.y * 6.283))); |
| +добавлено | float L = 0.04 * uLook.x * mix(0.75, 1.25, h.z); |
| +добавлено | float fade = st > 3.5 ? 1.0 - smoothstep(1.4, 2.5, s0.w) : st > 2.5 ? 1.0 : uLook.z; |
| +добавлено | if (fade <= 0.0) return; |
| +добавлено | vec3 blur = v * (1.0 / 120.0); // 180-degree shutter at 60 Hz |
| +добавлено | vec3 head = p + fwd * L * 0.5, tail = p - fwd * L * 0.5 - blur; |
| +добавлено | vec3 headV = (viewMatrix * vec4(head, 1.0)).xyz, tailV = (viewMatrix * vec4(tail, 1.0)).xyz; |
| +добавлено | float viewZ = max(-headV.z, 0.05); |
| +добавлено | if (viewZ < 0.15) return; |
| +добавлено | float pxW = 2.0 * viewZ / (projectionMatrix[1][1] * uResolution.y); |
| +добавлено | bool wings = st < 1.5; |
| +добавлено | float span = L * (wings ? 0.6 : 0.22); // half-width: the wing blur, or a folded body |
| +добавлено | float w = max(span, 0.8 * pxW), aa = span / w; |
| +добавлено | vec2 axis = tailV.xy - headV.xy; |
| +добавлено | float len = length(axis); |
| +добавлено | vec2 dir = len > 1e-5 ? axis / len : vec2(1.0, 0.0), perp = vec2(-dir.y, dir.x); |
| +добавлено | vec2 off = dir * (position.x < 0.0 ? -w : len + w) + perp * position.y * w; |
| +добавлено | gl_Position = projectionMatrix * vec4(headV + vec3(off, 0.0), 1.0); |
| +добавлено | vUv = vec2(position.x < 0.0 ? 0.0 : 1.0, position.y); |
| +добавлено | // motion blur spreads the same coverage along the streak |
| +добавлено | float streak = min(1.0, (L + 2.0 * w) / (len + 2.0 * w) * 1.2); |
| +добавлено | vK.x = uBody.x * fade * aa * streak; |
| +добавлено | vK.y = wings ? uBody.y * fade * aa * streak : 0.0; |
| +добавлено | vK.z = len / (len + 2.0 * w); |
| +добавлено | // lighting, isotropic (they're tiny): sky, a little sun, every spell light (the swarm's own glow) |
| +добавлено | gLightNoShadow = true; |
| +добавлено | vec3 V = normalize(cameraPosition - p); |
| +добавлено | vec3 spell = spellLightDiffuse(p, vec3(0.0)); |
| +добавлено | vBody = uChitin * mix(0.7, 1.3, h.w) * (uAmbient * 1.6 + uSunColor * 0.3 + spell * 0.5); |
| +добавлено | // wings: thin membranes, mostly transmission of the low sun (forward-peaked), sky and spell light |
| +добавлено | float flap = sin(fract(h.x * 17.0 + uTime * mix(14.0, 19.0, h.y)) * 6.283) * 1.1; |
| +добавлено | vec3 side = normalize(cross(fwd, vec3(0.0, 1.0, 0.0)) + vec3(1e-4, 0.0, 0.0)); |
| +добавлено | vec3 wn = vec3(0.0, cos(flap), 0.0) + side * sin(flap); |
| +добавлено | float glint = wings ? pow(abs(dot(wn, normalize(uSunDir + V))), 90.0) * uBody.z : 0.0; |
| +добавлено | vWing = vec3(0.4, 0.34, 0.22) * (uSunColor * hgPhase(dot(uSunDir, -V), 0.75) * 12.566 * uBody.w + uAmbient * 0.2 + spell * 0.15) |
| +добавлено | + uSunColor * glint; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const PLAGUE_BODY_FRAG = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | in vec2 vUv; |
| +добавлено | in vec3 vBody; |
| +добавлено | in vec3 vWing; |
| +добавлено | in vec4 vK; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | // x: 0 head cap .. 1 tail cap; y: across, in wing half-widths |
| +добавлено | float x = vUv.x, ends = 1.0 - vK.z; |
| +добавлено | float dx = x < ends * 0.5 ? (ends * 0.5 - x) / max(ends * 0.5, 1e-3) : x > 1.0 - ends * 0.5 ? (x - 1.0 + ends * 0.5) / max(ends * 0.5, 1e-3) : 0.0; |
| +добавлено | float y = abs(vUv.y); |
| +добавлено | // a slim body down the middle, blurred wing lobes either side of the thorax |
| +добавлено | float body = exp(-(dx * dx * 2.0 + y * y * 22.0)) * vK.x; |
| +добавлено | float wing = exp(-(dx * dx * 2.5 + pow(max(y - 0.4, 0.0), 2.0) * 10.0)) * smoothstep(0.1, 0.35, y) * (1.0 - x * 0.5) * vK.y; |
| +добавлено | float a = clamp(body + wing, 0.0, 1.0); |
| +добавлено | if (a < 0.04) discard; |
| +добавлено | fragColor = vec4((vBody * body + vWing * wing) / a, a); |
| +добавлено | }`; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { Swarm, PLAGUE_SIM, PLAGUE_LOOK, PLAGUE_BODY_VERT, PLAGUE_BODY_FRAG } from './plague-swarm.glsl.js'; |
| +добавлено | |
| +добавлено | // Plague Swarm (Acid hero). Thousands of bioluminescent insects, flocking for real on the GPU |
| +добавлено | // (plague-swarm.glsl.js: particle-in-cell boids, alignment / separation / cohesion from a 32^3 |
| +добавлено | // velocity-density grid, plus a goal field and curl turbulence). Reference: desert-locust swarm |
| +добавлено | // and starling-murmuration footage, Photinus fireflies (whose flashes lock into travelling waves). |
| +добавлено | // Anticipation: the swarm boils out of the caster's orb and churns into a murmuration overhead. |
| +добавлено | // Release: it pours off the cloud in a river that swoops over the arena onto the target. |
| +добавлено | // Impact: it wraps the dummy in a spinning ball, a third of it lands and crawls on the dummy, the |
| +добавлено | // lanterns sync into flash waves, and the dummy corrodes: dissolve, acid paint pooling under it, |
| +добавлено | // sizzle, drips and fumes, two orbiting green lights throwing its shadow round the floor. |
| +добавлено | // Aftermath: the swarm lifts off and scatters over the rim; a share die and drop, their lights |
| +добавлено | // going out on the floor; an eaten dummy over a corroded stain. |
| +добавлено | // Every insect is a motion-blurred chitin streak with blurred wings that glint in the sun (fx pass) |
| +добавлено | // and a lantern (lines pass); their light also lands on the floor through the swarm's floor light. |
| +добавлено | |
| +добавлено | const SIZES = { low: [64, 64], medium: [128, 64], high: [128, 128], ultra: [256, 128] }; |
| +добавлено | const CHARGE = 2.0, IMPACT = 1.8, SWOOP = 1.2, DELAY = 0.85; |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _c = new THREE.Vector3(), _d = new THREE.Vector3(); |
| +добавлено | const _m4 = new THREE.Matrix4(); |
| +добавлено | const ease = (x) => (x <= 0 ? 0 : x >= 1 ? 1 : x * x * (3 - 2 * x)); |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'plague-swarm', name: 'Plague Swarm', element: 'acid', order: 10, hue: '#8aff3c', |
| +добавлено | shape: 'area', |
| +добавлено | params: { |
| +добавлено | count: 1, size: 1, speed: 1, glow: 1, sync: 0.6, flock: 1, turbulence: 1, radius: 1.15, |
| +добавлено | landing: 0.32, hold: 3, corrosion: 1, |
| +добавлено | color: '#a8ff2a', color2: '#f0ff58', chitin: '#262619', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | count: [0.1, 1, 0.01], size: [0.5, 2.5, 0.01], speed: [0.4, 2, 0.01], glow: [0, 4, 0.01], sync: [0, 1, 0.01], |
| +добавлено | flock: [0, 2, 0.01], turbulence: [0, 3, 0.01], radius: [0.7, 2.2, 0.01], landing: [0, 0.8, 0.01], |
| +добавлено | hold: [1, 6, 0.1], corrosion: [0, 1.5, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | locusts: { glow: 0.3, size: 1.35, speed: 1.25, sync: 0, landing: 0.5, chitin: '#3d3420', color: '#c8ff3a', turbulence: 1.2 }, |
| +добавлено | fireflies: { glow: 2.4, size: 0.7, count: 0.5, sync: 0.9, speed: 0.6, turbulence: 0.6, landing: 0.15, color: '#d6ff4a', color2: '#ffd34a', chitin: '#121210' }, |
| +добавлено | blight: { color: '#6dff3a', color2: '#3affb4', glow: 1.5, corrosion: 1.45, hold: 4.5, radius: 1.35 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: CHARGE, impact: IMPACT, linger: 8 }, |
| +добавлено | kinds: {}, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles, Q = fx.Q; |
| +добавлено | const [W, H] = SIZES[Q.tier] ?? SIZES.high; |
| +добавлено | |
| +добавлено | // --- the swarm --------------------------------------------------------------------------------- |
| +добавлено | const u = { |
| +добавлено | uLeader: { value: new THREE.Vector4() }, uLeaderV: { value: new THREE.Vector4(0, 0, 0, 1) }, |
| +добавлено | uTarget: { value: new THREE.Vector4(0, 1, 2, 1) }, uMode: { value: new THREE.Vector4() }, |
| +добавлено | uFlock: { value: new THREE.Vector4() }, uMove: { value: new THREE.Vector4() }, |
| +добавлено | uSpawnP: { value: new THREE.Vector4() }, uDummyP: { value: new THREE.Vector4() }, |
| +добавлено | uDummyR: { value: new THREE.Matrix3() }, uClock: { value: new THREE.Vector4(-1, 0.15, 0, 0) }, |
| +добавлено | uPathA: { value: new THREE.Vector4() }, uPathM: { value: new THREE.Vector4() }, uPathB: { value: new THREE.Vector4() }, |
| +добавлено | uStream: { value: new THREE.Vector4(DELAY, 0.7, 0, 0) }, |
| +добавлено | uLook: { value: new THREE.Vector4(1, 1, 1, 0) }, uGlowA: { value: new THREE.Color() }, uGlowB: { value: new THREE.Color() }, |
| +добавлено | uChitin: { value: new THREE.Color() }, uBody: { value: new THREE.Vector4(1, 0.3, 4, 0.12) }, |
| +добавлено | }; |
| +добавлено | const swarm = new Swarm(fx, { W, H, sim: PLAGUE_SIM, look: PLAGUE_LOOK, uniforms: u, grid: true, light: { res: 64, box: 16, soft: 0.3 } }); |
| +добавлено | swarm.floorU.uFloor.value.set(0.012, 0.045, 0.6, 0); |
| +добавлено | fx.systems.push(swarm); |
| +добавлено | const glow = swarm.glowMesh(); |
| +добавлено | fx.scenes.lines.add(glow, swarm.floor); |
| +добавлено | // bodies in the opaque pass: full res, depth-tested, motion blur as MSAA coverage |
| +добавлено | const body = new THREE.Mesh(swarm.instancedQuad(), swarm.material(PLAGUE_BODY_VERT(W), PLAGUE_BODY_FRAG, {}, { alphaToCoverage: true })); |
| +добавлено | body.frustumCulled = false; |
| +добавлено | body.visible = false; |
| +добавлено | fx.scenes.opaque.add(body); |
| +добавлено | |
| +добавлено | // --- the acid family's kinds by full name ------------------------------------------------------ |
| +добавлено | const sizzle = P.emitter('acid.sizzle', { shape: 'line', radius: 0.26, spread: 0.6 }); |
| +добавлено | const drip = P.emitter('acid.drip', { shape: 'disk', radius: 0.26, spread: 0.15 }); |
| +добавлено | const fume = P.emitter('acid.fume', { shape: 'line', radius: 0.22, spread: 0.3 }); |
| +добавлено | const stain = P.emitter('acid.sizzle', { shape: 'disk', radius: 1, spread: 0.5 }); |
| +добавлено | |
| +добавлено | // colours (linear) |
| +добавлено | const glowA = new THREE.Color(), glowB = new THREE.Color(); |
| +добавлено | function syncColors() { |
| +добавлено | const p = S.params; |
| +добавлено | glowA.set(p.color); glowB.set(p.color2); |
| +добавлено | u.uGlowA.value.copy(glowA).multiplyScalar(18); |
| +добавлено | u.uGlowB.value.copy(glowB).multiplyScalar(18); |
| +добавлено | u.uChitin.value.set(p.chitin); |
| +добавлено | } |
| +добавлено | |
| +добавлено | let owner = null; |
| +добавлено | const lead = new THREE.Vector3(), leadPrev = new THREE.Vector3(), centroid = new THREE.Vector3(); |
| +добавлено | const cloud0 = new THREE.Vector3(), mid = new THREE.Vector3(), tc = new THREE.Vector3(); |
| +добавлено | |
| +добавлено | function releaseAll(c) { |
| +добавлено | const d = c.data; |
| +добавлено | d.l0?.release(); d.l1?.release(); d.vortex?.release(); |
| +добавлено | d.l0 = d.l1 = d.vortex = null; |
| +добавлено | } |
| +добавлено | function stop() { |
| +добавлено | owner = null; |
| +добавлено | swarm.active = false; |
| +добавлено | swarm.show(false); |
| +добавлено | body.visible = false; |
| +добавлено | swarm.reset(); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the cloud's centre (CPU): up out of the orb, churning over the caster until the swoop starts |
| +добавлено | function cloudAt(c, T, out) { |
| +добавлено | const t = Math.min(T, CHARGE), k = ease(t / 1.3); |
| +добавлено | out.copy(c.from).lerp(cloud0, k); |
| +добавлено | out.x += Math.sin(t * 1.3 + c.data.ph) * 0.6 * k; |
| +добавлено | out.y += Math.sin(t * 0.9) * 0.3 * k; |
| +добавлено | return out; |
| +добавлено | } |
| +добавлено | // the river (mirrors pathAt in the sim): a quadratic Bezier from the cloud over to the target |
| +добавлено | const pA = new THREE.Vector3(); |
| +добавлено | function pathAt(s, out) { |
| +добавлено | s = Math.min(1, Math.max(0, s)); |
| +добавлено | const k = s * s * (3 - 2 * s) * 0.6 + s * s * 0.4, i = 1 - k; |
| +добавлено | return out.set(i * i * pA.x + 2 * i * k * mid.x + k * k * tc.x, i * i * pA.y + 2 * i * k * mid.y + k * k * tc.y, i * i * pA.z + 2 * i * k * mid.z + k * k * tc.z); |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'color' || k === 'color2' || k === 'chitin') syncColors(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = c.data; |
| +добавлено | if (name === 'charge') { |
| +добавлено | syncColors(); |
| +добавлено | if (owner && owner !== c) releaseAll(owner); |
| +добавлено | if (!swarm.active) swarm.reset(); |
| +добавлено | owner = c; |
| +добавлено | swarm.active = true; |
| +добавлено | swarm.show(true); |
| +добавлено | body.visible = true; |
| +добавлено | d.ph = (c.seed % 100) * 0.0628; |
| +добавлено | d.side = c.seed % 2 ? 1 : -1; |
| +добавлено | d.hold = p.hold; |
| +добавлено | c.durations.linger = p.hold + 5; |
| +добавлено | d.nextSplat = 0; d.nextHit = 0; d.arrived = false; d.dispersed = false; |
| +добавлено | d.l0 = fx.lights.acquire(); |
| +добавлено | if (d.l0) { d.l0.range = 7; d.l0.intensity = 0; d.l0.shadow = true; d.l0.color.copy(glowA); } |
| +добавлено | d.l1 = null; d.vortex = null; |
| +добавлено | // the cloud forms over and in front of the caster, then the dive bends out to one side |
| +добавлено | cloud0.copy(c.from).add(_a.set(0, 2.6, -2.4)); |
| +добавлено | tc.set(c.to.x, c.target >= 0 ? 1.0 : Math.max(c.to.y, 0.9), c.to.z); |
| +добавлено | mid.copy(cloud0).lerp(tc, 0.5).add(_a.set(d.side * 5.5, 1.4, 0)); |
| +добавлено | cloudAt(c, 0, lead); leadPrev.copy(lead); centroid.copy(lead); |
| +добавлено | cloudAt(c, CHARGE, pA); |
| +добавлено | swarm.simU.uSpawnFrac.value = 0; |
| +добавлено | } else if (name === 'done') { |
| +добавлено | releaseAll(c); |
| +добавлено | if (c === owner) stop(); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | if (c !== owner) { releaseAll(c); return; } |
| +добавлено | const p = S.params, d = c.data, T = c.age; |
| +добавлено | const swoop = SWOOP / p.speed ** 0.5, tEnv = CHARGE + swoop, tDis = CHARGE + IMPACT + d.hold; |
| +добавлено | const dm = c.target >= 0 ? fx.dummies[c.target] : null; |
| +добавлено | |
| +добавлено | // --- leader, cloud, phase weights --- |
| +добавлено | leadPrev.copy(lead); |
| +добавлено | cloudAt(c, T, lead); |
| +добавлено | const lv = _b.subVectors(lead, leadPrev).multiplyScalar(dt > 0 ? 1 / dt : 0); |
| +добавлено | // env: the share of the swarm that has reached the target (each insect has its own delay) |
| +добавлено | const env = ease((T - (tEnv - 0.2)) / (DELAY + 0.4)); |
| +добавлено | const dis = ease((T - tDis) / 0.6); |
| +добавлено | const cloudR = 0.6 + 2.2 * ease(T / 1.6); |
| +добавлено | u.uLeader.value.set(lead.x, lead.y, lead.z, cloudR * Math.cbrt(p.count)); |
| +добавлено | u.uLeaderV.value.set(lv.x, lv.y, lv.z, 1); |
| +добавлено | u.uPathA.value.set(pA.x, pA.y, pA.z, swoop); |
| +добавлено | u.uPathM.value.set(mid.x, mid.y, mid.z, T); |
| +добавлено | u.uPathB.value.set(tc.x, tc.y, tc.z, CHARGE); |
| +добавлено | u.uStream.value.set(DELAY, 0.75 * Math.cbrt(p.count), 0, 0); |
| +добавлено | const R = p.radius * (0.8 + 0.2 * Math.cbrt(p.count)); |
| +добавлено | u.uTarget.value.set(tc.x, tc.y, tc.z, R); |
| +добавлено | const landing = ease((T - tEnv) / 1.2) * p.landing; |
| +добавлено | u.uMode.value.set(T > CHARGE ? 1 : 0, dis, dm ? landing : 0, 0.32); |
| +добавлено | u.uFlock.value.set(1.6 * p.flock, 7 * p.flock, 2.2 * p.flock, p.turbulence); |
| +добавлено | u.uMove.value.set(5 * p.speed, 3.4 * p.speed, 2.6, 14); |
| +добавлено | u.uSpawnP.value.set(c.from.x, c.from.y, c.from.z, 0.22); |
| +добавлено | u.uClock.value.x = T > tDis ? T - tDis : -1; |
| +добавлено | const fadeFly = 1 - ease((T - tDis - 1.2) / 2.2); |
| +добавлено | u.uLook.value.set(p.size, p.glow, fadeFly, p.sync * ease((T - tEnv) / 1.5) * (1 - dis)); |
| +добавлено | swarm.simU.uSpawnFrac.value = p.count * ease(T / 1.1); |
| +добавлено | if (dm) { |
| +добавлено | u.uDummyP.value.set(dm.pos.x, 0, dm.pos.z, dis > 0.05 ? 0 : 1); |
| +добавлено | u.uDummyR.value.setFromMatrix4(_m4.makeRotationFromQuaternion(dm.group.quaternion)); |
| +добавлено | } else u.uDummyP.value.w = 0; |
| +добавлено | |
| +добавлено | // where the swarm is (lags the leader): the grid and the floor light follow it |
| +добавлено | // the middle of the river while it flows |
| +добавлено | const sMid = (T - DELAY * 0.5 - CHARGE) / swoop; |
| +добавлено | if (T < CHARGE) _d.copy(lead); else if (sMid < 1) pathAt(sMid, _d); else _d.copy(tc); |
| +добавлено | centroid.lerp(_d, 1 - Math.exp(-dt * 4)); |
| +добавлено | const gs = T < CHARGE ? Math.min(14, 5 + cloudR * 3.2) : 16 - 9.5 * env; |
| +добавлено | const gb = swarm.gridBox, cell = gs / 32; |
| +добавлено | gb.set(centroid.x - gs / 2, Math.max(centroid.y - gs * 0.42, -0.3), centroid.z - gs / 2, cell); |
| +добавлено | const lb = swarm.lightBox, snap = lb.z / 64; |
| +добавлено | lb.x = Math.round((centroid.x - lb.z / 2) / snap) * snap; |
| +добавлено | lb.y = Math.round((centroid.z - lb.z / 2) / snap) * snap; |
| +добавлено | swarm.show(true); |
| +добавлено | |
| +добавлено | // --- light: the swarm's glow, then two lights orbiting the dummy with the ball --- |
| +добавлено | const alive = Math.min(1, swarm.simU.uSpawnFrac.value / Math.max(p.count, 1e-3)) * fadeFly; |
| +добавлено | const base = 9 * p.glow * Math.sqrt(p.count) * alive; |
| +добавлено | if (d.l0) { |
| +добавлено | const L = d.l0; |
| +добавлено | if (env < 0.5) { L.pos.copy(centroid); L.range = 6 + cloudR * 1.5; L.intensity = base * 1.25; } |
| +добавлено | else { |
| +добавлено | const a = T * 2.1 + d.ph; |
| +добавлено | L.pos.set(tc.x + Math.cos(a) * R * 1.1, tc.y + 0.6 + 0.25 * Math.sin(T * 1.3), tc.z + Math.sin(a) * R * 1.1); |
| +добавлено | L.range = 7; L.intensity = base * 0.6; |
| +добавлено | } |
| +добавлено | L.color.copy(glowA); |
| +добавлено | } |
| +добавлено | if (env > 0.5 && !d.l1 && !d.dispersed) { |
| +добавлено | d.l1 = fx.lights.acquire(); |
| +добавлено | if (d.l1) { d.l1.range = 7; d.l1.shadow = true; d.l1.color.copy(glowB).lerp(glowA, 0.5); } |
| +добавлено | } |
| +добавлено | if (d.l1) { |
| +добавлено | const a = T * 2.1 + d.ph + Math.PI; |
| +добавлено | d.l1.pos.set(tc.x + Math.cos(a) * R * 1.1, tc.y + 0.35, tc.z + Math.sin(a) * R * 1.1); |
| +добавлено | d.l1.intensity = base * 0.6 * env; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- arrival: the ball wraps the target, the corrosion starts --- |
| +добавлено | if (!d.arrived && T > tEnv - 0.1) { |
| +добавлено | d.arrived = true; |
| +добавлено | fx.rig.shake(0.12, 24); |
| +добавлено | fx.impacts.add('acid', _a.copy(tc), 1.3 * p.radius, 0.9 * p.corrosion, p.hold + 4, glowA); |
| +добавлено | d.vortex = fx.forces.acquire(); |
| +добавлено | if (d.vortex) { |
| +добавлено | const v = d.vortex; |
| +добавлено | v.type = 1; v.pos.set(tc.x, 0, tc.z); v.dir.set(0, 1, 0); v.w = 3.2; |
| +добавлено | v.p[0] = 0.9 * p.radius; v.p[1] = 6 * p.speed; v.p[2] = 0.7; v.p[3] = 0.2; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | const eating = env > 0.6 && dis < 0.5; |
| +добавлено | if (eating) { |
| +добавлено | const k = Math.min(1, (T - tEnv) / d.hold); |
| +добавлено | if (dm) { |
| +добавлено | dm.status.dissolve = Math.max(dm.status.dissolve, Math.min(0.92, p.corrosion * (0.12 + 0.62 * k))); |
| +добавлено | d.nextHit -= dt; |
| +добавлено | if (d.nextHit <= 0) { |
| +добавлено | d.nextHit = 0.18 + 0.25 * fx.rand(); |
| +добавлено | const a = fx.rand() * 6.283; |
| +добавлено | dm.hit(_a.set(Math.cos(a), 0, Math.sin(a)), 0.035 + 0.03 * fx.rand()); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // corrosion pools and spreads under it |
| +добавлено | d.nextSplat -= dt; |
| +добавлено | if (d.nextSplat <= 0) { |
| +добавлено | d.nextSplat = 0.3; |
| +добавлено | fx.ground.splat({ x: tc.x, z: tc.z, radius: (0.55 + 0.9 * k) * p.radius, acid: 0.16 * p.corrosion, pattern: 'noise', seed: (c.seed + T * 31) % 97 }); |
| +добавлено | } |
| +добавлено | if (dm) { |
| +добавлено | const hgt = 1.45 + 0.1 * Math.sin(T * 3); |
| +добавлено | sizzle.pos.set(dm.pos.x, 0.65, dm.pos.z); sizzle.p.set(dm.pos.x, hgt, dm.pos.z); sizzle.rate(170 * p.corrosion * k + 30, dt); |
| +добавлено | fume.pos.set(dm.pos.x, 0.7, dm.pos.z); fume.p.set(dm.pos.x, 1.5, dm.pos.z); fume.rate(26 * p.corrosion, dt); |
| +добавлено | drip.pos.set(dm.pos.x, 0.72, dm.pos.z); drip.vel.set(0, -0.3, 0); drip.rate(34 * p.corrosion * k, dt); |
| +добавлено | } |
| +добавлено | stain.pos.set(tc.x, 0.03, tc.z); stain.radius = 0.9 * p.radius; stain.rate(40 * p.corrosion * k, dt); |
| +добавлено | } |
| +добавлено | if (d.vortex) d.vortex.p[1] = 6 * p.speed * env * (1 - dis); |
| +добавлено | if (dis > 0.05 && !d.dispersed) { |
| +добавлено | d.dispersed = true; |
| +добавлено | d.vortex?.release(); d.vortex = null; |
| +добавлено | fx.rig.shake(0.06, 18); |
| +добавлено | } |
| +добавлено | if (d.l1 && fadeFly <= 0.01) { d.l1.release(); d.l1 = null; } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { U, COMMON_GLSL, LINES_BLENDING, FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL, FX_BLENDING, ARENA, arenaSDF } from '../../vfx/common.js'; |
| +добавлено | import { instanced } from '../../vfx/Ribbons.js'; |
| +добавлено | |
| +добавлено | // Starfall (Arcane hero). A pocket of night opens over the target: a raymarched lens of dark |
| +добавлено | // nebula (absorbing, albedo 0 so it skips lighting) whose log-spiral arms unfurl from the centre |
| +добавлено | // and turn with differential rotation, sprinkled with twinkling stars. An arcane circle draws |
| +добавлено | // itself on the floor and motes rise from its rim. Then stars ignite inside the canopy and streak |
| +добавлено | // down in parallel (one radiant per cast, like a meteor shower): each a 4-point diffraction-spike |
| +добавлено | // head sprite, a full-res white-hot train with glitter cells and a half-res soft glow train, |
| +добавлено | // shedding sparkles, carrying a shadowed light. Each lands in a starburst flare with sparks, |
| +добавлено | // stardust that settles and paints glow, a ground shock ring and a glow + corruption ring (violet |
| +добавлено | // veins that outlive the glow). The last and largest star hits the target: a nova, a push shell |
| +добавлено | // on loose particles, knockback, flash, hitstop. Stars stop on anything solid (arenaSDF), ripple |
| +добавлено | // an Aegis they hit (arcane inst.hit) and the pool. |
| +добавлено | |
| +добавлено | const TAU = Math.PI * 2; |
| +добавлено | const MAX_CASTS = 16, MAX_STARS = 25, MAX_SKY = 150, MAX_SKY_SPRITES = 300, MAX_SPRITES = 128, MAX_POPS = 48, MAX_TRAILS = 32; |
| +добавлено | const SLOTS = 3; // shadowed lights per cast, handed from star to star |
| +добавлено | |
| +добавлено | // --- canopy: a lens of night sky (volume) -------------------------------------------------------- |
| +добавлено | const CANOPY_GLSL = /* glsl */` |
| +добавлено | uniform vec4 uSky; // grow 0..1 (revealed radius), dissolve 0..1, emission, spin (rad) |
| +добавлено | uniform vec4 uSky2; // arms, twist, night density (1/m), seed |
| +добавлено | uniform vec4 uSky3; // impact pulse, -, -, - |
| +добавлено | uniform vec3 uSkyCore; |
| +добавлено | uniform vec3 uSkyColor; |
| +добавлено | uniform vec3 uSkyGlow; |
| +добавлено | void volumeSample(vec3 pw, vec3 pl, out float sigma, out vec3 albedo, out vec3 emission) { |
| +добавлено | sigma = 0.0; albedo = vec3(0.0); emission = vec3(0.0); |
| +добавлено | float e2 = dot(pl, pl); |
| +добавлено | if (e2 >= 1.0) return; |
| +добавлено | float r = length(pl.xz), y = pl.y; |
| +добавлено | float reveal = smoothstep(uSky.x * 1.25, uSky.x * 1.25 - 0.35, r); |
| +добавлено | if (reveal <= 0.0) return; |
| +добавлено | // differential rotation: the core turns faster than the rim (a whirlpool of night) |
| +добавлено | float spin = uSky.w * (0.45 + 0.75 / (0.35 + r)); |
| +добавлено | float c = cos(spin), s = sin(spin); |
| +добавлено | vec2 q = mat2(c, -s, s, c) * pl.xz; |
| +добавлено | float arms = 0.5 + 0.5 * cos(atan(q.y, q.x) * uSky2.x + log(max(r, 0.03)) * uSky2.y); |
| +добавлено | vec3 np = vec3(q * uVolScale.x * 0.085, y * uVolScale.y * 0.08) + uSky2.w; |
| +добавлено | float n1 = noise3(np), n2 = noise3(np * 2.7 + 0.31); |
| +добавлено | float n = n1 * 0.65 + n2 * 0.35; |
| +добавлено | // a lens, thickest in the middle, its rim frayed by the noise; dissolve eats the thin parts first |
| +добавлено | float body = smoothstep(1.0, 0.78, sqrt(e2) + (n - 0.5) * 0.45); |
| +добавлено | float keep = smoothstep(uSky.y * 1.3 - 0.25, uSky.y * 1.3, n + 0.3 * (1.0 - r)); |
| +добавлено | float d = body * reveal * keep; |
| +добавлено | if (d <= 0.0) return; |
| +добавлено | sigma = uSky2.z * d * (0.6 + 0.6 * n); |
| +добавлено | // a real night: near-black airglow, the arms a faint Milky-Way band of unresolved star clouds |
| +добавлено | // (granular, mostly blue-white, a hint of the spell's colour) cut by dark dust lanes |
| +добавлено | float lane = arms * (0.25 + 1.2 * n); |
| +добавлено | float grain = noise3(np * 7.3 + 1.7); |
| +добавлено | float wisp = (smoothstep(0.55, 1.0, lane) + smoothstep(0.3, 0.9, lane) * 0.15) * (0.35 + 1.1 * grain * grain); |
| +добавлено | float core = exp(-r * r * 22.0) * exp(-y * y * 4.0); |
| +добавлено | vec3 col = mix(vec3(0.62, 0.68, 0.95), mix(uSkyGlow, uSkyColor, smoothstep(0.6, 1.2, lane)), 0.4); |
| +добавлено | float rim = body * (1.0 - body) * 4.0 * reveal; |
| +добавлено | emission = ((vec3(0.004, 0.006, 0.02) + col * wisp * 0.42 * (0.3 + 0.7 * smoothstep(1.0, 0.1, r)) + mix(uSkyCore, vec3(1.0, 0.9, 0.75), 0.5) * core * 0.7) * d |
| +добавлено | + mix(uSkyGlow, uSkyColor, 0.5) * rim * 0.1) * uSky.z * (1.0 + 1.2 * uSky3.x); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- canopy stars: half-res emission sprites in the fx pass (the night's revealage would hide |
| +добавлено | // lines-pass stars inside it) --------------------------------------------------------------------- |
| +добавлено | const SKY_VERT = /* glsl */` |
| +добавлено | in vec4 aA; // centre, size m |
| +добавлено | in vec4 aB; // rgb * intensity, - |
| +добавлено | out vec2 vQ; |
| +добавлено | out vec3 vCol; |
| +добавлено | out float vViewZ; |
| +добавлено | void main() { |
| +добавлено | vQ = vec2(0.0); vCol = vec3(0.0); vViewZ = 1.0; |
| +добавлено | if (aA.w <= 0.0) { gl_Position = vec4(2.0, 2.0, 2.0, 1.0); return; } |
| +добавлено | vec4 view = viewMatrix * vec4(aA.xyz, 1.0); |
| +добавлено | float viewZ = max(-view.z, 0.05); |
| +добавлено | float pxW = 2.0 * viewZ / (projectionMatrix[1][1] * uFxResolution.y); |
| +добавлено | float s = max(aA.w, 3.0 * pxW), aa = aA.w / s; |
| +добавлено | vQ = position.xy; |
| +добавлено | view.xy += position.xy * s; |
| +добавлено | gl_Position = projectionMatrix * view; |
| +добавлено | vCol = aB.rgb * aa * aa; |
| +добавлено | vViewZ = viewZ; |
| +добавлено | }`; |
| +добавлено | const SKY_FRAG = /* glsl */` |
| +добавлено | in vec2 vQ; |
| +добавлено | in vec3 vCol; |
| +добавлено | in float vViewZ; |
| +добавлено | void main() { |
| +добавлено | float r2 = dot(vQ, vQ); |
| +добавлено | if (r2 >= 1.0) discard; |
| +добавлено | vec2 a = abs(vQ); |
| +добавлено | float sp = exp(-a.y * 16.0) * pow(1.0 - a.x, 3.0) + exp(-a.x * 16.0) * pow(1.0 - a.y, 3.0); |
| +добавлено | float e = 1.0 - r2; |
| +добавлено | fxOut(vec3(0.0), 0.0, vCol * (exp(-r2 * 30.0) * 1.5 + sp * 0.4) * e * e, vec2(0.0), vViewZ); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- sprites: star heads and impact flares (one instanced lines draw) ----------------------------- |
| +добавлено | const SPRITE_VERT = /* glsl */` |
| +добавлено | in vec4 aA; // centre, size m (0 = dead) |
| +добавлено | in vec4 aB; // rgb * intensity, type: [0,1) falling star (spin phase), [1,2] flare (1 + age) |
| +добавлено | out vec2 vQ; |
| +добавлено | out vec3 vCol; |
| +добавлено | out float vT; |
| +добавлено | void main() { |
| +добавлено | vQ = vec2(0.0); vCol = vec3(0.0); vT = 0.0; |
| +добавлено | if (aA.w <= 0.0) { gl_Position = vec4(2.0, 2.0, 2.0, 1.0); return; } |
| +добавлено | vec4 view = viewMatrix * vec4(aA.xyz, 1.0); |
| +добавлено | float viewZ = max(-view.z, 0.05); |
| +добавлено | float pxW = 2.0 * viewZ / (projectionMatrix[1][1] * uResolution.y); |
| +добавлено | float s = max(aA.w, 6.0 * pxW), aa = aA.w / s; // never under 6 px: keep the energy instead |
| +добавлено | float rot = aB.w < 1.0 ? (aB.w - 0.5) * 0.5 : 0.0; |
| +добавлено | vec2 cs = vec2(cos(rot), sin(rot)); |
| +добавлено | vQ = mat2(cs.x, cs.y, -cs.y, cs.x) * position.xy; |
| +добавлено | view.xy += position.xy * s; |
| +добавлено | gl_Position = projectionMatrix * view; |
| +добавлено | vCol = aB.rgb * aa * aa * smoothstep(0.2, 0.8, viewZ); |
| +добавлено | vT = aB.w; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const SPRITE_FRAG = /* glsl */` |
| +добавлено | in vec2 vQ; |
| +добавлено | in vec3 vCol; |
| +добавлено | in float vT; |
| +добавлено | out vec4 fragColor; |
| +добавлено | // a thin cross whose energy falls off along each arm (diffraction spikes) |
| +добавлено | float spikes(vec2 q, float w, float p) { |
| +добавлено | vec2 a = abs(q); |
| +добавлено | return exp(-a.y * w) * pow(max(1.0 - a.x, 0.0), p) + exp(-a.x * w) * pow(max(1.0 - a.y, 0.0), p); |
| +добавлено | } |
| +добавлено | void main() { |
| +добавлено | float r2 = dot(vQ, vQ); |
| +добавлено | if (r2 >= 1.0) discard; |
| +добавлено | vec3 white = vec3(max(vCol.r, max(vCol.g, vCol.b))); |
| +добавлено | vec2 dq = mat2(0.7071, 0.7071, -0.7071, 0.7071) * vQ; |
| +добавлено | vec3 col; |
| +добавлено | if (vT < 1.0) { |
| +добавлено | // falling star: white-hot core, halo, long spikes and short diagonals |
| +добавлено | float core = exp(-r2 * 900.0), inner = exp(-r2 * 110.0); |
| +добавлено | float halo = exp(-r2 * 16.0) * 0.3 + exp(-sqrt(r2) * 7.0) * 0.1; |
| +добавлено | float sp = spikes(vQ, 150.0, 3.0) + 0.4 * spikes(dq * 1.9, 170.0, 4.0); |
| +добавлено | col = white * (core * 3.0 + inner * 0.7) + vCol * (inner + halo + sp * 0.7); |
| +добавлено | } else { |
| +добавлено | // impact flare (age x): a blinding core that collapses, an expanding ring, spikes that shrink |
| +добавлено | float x = clamp(vT - 1.0, 0.0, 1.0), f = 1.0 - x, r = sqrt(r2); |
| +добавлено | float y = (r - 0.06 - 0.8 * x) / (0.02 + 0.07 * x); |
| +добавлено | float ring = exp(-y * y) * f * f; |
| +добавлено | float fl = exp(-r2 * (40.0 + 260.0 * x)) * f * f * f; |
| +добавлено | float L = 0.3 + 0.7 * f; |
| +добавлено | float sp = (spikes(vQ / L, 130.0, 2.5) + 0.45 * spikes(dq * 1.6 / L, 150.0, 3.5)) * f * f; |
| +добавлено | col = white * fl * 2.5 + vCol * (ring * 1.3 + fl * 2.0 + sp * 0.9 + exp(-r2 * 6.0) * 0.12 * f); |
| +добавлено | } |
| +добавлено | float edge = 1.0 - r2; |
| +добавлено | col *= edge * edge; |
| +добавлено | if (uDebug > 0.5 && uDebug < 1.5) col = vec3(0.04, 0.012, 0.004); |
| +добавлено | fragColor = vec4(col, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- trains: straight analytic ribbons, head (t = 0) to tail ------------------------------------- |
| +добавлено | // p0 = head, width; p1 = tail, -; p2 = seed, length m, -, - |
| +добавлено | const TRAIN_PATH = /* glsl */` |
| +добавлено | vec3 ribbonPath(float t, vec4 p0, vec4 p1, vec4 p2, vec4 p3, out float width) { |
| +добавлено | width = p0.w * pow(1.0 - t, 1.1); |
| +добавлено | return mix(p0.xyz, p1.xyz, t); |
| +добавлено | }`; |
| +добавлено | const TRAIN_UNIFORMS = /* glsl */` |
| +добавлено | uniform vec3 uSfCore; |
| +добавлено | uniform vec3 uSfColor; |
| +добавлено | uniform vec3 uSfGlow; |
| +добавлено | `; |
| +добавлено | const TRAIN_CORE = TRAIN_UNIFORMS + /* glsl */` |
| +добавлено | vec3 ribbonShade(RibbonFrag r, out vec3 media, out float alpha, out float distortion) { |
| +добавлено | media = vec3(0.0); alpha = 0.0; distortion = 0.0; |
| +добавлено | float t = r.uv.x, y = r.uv.y, s = t * r.p2.y; |
| +добавлено | vec3 c = mix(uSfCore, uSfColor, smoothstep(0.02, 0.3, t)); |
| +добавлено | c = mix(c, uSfGlow, smoothstep(0.4, 1.0, t)); |
| +добавлено | float core = exp(-y * y * 9.0), soft = exp(-y * y * 2.2) * 0.3; |
| +добавлено | float fall = pow(1.0 - t, 2.4) + 0.25 * exp(-t * 30.0); |
| +добавлено | // glitter: sparse cells along the train flash on and off |
| +добавлено | float g = s * 6.0 + r.p2.x, cell = floor(g); |
| +добавлено | float on = step(0.76, hash11(cell * 1.37 + floor(uTime * 18.0 + hash11(cell + 3.1) * 9.0))); |
| +добавлено | float gx = (fract(g) - 0.5) * 3.0; |
| +добавлено | float sp = on * exp(-gx * gx) * exp(-y * y * 3.0) * (1.0 - t); |
| +добавлено | return r.color * (c * (core + soft) * fall + uSfCore * sp * 1.6); |
| +добавлено | }`; |
| +добавлено | const TRAIN_GLOW = TRAIN_UNIFORMS + /* glsl */` |
| +добавлено | vec3 ribbonShade(RibbonFrag r, out vec3 media, out float alpha, out float distortion) { |
| +добавлено | media = vec3(0.0); alpha = 0.0; distortion = 0.0; |
| +добавлено | float t = r.uv.x, y = r.uv.y; |
| +добавлено | vec3 c = mix(uSfColor, uSfGlow, smoothstep(0.05, 0.8, t)); |
| +добавлено | float g = max(exp(-y * y * 3.5) - 0.03, 0.0); |
| +добавлено | float k = 1.0 - t; |
| +добавлено | return r.color * c * g * k * k * k; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // module scratch (nothing is allocated after create) |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _c = new THREE.Vector3(); |
| +добавлено | const UP = new THREE.Vector3(0, 1, 0); |
| +добавлено | const sstep = THREE.MathUtils.smoothstep; |
| +добавлено | const inPool = (x, z) => Math.abs(x - ARENA.pool.center.x) < ARENA.pool.half.x && Math.abs(z - ARENA.pool.center.y) < ARENA.pool.half.y; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'starfall', name: 'Starfall', element: 'arcane', order: 10, hue: '#ff4fd8', |
| +добавлено | shape: 'area', |
| +добавлено | params: { |
| +добавлено | stars: 18, radius: 3.2, duration: 1.6, height: 8.5, angle: 26, speed: 30, trail: 6, size: 1, |
| +добавлено | sparkles: 1, intensity: 1, sky: 1, night: 1, |
| +добавлено | color: '#ff4fd8', core: '#fff1fc', glow: '#7a3dff', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | stars: [3, MAX_STARS - 1, 1], radius: [1.5, 6, 0.01], duration: [0.6, 3, 0.01], height: [6, 16, 0.1], |
| +добавлено | angle: [0, 50, 0.5], speed: [12, 60, 0.1], trail: [1, 9, 0.01], size: [0.4, 2.5, 0.01], |
| +добавлено | sparkles: [0, 3, 0.01], intensity: [0.1, 3, 0.01], sky: [0, 2, 0.01], night: [0, 2, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | 'meteor shower': { stars: 24, duration: 1.3, angle: 38, speed: 44, trail: 6, size: 0.75 }, |
| +добавлено | 'grand comets': { stars: 6, size: 1.7, trail: 7.5, speed: 24, duration: 1.8, angle: 18 }, |
| +добавлено | twilight: { color: '#b06cff', core: '#f3e8ff', glow: '#3b2dff', sky: 1.3, night: 1.4 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 1.1, impact: 2, linger: 2.4 }, |
| +добавлено | kinds: { |
| +добавлено | // stardust: thrown out by each impact, settles and twinkles out, paints glow where it lands |
| +добавлено | dust: { style: 'glow', drag: 1.8, gravity: 3, curl: 0.9, curlScale: 0.5, restitution: 0.2, friction: 0.85, life: [1.1, 2.4], size: [0.024, 0.008], stretch: 0.008, emissive: 6, color: ['#fff1fc', '#ff4fd8'], alpha: [1, 0], fadeIn: 0.02, fadeOut: 0.55, paints: 'glow', paintAmount: 0.05 }, |
| +добавлено | // wisps of the night drifting up from the impact field after the barrage |
| +добавлено | // the bolide's persistent smoke train: lit dust that hangs in the air and twists (real fireballs leave one in daylight) |
| +добавлено | smoke: { style: 'media', emissive: 0, drag: 2.2, buoyancy: 0.08, curl: 0.9, curlScale: 0.22, life: [2.6, 4.4], size: [0.16, 0.85], sizePow: 0.55, color: ['#8c8494', '#5d5866'], alpha: [0.34, 0], fadeIn: 0.05, fadeOut: 0.5, density: 1.6, erosion: 0.6, forceResponse: 0.7 }, |
| +добавлено | wisp: { style: 'glow', drag: 1.6, buoyancy: 0.55, curl: 1.3, curlScale: 0.35, forceResponse: 0.6, life: [1.4, 2.8], size: [0.018, 0.006], stretch: 0.01, emissive: 4, color: ['#fff1fc', '#7a3dff'], alpha: [1, 0], fadeIn: 0.3, fadeOut: 0.5 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | // the arcane family's own sparks and sparkles, by full name |
| +добавлено | const spark = P.emitter('arcane.spark', { shape: 'sphere', radius: 0.08, spread: 3.2, radial: 3.5 }); |
| +добавлено | const glitter = P.emitter('arcane.sparkle', { shape: 'sphere', radius: 0.06, spread: 0.8 }); |
| +добавлено | const gather = P.emitter('arcane.sparkle', { shape: 'shell', radius: 3, spread: 0.2, radial: -1.6 }); |
| +добавлено | const rise = P.emitter('arcane.mote', { shape: 'ring', radius: 3, spread: 0.2 }); |
| +добавлено | const dust = P.emitter('starfall.dust', { shape: 'sphere', radius: 0.1, spread: 2.2, radial: 2.4 }); |
| +добавлено | const wisp = P.emitter('starfall.wisp', { shape: 'disk', radius: 3, spread: 0.25 }); |
| +добавлено | const smoke = P.emitter('starfall.smoke', { shape: 'sphere', radius: 0.06, spread: 0.25 }); |
| +добавлено | // meteorite ejecta and the dust it raises: earth's own pebbles and dust, by full name |
| +добавлено | const ejecta = P.emitter('earth.pebble', { shape: 'sphere', radius: 0.12, spread: 2.2, radial: 4.5 }); |
| +добавлено | const puff = P.emitter('earth.dust', { shape: 'sphere', radius: 0.25, spread: 0.8, radial: 1.6 }); |
| +добавлено | |
| +добавлено | // colours (linear), shared by reference with every material |
| +добавлено | const col = new THREE.Color(), core = new THREE.Color(), glow = new THREE.Color(), lightCol = new THREE.Color(), skyLightCol = new THREE.Color(); |
| +добавлено | const vCore = new THREE.Vector3(), vCol = new THREE.Vector3(), vGlow = new THREE.Vector3(); |
| +добавлено | let cC = '', cK = '', cG = ''; |
| +добавлено | function syncColors() { |
| +добавлено | const p = S.params; |
| +добавлено | if (p.color === cC && p.core === cK && p.glow === cG) return; |
| +добавлено | cC = p.color; cK = p.core; cG = p.glow; |
| +добавлено | col.set(cC); core.set(cK); glow.set(cG); |
| +добавлено | vCore.set(core.r, core.g, core.b); vCol.set(col.r, col.g, col.b); vGlow.set(glow.r, glow.g, glow.b); |
| +добавлено | lightCol.copy(col).lerp(core, 0.35); |
| +добавлено | skyLightCol.copy(glow).lerp(col, 0.5); |
| +добавлено | P.setKind('starfall.dust', { color: [cK, cC] }); |
| +добавлено | P.setKind('starfall.wisp', { color: [cK, cG] }); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // reusable option objects (the systems copy what they need) |
| +добавлено | const SIG = { pos: new THREE.Vector3(), normal: new THREE.Vector3(0, 1, 0), radius: 3, color: new THREE.Color(), intensity: 0, reveal: 0, rotation: 0, style: 'arcane', seed: 0 }; |
| +добавлено | const SHOCK = { pos: new THREE.Vector3(), radius0: 0.1, radius1: 2, duration: 0.4, thickness: 0.3, distortion: 0.03, color: col, intensity: 1, ground: true }; |
| +добавлено | const SPLAT = { x: 0, z: 0, radius: 1, glow: 0, corruption: 0, heat: 0, char: 0, crack: 0, pattern: 'ring', seed: 0 }; |
| +добавлено | |
| +добавлено | // --- canopies: two volume handles (two casts at once) --- |
| +добавлено | const canopies = []; |
| +добавлено | for (let i = 0; i < 2; i++) { |
| +добавлено | const v = fx.volumes.add({ |
| +добавлено | shape: 'sphere', glsl: CANOPY_GLSL, maxSteps: 12, |
| +добавлено | uniforms: { |
| +добавлено | uSky: { value: new THREE.Vector4() }, uSky2: { value: new THREE.Vector4(3, 2.2, 0.3, 0) }, uSky3: { value: new THREE.Vector4() }, |
| +добавлено | uSkyCore: { value: vCore }, uSkyColor: { value: vCol }, uSkyGlow: { value: vGlow }, |
| +добавлено | }, |
| +добавлено | }); |
| +добавлено | v.busy = false; |
| +добавлено | canopies.push(v); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- trains: full-res cores + half-res glow --- |
| +добавлено | const trainU = { uSfCore: { value: vCore }, uSfColor: { value: vCol }, uSfGlow: { value: vGlow } }; |
| +добавлено | const cores = new fx.AnalyticRibbons({ max: MAX_TRAILS, segments: 16, pathGLSL: TRAIN_PATH, shadeGLSL: TRAIN_CORE, target: 'lines', uniforms: trainU }); |
| +добавлено | const glows = new fx.AnalyticRibbons({ max: MAX_TRAILS, segments: 12, pathGLSL: TRAIN_PATH, shadeGLSL: TRAIN_GLOW, target: 'fx', uniforms: trainU }); |
| +добавлено | fx.scenes.lines.add(cores.mesh); |
| +добавлено | fx.scenes.fx.add(glows.mesh); |
| +добавлено | |
| +добавлено | // --- sprites --- |
| +добавлено | const sgeo = new THREE.InstancedBufferGeometry(); |
| +добавлено | sgeo.setAttribute('position', new THREE.Float32BufferAttribute([-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0], 3)); |
| +добавлено | sgeo.setIndex([0, 1, 2, 0, 2, 3]); |
| +добавлено | const aA = instanced(sgeo, 'aA', MAX_SPRITES), aB = instanced(sgeo, 'aB', MAX_SPRITES); |
| +добавлено | sgeo.instanceCount = 0; |
| +добавлено | const sprites = new THREE.Mesh(sgeo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, uniforms: { ...U }, ...LINES_BLENDING, |
| +добавлено | vertexShader: COMMON_GLSL + SPRITE_VERT, fragmentShader: COMMON_GLSL + SPRITE_FRAG, |
| +добавлено | })); |
| +добавлено | sprites.frustumCulled = false; |
| +добавлено | sprites.visible = false; |
| +добавлено | fx.scenes.lines.add(sprites); |
| +добавлено | const kgeo = new THREE.InstancedBufferGeometry(); |
| +добавлено | kgeo.setAttribute('position', sgeo.attributes.position); |
| +добавлено | kgeo.setIndex(sgeo.index); |
| +добавлено | const kA = instanced(kgeo, 'aA', MAX_SKY_SPRITES), kB = instanced(kgeo, 'aB', MAX_SKY_SPRITES); |
| +добавлено | kgeo.instanceCount = 0; |
| +добавлено | const skyStars = new THREE.Mesh(kgeo, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, uniforms: { ...U }, ...FX_BLENDING, |
| +добавлено | vertexShader: FX_VERT_HEAD + COMMON_GLSL + SKY_VERT, fragmentShader: FX_FRAG_HEAD + COMMON_GLSL + FX_OUT_GLSL + SKY_FRAG, |
| +добавлено | })); |
| +добавлено | skyStars.frustumCulled = false; |
| +добавлено | skyStars.visible = false; |
| +добавлено | fx.scenes.fx.add(skyStars); |
| +добавлено | const pops = Array.from({ length: MAX_POPS }, () => ({ pos: new THREE.Vector3(), age: 1, life: 0, size: 0, r: 0, g: 0, b: 0 })); |
| +добавлено | let popHead = 0; |
| +добавлено | function addPop(pos, size, c, k, life) { |
| +добавлено | const q = pops[popHead]; popHead = (popHead + 1) % MAX_POPS; |
| +добавлено | q.pos.copy(pos); q.size = size; q.life = life; q.age = 0; |
| +добавлено | q.r = c.r * k; q.g = c.g * k; q.b = c.b * k; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- per cast record (cast.js keeps 16) --- |
| +добавлено | const ST = Array.from({ length: MAX_CASTS }, () => ({ |
| +добавлено | on: false, centre: new THREE.Vector3(), sky: new THREE.Vector3(), fall: new THREE.Vector3(), tilt: new THREE.Vector3(), q: new THREE.Quaternion(), |
| +добавлено | R: 3, Rc: 4, Hc: 1.3, n: 0, launched: 0, landed: 0, bt: 0, grow: 0, dis: 0, spin: 0, pulse: 0, sky01: 0, |
| +добавлено | vol: null, sigil: null, light: null, push: null, pushT: 0, finaleDone: false, order: new Int8Array(MAX_STARS), |
| +добавлено | skyStars: new Float32Array(MAX_SKY * 4), |
| +добавлено | stars: Array.from({ length: MAX_STARS }, () => ({ |
| +добавлено | at: 0, s: new THREE.Vector3(), l: new THREE.Vector3(), pos: new THREE.Vector3(), dir: new THREE.Vector3(), |
| +добавлено | len: 0, dist: 0, v: 0, size: 1, state: 0, age: 0, train: 0, core: null, glow: null, finale: false, ph: 0, |
| +добавлено | })), |
| +добавлено | slots: Array.from({ length: SLOTS }, () => ({ h: null, star: null, flash: 0, follow: 0 })), |
| +добавлено | })); |
| +добавлено | |
| +добавлено | // lands stars on anything solid: floor, rim, pillars, dummies, spikes, shields |
| +добавлено | function stopAt(st) { |
| +добавлено | return st.dist >= st.len || (st.dist > 1 && st.pos.y < ARENA.pillarHeight + 0.5 && arenaSDF(st.pos) < 0.04); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function schedule(c, p, d) { |
| +добавлено | const n = Math.round(p.stars), D = p.duration; |
| +добавлено | d.n = n + 1; d.launched = 0; d.landed = 0; d.finaleDone = false; |
| +добавлено | // landing points: a jittered sunflower over the disc, launched in shuffled order |
| +добавлено | for (let i = 0; i < n; i++) d.order[i] = i; |
| +добавлено | for (let i = n - 1; i > 0; i--) { const j = Math.floor(fx.rand() * (i + 1)), t = d.order[i]; d.order[i] = d.order[j]; d.order[j] = t; } |
| +добавлено | const a0 = fx.rand() * TAU; |
| +добавлено | for (let k = 0; k <= n; k++) { |
| +добавлено | const st = d.stars[k], fin = k === n; |
| +добавлено | st.finale = fin; st.state = 0; st.age = 0; st.train = 0; st.core = st.glow = null; st.ph = fx.rand(); |
| +добавлено | if (fin) { |
| +добавлено | if (c.target >= 0) st.l.copy(fx.dummies[c.target].pos).setY(0); else st.l.copy(d.centre); |
| +добавлено | st.size = p.size * 1.9; |
| +добавлено | } else { |
| +добавлено | const i = d.order[k]; |
| +добавлено | const r = d.R * Math.sqrt((i + 0.5) / n) * (0.8 + 0.3 * fx.rand()), a = a0 + i * 2.39996 + (fx.rand() - 0.5) * 0.6; |
| +добавлено | st.l.set(d.centre.x + Math.cos(a) * r, 0, d.centre.z + Math.sin(a) * r); |
| +добавлено | st.size = p.size * (0.7 + 0.5 * fx.rand()); |
| +добавлено | } |
| +добавлено | if (inPool(st.l.x, st.l.z)) st.l.y = ARENA.pool.water; |
| +добавлено | // fall along the cast's radiant, out of the canopy's lower face (its tilted mid plane, lowered) |
| +добавлено | st.dir.copy(d.fall); |
| +добавлено | _a.copy(d.sky).addScaledVector(d.tilt, -d.Hc * 0.3); |
| +добавлено | st.len = _b.subVectors(st.l, _a).dot(d.tilt) / st.dir.dot(d.tilt); |
| +добавлено | st.s.copy(st.l).addScaledVector(st.dir, -st.len); |
| +добавлено | st.v = p.speed * (fin ? 0.75 : 0.85 + 0.3 * fx.rand()); |
| +добавлено | // launch times spread over the barrage, a little denser in the middle; the finale last |
| +добавлено | st.at = fin ? D * 0.95 : D * 0.82 * sstep((k + 0.5 * fx.rand()) / n, -0.15, 1.15); |
| +добавлено | st.dist = 0; st.pos.copy(st.s); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function launch(c, p, d, st) { |
| +добавлено | st.state = 1; st.age = 0; st.dist = 0; st.pos.copy(st.s); |
| +добавлено | st.core = cores.acquire(); st.glow = glows.acquire(); |
| +добавлено | if (st.core) st.core.color.setRGB(1, 1, 1); |
| +добавлено | if (st.glow) st.glow.color.setRGB(1, 1, 1); |
| +добавлено | addPop(st.s, 0.6 * st.size, core, 3 * p.intensity, 0.3); // the star ignites in the canopy |
| +добавлено | // a light follows it: the dimmest slot |
| +добавлено | let best = null, bi = 1e9; |
| +добавлено | for (const sl of d.slots) { |
| +добавлено | if (!sl.h) continue; |
| +добавлено | const k = Math.max(sl.flash, sl.follow); |
| +добавлено | if (k < bi) { bi = k; best = sl; } |
| +добавлено | } |
| +добавлено | if (best) { best.star = st; best.flash = 0; best.h.range = 7 * st.size; } |
| +добавлено | d.launched++; |
| +добавлено | } |
| +добавлено | |
| +добавлено | function land(c, p, d, st) { |
| +добавлено | st.state = 2; st.age = 0; |
| +добавлено | st.train = Math.min(st.dist, p.trail * st.size); |
| +добавлено | d.landed++; |
| +добавлено | d.pulse = Math.min(1.5, d.pulse + (st.finale ? 1.2 : 0.35)); |
| +добавлено | const g = st.pos, fin = st.finale, z = st.size; |
| +добавлено | // light: the slot that followed this star flashes here |
| +добавлено | for (const sl of d.slots) if (sl.star === st) { |
| +добавлено | sl.star = null; sl.follow = 0; |
| +добавлено | sl.flash = (fin ? 55 : 38) * z * p.intensity; |
| +добавлено | sl.h.range = (fin ? 9 : 5.5) * Math.sqrt(z); |
| +добавлено | sl.h.pos.copy(g).addScaledVector(UP, 0.45); |
| +добавлено | } |
| +добавлено | addPop(g, (fin ? 2.2 : 1.25) * z, col, 6 * p.intensity, fin ? 0.6 : 0.35); |
| +добавлено | // a white sub-flash keeps the centre hot while the coloured ring expands |
| +добавлено | addPop(_a.copy(g).addScaledVector(UP, 0.05), 0.55 * z, core, (fin ? 3.5 : 5) * p.intensity, fin ? 0.3 : 0.18); |
| +добавлено | spark.pos.copy(g).addScaledVector(UP, 0.05); spark.vel.set(0, fin ? 3 : 1.8, 0).addScaledVector(st.dir, -1.2); |
| +добавлено | spark.burst((fin ? 420 : 80) * p.sparkles * Math.min(z, 1.5)); |
| +добавлено | dust.pos.copy(spark.pos); dust.vel.set(0, fin ? 3.5 : 2, 0); dust.radial = fin ? 3.4 : 2.2; |
| +добавлено | dust.burst((fin ? 260 : 36) * p.sparkles); |
| +добавлено | glitter.pos.copy(spark.pos); glitter.vel.set(0, 1, 0); glitter.spread = fin ? 3 : 1.6; |
| +добавлено | glitter.burst((fin ? 260 : 50) * p.sparkles); |
| +добавлено | glitter.spread = 0.8; |
| +добавлено | spark.vel.set(0, 0, 0); glitter.vel.set(0, 0, 0); |
| +добавлено | // ground: shock ring, glow + corruption (violet veins outlive the glow and creep along the stone) |
| +добавлено | const onFloor = g.y < 2.6; // on the floor, or on something standing on it (a dummy, a shield) |
| +добавлено | SHOCK.pos.set(g.x, (inPool(g.x, g.z) ? ARENA.pool.water : 0) + 0.03, g.z); SHOCK.radius0 = 0.1; SHOCK.radius1 = (fin ? 6.5 : 1.8) * z; |
| +добавлено | SHOCK.duration = fin ? 0.7 : 0.38; SHOCK.thickness = fin ? 0.8 : 0.3; SHOCK.distortion = fin ? 0.05 : 0.02; |
| +добавлено | SHOCK.intensity = (fin ? 3 : 1.4) * p.intensity; SHOCK.ground = onFloor; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | if (onFloor) { |
| +добавлено | SPLAT.x = g.x; SPLAT.z = g.z; SPLAT.seed = (c.seed + d.landed * 17) % 100; |
| +добавлено | SPLAT.radius = (fin ? 1.6 : 0.75) * z; SPLAT.glow = fin ? 0.9 : 0.4; SPLAT.corruption = fin ? 1.5 : 0.75; SPLAT.pattern = fin ? 'radial' : 'ring'; |
| +добавлено | SPLAT.heat = fin ? 0.9 : 0.35; SPLAT.char = fin ? 0.7 : 0.3; SPLAT.crack = fin ? 0.8 : 0.15 * z; |
| +добавлено | fx.ground.splat(SPLAT); |
| +добавлено | SPLAT.heat = SPLAT.char = SPLAT.crack = 0; |
| +добавлено | if (fin) { SPLAT.radius = d.R; SPLAT.glow = 0.6; SPLAT.corruption = 0.6; SPLAT.pattern = 'ring'; fx.ground.splat(SPLAT); } |
| +добавлено | if (!inPool(g.x, g.z)) { |
| +добавлено | ejecta.pos.copy(g).addScaledVector(UP, 0.08); ejecta.vel.set(0, fin ? 4.5 : 2.6, 0).addScaledVector(st.dir, -1.5); |
| +добавлено | ejecta.burst((fin ? 70 : 5) * Math.min(z, 1.6)); |
| +добавлено | puff.pos.copy(g).addScaledVector(UP, 0.2); puff.vel.set(0, fin ? 1.2 : 0.6, 0); |
| +добавлено | puff.burst((fin ? 46 : 5) * Math.min(z, 1.6)); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | if (inPool(g.x, g.z)) fx.pool?.ripple(g.x, g.z, fin ? 0.08 : 0.035, fin ? 1 : 0.5); |
| +добавлено | // an Aegis in the way ripples |
| +добавлено | fx.spells.arcane?.inst?.hit?.(g, fin ? 1.4 : 0.6); |
| +добавлено | // dummies: knockback from the blast |
| +добавлено | const reach = (fin ? 3.2 : 1.3) * z; |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dist = Math.hypot(dm.pos.x - g.x, dm.pos.z - g.z); |
| +добавлено | if (dist > reach) continue; |
| +добавлено | _b.set(dm.pos.x - g.x, 0, dm.pos.z - g.z); |
| +добавлено | if (dist < 0.05) _b.copy(st.dir); |
| +добавлено | dm.hit(_b, (fin ? 1.1 : 0.3) * (1 - dist / reach) + 0.1); |
| +добавлено | } |
| +добавлено | if (fin) { |
| +добавлено | d.finaleDone = true; |
| +добавлено | fx.impacts.add('holy', _a.copy(g).setY(Math.max(g.y, 0) + 0.8), 3.2 * z, 0.7, 3.5, col); |
| +добавлено | fx.flash(0.14 * p.intensity); |
| +добавлено | fx.rig.shake(0.5, 13); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(2.5, 0.08); fx.rig.hitstop(0.06); } |
| +добавлено | // a pressure shell pushes loose particles outward |
| +добавлено | d.push = fx.forces.acquire(); |
| +добавлено | if (d.push) { d.push.type = 4; d.push.pos.copy(g).setY(Math.max(g.y, 0) + 0.3); d.push.p[0] = 0.3; d.push.p[1] = 1.2; d.push.p[2] = 60; d.push.p[3] = 0; d.pushT = 0; } |
| +добавлено | } else fx.rig.shake(0.07 * z, 22); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function release(d) { |
| +добавлено | for (const st of d.stars) { st.core?.release(); st.glow?.release(); st.core = st.glow = null; st.state = 3; } |
| +добавлено | for (const sl of d.slots) { sl.h?.release(); sl.h = null; sl.star = null; sl.flash = sl.follow = 0; } |
| +добавлено | if (d.vol) { d.vol.visible = false; d.vol.busy = false; } |
| +добавлено | d.sigil?.release(); d.light?.release(); d.push?.release(); |
| +добавлено | d.vol = d.sigil = d.light = d.push = null; |
| +добавлено | d.on = false; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // a train's ribbons from the star's state |
| +добавлено | function placeTrain(st, I) { |
| +добавлено | const len = st.state === 1 ? Math.min(st.dist, S.params.trail * st.size) : st.train; |
| +добавлено | _c.copy(st.pos).addScaledVector(st.dir, -Math.max(len, 0.01)); |
| +добавлено | const h = st.core, g = st.glow; |
| +добавлено | if (h) { |
| +добавлено | h.p0.set(st.pos.x, st.pos.y, st.pos.z, 0.11 * st.size); |
| +добавлено | h.p1.set(_c.x, _c.y, _c.z, 0); |
| +добавлено | h.p2.set(st.ph * 97, Math.max(len, 0.01), 0, 0); |
| +добавлено | h.intensity = 9 * I; |
| +добавлено | } |
| +добавлено | if (g) { |
| +добавлено | g.p0.set(st.pos.x, st.pos.y, st.pos.z, 0.85 * st.size); |
| +добавлено | g.p1.set(_c.x, _c.y, _c.z, 0); |
| +добавлено | g.p2.set(0, Math.max(len, 0.01), 0, 0); |
| +добавлено | g.intensity = 2 * I; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | syncColors(); |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'color' || k === 'core' || k === 'glow') syncColors(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = ST[c.index]; |
| +добавлено | syncColors(); |
| +добавлено | if (name === 'charge') { |
| +добавлено | d.on = true; d.grow = 0; d.dis = 0; d.spin = (c.seed % 628) / 100; d.pulse = 0; d.bt = 0; d.sky01 = 0; |
| +добавлено | c.durations.charge = 1.1; c.durations.impact = p.duration + 0.55; c.durations.linger = 2.4; |
| +добавлено | d.R = p.radius; d.Rc = p.radius * 1.7 + 1.0; d.Hc = 1.3 + 0.12 * p.radius; |
| +добавлено | if (c.target >= 0) d.centre.copy(fx.dummies[c.target].pos).setY(0); else d.centre.set(c.to.x, 0, c.to.z); |
| +добавлено | // the radiant: sideways to the cast (diagonal on screen from behind the caster), a bit towards it |
| +добавлено | const side = (c.seed & 1) ? 1 : -1, tanA = Math.tan(THREE.MathUtils.degToRad(p.angle)); |
| +добавлено | _a.set(-c.dir.z * side, 0, c.dir.x * side).addScaledVector(_b.set(-c.dir.x, 0, -c.dir.z), 0.35); |
| +добавлено | if (_a.lengthSq() < 1e-6) _a.set(1, 0, 0); |
| +добавлено | _a.normalize(); |
| +добавлено | d.fall.copy(_a).multiplyScalar(tanA).addScaledVector(UP, -1).normalize(); |
| +добавлено | const H = Math.max(p.height, d.Hc + 5); |
| +добавлено | // the canopy sits up the radiant from the target and turns its face to the caster's side |
| +добавлено | d.sky.copy(d.centre).addScaledVector(d.fall, -H / -d.fall.y); |
| +добавлено | d.tilt.set(c.dir.x, 0, c.dir.z); |
| +добавлено | if (d.tilt.lengthSq() < 1e-6) d.tilt.set(0, 0, -1); |
| +добавлено | d.tilt.normalize().multiplyScalar(0.7).add(UP).normalize(); |
| +добавлено | d.q.setFromUnitVectors(UP, d.tilt); |
| +добавлено | schedule(c, p, d); |
| +добавлено | // twinkling stars inside the canopy: angle, radius 0..1, height -1..1, phase |
| +добавлено | for (let i = 0; i < MAX_SKY; i++) { |
| +добавлено | const o = i * 4; |
| +добавлено | d.skyStars[o] = fx.rand() * TAU; d.skyStars[o + 1] = Math.sqrt(fx.rand()) * 0.88; |
| +добавлено | d.skyStars[o + 2] = -0.75 + fx.rand() * 0.9; d.skyStars[o + 3] = fx.rand(); |
| +добавлено | } |
| +добавлено | d.vol = null; |
| +добавлено | for (const v of canopies) if (!v.busy) { d.vol = v; break; } |
| +добавлено | if (d.vol) { |
| +добавлено | d.vol.busy = true; d.vol.visible = false; |
| +добавлено | d.vol.uniforms.uSky2.value.set(3, 2.4, 1.6 * p.night, (c.seed % 997) * 0.013); |
| +добавлено | } |
| +добавлено | SIG.pos.set(d.centre.x, 0.02, d.centre.z); SIG.radius = d.R; SIG.color.copy(col); |
| +добавлено | SIG.intensity = 0; SIG.reveal = 0; SIG.rotation = d.spin; SIG.seed = c.seed % 1000; |
| +добавлено | d.sigil = fx.sigils.acquire(SIG); |
| +добавлено | d.light = fx.lights.acquire(); |
| +добавлено | if (d.light) { d.light.range = d.Rc * 2.2 + 4; d.light.intensity = 0; d.light.color.copy(skyLightCol); d.light.pos.copy(d.sky).addScaledVector(d.tilt, -d.Hc - 0.6); } |
| +добавлено | for (const sl of d.slots) { |
| +добавлено | sl.h = fx.lights.acquire(); sl.star = null; sl.flash = sl.follow = 0; |
| +добавлено | if (sl.h) { sl.h.intensity = 0; sl.h.color.copy(lightCol); sl.h.shadow = true; sl.h.range = 7; } |
| +добавлено | } |
| +добавлено | d.push = null; |
| +добавлено | // night falls on the floor: a dim violet-veined disc under the canopy |
| +добавлено | SPLAT.x = d.centre.x; SPLAT.z = d.centre.z; SPLAT.radius = d.R * 1.1; SPLAT.glow = 0; SPLAT.corruption = 0.45 * p.night; |
| +добавлено | SPLAT.pattern = 'noise'; SPLAT.seed = c.seed % 100; |
| +добавлено | if (p.night > 0) fx.ground.splat(SPLAT); |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | d.bt = 0; |
| +добавлено | fx.rig.shake(0.1, 9); |
| +добавлено | } else if (name === 'done') release(d); |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = ST[c.index], I = p.intensity; |
| +добавлено | if (!d.on) return; |
| +добавлено | d.spin += dt * (0.35 + 0.5 * d.pulse); |
| +добавлено | d.pulse = Math.max(0, d.pulse - dt * 3); |
| +добавлено | let k = 1, sigI = 0; |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | k = c.t / c.durations.charge; |
| +добавлено | d.grow = sstep(k, 0.02, 0.95); |
| +добавлено | d.sky01 = sstep(k, 0.15, 0.9); |
| +добавлено | if (d.sigil) { d.sigil.reveal = sstep(k, 0, 0.85); sigI = (1 + 2.5 * k) * I; } |
| +добавлено | rise.pos.set(d.centre.x, 0.05, d.centre.z); rise.radius = d.R; rise.vel.set(0, 2.5 + 3 * k, 0); |
| +добавлено | rise.rate(700 * k * p.sparkles, dt); |
| +добавлено | gather.pos.copy(d.sky); gather.radius = d.Rc * 0.8; gather.rate(600 * d.grow * p.sparkles, dt); |
| +добавлено | fx.arena.setFocus(fx.arena.focus.color, 2.5 + 3 * k); |
| +добавлено | } else if (c.phase === 'impact') { |
| +добавлено | d.grow = 1; d.sky01 = 1; |
| +добавлено | d.bt += dt; |
| +добавлено | sigI = (1.6 + 2.5 * d.pulse) * I; |
| +добавлено | if (d.sigil) d.sigil.reveal = 1; |
| +добавлено | while (d.launched < d.n && d.bt >= d.stars[d.launched].at) launch(c, p, d, d.stars[d.launched]); |
| +добавлено | fx.arena.setFocus(fx.arena.focus.color, 2.5 + 2 * (1 - c.t / c.durations.impact)); |
| +добавлено | } else { |
| +добавлено | // linger: the night thins away, the circle unwinds |
| +добавлено | const t = c.t / c.durations.linger; |
| +добавлено | d.dis = sstep(t, 0.05, 0.85); |
| +добавлено | d.sky01 = 1 - sstep(t, 0, 0.7); |
| +добавлено | if (d.sigil) d.sigil.reveal = 1 - sstep(t, 0.2, 0.9); |
| +добавлено | sigI = 1.6 * I * (1 - t); |
| +добавлено | if (t < 0.6) { wisp.pos.set(d.centre.x, 0.1, d.centre.z); wisp.radius = d.R; wisp.vel.set(0, 0.5, 0); wisp.rate(220 * p.sparkles * (1 - t / 0.6), dt); } |
| +добавлено | fx.arena.setFocus(fx.arena.focus.color, 2.5); |
| +добавлено | } |
| +добавлено | if (d.sigil) { d.sigil.intensity = sigI; d.sigil.rotation = d.spin * 0.25; } |
| +добавлено | |
| +добавлено | // stars: fly, land, drain |
| +добавлено | for (let i = 0; i < d.launched; i++) { |
| +добавлено | const st = d.stars[i]; |
| +добавлено | if (st.state === 1) { |
| +добавлено | st.age += dt; |
| +добавлено | const v = st.v + 25 * st.age; |
| +добавлено | st.dist = Math.min(st.len, st.dist + v * dt); |
| +добавлено | st.pos.copy(st.s).addScaledVector(st.dir, st.dist); |
| +добавлено | if (stopAt(st)) land(c, p, d, st); |
| +добавлено | else { |
| +добавлено | glitter.pos.copy(st.pos); glitter.vel.copy(st.dir).multiplyScalar(-1.5); glitter.rate(260 * p.sparkles * Math.min(st.size, 1.5), dt); glitter.vel.set(0, 0, 0); |
| +добавлено | const sm = st.finale ? 1 : Math.max(0, st.size - 1.2); |
| +добавлено | if (sm > 0) { smoke.pos.copy(st.pos); smoke.vel.copy(st.dir).multiplyScalar(-0.6); smoke.rate(170 * sm, dt); } |
| +добавлено | } |
| +добавлено | } else if (st.state === 2) { |
| +добавлено | st.age += dt; |
| +добавлено | st.train = Math.max(0, st.train - dt * 6); // a persistent train: lingers, shortens into the impact |
| +добавлено | if (st.train <= 0.02 || st.age > 0.7) { st.state = 3; st.core?.release(); st.glow?.release(); st.core = st.glow = null; } |
| +добавлено | } |
| +добавлено | if (st.state === 1 || st.state === 2) { |
| +добавлено | const f = st.state === 1 ? Math.min(1, st.age / 0.06) : Math.exp(-st.age * 4.5) * (1 - st.age / 0.7); |
| +добавлено | placeTrain(st, I * f * (st.finale ? 1.6 : 1) * Math.min(st.size, 1.6)); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // lights |
| +добавлено | for (const sl of d.slots) { |
| +добавлено | if (!sl.h) continue; |
| +добавлено | sl.flash *= Math.exp(-dt / 0.11); |
| +добавлено | const st = sl.star; |
| +добавлено | if (st && st.state === 1) { sl.h.pos.copy(st.pos); sl.follow = 9 * st.size * I * Math.min(1, st.age / 0.08); } else sl.follow = 0; |
| +добавлено | sl.h.intensity = Math.max(sl.flash, sl.follow); |
| +добавлено | } |
| +добавлено | if (d.light) { |
| +добавлено | d.light.pos.copy(d.sky).addScaledVector(d.tilt, -d.Hc - 0.6); |
| +добавлено | d.light.intensity = (5 * d.sky01 + 6 * d.pulse) * I * p.sky; |
| +добавлено | } |
| +добавлено | if (d.push) { |
| +добавлено | d.pushT += dt; |
| +добавлено | d.push.p[0] = 0.3 + 9 * d.pushT; d.push.p[2] = 60 * Math.max(0, 1 - d.pushT / 0.5); |
| +добавлено | if (d.pushT > 0.5) { d.push.release(); d.push = null; } |
| +добавлено | } |
| +добавлено | // canopy |
| +добавлено | const v = d.vol; |
| +добавлено | if (v) { |
| +добавлено | v.visible = d.grow > 0.01 && d.dis < 0.995 && p.sky + p.night > 0; |
| +добавлено | if (v.visible) { |
| +добавлено | v.position.copy(d.sky); |
| +добавлено | v.quaternion.copy(d.q); |
| +добавлено | v.scale.set(d.Rc, d.Hc, d.Rc); |
| +добавлено | v.uniforms.uSky.value.set(d.grow, d.dis, 1.4 * I * p.sky, d.spin); |
| +добавлено | v.uniforms.uSky2.value.z = 1.6 * p.night; |
| +добавлено | v.uniforms.uSky3.value.x = d.pulse; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | tick(dt) { |
| +добавлено | syncColors(); |
| +добавлено | cores.update(); glows.update(); |
| +добавлено | const A = aA.array, B = aB.array, I = S.params.intensity; |
| +добавлено | const time = U.uTime.value, KA = kA.array, KB = kB.array; |
| +добавлено | let n = 0, m = 0; |
| +добавлено | for (const d of ST) { |
| +добавлено | if (!d.on) continue; |
| +добавлено | // falling star heads |
| +добавлено | for (let i = 0; i < d.launched && n < MAX_SPRITES; i++) { |
| +добавлено | const st = d.stars[i]; |
| +добавлено | if (st.state !== 1) continue; |
| +добавлено | const fl = 0.8 + 0.2 * Math.sin(st.age * 41 + st.ph * 13) * Math.sin(st.age * 17 + st.ph * 7) + 0.25 * Math.max(0, Math.sin(st.age * 9 + st.ph * 31) - 0.7) / 0.3; |
| +добавлено | const o = n * 4, f = Math.min(1, st.age / 0.05), k = (st.finale ? 9 : 6) * I * f * fl; |
| +добавлено | A[o] = st.pos.x; A[o + 1] = st.pos.y; A[o + 2] = st.pos.z; A[o + 3] = (st.finale ? 2.4 : 1.5) * st.size * (1 + 0.08 * Math.sin(st.age * 50 + st.ph * 9)); |
| +добавлено | B[o] = (col.r * 0.6 + core.r * 0.4) * k; B[o + 1] = (col.g * 0.6 + core.g * 0.4) * k; B[o + 2] = (col.b * 0.6 + core.b * 0.4) * k; |
| +добавлено | B[o + 3] = (st.ph + st.age * 0.5) % 1; |
| +добавлено | n++; |
| +добавлено | } |
| +добавлено | // canopy stars, turning with the nebula (same differential rotation) |
| +добавлено | const vis = d.sky01 * S.params.sky; |
| +добавлено | if (vis > 0.01) for (let i = 0; i < MAX_SKY && m < MAX_SKY_SPRITES; i++) { |
| +добавлено | const q = i * 4, r01 = d.skyStars[q + 1]; |
| +добавлено | if (r01 > d.grow * 1.2 - 0.1) continue; |
| +добавлено | const ang = d.skyStars[q] - d.spin * (0.45 + 0.75 / (0.35 + r01)); |
| +добавлено | const ph = d.skyStars[q + 3], tw = 0.55 + 0.45 * Math.sin(time * (3 + 5 * ph) + ph * 40); |
| +добавлено | const o = m * 4, kk = 5 * I * vis * tw * tw * (0.3 + ph * ph); |
| +добавлено | _a.set(Math.cos(ang) * r01 * d.Rc, d.skyStars[q + 2] * d.Hc, Math.sin(ang) * r01 * d.Rc).applyQuaternion(d.q).add(d.sky); |
| +добавлено | KA[o] = _a.x; KA[o + 1] = _a.y; KA[o + 2] = _a.z; |
| +добавлено | KA[o + 3] = 0.04 + 0.14 * ph * ph * ph; |
| +добавлено | const w = ph * 0.7; // some white, some tinted |
| +добавлено | KB[o] = (core.r * (1 - w) + col.r * w) * kk; KB[o + 1] = (core.g * (1 - w) + col.g * w) * kk; KB[o + 2] = (core.b * (1 - w) + col.b * w) * kk; |
| +добавлено | m++; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | for (const q of pops) { |
| +добавлено | if (q.age >= q.life) continue; |
| +добавлено | q.age += dt; |
| +добавлено | if (q.age >= q.life || n >= MAX_SPRITES) continue; |
| +добавлено | const o = n * 4; |
| +добавлено | A[o] = q.pos.x; A[o + 1] = q.pos.y; A[o + 2] = q.pos.z; A[o + 3] = q.size; |
| +добавлено | B[o] = q.r; B[o + 1] = q.g; B[o + 2] = q.b; B[o + 3] = 1 + q.age / q.life; |
| +добавлено | n++; |
| +добавлено | } |
| +добавлено | sgeo.instanceCount = n; |
| +добавлено | sprites.visible = n > 0; |
| +добавлено | if (n > 0) aA.needsUpdate = aB.needsUpdate = true; |
| +добавлено | kgeo.instanceCount = m; |
| +добавлено | skyStars.visible = m > 0; |
| +добавлено | if (m > 0) kA.needsUpdate = kB.needsUpdate = true; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { ARENA, arenaSDF } from '../vfx/common.js'; |
| +добавлено | |
| +добавлено | // Cast pool + phase clock (PLAN 2.4). One record per live cast; spells keep per-cast state in |
| +добавлено | // c.data (reused, so they reset it in phase 'charge'). Phases: |
| +добавлено | // charge -> release (instant) -> travel (projectile flight / beam sustain) -> impact -> linger -> done |
| +добавлено | // shape decides travel: projectile integrates until it hits the arena or reaches the target, |
| +добавлено | // beam sustains while held (or timing.sustain), strike/area/self skip it. A spell may override |
| +добавлено | // shape per form with formShapes: { 'Form name': 'strike' }. |
| +добавлено | const MAX_CASTS = 16; |
| +добавлено | const _d = new THREE.Vector3(); |
| +добавлено | |
| +добавлено | function record(i) { |
| +добавлено | return { |
| +добавлено | index: i, alive: false, spell: null, inst: null, form: '', shape: 'projectile', phase: 'done', t: 0, age: 0, seed: 0, |
| +добавлено | from: new THREE.Vector3(), to: new THREE.Vector3(), pos: new THREE.Vector3(), vel: new THREE.Vector3(), dir: new THREE.Vector3(), |
| +добавлено | held: false, travelled: 0, target: -1, data: {}, durations: { charge: 0, travel: 0, impact: 0, linger: 0 }, |
| +добавлено | }; |
| +добавлено | } |
| +добавлено | |
| +добавлено | export class Casts { |
| +добавлено | constructor(fx) { |
| +добавлено | this.fx = fx; |
| +добавлено | this.list = Array.from({ length: MAX_CASTS }, (_, i) => record(i)); |
| +добавлено | this.last = null; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // spell: module from SPELLS (with .inst set at load). to: world point. target: dummy index or -1. |
| +добавлено | cast(spell, to, { form = spell.forms?.[0] ?? '', from = ARENA.focus, target = -1, held = false, seed } = {}) { |
| +добавлено | let c = this.list.find((x) => !x.alive); |
| +добавлено | if (!c) { // pool full: recycle the oldest |
| +добавлено | c = this.list.reduce((a, b) => (b.age > a.age ? b : a)); |
| +добавлено | this.enter(c, 'done'); |
| +добавлено | } |
| +добавлено | Object.assign(c, { alive: true, spell, inst: spell.inst, form, shape: spell.formShapes?.[form] ?? spell.shape ?? 'projectile', t: 0, age: 0, held, travelled: 0, target }); |
| +добавлено | c.seed = seed ?? Math.floor(this.fx.rand() * 1e6); |
| +добавлено | c.from.copy(from); c.to.copy(to); c.pos.copy(from); |
| +добавлено | c.dir.subVectors(to, from).normalize(); |
| +добавлено | c.vel.set(0, 0, 0); |
| +добавлено | const T = spell.timing ?? {}; |
| +добавлено | c.durations.charge = T.charge ?? 0.5; |
| +добавлено | c.durations.travel = T.sustain ?? 4; |
| +добавлено | c.durations.impact = T.impact ?? 0.2; |
| +добавлено | c.durations.linger = T.linger ?? 2; |
| +добавлено | this.enter(c, 'charge'); |
| +добавлено | this.last = c; |
| +добавлено | return c; |
| +добавлено | } |
| +добавлено | |
| +добавлено | enter(c, phase) { |
| +добавлено | c.phase = phase; |
| +добавлено | c.t = 0; |
| +добавлено | c.inst.phase?.(c, phase); |
| +добавлено | if (phase === 'done') c.alive = false; |
| +добавлено | } |
| +добавлено | |
| +добавлено | releaseHeld() { for (const c of this.list) c.held = false; } |
| +добавлено | clear() { for (const c of this.list) if (c.alive) this.enter(c, 'done'); } |
| +добавлено | |
| +добавлено | update(dt) { |
| +добавлено | for (const c of this.list) { |
| +добавлено | if (!c.alive) continue; |
| +добавлено | c.t += dt; |
| +добавлено | c.age += dt; |
| +добавлено | const s = c.spell, d = c.durations; |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | c.pos.copy(c.from); |
| +добавлено | if (c.t >= d.charge) { |
| +добавлено | this.enter(c, 'release'); |
| +добавлено | const shape = c.shape; |
| +добавлено | if (shape === 'projectile') { |
| +добавлено | c.vel.copy(c.dir).multiplyScalar(s.params?.speed ?? s.speed ?? 15); |
| +добавлено | if (s.lob) c.vel.y += s.lob; // lobbed: initial upward speed, gravity below |
| +добавлено | this.enter(c, 'travel'); |
| +добавлено | } else if (shape === 'beam') this.enter(c, 'travel'); |
| +добавлено | else { if (shape !== 'self') c.pos.copy(c.to); this.enter(c, 'impact'); } |
| +добавлено | } |
| +добавлено | } else if (c.phase === 'travel') { |
| +добавлено | if (c.shape === 'projectile') { |
| +добавлено | // the spell may steer (homing) by editing c.vel in its update; integrate here |
| +добавлено | if (c.inst.travel) { if (c.inst.travel(c, dt)) { this.enter(c, 'impact'); continue; } } |
| +добавлено | else { |
| +добавлено | if (s.lob) c.vel.y -= 9.81 * dt; |
| +добавлено | c.pos.addScaledVector(c.vel, dt); |
| +добавлено | c.travelled += c.vel.length() * dt; |
| +добавлено | const r = s.radius ?? 0.3; |
| +добавлено | const hit = (c.travelled > 1 && arenaSDF(c.pos) < r) || (!s.lob && _d.subVectors(c.to, c.pos).dot(c.dir) <= 0) || c.t > d.travel; |
| +добавлено | if (hit) { if (!s.lob && c.t <= d.travel && _d.subVectors(c.to, c.pos).dot(c.dir) <= 0) c.pos.copy(c.to); this.enter(c, 'impact'); continue; } |
| +добавлено | } |
| +добавлено | } else if (!c.held && c.t >= (s.timing?.sustain ?? 0)) { this.enter(c, 'impact'); continue; } |
| +добавлено | } else if (c.phase === 'impact') { |
| +добавлено | if (c.t >= d.impact) this.enter(c, 'linger'); |
| +добавлено | } else if (c.phase === 'linger') { |
| +добавлено | if (c.t >= d.linger) { this.enter(c, 'done'); continue; } |
| +добавлено | } |
| +добавлено | if (c.alive) c.inst.update?.(c, dt); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // for the HUD phase strip: the newest live cast |
| +добавлено | active() { return this.last?.alive ? this.last : null; } |
| +добавлено | count() { let n = 0; for (const c of this.list) n += c.alive ? 1 : 0; return n; } |
| +добавлено | } |
| +добавлено | // Scripted combo demos (PLAN 3 "Combos"). The interactions themselves are emergent (ground |
| +добавлено | // field, shared forces, occluders); these just fire the right spells in the right order. |
| +добавлено | // step: { spell, form, target (dummy index) | to [x, y, z], delay (s after the previous step), hold (s, beams) } |
| +добавлено | const POOL = [9.75, 0, 0]; |
| +добавлено | |
| +добавлено | export const COMBOS = [ |
| +добавлено | { name: 'Steam (frost, then fire)', steps: [ |
| +добавлено | { spell: 'ice', form: 'Glacial Spike', target: 0 }, |
| +добавлено | { spell: 'fire', form: 'Fireball', target: 0, delay: 1.8 }, |
| +добавлено | ] }, |
| +добавлено | { name: 'Conduction (soak the pool, then lightning)', steps: [ |
| +добавлено | { spell: 'water', form: 'Tidal Lance', to: POOL, hold: 1.2 }, |
| +добавлено | { spell: 'lightning', form: 'Thunderstrike', to: POOL, delay: 1.6 }, |
| +добавлено | ] }, |
| +добавлено | { name: 'Firestorm (cyclone, then fireball)', steps: [ |
| +добавлено | { spell: 'wind', form: 'Cyclone', target: 0 }, |
| +добавлено | { spell: 'fire', form: 'Fireball', target: 0, delay: 1.0 }, |
| +добавлено | { spell: 'fire', form: 'Inferno', target: 0, delay: 0.6 }, |
| +добавлено | ] }, |
| +добавлено | { name: 'Magma (upheaval, then fire)', steps: [ |
| +добавлено | { spell: 'earth', form: 'Upheaval', target: 1 }, |
| +добавлено | { spell: 'fire', form: 'Inferno', target: 1, delay: 1.2 }, |
| +добавлено | ] }, |
| +добавлено | { name: 'Flash freeze (soak, then ice)', steps: [ |
| +добавлено | { spell: 'water', form: 'Tidal Lance', target: 0, hold: 1.2 }, |
| +добавлено | { spell: 'ice', form: 'Glacial Spike', target: 0, delay: 1.6 }, |
| +добавлено | ] }, |
| +добавлено | { name: 'Event horizon (storm, embers, singularity)', steps: [ |
| +добавлено | { spell: 'wind', form: 'Cyclone', target: 0 }, |
| +добавлено | { spell: 'fire', form: 'Inferno', target: 0, delay: 0.5 }, |
| +добавлено | { spell: 'void', form: 'Singularity', target: 0, delay: 1.2 }, |
| +добавлено | ] }, |
| +добавлено | { name: 'Aegis test (every element vs the shield)', steps: [ |
| +добавлено | { spell: 'arcane', form: 'Aegis', target: 0 }, |
| +добавлено | ...['lightning', 'fire', 'water', 'ice', 'earth', 'wind', 'void', 'light', 'acid', 'arcane'].map((spell, i) => ({ |
| +добавлено | spell, target: 0, delay: i === 0 ? 1.2 : 0.9, ...(spell === 'arcane' ? { form: 'Arcane Barrage' } : {}), |
| +добавлено | })), |
| +добавлено | ] }, |
| +добавлено | ]; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { FullscreenQuad, rawPass } from '../engine/fullscreen.js'; |
| +добавлено | import { U, COMMON_GLSL, NOISE_GLSL, WORLD_GLSL, LIGHTS_GLSL, arenaSDF } from '../vfx/common.js'; |
| +добавлено | import { blackbodyRGB } from '../vfx/noise.js'; |
| +добавлено | import { FluidVolume } from '../vfx/FluidVolume.js'; |
| +добавлено | import { mergeVertices } from 'three/addons/utils/BufferGeometryUtils.js'; |
| +добавлено | |
| +добавлено | // Earth (PLAN 3). |
| +добавлено | // Upheaval: a crack races from the caster's side to the target along the vein network (a 'line' |
| +добавлено | // splat of crack potential re-stamped as its head advances) while the floor rumbles at 8 Hz, |
| +добавлено | // pebbles hop and dust seeps from it. Then a line of rock spikes erupts in sequence (staggered |
| +добавлено | // ease-out-back), each tearing a radial crack, throwing rock debris and chunks, rolling a ring of |
| +добавлено | // ground-hugging dust outward and a plume up; the last and biggest rams the target with a floor |
| +добавлено | // shock ring. The spikes crumble into falling rock; the cracks stay ~15 s. |
| +добавлено | // Meteor: rock chunks tear out of the floor around the caster and fly in to assemble a boulder |
| +добавлено | // (a particle swarm pulled in by an attractor fills the gaps), which is then lobbed on a heavy |
| +добавлено | // ballistic arc, glowing seams heating up in flight, and lands in a crater: crack splat, a rim of |
| +добавлено | // upturned slabs, debris, dust, shock ring, the heaviest shake. |
| +добавлено | // Magma: a tiny local GPU probe reads ground heat under every live spike (any heat source: fire, |
| +добавлено | // lightning, a molten preset); hot spikes glow (spike.heat), add a 'magma' impact (floor cracks |
| +добавлено | // glow around them) and crumble into glowing chunks. |
| +добавлено | |
| +добавлено | const MAX_SPIKES = 12, MAX_RIM = 10, CHUNKS = 15, BOULDERS = 3, PROBES = 16; |
| +добавлено | const CORE_R = 0.62; // m per meteorSize: the finished boulder's radius |
| +добавлено | const FRAG = 0.3; // fragment geometry scale |
| +добавлено | const GRAVITY = 16; // the boulder's arc: heavier than life reads as weight |
| +добавлено | const STATES = 16; // one per cast record |
| +добавлено | |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _c = new THREE.Vector3(), _s = new THREE.Vector3(); |
| +добавлено | const _push = new THREE.Vector3(), _q = new THREE.Quaternion(), _q2 = new THREE.Quaternion(), _m = new THREE.Matrix4(), _col = new THREE.Color(); |
| +добавлено | const UP = new THREE.Vector3(0, 1, 0); |
| +добавлено | const smooth = (x) => { x = Math.min(1, Math.max(0, x)); return x * x * (3 - 2 * x); }; |
| +добавлено | |
| +добавлено | // reused option objects: the shared systems destructure options, so a literal per call would allocate |
| +добавлено | const SPL = { x: 0, z: 0, radius: 1, heat: 0, char: 0, wet: 0, frost: 0, crack: 0, acid: 0, corruption: 0, glow: 0, pattern: 'radial', seed: 0, angle: 0, x2: 0, z2: 0, width: 0 }; |
| +добавлено | const SPK = { pos: new THREE.Vector3(), dir: new THREE.Vector3(), height: 1, radius: 0.4, material: 'rock', delay: 0, rise: 0.15, life: 3, crumble: 0.9, heat: 0, seed: 1 }; |
| +добавлено | const SHK = { pos: new THREE.Vector3(), radius0: 0.2, radius1: 6, duration: 0.5, thickness: 0.6, distortion: 0.05, color: null, intensity: 0, ground: true }; |
| +добавлено | |
| +добавлено | // --- boulder geometry (built once at load) --------------------------------------------------- |
| +добавлено | function hash3i(x, y, z, s) { |
| +добавлено | let h = Math.imul(x, 374761393) + Math.imul(y, 668265263) + Math.imul(z, 1440662683) + Math.imul(s, 1274126177) | 0; |
| +добавлено | h = Math.imul(h ^ (h >>> 13), 1274126177); |
| +добавлено | return ((h ^ (h >>> 16)) >>> 0) / 4294967296; |
| +добавлено | } |
| +добавлено | function vnoise(x, y, z, s) { |
| +добавлено | const xi = Math.floor(x), yi = Math.floor(y), zi = Math.floor(z); |
| +добавлено | const u = smooth(x - xi), v = smooth(y - yi), w = smooth(z - zi); |
| +добавлено | const c = (i, j, k) => hash3i(xi + i, yi + j, zi + k, s); |
| +добавлено | const L = (a, b, t) => a + (b - a) * t; |
| +добавлено | return L(L(L(c(0, 0, 0), c(1, 0, 0), u), L(c(0, 1, 0), c(1, 1, 0), u), v), L(L(c(0, 0, 1), c(1, 0, 1), u), L(c(0, 1, 1), c(1, 1, 1), u), v), w); |
| +добавлено | } |
| +добавлено | function fbm(x, y, z, s, oct) { |
| +добавлено | let sum = 0, a = 0.5, n = 0; |
| +добавлено | for (let o = 0; o < oct; o++) { sum += a * vnoise(x, y, z, s + o * 17); n += a; a *= 0.5; x *= 2.03; y *= 2.03; z *= 2.03; } |
| +добавлено | return sum / n; |
| +добавлено | } |
| +добавлено | const smin = (a, b, k) => { const h = Math.max(k - Math.abs(a - b), 0) / k; return Math.min(a, b) - h * h * k * 0.25; }; |
| +добавлено | |
| +добавлено | // A volcanic bomb: an icosphere (`detail` subdivisions per edge) pushed out by broad lumps, cut by |
| +добавлено | // fracture planes (broken faces with rounded edges), split into crust plates by grooves (spherical |
| +добавлено | // voronoi seams, where the molten core shows), with ridged crags and grain on top. Smooth normals. |
| +добавлено | // aCrack = seam (0..1), aAO = cavity occlusion from the depth below the lumpy envelope. |
| +добавлено | function boulderGeometry(detail, seed, plates, grain) { |
| +добавлено | const rand = lcg(seed); |
| +добавлено | const g0 = new THREE.IcosahedronGeometry(1, detail); |
| +добавлено | g0.deleteAttribute('normal'); g0.deleteAttribute('uv'); |
| +добавлено | const g = mergeVertices(g0); |
| +добавлено | const p = g.attributes.position, n = p.count; |
| +добавлено | const planes = []; |
| +добавлено | for (let k = 0; k < 7; k++) { |
| +добавлено | const v = new THREE.Vector3(rand() - 0.5, rand() - 0.5, rand() - 0.5).normalize(); |
| +добавлено | planes.push([v.x, v.y, v.z, 0.7 + rand() * 0.16]); |
| +добавлено | } |
| +добавлено | const pts = []; |
| +добавлено | for (let k = 0; k < plates; k++) { |
| +добавлено | const v = new THREE.Vector3(rand() - 0.5, rand() - 0.5, rand() - 0.5).normalize(); // random: plates of every size |
| +добавлено | pts.push(v.x, v.y, v.z); |
| +добавлено | } |
| +добавлено | const sy = 0.74 + rand() * 0.1, sz = 0.84 + rand() * 0.1; |
| +добавлено | const crack = new Float32Array(n), ao = new Float32Array(n); |
| +добавлено | for (let i = 0; i < n; i++) { |
| +добавлено | let x = p.getX(i), y = p.getY(i), z = p.getZ(i); |
| +добавлено | const l = Math.hypot(x, y, z); x /= l; y /= l; z /= l; |
| +добавлено | let r = (0.92 + 0.5 * (fbm(x * 1.1 + 3.1, y * 1.1, z * 1.1, seed, 3) - 0.5)) * (1 + 0.12 * x); // lumpy, one end heavier |
| +добавлено | for (const [a, b, c, o] of planes) { const d = x * a + y * b + z * c; if (d > 0.05) r = smin(r, o / d, 0.07); } |
| +добавлено | const env = r; |
| +добавлено | let f1 = 9, f2 = 9; |
| +добавлено | for (let k = 0; k < pts.length; k += 3) { |
| +добавлено | const dx = x - pts[k], dy = y - pts[k + 1], dz = z - pts[k + 2], d = dx * dx + dy * dy + dz * dz; |
| +добавлено | if (d < f1) { f2 = f1; f1 = d; } else if (d < f2) f2 = d; |
| +добавлено | } |
| +добавлено | const seam = (1 - smooth((Math.sqrt(f2) - Math.sqrt(f1)) / (0.05 + 0.06 * vnoise(x * 4, y * 4, z * 4, seed + 3)))); |
| +добавлено | r -= 0.05 * seam * seam; |
| +добавлено | const ridge = 1 - Math.abs(2 * fbm(x * 3.3, y * 3.3 + 7.7, z * 3.3, seed + 5, 3) - 1); |
| +добавлено | r += 0.04 * ridge * ridge - 0.018; |
| +добавлено | r += grain * (fbm(x * 11, y * 11, z * 11 + 1.3, seed + 9, 2) - 0.5); |
| +добавлено | p.setXYZ(i, x * r, y * r * sy, z * r * sz); |
| +добавлено | crack[i] = seam; |
| +добавлено | ao[i] = 1 - 0.8 * smooth((env - r - 0.008) / 0.06); |
| +добавлено | } |
| +добавлено | g.setAttribute('aCrack', new THREE.BufferAttribute(crack, 1)); |
| +добавлено | g.setAttribute('aAO', new THREE.BufferAttribute(ao, 1)); |
| +добавлено | g.computeVertexNormals(); |
| +добавлено | g.computeBoundingSphere(); |
| +добавлено | return g; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // Molten-crusted rock (MeshStandardMaterial + onBeforeCompile, opaque pass): a dark basalt crust, |
| +добавлено | // a paler ash skin on the lumps, cavities darker (baked AO on ambient, half on direct light), a |
| +добавлено | // derivative bump from two noise taps for the rough crust; the seams (geometry grooves) and a finer |
| +добавлено | // crazing glow at the blackbody colour of the heat, the face ramming the air hottest; a dull red |
| +добавлено | // halo under the crust around the seams. |
| +добавлено | const BOULDER_PARS = /* glsl */` |
| +добавлено | uniform vec3 uRock; |
| +добавлено | uniform vec4 uBoulder; // heat 0..1, -, -, - |
| +добавлено | uniform vec3 uBoulderVel; // world flight direction (0 when not flying) |
| +добавлено | varying vec3 vObj; |
| +добавлено | varying float vSeed; |
| +добавлено | varying float vCrack; |
| +добавлено | varying float vAO; |
| +добавлено | float gGlow; float gHalo; float gBump; float gRough; |
| +добавлено | `; |
| +добавлено | const BOULDER_ALBEDO = /* glsl */` |
| +добавлено | { |
| +добавлено | vec3 o = vObj + vSeed; |
| +добавлено | vec4 nA = noise3v(o * 0.55); |
| +добавлено | vec4 nB = noise3v(o * 2.1 + 0.37); |
| +добавлено | // crazing: the fbm's zero set, ~constant width in pixels, coverage-preserving with distance |
| +добавлено | float fv = fwidth(nB.r), w = 0.016; |
| +добавлено | float craze = (1.0 - smoothstep(w, w + fv * 1.5, abs(nB.r - 0.5))) * w / (w + 0.75 * fv) * smoothstep(0.4, 0.65, nA.g); |
| +добавлено | // seams: open and hot in places, crusted over in others, so no even network of lines |
| +добавлено | float seam = smoothstep(0.25, 0.9, vCrack) * smoothstep(0.3, 0.62, nA.g + 0.25 * nB.b); |
| +добавлено | gGlow = max(seam, craze * 0.3); |
| +добавлено | gHalo = smoothstep(0.0, 0.75, vCrack); |
| +добавлено | vec3 crust = uRock * (0.6 + 0.8 * nB.g) * (0.8 + 0.4 * nA.r); |
| +добавлено | crust = mix(crust, vec3(0.15, 0.14, 0.13) * (0.75 + 0.5 * nB.b), smoothstep(0.5, 0.8, nA.b) * 0.5); |
| +добавлено | crust *= mix(0.4, 1.0, vAO); |
| +добавлено | diffuseColor.rgb = crust * (1.0 - 0.85 * gGlow); |
| +добавлено | gBump = nB.r * 0.5 + nB.a * 0.3 + nA.r * 0.2 - gGlow * 0.3; |
| +добавлено | gRough = clamp(mix(0.94, 0.45, gGlow) - 0.12 * nB.b, 0.3, 1.0); |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | const BOULDER_BUMP = /* glsl */` |
| +добавлено | { |
| +добавлено | vec3 dpdx = dFdx(-vViewPosition), dpdy = dFdy(-vViewPosition); |
| +добавлено | float dhx = dFdx(gBump), dhy = dFdy(gBump); |
| +добавлено | vec3 r1 = cross(dpdy, normal), r2 = cross(normal, dpdx); |
| +добавлено | float det = dot(dpdx, r1); |
| +добавлено | normal = normalize(abs(det) * normal - sign(det) * (dhx * r1 + dhy * r2) * 0.035); |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | const BOULDER_EMIT = /* glsl */` |
| +добавлено | { |
| +добавлено | float h = uBoulder.x; |
| +добавлено | if (h > 0.001) { |
| +добавлено | vec3 wN = normalize((vec4(normal, 0.0) * viewMatrix).xyz); |
| +добавлено | float lead = dot(wN, uBoulderVel); |
| +добавлено | float hh = clamp(h * (1.0 + 0.55 * max(lead, 0.0) - 0.3 * max(-lead, 0.0)), 0.0, 1.2); |
| +добавлено | float T = mix(800.0, 1420.0, hh), Th = T * 0.78; |
| +добавлено | float R = pow(T / 1500.0, 4.0) * smoothstep(800.0, 1050.0, T); |
| +добавлено | float Rh = pow(Th / 1500.0, 4.0) * smoothstep(720.0, 1050.0, Th); |
| +добавлено | totalEmissiveRadiance += blackbody(T) * (R * 7.0 * gGlow) + blackbody(Th) * (Rh * 0.3 * gHalo); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | const BOULDER_LIGHT = /* glsl */` |
| +добавлено | { |
| +добавлено | reflectedLight.indirectDiffuse *= vAO; |
| +добавлено | reflectedLight.indirectSpecular *= vAO; |
| +добавлено | reflectedLight.directDiffuse *= mix(0.5, 1.0, vAO); |
| +добавлено | vec3 wN = normalize((vec4(normal, 0.0) * viewMatrix).xyz); |
| +добавлено | vec3 wP = cameraPosition + (vec4(-vViewPosition, 0.0) * viewMatrix).xyz; |
| +добавлено | vec3 sd, ss; |
| +добавлено | gLightNoShadow = true; |
| +добавлено | spellLightPBR(wP, wN, normalize(cameraPosition - wP), material.roughness, material.specularColor, material.diffuseColor, sd, ss); |
| +добавлено | reflectedLight.directDiffuse += sd * vAO; |
| +добавлено | reflectedLight.directSpecular += ss; |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | function boulderMaterial(own) { |
| +добавлено | const mat = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.9, metalness: 0 }); |
| +добавлено | mat.onBeforeCompile = (sh) => { |
| +добавлено | Object.assign(sh.uniforms, U, own); |
| +добавлено | sh.vertexShader = sh.vertexShader |
| +добавлено | .replace('#include <common>', '#include <common>\nattribute float aCrack;\nattribute float aAO;\nvarying vec3 vObj;\nvarying float vSeed;\nvarying float vCrack;\nvarying float vAO;') |
| +добавлено | .replace('#include <begin_vertex>', '#include <begin_vertex>\nvObj = position;\nvSeed = float(gl_InstanceID) * 7.31;\nvCrack = aCrack;\nvAO = aAO;'); |
| +добавлено | sh.fragmentShader = sh.fragmentShader |
| +добавлено | .replace('#include <common>', '#include <common>\n' + COMMON_GLSL + NOISE_GLSL + WORLD_GLSL + LIGHTS_GLSL + BOULDER_PARS) |
| +добавлено | .replace('#include <color_fragment>', '#include <color_fragment>\n' + BOULDER_ALBEDO) |
| +добавлено | .replace('#include <roughnessmap_fragment>', 'float roughnessFactor = gRough;') |
| +добавлено | .replace('#include <normal_fragment_maps>', '#include <normal_fragment_maps>\n' + BOULDER_BUMP) |
| +добавлено | .replace('#include <emissivemap_fragment>', '#include <emissivemap_fragment>\n' + BOULDER_EMIT) |
| +добавлено | .replace('#include <lights_fragment_end>', '#include <lights_fragment_end>\n' + BOULDER_LIGHT); |
| +добавлено | }; |
| +добавлено | mat.customProgramCacheKey = () => 'ue-earth-boulder2'; |
| +добавлено | return mat; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // ground heat under live spikes: PROBES texels, read back async (PBO + fence, 2-3 frames late) |
| +добавлено | const PROBE_FRAG = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | uniform sampler2D uGroundA; |
| +добавлено | uniform vec4 uGroundRect; |
| +добавлено | uniform vec2 uProbe[${PROBES}]; |
| +добавлено | out vec4 outColor; |
| +добавлено | void main() { outColor = texture(uGroundA, (uProbe[int(gl_FragCoord.x)] - uGroundRect.xy) * uGroundRect.w); }`; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'earth', name: 'Earth', key: '5', hue: '#c08a4a', |
| +добавлено | shape: 'projectile', forms: ['Upheaval', 'Meteor'], speed: 8, radius: 0.5, |
| +добавлено | params: { |
| +добавлено | size: 1, height: 2.4, spikes: 7, stagger: 0.03, hold: 3, debris: 1, dust: 1, crack: 1, shake: 1, |
| +добавлено | heat: 0, glow: 0, meteorSize: 1, arc: 3.5, meteorHeat: 0.6, rim: 6, |
| +добавлено | rockColor: '#3b342d', dustColor: '#696159', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | size: [0.4, 2, 0.01], height: [0.8, 4.5, 0.01], spikes: [3, MAX_SPIKES, 1], stagger: [0, 0.12, 0.001], hold: [0.5, 8, 0.1], |
| +добавлено | debris: [0, 3, 0.01], dust: [0, 3, 0.01], crack: [0, 2, 0.01], shake: [0, 2, 0.01], heat: [0, 1, 0.01], glow: [0, 2, 0.01], |
| +добавлено | meteorSize: [0.5, 2, 0.01], arc: [1, 8, 0.1], meteorHeat: [0, 1, 0.01], rim: [0, MAX_RIM, 1], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | molten: { heat: 0.85, meteorHeat: 1, glow: 1, dustColor: '#4a3c35', rockColor: '#2e2622' }, |
| +добавлено | desert: { dustColor: '#b39573', rockColor: '#6b5643', dust: 1.5, crack: 0.7 }, |
| +добавлено | tectonic: { spikes: 11, height: 3.4, size: 1.3, stagger: 0.045, shake: 1.4, debris: 1.4, meteorSize: 1.6, rim: 10 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 0.5, impact: 0.35, linger: 4.3 }, |
| +добавлено | kinds: { |
| +добавлено | // debris (instanced rock meshes in the opaque pass): color[1] = emission, alpha[1] = roughness |
| +добавлено | pebble: { style: 'debris', mesh: 'rock', gravity: 11, drag: 0.1, restitution: 0.45, friction: 0.5, life: [0.9, 1.6], size: [0.05, 0.05], spin: 9, color: ['#4a4038', '#000000'], alpha: [1, 0.85], fadeOut: 0.25, forceResponse: 0 }, |
| +добавлено | rock: { style: 'debris', mesh: 'rock', gravity: 11, drag: 0.08, restitution: 0.3, friction: 0.6, life: [3.2, 4.8], size: [0.09, 0.09], spin: 7, color: ['#3b342d', '#000000'], alpha: [1, 0.9], fadeOut: 0.12, forceResponse: 0 }, |
| +добавлено | chunk: { style: 'debris', mesh: 'rock', gravity: 11, drag: 0.05, restitution: 0.22, friction: 0.7, life: [3.6, 5], size: [0.19, 0.19], spin: 4, color: ['#3b342d', '#000000'], alpha: [1, 0.9], fadeOut: 0.12, forceResponse: 0 }, |
| +добавлено | magma: { style: 'debris', mesh: 'rock', gravity: 11, drag: 0.08, restitution: 0.25, friction: 0.65, life: [2.6, 4], size: [0.13, 0.13], spin: 6, color: ['#1c130e', new THREE.Color(5, 0.85, 0.1)], alpha: [1, 0.7], fadeOut: 0.15, forceResponse: 0, paints: 'heat', paintAmount: 0.05 }, |
| +добавлено | gather: { style: 'debris', mesh: 'rock', gravity: 2, drag: 0.6, restitution: -1, life: [0.6, 1.0], size: [0.11, 0.07], spin: 10, color: ['#3b342d', '#000000'], alpha: [1, 0.9], fadeOut: 0.1, forceResponse: 1 }, |
| +добавлено | // media (half-res WBOIT spherical billboards) |
| +добавлено | dust: { style: 'media', drag: 2.4, buoyancy: -0.35, curl: 0.5, curlScale: 0.18, restitution: 0, friction: 0, life: [2.4, 4.2], size: [0.45, 2.0], sizePow: 0.55, color: ['#5f4e3e', '#4e4034'], alpha: [0.1875, 0.0875], density: 2.0, erosion: 0.55, fadeIn: 0.05, fadeOut: 0.6, forceResponse: 0.5, lightResponse: 1.5 }, |
| +добавлено | // Upheaval's own dust and plume: the same media at half the count and 1.75x the alpha (about the |
| +добавлено | // same opacity through the cloud, half as many overlapping billboards: they were 2.2 ms of overdraw at |
| +добавлено | // its peak). Its own kinds, so Meteor and Starfall (which borrows earth.dust) keep theirs. |
| +добавлено | updust: { style: 'media', drag: 2.4, buoyancy: -0.35, curl: 0.5, curlScale: 0.18, restitution: 0, friction: 0, life: [2.4, 4.2], size: [0.45, 2.0], sizePow: 0.55, color: ['#5f4e3e', '#4e4034'], alpha: [0.33, 0.155], density: 2.0, erosion: 0.55, fadeIn: 0.05, fadeOut: 0.6, forceResponse: 0.5, lightResponse: 1.5 }, |
| +добавлено | upplume: { style: 'media', drag: 1.8, buoyancy: 0.35, curl: 0.7, curlScale: 0.2, life: [2, 3.4], size: [0.35, 1.7], sizePow: 0.6, color: ['#5f4e3e', '#4e4034'], alpha: [0.27, 0.11], density: 1.8, erosion: 0.6, fadeIn: 0.06, fadeOut: 0.6, forceResponse: 0.6, lightResponse: 1.5 }, |
| +добавлено | plume: { style: 'media', drag: 1.8, buoyancy: 0.35, curl: 0.7, curlScale: 0.2, life: [2, 3.4], size: [0.35, 1.7], sizePow: 0.6, color: ['#5f4e3e', '#4e4034'], alpha: [0.15, 0.0625], density: 1.8, erosion: 0.6, fadeIn: 0.06, fadeOut: 0.6, forceResponse: 0.6, lightResponse: 1.5 }, |
| +добавлено | seep: { style: 'media', drag: 1.8, buoyancy: 0.5, curl: 0.8, curlScale: 0.3, life: [0.9, 1.7], size: [0.2, 0.9], color: ['#6e5c49', '#5c4e40'], alpha: [0.1375, 0], density: 2, erosion: 0.55, fadeIn: 0.15, forceResponse: 0.5 }, |
| +добавлено | trail: { style: 'media', drag: 2.2, buoyancy: 0.2, curl: 0.6, curlScale: 0.25, life: [0.7, 1.4], size: [0.25, 0.9], color: ['#6e5c49', '#5c4e40'], alpha: [0.075, 0], density: 1.4, erosion: 0.6, forceResponse: 0.5 }, |
| +добавлено | // glow |
| +добавлено | ember: { style: 'glow', drag: 0.8, buoyancy: 0.6, gravity: 3, curl: 1.5, curlScale: 0.4, life: [0.5, 1.3], size: [0.035, 0.012], stretch: 0.02, emissive: 3, temp: [1800, 900], restitution: 0.3, friction: 0.4 }, |
| +добавлено | // the meteor's wake and impact: smoke boiling off the crust, an invisible heat shimmer (media |
| +добавлено | // that only writes distortion), molten spatter, and a blast of incandescent dust turning to smoke |
| +добавлено | smoke: { style: 'media', drag: 1.5, buoyancy: 0.55, curl: 0.9, curlScale: 0.25, life: [1.8, 3.2], size: [0.35, 2.2], sizePow: 0.5, color: ['#2e2a27', '#3e3934'], alpha: [0.22, 0.06], density: 2.6, erosion: 0.6, fadeIn: 0.08, fadeOut: 0.55, forceResponse: 0.6, lightResponse: 1.3 }, |
| +добавлено | haze: { style: 'media', drag: 2.4, buoyancy: 0.9, curl: 1.2, curlScale: 0.4, life: [0.35, 0.7], size: [0.45, 1.0], color: ['#7f7f7f', '#7f7f7f'], alpha: [0.006, 0], density: 1, erosion: 0.3, distortion: 90, fadeIn: 0.1, fadeOut: 0.5, forceResponse: 0.3 }, |
| +добавлено | spatter: { style: 'glow', drag: 0.35, gravity: 9.8, life: [0.4, 0.9], size: [0.028, 0.012], stretch: 0.035, emissive: 5, temp: [1550, 900], restitution: 0.15, friction: 0.7 }, |
| +добавлено | blast: { style: 'media', drag: 2.3, buoyancy: 0.8, curl: 1.0, curlScale: 0.3, life: [1.0, 2.2], size: [0.5, 2.1], sizePow: 0.5, emissive: 0.4, temp: [1350, 650], toSmokeAt: 0.12, smokeAlbedo: 0.1, alpha: [0.3, 0.05], density: 2.4, erosion: 0.7, fadeIn: 0.03, fadeOut: 0.5, forceResponse: 0.5 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles, gl = fx.renderer.getContext(); |
| +добавлено | // emitters: reusable handles, fields set right before each burst / rate |
| +добавлено | const pebbleLine = P.emitter('earth.pebble', { shape: 'line', radius: 0.12, spread: 0.9 }); |
| +добавлено | const seepLine = P.emitter('earth.seep', { shape: 'line', radius: 0.1, spread: 0.25 }); |
| +добавлено | const rockCone = P.emitter('earth.rock', { shape: 'cone', radius: 0.3, spread: 1.2, radial: 6, cone: 0.8 }); |
| +добавлено | const rockRing = P.emitter('earth.rock', { shape: 'ring', radius: 0.5, spread: 1.7, radial: 2.6 }); |
| +добавлено | const chunkRing = P.emitter('earth.chunk', { shape: 'ring', radius: 0.5, spread: 0.8, radial: 2.5 }); |
| +добавлено | const dustRing = P.emitter('earth.dust', { shape: 'ring', radius: 0.6, spread: 0.8, radial: 6.5 }); |
| +добавлено | const plume = P.emitter('earth.plume', { shape: 'sphere', radius: 0.4, spread: 1.0 }); |
| +добавлено | const upRing = P.emitter('earth.updust', { shape: 'ring', radius: 0.6, spread: 0.8, radial: 6.5 }); |
| +добавлено | const upPlume = P.emitter('earth.upplume', { shape: 'sphere', radius: 0.4, spread: 1.0 }); |
| +добавлено | const upPuff = P.emitter('earth.updust', { shape: 'sphere', radius: 0.4, spread: 0.8, radial: 1.5 }); |
| +добавлено | const rockLine = P.emitter('earth.rock', { shape: 'line', radius: 0.15, spread: 1.0 }); |
| +добавлено | const chunkLine = P.emitter('earth.chunk', { shape: 'line', radius: 0.15, spread: 0.7 }); |
| +добавлено | const magmaLine = P.emitter('earth.magma', { shape: 'line', radius: 0.15, spread: 1.0 }); |
| +добавлено | const magmaBurst = P.emitter('earth.magma', { shape: 'sphere', radius: 0.4, spread: 2, radial: 5 }); |
| +добавлено | const puff = P.emitter('earth.dust', { shape: 'sphere', radius: 0.4, spread: 0.8, radial: 1.5 }); |
| +добавлено | const gather = P.emitter('earth.gather', { shape: 'ring', radius: 2.6, spread: 0.5, radial: -4 }); |
| +добавлено | const gatherDust = P.emitter('earth.seep', { shape: 'ring', radius: 2.6, spread: 0.4 }); |
| +добавлено | const trailDust = P.emitter('earth.trail', { shape: 'sphere', radius: 0.4, spread: 0.4 }); |
| +добавлено | const trailPebble = P.emitter('earth.pebble', { shape: 'sphere', radius: 0.4, spread: 0.6 }); |
| +добавлено | const ember = P.emitter('earth.ember', { shape: 'sphere', radius: 0.45, spread: 1.2 }); |
| +добавлено | const tear = P.emitter('earth.pebble', { shape: 'sphere', radius: 0.25, spread: 1.6 }); |
| +добавлено | const bigChunk = P.emitter('earth.chunk', { shape: 'sphere', radius: 0.4, spread: 1.5, radial: 4 }); |
| +добавлено | const smoke = P.emitter('earth.smoke', { shape: 'sphere', radius: 0.35, spread: 0.5 }); |
| +добавлено | const haze = P.emitter('earth.haze', { shape: 'sphere', radius: 0.4, spread: 0.3 }); |
| +добавлено | const spatter = P.emitter('earth.spatter', { shape: 'shell', radius: 0.45, spread: 1.6, radial: 2.5 }); |
| +добавлено | const blast = P.emitter('earth.blast', { shape: 'sphere', radius: 0.6, spread: 1.2, radial: 3 }); |
| +добавлено | |
| +добавлено | // --- per-cast state, preallocated (c.data.e = states[c.index]) --- |
| +добавлено | const spikeState = () => ({ h: null, seed: 0, pos: new THREE.Vector3(), dir: new THREE.Vector3(), height: 1, radius: 0.4, delay: 0, life: 3, u: 0, rim: false, erupted: false, crumbled: false, ground: 0, heat: 0, magma: false }); |
| +добавлено | const states = Array.from({ length: STATES }, () => ({ |
| +добавлено | alive: false, spikes: Array.from({ length: MAX_SPIKES + MAX_RIM }, spikeState), n: 0, |
| +добавлено | start: new THREE.Vector3(), end: new THREE.Vector3(), fwd: new THREE.Vector3(), perp: new THREE.Vector3(), |
| +добавлено | cStart: new THREE.Vector3(), cEnd: new THREE.Vector3(), head: new THREE.Vector3(), splatT: 0, impactAge: 0, |
| +добавлено | light: null, attract: null, boulder: null, center: new THREE.Vector3(), rot: new THREE.Quaternion(), axis: new THREE.Vector3(), |
| +добавлено | heat: 0, magmaCount: 0, size: 1, fl: null, flAge: 0, |
| +добавлено | })); |
| +добавлено | |
| +добавлено | // --- boulders: the finished rock (one instance, ~20k triangles) and the fragments that fly in |
| +добавлено | // to build it (one instanced draw); one material per boulder, so one program for both --- |
| +добавлено | // fragments are modelled at 0.3 scale so the shader's object-space noise is the same size on both |
| +добавлено | const rockGeo = boulderGeometry(31, 7, 26, 0.012), fragGeo = boulderGeometry(6, 13, 9, 0.02).scale(FRAG, FRAG, FRAG); |
| +добавлено | const slotDirs = []; |
| +добавлено | for (let j = 0; j < CHUNKS; j++) { // fibonacci sphere |
| +добавлено | const y = 1 - (j + 0.5) / CHUNKS * 2, r = Math.sqrt(1 - y * y), a = j * 2.39996; |
| +добавлено | slotDirs.push(new THREE.Vector3(Math.cos(a) * r, y, Math.sin(a) * r)); |
| +добавлено | } |
| +добавлено | const boulders = []; |
| +добавлено | for (let b = 0; b < BOULDERS; b++) { |
| +добавлено | const own = { uRock: { value: new THREE.Color() }, uBoulder: { value: new THREE.Vector4() }, uBoulderVel: { value: new THREE.Vector3() } }; |
| +добавлено | const mat = boulderMaterial(own); |
| +добавлено | const core = new THREE.InstancedMesh(rockGeo, mat, 1), frags = new THREE.InstancedMesh(fragGeo, mat, CHUNKS); |
| +добавлено | for (const m of [core, frags]) { |
| +добавлено | m.instanceMatrix.setUsage(THREE.DynamicDrawUsage); |
| +добавлено | m.frustumCulled = false; |
| +добавлено | m.visible = false; |
| +добавлено | fx.scenes.opaque.add(m); |
| +добавлено | } |
| +добавлено | const rnd = lcg(b * 977 + 3); |
| +добавлено | const chunks = Array.from({ length: CHUNKS }, (_, j) => ({ |
| +добавлено | slot: slotDirs[j].clone(), |
| +добавлено | q: new THREE.Quaternion().setFromEuler(new THREE.Euler(rnd() * 6.3, rnd() * 6.3, rnd() * 6.3)), |
| +добавлено | scale: new THREE.Vector3(1, 0.7 + rnd() * 0.4, 0.8 + rnd() * 0.35).multiplyScalar((0.17 + rnd() * 0.09) / FRAG), |
| +добавлено | from: new THREE.Vector3(), t0: 0, launched: false, arrived: false, |
| +добавлено | })); |
| +добавлено | boulders.push({ core, frags, own, chunks, busy: false }); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- heat probe (local: groundAt only covers dummies and cast targets) --- |
| +добавлено | const probeXZ = Array.from({ length: PROBES }, () => new THREE.Vector2()); |
| +добавлено | const probeOwner = new Array(PROBES).fill(null), probeSeed = new Int32Array(PROBES); |
| +добавлено | const probeOut = new Float32Array(PROBES * 4); |
| +добавлено | const probeRT = new THREE.WebGLRenderTarget(PROBES, 1, { type: THREE.FloatType, minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, depthBuffer: false, generateMipmaps: false }); |
| +добавлено | const probeQuad = new FullscreenQuad(rawPass(PROBE_FRAG, { uGroundA: U.uGroundA, uGroundRect: U.uGroundRect, uProbe: { value: probeXZ } })); |
| +добавлено | const pbo = gl.createBuffer(); // storage at the first readPixels (see GroundField's probes) |
| +добавлено | let sync = null, probeN = 0, sized = false; |
| +добавлено | { const prev = fx.renderer.getRenderTarget(); probeQuad.render(fx.renderer, probeRT); fx.renderer.setRenderTarget(prev); } // compile now, not mid-cast |
| +добавлено | |
| +добавлено | const magmaCol = blackbodyRGB(1300, new THREE.Color()); |
| +добавлено | |
| +добавлено | // the impact cloud: the shared combustion fluid, its soot channel carrying the dust |
| +добавлено | const fluid = FluidVolume.get(fx); |
| +добавлено | // shares the fluid's render program, so warming this spell alone (phones: on selection) |
| +добавлено | // compiles it; the domains themselves belong to whichever spell created the fluid first |
| +добавлено | const fluidStub = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), fluid.domains[0].material); |
| +добавлено | fluidStub.visible = false; fluidStub.frustumCulled = false; |
| +добавлено | fx.scenes.fx.add(fluidStub); |
| +добавлено | const SRC = { pos: new THREE.Vector3(), vel: new THREE.Vector3(), radius: 1, fuel: 0, temp: 0, soot: 0, radial: 0, noise: 0, noiseScale: 1, seed: 0, coupling: 0, burst: false }; |
| +добавлено | function src(f, x, y, z, radius, fuel, temp, soot, burst, noise, noiseScale, seed, vx, vy, vz, radial, coupling) { |
| +добавлено | SRC.pos.set(x, y, z); SRC.radius = radius; SRC.fuel = fuel; SRC.temp = temp; SRC.soot = soot; SRC.burst = burst; |
| +добавлено | SRC.noise = noise; SRC.noiseScale = noiseScale; SRC.seed = seed; SRC.vel.set(vx, vy, vz); SRC.radial = radial; SRC.coupling = coupling; |
| +добавлено | f.emit(SRC); |
| +добавлено | } |
| +добавлено | const lightCol = new THREE.Color(); |
| +добавлено | let dustColor = '', rockColor = ''; |
| +добавлено | |
| +добавлено | function splat(x, z, radius, pattern, crack, heat, char, seed, x2 = x, z2 = z, width = 0) { |
| +добавлено | SPL.x = x; SPL.z = z; SPL.radius = radius; SPL.pattern = pattern; SPL.crack = crack; SPL.heat = heat; SPL.char = char; |
| +добавлено | SPL.seed = seed; SPL.x2 = x2; SPL.z2 = z2; SPL.width = width; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | function shock(pos, r1, duration, distortion) { |
| +добавлено | SHK.pos.copy(pos).setY(0.04); SHK.radius1 = r1; SHK.duration = duration; SHK.distortion = distortion; SHK.thickness = 0.35 + r1 * 0.08; |
| +добавлено | fx.shocks?.spawn(SHK); |
| +добавлено | } |
| +добавлено | function hitDummies(pos, reach, strength, push) { |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dist = Math.hypot(dm.pos.x - pos.x, dm.pos.z - pos.z); |
| +добавлено | if (dist < reach) dm.hit(_c.subVectors(dm.pos, pos).setY(0).normalize().addScaledVector(push, 0.6), strength * (1 - dist / reach)); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // colour params feed the kind table only when they change (setKind allocates a little) |
| +добавлено | function syncColors(p) { |
| +добавлено | if (p.dustColor !== dustColor) { |
| +добавлено | dustColor = p.dustColor; |
| +добавлено | const a = new THREE.Color(dustColor), b = a.clone().multiplyScalar(0.82); |
| +добавлено | for (const k of ['dust', 'plume', 'seep', 'trail', 'updust', 'upplume']) P.setKind(`earth.${k}`, { color: [a, b] }); |
| +добавлено | } |
| +добавлено | if (p.rockColor !== rockColor) { |
| +добавлено | rockColor = p.rockColor; |
| +добавлено | const a = new THREE.Color(rockColor); |
| +добавлено | for (const k of ['rock', 'chunk', 'gather']) P.setKind(`earth.${k}`, { color: [a, '#000000'] }); |
| +добавлено | P.setKind('earth.pebble', { color: [a.clone().multiplyScalar(1.25), '#000000'] }); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function spawnSpike(c, d, s, delay, life, heat) { |
| +добавлено | SPK.pos.copy(s.pos); SPK.dir.copy(s.dir); SPK.height = s.height; SPK.radius = s.radius; |
| +добавлено | SPK.delay = delay; SPK.life = life; SPK.heat = heat; SPK.seed = s.seed; |
| +добавлено | s.h = fx.spikes.spawn(SPK); |
| +добавлено | s.delay = delay; s.life = life; s.erupted = s.crumbled = s.magma = false; s.ground = 0; s.heat = heat; |
| +добавлено | } |
| +добавлено | const owns = (s) => s.h && s.h.active && s.h.seed === s.seed; |
| +добавлено | |
| +добавлено | // --- Upheaval --------------------------------------------------------------------------- |
| +добавлено | function layoutLine(c, p, d) { |
| +добавлено | const n = Math.round(p.spikes); |
| +добавлено | d.n = n; |
| +добавлено | d.fwd.set(c.to.x - c.from.x, 0, c.to.z - c.from.z); |
| +добавлено | const dist = d.fwd.length(); |
| +добавлено | d.fwd.multiplyScalar(1 / Math.max(dist, 1e-3)); |
| +добавлено | if (dist < 1e-3) d.fwd.set(0, 0, -1); |
| +добавлено | d.perp.set(-d.fwd.z, 0, d.fwd.x); |
| +добавлено | const endD = Math.max(dist - (c.target >= 0 ? 1.0 : 0), 1.2); |
| +добавлено | const startD = Math.min(Math.max(dist - 10, 1.8), endD - 0.6); |
| +добавлено | d.start.set(c.from.x, 0, c.from.z).addScaledVector(d.fwd, startD); |
| +добавлено | d.end.set(c.from.x, 0, c.from.z).addScaledVector(d.fwd, endD); |
| +добавлено | d.cStart.copy(d.start).addScaledVector(d.fwd, -0.7); |
| +добавлено | d.cEnd.copy(d.end).addScaledVector(d.fwd, 0.5); |
| +добавлено | for (let k = 0; k < n; k++) { |
| +добавлено | const s = d.spikes[k], u = n > 1 ? k / (n - 1) : 1, side = k % 2 ? 1 : -1; |
| +добавлено | s.u = u; s.rim = false; |
| +добавлено | s.pos.lerpVectors(d.start, d.end, u).addScaledVector(d.perp, side * (0.15 + 0.22 * fx.rand()) * p.size * (k === n - 1 ? 0.3 : 1)); |
| +добавлено | s.height = p.height * p.size * (0.42 + 0.58 * Math.pow(u, 0.8)) * (0.85 + 0.3 * fx.rand()); |
| +добавлено | s.radius = p.size * (0.3 + 0.25 * u) * (0.9 + 0.2 * fx.rand()); |
| +добавлено | // lean forward (the wave's direction) and away from the line |
| +добавлено | s.dir.copy(UP).addScaledVector(d.fwd, k === n - 1 ? 0.16 : 0.2 + 0.18 * fx.rand()).addScaledVector(d.perp, side * (0.1 + 0.12 * fx.rand())).normalize(); |
| +добавлено | s.seed = ((c.seed * 31 + k * 7919) >>> 0) & 0xffffff || 1; |
| +добавлено | s.h = null; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function erupt(c, p, d, s, last) { |
| +добавлено | const b = s.pos, k = 0.6 + 0.8 * s.u, R = s.radius; |
| +добавлено | splat(b.x, b.z, R * 2.6, 'radial', 1.7 * p.crack, 0.6 * p.heat, 0.15, s.seed % 89); |
| +добавлено | // rock flung up and out from around the base (outside the spike's capsule: inside it they'd |
| +добавлено | // ride up the invisible part of the capsule and rest in mid-air), chunks shoved aside |
| +добавлено | rockRing.pos.copy(b).setY(0.2); rockRing.radius = R * 1.05; rockRing.radial = 2.6 * Math.sqrt(p.size); |
| +добавлено | rockRing.vel.copy(d.fwd).multiplyScalar(0.8).setY((4.5 + 2 * s.u) * Math.sqrt(p.size)); rockRing.size = p.size; rockRing.burst(74 * p.debris * k); |
| +добавлено | chunkRing.pos.copy(b).setY(0.2); chunkRing.radius = R * 0.9; chunkRing.vel.set(0, 2.6, 0); chunkRing.size = p.size; chunkRing.burst(8 * p.debris * k); |
| +добавлено | if (s.h && s.h.heat > 0.3) { magmaBurst.pos.copy(b).setY(0.3); magmaBurst.radius = R * 0.6; magmaBurst.vel.set(0, 3, 0); magmaBurst.burst(20 * p.debris * k); } |
| +добавлено | // dust: a ring that rolls outward along the floor, a plume thrown up with the spike |
| +добавлено | upRing.pos.copy(b).setY(0.25); upRing.radius = R * 1.1; upRing.vel.set(0, 0.3, 0); upRing.radial = 6.5 * Math.sqrt(p.size); upRing.size = p.size; |
| +добавлено | upRing.burst(70 * p.dust * k); |
| +добавлено | upPlume.pos.copy(b).setY(0.6); upPlume.vel.copy(s.dir).multiplyScalar(4.2 * Math.sqrt(p.size)); upPlume.radius = R; upPlume.size = p.size; upPlume.burst(30 * p.dust * k); |
| +добавлено | fx.rig.shake(0.09 * p.shake * (0.5 + s.u), 7); |
| +добавлено | if (last) { |
| +добавлено | shock(b, 7 * p.size, 0.55, 0.06); |
| +добавлено | fx.rig.shake(0.35 * p.shake, 6); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(2.5, 0.12); fx.rig.hitstop(0.04); } |
| +добавлено | if (d.light) { d.light.intensity = 30 * p.glow; d.light.pos.copy(b).setY(0.6); } |
| +добавлено | } else if (s.u > 0.3 && Math.round(s.u * 6) % 2 === 0) shock(b, 2.6 * p.size, 0.4, 0.03); |
| +добавлено | hitDummies(b, R + 1.1, last ? 1.6 : 1.0, d.fwd); |
| +добавлено | s.erupted = true; |
| +добавлено | } |
| +добавлено | |
| +добавлено | function crumble(p, s) { |
| +добавлено | const b = s.pos, top = s.h.top(_a), hot = s.h.heat > 0.3, R = s.radius; |
| +добавлено | const line = hot ? magmaLine : rockLine; |
| +добавлено | line.pos.copy(b).setY(0.3); line.p.copy(top); line.radius = R * 0.45; line.vel.set(0, 0.3, 0); line.size = p.size; |
| +добавлено | line.burst(34 * p.debris * (0.5 + s.u)); |
| +добавлено | if (hot) { rockLine.pos.copy(b).setY(0.3); rockLine.p.copy(top); rockLine.radius = R * 0.45; rockLine.vel.set(0, 0.3, 0); rockLine.burst(14 * p.debris); } |
| +добавлено | chunkLine.pos.copy(b).setY(0.3); chunkLine.p.copy(top); chunkLine.radius = R * 0.4; chunkLine.vel.set(0, 0.2, 0); chunkLine.size = p.size; |
| +добавлено | chunkLine.burst(7 * p.debris * (0.5 + s.u)); |
| +добавлено | upPuff.pos.copy(b).setY(0.4); upPuff.radius = R; upPuff.vel.set(0, 0.2, 0); upPuff.radial = 2; upPuff.size = p.size * 0.8; upPuff.burst(14 * p.dust); |
| +добавлено | s.crumbled = true; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // heat under each spike -> spike.heat (rock holds heat longer than the floor) + magma decals |
| +добавлено | function heatSpikes(p, d, dt, heat0) { |
| +добавлено | const total = d.n + (d.rimN ?? 0); |
| +добавлено | for (let i = 0; i < total; i++) { |
| +добавлено | const s = d.spikes[i]; |
| +добавлено | if (!owns(s)) continue; |
| +добавлено | s.heat = Math.max(s.heat * Math.exp(-dt / 6), Math.min(1, s.ground * 1.3), heat0); |
| +добавлено | s.h.heat = s.heat; |
| +добавлено | if (!s.magma && s.heat > 0.3 && d.magmaCount < 3) { |
| +добавлено | fx.impacts.add('magma', _a.copy(s.pos).setY(0.3), s.radius * 2.6, Math.min(1, s.heat), 6, magmaCol); |
| +добавлено | s.magma = true; d.magmaCount++; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- Meteor ------------------------------------------------------------------------------ |
| +добавлено | function setupBoulder(c, p, d) { |
| +добавлено | let B = null; |
| +добавлено | for (const x of boulders) if (!x.busy) { B = x; break; } |
| +добавлено | d.boulder = B; |
| +добавлено | d.size = p.meteorSize; |
| +добавлено | d.center.copy(c.from).setY(c.from.y + 0.75 * p.meteorSize); |
| +добавлено | d.rot.identity(); |
| +добавлено | if (!B) return; |
| +добавлено | B.busy = true; |
| +добавлено | B.core.visible = B.frags.visible = true; |
| +добавлено | _col.set(p.rockColor); B.own.uRock.value.copy(_col); |
| +добавлено | B.own.uBoulder.value.set(0, 0, 0, 0); |
| +добавлено | B.own.uBoulderVel.value.set(0, 0, 0); |
| +добавлено | const T = c.durations.charge; |
| +добавлено | for (let j = 0; j < CHUNKS; j++) { |
| +добавлено | const ch = B.chunks[j], a = j * 2.39996 + c.seed * 0.001, r = 2.1 + fx.rand() * 1.1; |
| +добавлено | ch.from.set(d.center.x + Math.cos(a) * r, 0.05, d.center.z + Math.sin(a) * r); |
| +добавлено | ch.t0 = 0.04 * T + (j / (CHUNKS - 1)) * 0.6 * T + fx.rand() * 0.04; |
| +добавлено | ch.launched = ch.arrived = false; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // charge: the core swells while fragments are torn out of the floor, fly in and are pressed |
| +добавлено | // into it; in flight only the finished rock is drawn |
| +добавлено | function poseBoulder(c, p, d, charging) { |
| +добавлено | const B = d.boulder; |
| +добавлено | if (!B) return; |
| +добавлено | const S = d.size, T = c.durations.charge, center = c.phase === 'charge' ? d.center : c.pos; |
| +добавлено | const grow = charging ? 0.35 + 0.65 * smooth(c.t / (0.88 * T)) : 1, R = CORE_R * S * grow; |
| +добавлено | _m.compose(center, d.rot, _s.setScalar(R)); |
| +добавлено | B.core.setMatrixAt(0, _m); |
| +добавлено | B.core.instanceMatrix.needsUpdate = true; |
| +добавлено | B.frags.visible = charging; |
| +добавлено | if (!charging) return; |
| +добавлено | const fl = 0.32; |
| +добавлено | for (let j = 0; j < CHUNKS; j++) { |
| +добавлено | const ch = B.chunks[j], u = (c.t - ch.t0) / fl; |
| +добавлено | _a.copy(ch.slot).multiplyScalar(R * 0.95).applyQuaternion(d.rot).add(center); // where it lands on the core |
| +добавлено | _q.copy(d.rot).multiply(ch.q); |
| +добавлено | _s.copy(ch.scale).multiplyScalar(S); |
| +добавлено | if (u < 0) _s.setScalar(0); |
| +добавлено | else if (u < 1) { |
| +добавлено | if (!ch.launched) { // torn out of the floor |
| +добавлено | ch.launched = true; |
| +добавлено | tear.pos.copy(ch.from); tear.vel.set(0, 2.5, 0); tear.size = S; tear.burst(10 * p.debris); |
| +добавлено | puff.pos.copy(ch.from).setY(0.3); puff.radius = 0.3; puff.vel.set(0, 0.6, 0); puff.radial = 1.2; puff.size = 0.6; puff.burst(5 * p.dust); |
| +добавлено | splat(ch.from.x, ch.from.z, 0.5 * S, 'radial', 1.3 * p.crack, 0, 0.1, j * 7); |
| +добавлено | } |
| +добавлено | const e = u * u; // pulled in: accelerates into its slot |
| +добавлено | _b.copy(ch.from).lerp(_a, e); |
| +добавлено | _b.y += 1.1 * 4 * e * (1 - e); |
| +добавлено | _a.copy(_b); |
| +добавлено | _q2.setFromAxisAngle(UP, (1 - e) * 5); _q.multiply(_q2); |
| +добавлено | } else { |
| +добавлено | if (!ch.arrived) { |
| +добавлено | ch.arrived = true; |
| +добавлено | fx.rig.shake(0.035 * p.shake, 10); |
| +добавлено | tear.pos.copy(_a); tear.vel.set(0, 0.5, 0); tear.size = 0.6 * S; tear.burst(4 * p.debris); |
| +добавлено | } |
| +добавлено | const v = Math.min(1, (u - 1) * fl / 0.2); // pressed in: sinks into the core and merges |
| +добавлено | _a.lerp(center, 0.45 * v); |
| +добавлено | _s.multiplyScalar(1 - v * v); |
| +добавлено | } |
| +добавлено | _m.compose(_a, _q, _s); |
| +добавлено | B.frags.setMatrixAt(j, _m); |
| +добавлено | } |
| +добавлено | B.frags.instanceMatrix.needsUpdate = true; |
| +добавлено | } |
| +добавлено | |
| +добавлено | function freeBoulder(d) { |
| +добавлено | if (!d.boulder) return; |
| +добавлено | d.boulder.core.visible = d.boulder.frags.visible = false; |
| +добавлено | d.boulder.busy = false; |
| +добавлено | d.boulder = null; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- the impact cloud ---------------------------------------------------------------------- |
| +добавлено | // Reference: impact tests and bolide strikes (hypervelocity shots into sand, quarry blasts): a |
| +добавлено | // short white-hot vapour flash at contact, an inverted cone of ejecta and dust thrown out at |
| +добавлено | // ~50 degrees with a base surge racing out low, then a dirty brown column boiling up, lit orange |
| +добавлено | // from the crater underneath and by the sun on top. Never a glowing dome: rock dust is dark. |
| +добавлено | function impactCloud(c, p, d, g, S, hot) { |
| +добавлено | const f = d.fl = fluid.acquire({ size: [11 * S, 7 * S, 11 * S] }); |
| +добавлено | if (!f) return false; |
| +добавлено | d.flAge = 0; |
| +добавлено | f.position.set(g.x, 0.5 * f.size.y - f.h, g.z); |
| +добавлено | Object.assign(f, { |
| +добавлено | ignition: 0.25, burnRate: 6, heatPerFuel: 0.4, sootYield: 1.2, expansion: 1.3, cooling: 2.4, radiative: 1.6, |
| +добавлено | sootDecay: 0.05, fuelDecay: 0.5, buoyancy: 4.5, sootWeight: 0.8, vorticity: 9, damping: 0.4, forceCoupling: 0.5, |
| +добавлено | turbulence: 12, turbulenceFreq: 0.5 / S, sootCool: 2, sootErode: 0, |
| +добавлено | emission: 5.5, temperature: 1750, sootDensity: 2.8, flameDensity: 2.5, fuelDensity: 0, absorb: 0, |
| +добавлено | g: 0.4, sun: 1.25, sky: 1.2, fireLight: 0.9, upres: 1.4, upresScale: 4, edgeFade: 5, |
| +добавлено | detailFreq: 0.5 / S, detailErode: 0.3, detailHeat: 0.35, nearFade: 0.4, |
| +добавлено | }); |
| +добавлено | // dust albedo from the dust colour (a little lighter than the settled dust: fine grains scatter) |
| +добавлено | f.sootColor.set(p.dustColor).multiplyScalar(1.5); |
| +добавлено | f.tint.setRGB(1, 1, 1); |
| +добавлено | f.detailCenter.copy(g); |
| +добавлено | const sd = (c.seed % 89) * 0.31; |
| +добавлено | // the vapour flash: a white-hot core thrown outwards, heat by the meteor's heat |
| +добавлено | src(f, g.x, 0.35 * S, g.z, 0.9 * S, 0.25 + 0.6 * hot, 0.3 + 0.7 * hot, 1.6, true, 0.85, 2.4 / S, sd, 0, 2, 0, 6 * S, 400); |
| +добавлено | // the ejecta curtain: six lobes thrown up and out |
| +добавлено | for (let i = 0; i < 6; i++) { |
| +добавлено | const a = sd + i * 1.0472 + 0.3 * Math.sin(i * 2.7 + sd), ca = Math.cos(a), sa = Math.sin(a), up = 7 + 2 * Math.sin(i * 1.9 + sd); |
| +добавлено | src(f, g.x + ca * 0.75 * S, 0.5 * S, g.z + sa * 0.75 * S, 0.7 * S, 0.3 * hot, 0.25 + 0.3 * hot, 2.4, true, 0.9, 2.8 / S, sd + i * 1.7, |
| +добавлено | ca * 6.5 * S, up * Math.sqrt(S), sa * 6.5 * S, 1.5 * S, 300); |
| +добавлено | } |
| +добавлено | return true; |
| +добавлено | } |
| +добавлено | function updateImpactCloud(c, p, d, dt) { |
| +добавлено | const f = d.fl; |
| +добавлено | if (!f) return; |
| +добавлено | const a = (d.flAge += dt), S = d.size, hot = d.heat, g = c.pos, sd = (c.seed % 89) * 0.31; |
| +добавлено | if (a <= 1.5 * dt) { |
| +добавлено | // the base surge: lobes racing out along the floor |
| +добавлено | for (let i = 0; i < 6; i++) { |
| +добавлено | const ang = sd + 0.52 + i * 1.0472, ca = Math.cos(ang), sa = Math.sin(ang); |
| +добавлено | src(f, g.x + ca * 1.3 * S, 0.3, g.z + sa * 1.3 * S, 0.55 * S, 0, 0.1 + 0.15 * hot, 1.3, true, 0.9, 3 / S, sd + 9 + i, ca * 10 * S, 0.6, sa * 10 * S, 0, 300); |
| +добавлено | } |
| +добавлено | } else if (a < 0.45) { |
| +добавлено | // the crater keeps blowing dust up for a moment, hot underneath |
| +добавлено | const k = 1 - a / 0.45; |
| +добавлено | src(f, g.x, 0.3, g.z, 0.75 * S, 0.8 * hot * k, 0.6 * hot * k, 5 * k, false, 0.8, 2 / S, sd + 4 + a, 0, 5 * Math.sqrt(S), 0, 2 * S, 8); |
| +добавлено | } |
| +добавлено | // the column climbs and spreads; the dust thins from its edges late |
| +добавлено | f.detailCenter.set(g.x, 0.9 * S * Math.min(a, 3), g.z); |
| +добавлено | f.detailFreq = 0.5 / (S * (0.8 + 0.5 * (1 - Math.exp(-a / 0.6)))); |
| +добавлено | f.sootErode = 1.4 * THREE.MathUtils.smoothstep(a, 1.6, 3.6); |
| +добавлено | f.emission = (3.5 + 4 * Math.exp(-a / 0.08)) * Math.max(hot, 0.2); |
| +добавлено | if (a > 3.85) { f.release(); d.fl = null; } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function land(c, p, d) { |
| +добавлено | const S = d.size, g = _b.set(c.pos.x, 0, c.pos.z), hot = d.heat; |
| +добавлено | freeBoulder(d); |
| +добавлено | splat(g.x, g.z, 2.1 * S, 'radial', 2.4 * p.crack, 1.0 * hot, 0.45, c.seed % 97); |
| +добавлено | splat(g.x, g.z, 2.5 * S, 'ring', 2.0 * p.crack, 0.4 * hot, 0.2, (c.seed + 31) % 97, g.x, g.z, 0.45 * S); |
| +добавлено | rockCone.pos.copy(g).setY(0.2); rockCone.p.set(0, 1, 0); rockCone.cone = 1.1; rockCone.radius = 0.6 * S; rockCone.radial = 7.5 * Math.sqrt(S); rockCone.size = S; |
| +добавлено | rockCone.burst(450 * p.debris); |
| +добавлено | rockCone.cone = 0.8; |
| +добавлено | chunkRing.pos.copy(g).setY(0.25); chunkRing.radius = 0.9 * S; chunkRing.vel.set(0, 3.5, 0); chunkRing.radial = 4; chunkRing.size = S; chunkRing.burst(30 * p.debris); |
| +добавлено | chunkRing.radial = 2.5; |
| +добавлено | bigChunk.pos.copy(c.pos); bigChunk.vel.copy(c.vel).multiplyScalar(0.12).setY(3); bigChunk.size = 1.4 * S; bigChunk.burst(8); |
| +добавлено | if (hot > 0.25) { magmaBurst.pos.copy(g).setY(0.4); magmaBurst.radius = 0.6 * S; magmaBurst.vel.set(0, 4, 0); magmaBurst.size = S; magmaBurst.burst(35 * hot * p.debris); } |
| +добавлено | // the cloud (simulated when a fluid slot is free); else the molten core bursts into |
| +добавлено | // incandescent dust that chokes to smoke |
| +добавлено | const cloud = impactCloud(c, p, d, g, S, hot); |
| +добавлено | if (hot > 0.05) { |
| +добавлено | if (!cloud) { blast.pos.copy(g).setY(0.5 * S); blast.radius = 0.6 * S; blast.vel.copy(c.vel).multiplyScalar(0.08).setY(2.2); blast.radial = 3 * Math.sqrt(S); blast.size = S; blast.burst(28 * Math.min(1, hot * 1.6) * p.dust); } |
| +добавлено | spatter.pos.copy(g).setY(0.3); spatter.radius = 0.5 * S; spatter.vel.set(0, 3.5, 0); spatter.radial = 6; spatter.burst(260 * hot); |
| +добавлено | spatter.radial = 2.5; |
| +добавлено | } |
| +добавлено | dustRing.pos.copy(g).setY(0.3); dustRing.radius = 1.0 * S; dustRing.vel.set(0, 0.4, 0); dustRing.radial = 8.5 * Math.sqrt(S); dustRing.size = 1.2 * S; |
| +добавлено | dustRing.burst((cloud ? 110 : 950) * p.dust); |
| +добавлено | plume.pos.copy(g).setY(0.7); plume.radius = 0.9 * S; plume.vel.set(0, 3.5 * Math.sqrt(S), 0); plume.size = 1.2 * S; plume.burst((cloud ? 40 : 320) * p.dust); |
| +добавлено | shock(g, 9 * S, 0.6, 0.075); |
| +добавлено | SHK.ground = false; SHK.pos.copy(g).setY(0.6); SHK.radius1 = 4 * S; SHK.thickness = 0.6; SHK.duration = 0.35; SHK.distortion = 0.04; |
| +добавлено | fx.shocks?.spawn(SHK); |
| +добавлено | SHK.ground = true; |
| +добавлено | // crater rim: slabs upturned around the hole, leaning out |
| +добавлено | const rn = Math.min(MAX_RIM, Math.round(p.rim)); |
| +добавлено | d.rimN = rn; d.n = 0; |
| +добавлено | for (let k = 0; k < rn; k++) { |
| +добавлено | const s = d.spikes[k], a = (k / rn) * Math.PI * 2 + fx.rand() * 0.5, out = _a.set(Math.cos(a), 0, Math.sin(a)); |
| +добавлено | s.rim = true; s.u = 0.5; |
| +добавлено | s.pos.copy(g).addScaledVector(out, 1.45 * S * (0.9 + 0.2 * fx.rand())); |
| +добавлено | s.dir.copy(out).multiplyScalar(1.0 + 0.4 * fx.rand()).add(UP).normalize(); |
| +добавлено | s.height = (0.55 + 0.45 * fx.rand()) * S; s.radius = (0.32 + 0.12 * fx.rand()) * S; |
| +добавлено | s.seed = ((c.seed * 131 + k * 4099) >>> 0) & 0xffffff || 1; |
| +добавлено | spawnSpike(c, d, s, fx.rand() * 0.05, 2.4 + fx.rand() * 0.4, 0.45 * hot); |
| +добавлено | s.erupted = true; |
| +добавлено | } |
| +добавлено | fx.rig.shake(1.0 * p.shake, 7); |
| +добавлено | fx.rig.shake(0.4 * p.shake, 18); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(3.5, 0.14); fx.rig.hitstop(0.06); } |
| +добавлено | if (hot > 0.25) { |
| +добавлено | fx.impacts.add('magma', g, 2.4 * S, hot, 6, magmaCol); |
| +добавлено | fx.flash(0.15 * hot); |
| +добавлено | } |
| +добавлено | if (d.light) { d.light.intensity = (cloud ? 22 : 30) * hot; d.light.range = 12 * S; d.light.pos.copy(g).setY(cloud ? 0.4 : 0.8); } |
| +добавлено | hitDummies(g, 4 * S, 1.6, _push.copy(c.vel).setY(0).normalize()); |
| +добавлено | for (const dm of fx.dummies) if (hot > 0.25 && Math.hypot(dm.pos.x - g.x, dm.pos.z - g.z) < 2.5 * S) dm.status.burn = Math.max(dm.status.burn, 0.5 * hot); |
| +добавлено | const pool = fx.ARENA.pool; |
| +добавлено | if (Math.abs(g.x - pool.center.x) < pool.half.x + 3 && Math.abs(g.z - pool.center.y) < pool.half.y + 3) { |
| +добавлено | fx.pool?.ripple?.(Math.min(Math.max(g.x, pool.center.x - pool.half.x), pool.center.x + pool.half.x), Math.min(Math.max(g.z, pool.center.y - pool.half.y), pool.center.y + pool.half.y), 0.06, 0.8); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | // projectile shape for both forms: Upheaval ends travel at once, Meteor flies its own arc |
| +добавлено | travel(c, dt) { |
| +добавлено | if (c.form !== 'Meteor') { c.pos.copy(c.to); return true; } |
| +добавлено | c.vel.y -= GRAVITY * dt; |
| +добавлено | c.pos.addScaledVector(c.vel, dt); |
| +добавлено | c.travelled += c.vel.length() * dt; |
| +добавлено | const d = c.data.e; |
| +добавлено | return (c.travelled > 1 && arenaSDF(c.pos) < 0.5 * d.size) || c.t > 5; |
| +добавлено | }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params; |
| +добавлено | if (name === 'charge') { |
| +добавлено | syncColors(p); |
| +добавлено | const d = c.data.e = states[c.index]; |
| +добавлено | d.alive = true; d.n = 0; d.rimN = 0; d.magmaCount = 0; d.splatT = 0; d.heat = 0; d.boulder = null; d.fl = null; |
| +добавлено | for (const s of d.spikes) { s.h = null; s.erupted = s.crumbled = s.magma = false; s.ground = s.heat = 0; } |
| +добавлено | d.light = (c.form === 'Meteor' ? p.meteorHeat : p.glow) > 0 ? fx.lights.acquire() : null; |
| +добавлено | if (d.light) { d.light.intensity = 0; d.light.range = 5; d.light.shadow = false; blackbodyRGB(1500, d.light.color); } |
| +добавлено | if (c.form === 'Meteor') { |
| +добавлено | c.durations.charge = 1.0; c.durations.impact = 0.3; c.durations.linger = 3.6; |
| +добавлено | setupBoulder(c, p, d); |
| +добавлено | d.attract = fx.forces.acquire(); |
| +добавлено | if (d.attract) { d.attract.type = 2; d.attract.p[0] = 45; d.attract.p[1] = 0.45; d.attract.p[2] = 0.5 * p.meteorSize; d.attract.p[3] = 3.2; d.attract.pos.copy(d.center); } |
| +добавлено | splat(d.center.x, d.center.z, 2.6, 'ring', 1.4 * p.crack, 0, 0.1, c.seed % 53, d.center.x, d.center.z, 0.5); |
| +добавлено | } else { |
| +добавлено | c.durations.charge = S.timing.charge; c.durations.impact = S.timing.impact; |
| +добавлено | layoutLine(c, p, d); |
| +добавлено | d.head.copy(d.cStart); |
| +добавлено | } |
| +добавлено | } else if (name === 'travel') { |
| +добавлено | const d = c.data.e; |
| +добавлено | if (c.form === 'Meteor') { |
| +добавлено | d.attract?.release(); d.attract = null; |
| +добавлено | c.pos.copy(d.center); |
| +добавлено | // ballistic: apex `arc` m above the launch, landing on the target |
| +добавлено | const vy = Math.sqrt(2 * GRAVITY * p.arc), dy = c.to.y - c.pos.y; |
| +добавлено | const T = (vy + Math.sqrt(Math.max(vy * vy - 2 * GRAVITY * dy, 0))) / GRAVITY; |
| +добавлено | c.vel.set((c.to.x - c.pos.x) / T, vy, (c.to.z - c.pos.z) / T); |
| +добавлено | d.axis.set(c.vel.z, 0, -c.vel.x).normalize(); // roll forward |
| +добавлено | fx.rig.shake(0.18 * p.shake, 10); |
| +добавлено | puff.pos.copy(c.pos); puff.radius = 0.5 * d.size; puff.vel.set(0, -0.5, 0); puff.radial = 2.5; puff.size = d.size; puff.burst(30 * p.dust); |
| +добавлено | } |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | const d = c.data.e; |
| +добавлено | d.impactAge = c.age; |
| +добавлено | if (c.form === 'Meteor') land(c, p, d); |
| +добавлено | else { |
| +добавлено | // the whole line cracks open; spikes rise in sequence |
| +добавлено | splat(d.cStart.x, d.cStart.z, 0.3, 'line', 1.9 * p.crack, 0.8 * p.heat, 0.1, c.seed % 97, d.cEnd.x, d.cEnd.z, 0.6 * p.size); |
| +добавлено | let tail = 0; |
| +добавлено | for (let k = 0; k < d.n; k++) { |
| +добавлено | const s = d.spikes[k]; |
| +добавлено | spawnSpike(c, d, s, k * p.stagger, p.hold + 0.25 * s.u, p.heat); |
| +добавлено | tail = Math.max(tail, s.delay + s.life + 0.9); |
| +добавлено | } |
| +добавлено | c.durations.linger = Math.max(tail - c.durations.impact + 0.3, 1); |
| +добавлено | fx.rig.shake(0.3 * p.shake, 9); |
| +добавлено | if (d.light) d.light.pos.copy(d.start).setY(0.5); |
| +добавлено | } |
| +добавлено | } else if (name === 'done') { |
| +добавлено | const d = c.data.e; |
| +добавлено | if (!d) return; |
| +добавлено | for (const s of d.spikes) { if (owns(s)) s.h.release(); s.h = null; } |
| +добавлено | d.light?.release(); d.light = null; |
| +добавлено | d.attract?.release(); d.attract = null; |
| +добавлено | d.fl?.release(); d.fl = null; |
| +добавлено | freeBoulder(d); |
| +добавлено | d.alive = false; |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = c.data.e, L = d.light; |
| +добавлено | if (c.form === 'Meteor') { |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge; |
| +добавлено | d.rot.multiply(_q.setFromAxisAngle(UP, dt * (0.6 + 2.5 * k))); // turning as it forms |
| +добавлено | poseBoulder(c, p, d, true); |
| +добавлено | d.heat = p.meteorHeat * 0.25 * smooth((k - 0.55) / 0.45); // compressing: the seams start to glow |
| +добавлено | if (d.boulder) d.boulder.own.uBoulder.value.x = d.heat; |
| +добавлено | gather.pos.copy(d.center).setY(0.12); gather.radius = 2.5; gather.vel.set(0, 4.6 + 0.6 * p.meteorSize, 0); gather.size = p.meteorSize; |
| +добавлено | if (k < 0.85) gather.rate(420 * p.debris, dt); |
| +добавлено | gatherDust.pos.copy(d.center).setY(0.15); gatherDust.radius = 2.5; gatherDust.vel.set(0, 0.7, 0); gatherDust.rate(110 * p.dust * (1 - k), dt); |
| +добавлено | if (d.attract) d.attract.p[2] = 0.45 * p.meteorSize * (0.5 + 0.5 * k); |
| +добавлено | fx.rig.shake(dt * 1.9 * p.shake, 8); |
| +добавлено | if (L) { L.pos.copy(d.center); L.pos.y -= 0.9 * d.size; L.intensity = 6 * d.heat * (0.85 + 0.15 * Math.sin(c.age * 23)); } // below, not inside: unshadowed |
| +добавлено | } else if (c.phase === 'travel') { |
| +добавлено | d.rot.premultiply(_q.setFromAxisAngle(d.axis, dt * 5 / d.size)); |
| +добавлено | poseBoulder(c, p, d, false); |
| +добавлено | d.heat = p.meteorHeat * Math.min(1, 0.5 + c.t * 0.9); // heats up in flight |
| +добавлено | _c.copy(c.vel).normalize(); |
| +добавлено | if (d.boulder) { d.boulder.own.uBoulder.value.x = d.heat; d.boulder.own.uBoulderVel.value.copy(_c); } |
| +добавлено | // the wake: smoke boiling off the hot crust, a heat shimmer, grit and pebbles shed, embers |
| +добавлено | // and molten spatter flung off the seams |
| +добавлено | const R = CORE_R * d.size, h = Math.max(d.heat, 0.15); |
| +добавлено | _a.copy(c.pos).addScaledVector(_c, -0.45 * R); |
| +добавлено | smoke.pos.copy(_a); smoke.vel.copy(c.vel).multiplyScalar(0.1); smoke.radius = 0.55 * R; smoke.size = d.size; smoke.rate(200 * p.dust * h, dt); |
| +добавлено | haze.pos.copy(_a); haze.vel.copy(c.vel).multiplyScalar(0.04); haze.radius = 0.6 * R; haze.size = d.size; haze.rate(70 * h, dt); |
| +добавлено | trailDust.pos.copy(_a); trailDust.vel.copy(c.vel).multiplyScalar(0.1); trailDust.radius = 0.5 * R; trailDust.size = d.size; trailDust.rate(45 * p.dust, dt); |
| +добавлено | trailPebble.pos.copy(c.pos); trailPebble.vel.copy(c.vel).multiplyScalar(0.35); trailPebble.radius = 0.7 * R; trailPebble.rate(35 * p.debris, dt); |
| +добавлено | if (d.heat > 0.05) { |
| +добавлено | ember.pos.copy(c.pos); ember.vel.copy(c.vel).multiplyScalar(0.15); ember.radius = 0.8 * R; ember.rate(260 * d.heat, dt); |
| +добавлено | spatter.pos.copy(c.pos); spatter.vel.copy(c.vel).multiplyScalar(0.55); spatter.radius = 0.85 * R; spatter.rate(70 * d.heat, dt); |
| +добавлено | } |
| +добавлено | if (L) { L.pos.copy(c.vel).setLength(-1.1 * d.size).add(c.pos); L.range = 7; L.intensity = 8 * d.heat * (0.85 + 0.15 * Math.sin(c.age * 29)); } // trailing, lights the plume |
| +добавлено | } else { |
| +добавлено | updateImpactCloud(c, p, d, dt); |
| +добавлено | if (L) L.intensity = Math.max(L.intensity * Math.exp(-dt / 0.35), 3 * d.heat * Math.max(0, 1 - c.t / 2.5) * (c.phase === 'linger' ? 1 : 0)); |
| +добавлено | heatSpikes(p, d, dt, 0); |
| +добавлено | } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | // Upheaval |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge, u = smooth(k / 0.85); |
| +добавлено | d.head.lerpVectors(d.cStart, d.cEnd, u); |
| +добавлено | d.splatT -= dt; |
| +добавлено | if (d.splatT <= 0) { // re-stamped as the head advances; potential channels take the max, so it doesn't stack |
| +добавлено | d.splatT = 0.035; |
| +добавлено | splat(d.cStart.x, d.cStart.z, 0.3, 'line', 1.6 * p.crack, 0, 0, c.seed % 97, d.head.x, d.head.z, 0.45 * p.size); |
| +добавлено | } |
| +добавлено | pebbleLine.pos.copy(d.cStart); pebbleLine.p.copy(d.head); pebbleLine.vel.set(0, 2.2, 0); pebbleLine.size = p.size; pebbleLine.rate(650 * p.debris, dt); |
| +добавлено | seepLine.pos.copy(d.cStart); seepLine.p.copy(d.head); seepLine.vel.set(0, 0.6, 0); seepLine.size = p.size; seepLine.rate(360 * p.dust, dt); |
| +добавлено | fx.rig.shake(dt * 2.2 * p.shake, 8); |
| +добавлено | if (L) { L.pos.copy(d.head).setY(0.35); L.range = 4; L.intensity = 8 * p.glow * k; } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (c.phase === 'travel') return; |
| +добавлено | const te = c.age - d.impactAge; |
| +добавлено | for (let i = 0; i < d.n; i++) { |
| +добавлено | const s = d.spikes[i]; |
| +добавлено | if (!s.erupted && te >= s.delay) erupt(c, p, d, s, i === d.n - 1); |
| +добавлено | if (s.erupted && !s.crumbled && owns(s) && te >= s.delay + s.life) crumble(p, s); |
| +добавлено | } |
| +добавлено | heatSpikes(p, d, dt, p.heat); |
| +добавлено | if (L) { |
| +добавлено | if (te < 0.25 && d.n) L.pos.lerpVectors(d.start, d.end, Math.min(1, te / Math.max(d.n * p.stagger, 0.01))).setY(0.5); |
| +добавлено | L.intensity *= Math.exp(-dt / 0.3); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | // every frame: read back last request, then probe the ground heat under every live spike |
| +добавлено | tick() { |
| +добавлено | if (sync && gl.getSyncParameter(sync, gl.SYNC_STATUS) === gl.SIGNALED) { |
| +добавлено | gl.deleteSync(sync); sync = null; |
| +добавлено | gl.bindBuffer(gl.PIXEL_PACK_BUFFER, pbo); |
| +добавлено | gl.getBufferSubData(gl.PIXEL_PACK_BUFFER, 0, probeOut, 0, probeN * 4); |
| +добавлено | gl.bindBuffer(gl.PIXEL_PACK_BUFFER, null); |
| +добавлено | for (let i = 0; i < probeN; i++) { const s = probeOwner[i]; if (s && s.seed === probeSeed[i]) s.ground = probeOut[i * 4]; probeOwner[i] = null; } |
| +добавлено | } |
| +добавлено | if (sync) return; |
| +добавлено | probeN = 0; |
| +добавлено | for (const d of states) { |
| +добавлено | if (!d.alive) continue; |
| +добавлено | const total = d.n + d.rimN; |
| +добавлено | for (let i = 0; i < total && probeN < PROBES; i++) { |
| +добавлено | const s = d.spikes[i]; |
| +добавлено | if (!owns(s)) continue; |
| +добавлено | probeXZ[probeN].set(s.pos.x, s.pos.z); |
| +добавлено | probeOwner[probeN] = s; probeSeed[probeN] = s.seed; |
| +добавлено | probeN++; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | if (!probeN) return; |
| +добавлено | const r = fx.renderer, prev = r.getRenderTarget(); |
| +добавлено | probeQuad.render(r, probeRT); |
| +добавлено | gl.bindBuffer(gl.PIXEL_PACK_BUFFER, pbo); |
| +добавлено | if (!sized) { gl.bufferData(gl.PIXEL_PACK_BUFFER, PROBES * 16, gl.STREAM_READ); sized = true; } |
| +добавлено | gl.readPixels(0, 0, probeN, 1, gl.RGBA, gl.FLOAT, 0); |
| +добавлено | gl.bindBuffer(gl.PIXEL_PACK_BUFFER, null); |
| +добавлено | sync = gl.fenceSync(gl.SYNC_GPU_COMMANDS_COMPLETE, 0); |
| +добавлено | r.setRenderTarget(prev); |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | function lcg(seed) { |
| +добавлено | let s = (seed >>> 0) || 1; |
| +добавлено | return () => ((s = (Math.imul(s, 1664525) + 1013904223) >>> 0) / 4294967296); |
| +добавлено | } |
| +добавлено | import { COMMON_GLSL, NOISE_GLSL } from '../../vfx/common.js'; |
| +добавлено | |
| +добавлено | // The rift: a flat quad on the floor that shows a trench below it (interior mapping). Each pixel |
| +добавлено | // inside the opening marches its view ray down a V-shaped cut whose two walls are jagged along the |
| +добавлено | // line (a baked 1D profile: meander + left / right jag) and stepped with depth (ledges); it lands on |
| +добавлено | // a wall (dark basalt lit from below by the magma, red-hot near the bottom, glowing veins) or on |
| +добавлено | // the magma itself (crust plates drifting over bright seams). Hot air in the cut scatters the glow, |
| +добавлено | // so a grazing view, which only sees the top of the far wall, still reads as a seam of fire. |
| +добавлено | // Pixels outside the opening are discarded after one profile tap. Edges use alpha-to-coverage. |
| +добавлено | // |
| +добавлено | // Local frame: u along the line from uA (metres), v across it, z depth below the floor (down). |
| +добавлено | |
| +добавлено | // magma radiance at T: blackbody chromaticity, brightness ~ (T / 1300 K)^4, no glow below ~650 K |
| +добавлено | export const MAGMA_L = /* glsl */` |
| +добавлено | vec3 magmaL(float T) { float k = T / 1300.0; k *= k; return blackbody(T) * (k * k * smoothstep(620.0, 950.0, T)); } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | export const CRACK_VERT = /* glsl */` |
| +добавлено | uniform vec3 uA; |
| +добавлено | uniform vec2 uT; |
| +добавлено | uniform vec4 uQuad; // u0, u1, half width, y |
| +добавлено | out vec3 vW; |
| +добавлено | void main() { |
| +добавлено | vec2 n = vec2(-uT.y, uT.x); |
| +добавлено | float u = mix(uQuad.x, uQuad.y, position.x * 0.5 + 0.5); |
| +добавлено | float v = position.y * uQuad.z; |
| +добавлено | vW = vec3(uA.x + uT.x * u + n.x * v, uQuad.w, uA.z + uT.y * u + n.y * v); |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(vW, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const CRACK_FRAG = COMMON_GLSL + NOISE_GLSL + MAGMA_L + /* glsl */` |
| +добавлено | uniform sampler2D uProfile; |
| +добавлено | uniform vec3 uA; |
| +добавлено | uniform vec2 uT; |
| +добавлено | uniform vec4 uCrackA; // length L, max half width W, depth D, hairline front 0..1 |
| +добавлено | uniform vec4 uCrackB; // s since the split (< 0 before), unzip time s, close 1 -> 0, heat 0..1 |
| +добавлено | uniform vec4 uCrackC; // profile offset, meander m, hairline half width m, seed |
| +добавлено | uniform vec4 uCrackD; // intensity, magma temperature K, glow haze, - |
| +добавлено | uniform vec3 uTint; |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | in vec3 vW; |
| +добавлено | out vec4 fragColor; |
| +добавлено | |
| +добавлено | vec4 prof(float u) { return texture(uProfile, vec2(u * (1.0 / 12.0) + uCrackC.x, 0.5)); } |
| +добавлено | |
| +добавлено | // opening at x = u / L: unzips from the start, ease-out-back |
| +добавлено | float openAt(float x) { |
| +добавлено | float k = clamp((uCrackB.x - x * uCrackB.y) / 0.24, 0.0, 1.0) - 1.0; |
| +добавлено | return uCrackB.x - x * uCrackB.y > 0.0 ? 1.0 + 2.70158 * k * k * k + 1.70158 * k * k : 0.0; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // centre line and the two surface half widths at u |
| +добавлено | void edges(float u, out float c, out float hl, out float hr, out float ledge) { |
| +добавлено | vec4 p = prof(u); |
| +добавлено | float x = clamp(u / uCrackA.x, 0.0, 1.0); |
| +добавлено | float env = pow(max(sin(3.14159265 * x), 0.0), 0.55); |
| +добавлено | float front = smoothstep(uCrackA.w + 0.03, uCrackA.w - 0.03, x); |
| +добавлено | float w = max(uCrackC.z * front, uCrackA.y * openAt(x)) * env * uCrackB.z; |
| +добавлено | c = (p.r - 0.5) * 2.0 * uCrackC.y * env; |
| +добавлено | hl = w * (0.5 + p.g); |
| +добавлено | hr = w * (0.5 + p.b); |
| +добавлено | ledge = p.a; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // wall half width at depth z: a V narrowing to the bottom, stepped into ledges |
| +добавлено | float hwAt(float hw0, float z, float side, float ledge) { |
| +добавлено | float s = clamp(z / uCrackA.z, 0.0, 1.0); |
| +добавлено | float l = (0.11 * sin(z * 23.0 + ledge * 31.0 + side * 1.7) + 0.06 * sin(z * 57.0 + ledge * 13.0 - side)) * smoothstep(0.0, 0.07, z); |
| +добавлено | return hw0 * pow(1.0 - s, 0.8) * (1.0 + l); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // > 0 inside the cut |
| +добавлено | float inside(vec3 q) { |
| +добавлено | float c, hl, hr, lg; |
| +добавлено | edges(q.x, c, hl, hr, lg); |
| +добавлено | float d = q.y - c; |
| +добавлено | return min(hwAt(hr, q.z, 1.0, lg) - d, hwAt(hl, q.z, -1.0, lg) + d); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // signed distance into the rock of one wall (side +-1) |
| +добавлено | float wallG(vec3 q, float side) { |
| +добавлено | float c, hl, hr, lg; |
| +добавлено | edges(q.x, c, hl, hr, lg); |
| +добавлено | return side * (q.y - c) - hwAt(side > 0.0 ? hr : hl, q.z, side, lg); |
| +добавлено | } |
| +добавлено | |
| +добавлено | void main() { |
| +добавлено | vec2 n2 = vec2(-uT.y, uT.x); |
| +добавлено | vec2 rel = vW.xz - uA.xz; |
| +добавлено | float u0 = dot(rel, uT), v0 = dot(rel, n2); |
| +добавлено | float c0, hl0, hr0, lg0; |
| +добавлено | edges(u0, c0, hl0, hr0, lg0); |
| +добавлено | float d0 = v0 - c0; |
| +добавлено | float edge = min(hr0 - d0, hl0 + d0); |
| +добавлено | float fw = max(fwidth(v0), 1e-4); |
| +добавлено | float cov = clamp(edge / fw + 0.5, 0.0, 1.0); |
| +добавлено | if (cov <= 0.0) discard; |
| +добавлено | |
| +добавлено | vec3 rd = normalize(vW - cameraPosition); |
| +добавлено | vec3 dq = vec3(dot(rd.xz, uT), dot(rd.xz, n2), max(-rd.y, 0.02)); // local ray: along, across, down |
| +добавлено | vec3 q0 = vec3(u0, v0, 0.0); |
| +добавлено | float D = uCrackA.z, Dm = 0.72 * D; |
| +добавлено | float tEnd = Dm / dq.z, tPrev = 0.0, tHit = tEnd; |
| +добавлено | bool wall = false; |
| +добавлено | for (int i = 1; i <= 14; i++) { |
| +добавлено | float t = tEnd * pow(float(i) / 14.0, 1.6); |
| +добавлено | if (inside(q0 + dq * t) < 0.0) { |
| +добавлено | float a = tPrev, b = t; |
| +добавлено | for (int j = 0; j < 5; j++) { float m = 0.5 * (a + b); if (inside(q0 + dq * m) < 0.0) b = m; else a = m; } |
| +добавлено | tHit = b; wall = true; |
| +добавлено | break; |
| +добавлено | } |
| +добавлено | tPrev = t; |
| +добавлено | } |
| +добавлено | vec3 q = q0 + dq * tHit; |
| +добавлено | float heat = uCrackB.w, I = uCrackD.x; |
| +добавлено | float Tm = mix(560.0, uCrackD.y, heat); |
| +добавлено | vec3 T3 = vec3(uT.x, 0.0, uT.y), N3 = vec3(n2.x, 0.0, n2.y); |
| +добавлено | vec3 col; |
| +добавлено | float s = clamp(q.z / Dm, 0.0, 1.0); |
| +добавлено | if (wall) { |
| +добавлено | float c, hl, hr, lg; |
| +добавлено | edges(q.x, c, hl, hr, lg); |
| +добавлено | float side = q.y - c > 0.0 ? 1.0 : -1.0; |
| +добавлено | // normal from the wall's gradient (into the cut) |
| +добавлено | const float e = 0.03; |
| +добавлено | float gu = (wallG(q + vec3(e, 0.0, 0.0), side) - wallG(q - vec3(e, 0.0, 0.0), side)) / (2.0 * e); |
| +добавлено | float gz = (wallG(q + vec3(0.0, 0.0, e), side) - wallG(q - vec3(0.0, 0.0, e), side)) / (2.0 * e); |
| +добавлено | vec3 gN = -normalize(vec3(gu, side, gz)); |
| +добавлено | vec3 nW = normalize(T3 * gN.x + N3 * gN.y - vec3(0.0, gN.z, 0.0)); |
| +добавлено | vec4 nz = noise3v(vec3(q.x * 0.31, q.z * 0.9, side * 0.37 + uCrackC.w)); |
| +добавлено | vec4 nf = noise3v(vec3(q.x * 1.3, q.z * 2.6, side * 0.61 + uCrackC.w + 0.5)); |
| +добавлено | float strata = 0.55 + 0.9 * nz.r * (0.75 + 0.5 * sin(q.z * 40.0 + nz.g * 9.0)); |
| +добавлено | vec3 albedo = vec3(0.05, 0.043, 0.038) * strata; |
| +добавлено | // glow from the magma below lights the wall; overhangs (facing down) catch the most |
| +добавлено | vec3 Lm = magmaL(Tm) * uTint * I * (0.22 + 0.78 * pow(s, 1.3)) * (0.65 + 0.35 * max(-nW.y, 0.0) + 0.3 * max(dot(nW, -N3 * side), 0.0)); |
| +добавлено | col = albedo * Lm * 5.0; |
| +добавлено | // the rock itself is red-hot near the bottom; veins glow further up |
| +добавлено | float vein = smoothstep(0.035, 0.0, abs(nf.r - 0.5)) * smoothstep(0.15, 0.6, nz.g); |
| +добавлено | // (in patches: the walls chill to a dull red skin where the melt has drained off them, so the cut |
| +добавлено | // never reads as one smooth orange gradient) |
| +добавлено | float Tr = mix(450.0, Tm * 0.95, pow(s, 1.4) * (0.62 + 0.55 * nf.g) * (1.0 - 0.3 * smoothstep(0.45, 0.7, nz.b))) + vein * 380.0 * heat * (0.3 + 0.7 * s); |
| +добавлено | col += magmaL(Tr) * uTint * I * 0.6; |
| +добавлено | // sky and sun on the upper wall |
| +добавлено | col += albedo * (uAmbient * (1.0 - s) * 1.4 + uSunColor * max(dot(nW, uSunDir), 0.0) * smoothstep(0.3, 0.0, s)); |
| +добавлено | } else { |
| +добавлено | // magma (Holuhraun / Fagradalsfjall channels and overflows): a dark skin, not open melt. Crust |
| +добавлено | // plates (grey-black basalt, sky-lit, a glassy sheen) drift along the cut, torn apart by thin |
| +добавлено | // glowing cracks; the skin closes first along the banks and as the heat sags; only mid-channel, |
| +добавлено | // where the flow still feeds, does it split open into bright pahoehoe toes, in pulses |
| +добавлено | vec3 mq = vec3(q.x * 0.42 - uCrackD.w * 0.025, q.y * 0.9, uCrackD.w * 0.018 + uCrackC.w); |
| +добавлено | vec4 n1 = noise3v(mq), n2v = noise3v(mq * 2.3 + 0.41); |
| +добавлено | float c, hl, hr, lg; |
| +добавлено | edges(q.x, c, hl, hr, lg); |
| +добавлено | float dc = q.y - c, bank = smoothstep(0.35, 0.95, abs(dc) / max(hwAt(dc > 0.0 ? hr : hl, q.z, sign(dc), lg), 1e-3)); |
| +добавлено | float seam = 1.0 - smoothstep(0.02, 0.1 + 0.08 * heat, n1.g * 0.75 + n2v.g * 0.25); |
| +добавлено | float feed = (0.55 + 0.45 * sin(uCrackD.w * 0.9 + q.x * 1.3 + n1.b * 4.0)) * heat * (1.0 - bank); |
| +добавлено | float open = smoothstep(0.66, 0.8, n2v.r + 0.35 * feed - 0.2 * bank); |
| +добавлено | float skin = 1.0 - max(open, seam * (1.0 - 0.5 * bank)); |
| +добавлено | // the skin's own glow: dull red where thin and fresh, black once it has crusted |
| +добавлено | float Tc = Tm * mix(0.66, 0.4, clamp(bank + (1.0 - heat) + 0.6 * (n1.r - 0.4), 0.0, 1.0)); |
| +добавлено | float T = mix(Tm * (0.88 + 0.12 * open), Tc, skin); |
| +добавлено | col = magmaL(T) * uTint * I * (1.0 + 0.25 * sin(uCrackD.w * 3.0 + n1.b * 12.0) * (1.0 - skin)); |
| +добавлено | vec3 crustA = vec3(0.045, 0.04, 0.037) * (0.7 + 0.6 * n2v.b); |
| +добавлено | col += skin * (crustA * (uAmbient * 1.2 + uSunColor * max(uSunDir.y, 0.0)) + 0.05 * uAmbient * (1.0 - n1.r)); // (+ the sheen of fresh glassy crust) |
| +добавлено | } |
| +добавлено | // hot air in the cut scatters the glow (more the deeper and longer the ray runs inside) |
| +добавлено | float path = 1.0 - exp(-tHit * 1.4); |
| +добавлено | col += magmaL(Tm) * uTint * I * uCrackD.z * 0.7 * path * (0.5 + 0.5 * s); |
| +добавлено | fragColor = vec4(col, cov); |
| +добавлено | }`; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { U } from '../../vfx/common.js'; |
| +добавлено | import { blackbodyRGB } from '../../vfx/noise.js'; |
| +добавлено | import { CRACK_VERT, CRACK_FRAG } from './fissure.glsl.js'; |
| +добавлено | |
| +добавлено | // Fissure (earth hero). Anticipation: a hairline crack races from the caster's side toward the |
| +добавлено | // target and through it, glowing faintly, while the floor rumbles, pebbles hop and dust and |
| +добавлено | // embers seep from it. Release: the ground splits. The cut unzips along the line in a fraction |
| +добавлено | // of a second (an interior-mapped trench, fissure.glsl.js: walls lit from below by magma deep in |
| +добавлено | // the crack), its lips heave up as broken rock slabs (spikes with magma heat in their veins), |
| +добавлено | // each stretch throws rock, glowing magma and spatter, jets steam and rolls dust outward; shock |
| +добавлено | // rings, a pressure push on loose particles, knockback away from the line, burning dummies. |
| +добавлено | // Aftermath: vents along the crack puff steam and spit magma, heat haze shimmers over it and a |
| +добавлено | // thermal lifts what drifts by; the magma cools and crusts over, the cut closes, the lips crumble, |
| +добавлено | // and the floor keeps the crack, the char and a fading glow. |
| +добавлено | |
| +добавлено | const MAX_CRACKS = 3, STATES = 16, MAX_LIPS = 8, SEGS = 8, VENTS = 6; |
| +добавлено | const PROFILE_N = 512, PERIOD = 12; // the profile repeats every 12 m of crack |
| +добавлено | const CLOSE = 1.4; // s at the end of `hold`: cools, closes, crumbles |
| +добавлено | const UP = new THREE.Vector3(0, 1, 0); |
| +добавлено | const smooth = (x) => { x = Math.min(1, Math.max(0, x)); return x * x * (3 - 2 * x); }; |
| +добавлено | |
| +добавлено | // reused option objects: the shared systems read fields, a literal per call would allocate |
| +добавлено | const SPL = { x: 0, z: 0, radius: 1, heat: 0, char: 0, wet: 0, frost: 0, crack: 0, acid: 0, corruption: 0, glow: 0, pattern: 'line', seed: 0, angle: 0, x2: 0, z2: 0, width: 0 }; |
| +добавлено | const SPK = { pos: new THREE.Vector3(), dir: new THREE.Vector3(), height: 1, radius: 0.4, material: 'rock', delay: 0, rise: 0.16, life: 3, crumble: 1.0, heat: 1, seed: 1 }; |
| +добавлено | const SHK = { pos: new THREE.Vector3(), radius0: 0.3, radius1: 4, duration: 0.5, thickness: 0.5, distortion: 0.05, color: null, intensity: 0, ground: true }; |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _c = new THREE.Vector3(), _col = new THREE.Color(); |
| +добавлено | const EDGE = { c: 0, hl: 0, hr: 0 }; |
| +добавлено | |
| +добавлено | function lcg(seed) { |
| +добавлено | let s = (seed >>> 0) || 1; |
| +добавлено | return () => ((s = (Math.imul(s, 1664525) + 1013904223) >>> 0) / 4294967296); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // 1D crack profile (periodic): r meander, g left jag, b right jag, a ledge phase |
| +добавлено | function buildProfile() { |
| +добавлено | const rnd = lcg(9127), data = new Uint8Array(PROFILE_N * 4); |
| +добавлено | const lat = (n) => Float32Array.from({ length: n }, rnd); |
| +добавлено | const oct = (L, x) => { const n = L.length, f = x * n, i = Math.floor(f), t = f - i, s = t * t * (3 - 2 * t); return L[i % n] * (1 - s) + L[(i + 1) % n] * s; }; |
| +добавлено | const M = [lat(3), lat(7)], JL = [lat(17), lat(41), lat(97), lat(223)], JR = [lat(19), lat(43), lat(101), lat(227)], LG = [lat(23), lat(59)]; |
| +добавлено | const jag = (J, x) => oct(J[0], x) * 0.42 + oct(J[1], x) * 0.28 + oct(J[2], x) * 0.18 + oct(J[3], x) * 0.12; |
| +добавлено | const st = (v) => Math.round(255 * Math.min(1, Math.max(0, (v - 0.5) * 1.9 + 0.5))); |
| +добавлено | for (let i = 0; i < PROFILE_N; i++) { |
| +добавлено | const x = (i + 0.5) / PROFILE_N; |
| +добавлено | data[i * 4] = st(oct(M[0], x) * 0.65 + oct(M[1], x) * 0.35); |
| +добавлено | data[i * 4 + 1] = st(jag(JL, x)); |
| +добавлено | data[i * 4 + 2] = st(jag(JR, x)); |
| +добавлено | data[i * 4 + 3] = st(oct(LG[0], x) * 0.6 + oct(LG[1], x) * 0.4); |
| +добавлено | } |
| +добавлено | return data; |
| +добавлено | } |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'fissure', name: 'Fissure', element: 'earth', order: 10, hue: '#ff6a1f', |
| +добавлено | shape: 'area', |
| +добавлено | params: { |
| +добавлено | width: 0.42, depth: 1.1, reach: 9, overshoot: 1.6, hold: 4.2, temperature: 1350, intensity: 1, |
| +добавлено | lips: 5, lipHeight: 0.4, unzip: 0.2, debris: 1, steam: 1, dust: 1, haze: 1, shake: 1, tint: '#ffffff', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | width: [0.15, 1, 0.01], depth: [0.4, 2.5, 0.01], reach: [3, 16, 0.1], overshoot: [0, 4, 0.05], hold: [1.5, 9, 0.1], |
| +добавлено | temperature: [900, 2600, 10], intensity: [0, 3, 0.01], lips: [0, MAX_LIPS, 1], lipHeight: [0.2, 1.8, 0.01], unzip: [0.02, 0.8, 0.01], |
| +добавлено | debris: [0, 3, 0.01], steam: [0, 3, 0.01], dust: [0, 3, 0.01], haze: [0, 3, 0.01], shake: [0, 2, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | 'obsidian rift': { temperature: 1100, width: 0.3, depth: 1.8, steam: 2, intensity: 1.3, lipHeight: 0.55 }, |
| +добавлено | cataclysm: { width: 0.75, reach: 13, lips: 8, lipHeight: 1.25, debris: 1.7, dust: 1.5, shake: 1.6, unzip: 0.32, hold: 5.5 }, |
| +добавлено | 'sulfur vent': { tint: '#8fb0ff', temperature: 2400, steam: 2.4, debris: 0.7, intensity: 0.8 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 0.9, impact: 0.35, linger: 6 }, |
| +добавлено | kinds: { |
| +добавлено | // (the landing heat is kept low: thousands of drops summed past the field's crust regime into |
| +добавлено | // flat bright orange puddles; a spill beside a lava channel crusts dark, glowing only in its cracks) |
| +добавлено | // magma droplets flung out of the crack: white-yellow, cooling to red, heat where they land |
| +добавлено | spatter: { style: 'glow', drag: 0.25, gravity: 9.8, killOnCollide: true, life: [0.7, 1.5], size: [0.026, 0.012], stretch: 0.045, emissive: 2.6, temp: [1500, 820], paints: 'heat', paintAmount: 0.022, fadeOut: 0.2 }, |
| +добавлено | // fat lava clots: real rock lumps that glow as they fly and crust black where they land |
| +добавлено | glob: { style: 'debris', mesh: 'rock', gravity: 9.8, drag: 0.06, restitution: 0.12, friction: 0.8, life: [2.6, 3.8], size: [0.075, 0.075], spin: 5, color: ['#17100c', new THREE.Color(7, 1.3, 0.16)], alpha: [1, 0.85], fadeOut: 0.15, forceResponse: 0, paints: 'heat', paintAmount: 0.035 }, |
| +добавлено | // the curtain of fire: a sheet of fountaining lava along the whole cut (real fissure eruptions, |
| +добавлено | // Kilauea 2018 / Sundhnukur 2024): clots rise, slow, fall back, cooling from yellow to red |
| +добавлено | fountain: { style: 'glow', drag: 0.35, gravity: 9.8, killOnCollide: true, life: [0.8, 1.5], size: [0.028, 0.014], stretch: 0.05, emissive: 2.6, temp: [1450, 880], paints: 'heat', paintAmount: 0.011, fadeOut: 0.2 }, |
| +добавлено | // volcanic gas over the curtain: blue-grey, lit orange from below by the fountain |
| +добавлено | gas: { style: 'media', emissive: 0, drag: 1.1, buoyancy: 3.2, curl: 1.1, curlScale: 0.25, life: [2.4, 3.8], size: [0.45, 2.6], sizePow: 0.5, color: ['#a3a7ad', '#757b84'], alpha: [0.085, 0], density: 1.3, erosion: 0.65, fadeIn: 0.1, fadeOut: 0.55, forceResponse: 0.8, lightResponse: 1.6 }, |
| +добавлено | // steam: lit (the magma light colours it from below), rolls up fast and thins |
| +добавлено | steam: { style: 'media', emissive: 0, drag: 1.5, buoyancy: 2.6, curl: 1.4, curlScale: 0.35, life: [1.3, 2.6], size: [0.22, 1.5], sizePow: 0.5, color: ['#cfc9c4', '#8f8984'], alpha: [0.13, 0], density: 1.4, erosion: 0.7, fadeIn: 0.08, fadeOut: 0.5, forceResponse: 0.8, lightResponse: 1 }, |
| +добавлено | // heat haze: almost no coverage, all distortion |
| +добавлено | // dust: dark rock flour; barely takes the magma light so it reads as smoke, not glow |
| +добавлено | dust: { style: 'media', emissive: 0, drag: 2.2, buoyancy: -0.2, curl: 0.6, curlScale: 0.2, life: [2.2, 3.8], size: [0.4, 1.8], sizePow: 0.55, color: ['#4a3f36', '#3a322c'], alpha: [0.2, 0.08], density: 2.2, erosion: 0.55, fadeIn: 0.05, fadeOut: 0.6, forceResponse: 0.5, lightResponse: 0.35 }, |
| +добавлено | haze: { style: 'media', emissive: 0, drag: 2.5, buoyancy: 1.8, curl: 0.8, curlScale: 0.5, life: [0.8, 1.5], size: [0.5, 1.1], color: ['#ffffff', '#ffffff'], alpha: [0.018, 0], density: 0.6, erosion: 0.3, distortion: 40, fadeIn: 0.25, fadeOut: 0.4, forceResponse: 0.6 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const line = (kind, spread, radius = 0.12) => P.emitter(kind, { shape: 'line', radius, spread }); |
| +добавлено | const pebbleLine = line('earth.pebble', 0.9), seepLine = line('earth.seep', 0.25, 0.1), emberLine = line('earth.ember', 1.2, 0.08); |
| +добавлено | const rockSeg = line('earth.rock', 1.3), chunkSeg = line('earth.chunk', 0.9), magmaSeg = line('earth.magma', 1.4); |
| +добавлено | const spatterSeg = line('fissure.spatter', 1.8, 0.08), globSeg = line('fissure.glob', 2.2, 0.1), steamSeg = line('fissure.steam', 0.8, 0.15), steamLine = line('fissure.steam', 0.35, 0.1); |
| +добавлено | const dustSeg = line('fissure.dust', 0.7, 0.2), plumeSeg = line('fissure.dust', 0.9, 0.2), hazeLine = line('fissure.haze', 0.3, 0.15); |
| +добавлено | const fountainSeg = line('fissure.fountain', 0.75, 0.07), gasLine = line('fissure.gas', 0.5, 0.25); |
| +добавлено | const ventSteam = P.emitter('fissure.steam', { shape: 'cone', radius: 0.12, spread: 0.6, radial: 4, cone: 0.25 }); |
| +добавлено | const ventSpatter = P.emitter('fissure.spatter', { shape: 'cone', radius: 0.08, spread: 1.2, radial: 5.5, cone: 0.35 }); |
| +добавлено | const ventMagma = P.emitter('earth.magma', { shape: 'cone', radius: 0.1, spread: 0.8, radial: 4.5, cone: 0.4 }); |
| +добавлено | |
| +добавлено | // --- the rift decals: one program, per-mesh uniforms --- |
| +добавлено | const profile = buildProfile(); |
| +добавлено | const profileTex = new THREE.DataTexture(profile, PROFILE_N, 1, THREE.RGBAFormat, THREE.UnsignedByteType); |
| +добавлено | profileTex.wrapS = THREE.RepeatWrapping; |
| +добавлено | profileTex.magFilter = profileTex.minFilter = THREE.LinearFilter; |
| +добавлено | profileTex.needsUpdate = true; |
| +добавлено | const tint = new THREE.Vector3(1, 1, 1), tintCol = new THREE.Color(1, 1, 1); |
| +добавлено | const quad = new THREE.PlaneGeometry(2, 2, 1, 1); |
| +добавлено | const cracks = []; |
| +добавлено | for (let i = 0; i < MAX_CRACKS; i++) { |
| +добавлено | const m = new THREE.Mesh(quad, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: CRACK_VERT, fragmentShader: CRACK_FRAG, alphaToCoverage: true, side: THREE.DoubleSide, |
| +добавлено | uniforms: { |
| +добавлено | ...U, uProfile: { value: profileTex }, uA: { value: new THREE.Vector3() }, uT: { value: new THREE.Vector2(1, 0) }, |
| +добавлено | uQuad: { value: new THREE.Vector4() }, uCrackA: { value: new THREE.Vector4() }, uCrackB: { value: new THREE.Vector4() }, |
| +добавлено | uCrackC: { value: new THREE.Vector4() }, uCrackD: { value: new THREE.Vector4() }, uTint: { value: tint }, |
| +добавлено | }, |
| +добавлено | })); |
| +добавлено | m.frustumCulled = false; |
| +добавлено | m.visible = false; |
| +добавлено | m.renderOrder = -0.5; // after the spikes (lips), before the floor would be ideal; the floor is order 0 |
| +добавлено | fx.scenes.opaque.add(m); |
| +добавлено | cracks.push(m); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- per-cast state --- |
| +добавлено | const lipState = () => ({ h: null, seed: 0, u: 0, pos: new THREE.Vector3(), dir: new THREE.Vector3(), height: 1, radius: 0.4, delay: 0 }); |
| +добавлено | const states = Array.from({ length: STATES }, () => ({ |
| +добавлено | crack: null, A: new THREE.Vector3(), B: new THREE.Vector3(), T: new THREE.Vector3(), N: new THREE.Vector3(), head: new THREE.Vector3(), |
| +добавлено | L: 1, W: 0.4, offs: 0, split: false, te: 0, splatT: 0, heatT: 0, heat: 0, close: 1, |
| +добавлено | lips: Array.from({ length: MAX_LIPS * 2 }, lipState), nLips: 0, seg: new Uint8Array(SEGS), |
| +добавлено | vents: Array.from({ length: VENTS }, () => ({ u: 0, next: 0 })), |
| +добавлено | light: null, shock: null, up0: null, up1: null, shockT: 0, |
| +добавлено | })); |
| +добавлено | |
| +добавлено | function edgeAt(d, u, open) { |
| +добавлено | const x = (u / PERIOD + d.offs) % 1, f = x * PROFILE_N - 0.5; |
| +добавлено | let i0 = Math.floor(f); |
| +добавлено | const t = f - i0; |
| +добавлено | i0 = (i0 + PROFILE_N) % PROFILE_N; |
| +добавлено | const a = i0 * 4, b = ((i0 + 1) % PROFILE_N) * 4, t1 = (1 - t) / 255, t2 = t / 255; |
| +добавлено | const xx = Math.min(1, Math.max(0, u / d.L)), env = Math.pow(Math.max(Math.sin(Math.PI * xx), 0), 0.55); |
| +добавлено | const w = d.W * open * env; |
| +добавлено | EDGE.c = ((profile[a] * t1 + profile[b] * t2) - 0.5) * 2 * meander(d) * env; |
| +добавлено | EDGE.hl = w * (0.5 + profile[a + 1] * t1 + profile[b + 1] * t2); |
| +добавлено | EDGE.hr = w * (0.5 + profile[a + 2] * t1 + profile[b + 2] * t2); |
| +добавлено | return EDGE; |
| +добавлено | } |
| +добавлено | const meander = (d) => 0.35 * d.W + 0.08; |
| +добавлено | // world point at (u, v) on the crack's frame |
| +добавлено | const at = (d, u, v, y, out) => out.copy(d.A).addScaledVector(d.T, u).addScaledVector(d.N, v).setY(y); |
| +добавлено | |
| +добавлено | function splat(x, z, x2, z2, width, crack, heat, char, pattern, seed, radius = 0.3) { |
| +добавлено | SPL.x = x; SPL.z = z; SPL.x2 = x2; SPL.z2 = z2; SPL.width = width; SPL.radius = radius; SPL.crack = crack; SPL.heat = heat; SPL.char = char; |
| +добавлено | SPL.pattern = pattern; SPL.seed = seed; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | function shock(pos, r1, duration, distortion) { |
| +добавлено | SHK.pos.copy(pos).setY(0.04); SHK.radius1 = r1; SHK.duration = duration; SHK.distortion = distortion; SHK.thickness = 0.35 + r1 * 0.08; |
| +добавлено | fx.shocks?.spawn(SHK); |
| +добавлено | } |
| +добавлено | // distance from p to the crack segment (xz), and the side it is on |
| +добавлено | const LD = { d: 0, side: 1 }; |
| +добавлено | function lineDist(d, p) { |
| +добавлено | _c.set(p.x - d.A.x, 0, p.z - d.A.z); |
| +добавлено | const u = Math.min(d.L, Math.max(0, _c.dot(d.T))); |
| +добавлено | LD.d = Math.hypot(p.x - (d.A.x + d.T.x * u), p.z - (d.A.z + d.T.z * u)); |
| +добавлено | LD.side = Math.sign(_c.dot(d.N)) || 1; |
| +добавлено | return LD; |
| +добавлено | } |
| +добавлено | // emit between _a and _b (a burst) or along the whole cut (a rate) |
| +добавлено | function emitAB(e, n, vy, size) { e.pos.copy(_a); e.p.copy(_b); e.vel.set(0, vy, 0); e.size = size; e.burst(n); } |
| +добавлено | function emitSide(d, e, n, side, off, out, vy, size) { |
| +добавлено | e.pos.copy(_a).addScaledVector(d.N, side * off); e.p.copy(_b).addScaledVector(d.N, side * off); |
| +добавлено | e.vel.copy(d.N).multiplyScalar(side * out).setY(vy); e.size = size; e.burst(n); |
| +добавлено | } |
| +добавлено | function alongHead(d, e, n, vy, dt) { e.pos.copy(d.A).setY(0.05); e.p.copy(d.head).setY(0.05); e.vel.set(0, vy, 0); e.rate(n, dt); } |
| +добавлено | function alongCut(d, e, n, vy, y, dt) { e.pos.copy(d.A).addScaledVector(d.T, 0.4).setY(y); e.p.copy(d.B).addScaledVector(d.T, -0.4).setY(y); e.vel.set(0, vy, 0); e.rate(n, dt); } |
| +добавлено | |
| +добавлено | // tinted blackbody for the spatter kind (the kind table has no tint), written as texels |
| +добавлено | let lastT = -1, lastTint = ''; |
| +добавлено | const HOT = { white: { temp: [0, 0] }, tinted: { temp: null, color: [new THREE.Color(), new THREE.Color()] } }; |
| +добавлено | const GLOB = { color: [new THREE.Color('#17100c'), new THREE.Color()] }; |
| +добавлено | function retint() { |
| +добавлено | const p = S.params; |
| +добавлено | if (p.temperature === lastT && p.tint === lastTint) return; |
| +добавлено | lastT = p.temperature; lastTint = p.tint; |
| +добавлено | tintCol.set(p.tint); tint.set(tintCol.r, tintCol.g, tintCol.b); |
| +добавлено | const T = p.temperature * 1.1; |
| +добавлено | if (tintCol.r > 0.97 && tintCol.g > 0.97 && tintCol.b > 0.97) { HOT.white.temp[0] = T; HOT.white.temp[1] = T * 0.55; P.setKind('fissure.spatter', HOT.white); P.setKind('fissure.fountain', HOT.white); } |
| +добавлено | else { |
| +добавлено | blackbodyRGB(T, HOT.tinted.color[0]).multiply(tintCol); |
| +добавлено | blackbodyRGB(T * 0.55, HOT.tinted.color[1]).multiply(tintCol).multiplyScalar(0.55 ** 3); |
| +добавлено | P.setKind('fissure.spatter', HOT.tinted); P.setKind('fissure.fountain', HOT.tinted); |
| +добавлено | } |
| +добавлено | // the clots' glow: blackbody at the magma temperature (blackbodyRGB is ~3.4 bright at 1350 K), tinted |
| +добавлено | blackbodyRGB(p.temperature, GLOB.color[1]).multiply(tintCol).multiplyScalar(1.5 * Math.pow(p.temperature / 1300, 4)); // ~ (5, 0.55, 0) at 1350 K, like earth.magma |
| +добавлено | P.setKind('fissure.glob', GLOB); |
| +добавлено | } |
| +добавлено | retint(); |
| +добавлено | |
| +добавлено | function layout(c, p, d) { |
| +добавлено | d.T.set(c.to.x - c.from.x, 0, c.to.z - c.from.z); |
| +добавлено | const dist = d.T.length(); |
| +добавлено | if (dist < 1e-3) d.T.set(0, 0, -1); else d.T.multiplyScalar(1 / dist); |
| +добавлено | d.N.set(-d.T.z, 0, d.T.x); |
| +добавлено | const startD = Math.min(Math.max(dist - p.reach, 1.6), Math.max(dist - 1.5, 0.5)); |
| +добавлено | d.A.set(c.from.x, 0, c.from.z).addScaledVector(d.T, startD); |
| +добавлено | d.B.set(c.to.x, 0, c.to.z).addScaledVector(d.T, p.overshoot); |
| +добавлено | // stay inside the rim wall |
| +добавлено | const rim = fx.ARENA.radius - 1; |
| +добавлено | if (d.B.length() > rim) d.B.setLength(rim); |
| +добавлено | d.L = Math.max(d.B.distanceTo(d.A), 1.5); |
| +добавлено | d.W = p.width; |
| +добавлено | d.offs = (c.seed % 997) / 997; |
| +добавлено | d.head.copy(d.A); |
| +добавлено | // lips: broken slabs along both edges, heaved up and leaning away from the cut |
| +добавлено | const n = Math.min(MAX_LIPS, Math.round(p.lips)); |
| +добавлено | d.nLips = 0; |
| +добавлено | for (let side = -1; side <= 1; side += 2) { |
| +добавлено | for (let k = 0; k < n; k++) { |
| +добавлено | const s = d.lips[d.nLips++]; |
| +добавлено | s.u = d.L * Math.min(0.96, Math.max(0.04, (k + 0.25 + 0.5 * fx.rand() + (side > 0 ? 0.35 : 0)) / (n + 0.35))); |
| +добавлено | const e = edgeAt(d, s.u, 1), x = s.u / d.L, env = Math.pow(Math.max(Math.sin(Math.PI * x), 0), 0.5); |
| +добавлено | s.radius = p.width * (0.7 + 0.35 * fx.rand()) * (0.6 + 0.4 * env) + 0.12; |
| +добавлено | s.height = p.lipHeight * (0.5 + 0.5 * env) * (0.7 + 0.5 * fx.rand()); |
| +добавлено | at(d, s.u, e.c + side * ((side > 0 ? e.hr : e.hl) + s.radius * 0.55), 0, s.pos); |
| +добавлено | s.dir.copy(UP).addScaledVector(d.N, side * (1.4 + 0.6 * fx.rand())).addScaledVector(d.T, (fx.rand() - 0.5) * 0.6).normalize(); |
| +добавлено | s.delay = x * p.unzip + 0.02 + 0.03 * fx.rand(); |
| +добавлено | s.seed = ((c.seed * 31 + d.nLips * 7919) >>> 0) & 0xffffff || 1; |
| +добавлено | s.h = null; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | for (let k = 0; k < VENTS; k++) { d.vents[k].u = d.L * (0.08 + 0.84 * (k + fx.rand()) / VENTS); d.vents[k].next = 0.25 + fx.rand() * 0.8; } |
| +добавлено | d.seg.fill(0); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function setCrack(d, p, front, te, close, heat) { |
| +добавлено | const m = d.crack; |
| +добавлено | if (!m) return; |
| +добавлено | const u = m.material.uniforms; |
| +добавлено | u.uA.value.copy(d.A); u.uT.value.set(d.T.x, d.T.z); |
| +добавлено | u.uQuad.value.set(-0.1, d.L + 0.1, d.W * 1.5 + meander(d) + 0.05, 0.004); |
| +добавлено | u.uCrackA.value.set(d.L, d.W, p.depth, front); |
| +добавлено | u.uCrackB.value.set(te, p.unzip, close, heat); |
| +добавлено | u.uCrackC.value.set(d.offs, meander(d), 0.022, (d.offs * 37) % 1); |
| +добавлено | u.uCrackD.value.set(p.intensity, p.temperature, p.haze, U.uTime.value); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // one stretch of the cut tears open: rock, magma, spatter, steam, dust |
| +добавлено | function tearSegment(c, p, d, k) { |
| +добавлено | const u0 = (k / SEGS) * d.L, u1 = ((k + 1) / SEGS) * d.L, w = p.width, sz = Math.sqrt(w / 0.42); |
| +добавлено | at(d, u0, edgeAt(d, u0, 1).c, 0.15, _a); at(d, u1, edgeAt(d, u1, 1).c, 0.15, _b); |
| +добавлено | emitAB(spatterSeg, 45 * p.debris, 6.2, 1); |
| +добавлено | emitAB(globSeg, 14 * p.debris, 8, 1); |
| +добавлено | emitAB(steamSeg, 14 * p.steam, 3.5, 1.1 * sz); |
| +добавлено | emitAB(plumeSeg, 6 * p.dust, 2.6, sz); |
| +добавлено | // solids are torn off the lips and thrown outward (anything landing in the cut would rest on |
| +добавлено | // the floor plane, floating over the trench); dust rolls out along the floor |
| +добавлено | for (let side = -1; side <= 1; side += 2) { |
| +добавлено | emitSide(d, rockSeg, 10 * p.debris, side, w * 1.2, 1.8 * sz, 3.4 * sz, sz); |
| +добавлено | emitSide(d, chunkSeg, 3 * p.debris, side, w * 1.3, 1.8 * sz, 2.8 * sz, sz); |
| +добавлено | emitSide(d, magmaSeg, 6 * p.debris, side, w * 1.1, 2.4 * sz, 4.2 * sz, 1); |
| +добавлено | emitSide(d, dustSeg, 12 * p.dust, side, w * 2.4, 4.5 * sz, 0.35, sz); |
| +добавлено | } |
| +добавлено | if (k === 1 || k === 4 || k === SEGS - 1) shock(_c.lerpVectors(_a, _b, 0.5), (k === SEGS - 1 ? 5 : 3.2) * sz, 0.45, 0.05); |
| +добавлено | fx.rig.shake(0.06 * p.shake, 9); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function split(c, p, d) { |
| +добавлено | d.split = true; d.te = 0; |
| +добавлено | const sz = Math.sqrt(p.width / 0.42); |
| +добавлено | for (let i = 0; i < d.nLips; i++) { |
| +добавлено | const s = d.lips[i]; |
| +добавлено | SPK.pos.copy(s.pos); SPK.dir.copy(s.dir); SPK.height = s.height; SPK.radius = s.radius; |
| +добавлено | SPK.delay = s.delay; SPK.life = Math.max(0.6, p.hold - s.delay + 0.2); SPK.heat = 0.45; SPK.seed = s.seed; |
| +добавлено | s.h = fx.spikes.spawn(SPK); |
| +добавлено | } |
| +добавлено | // the floor: the cut cracks the vein network for metres around, heat and char along it (the |
| +добавлено | // heat kept in the crust regime: more lit the cracked floor beside the cut as flat orange puddles) |
| +добавлено | splat(d.A.x, d.A.z, d.B.x, d.B.z, p.width * 1.8, 2.1, 0.2, 0.5, 'line', c.seed % 97); |
| +добавлено | splat(d.A.x, d.A.z, d.A.x, d.A.z, 0, 1.7, 0.2, 0.2, 'radial', (c.seed + 13) % 97, 1.3 * sz); |
| +добавлено | splat(d.B.x, d.B.z, d.B.x, d.B.z, 0, 1.8, 0.2, 0.2, 'radial', (c.seed + 29) % 97, 1.5 * sz); |
| +добавлено | for (let k = 0; k < 3; k++) fx.impacts.add('magma', at(d, d.L * (0.2 + 0.3 * k), 0, 0.2, _a), 1.4 * sz + 0.4, 0.55, p.hold + 1.5, blackbodyRGB(p.temperature, _col).multiply(tintCol)); |
| +добавлено | // a pressure push on everything loose, then a thermal over the crack |
| +добавлено | d.shock = fx.forces.acquire(); |
| +добавлено | if (d.shock) { d.shock.type = 4; d.shock.p[0] = 0.5; d.shock.p[1] = 1.5; d.shock.p[2] = 28; d.shock.p[3] = 0; at(d, d.L * 0.5, 0, 0.4, d.shock.pos); } |
| +добавлено | d.shockT = 0; |
| +добавлено | for (let k = 0; k < 2; k++) { |
| +добавлено | const f = fx.forces.acquire(); |
| +добавлено | if (!f) break; |
| +добавлено | f.type = 5; f.dir.set(0, 1, 0); f.p[0] = 2.6; f.p[1] = d.L * 0.27 + 0.4; f.p[2] = 4.5; f.p[3] = 0; at(d, d.L * (0.25 + 0.5 * k), 0, -0.3, f.pos); |
| +добавлено | if (k === 0) d.up0 = f; else d.up1 = f; |
| +добавлено | } |
| +добавлено | // knockback away from the line, the nearest catch fire |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const ld = lineDist(d, dm.pos), reach = 1.4 + p.width * 2; |
| +добавлено | if (ld.d < reach) { |
| +добавлено | dm.hit(_b.copy(d.N).multiplyScalar(ld.side).addScaledVector(UP, 0.3), 1.5 * (1 - ld.d / reach) + 0.3); |
| +добавлено | if (ld.d < reach * 0.6) dm.status.burn = Math.max(dm.status.burn, 0.8); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // the pool hisses if the crack reaches it |
| +добавлено | const pool = fx.ARENA.pool; |
| +добавлено | const px = Math.min(Math.max(d.B.x, pool.center.x - pool.half.x), pool.center.x + pool.half.x), pz = Math.min(Math.max(d.B.z, pool.center.y - pool.half.y), pool.center.y + pool.half.y); |
| +добавлено | _a.set(px, 0, pz); |
| +добавлено | if (lineDist(d, _a).d < 1.2) { |
| +добавлено | fx.pool?.ripple?.(px, pz, 0.08, 1.2); |
| +добавлено | ventSteam.pos.set(px, 0.1, pz); ventSteam.p.set(0, 1, 0); ventSteam.size = 1.6; ventSteam.burst(160 * p.steam); |
| +добавлено | } |
| +добавлено | fx.rig.shake(0.55 * p.shake, 7); |
| +добавлено | fx.rig.shake(0.25 * p.shake, 16); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(2.2, 0.12); fx.rig.hitstop(0.05); } |
| +добавлено | fx.flash(0.12 * p.intensity); |
| +добавлено | const L = d.light; |
| +добавлено | if (L) { L.type = 1; L.range = 6 + 3 * p.width; L.shadow = true; L.intensity = 9 * p.intensity; at(d, 0.3, 0, 0.05, L.pos); at(d, d.L - 0.3, 0, 0.05, L.end); } |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'tint' || k === 'temperature') retint(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params; |
| +добавлено | if (name === 'charge') { |
| +добавлено | retint(); |
| +добавлено | const d = c.data.f = states[c.index]; |
| +добавлено | d.split = false; d.te = 0; d.splatT = 0; d.heatT = 0; d.heat = 0; d.close = 1; d.shockT = 0; |
| +добавлено | for (const s of d.lips) s.h = null; |
| +добавлено | layout(c, p, d); |
| +добавлено | c.durations.linger = p.hold + CLOSE + 0.8; |
| +добавлено | d.crack = null; |
| +добавлено | for (const m of cracks) if (!m.visible) { d.crack = m; m.visible = true; break; } |
| +добавлено | setCrack(d, p, 0, -1, 1, 0.3); |
| +добавлено | d.light = fx.lights.acquire(); |
| +добавлено | if (d.light) { d.light.type = 0; d.light.range = 4; d.light.intensity = 0; d.light.shadow = false; blackbodyRGB(p.temperature, d.light.color).multiply(tintCol); } |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | split(c, p, c.data.f); |
| +добавлено | } else if (name === 'done') { |
| +добавлено | const d = c.data.f; |
| +добавлено | if (!d) return; |
| +добавлено | for (const s of d.lips) { if (s.h && s.h.active && s.h.seed === s.seed) s.h.release(); s.h = null; } |
| +добавлено | if (d.crack) d.crack.visible = false; |
| +добавлено | d.crack = null; |
| +добавлено | d.light?.release(); d.shock?.release(); d.up0?.release(); d.up1?.release(); |
| +добавлено | d.light = d.shock = d.up0 = d.up1 = null; |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = c.data.f, L = d.light; |
| +добавлено | if (!d.split) { |
| +добавлено | // the hairline races to the target and past it |
| +добавлено | const k = c.t / c.durations.charge, front = smooth(k / 0.85); |
| +добавлено | d.head.lerpVectors(d.A, d.B, front); |
| +добавлено | setCrack(d, p, front, -1, 1, 0.25 + 0.35 * k); |
| +добавлено | d.splatT -= dt; |
| +добавлено | if (d.splatT <= 0) { d.splatT = 0.035; splat(d.A.x, d.A.z, d.head.x, d.head.z, 0.35, 1.5, 0.25 * k, 0, 'line', c.seed % 97); } |
| +добавлено | alongHead(d, pebbleLine, 520 * p.debris, 2.2, dt); |
| +добавлено | alongHead(d, seepLine, 300 * p.dust, 0.6, dt); |
| +добавлено | alongHead(d, emberLine, 160 * k, 1.2, dt); |
| +добавлено | fx.rig.shake(dt * 2.2 * p.shake * (0.4 + k), 8); |
| +добавлено | if (L) { L.pos.copy(d.head).setY(0.25); L.intensity = 7 * p.intensity * k * (0.8 + 0.2 * Math.sin(c.age * 37)); } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | |
| +добавлено | d.te += dt; |
| +добавлено | const te = d.te, hold = p.hold; |
| +добавлено | // tear each stretch as the unzip passes it |
| +добавлено | for (let k = 0; k < SEGS; k++) if (!d.seg[k] && te >= ((k + 0.5) / SEGS) * p.unzip) { d.seg[k] = 1; tearSegment(c, p, d, k); } |
| +добавлено | // cooling: full heat for a moment, sags over the hold, then crusts over and closes |
| +добавлено | const cl = smooth((te - hold) / CLOSE); |
| +добавлено | d.close = 1 - smooth((te - hold - 0.25 * CLOSE) / (0.75 * CLOSE)); |
| +добавлено | d.heat = (1 - 0.4 * smooth((te - 0.8) / Math.max(hold - 0.8, 0.1))) * (1 - cl); |
| +добавлено | const flick = 0.82 + 0.12 * Math.sin(c.age * 23 + c.seed) * Math.sin(c.age * 9.7) + 0.06 * Math.sin(c.age * 61); |
| +добавлено | setCrack(d, p, 1, te, d.close, d.heat * (0.94 + 0.06 * flick)); |
| +добавлено | if (d.crack && d.close <= 0) { d.crack.visible = false; d.crack = null; } |
| +добавлено | for (let i = 0; i < d.nLips; i++) { const s = d.lips[i]; if (s.h && s.h.active && s.h.seed === s.seed) s.h.heat = 0.45 * d.heat; } |
| +добавлено | |
| +добавлено | // the floor stays hot along the crack while it is open |
| +добавлено | d.heatT -= dt; |
| +добавлено | if (d.heatT <= 0 && d.heat > 0.05) { d.heatT = 0.3; splat(d.A.x, d.A.z, d.B.x, d.B.z, p.width * 1.4, 0, 0.12 * d.heat, 0.05, 'line', c.seed % 97); } |
| +добавлено | |
| +добавлено | // vents: steam puffs, magma spurts |
| +добавлено | const on = d.heat * (1 - cl); |
| +добавлено | for (let k = 0; k < VENTS; k++) { |
| +добавлено | const v = d.vents[k]; |
| +добавлено | v.next -= dt; |
| +добавлено | if (v.next > 0 || on < 0.05) continue; |
| +добавлено | v.next = 0.35 + fx.rand() * 0.9; |
| +добавлено | const e = edgeAt(d, v.u, d.close); |
| +добавлено | at(d, v.u, e.c, 0.08, _a); |
| +добавлено | ventSteam.pos.copy(_a); ventSteam.p.set((fx.rand() - 0.5) * 0.3, 1, (fx.rand() - 0.5) * 0.3); ventSteam.size = 1.0 + 0.5 * on; ventSteam.burst(14 * p.steam * on); |
| +добавлено | if (fx.rand() < 0.45) { |
| +добавлено | ventSpatter.pos.copy(_a); ventSpatter.p.copy(ventSteam.p); ventSpatter.burst(26 * p.debris * on); |
| +добавлено | if (fx.rand() < 0.5) { ventMagma.pos.copy(_a); ventMagma.p.copy(UP).addScaledVector(d.N, fx.rand() < 0.5 ? -0.9 : 0.9); ventMagma.burst(3 * p.debris); } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // the curtain of fire: fountains along the whole cut, each stretch pulsing to its own height, |
| +добавлено | // strongest just after the split and dying back to the vents within ~2 s |
| +добавлено | const fz = smooth(te / 0.1) * (1 - smooth((te - 0.5) / 1.5)) * (1 - cl); |
| +добавлено | if (fz > 0.01) { |
| +добавлено | for (let k = 0; k < 4; k++) { |
| +добавлено | const pulse = 0.5 + 0.5 * Math.sin(c.age * (5.3 + 1.7 * k) + k * 2.1 + d.offs * 20) * Math.sin(c.age * (2.9 + 0.8 * k) + k); |
| +добавлено | const u0 = d.L * (0.06 + 0.22 * k), u1 = u0 + d.L * 0.22; |
| +добавлено | at(d, u0, edgeAt(d, u0, 1).c, 0.05, _a); at(d, u1, edgeAt(d, u1, 1).c, 0.05, _b); |
| +добавлено | fountainSeg.pos.copy(_a); fountainSeg.p.copy(_b); |
| +добавлено | fountainSeg.vel.set(0, (3.2 + 4.2 * pulse) * Math.sqrt(fz) * (0.8 + 0.4 * p.intensity), 0); |
| +добавлено | fountainSeg.rate(520 * p.debris * fz * (0.5 + 0.5 * pulse), dt); |
| +добавлено | fountainSeg.vel.y *= 0.3; fountainSeg.rate(260 * p.debris * fz, dt); // the low, dense sheet at the mouth |
| +добавлено | } |
| +добавлено | alongCut(d, gasLine, 70 * p.steam * fz, 2.2, 0.9, dt); |
| +добавлено | } |
| +добавлено | // a steady breath of steam, embers and heat haze along the whole cut |
| +добавлено | if (on > 0.02) { |
| +добавлено | alongCut(d, steamLine, 26 * p.steam * on, 1.4, 0.06, dt); |
| +добавлено | alongCut(d, emberLine, 90 * on, 1.6, 0.06, dt); |
| +добавлено | if (p.haze > 0) alongCut(d, hazeLine, 70 * p.haze * on, 0.8, 0.35, dt); |
| +добавлено | } |
| +добавлено | // forces: the pressure shell expands and goes; the thermal follows the heat |
| +добавлено | if (d.shock) { |
| +добавлено | d.shockT += dt; |
| +добавлено | d.shock.p[0] = 0.5 + 9 * Math.pow(Math.min(d.shockT / 0.7, 1), 0.6); |
| +добавлено | d.shock.p[2] = 28 * (1 - d.shockT / 0.7); |
| +добавлено | if (d.shockT > 0.7) { d.shock.release(); d.shock = null; } |
| +добавлено | } |
| +добавлено | if (d.up0) d.up0.p[3] = 0.9 * on; |
| +добавлено | if (d.up1) d.up1.p[3] = 0.9 * on; |
| +добавлено | // dummies standing on it keep burning |
| +добавлено | if (on > 0.3) for (const dm of fx.dummies) if (lineDist(d, dm.pos).d < p.width + 0.45) dm.status.burn = Math.max(dm.status.burn, 0.7 * on); |
| +добавлено | if (L) L.intensity = Math.max(L.intensity * Math.exp(-dt / 0.25), 3.5 * p.intensity * on * flick); |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import { COMMON_GLSL, NOISE_GLSL } from '../../vfx/common.js'; |
| +добавлено | import { MAGMA_L } from '../earth/fissure.glsl.js'; |
| +добавлено | |
| +добавлено | // The vent: a flat disc on the floor that shows the throat below it (interior mapping, the same |
| +добавлено | // trick as the fissure's rift). Each pixel inside the jagged mouth marches its view ray down a |
| +добавлено | // funnel (radius jagged by angle, stepped into ledges with depth) and lands on a wall (dark basalt |
| +добавлено | // lit from below, red-hot near the magma, glowing veins) or on the magma itself: crust plates |
| +добавлено | // carried outward from the upwelling at the centre over bright seams. The magma level rises |
| +добавлено | // during the charge and drains as the eruption dies; then the surface crusts over and cools. |
| +добавлено | // Pixels outside the mouth are discarded after a few sines. Edges use alpha-to-coverage. |
| +добавлено | |
| +добавлено | export const VENT_VERT = /* glsl */` |
| +добавлено | uniform vec4 uVent; // centre x, z, quad half size, y |
| +добавлено | out vec3 vW; |
| +добавлено | void main() { |
| +добавлено | vW = vec3(uVent.x + position.x * uVent.z, uVent.w, uVent.y - position.y * uVent.z); |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(vW, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const VENT_FRAG = COMMON_GLSL + NOISE_GLSL + MAGMA_L + /* glsl */` |
| +добавлено | uniform vec4 uVent; |
| +добавлено | uniform vec4 uVentA; // mouth radius, throat depth, magma depth below the floor, jag |
| +добавлено | uniform vec4 uVentB; // heat 0..1, magma temperature K, intensity, time |
| +добавлено | uniform vec4 uVentC; // upwelling 0..1, seed, crust 0..1, glow haze |
| +добавлено | uniform vec3 uTint; |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | in vec3 vW; |
| +добавлено | out vec4 fragColor; |
| +добавлено | |
| +добавлено | float jagA(float a) { |
| +добавлено | float s = uVentC.y; |
| +добавлено | return 0.5 * sin(3.0 * a + s * 6.1) + 0.3 * sin(7.0 * a + s * 11.3) + 0.2 * sin(13.0 * a + s * 3.7); |
| +добавлено | } |
| +добавлено | // funnel radius at angle a, depth z (down): jagged mouth, narrowing throat, ledges |
| +добавлено | float radAt(float a, float z) { |
| +добавлено | float R = uVentA.x * (1.0 + uVentA.w * jagA(a)); |
| +добавлено | float s = clamp(z / uVentA.y, 0.0, 1.0); |
| +добавлено | float l = (0.09 * sin(z * 19.0 + a * 3.0 + uVentC.y * 5.0) + 0.05 * sin(z * 47.0 - a * 5.0)) * smoothstep(0.0, 0.06, z); |
| +добавлено | return R * (1.0 - 0.6 * pow(s, 0.8)) * (1.0 + l); |
| +добавлено | } |
| +добавлено | float inside(vec3 q) { return radAt(atan(q.z, q.x), q.y) - length(q.xz); } |
| +добавлено | |
| +добавлено | void main() { |
| +добавлено | vec3 q0 = vec3(vW.x - uVent.x, 0.0, vW.z - uVent.y); |
| +добавлено | float r0 = length(q0.xz); |
| +добавлено | float edge = radAt(atan(q0.z, q0.x), 0.0) - r0; |
| +добавлено | float cov = clamp(edge / max(fwidth(r0), 1e-4) + 0.5, 0.0, 1.0); |
| +добавлено | if (cov <= 0.0) discard; |
| +добавлено | |
| +добавлено | vec3 rd = normalize(vW - cameraPosition); |
| +добавлено | vec3 dq = vec3(rd.x, max(-rd.y, 0.02), rd.z); // local ray, y = depth (down) |
| +добавлено | float Dm = uVentA.z; |
| +добавлено | float tEnd = Dm / dq.y, tPrev = 0.0, tHit = tEnd; |
| +добавлено | bool wall = false; |
| +добавлено | for (int i = 1; i <= 12; i++) { |
| +добавлено | float t = tEnd * pow(float(i) / 12.0, 1.5); |
| +добавлено | if (inside(q0 + dq * t) < 0.0) { |
| +добавлено | float a = tPrev, b = t; |
| +добавлено | for (int j = 0; j < 5; j++) { float m = 0.5 * (a + b); if (inside(q0 + dq * m) < 0.0) b = m; else a = m; } |
| +добавлено | tHit = b; wall = true; |
| +добавлено | break; |
| +добавлено | } |
| +добавлено | tPrev = t; |
| +добавлено | } |
| +добавлено | vec3 q = q0 + dq * tHit; |
| +добавлено | float heat = uVentB.x, I = uVentB.z, time = uVentB.w; |
| +добавлено | float Tm = mix(560.0, uVentB.y, heat); |
| +добавлено | float s = clamp(q.y / Dm, 0.0, 1.0); |
| +добавлено | vec3 col; |
| +добавлено | if (wall) { |
| +добавлено | float a = atan(q.z, q.x), r = length(q.xz); |
| +добавлено | // normal from the funnel's gradient (pointing into the throat) |
| +добавлено | const float e = 0.02; |
| +добавлено | float ga = (radAt(a + e, q.y) - radAt(a - e, q.y)) / (2.0 * e * max(r, 0.05)); |
| +добавлено | float gz = (radAt(a, q.y + e) - radAt(a, q.y - e)) / (2.0 * e); |
| +добавлено | vec2 rad = q.xz / max(r, 1e-4), tan2 = vec2(-rad.y, rad.x); |
| +добавлено | vec3 nW = normalize(vec3(-rad.x + tan2.x * ga, -gz, -rad.y + tan2.y * ga)); // world: into the throat; up where it narrows |
| +добавлено | vec4 nz = noise3v(vec3(a * 1.3, q.y * 1.4, uVentC.y)); |
| +добавлено | vec4 nf = noise3v(vec3(a * 4.1, q.y * 4.0, uVentC.y + 0.5)); |
| +добавлено | float strata = 0.55 + 0.9 * nz.r * (0.75 + 0.5 * sin(q.y * 45.0 + nz.g * 9.0)); |
| +добавлено | vec3 albedo = vec3(0.05, 0.043, 0.038) * strata; |
| +добавлено | vec3 Lm = magmaL(Tm) * uTint * I * (0.25 + 0.75 * pow(s, 1.3)) * (0.7 + 0.3 * max(-nW.y, 0.0)); |
| +добавлено | col = albedo * Lm * 5.0; |
| +добавлено | float vein = smoothstep(0.035, 0.0, abs(nf.r - 0.5)) * smoothstep(0.15, 0.6, nz.g); |
| +добавлено | float Tr = mix(450.0, Tm * 0.95, pow(s, 1.3) * (0.85 + 0.3 * nf.g)) + vein * 380.0 * heat * (0.3 + 0.7 * s); |
| +добавлено | col += magmaL(Tr) * uTint * I * 0.6; |
| +добавлено | col += albedo * (uAmbient * (1.0 - s) * 1.4 + uSunColor * max(dot(nW, uSunDir), 0.0) * smoothstep(0.3, 0.0, s)); |
| +добавлено | } else { |
| +добавлено | // magma: plates carried outward from the upwelling, bright seams between them, churning |
| +добавлено | float r = length(q.xz), a = atan(q.z, q.x); |
| +добавлено | vec3 mq = vec3(q.xz * 2.4 - (q.xz / max(r, 1e-3)) * time * 0.22, time * 0.05 + uVentC.y); |
| +добавлено | vec4 n1 = noise3v(mq), n2 = noise3v(mq * 2.3 + 0.41); |
| +добавлено | float seam = 1.0 - smoothstep(0.08, 0.3, n1.g * 0.75 + n2.g * 0.25); |
| +добавлено | float crust = smoothstep(0.45, 0.62, n1.r + 0.2 * (n2.r - 0.5)) * (0.6 + 0.4 * uVentC.z); |
| +добавлено | float up = uVentC.x * exp(-r * r / max(uVentA.x * uVentA.x * 0.18, 1e-4)); |
| +добавлено | float T = Tm * mix(1.0, 0.7, crust * (1.0 - seam) * (1.0 - up)) * (1.0 + 0.12 * seam + 0.1 * up); |
| +добавлено | T *= mix(1.0, 0.78, uVentC.z * (1.0 - seam)); // the crust thickens and darkens as it cools |
| +добавлено | col = magmaL(T) * uTint * I * (1.1 + 0.4 * sin(time * 3.0 + n1.b * 12.0) * seam); |
| +добавлено | } |
| +добавлено | // hot gas in the throat scatters the glow |
| +добавлено | float path = 1.0 - exp(-tHit * 1.6); |
| +добавлено | col += magmaL(Tm) * uTint * I * uVentC.w * 0.7 * path * (0.5 + 0.5 * s); |
| +добавлено | fragColor = vec4(col, cov); |
| +добавлено | }`; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { U } from '../../vfx/common.js'; |
| +добавлено | import { blackbodyRGB } from '../../vfx/noise.js'; |
| +добавлено | import { VENT_VERT, VENT_FRAG } from './lava-geyser.glsl.js'; |
| +добавлено | |
| +добавлено | // Lava Geyser (earth + fire). Reference: Hawaiian lava fountains (Kilauea 2018 fissure 8, |
| +добавлено | // Halema'uma'u 2021-24 episodes) and Fagradalsfjall 2021: a column of incandescent clots, yellow |
| +добавлено | // at the vent and redder as it rises, decelerating to a ragged top and falling back as a fan; |
| +добавлено | // bombs arcing out on their own, darkening as they fly and landing as black rock that still glows |
| +добавлено | // in its cracks; a blue-grey gas plume lit orange from below. |
| +добавлено | // Anticipation: the ground bulges and cracks radially, a spatter rampart heaves up around a |
| +добавлено | // mouth that opens over rising magma (an interior-mapped throat, lava-geyser.glsl.js), steam and |
| +добавлено | // embers seep, the floor rumbles, a throat-clearing spurt. Release: the fountain bursts out with |
| +добавлено | // a pressure shock, rock and bombs, knockback, fire. Linger: the fountain surges and sags, bombs |
| +добавлено | // keep arcing out, the plume builds and the column lights the arena like a vertical lamp. |
| +добавлено | // Aftermath: the fountain dies back to spatter, the magma drains and crusts over, the rampart |
| +добавлено | // crumbles, the landed bombs cool to black. |
| +добавлено | |
| +добавлено | const STATES = 16, MAX_VENTS = 3, MAX_RIM = 10, BOMBS = 6; |
| +добавлено | const SUBSIDE = 1.6; // s at the end of `duration` while the fountain dies back |
| +добавлено | const COOL = 2.4; // s after that: drains, crusts, crumbles |
| +добавлено | const UP = new THREE.Vector3(0, 1, 0); |
| +добавлено | const smooth = (x) => { x = Math.min(1, Math.max(0, x)); return x * x * (3 - 2 * x); }; |
| +добавлено | |
| +добавлено | // reused option objects: the shared systems read fields, a literal per call would allocate |
| +добавлено | const SPL = { x: 0, z: 0, radius: 1, heat: 0, char: 0, wet: 0, frost: 0, crack: 0, acid: 0, corruption: 0, glow: 0, pattern: 'radial', seed: 0, angle: 0, x2: 0, z2: 0, width: 0 }; |
| +добавлено | const SPK = { pos: new THREE.Vector3(), dir: new THREE.Vector3(), height: 1, radius: 0.4, material: 'rock', delay: 0, rise: 0.6, life: 3, crumble: 1.0, heat: 0.3, seed: 1 }; |
| +добавлено | const SHK = { pos: new THREE.Vector3(), radius0: 0.3, radius1: 4, duration: 0.5, thickness: 0.5, distortion: 0.05, color: null, intensity: 0, ground: true }; |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _col = new THREE.Color(); |
| +добавлено | |
| +добавлено | // The plume (gas column, ash bursts, heat haze over the fountain) sits behind this small interface |
| +добавлено | // so it can move onto the shared fluid volume (src/vfx/FluidVolume.js) when that lands: |
| +добавлено | // burst(pos, top, amount) an eruption pulse of dark ash |
| +добавлено | // update(pos, top, dt, strength, p) the sustained gas column over a fountain `top` m tall |
| +добавлено | function makePlume(P) { |
| +добавлено | const gas = P.emitter('lava-geyser.gas', { shape: 'disk', radius: 0.35, spread: 0.7 }); |
| +добавлено | const ash = P.emitter('lava-geyser.ash', { shape: 'sphere', radius: 0.35, spread: 1.4, radial: 1.8 }); |
| +добавлено | const haze = P.emitter('lava-geyser.haze', { shape: 'disk', radius: 0.35, spread: 0.3 }); |
| +добавлено | return { |
| +добавлено | burst(pos, top, amount) { ash.pos.copy(pos).setY(Math.max(top * 0.55, 0.8)); ash.vel.set(0, 3.2, 0); ash.burst(amount); }, |
| +добавлено | update(pos, top, dt, strength, p) { |
| +добавлено | if (strength <= 0.01) return; |
| +добавлено | gas.pos.copy(pos).setY(top * 0.45 + 0.4); gas.vel.set(0, 2.4, 0); gas.size = 0.8 + 0.25 * top / 6; |
| +добавлено | gas.rate(40 * p.plume * strength, dt); |
| +добавлено | if (p.haze > 0) { haze.pos.copy(pos).setY(0.5); haze.vel.set(0, 1.4, 0); haze.rate(36 * p.haze * strength, dt); } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | } |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'lava-geyser', name: 'Lava Geyser', element: 'earth', order: 20, hue: '#ff5a14', |
| +добавлено | shape: 'area', |
| +добавлено | params: { |
| +добавлено | height: 6, width: 0.4, duration: 4, temperature: 1400, intensity: 1, surge: 1, bombs: 1, spatter: 1, |
| +добавлено | plume: 1, haze: 1, rim: 7, rimHeight: 0.55, shake: 1, tint: '#ffffff', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | height: [2, 12, 0.1], width: [0.12, 0.8, 0.01], duration: [1.5, 9, 0.1], temperature: [1000, 2600, 10], intensity: [0, 3, 0.01], |
| +добавлено | surge: [0, 3, 0.01], bombs: [0, 3, 0.01], spatter: [0, 3, 0.01], plume: [0, 3, 0.01], haze: [0, 3, 0.01], |
| +добавлено | rim: [0, MAX_RIM, 1], rimHeight: [0.2, 1.6, 0.01], shake: [0, 2, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | 'hawaiian fountain': { height: 9.5, width: 0.26, surge: 0.5, bombs: 0.6, plume: 0.8, duration: 5.5, rimHeight: 0.45 }, |
| +добавлено | strombolian: { height: 5, width: 0.36, surge: 2.4, bombs: 2.2, plume: 1.7, spatter: 0.7, rimHeight: 0.85, rim: 9 }, |
| +добавлено | 'sulfur blue': { tint: '#7fa6ff', temperature: 1750, height: 4.2, plume: 1.8, bombs: 0.4, intensity: 0.9 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 1.1, impact: 0.3, linger: 8 }, |
| +добавлено | kinds: { |
| +добавлено | // the fountain: incandescent clots, yellow at the vent, red as they rise, slow and fall back |
| +добавлено | jet: { style: 'glow', drag: 0.18, gravity: 9.8, killOnCollide: true, life: [2.2, 3.2], size: [0.075, 0.035], stretch: 0.045, emissive: 2.4, temp: [1480, 1060], paints: 'heat', paintAmount: 0.03, fadeIn: 0.02, fadeOut: 0.15 }, |
| +добавлено | // the dense, hottest base of the column |
| +добавлено | core: { style: 'glow', drag: 0.8, gravity: 5, killOnCollide: true, life: [0.3, 0.6], size: [0.11, 0.07], stretch: 0.035, emissive: 2.2, temp: [1550, 1300], fadeIn: 0.02, fadeOut: 0.35 }, |
| +добавлено | // the molten mass of the column's lower half: semi-opaque, self-lit, torn by noise |
| +добавлено | mass: { style: 'media', drag: 0.25, gravity: 9.8, curl: 0.6, curlScale: 0.4, life: [0.55, 0.95], size: [0.2, 0.42], sizePow: 0.7, emissive: 4, temp: [1500, 1150], alpha: [0.85, 0], density: 7, erosion: 1.3, fadeIn: 0.05, fadeOut: 0.35, forceResponse: 0.2 }, |
| +добавлено | // fine spatter thrown sideways at the mouth |
| +добавлено | spray: { style: 'glow', drag: 0.3, gravity: 9.8, killOnCollide: true, life: [0.6, 1.3], size: [0.022, 0.01], stretch: 0.04, emissive: 2.6, temp: [1450, 850], paints: 'heat', paintAmount: 0.02 }, |
| +добавлено | // sparks of glowing tephra the plume carries up |
| +добавлено | ember: { style: 'glow', drag: 0.7, buoyancy: 1.4, gravity: 1.5, curl: 2.2, curlScale: 0.45, life: [1.0, 2.4], size: [0.022, 0.008], stretch: 0.025, emissive: 3, temp: [1700, 850], forceResponse: 1.5 }, |
| +добавлено | // lava bombs: rock lumps glowing as they fly, crusting black where they land |
| +добавлено | clot: { style: 'debris', mesh: 'rock', gravity: 9.8, drag: 0.05, restitution: 0.1, friction: 0.85, life: [3.4, 4.4], size: [0.09, 0.09], spin: 4, color: ['#15100c', new THREE.Color(5, 0.55, 0.02)], alpha: [1, 1], fadeOut: 0.05, forceResponse: 0, paints: 'heat', paintAmount: 0.08, morph: 'lava-geyser.cinder' }, |
| +добавлено | // ...and what it becomes on landing: a crusted lump, dull red in its cracks, cooling to black |
| +добавлено | cinder: { style: 'debris', mesh: 'rock', gravity: 9.8, drag: 0.05, restitution: 0.06, friction: 0.95, life: [5.5, 7.5], size: [0.09, 0.09], spin: 2, color: ['#120d0a', new THREE.Color(1.5, 0.14, 0)], alpha: [1, 0.9], fadeOut: 0.12, forceResponse: 0, paints: 'heat', paintAmount: 0.03 }, |
| +добавлено | // volcanic gas: blue-grey, lit orange from below by the fountain |
| +добавлено | gas: { style: 'media', emissive: 0, drag: 0.9, buoyancy: 3.4, curl: 1.0, curlScale: 0.22, life: [3.0, 4.6], size: [0.8, 3.6], sizePow: 0.5, color: ['#959aa0', '#71757b'], alpha: [0.16, 0], density: 1.4, erosion: 0.6, fadeIn: 0.12, fadeOut: 0.5, forceResponse: 0.9, lightResponse: 1.7 }, |
| +добавлено | // ash: the dark pulse of each explosive burst |
| +добавлено | ash: { style: 'media', emissive: 0, drag: 1.2, buoyancy: 2.4, curl: 1.3, curlScale: 0.3, life: [2.4, 4], size: [0.4, 2.2], sizePow: 0.55, color: ['#3a3532', '#55504b'], alpha: [0.18, 0], density: 2.2, erosion: 0.7, fadeIn: 0.06, fadeOut: 0.5, forceResponse: 0.8, lightResponse: 1.3 }, |
| +добавлено | steam: { style: 'media', emissive: 0, drag: 1.5, buoyancy: 2.4, curl: 1.3, curlScale: 0.35, life: [1.3, 2.4], size: [0.2, 1.2], sizePow: 0.5, color: ['#cfc9c4', '#8f8984'], alpha: [0.12, 0], density: 1.4, erosion: 0.7, fadeIn: 0.08, fadeOut: 0.5, forceResponse: 0.8, lightResponse: 0.8 }, |
| +добавлено | dust: { style: 'media', emissive: 0, drag: 2.2, buoyancy: -0.2, curl: 0.6, curlScale: 0.2, life: [2.2, 3.8], size: [0.4, 1.8], sizePow: 0.55, color: ['#4a3f36', '#3a322c'], alpha: [0.2, 0.08], density: 2.2, erosion: 0.55, fadeIn: 0.05, fadeOut: 0.6, forceResponse: 0.5, lightResponse: 0.35 }, |
| +добавлено | haze: { style: 'media', emissive: 0, drag: 2.5, buoyancy: 2.2, curl: 0.8, curlScale: 0.5, life: [0.8, 1.5], size: [0.5, 1.2], color: ['#ffffff', '#ffffff'], alpha: [0.018, 0], density: 0.6, erosion: 0.3, distortion: 40, fadeIn: 0.25, fadeOut: 0.4, forceResponse: 0.6 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const cone = (kind, radius, spread, radial, c) => P.emitter(kind, { shape: 'cone', radius, spread, radial, cone: c }); |
| +добавлено | const jet = cone('lava-geyser.jet', 0.2, 0.5, 10, 0.13), fan = cone('lava-geyser.jet', 0.2, 0.7, 9, 0.24), core = cone('lava-geyser.core', 0.16, 0.4, 3, 0.15); |
| +добавлено | const mass = cone('lava-geyser.mass', 0.14, 0.4, 6, 0.08); |
| +добавлено | const spray = cone('lava-geyser.spray', 0.15, 1.2, 4, 0.75), clot = cone('lava-geyser.clot', 0.15, 0.8, 8, 0.38); |
| +добавлено | const rockC = cone('earth.rock', 0.3, 1.4, 6, 0.6), chunkC = cone('earth.chunk', 0.3, 1, 5, 0.5); |
| +добавлено | const ember = P.emitter('lava-geyser.ember', { shape: 'disk', radius: 0.5, spread: 1.2 }); |
| +добавлено | const pebble = P.emitter('earth.pebble', { shape: 'disk', radius: 1.4, spread: 0.8 }); |
| +добавлено | const seep = P.emitter('lava-geyser.steam', { shape: 'ring', radius: 1, spread: 0.3 }); |
| +добавлено | const dust = P.emitter('lava-geyser.dust', { shape: 'ring', radius: 0.7, spread: 0.6, radial: 4.5 }); |
| +добавлено | const bombE = P.emitter('lava-geyser.clot', { shape: 'sphere', radius: 0, spread: 0 }); |
| +добавлено | const smokeE = P.emitter('lava-geyser.ash', { shape: 'sphere', radius: 0.05, spread: 0.25 }); |
| +добавлено | const puff = P.emitter('lava-geyser.dust', { shape: 'sphere', radius: 0.1, spread: 0.8, radial: 1 }); |
| +добавлено | const plume = makePlume(P); |
| +добавлено | |
| +добавлено | // --- the vents: one program, per-mesh uniforms --- |
| +добавлено | const tint = new THREE.Vector3(1, 1, 1), tintCol = new THREE.Color(1, 1, 1); |
| +добавлено | const quad = new THREE.PlaneGeometry(2, 2, 1, 1); |
| +добавлено | const vents = []; |
| +добавлено | for (let i = 0; i < MAX_VENTS; i++) { |
| +добавлено | const m = new THREE.Mesh(quad, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: VENT_VERT, fragmentShader: VENT_FRAG, alphaToCoverage: true, side: THREE.DoubleSide, |
| +добавлено | uniforms: { |
| +добавлено | ...U, uVent: { value: new THREE.Vector4() }, uVentA: { value: new THREE.Vector4() }, uVentB: { value: new THREE.Vector4() }, |
| +добавлено | uVentC: { value: new THREE.Vector4() }, uTint: { value: tint }, |
| +добавлено | }, |
| +добавлено | })); |
| +добавлено | m.frustumCulled = false; |
| +добавлено | m.visible = false; |
| +добавлено | m.renderOrder = -0.5; |
| +добавлено | fx.scenes.opaque.add(m); |
| +добавлено | vents.push(m); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- per-cast state --- |
| +добавлено | const rimState = () => ({ h: null, seed: 0, pos: new THREE.Vector3(), dir: new THREE.Vector3(), height: 1, radius: 0.3, delay: 0 }); |
| +добавлено | const bombState = () => ({ trail: null, alive: false, pos: new THREE.Vector3(), vel: new THREE.Vector3(), t: 0 }); |
| +добавлено | const states = Array.from({ length: STATES }, () => ({ |
| +добавлено | vent: null, pos: new THREE.Vector3(), R: 0.4, seedF: 0, erupted: false, te: 0, spurt: false, |
| +добавлено | heat: 0, level: 1, up: 0, crust: 0, h: 0, env: 0, surgeV: 0, surgeT: 0, bombT: 0, splatT: 0, |
| +добавлено | rim: Array.from({ length: MAX_RIM }, rimState), nRim: 0, bombs: Array.from({ length: BOMBS }, bombState), |
| +добавлено | light: null, shock: null, draft: null, shockT: 0, |
| +добавлено | })); |
| +добавлено | |
| +добавлено | function splat(x, z, radius, crack, heat, char, pattern, seed) { |
| +добавлено | SPL.x = SPL.x2 = x; SPL.z = SPL.z2 = z; SPL.radius = radius; SPL.crack = crack; SPL.heat = heat; SPL.char = char; |
| +добавлено | SPL.pattern = pattern; SPL.seed = seed; SPL.width = 0; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | function shock(pos, r1, duration, distortion) { |
| +добавлено | SHK.pos.copy(pos).setY(0.04); SHK.radius1 = r1; SHK.duration = duration; SHK.distortion = distortion; SHK.thickness = 0.35 + r1 * 0.08; |
| +добавлено | fx.shocks?.spawn(SHK); |
| +добавлено | } |
| +добавлено | // jet speed for a fountain h m tall (with a little extra for drag) |
| +добавлено | const v0For = (h) => Math.sqrt(2 * 9.8 * Math.max(h, 0.1)) * (1 + 0.02 * h); |
| +добавлено | function fire(e, pos, y, speed, n) { e.pos.copy(pos).setY(y); e.p.copy(UP); e.radial = speed; e.burst(n); } |
| +добавлено | function stream(e, pos, y, speed, n, dt) { e.pos.copy(pos).setY(y); e.p.copy(UP); e.radial = speed; e.rate(n, dt); } |
| +добавлено | |
| +добавлено | // tinted blackbody for the glow kinds (the kind table has no tint), written as texels |
| +добавлено | let lastT = -1, lastTint = ''; |
| +добавлено | const HOT = { |
| +добавлено | jet: { w: { temp: [0, 0] }, t: { temp: null, color: [new THREE.Color(), new THREE.Color()] }, a: 1.06, b: 0.76 }, |
| +добавлено | core: { w: { temp: [0, 0] }, t: { temp: null, color: [new THREE.Color(), new THREE.Color()] }, a: 1.1, b: 0.93 }, |
| +добавлено | spray: { w: { temp: [0, 0] }, t: { temp: null, color: [new THREE.Color(), new THREE.Color()] }, a: 1.04, b: 0.6 }, |
| +добавлено | ember: { w: { temp: [0, 0] }, t: { temp: null, color: [new THREE.Color(), new THREE.Color()] }, a: 1.2, b: 0.6 }, |
| +добавлено | mass: { w: { temp: [0, 0] }, t: { temp: null, color: [new THREE.Color(), new THREE.Color()] }, a: 1.07, b: 0.82 }, |
| +добавлено | }; |
| +добавлено | const CLOT = { color: [new THREE.Color('#15100c'), new THREE.Color()] }; |
| +добавлено | const white = () => tintCol.r > 0.97 && tintCol.g > 0.97 && tintCol.b > 0.97; |
| +добавлено | function hot(name, h, T) { |
| +добавлено | if (white()) { h.w.temp[0] = T * h.a; h.w.temp[1] = T * h.b; P.setKind(name, h.w); return; } |
| +добавлено | blackbodyRGB(T * h.a, h.t.color[0]).multiply(tintCol); |
| +добавлено | blackbodyRGB(T * h.b, h.t.color[1]).multiply(tintCol).multiplyScalar((h.b / h.a) ** 3); |
| +добавлено | P.setKind(name, h.t); |
| +добавлено | } |
| +добавлено | function retint() { |
| +добавлено | const p = S.params; |
| +добавлено | if (p.temperature === lastT && p.tint === lastTint) return; |
| +добавлено | lastT = p.temperature; lastTint = p.tint; |
| +добавлено | tintCol.set(p.tint); tint.set(tintCol.r, tintCol.g, tintCol.b); |
| +добавлено | hot('lava-geyser.jet', HOT.jet, p.temperature); hot('lava-geyser.core', HOT.core, p.temperature); |
| +добавлено | hot('lava-geyser.spray', HOT.spray, p.temperature); hot('lava-geyser.ember', HOT.ember, p.temperature); |
| +добавлено | hot('lava-geyser.mass', HOT.mass, p.temperature); |
| +добавлено | // blackbodyRGB is ~3.5 bright at 1400 K: the clots glow like earth.magma, ~ (5, 0.55, 0) |
| +добавлено | blackbodyRGB(p.temperature, CLOT.color[1]).multiply(tintCol).multiplyScalar(1.4 * Math.pow(p.temperature / 1400, 4)); |
| +добавлено | P.setKind('lava-geyser.clot', CLOT); |
| +добавлено | } |
| +добавлено | retint(); |
| +добавлено | // the light's colour: blackbody chromaticity at unit brightness |
| +добавлено | function lightColor(T, out) { blackbodyRGB(T, out).multiply(tintCol); const m = Math.max(out.r, out.g, out.b, 1e-4); return out.multiplyScalar(1 / m); } |
| +добавлено | |
| +добавлено | function setVent(st, p) { |
| +добавлено | const m = st.vent; |
| +добавлено | if (!m) return; |
| +добавлено | const u = m.material.uniforms, D = 0.9 + 1.6 * p.width; |
| +добавлено | u.uVent.value.set(st.pos.x, st.pos.z, st.R * 1.35 + 0.05, 0.004); |
| +добавлено | u.uVentA.value.set(st.R, D, D * st.level, 0.16); |
| +добавлено | u.uVentB.value.set(st.heat, p.temperature, p.intensity, U.uTime.value); |
| +добавлено | u.uVentC.value.set(st.up, st.seedF, st.crust, p.haze); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function layout(c, p, st) { |
| +добавлено | // the vent opens just short of the target, so the column stands beside it and the fan rains on it |
| +добавлено | _a.set(c.to.x - c.from.x, 0, c.to.z - c.from.z); |
| +добавлено | const dist = _a.length(); |
| +добавлено | st.pos.set(c.to.x, 0, c.to.z); |
| +добавлено | if (dist > 2.5) st.pos.addScaledVector(_a, -Math.min(1.1, dist * 0.2) / dist); |
| +добавлено | const rim = fx.ARENA.radius - 1.5; |
| +добавлено | if (st.pos.length() > rim) st.pos.setLength(rim); |
| +добавлено | st.seedF = (c.seed % 997) / 997; |
| +добавлено | // the spatter rampart: broken slabs heaved up around the mouth, leaning out |
| +добавлено | const n = Math.min(MAX_RIM, Math.round(p.rim)), R = p.width * 1.6 + 0.12; |
| +добавлено | st.nRim = n; |
| +добавлено | for (let k = 0; k < n; k++) { |
| +добавлено | const s = st.rim[k], a = (k + 0.3 * fx.rand()) / Math.max(n, 1) * Math.PI * 2; |
| +добавлено | s.radius = R * (0.55 + 0.35 * fx.rand()) + 0.1; |
| +добавлено | s.height = p.rimHeight * (0.6 + 0.6 * fx.rand()); |
| +добавлено | s.pos.set(st.pos.x + Math.cos(a) * (R + s.radius * 0.5), 0, st.pos.z + Math.sin(a) * (R + s.radius * 0.5)); |
| +добавлено | s.dir.set(Math.cos(a), 0, Math.sin(a)).multiplyScalar(0.9 + 0.6 * fx.rand()).add(UP).normalize(); |
| +добавлено | s.delay = 0.08 + 0.25 * fx.rand(); |
| +добавлено | s.seed = ((c.seed * 37 + k * 7919) >>> 0) & 0xffffff || 1; |
| +добавлено | s.h = null; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function spawnRim(p, st) { |
| +добавлено | for (let k = 0; k < st.nRim; k++) { |
| +добавлено | const s = st.rim[k]; |
| +добавлено | SPK.pos.copy(s.pos); SPK.dir.copy(s.dir); SPK.height = s.height; SPK.radius = s.radius; |
| +добавлено | SPK.delay = s.delay; SPK.rise = 0.7; SPK.life = S.timing.charge + 0.3 + p.duration + SUBSIDE * 0.5 + COOL * 0.6 - s.delay; |
| +добавлено | SPK.heat = 0.12; SPK.seed = s.seed; SPK.crumble = 1.2; |
| +добавлено | s.h = fx.spikes.spawn(SPK); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | const owns = (s) => s.h && s.h.active && s.h.seed === s.seed; |
| +добавлено | |
| +добавлено | function launchBomb(p, st, speed) { |
| +добавлено | for (let i = 0; i < BOMBS; i++) { |
| +добавлено | const b = st.bombs[i]; |
| +добавлено | if (b.alive) continue; |
| +добавлено | b.trail = fx.trails.acquire(); |
| +добавлено | if (!b.trail) return; |
| +добавлено | const a = fx.rand() * Math.PI * 2, tilt = 0.25 + 0.4 * fx.rand(); |
| +добавлено | b.pos.copy(st.pos).setY(0.4); |
| +добавлено | b.vel.set(Math.cos(a) * Math.sin(tilt), Math.cos(tilt), Math.sin(a) * Math.sin(tilt)).multiplyScalar(speed * (0.75 + 0.3 * fx.rand())); |
| +добавлено | b.alive = true; b.t = 0; |
| +добавлено | const t = b.trail; |
| +добавлено | t.width = 0.09; t.intensity = 1.4 * p.intensity; t.glow = 2; t.glowIntensity = 0.15; t.fadeTime = 0.09; t.spacing = 0.08; t.distortion = 0; |
| +добавлено | lightColor(p.temperature * 0.95, t.color); |
| +добавлено | t.push(b.pos); |
| +добавлено | // the bomb itself flies on the GPU along the same arc (a clot that crusts where it lands) |
| +добавлено | bombE.pos.copy(b.pos); bombE.vel.copy(b.vel); bombE.size = 2.2; bombE.burst(1); |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | function updateBombs(p, st, dt) { |
| +добавлено | for (let i = 0; i < BOMBS; i++) { |
| +добавлено | const b = st.bombs[i]; |
| +добавлено | if (!b.alive) continue; |
| +добавлено | b.t += dt; |
| +добавлено | b.vel.y -= 9.8 * dt; |
| +добавлено | b.vel.multiplyScalar(1 - 0.04 * dt); |
| +добавлено | b.pos.addScaledVector(b.vel, dt); |
| +добавлено | // it darkens as it flies |
| +добавлено | if (b.trail) { b.trail.push(b.pos); b.trail.intensity = 1.4 * p.intensity * Math.max(0.25, 1 - b.t * 0.3); } |
| +добавлено | if (b.t < 1.6) { smokeE.pos.copy(b.pos); smokeE.vel.set(0, 0.4, 0); smokeE.size = 0.22; smokeE.rate(26 * p.plume, dt); } |
| +добавлено | const r = Math.hypot(b.pos.x, b.pos.z); |
| +добавлено | if (b.pos.y <= 0.08 || r > fx.ARENA.radius - 0.4 || b.t > 5) { |
| +добавлено | if (b.pos.y <= 0.08 && r < fx.ARENA.radius - 0.4) { |
| +добавлено | // landing: a scorch and a puff of dust (the GPU clot crusts into a cinder there) |
| +добавлено | puff.pos.copy(b.pos).setY(0.1); puff.vel.set(0, 0.6, 0); puff.burst(5 * p.bombs); |
| +добавлено | splat(b.pos.x, b.pos.z, 0.45, 0.9, 0.35, 0.35, 'radial', (i * 13 + 7) % 97); |
| +добавлено | } |
| +добавлено | b.trail?.release(); b.trail = null; b.alive = false; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function erupt(c, p, st) { |
| +добавлено | st.erupted = true; st.te = 0; st.surgeV = 0.35 * p.surge; st.surgeT = 0.8; |
| +добавлено | const H = p.height, v0 = v0For(H * 1.15); |
| +добавлено | // the burst: the first column overshoots, rock and bombs fly, a pulse of ash |
| +добавлено | fire(jet, st.pos, 0.1, v0, 520 * p.spatter); |
| +добавлено | fire(core, st.pos, 0.1, v0 * 0.4, 140 * p.spatter); |
| +добавлено | fire(mass, st.pos, 0.1, v0 * 0.6, 40 * p.spatter); |
| +добавлено | fire(spray, st.pos, 0.15, 5, 220 * p.spatter); |
| +добавлено | fire(clot, st.pos, 0.3, v0 * 0.7, 30 * p.bombs); |
| +добавлено | fire(rockC, st.pos, 0.3, 6.5, 40 * p.bombs); |
| +добавлено | fire(chunkC, st.pos, 0.3, 5.5, 8 * p.bombs); |
| +добавлено | dust.pos.copy(st.pos).setY(0.15); dust.vel.set(0, 0.3, 0); dust.burst(60); |
| +добавлено | plume.burst(st.pos, H, 40 * p.plume); |
| +добавлено | for (let i = 0; i < 3; i++) launchBomb(p, st, v0 * 0.8); |
| +добавлено | // the floor: a radial crack network, heat and char, a magma pool glow at the vent |
| +добавлено | splat(st.pos.x, st.pos.z, 2.6, 2.0, 0.5, 0.45, 'radial', (c.seed + 3) % 97); |
| +добавлено | splat(st.pos.x, st.pos.z, 1.2, 0.6, 0.6, 0.6, 'noise', (c.seed + 11) % 97); |
| +добавлено | fx.impacts.add('magma', _a.copy(st.pos).setY(0.15), 1.5, 0.6, p.duration + SUBSIDE + COOL, lightColor(p.temperature, _col)); |
| +добавлено | shock(st.pos, 5.5, 0.55, 0.06); |
| +добавлено | // a pressure shell on everything loose, then the draft the column pulls |
| +добавлено | st.shock = fx.forces.acquire(); |
| +добавлено | if (st.shock) { st.shock.type = 4; st.shock.p[0] = 0.4; st.shock.p[1] = 1.4; st.shock.p[2] = 30; st.shock.p[3] = 0; st.shock.pos.copy(st.pos).setY(0.5); } |
| +добавлено | st.shockT = 0; |
| +добавлено | st.draft = fx.forces.acquire(); |
| +добавлено | if (st.draft) { const f = st.draft; f.type = 5; f.dir.set(0, 1, 0); f.p[0] = 3.5; f.p[1] = 1.3; f.p[2] = H + 3; f.p[3] = 0.9; f.pos.copy(st.pos).setY(-0.2); } |
| +добавлено | // dummies: thrown off the vent, the close ones catch fire |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const d = Math.hypot(dm.pos.x - st.pos.x, dm.pos.z - st.pos.z), reach = 2.4 + p.width * 2; |
| +добавлено | if (d < reach) { |
| +добавлено | _b.set(dm.pos.x - st.pos.x, 0, dm.pos.z - st.pos.z); |
| +добавлено | if (_b.lengthSq() < 1e-4) _b.set(1, 0, 0); |
| +добавлено | _b.normalize().addScaledVector(UP, 0.8); |
| +добавлено | dm.hit(_b, 2.2 * (1 - d / reach) + 0.4); |
| +добавлено | if (d < reach * 0.7) dm.status.burn = Math.max(dm.status.burn, 0.9); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // the pool hisses if the vent is near it |
| +добавлено | const pool = fx.ARENA.pool; |
| +добавлено | const px = Math.min(Math.max(st.pos.x, pool.center.x - pool.half.x), pool.center.x + pool.half.x), pz = Math.min(Math.max(st.pos.z, pool.center.y - pool.half.y), pool.center.y + pool.half.y); |
| +добавлено | if (Math.hypot(px - st.pos.x, pz - st.pos.z) < 3) { |
| +добавлено | fx.pool?.ripple?.(px, pz, 0.1, 1.6); |
| +добавлено | seep.pos.set(px, 0.1, pz); seep.radius = 0.4; seep.vel.set(0, 1.5, 0); seep.size = 1.5; seep.burst(120 * p.plume); |
| +добавлено | } |
| +добавлено | fx.rig.shake(0.6 * p.shake, 7); |
| +добавлено | fx.rig.shake(0.25 * p.shake, 16); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(2.6, 0.12); fx.rig.hitstop(0.05); } |
| +добавлено | fx.flash(0.14 * p.intensity); |
| +добавлено | const L = st.light; |
| +добавлено | if (L) { L.type = 1; L.range = 9 + H * 0.6; L.shadow = true; L.intensity = 20 * p.intensity; L.pos.copy(st.pos).setY(0.3); L.end.copy(st.pos).setY(0.3 + H * 0.5); } |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'tint' || k === 'temperature') retint(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params; |
| +добавлено | if (name === 'charge') { |
| +добавлено | retint(); |
| +добавлено | const st = c.data.g = states[c.index]; |
| +добавлено | st.erupted = false; st.spurt = false; st.te = 0; st.heat = 0.2; st.level = 1; st.up = 0; st.crust = 0.6; st.h = 0; st.env = 0; |
| +добавлено | st.surgeV = 0; st.surgeT = 0; st.bombT = 0.4; st.splatT = 0; st.shockT = 0; st.R = 0.02; |
| +добавлено | for (const b of st.bombs) { b.alive = false; b.trail = null; } |
| +добавлено | layout(c, p, st); |
| +добавлено | c.durations.linger = p.duration + SUBSIDE + COOL; |
| +добавлено | st.vent = null; |
| +добавлено | for (const m of vents) if (!m.visible) { st.vent = m; m.visible = true; break; } |
| +добавлено | setVent(st, p); |
| +добавлено | spawnRim(p, st); |
| +добавлено | st.light = fx.lights.acquire(); |
| +добавлено | if (st.light) { st.light.type = 0; st.light.range = 5; st.light.intensity = 0; st.light.shadow = false; lightColor(p.temperature, st.light.color); st.light.pos.copy(st.pos).setY(0.35); } |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | erupt(c, p, c.data.g); |
| +добавлено | } else if (name === 'done') { |
| +добавлено | const st = c.data.g; |
| +добавлено | if (!st) return; |
| +добавлено | for (let k = 0; k < st.nRim; k++) { const s = st.rim[k]; if (owns(s)) s.h.release(); s.h = null; } |
| +добавлено | for (const b of st.bombs) { b.trail?.release(); b.trail = null; b.alive = false; } |
| +добавлено | if (st.vent) st.vent.visible = false; |
| +добавлено | st.vent = null; |
| +добавлено | st.light?.release(); st.shock?.release(); st.draft?.release(); |
| +добавлено | st.light = st.shock = st.draft = null; |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, st = c.data.g, L = st.light; |
| +добавлено | const flick = 0.84 + 0.1 * Math.sin(c.age * 23 + st.seedF * 9) * Math.sin(c.age * 9.7) + 0.06 * Math.sin(c.age * 61); |
| +добавлено | if (!st.erupted) { |
| +добавлено | // the ground bulges and cracks; the mouth opens over rising magma |
| +добавлено | const k = Math.min(c.t / c.durations.charge, 1); |
| +добавлено | st.R = (p.width * 1.25) * (0.15 + 0.85 * smooth(k / 0.7)); |
| +добавлено | st.level = 1 - 0.6 * smooth(k); |
| +добавлено | st.heat = 0.35 + 0.6 * k; st.up = 0.5 * k; st.crust = 0.6 * (1 - k); |
| +добавлено | setVent(st, p); |
| +добавлено | for (let i = 0; i < st.nRim; i++) { const s = st.rim[i]; if (owns(s)) s.h.heat = 0.12 + 0.2 * k; } |
| +добавлено | st.splatT -= dt; |
| +добавлено | if (st.splatT <= 0) { st.splatT = 0.12; splat(st.pos.x, st.pos.z, 0.6 + 1.8 * k, 1.1 + 0.6 * k, 0.12 + 0.25 * k, 0.1, 'radial', c.seed % 97); } |
| +добавлено | pebble.pos.copy(st.pos).setY(0.05); pebble.vel.set(0, 1.6 + 1.6 * k, 0); pebble.rate(160 * k * p.bombs, dt); |
| +добавлено | seep.pos.copy(st.pos).setY(0.05); seep.radius = 0.6 + 1.2 * k; seep.vel.set(0, 1.2, 0); seep.size = 1; seep.rate(30 * p.plume, dt); |
| +добавлено | ember.pos.copy(st.pos).setY(0.1); ember.vel.set(0, 2, 0); ember.rate(70 * k, dt); |
| +добавлено | fx.rig.shake(dt * 2.4 * p.shake * (0.3 + k), 8); |
| +добавлено | // a throat-clearing spurt just before the eruption |
| +добавлено | if (!st.spurt && k > 0.68) { |
| +добавлено | st.spurt = true; |
| +добавлено | fire(core, st.pos, 0.1, 4.5, 50 * p.spatter); fire(spray, st.pos, 0.1, 3.5, 70 * p.spatter); fire(jet, st.pos, 0.1, v0For(1.2), 60 * p.spatter); |
| +добавлено | fx.rig.shake(0.12 * p.shake, 10); |
| +добавлено | } |
| +добавлено | if (L) L.intensity = 6 * p.intensity * k * k * flick; |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | |
| +добавлено | st.te += dt; |
| +добавлено | const te = st.te, dur = p.duration; |
| +добавлено | // the fountain: full, then dies back over SUBSIDE; surges ride on top |
| +добавлено | st.env = 1 - smooth((te - dur) / SUBSIDE); |
| +добавлено | const cool = smooth((te - dur - SUBSIDE * 0.5) / COOL); |
| +добавлено | st.surgeT -= dt; |
| +добавлено | if (st.surgeT <= 0 && st.env > 0.3) { st.surgeT = (0.9 + 0.9 * fx.rand()) / Math.max(0.35, Math.sqrt(p.surge)); st.surgeV = Math.max(st.surgeV, (0.15 + 0.3 * fx.rand()) * p.surge); if (p.surge > 1.4) plume.burst(st.pos, st.h, 12 * p.plume * p.surge); } |
| +добавлено | st.surgeV *= Math.exp(-dt / 0.45); |
| +добавлено | const wob = 0.86 + 0.08 * Math.sin(c.age * 3.1 + st.seedF * 7) + 0.06 * Math.sin(c.age * 7.3); |
| +добавлено | st.h = p.height * st.env * wob * (1 + st.surgeV); |
| +добавлено | const env = st.env, v0 = v0For(st.h); |
| +добавлено | if (env > 0.01) { |
| +добавлено | jet.radius = fan.radius = p.width * 0.75; mass.radius = p.width * 0.5; core.radius = p.width * 0.55; |
| +добавлено | stream(jet, st.pos, 0.1, v0, 1300 * p.spatter * env * (0.7 + 0.6 * st.surgeV), dt); |
| +добавлено | stream(jet, st.pos, 0.1, v0 * 0.72, 300 * p.spatter * env, dt); |
| +добавлено | stream(fan, st.pos, 0.1, v0 * 0.88, 420 * p.spatter * env, dt); // the falling curtain spreads wide |
| +добавлено | stream(mass, st.pos, 0.1, v0 * (0.55 + 0.15 * fx.rand()), 70 * p.spatter * env, dt); |
| +добавлено | stream(core, st.pos, 0.1, v0 * 0.3 + 1, 500 * p.spatter * env, dt); |
| +добавлено | stream(spray, st.pos, 0.15, 3 + 2 * env, 120 * p.spatter * env, dt); |
| +добавлено | stream(clot, st.pos, 0.3, v0 * 0.7, 16 * p.bombs * env * (1 + 2 * st.surgeV), dt); |
| +добавлено | ember.pos.copy(st.pos).setY(0.4 + st.h * 0.3); ember.vel.set(0, 3, 0); ember.rate(90 * env, dt); |
| +добавлено | st.bombT -= dt; |
| +добавлено | if (st.bombT <= 0 && env > 0.4 && p.bombs > 0) { st.bombT = (0.35 + 0.6 * fx.rand()) / p.bombs; launchBomb(p, st, v0 * 0.8); } |
| +добавлено | } else if (cool < 0.8) { |
| +добавлено | // last gasps: the vent spits now and then |
| +добавлено | st.bombT -= dt; |
| +добавлено | if (st.bombT <= 0) { st.bombT = 0.3 + 0.5 * fx.rand(); fire(spray, st.pos, 0.1, 2.5, 18 * p.spatter * (1 - cool)); } |
| +добавлено | } |
| +добавлено | plume.update(st.pos, Math.max(st.h, 1.2), dt, Math.max(env, 0.25 * (1 - cool)), p); |
| +добавлено | updateBombs(p, st, dt); |
| +добавлено | |
| +добавлено | // the vent drains as the fountain dies, then crusts and cools |
| +добавлено | st.level = 0.4 + 0.45 * (1 - env); |
| +добавлено | st.up = env * (0.6 + 0.4 * st.surgeV); |
| +добавлено | st.heat = (0.85 + 0.15 * env) * (1 - 0.75 * cool) * (0.95 + 0.05 * flick); |
| +добавлено | st.crust = 0.35 * (1 - env) + 0.65 * cool; |
| +добавлено | st.R = p.width * 1.25 * (1 - 0.25 * cool); |
| +добавлено | setVent(st, p); |
| +добавлено | for (let i = 0; i < st.nRim; i++) { const s = st.rim[i]; if (owns(s)) s.h.heat = 0.38 * st.heat * (1 - cool); } |
| +добавлено | st.splatT -= dt; |
| +добавлено | if (st.splatT <= 0 && st.heat > 0.1) { st.splatT = 0.3; splat(st.pos.x, st.pos.z, 1.3, 0, 0.35 * st.heat * (1 - cool), 0.04, 'radial', c.seed % 97); } |
| +добавлено | |
| +добавлено | // forces: the pressure shell expands and goes; the draft follows the column |
| +добавлено | if (st.shock) { |
| +добавлено | st.shockT += dt; |
| +добавлено | st.shock.p[0] = 0.4 + 8 * Math.pow(Math.min(st.shockT / 0.6, 1), 0.6); |
| +добавлено | st.shock.p[2] = 30 * (1 - st.shockT / 0.6); |
| +добавлено | if (st.shockT > 0.6) { st.shock.release(); st.shock = null; } |
| +добавлено | } |
| +добавлено | if (st.draft) { st.draft.p[2] = st.h + 3; st.draft.p[3] = 0.9 * env; } |
| +добавлено | // fallout: whatever stands under the fan burns |
| +добавлено | if (env > 0.3) { |
| +добавлено | const fan = 0.8 + st.h * 0.18; |
| +добавлено | for (const dm of fx.dummies) if (Math.hypot(dm.pos.x - st.pos.x, dm.pos.z - st.pos.z) < fan) dm.status.burn = Math.max(dm.status.burn, 0.6 * env); |
| +добавлено | } |
| +добавлено | fx.rig.shake(dt * 1.2 * p.shake * env * (0.5 + st.surgeV), 9); |
| +добавлено | // the column is a vertical lamp: brighter and taller with each surge |
| +добавлено | if (L) { |
| +добавлено | L.end.copy(st.pos).setY(0.3 + Math.max(st.h, 0.3) * 0.5); |
| +добавлено | L.intensity = Math.max(L.intensity * Math.exp(-dt / 0.25), (16 * env * (1 + 0.6 * st.surgeV) + 2.5 * st.heat * (1 - cool)) * p.intensity * flick); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { blackbodyRGB } from '../vfx/noise.js'; |
| +добавлено | import { FluidVolume } from '../vfx/FluidVolume.js'; |
| +добавлено | import { arenaSDF, COMMON_GLSL, NOISE_GLSL, FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL, FX_BLENDING } from '../vfx/common.js'; |
| +добавлено | |
| +добавлено | // Fire (PLAN 3). Fireball: a raymarched teardrop whose noise streams backwards past the ball and |
| +добавлено | // is stretched into licking tongues down the tail; fire is emission = sigma * L(T) (hot soot glows, |
| +добавлено | // cold soot is smoke), so a white-yellow core shows through a thinner orange body, with a dark |
| +добавлено | // sooty fringe heaviest on the tail. Small fast-cooling flame licks, curling dark smoke, embers |
| +добавлено | // and a short hot core ribbon trail it. Impact: a pyroclastic volume (billows, vortex-ring roll, |
| +добавлено | // cools outside-in from white to orange to rolling dark smoke), sparks, glowing debris, a dust |
| +добавлено | // ring, a shockwave, heat into the ground field (which then grows ground flames by itself). |
| +добавлено | // Inferno: a roaring column volume swirled by a vortex force, a line light, heat-haze billboard, |
| +добавлено | // embers and a scorched ring. |
| +добавлено | |
| +добавлено | // fire radiance at T: blackbody chromaticity, brightness ~ (T / Tref)^4 (Stefan-Boltzmann), and no |
| +добавлено | // visible glow below ~700 K (Draper point ~800 K): T^4 alone leaves cooled soot glowing dull red, which |
| +добавлено | // the night exposure (10x dusk) turns into a red veil over the smoke |
| +добавлено | const FIRE_L = /* glsl */` |
| +добавлено | vec3 fireL(float T, float Tref) { float k = T / Tref; k *= k; return blackbody(T) * (k * k * smoothstep(700.0, 1000.0, T)); } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | const FIREBALL_GLSL = FIRE_L + /* glsl */` |
| +добавлено | uniform vec4 uBall; // radius m, core temperature K, intensity, time s |
| +добавлено | uniform vec4 uBall2; // tail length m, flow m/s, soot, seed |
| +добавлено | uniform vec3 uBallPos; // ball centre (the proxy capsule also covers the tail behind it) |
| +добавлено | uniform vec3 uTint; |
| +добавлено | void volumeSample(vec3 pw, vec3 pl, out float sigma, out vec3 albedo, out vec3 emission) { |
| +добавлено | sigma = 0.0; albedo = vec3(0.0); emission = vec3(0.0); |
| +добавлено | float R = uBall.x, time = uBall.w, L = uBall2.x; |
| +добавлено | vec3 q = uVolRot * (pw - uBallPos); // y = flight direction, metres |
| +добавлено | float u = -q.y / L, uc = clamp(u, 0.0, 1.0); // 0 at the centre .. 1 at the tail tip |
| +добавлено | // teardrop: a sphere ahead, a tapering tail behind |
| +добавлено | float d0 = R * sqrt(max(1.0 - 0.9 * uc, 0.0)) - length(vec3(q.x, max(q.y, 0.0), q.z)); |
| +добавлено | float amp = R * (0.32 + 0.7 * uc); |
| +добавлено | if (d0 < -amp || u > 1.3) return; |
| +добавлено | // flames stream backwards past the ball (noise scrolls along -y), stretched a little into |
| +добавлено | // tongues down the tail. Band-limited on purpose: the baked noise's finest octaves sit far below |
| +добавлено | // the march step and would only alias into grain, so the frequency stays low and the detail |
| +добавлено | // octave fades out as the step grows (gVolDt). |
| +добавлено | float s = 0.34 / R; |
| +добавлено | vec3 np = vec3(q.x, q.y * mix(1.0, 0.6, uc) + uBall2.y * time, q.z) * s; |
| +добавлено | np += curl3(np * 0.4 + vec3(0.0, 0.1 * time, uBall2.w)) * 0.3 + uBall2.w; |
| +добавлено | float n1 = noise3(np), n2 = noise3(np * 1.9 + vec3(0.37, 0.5 * s * time, 0.11)); |
| +добавлено | float detail = 1.0 - smoothstep(0.35, 0.9, gVolDt / R); |
| +добавлено | float d = d0 + (n1 - 0.5) * 2.0 * amp + (n2 - 0.5) * 0.7 * amp * detail - 0.3 * R * uc; |
| +добавлено | float tail = 1.0 - smoothstep(0.8, 1.25, u); |
| +добавлено | float body = smoothstep(0.0, 0.5 * R, d) * tail; |
| +добавлено | // boiling cells (Worley, riding the flow): the burning surface breaks into dense, hot cells with |
| +добавлено | // thin, cooler, sooty folds between them, so the white core shows through in pockets |
| +добавлено | float cell = mix(0.5, noise3v(np * 1.5 + vec3(0.0, 0.35 * s * time, 0.0)).b, detail); |
| +добавлено | float fold = 1.0 - cell; |
| +добавлено | float heat = clamp(pow(clamp(d / R, 0.0, 1.0), 1.15) * 1.9 * (1.0 - 0.55 * uc) + 0.2 * (n2 - 0.5) + 0.45 * (cell - 0.5), 0.0, 1.0); |
| +добавлено | float T = mix(760.0, uBall.y * 1.45, heat); |
| +добавлено | float sf = body * (1.0 + 4.0 * cell * cell) / R; |
| +добавлено | // soot: where the tongues cool on the trailing side and in the folds, patchy, nothing ahead |
| +добавлено | float soot = smoothstep(-0.3 * R, 0.2 * R, d) * (1.0 - smoothstep(0.0, 0.45, heat)) |
| +добавлено | * max(smoothstep(0.42, 0.75, n2), 0.7 * fold * fold) * smoothstep(-0.25, 0.7, u) * tail; |
| +добавлено | float ss = soot * uBall2.z * 8.0 / R; |
| +добавлено | sigma = sf + ss; |
| +добавлено | emission = sf * fireL(T, uBall.y) * uTint * uBall.z; |
| +добавлено | albedo = vec3(0.02, 0.018, 0.016) * (ss / max(sigma, 1e-4)); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // heat haze over the inferno: a vertical camera-facing billboard writing only distortion |
| +добавлено | const HAZE_VERT = FX_VERT_HEAD + /* glsl */` |
| +добавлено | uniform vec4 uHazeA; // base xyz, half width m |
| +добавлено | uniform vec4 uHazeB; // height m, strength m (apparent displacement), -, - |
| +добавлено | out vec2 vUv; |
| +добавлено | out vec3 vW; |
| +добавлено | out float vViewZ; |
| +добавлено | out vec2 vPerM; // screen uv per metre at this depth |
| +добавлено | void main() { |
| +добавлено | vec3 side = cross(vec3(0.0, 1.0, 0.0), cameraPosition - uHazeA.xyz); |
| +добавлено | side = dot(side, side) > 1e-8 ? normalize(side) : vec3(1.0, 0.0, 0.0); |
| +добавлено | vec3 w = uHazeA.xyz + side * position.x * uHazeA.w + vec3(0.0, (position.y * 0.5 + 0.5) * uHazeB.x, 0.0); |
| +добавлено | vec4 v = viewMatrix * vec4(w, 1.0); |
| +добавлено | vUv = position.xy; vW = w; vViewZ = -v.z; |
| +добавлено | vPerM = 0.5 * vec2(projectionMatrix[0][0], projectionMatrix[1][1]) / max(-v.z, 0.1); |
| +добавлено | gl_Position = projectionMatrix * v; |
| +добавлено | }`; |
| +добавлено | const HAZE_FRAG = FX_FRAG_HEAD + COMMON_GLSL + NOISE_GLSL + FX_OUT_GLSL + /* glsl */` |
| +добавлено | uniform vec4 uHazeB; |
| +добавлено | in vec2 vUv; |
| +добавлено | in vec3 vW; |
| +добавлено | in float vViewZ; |
| +добавлено | in vec2 vPerM; |
| +добавлено | void main() { |
| +добавлено | float h = vUv.y * 0.5 + 0.5; |
| +добавлено | float m = (1.0 - vUv.x * vUv.x) * smoothstep(0.0, 0.08, h) * (1.0 - smoothstep(0.5, 1.0, h)); |
| +добавлено | vec3 np = vec3(vW.x * 0.8, vW.y * 0.28 - uTime * 0.9, vW.z * 0.8); |
| +добавлено | vec2 n = vec2(noise3(np), noise3(np.zxy * 1.3 + 0.37)) - 0.5; |
| +добавлено | fxOut(vec3(0.0), 0.0, vec3(0.0), n * (uHazeB.y * m * m) * vPerM, vViewZ); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // flame-ish kinds: [kind, hot x T, cold x T]; recoloured when temperature or tint change |
| +добавлено | const HOT_KINDS = [['flame', 1.25, 0.38], ['ember', 1.1, 0.5], ['spark', 1.45, 0.6]]; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'fire', name: 'Fire', key: '2', hue: '#ff7a1a', |
| +добавлено | shape: 'projectile', forms: ['Fireball', 'Inferno'], radius: 0.35, |
| +добавлено | params: { size: 1, speed: 18, temperature: 1900, intensity: 1, soot: 1, embers: 1, smoke: 1, haze: 1, scorch: 1, height: 1, tint: '#ffffff' }, |
| +добавлено | ranges: { size: [0.3, 3, 0.01], speed: [4, 40, 0.1], temperature: [900, 6500, 10], intensity: [0, 3, 0.01], soot: [0, 3, 0.01], embers: [0, 3, 0.01], smoke: [0, 3, 0.01], haze: [0, 3, 0.01], scorch: [0, 2, 0.01], height: [0.4, 2, 0.01] }, |
| +добавлено | presets: { |
| +добавлено | hellfire: { temperature: 1500, tint: '#ff6a4a', smoke: 1.6, soot: 1.6, intensity: 1.2 }, |
| +добавлено | 'blue flame': { temperature: 6500, tint: '#8fb8ff', smoke: 0.3, soot: 0.15 }, |
| +добавлено | soulfire: { temperature: 2600, tint: '#3cff9a', smoke: 0.5, soot: 0.5 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 0.7, impact: 0.25, linger: 5, sustain: 2.6 }, |
| +добавлено | kinds: { |
| +добавлено | // small fast-cooling licks: they ride off the ball, curl, erode away and turn to soot |
| +добавлено | flame: { style: 'media', drag: 3.6, buoyancy: 3.8, curl: 3.2, curlScale: 0.6, life: [0.16, 0.36], size: [0.09, 0.32], sizePow: 0.6, emissive: 1.3, temp: [2375, 720], toSmokeAt: 0.5, smokeAlbedo: 0.04, alpha: [0.75, 0], density: 9, distortion: 0.6, erosion: 1.6, fadeIn: 0.06, fadeOut: 0.3 }, |
| +добавлено | ember: { style: 'glow', drag: 0.6, buoyancy: 1.7, gravity: 1.2, curl: 2.8, curlScale: 0.5, life: [1.0, 2.6], size: [0.028, 0.01], stretch: 0.025, emissive: 4, temp: [2090, 950], restitution: 0.3, friction: 0.4, paints: 'char', paintAmount: 0.05 }, |
| +добавлено | spark: { style: 'glow', drag: 0.35, gravity: 9.8, restitution: 0.4, friction: 0.3, life: [0.4, 1.3], size: [0.03, 0.01], stretch: 0.035, emissive: 6, temp: [2750, 1140], paints: 'heat', paintAmount: 0.04 }, |
| +добавлено | smoke: { style: 'media', emissive: 0, drag: 1.3, buoyancy: 1.6, curl: 1.7, curlScale: 0.32, life: [2.2, 4.5], size: [0.35, 2.0], sizePow: 0.55, color: ['#15120f', '#2e2b28'], alpha: [0.62, 0], density: 2.6, erosion: 0.8, fadeIn: 0.12, fadeOut: 0.45 }, |
| +добавлено | dust: { style: 'media', emissive: 0, drag: 3.2, buoyancy: 0.15, curl: 0.6, life: [0.8, 1.8], size: [0.35, 1.4], color: ['#463c34', '#36302b'], alpha: [0.38, 0], density: 1.4, erosion: 0.6 }, |
| +добавлено | debris: { style: 'debris', mesh: 'rock', drag: 0.25, gravity: 9.8, restitution: 0.3, friction: 0.6, life: [1.6, 3.2], size: [0.085, 0.085], color: ['#2a2220', '#ff5a10'], alpha: [1, 0.9], spin: 7, fadeOut: 0.15, paints: 'heat', paintAmount: 0.08 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const lick = P.emitter('fire.flame', { shape: 'shell', radius: 0.32, spread: 0.9, radial: 1.3 }); |
| +добавлено | const flame = P.emitter('fire.flame', { shape: 'sphere', radius: 0.2, spread: 0.5 }); |
| +добавлено | const muzzle = P.emitter('fire.flame', { shape: 'cone', radius: 0.15, spread: 0.6, radial: 7, cone: 0.5 }); |
| +добавлено | const pillar = P.emitter('fire.flame', { shape: 'disk', radius: 1.0, spread: 0.7, radial: -0.6 }); |
| +добавлено | const blast = P.emitter('fire.flame', { shape: 'sphere', radius: 0.6, spread: 2.2, radial: 5, life: 2.6 }); |
| +добавлено | const ember = P.emitter('fire.ember', { shape: 'sphere', radius: 0.25, spread: 1.2 }); |
| +добавлено | const spark = P.emitter('fire.spark', { shape: 'sphere', radius: 0.3, spread: 8, radial: 4 }); |
| +добавлено | const smoke = P.emitter('fire.smoke', { shape: 'sphere', radius: 0.5, spread: 0.6 }); |
| +добавлено | const dust = P.emitter('fire.dust', { shape: 'ring', radius: 0.8, spread: 0.3, radial: 7 }); |
| +добавлено | const debris = P.emitter('fire.debris', { shape: 'sphere', radius: 0.4, spread: 4, radial: 6 }); |
| +добавлено | // ground flames wherever the field is hot (the same flame kind, a little bigger and slower) |
| +добавлено | P.fieldEmitter('fire.flame', { slots: 1536, a: [1, 0, 0, 0], threshold: 0.35, rate: 5, up: 0.8, lifeScale: 1.8, sizeScale: 1.4 }); |
| +добавлено | |
| +добавлено | // the shared combustion fluid: the explosion and the inferno column are simulated fire |
| +добавлено | const fluid = FluidVolume.get(fx); |
| +добавлено | |
| +добавлено | // flight: one raymarched fireball per live cast |
| +добавлено | const tint = new THREE.Vector3(1, 1, 1); |
| +добавлено | const balls = []; |
| +добавлено | for (let i = 0; i < 3; i++) { |
| +добавлено | const v = fx.volumes.add({ shape: 'capsule', glsl: FIREBALL_GLSL, maxSteps: 32, uniforms: { uTint: { value: tint }, uBall: { value: new THREE.Vector4() }, uBall2: { value: new THREE.Vector4() }, uBallPos: { value: new THREE.Vector3() } } }); |
| +добавлено | v.visible = false; v.g = 0.35; |
| +добавлено | balls.push(v); |
| +добавлено | } |
| +добавлено | // where each cast's ball forms: in front of the caster's orb, towards the target (one per cast record) |
| +добавлено | const homes = Array.from({ length: 16 }, () => new THREE.Vector3()); |
| +добавлено | const isFree = (v) => !v.visible; |
| +добавлено | const take = (list) => list.find(isFree) ?? null; |
| +добавлено | |
| +добавлено | // heat-haze billboards (inferno column, explosion) |
| +добавлено | const hazeGeo = new THREE.PlaneGeometry(2, 2); |
| +добавлено | const hazes = []; |
| +добавлено | for (let i = 0; i < 3; i++) { |
| +добавлено | const m = new THREE.Mesh(hazeGeo, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: HAZE_VERT, fragmentShader: HAZE_FRAG, ...FX_BLENDING, side: THREE.DoubleSide, |
| +добавлено | uniforms: { ...fx.U, uHazeA: { value: new THREE.Vector4() }, uHazeB: { value: new THREE.Vector4() } }, |
| +добавлено | })); |
| +добавлено | m.frustumCulled = false; |
| +добавлено | m.visible = false; |
| +добавлено | fx.scenes.fx.add(m); |
| +добавлено | hazes.push(m); |
| +добавлено | } |
| +добавлено | |
| +добавлено | const col = new THREE.Color(), tintCol = new THREE.Color(); |
| +добавлено | const lightColor = (T, out) => { blackbodyRGB(T, out); return out.multiply(tintCol); }; |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _vd = new THREE.Vector3(), _Y = new THREE.Vector3(0, 1, 0), _down = new THREE.Vector3(0, -1, 0), _shC = new THREE.Vector3(); |
| +добавлено | const SPLAT = {}, SHOCK = { pos: new THREE.Vector3() }; |
| +добавлено | const SRC = { pos: new THREE.Vector3(), vel: new THREE.Vector3(), radius: 1, fuel: 0, temp: 0, soot: 0, radial: 0, noise: 0, noiseScale: 1, seed: 0, coupling: 0, burst: false }; |
| +добавлено | let lastT = -1, lastTint = ''; |
| +добавлено | // kind patches, reused (retint runs from phases) |
| +добавлено | const HOT = HOT_KINDS.map(([k, a, b]) => ({ name: `fire.${k}`, a, b, white: { temp: [0, 0] }, tinted: { temp: null, color: [new THREE.Color(), new THREE.Color()] } })); |
| +добавлено | const DEBRIS = { color: [new THREE.Color('#2a2220'), new THREE.Color()] }; |
| +добавлено | |
| +добавлено | // particle kinds follow temperature and tint: blackbody ramps when untinted, else tinted |
| +добавлено | // blackbody colours (the kind table has no tint), written as texels (no recompile) |
| +добавлено | function retint() { |
| +добавлено | const p = S.params; |
| +добавлено | if (p.temperature === lastT && p.tint === lastTint) return; |
| +добавлено | lastT = p.temperature; lastTint = p.tint; |
| +добавлено | tintCol.set(p.tint); |
| +добавлено | tint.set(tintCol.r, tintCol.g, tintCol.b); |
| +добавлено | const white = tintCol.r > 0.97 && tintCol.g > 0.97 && tintCol.b > 0.97; |
| +добавлено | for (const h of HOT) { |
| +добавлено | if (white) { h.white.temp[0] = p.temperature * h.a; h.white.temp[1] = p.temperature * h.b; P.setKind(h.name, h.white); } |
| +добавлено | else { |
| +добавлено | blackbodyRGB(p.temperature * h.a, h.tinted.color[0]).multiply(tintCol); |
| +добавлено | blackbodyRGB(p.temperature * h.b, h.tinted.color[1]).multiply(tintCol).multiplyScalar((h.b / h.a) ** 3); |
| +добавлено | P.setKind(h.name, h.tinted); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // glowing debris: tinted blackbody at 0.8 T, normalised to a peak channel of 1 (#ff5a10 at 1900 K) |
| +добавлено | const e = blackbodyRGB(p.temperature * 0.8, DEBRIS.color[1]).multiply(tintCol); |
| +добавлено | e.multiplyScalar(1 / Math.max(e.r, e.g, e.b, 1e-4)); |
| +добавлено | P.setKind('fire.debris', DEBRIS); |
| +добавлено | } |
| +добавлено | retint(); |
| +добавлено | |
| +добавлено | function splat(x, z, radius, heat, char, pattern, seed, width = 0) { |
| +добавлено | Object.assign(SPLAT, { x, z, radius, heat, char, pattern, seed, width }); |
| +добавлено | fx.ground.splat(SPLAT); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // look shared by both fluid forms (spell params -> domain look) |
| +добавлено | function lookFluid(f, p) { |
| +добавлено | f.temperature = p.temperature; |
| +добавлено | f.tint.copy(tintCol); |
| +добавлено | f.emission = 6.5 * p.intensity; |
| +добавлено | f.sootDensity = 3.2; |
| +добавлено | f.flameDensity = 0.9; |
| +добавлено | f.sootYield = 0.75 * p.soot; |
| +добавлено | f.sootColor.setRGB(0.09, 0.085, 0.08); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // one emitter record, reused |
| +добавлено | function src(f, x, y, z, radius, fuel, temp, soot, burst, noise, noiseScale, seed, vx, vy, vz, radial, coupling) { |
| +добавлено | SRC.pos.set(x, y, z); SRC.radius = radius; SRC.fuel = fuel; SRC.temp = temp; SRC.soot = soot; SRC.burst = burst; |
| +добавлено | SRC.noise = noise; SRC.noiseScale = noiseScale; SRC.seed = seed; SRC.vel.set(vx, vy, vz); SRC.radial = radial; SRC.coupling = coupling; |
| +добавлено | f.emit(SRC); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // capsule proxy along the flight direction: front cap around the ball, body over the tail |
| +добавлено | function placeBall(v, pos, vel, R, I, time, tail, flow, seed) { |
| +добавлено | const p = S.params, sp = vel.length(); |
| +добавлено | if (sp > 1e-4) _vd.copy(vel).divideScalar(sp); else _vd.set(0, 1, 0); |
| +добавлено | const front = R * 1.45, back = tail + R * 0.35, ra = R * 1.5; |
| +добавлено | v.quaternion.setFromUnitVectors(_Y, _vd); |
| +добавлено | v.scale.set(ra, Math.max((front + back) * 0.5, ra), ra); |
| +добавлено | v.position.copy(pos).addScaledVector(_vd, (front - back) * 0.5); |
| +добавлено | v.uniforms.uBall.value.set(R, p.temperature, I, time); |
| +добавлено | v.uniforms.uBall2.value.set(tail, flow, p.soot, seed); |
| +добавлено | v.uniforms.uBallPos.value.copy(pos); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function freeHaze() { return hazes.find(isFree) ?? null; } |
| +добавлено | function placeHaze(m, x, y, z, halfWidth, height, strength) { |
| +добавлено | m.material.uniforms.uHazeA.value.set(x, y, z, halfWidth); |
| +добавлено | m.material.uniforms.uHazeB.value.set(height, strength, 0, 0); |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'tint' || k === 'temperature') retint(); }, |
| +добавлено | |
| +добавлено | // Fireball flies like any projectile; Inferno erupts at the target with no flight |
| +добавлено | travel(c, dt) { |
| +добавлено | if (c.form === 'Inferno') { c.pos.copy(c.to); return true; } |
| +добавлено | c.pos.addScaledVector(c.vel, dt); |
| +добавлено | c.travelled += c.vel.length() * dt; |
| +добавлено | const passed = _a.subVectors(c.to, c.pos).dot(c.dir) <= 0; |
| +добавлено | const hit = (c.travelled > 1 && arenaSDF(c.pos) < S.radius) || passed || c.t > c.durations.travel; |
| +добавлено | if (hit && passed && c.t <= c.durations.travel) c.pos.copy(c.to); |
| +добавлено | return hit; |
| +добавлено | }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = c.data; |
| +добавлено | retint(); |
| +добавлено | if (name === 'charge') { |
| +добавлено | d.light = fx.lights.acquire(); |
| +добавлено | if (d.light) { d.light.range = 9; d.light.intensity = 0; d.light.shadow = true; d.light.type = 0; lightColor(p.temperature, d.light.color); } |
| +добавлено | d.ball = c.form === 'Fireball' ? take(balls) : null; |
| +добавлено | d.home = homes[c.index % 16].copy(c.from).addScaledVector(c.dir, 0.6 * p.size).setY(c.from.y + 0.12 * p.size); |
| +добавлено | if (d.ball) { d.ball.visible = true; placeBall(d.ball, d.home, _down, 0.01, 0, 0, 0.02, 2, c.seed % 89); } |
| +добавлено | d.fl = null; d.trail = null; d.vortex = null; d.haze = null; d.age = 0; d.nextSplat = 0; d.dimmed = false; |
| +добавлено | } else if (name === 'release') { |
| +добавлено | fx.rig.shake(0.12, 10); |
| +добавлено | if (c.form === 'Fireball') { |
| +добавлено | // it leaves from where it formed; the orb that fed it lights back up |
| +добавлено | c.pos.copy(d.home); |
| +добавлено | fx.arena.setFocus(fx.arena.focus.color, 2.5); |
| +добавлено | d.dimmed = false; |
| +добавлено | d.trail = fx.trails.acquire(); |
| +добавлено | const t = d.trail; |
| +добавлено | if (t) { t.width = 0.09 * p.size; t.intensity = 2.5 * p.intensity; t.glow = 3; t.glowIntensity = 0.22; t.fadeTime = 0.14; t.distortion = 0.1 * p.haze; lightColor(p.temperature * 1.15, t.color); } |
| +добавлено | muzzle.pos.copy(c.pos); muzzle.p.copy(c.dir); muzzle.vel.copy(c.dir).multiplyScalar(3); muzzle.size = p.size; muzzle.burst(70 * p.size); |
| +добавлено | } |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | if (d.ball) { d.ball.visible = false; d.ball = null; } |
| +добавлено | d.trail?.release(); d.trail = null; |
| +добавлено | if (c.form === 'Fireball') explode(c, p, d); |
| +добавлено | else erupt(c, p, d); |
| +добавлено | } else if (name === 'done') { |
| +добавлено | if (d.dimmed) { d.dimmed = false; fx.arena.setFocus(fx.arena.focus.color, 2.5); } // cut short while charging |
| +добавлено | d.light?.release(); d.light = null; |
| +добавлено | d.vortex?.release(); d.vortex = null; |
| +добавлено | d.trail?.release(); d.trail = null; |
| +добавлено | d.fl?.release(); d.fl = null; |
| +добавлено | if (d.ball) d.ball.visible = false; |
| +добавлено | if (d.haze) d.haze.visible = false; |
| +добавлено | d.ball = d.haze = null; |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = c.data, L = d.light; |
| +добавлено | const flick = 0.8 + 0.2 * Math.sin(c.age * 31 + c.seed) * Math.sin(c.age * 13.7) + 0.06 * Math.sin(c.age * 71); |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge, at = c.form === 'Fireball' ? d.home : c.pos; |
| +добавлено | if (d.ball) { |
| +добавлено | // the ball boils up out of nothing in front of the orb, which pours itself into it |
| +добавлено | const e = THREE.MathUtils.smoothstep(k, 0, 0.8); |
| +добавлено | placeBall(d.ball, at, _down, 0.4 * p.size * (0.15 + 0.85 * e), 6 * p.intensity * (0.4 + 0.6 * e), c.age, 0.42 * p.size * (0.5 + e), 2.8, c.seed % 89); |
| +добавлено | fx.arena.setFocus(fx.arena.focus.color, 2.5 * (1 - 0.8 * e)); |
| +добавлено | d.dimmed = true; |
| +добавлено | } |
| +добавлено | flame.pos.copy(at); flame.vel.set(0, 0.6, 0); flame.size = p.size * 0.5; flame.rate(140 * p.size, dt); |
| +добавлено | // embers spiral in from a ring around the focus |
| +добавлено | ember.pos.copy(at).addScaledVector(_a.set(Math.sin(c.age * 9), 0.3, Math.cos(c.age * 9)), 0.9); |
| +добавлено | ember.vel.subVectors(at, ember.pos).multiplyScalar(2.5).addScaledVector(_a.set(Math.cos(c.age * 9), 0, -Math.sin(c.age * 9)), 2); |
| +добавлено | ember.rate(180 * p.embers, dt); |
| +добавлено | if (L) { L.pos.copy(at); L.intensity = 14 * k * flick * p.intensity; } |
| +добавлено | if (c.form === 'Inferno') { |
| +добавлено | // the ground at the target heats up in a ring before the eruption |
| +добавлено | if (c.t >= d.nextSplat) { d.nextSplat = c.t + 0.15; splat(c.to.x, c.to.z, 1.7 * p.size * (0.6 + 0.4 * k), 0.3 * p.scorch, 0.2, 'ring', c.seed + c.t, 0.35 * p.size); } |
| +добавлено | ember.pos.set(c.to.x, 0.15, c.to.z); ember.vel.set(0, 1.5, 0); ember.rate(90 * p.embers * k, dt); |
| +добавлено | } |
| +добавлено | } else if (c.phase === 'travel') { |
| +добавлено | const R = 0.4 * p.size, sp = c.vel.length(); |
| +добавлено | if (d.ball) placeBall(d.ball, c.pos, c.vel, R, 4.5 * p.intensity, c.age, R * 2.8, 3 + 0.4 * sp, c.seed % 89); |
| +добавлено | lick.pos.copy(c.pos); lick.radius = 0.8 * R; lick.vel.copy(c.vel).multiplyScalar(0.42); lick.size = p.size; lick.rate(1600 * p.size, dt); |
| +добавлено | smoke.pos.copy(c.pos).addScaledVector(c.dir, -1.2 * R); smoke.radius = 0.3 * p.size; smoke.vel.set(0, 0.7, 0).addScaledVector(c.vel, 0.04); smoke.size = 0.6 * p.size; smoke.rate(75 * p.smoke, dt); |
| +добавлено | ember.pos.copy(c.pos); ember.vel.copy(c.vel).multiplyScalar(0.2); ember.rate(200 * p.embers, dt); |
| +добавлено | d.trail?.push(c.pos); |
| +добавлено | if (L) { L.pos.copy(c.pos); L.intensity = 18 * flick * p.intensity; } |
| +добавлено | } else if (c.form === 'Fireball') updateExplosion(c, p, d, dt, flick); |
| +добавлено | else updateInferno(c, p, d, dt, flick); |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | // --- Fireball impact: a fluid fireball ------------------------------------------------ |
| +добавлено | // Reference: gasoline / fuel-air fireballs (film pyro, high-speed footage): a fuel cloud |
| +добавлено | // ignites and expands violently, its surface boils into turbulent white-yellow lobes, buoyancy |
| +добавлено | // turns it into a rolling vortex ring that climbs and mushrooms, the burning shell cools to |
| +добавлено | // orange and red and is swallowed by black soot, which rises sunlit on top, dark underneath. |
| +добавлено | // the radius of a formed Aegis over the target (its centre in _shC) that the ball met from outside; 0: none |
| +добавлено | function shieldAt(c) { |
| +добавлено | const r = fx.spells.arcane?.inst?.shieldOver?.(c.to.x, c.to.z, _shC) ?? 0; |
| +добавлено | return r > 0 && _shC.distanceToSquared(c.from) > r * r ? r : 0; |
| +добавлено | } |
| +добавлено | |
| +добавлено | function explode(c, p, d) { |
| +добавлено | const pos = c.pos, s = p.size; |
| +добавлено | // an Aegis takes the blast on its shell (the ball stopped there: the dome is an occluder). It goes |
| +добавлено | // off just outside it, within the shield's 0.9 m hit reach so the shell still ripples, and the dome |
| +добавлено | // is a solid in the fire's sim: the fireball splashes on it and rolls up and over it instead of |
| +добавлено | // filling it and hiding the hit |
| +добавлено | const shR = shieldAt(c); |
| +добавлено | if (shR > 0) { |
| +добавлено | _a.subVectors(pos, _shC); |
| +добавлено | if (_a.lengthSq() < 1e-6) _a.subVectors(c.from, _shC).setY(0); |
| +добавлено | pos.copy(_shC).addScaledVector(_a.normalize(), shR + Math.min(0.8 * s, 0.85)); |
| +добавлено | } |
| +добавлено | const f = d.fl = fluid.acquire({ size: [8.5 * s, 10 * s, 8.5 * s] }); |
| +добавлено | if (f) { |
| +добавлено | if (shR > 0) f.obstacle.set(_shC.x, _shC.y, _shC.z, shR); |
| +добавлено | f.position.set(pos.x, 0.5 * f.size.y - f.h, pos.z); |
| +добавлено | lookFluid(f, p); |
| +добавлено | Object.assign(f, { |
| +добавлено | ignition: 0.25, burnRate: 2.6, heatPerFuel: 0.5, expansion: 1.2, cooling: 0.35, radiative: 0.5, sootYield: 0.7 * p.soot, sootDensity: 3.6, flameDensity: 3.5, |
| +добавлено | sootDecay: 0.06, fuelDecay: 0.2, buoyancy: 8, sootWeight: 0.35, vorticity: 7, damping: 0.25, forceCoupling: 0.6, |
| +добавлено | turbulence: 14, turbulenceFreq: 0.45 / s, sootCool: 0.9, detailFreq: 0.4 / s, detailErode: 0.3, detailHeat: 0.4, |
| +добавлено | emptyBelow: 0.05, // the invisible veil round the smoke is skipped (~0.1 ms) |
| +добавлено | }); |
| +добавлено | f.detailCenter.copy(pos); |
| +добавлено | } else { // pool full: a particle fireball |
| +добавлено | blast.pos.copy(pos); blast.radius = 0.6 * s; blast.vel.set(0, 1.5, 0); blast.size = 2.2 * s; blast.burst(300 * s); |
| +добавлено | smoke.pos.copy(pos).setY(pos.y + 0.8 * s); smoke.radius = 1.0 * s; smoke.vel.set(0, 0.6, 0); smoke.size = 1.25 * s; smoke.burst(140 * p.smoke); |
| +добавлено | } |
| +добавлено | spark.pos.copy(pos); spark.vel.set(0, 1, 0); spark.burst(380 * p.embers); |
| +добавлено | debris.pos.copy(pos); debris.vel.set(0, 3, 0); debris.size = s; debris.burst(70 * s); |
| +добавлено | ember.pos.copy(pos); ember.vel.set(0, 2, 0); ember.burst(160 * p.embers); |
| +добавлено | dust.pos.copy(pos).setY(0.15); dust.size = 0.8 * s; dust.burst(45 * s); |
| +добавлено | SHOCK.radius0 = 0.3; SHOCK.radius1 = 7 * s; SHOCK.duration = 0.5; SHOCK.thickness = 0.8; SHOCK.distortion = 0.035 * p.haze; SHOCK.intensity = 0; SHOCK.ground = false; |
| +добавлено | SHOCK.pos.copy(pos); fx.shocks?.spawn(SHOCK); |
| +добавлено | splat(pos.x, pos.z, 2.6 * s * p.scorch, 0.45, 0.9, 'noise', c.seed); |
| +добавлено | fx.impacts.add('scorch', pos, 3.2 * s, 0.6, 9, lightColor(p.temperature, col)); |
| +добавлено | if (d.light) { d.light.intensity = 60 * p.intensity; d.light.range = 16 * s; d.light.pos.copy(pos).setY(pos.y + 0.6); } |
| +добавлено | d.haze = freeHaze(); |
| +добавлено | if (d.haze) d.haze.visible = true; |
| +добавлено | fx.flash(0.2 * p.intensity); |
| +добавлено | fx.rig.shake(0.5, 12); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(3, 0.08); fx.rig.hitstop(0.05); } |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dist = Math.hypot(dm.pos.x - pos.x, dm.pos.z - pos.z); |
| +добавлено | if (shR > 0 && (dm.pos.x - _shC.x) ** 2 + (dm.pos.z - _shC.z) ** 2 < shR * shR) continue; // sheltered |
| +добавлено | if (dist < 3 * s) { dm.status.burn = Math.max(dm.status.burn, 1 - dist / (3 * s) + 0.3); dm.hit(_a.subVectors(dm.pos, pos).setY(0).normalize(), 1 - dist / (3 * s)); } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function updateExplosion(c, p, d, dt, flick) { |
| +добавлено | const a = (d.age += dt), s = p.size, f = d.fl, pos = c.pos, sd = (c.seed % 97) * 0.37; |
| +добавлено | if (f) { |
| +добавлено | const r = shieldAt(c); // the dome stays solid while it stands |
| +добавлено | f.obstacle.set(_shC.x, _shC.y, _shC.z, r); |
| +добавлено | if (a <= dt + 1e-4) { |
| +добавлено | // the fuel cloud: one lumpy core and three off-centre lobes, all igniting at once and |
| +добавлено | // thrown outwards; the noise makes the surface uneven from the first frame |
| +добавлено | src(f, pos.x, pos.y, pos.z, 1.3 * s, 2.0, 1.05, 0.05, true, 0.85, 2.2 / s, sd, 0, 0, 0, 7 * s, 400); |
| +добавлено | for (let i = 0; i < 3; i++) { |
| +добавлено | const ang = sd * 3 + i * 2.1, r = 0.75 * s; |
| +добавлено | _b.set(Math.cos(ang) * r, (i - 1) * 0.35 * s, Math.sin(ang) * r); |
| +добавлено | src(f, pos.x + _b.x, Math.max(pos.y + _b.y, 0.45 * s), pos.z + _b.z, 0.85 * s, 1.8, 1.0, 0.05, true, 0.8, 2.6 / s, sd + i * 1.7, _b.x * 5.5, _b.y * 5.5 + 1.5, _b.z * 5.5, 3 * s, 200); |
| +добавлено | // a fuel-rich, barely lit pocket outside each lobe: dark soot laced over the fire from the first frame |
| +добавлено | const r2 = 1.55 * s, a2 = ang + 1.05; |
| +добавлено | _b.set(Math.cos(a2) * r2, (1 - i) * 0.45 * s + 0.35 * s, Math.sin(a2) * r2); |
| +добавлено | src(f, pos.x + _b.x, Math.max(pos.y + _b.y, 0.4 * s), pos.z + _b.z, 0.55 * s, 0.6, 0.45, 0.7, true, 0.9, 3 / s, sd + 4 + i * 2.3, _b.x * 3, _b.y * 3 + 1, _b.z * 3, 1.5 * s, 200); |
| +добавлено | } |
| +добавлено | } else if (a < 0.16) { |
| +добавлено | // fuel keeps pouring out of the core for a few frames: the fireball keeps growing |
| +добавлено | src(f, pos.x, pos.y, pos.z, 0.8 * s, 9, 0.8, 0, false, 0.5, 1.4 / s, sd + 5, 0, 1, 0, 5 * s, 30); |
| +добавлено | } |
| +добавлено | // the soot erodes late, thin edges first, so the billows stay sharp until the cloud is gone (~5 s) |
| +добавлено | f.sootErode = 1.6 * THREE.MathUtils.smoothstep(a, 1.5, 4.4); |
| +добавлено | // the flash: the first few frames run hotter than the settled burn |
| +добавлено | f.emission = (3.6 + 4 * Math.exp(-a / 0.18)) * p.intensity; |
| +добавлено | // the cell pattern rides the gas: it climbs with the fireball and grows as it expands |
| +добавлено | const grow = 1 - Math.exp(-a / 0.45); |
| +добавлено | f.detailCenter.set(pos.x, pos.y + 1.1 * s * Math.min(a, 3), pos.z); |
| +добавлено | f.detailFreq = 0.4 / (s * (0.75 + 0.75 * grow)); |
| +добавлено | if (a > 5.2) { f.release(); d.fl = null; } |
| +добавлено | } |
| +добавлено | // fluid heat drives the light; it climbs with the fireball |
| +добавлено | const heat = f ? f.heat : 3 * Math.exp(-a * 1.5); |
| +добавлено | if (d.haze) { |
| +добавлено | d.haze.visible = a < 3.5; |
| +добавлено | placeHaze(d.haze, pos.x, Math.max(0, pos.y - 0.5), pos.z, 2.6 * s, 6 * s, 0.07 * p.haze * Math.min(1, heat / 2)); |
| +добавлено | } |
| +добавлено | if (!f && c.phase === 'linger' && c.t < 2) { smoke.pos.copy(pos).setY(pos.y + 2.4 * s + 1.2 * c.t); smoke.radius = 0.9 * s; smoke.vel.set(0, 1.2, 0); smoke.size = 1.4 * s; smoke.rate(4 * p.smoke, dt); } |
| +добавлено | const L = d.light; |
| +добавлено | if (L) { |
| +добавлено | L.pos.set(pos.x, pos.y + 0.6 + Math.min(2.5 * s, a * 1.4 * s), pos.z); |
| +добавлено | L.intensity = Math.min(70, 16 * heat) * p.intensity * (0.85 + 0.15 * flick); |
| +добавлено | L.range = 16 * s; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- Inferno: a fluid fire column -------------------------------------------------------- |
| +добавлено | // Reference: a large pool / gasoline fire: a white-yellow base hugging the ground, flames |
| +добавлено | // necking in and tearing into licking tongues with clear gaps, orange-red tips, and black |
| +добавлено | // smoke rolling off the top. |
| +добавлено | function erupt(c, p, d) { |
| +добавлено | const x = c.to.x, z = c.to.z, s = p.size, H = 8.5 * s * p.height; |
| +добавлено | const f = d.fl = fluid.acquire({ size: [4.2 * s, H, 4.2 * s], res: Math.round(fluid.res * 1.25) }); |
| +добавлено | if (f) { |
| +добавлено | f.position.set(x, 0.5 * f.size.y - f.h, z); |
| +добавлено | lookFluid(f, p); |
| +добавлено | Object.assign(f, { |
| +добавлено | ignition: 0.2, burnRate: 4.5, heatPerFuel: 0.85, expansion: 0.15, cooling: 1.0, radiative: 0.6, |
| +добавлено | sootDecay: 0.18, fuelDecay: 0.6, buoyancy: 6.5, sootWeight: 0.25, vorticity: 9, damping: 0.2, forceCoupling: 0.15, |
| +добавлено | // resolved turbulence and boiling cells: tongues with gaps, mottled burning surfaces |
| +добавлено | turbulence: 9, turbulenceFreq: 0.6 / s, sootCool: 1, detailFreq: 0.55 / s, detailErode: 0.25, detailHeat: 0.35, |
| +добавлено | }); |
| +добавлено | f.sootYield = 0.45 * p.soot; |
| +добавлено | f.detailCenter.set(x, 0, z); |
| +добавлено | // the eruption: a lumpy slug of burning fuel thrown straight up out of the ground |
| +добавлено | src(f, x, 0.6 * s, z, 0.8 * s, 2.0, 1.0, 0.05, true, 0.85, 2.4 / s, (c.seed % 83) * 0.41 + 7, 0, 11 * s, 0, 0, 300); |
| +добавлено | } else { pillar.pos.set(x, 0.1, z); pillar.radius = 0.95 * s; pillar.vel.set(0, 3.5, 0); pillar.size = 1.3 * s; pillar.burst(300 * s); } |
| +добавлено | d.haze = freeHaze(); |
| +добавлено | if (d.haze) d.haze.visible = true; |
| +добавлено | d.vortex = fx.forces.acquire(); |
| +добавлено | if (d.vortex) { const v = d.vortex; v.type = 1; v.p[0] = 0.9 * s; v.p[1] = 9 * s; v.p[2] = 5; v.p[3] = 1.2; v.w = 7 * s * p.height; v.pos.set(x, 0, z); v.dir.set(0, 1, 0); } |
| +добавлено | splat(x, z, 2.4 * s, 0.5 * p.scorch, 0.7, 'noise', c.seed); |
| +добавлено | splat(x, z, 2.1 * s, 0.25 * p.scorch, 1.1 * p.scorch, 'ring', c.seed + 1, 0.45 * s); // the scorched ring |
| +добавлено | fx.impacts.add('scorch', _a.set(x, 0.8, z), 2.6 * s, 0.6, 8, lightColor(p.temperature, col)); |
| +добавлено | spark.pos.set(x, 0.4, z); spark.vel.set(0, 4, 0); spark.burst(260 * p.embers); |
| +добавлено | dust.pos.set(x, 0.15, z); dust.size = s; dust.burst(70 * s); |
| +добавлено | SHOCK.radius0 = 0.5; SHOCK.radius1 = 6 * s; SHOCK.duration = 0.6; SHOCK.thickness = 0.7; SHOCK.distortion = 0.03 * p.haze; SHOCK.intensity = 0; SHOCK.ground = true; |
| +добавлено | SHOCK.pos.set(x, 0, z); fx.shocks?.spawn(SHOCK); |
| +добавлено | if (d.light) { d.light.type = 1; d.light.range = 12 * s; d.light.shadow = true; } |
| +добавлено | fx.flash(0.12 * p.intensity); |
| +добавлено | fx.rig.shake(0.35, 9); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function updateInferno(c, p, d, dt, flick) { |
| +добавлено | const a = (d.age += dt), x = c.to.x, z = c.to.z, s = p.size, f = d.fl, sd = (c.seed % 83) * 0.41; |
| +добавлено | const burn = S.timing.sustain, die = THREE.MathUtils.smoothstep(a, burn, burn + 1.3), on = 1 - die; |
| +добавлено | const R = 0.95 * s; |
| +добавлено | if (f) { |
| +добавлено | if (a < 0.3) { // the jet keeps feeding the eruption for a moment |
| +добавлено | const k = 1 - a / 0.3; |
| +добавлено | src(f, x, 0.4, z, 0.75 * R, 10 * k, 1.0 * k, 0, false, 0.8, 2 / s, sd + 5 + a, 0, 11 * s, 0, 0, 25); |
| +добавлено | } |
| +добавлено | // the boiling cells climb with the gas |
| +добавлено | f.detailCenter.set(x, 2.2 * s * a, z); |
| +добавлено | if (on > 0.02) { |
| +добавлено | // the jet first; the pool of burning fuel spreads in under it from the centre |
| +добавлено | const ramp = THREE.MathUtils.smoothstep(a, 0.05, 0.75) * on; |
| +добавлено | // a flickering pool of burning fuel at the base, its patches drifting up through it |
| +добавлено | src(f, x, 0.2, z, 1.25 * R * (0.45 + 0.55 * ramp), 1.3 * ramp, 0.55 * ramp, 0, true, 0.85, 1.6 / s, sd, 0, 2.2, 0, -0.6, 6); |
| +добавлено | // a hot core feeding the column |
| +добавлено | src(f, x, 0.35, z, 0.65 * R, 1.6 * ramp, 0.8 * ramp, 0, true, 0.5, 2.2 / s, sd + 3, 0, 4.5, 0, 0, 10); |
| +добавлено | } |
| +добавлено | f.sootDecay = 0.18 + 0.6 * THREE.MathUtils.smoothstep(a, burn + 1.5, burn + 4); |
| +добавлено | f.emission = 6.5 * p.intensity; |
| +добавлено | if (a > burn + 4.6) { f.release(); d.fl = null; } |
| +добавлено | } |
| +добавлено | const heat = f ? f.heat : 4 * on; |
| +добавлено | const H = Math.max(0.6, 6 * s * p.height * Math.min(1, heat / 6)); |
| +добавлено | if (d.haze) { |
| +добавлено | d.haze.visible = a < burn + 2.2; |
| +добавлено | placeHaze(d.haze, x, 0, z, 2.2 * s, H * 1.6 + 2, 0.09 * p.haze * (1 - 0.7 * die)); |
| +добавлено | } |
| +добавлено | if (on > 0.01) { |
| +добавлено | ember.pos.set(x, 0.5, z); ember.radius = R; ember.vel.set(0, 2, 0); ember.rate(260 * p.embers * on, dt); |
| +добавлено | if (Math.floor(a * 4) !== Math.floor((a - dt) * 4)) splat(x, z, 2 * s, 0.25 * p.scorch, 0.15, 'noise', c.seed + a); |
| +добавлено | for (const dm of fx.dummies) if (Math.hypot(dm.pos.x - x, dm.pos.z - z) < 1.6 * s) dm.status.burn = Math.max(dm.status.burn, 0.9); |
| +добавлено | } |
| +добавлено | if (d.vortex) d.vortex.w = 7 * s * p.height * on; |
| +добавлено | const L = d.light; |
| +добавлено | if (L) { |
| +добавлено | L.pos.set(x, 0.3, z); L.end.set(x, Math.max(0.6, H * 0.6), z); |
| +добавлено | L.intensity = Math.min(40, 5 * heat) * p.intensity * (0.8 + 0.2 * flick) + 3 * p.intensity * Math.max(0, 1 - c.t / c.durations.linger) * die; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import { COMMON_GLSL, NOISE_GLSL, WORLD_GLSL, LIGHTS_GLSL } from '../../vfx/common.js'; |
| +добавлено | import { SWARM_STATE_GLSL, SWARM_DUMMY_GLSL, SWARM_BOIDS_GLSL } from '../acid/plague-swarm.glsl.js'; |
| +добавлено | |
| +добавлено | // Ember Swarm shaders, on the GPU agent engine in plague-swarm.glsl.js (same state, flocking grid |
| +добавлено | // and floor light). Agent states: 1 flying hot, 2 stuck burning on the dummy, 3 cooling flake, |
| +добавлено | // 4 lying on the floor (v.x = its temperature on landing), 5 spent. |
| +добавлено | |
| +добавлено | // --- temperature: shared by the sim (hot ash rises) and the look --------------------------------- |
| +добавлено | // each ember has its own: most are dull, a few burn far hotter (steep power law). Flying, airflow |
| +добавлено | // fans it. Falling, radiative cooling (dT/dt ~ -T^4) gives T = T0 / cbrt(1 + t / tc). |
| +добавлено | const KELVIN_GLSL = /* glsl */` |
| +добавлено | uniform vec4 uHeat; // base temperature (K), airflow fan share, global heat, - |
| +добавлено | vec4 emberHash(int idx) { return hash41(float(idx) * 0.5772157 + 2.9); } |
| +добавлено | float emberKelvin(int idx, vec3 v, float st, float age) { |
| +добавлено | vec4 e = emberHash(idx); |
| +добавлено | float T0 = uHeat.x * mix(0.74, 1.3, e.x * e.x); // median as before (flake lifetimes, so cost, unchanged), fewer white-hot outliers |
| +добавлено | float tc = mix(0.14, 0.55, e.y); |
| +добавлено | if (st > 3.5) return v.x * pow(1.0 + age / (tc * 10.0), -0.3333); |
| +добавлено | if (st > 2.5) return T0 * 1.12 * pow(1.0 + age / tc, -0.3333); |
| +добавлено | if (st > 1.5) return T0 * 1.12 * pow(1.0 + age / (tc * 3.0), -0.3333); |
| +добавлено | return T0 * uHeat.z * (1.0 + uHeat.y * smoothstep(1.5, 9.0, length(v))); |
| +добавлено | } |
| +добавлено | float emberRestLife(vec4 h) { return mix(2.0, 4.2, h.w); } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | // --- behaviour ------------------------------------------------------------------------------------ |
| +добавлено | export const EMBER_SIM = /* glsl */` |
| +добавлено | uniform vec4 uLeader; // cloud centre xyz, radius (m) |
| +добавлено | uniform vec4 uLeaderV; // leader velocity xyz, - |
| +добавлено | uniform vec4 uTarget; // shell centre xyz, radius |
| +добавлено | uniform vec4 uMode; // shell on 0..1, disperse 0..1, landing share, attrition share |
| +добавлено | uniform vec4 uFlock; // alignment, separation, cohesion, turbulence |
| +добавлено | uniform vec4 uMove; // cruise m/s, orbit m/s, steering gain, rest density (agents / cell) |
| +добавлено | uniform vec4 uSpawnP; // orb centre xyz, radius |
| +добавлено | uniform vec4 uSpawnF; // floor spawn centre x, z, radius, share of the swarm |
| +добавлено | uniform vec4 uClock; // s since dispersal (-1 before), force response, s since shell closed (-1 before), hold (s) |
| +добавлено | uniform vec4 uPathA; // ribbon path: start xyz, duration (s) |
| +добавлено | uniform vec4 uPathM; // its Bezier control point xyz, cast clock (s) |
| +добавлено | uniform vec4 uPathB; // its end xyz, start time (s) |
| +добавлено | uniform vec4 uStream; // delay spread along the ribbon (s), half-width (m), -, - |
| +добавлено | uniform vec4 uDummyP; // target dummy base xyz, 1 = landing allowed |
| +добавлено | uniform mat3 uDummyR; |
| +добавлено | ${KELVIN_GLSL} |
| +добавлено | ${SWARM_BOIDS_GLSL} |
| +добавлено | ${SWARM_DUMMY_GLSL} |
| +добавлено | vec3 pathAt(float s) { |
| +добавлено | s = clamp(s, 0.0, 1.0); |
| +добавлено | float k = s * s * (3.0 - 2.0 * s) * 0.6 + s * s * 0.4, i = 1.0 - k; |
| +добавлено | return i * i * uPathA.xyz + 2.0 * i * k * uPathM.xyz + k * k * uPathB.xyz; |
| +добавлено | } |
| +добавлено | |
| +добавлено | void swarmSpawn(int idx, vec4 h, inout vec3 p, inout vec3 v) { |
| +добавлено | vec3 r = hash31(float(idx) * 1.37 + uTime * 13.1); |
| +добавлено | if (h.z < uSpawnF.w) { // lifting off the floor around the caster |
| +добавлено | float a = r.x * 6.2832, R = uSpawnF.z * sqrt(mix(0.04, 1.0, r.y)); |
| +добавлено | p = vec3(uSpawnF.x + cos(a) * R, 0.03, uSpawnF.y + sin(a) * R); |
| +добавлено | v = vec3(0.0, 1.4 + 2.2 * r.z, 0.0) + normalize(uLeader.xyz - p) * (0.5 + r.y) + vec3(-sin(a), 0.0, cos(a)) * 1.1; |
| +добавлено | } else { // boiling out of the orb |
| +добавлено | vec3 d = randomOnSphere(r.xy); |
| +добавлено | p = uSpawnP.xyz + d * uSpawnP.w * (0.4 + 0.6 * r.z); |
| +добавлено | v = (normalize(cross(vec3(0.0, 1.0, 0.0), d) + 1e-4) * 2.4 + vec3(0.0, 1.8, 0.0) + d * 1.2 + normalize(uLeader.xyz - p)) * mix(0.7, 1.3, h.y); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | void swarmStep(int idx, vec4 h, inout vec3 p, inout vec3 v, inout float st, inout float age, float dt) { |
| +добавлено | vec4 h2 = swarmHash2(idx), h3 = swarmHash3(idx); |
| +добавлено | float t = uTime; |
| +добавлено | if (st > 3.5) { // on the floor: smoulders out, then it's gone (v.x = landing temperature) |
| +добавлено | if (age > emberRestLife(h)) st = 5.0; |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (st > 2.5) { // a cooling flake: hot ash rides its own draught, cold ash flutters down |
| +добавлено | float hot = smoothstep(800.0, 1500.0, emberKelvin(idx, v, st, age)); |
| +добавлено | vec3 wind = curl3(p * 0.22 + vec3(0.0, t * 0.03, 0.4)) * 1.1 + curl3(p * 1.3 + h.xyz * 5.0 + t * 0.25) * 0.7; |
| +добавлено | vec3 drift = vec3(0.0, 1.3 * hot - mix(0.55, 1.25, h.y), 0.0) + wind; |
| +добавлено | v += (drift - v) * (1.0 - exp(-mix(1.6, 3.4, h.z) * dt)); |
| +добавлено | float kill = 0.0; |
| +добавлено | v += forceAccel(p, v, kill) * uClock.y * dt; |
| +добавлено | p += v * dt; |
| +добавлено | float d = arenaSDF(p); |
| +добавлено | if (d < 0.006) { |
| +добавлено | vec3 n = arenaNormal(p); |
| +добавлено | p += n * (0.006 - d); |
| +добавлено | if (n.y > 0.5) { v = vec3(emberKelvin(idx, v, st, age), 0.0, 0.0); st = 4.0; age = 0.0; } |
| +добавлено | else v -= n * min(dot(v, n), 0.0); |
| +добавлено | } else if (age > 14.0) st = 5.0; |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (st > 1.5) { // stuck to the dummy, burning into it |
| +добавлено | vec3 n = dummyNormal(p); |
| +добавлено | p -= n * (dummySDF(p) - 0.006); |
| +добавлено | v = vec3(0.0); |
| +добавлено | if (uDummyP.w < 0.5 || age > mix(1.2, 3.4, h3.y)) { st = 3.0; age = 0.0; v = n * 0.7; } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- flying --- |
| +добавлено | vec3 rr = p - uTarget.xyz; |
| +добавлено | if (uClock.x >= 0.0 && uClock.x > h2.z * 0.25) { // the burst: the flock flies apart and cools |
| +добавлено | st = 3.0; age = 0.0; |
| +добавлено | vec3 dir = normalize(rr * vec3(1.0, 0.75, 1.0) + (h.xyz - 0.5) * 0.6 + vec3(0.0, 0.3, 0.0) + 1e-3); |
| +добавлено | v = dir * mix(3.5, 12.0, h3.y * h3.y) + v * 0.35; |
| +добавлено | p += v * dt; |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | // each ember leaves on its own delay: the sheet below unwinds in delay order, into the ribbon |
| +добавлено | float sPath = (uPathM.w - h2.z * uStream.x - uPathB.w) / uPathA.w; |
| +добавлено | float env = smoothstep(0.82, 1.1, sPath) * uMode.x, inStream = (1.0 - env) * step(0.0, sPath); |
| +добавлено | float cruise = uMove.x * mix(0.75, 1.25, h.y); |
| +добавлено | vec3 acc = boidAccel(p, v) * (1.0 - 0.75 * inStream); |
| +добавлено | |
| +добавлено | // goal 1: a slot on a slowly folding, precessing sheet (the murmuration) |
| +добавлено | float tS = t * 0.62, r0 = sqrt(h3.x), th = h2.z * 6.2832; |
| +добавлено | vec3 o = vec3(r0 * cos(th), (h3.z - 0.5) * 0.16, r0 * sin(th)); |
| +добавлено | o.y += 0.6 * sin(2.1 * o.x + 1.3 * tS) + 0.42 * sin(1.7 * o.z - 0.9 * tS + 1.7) + 0.3 * o.x * o.z * sin(0.7 * tS); |
| +добавлено | float ys = 0.5 * tS, tilt = 0.55 * sin(0.42 * tS) + 0.45; |
| +добавлено | o.xz = mat2(cos(ys), -sin(ys), sin(ys), cos(ys)) * (o.xz * vec2(1.0 + 0.4 * sin(0.6 * tS), 1.0 - 0.3 * sin(0.6 * tS))); |
| +добавлено | o.xy = mat2(cos(tilt), -sin(tilt), sin(tilt), cos(tilt)) * o.xy; |
| +добавлено | o += 0.2 * curl3(o * 0.3 + vec3(0.13, tS * 0.04, 0.71)); |
| +добавлено | vec3 goal = uLeader.xyz + o * uLeader.w, lv = uLeaderV.xyz; |
| +добавлено | if (sPath > 0.0) { // in the ribbon: a flat, twisting cross-section that undulates down its length |
| +добавлено | vec3 P = pathAt(sPath); |
| +добавлено | lv = (P - pathAt(sPath - 0.02 / uPathA.w)) / 0.02; |
| +добавлено | vec3 T = normalize(lv + vec3(1e-4, 0.0, 0.0)), S = normalize(cross(T, vec3(0.0, 1.0, 0.0)) + 1e-4), N = cross(S, T); |
| +добавлено | float bell = sin(3.14159 * clamp(sPath, 0.0, 1.0)), tw = sPath * 5.5 + t * 1.3; |
| +добавлено | float W = uStream.y * (0.12 + sqrt(max(4.0 * h2.z * (1.0 - h2.z), 0.0))); |
| +добавлено | P += (S * sin(sPath * 7.0 - t * 3.1) * 0.5 + N * sin(sPath * 4.3 - t * 2.2) * 0.4) * bell; |
| +добавлено | goal = P + (S * cos(tw) + N * sin(tw)) * (h3.x * 2.0 - 1.0) * W + (N * cos(tw) - S * sin(tw)) * (h3.z - 0.5) * W * 0.22; |
| +добавлено | } |
| +добавлено | vec3 dc = (goal - p) * mix(3.0, 5.0, inStream); |
| +добавлено | float dl = length(dc), cap = cruise * mix(1.7, 3.0, inStream); |
| +добавлено | if (dl > cap) dc *= cap / dl; |
| +добавлено | vec3 dCloud = dc + lv; |
| +добавлено | |
| +добавлено | // goal 2: the shell. Three wound bands on precessing axes and a diffuse share: the ribbon keeps |
| +добавлено | // its windings, like thread on a ball, and a loose halo of long strays outside it |
| +добавлено | float R = uTarget.w; |
| +добавлено | vec3 rs = rr / vec3(1.0, 1.25, 1.0); |
| +добавлено | float dist = max(length(rs), 1e-3); |
| +добавлено | float axA = floor(h3.w * 3.0) * 2.0944 + t * 0.23; |
| +добавлено | vec3 ax = normalize(normalize(vec3(cos(axA) * 0.62, 1.0, sin(axA) * 0.62)) + (vec3(h2.x, h.w, h3.y) - 0.5) * 0.5); |
| +добавлено | if (h3.z < 0.08) ax = -ax; // a few fly against the stream |
| +добавлено | float lump = noise3(rs / dist * 0.35 + vec3(t * 0.06, 0.0, 0.31)) - 0.5; |
| +добавлено | bool stray = h2.x > 0.88; |
| +добавлено | float shellR = R * (1.0 + 0.05 * sin(t * 1.9 + h.x * 0.8)) * (1.0 + 0.8 * lump) * (stray ? mix(1.3, 2.3, h3.x) : mix(0.9, 1.1, h3.x)); |
| +добавлено | float la = dot(rr, ax), latGoal = (h3.x - 0.5) * R * (fract(h3.w * 3.0) < 0.7 ? 0.7 : 1.9); |
| +добавлено | vec3 tang = normalize(cross(ax, rr) + 1e-4); |
| +добавлено | float vt = uMove.y * mix(0.7, 1.25, h.w) * (stray ? 0.75 : 1.0); |
| +добавлено | vec3 dEnv = tang * vt + (rs / dist) * (shellR - dist) * 5.0 + ax * (latGoal - la) * 2.2; |
| +добавлено | // centripetal feed-forward: hold the circle about the ember's own axis |
| +добавлено | vec3 q = rr - ax * la; |
| +добавлено | float qr = max(length(q), 0.25); |
| +добавлено | acc -= q / qr * (vt * vt / qr) * env; |
| +добавлено | // landers make for the dummy and stick there, burning |
| +добавлено | bool lander = h2.w < uMode.z && uDummyP.w > 0.5; |
| +добавлено | float dd = 1e3; |
| +добавлено | if (lander) { |
| +добавлено | dd = dummySDF(p); |
| +добавлено | dEnv = mix(dEnv, -dummyNormal(p) * clamp(dd * 4.0, 0.5, cruise) + tang * 0.6, smoothstep(1.4, 0.35, dd)); |
| +добавлено | if (dd < 0.025 && env > 0.9) { st = 2.0; age = 0.0; v = vec3(0.0); return; } |
| +добавлено | } |
| +добавлено | acc += uMove.z * mix(1.0 + 2.2 * inStream, 2.6, env) * (mix(dCloud, dEnv, env) - v); |
| +добавлено | |
| +добавлено | // turbulence: swarm-scale eddies, each ember's own wander, sudden darts in the shell |
| +добавлено | acc += curl3(p * 0.06 + vec3(0.0, t * 0.025, 0.0)) * uFlock.w * 2.4; |
| +добавлено | acc += curl3(p * 0.85 + h.xyz * 7.0 + vec3(t * 0.37)) * uFlock.w * mix(1.0, 3.2, env); |
| +добавлено | acc += curl3(p * 2.2 + h.zxy * 13.0 + vec3(t * 1.3)) * uFlock.w * 4.0 * env; |
| +добавлено | float sp = length(v); |
| +добавлено | acc += (v / max(sp, 1e-3)) * (cruise - sp) * 1.3 * (1.0 - inStream); |
| +добавлено | // the world: floor, rim, pillars, dummies (not the lander's own target), other spells' forces |
| +добавлено | float kill = 0.0; |
| +добавлено | bool nearTarget = lander && dd < 1.0; |
| +добавлено | float dw = nearTarget ? p.y : arenaSDF(p); |
| +добавлено | if (dw < 0.6) acc += (nearTarget ? vec3(0.0, 1.0, 0.0) : arenaNormal(p)) * (0.6 - dw) * 26.0; |
| +добавлено | acc += forceAccel(p, v, kill) * uClock.y; |
| +добавлено | v += acc * dt; |
| +добавлено | sp = length(v); |
| +добавлено | float vMax = cruise * mix(2.3, 3.6, inStream); |
| +добавлено | if (sp > vMax) v *= vMax / sp; |
| +добавлено | p += v * dt; |
| +добавлено | float d2 = (nearTarget ? p.y : arenaSDF(p)) - 0.02; |
| +добавлено | if (d2 < 0.0) { |
| +добавлено | vec3 nn = nearTarget ? vec3(0.0, 1.0, 0.0) : arenaNormal(p); |
| +добавлено | p -= nn * d2; |
| +добавлено | v -= nn * min(dot(v, nn), 0.0) * 1.3; |
| +добавлено | } |
| +добавлено | // the shell sheds: its own moment for each, it stops flying, cools and falls |
| +добавлено | if (uClock.z >= 0.0 && h2.y < uMode.w && uClock.z > fract(h2.y * 7.31) * uClock.w) { st = 3.0; age = 0.0; v *= 0.45; } |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- look ----------------------------------------------------------------------------------------- |
| +добавлено | export const EMBER_LOOK = /* glsl */` |
| +добавлено | uniform vec4 uLook; // size, glow, shutter (s), arcane share |
| +добавлено | uniform vec4 uShape; // master fade, shell curvature 0..1, overlap dimming, share leaving a flake |
| +добавлено | uniform vec4 uTarget; |
| +добавлено | uniform vec3 uArcane; // luminance-1 magenta |
| +добавлено | ${KELVIN_GLSL} |
| +добавлено | // Stefan-Boltzmann, with the Draper point: nothing visible below ~800 K |
| +добавлено | float emberI(float T) { float r = T / 1500.0; r *= r; return r * r * smoothstep(760.0, 980.0, T); } |
| +добавлено | float emberFade(float st, float age) { return uShape.x * (st < 1.5 ? smoothstep(0.0, 0.5, age) : 1.0); } |
| +добавлено | // slow breathing of the burn (the fast tumbling is per pixel along the streak) |
| +добавлено | float emberBreath(vec4 e) { return 0.78 + 0.22 * sin(uTime * mix(2.0, 7.0, e.z) + e.w * 40.0); } |
| +добавлено | vec3 swarmLight(int idx, vec4 h, vec3 p, vec3 v, float st, float age) { |
| +добавлено | float T = emberKelvin(idx, v, st, age); |
| +добавлено | return blackbody(T) * emberI(T) * emberFade(st, age) * uLook.y; |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | // a long-exposure streak: the ember's real path over the shutter (bent by its orbit), with its |
| +добавлено | // temperature on the head and its tumbling drawn along the length as dashes. x: -1 head cap, |
| +добавлено | // 0..1 along the path, 2 tail cap; y: -1..1 across. |
| +добавлено | export const STREAK_VERT = (W) => /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | precision highp int; |
| +добавлено | precision highp sampler2D; |
| +добавлено | uniform mat4 viewMatrix; |
| +добавлено | uniform mat4 projectionMatrix; |
| +добавлено | in vec3 position; |
| +добавлено | ${COMMON_GLSL} |
| +добавлено | ${NOISE_GLSL} |
| +добавлено | ${SWARM_STATE_GLSL(W)} |
| +добавлено | ${EMBER_LOOK} |
| +добавлено | out vec3 vUvC; // s along, y across, cap -1..1 |
| +добавлено | out vec3 vCol; |
| +добавлено | out vec3 vCore; |
| +добавлено | out vec3 vTumble; // phase at the head, radians over the streak, depth |
| +добавлено | void main() { |
| +добавлено | int idx = gl_InstanceID; |
| +добавлено | ivec2 px = swarmTexel(idx); |
| +добавлено | vec4 s0 = texelFetch(uS0, px, 0), s1 = texelFetch(uS1, px, 0); |
| +добавлено | gl_Position = vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | vUvC = vec3(0.0); vCol = vCore = vTumble = vec3(0.0); |
| +добавлено | float st = s1.w; |
| +добавлено | if (st < 0.5 || st > 4.5) return; |
| +добавлено | float T = emberKelvin(idx, s1.xyz, st, s0.w); |
| +добавлено | float I = emberI(T) * emberFade(st, s0.w); |
| +добавлено | if (I < 1e-3) return; |
| +добавлено | vec4 e = emberHash(idx); |
| +добавлено | vec3 p = s0.xyz, v = (st > 1.5 && st < 2.5) || st > 3.5 ? vec3(0.0) : s1.xyz; |
| +добавлено | float tau = uLook.z * mix(0.6, 1.4, e.y); |
| +добавлено | // the path backwards over the shutter: x(-t) = p - v t + a t^2 / 2, a = the orbit's centripetal |
| +добавлено | vec3 a = vec3(0.0); |
| +добавлено | float sp2 = dot(v, v); |
| +добавлено | if (st < 1.5 && sp2 > 0.01) { |
| +добавлено | vec3 rr = p - uTarget.xyz; |
| +добавлено | vec3 q = rr - v * (dot(rr, v) / sp2); |
| +добавлено | float q2 = dot(q, q); |
| +добавлено | a = -q * (min(sp2 / max(q2, 0.09), 60.0)) * uShape.y * (1.0 - smoothstep(4.0, 9.0, dot(rr, rr))); |
| +добавлено | } |
| +добавлено | // overlap: thousands of lines in one place must keep their structure, so the dense parts run dimmer |
| +добавлено | float dens = st < 1.5 ? uShape.z * mix(1.0, 0.14, uShape.y * (1.0 - smoothstep(1.15, 1.7, length(p - uTarget.xyz) / uTarget.w))) : mix(0.35, 1.0, smoothstep(0.0, 0.5, s0.w)); |
| +добавлено | float s = clamp(position.x, 0.0, 1.0), cap = position.x < 0.0 ? -1.0 : position.x > 1.0 ? 1.0 : 0.0; |
| +добавлено | float ts = tau * s; |
| +добавлено | vec3 P = p - v * ts + a * (0.5 * ts * ts), dP = (a * ts - v) * tau; |
| +добавлено | vec4 Pv = viewMatrix * vec4(P, 1.0); |
| +добавлено | Pv.z += 0.02; |
| +добавлено | vec2 tv = (mat3(viewMatrix) * dP).xy; |
| +добавлено | float len = length(tv); |
| +добавлено | vec2 dir = len > 1e-5 ? tv / len : vec2(1.0, 0.0), perp = vec2(-dir.y, dir.x); |
| +добавлено | float viewZ = max(-Pv.z, 0.05); |
| +добавлено | float pxWorld = 2.0 * viewZ / (projectionMatrix[1][1] * uResolution.y); |
| +добавлено | float breath = emberBreath(e); |
| +добавлено | // sizes run a power law, as in wildfire ember showers: mostly grains, a few fat burning flakes |
| +добавлено | float big = e.z * e.z * e.z; |
| +добавлено | float size = uLook.x * 0.0052 * mix(0.55, 2.2, big) * (0.7 + 0.3 * breath); // mean and mean-square width at or under a uniform spread: no extra fill |
| +добавлено | float w = max(size, 1.05 * pxWorld), aa = size / w; |
| +добавлено | // it never flies a ruled line: a small wander grows down the tail |
| +добавлено | Pv.xy += perp * (0.035 * len * s * sin(s * (3.0 + 9.0 * e.x) + e.y * 40.0 + uTime * 3.0)); |
| +добавлено | Pv.xy += dir * cap * w + perp * position.y * w; |
| +добавлено | gl_Position = projectionMatrix * Pv; |
| +добавлено | // the same energy spread down the streak, a little held back so long streaks still read |
| +добавлено | // (a sub-pixel ember keeps more than its area: seen from across the arena it is a point of light |
| +добавлено | // that blooms, not a dim smear) |
| +добавлено | float k = pow(2.0 * w / (len + 2.0 * w), 0.55) * pow(aa, 0.4) * smoothstep(0.25, 0.8, viewZ) * breath * uLook.y * I * dens; |
| +добавлено | // the hottest cores whiten; for the arcane touch one ember in eight (once it burns properly) runs |
| +добавлено | // magenta, so the swarm is shot through with it instead of tinted pink (the hot ones carry most |
| +добавлено | // of the light: they stay blackbody) |
| +добавлено | // the glow is the ember's own blackbody colour; the core reads a step hotter (yellow, as a camera |
| +добавлено | // sees a glowing ember), never white |
| +добавлено | float hot = smoothstep(uHeat.x * 1.1, uHeat.x * 1.4, T); |
| +добавлено | float arc = uLook.w * 1.5 * step(0.875, e.w) * smoothstep(1050.0, 1350.0, T); |
| +добавлено | vCol = mix(blackbody(T), uArcane, min(arc, 0.9)) * k; |
| +добавлено | vCore = mix(blackbody(T * 1.3), uArcane, min(arc, 0.7)) * k * (0.4 + 0.7 * hot); |
| +добавлено | float om = mix(60.0, 220.0, e.z); |
| +добавлено | vUvC = vec3(s, position.y, cap); |
| +добавлено | vTumble = vec3(om * uTime + e.w * 80.0, -om * tau, e.w > 0.55 ? 0.72 : 0.18); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const STREAK_FRAG = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | in vec3 vUvC; |
| +добавлено | in vec3 vCol; |
| +добавлено | in vec3 vCore; |
| +добавлено | in vec3 vTumble; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | float s = vUvC.x, d2 = vUvC.y * vUvC.y + vUvC.z * vUvC.z; |
| +добавлено | float dash = 1.0 - vTumble.z * (0.5 + 0.5 * cos(vTumble.x + vTumble.y * s)); |
| +добавлено | fragColor = vec4((vCol * exp(-d2 * 3.0) + vCore * exp(-d2 * 20.0)) * dash * mix(1.0, 0.3, s), 0.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // flakes: what is left of an ember. Ragged tumbling scraps in the opaque pass (alpha to coverage), |
| +добавлено | // charred and lit by the sky and the spell's lights, their burning edge still glowing while hot |
| +добавлено | export const FLAKE_VERT = (W) => /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | precision highp int; |
| +добавлено | precision highp sampler2D; |
| +добавлено | uniform mat4 viewMatrix; |
| +добавлено | uniform mat4 projectionMatrix; |
| +добавлено | in vec3 position; |
| +добавлено | ${COMMON_GLSL} |
| +добавлено | ${NOISE_GLSL} |
| +добавлено | ${WORLD_GLSL} |
| +добавлено | ${LIGHTS_GLSL} |
| +добавлено | ${SWARM_STATE_GLSL(W)} |
| +добавлено | ${EMBER_LOOK} |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | uniform vec3 uAsh; |
| +добавлено | out vec2 vUv; |
| +добавлено | out vec3 vLit; |
| +добавлено | out vec3 vEmit; |
| +добавлено | out vec2 vK; // shape seed, alpha |
| +добавлено | void main() { |
| +добавлено | int idx = gl_InstanceID; |
| +добавлено | ivec2 px = swarmTexel(idx); |
| +добавлено | vec4 s0 = texelFetch(uS0, px, 0), s1 = texelFetch(uS1, px, 0); |
| +добавлено | gl_Position = vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | vUv = vK = vec2(0.0); vLit = vEmit = vec3(0.0); |
| +добавлено | float st = s1.w; |
| +добавлено | if (st < 1.5 || st > 4.5) return; |
| +добавлено | vec4 h = swarmHash(idx), e = emberHash(idx); |
| +добавлено | float age = s0.w; |
| +добавлено | float fade = uShape.x * (st > 3.5 ? 1.0 - smoothstep(emberRestLife(h) - 1.2, emberRestLife(h), age) : 1.0); |
| +добавлено | if (fade <= 0.0) return; |
| +добавлено | vec3 p = s0.xyz, ax1, ax2; |
| +добавлено | if (st > 3.5) { // lying flat |
| +добавлено | float a = h.y * 6.2832; |
| +добавлено | ax1 = vec3(cos(a), 0.0, sin(a)); ax2 = vec3(-sin(a), 0.0, cos(a)); |
| +добавлено | p.y += 0.004; |
| +добавлено | } else { // tumbling (still, once stuck to the dummy) |
| +добавлено | float w = st > 2.5 ? mix(2.0, 9.0, e.w) : 0.0, a1 = uTime * w + h.x * 50.0, a2 = uTime * w * 0.63 + h.y * 50.0; |
| +добавлено | ax1 = vec3(cos(a1), sin(a1) * cos(a2), sin(a1) * sin(a2)); |
| +добавлено | ax2 = normalize(cross(ax1, normalize(h.xyz - 0.5 + 1e-3))); |
| +добавлено | } |
| +добавлено | // most sparks burn out to nothing as they cool; what stays is fine powder and a few real scraps |
| +добавлено | float keep = step(e.w, uShape.w) + (1.0 - step(e.w, uShape.w)) * smoothstep(850.0, 1050.0, emberKelvin(idx, s1.xyz, st, age)); |
| +добавлено | if (keep <= 0.0) return; |
| +добавлено | float L = 0.012 * uLook.x * mix(0.45, 2.0, pow(e.z, 4.0)) * keep; |
| +добавлено | vec4 cv = viewMatrix * vec4(p, 1.0); |
| +добавлено | float viewZ = max(-cv.z, 0.05); |
| +добавлено | if (viewZ < 0.15) return; |
| +добавлено | float pxW = 2.0 * viewZ / (projectionMatrix[1][1] * uResolution.y); |
| +добавлено | float grow = max(1.0, 0.9 * pxW / L); // never thinner than a pixel: spread as coverage instead |
| +добавлено | vec3 Pw = p + (ax1 * position.x + ax2 * position.y * 0.75) * L * grow; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(Pw, 1.0); |
| +добавлено | vUv = position.xy; |
| +добавлено | vK = vec2(h.z * 40.0, fade / (grow * grow)); |
| +добавлено | float T = emberKelvin(idx, s1.xyz, st, age); |
| +добавлено | gLightNoShadow = true; |
| +добавлено | vec3 n = normalize(cross(ax1, ax2)); |
| +добавлено | vLit = uAsh * mix(0.5, 1.6, e.x) * (uAmbient * 1.6 + uSunColor * abs(dot(n, uSunDir)) * 0.5 + spellLightDiffuse(p, vec3(0.0)) * 0.6); |
| +добавлено | vEmit = blackbody(T) * emberI(T) * uLook.y * 0.3; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const FLAKE_FRAG = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | in vec2 vUv; |
| +добавлено | in vec3 vLit; |
| +добавлено | in vec3 vEmit; |
| +добавлено | in vec2 vK; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | float r = length(vUv), th = atan(vUv.y, vUv.x); |
| +добавлено | float edge = 0.7 + 0.14 * sin(th * 2.0 + vK.x) + 0.12 * sin(th * 5.0 + vK.x * 2.3) + 0.06 * sin(th * 11.0 + vK.x * 3.7); |
| +добавлено | float a = (1.0 - smoothstep(edge - 0.12, edge, r)) * vK.y; |
| +добавлено | if (a < 0.04) discard; |
| +добавлено | fragColor = vec4(vLit + vEmit * (0.04 + 1.4 * smoothstep(edge - 0.35, edge - 0.02, r)), a); |
| +добавлено | }`; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { blackbodyRGB } from '../../vfx/noise.js'; |
| +добавлено | import { LINES_BLENDING } from '../../vfx/common.js'; |
| +добавлено | import { Swarm } from '../acid/plague-swarm.glsl.js'; |
| +добавлено | import { EMBER_SIM, EMBER_LOOK, STREAK_VERT, STREAK_FRAG, FLAKE_VERT, FLAKE_FRAG } from './ember-swarm.glsl.js'; |
| +добавлено | |
| +добавлено | // Ember Swarm (SPELLBOOK hybrid, fire + arcane). Thousands of homing embers, flocking on the GPU |
| +добавлено | // (the agent engine in plague-swarm.glsl.js). Reference: long-exposure forge sparks and fireflies, |
| +добавлено | // burning-paper embers. |
| +добавлено | // Anticipation: embers lift off the floor and out of the orb into a slowly folding murmuration. |
| +добавлено | // Release: the sheet unwinds, in order, into one living ribbon: flat, twisting, undulating, its |
| +добавлено | // line light sweeping the floor, and the airflow fans each ember hotter. |
| +добавлено | // Impact: the ribbon winds round the target into a dense orbiting shell of hot streaks; a share |
| +добавлено | // sticks to the dummy and burns; the rest shed one by one, cooling into dark flakes that fall. |
| +добавлено | // Aftermath: the shell bursts, everything cools by radiative law and drifts down as ash, leaving |
| +добавлено | // scorch and glowing specks. |
| +добавлено | // Every ember: its own blackbody temperature (T^4 brightness), flicker and curved long-exposure |
| +добавлено | // streak; the arcane is only magenta in the hottest cores. All their light lands on the floor |
| +добавлено | // through the swarm's floor light; three orbiting lights carry it to the dummy. |
| +добавлено | |
| +добавлено | const SIZES = { low: [64, 64], medium: [128, 64], high: [128, 128], ultra: [256, 128] }; |
| +добавлено | const CHARGE = 2.6, IMPACT = 1.6, SWOOP = 1.3, DELAY = 0.9, AFTER = 4.4; |
| +добавлено | const TAU = Math.PI * 2; |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _d = new THREE.Vector3(), _m4 = new THREE.Matrix4(), _col = new THREE.Color(); |
| +добавлено | const ease = (x) => (x <= 0 ? 0 : x >= 1 ? 1 : x * x * (3 - 2 * x)); |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'ember-swarm', name: 'Ember Swarm', element: 'fire', order: 20, hue: '#ff7a1e', |
| +добавлено | shape: 'area', |
| +добавлено | params: { |
| +добавлено | count: 1, size: 1, speed: 1, temperature: 1500, glow: 1, exposure: 1, arcane: 0.2, flock: 1, turbulence: 1, radius: 1.05, |
| +добавлено | hold: 2.4, shed: 0.3, cling: 0.1, haze: 1, smoke: 1, scorch: 1, |
| +добавлено | arcaneColor: '#ff2fb4', ash: '#2a2623', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | count: [0.1, 1, 0.01], size: [0.5, 2.5, 0.01], speed: [0.5, 2, 0.01], temperature: [1000, 3000, 10], glow: [0, 4, 0.01], exposure: [0.2, 3, 0.01], |
| +добавлено | arcane: [0, 1, 0.01], flock: [0, 2, 0.01], turbulence: [0, 3, 0.01], radius: [0.7, 2, 0.01], hold: [1, 6, 0.1], shed: [0, 0.7, 0.01], cling: [0, 0.4, 0.01], |
| +добавлено | haze: [0, 3, 0.01], smoke: [0, 3, 0.01], scorch: [0, 2, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | 'forge sparks': { temperature: 2200, exposure: 1.7, size: 0.8, speed: 1.35, turbulence: 0.7, arcane: 0.15, smoke: 0.5 }, |
| +добавлено | fireflies: { temperature: 1250, count: 0.45, exposure: 0.7, speed: 0.65, size: 1.2, glow: 1.8, haze: 0.2, smoke: 0.2, scorch: 0.3, arcane: 0 }, |
| +добавлено | 'witch fire': { temperature: 2300, arcane: 1, glow: 1.2, hold: 4, radius: 1.3, shed: 0.45, arcaneColor: '#e01fff' }, |
| +добавлено | }, |
| +добавлено | timing: { charge: CHARGE, impact: IMPACT, linger: 2.4 + AFTER }, |
| +добавлено | kinds: { |
| +добавлено | // scorched sacking: thin lit smoke, the shell's own light does the rest |
| +добавлено | smoke: { style: 'media', emissive: 0, drag: 1.6, buoyancy: 1.8, curl: 2.4, curlScale: 0.5, life: [1.4, 3.0], size: [0.12, 0.9], sizePow: 0.6, color: ['#2b2622', '#55504a'], alpha: [0.4, 0], density: 1.6, erosion: 1.3, fadeIn: 0.2, fadeOut: 0.5 }, |
| +добавлено | haze: { style: 'media', drag: 0.4, buoyancy: 1.2, forceResponse: 1.5, life: [0.8, 1.3], size: [0.7, 1.3], alpha: [1, 0], density: 0, distortion: 0.7, fadeIn: 0.25, fadeOut: 0.4 }, |
| +добавлено | // embers that strike the dummy spit a few fast sparks |
| +добавлено | spark: { style: 'glow', drag: 0.6, gravity: 9.8, restitution: 0.4, friction: 0.3, life: [0.2, 0.55], size: [0.012, 0.004], stretch: 0.03, emissive: 6, temp: [2300, 1100], paints: 'heat', paintAmount: 0.03 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const [W, H] = SIZES[fx.Q.tier] ?? SIZES.high; |
| +добавлено | |
| +добавлено | // --- the swarm: shared engine, ember behaviour and look ------------------------------------------ |
| +добавлено | const u = { |
| +добавлено | uLeader: { value: new THREE.Vector4() }, uLeaderV: { value: new THREE.Vector4() }, |
| +добавлено | uTarget: { value: new THREE.Vector4(0, 1, 2, 1) }, uMode: { value: new THREE.Vector4() }, |
| +добавлено | uFlock: { value: new THREE.Vector4() }, uMove: { value: new THREE.Vector4() }, |
| +добавлено | uSpawnP: { value: new THREE.Vector4() }, uSpawnF: { value: new THREE.Vector4() }, |
| +добавлено | uClock: { value: new THREE.Vector4(-1, 0.15, -1, 1) }, |
| +добавлено | uPathA: { value: new THREE.Vector4() }, uPathM: { value: new THREE.Vector4() }, uPathB: { value: new THREE.Vector4() }, |
| +добавлено | uStream: { value: new THREE.Vector4(DELAY, 0.7, 0, 0) }, |
| +добавлено | uDummyP: { value: new THREE.Vector4() }, uDummyR: { value: new THREE.Matrix3() }, |
| +добавлено | uHeat: { value: new THREE.Vector4(1550, 0.16, 1, 0) }, |
| +добавлено | uLook: { value: new THREE.Vector4(1, 1, 0.06, 0.5) }, uShape: { value: new THREE.Vector4(1, 0, 0, 0) }, |
| +добавлено | uArcane: { value: new THREE.Color() }, uAsh: { value: new THREE.Color() }, |
| +добавлено | }; |
| +добавлено | const swarm = new Swarm(fx, { W, H, sim: EMBER_SIM, look: EMBER_LOOK, uniforms: u, grid: true, light: { res: 64, box: 16, soft: 0.9 } }); |
| +добавлено | const nK = Math.sqrt(16384 / swarm.N); // fewer embers on low tiers: each carries more light |
| +добавлено | swarm.floorU.uFloor.value.set(0.05 * nK, 0.05, 0.6, 0); // a dense swarm lights the ground under it |
| +добавлено | fx.systems.push(swarm); |
| +добавлено | |
| +добавлено | // streaks: a strip per ember (4 path segments + 2 caps) |
| +добавлено | const xs = [-1, 0, 1 / 3, 2 / 3, 1, 2], pos = [], idx = []; |
| +добавлено | xs.forEach((x, i) => { pos.push(x, -1, 0, x, 1, 0); if (i) idx.push(2 * i - 2, 2 * i, 2 * i + 1, 2 * i - 2, 2 * i + 1, 2 * i - 1); }); |
| +добавлено | const strip = new THREE.InstancedBufferGeometry(); |
| +добавлено | strip.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3)); |
| +добавлено | strip.setIndex(idx); |
| +добавлено | strip.instanceCount = swarm.N; |
| +добавлено | const streaks = new THREE.Mesh(strip, swarm.material(STREAK_VERT(W), STREAK_FRAG, {}, LINES_BLENDING)); |
| +добавлено | streaks.frustumCulled = false; |
| +добавлено | streaks.renderOrder = 11; |
| +добавлено | streaks.layers.enable(1); |
| +добавлено | streaks.visible = false; |
| +добавлено | swarm.glow = streaks; // swarm.show() drives it |
| +добавлено | fx.scenes.lines.add(streaks, swarm.floor); |
| +добавлено | // flakes: dark solid scraps, so they read against the glow |
| +добавлено | const flakes = new THREE.Mesh(swarm.instancedQuad(), swarm.material(FLAKE_VERT(W), FLAKE_FRAG, {}, { alphaToCoverage: true })); |
| +добавлено | flakes.frustumCulled = false; |
| +добавлено | flakes.visible = false; |
| +добавлено | fx.scenes.opaque.add(flakes); |
| +добавлено | |
| +добавлено | const E = { |
| +добавлено | smoke: P.emitter('ember-swarm.smoke', { shape: 'line', radius: 0.2, spread: 0.3 }), |
| +добавлено | burst: P.emitter('ember-swarm.smoke', { shape: 'sphere', radius: 0.8, spread: 0.8, radial: 1.6 }), |
| +добавлено | haze: P.emitter('ember-swarm.haze', { shape: 'sphere', radius: 1, spread: 0.3 }), |
| +добавлено | spark: P.emitter('ember-swarm.spark', { shape: 'sphere', radius: 0.4, spread: 3, radial: 2 }), |
| +добавлено | }; |
| +добавлено | const SPLAT = { x: 0, z: 0, radius: 1, heat: 0, char: 0, glow: 0, pattern: 'noise', seed: 0 }; |
| +добавлено | const SHOCK = { pos: new THREE.Vector3(), radius0: 0.5, radius1: 5, duration: 0.45, thickness: 0.7, distortion: 0.03, intensity: 0, ground: false }; |
| +добавлено | |
| +добавлено | function syncColors() { |
| +добавлено | const p = S.params; |
| +добавлено | const a = u.uArcane.value.set(p.arcaneColor); |
| +добавлено | a.multiplyScalar(1 / Math.max(0.2126 * a.r + 0.7152 * a.g + 0.0722 * a.b, 1e-3)); |
| +добавлено | u.uAsh.value.set(p.ash); |
| +добавлено | } |
| +добавлено | const lightColor = (T, out) => blackbodyRGB(T, out); |
| +добавлено | |
| +добавлено | let owner = null; |
| +добавлено | const lead = new THREE.Vector3(), leadPrev = new THREE.Vector3(), centroid = new THREE.Vector3(); |
| +добавлено | const cloud0 = new THREE.Vector3(), pA = new THREE.Vector3(), mid = new THREE.Vector3(), tc = new THREE.Vector3(); |
| +добавлено | |
| +добавлено | function releaseAll(c) { |
| +добавлено | const d = c.data; |
| +добавлено | d.l0?.release(); d.l1?.release(); d.l2?.release(); d.vortex?.release(); d.shock?.release(); |
| +добавлено | d.l0 = d.l1 = d.l2 = d.vortex = d.shock = null; |
| +добавлено | } |
| +добавлено | function stop() { |
| +добавлено | owner = null; |
| +добавлено | swarm.active = false; |
| +добавлено | swarm.show(false); |
| +добавлено | flakes.visible = false; |
| +добавлено | swarm.reset(); |
| +добавлено | } |
| +добавлено | function splat(x, z, radius, heat, char, glow, seed) { |
| +добавлено | SPLAT.x = x; SPLAT.z = z; SPLAT.radius = radius; SPLAT.heat = heat; SPLAT.char = char; SPLAT.glow = glow; SPLAT.seed = seed; |
| +добавлено | fx.ground.splat(SPLAT); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the cloud's centre (CPU): up off the caster, wandering until the ribbon starts |
| +добавлено | function cloudAt(c, T, out) { |
| +добавлено | const t = Math.min(T, CHARGE), k = ease(t / 1.5); |
| +добавлено | out.copy(c.from).lerp(cloud0, k); |
| +добавлено | out.x += Math.sin(t * 1.1 + c.data.ph) * 0.5 * k; |
| +добавлено | out.y += Math.sin(t * 0.8) * 0.25 * k; |
| +добавлено | return out; |
| +добавлено | } |
| +добавлено | // the ribbon's centreline (mirrors pathAt in the sim) |
| +добавлено | function pathAt(s, out) { |
| +добавлено | s = Math.min(1, Math.max(0, s)); |
| +добавлено | const k = s * s * (3 - 2 * s) * 0.6 + s * s * 0.4, i = 1 - k; |
| +добавлено | return out.set(i * i * pA.x + 2 * i * k * mid.x + k * k * tc.x, i * i * pA.y + 2 * i * k * mid.y + k * k * tc.y, i * i * pA.z + 2 * i * k * mid.z + k * k * tc.z); |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'arcaneColor' || k === 'ash') syncColors(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = c.data; |
| +добавлено | if (name === 'charge') { |
| +добавлено | syncColors(); |
| +добавлено | if (owner && owner !== c) releaseAll(owner); |
| +добавлено | if (!owner || owner.data.dispersed) swarm.reset(); |
| +добавлено | owner = c; |
| +добавлено | swarm.active = true; |
| +добавлено | swarm.show(true); |
| +добавлено | flakes.visible = true; |
| +добавлено | d.ph = (c.seed % 100) * 0.0628; |
| +добавлено | d.side = c.seed % 2 ? 1 : -1; |
| +добавлено | d.hold = p.hold; |
| +добавлено | c.durations.linger = p.hold + AFTER; |
| +добавлено | d.nextSplat = 0; d.nextHit = 0; d.arrived = false; d.closed = false; d.dispersed = false; d.tShock = 0; |
| +добавлено | d.l0 = fx.lights.acquire(); |
| +добавлено | if (d.l0) { d.l0.range = 8; d.l0.intensity = 0; d.l0.shadow = true; } |
| +добавлено | d.l1 = d.l2 = d.vortex = d.shock = null; |
| +добавлено | // the sheet forms over the caster, leaning at the target; the ribbon bends out to one side |
| +добавлено | _a.subVectors(c.to, c.from).setY(0); |
| +добавлено | if (_a.lengthSq() < 1e-4) _a.set(0, 0, -1); |
| +добавлено | _a.normalize(); |
| +добавлено | cloud0.copy(c.from).addScaledVector(_a, 3.6); cloud0.y += 2.1; |
| +добавлено | tc.set(c.to.x, c.target >= 0 ? 1.0 : Math.max(c.to.y, 0.9), c.to.z); |
| +добавлено | mid.copy(cloud0).lerp(tc, 0.5); |
| +добавлено | mid.x += -_a.z * d.side * 5; mid.z += _a.x * d.side * 5; mid.y += 1.2; |
| +добавлено | const r2 = Math.hypot(mid.x, mid.z), lim = fx.ARENA.radius - 2; |
| +добавлено | if (r2 > lim) { mid.x *= lim / r2; mid.z *= lim / r2; } |
| +добавлено | cloudAt(c, 0, lead); leadPrev.copy(lead); centroid.copy(lead); |
| +добавлено | cloudAt(c, CHARGE, pA); |
| +добавлено | swarm.simU.uSpawnFrac.value = 0; |
| +добавлено | // the floor starts to smoulder where they will lift from |
| +добавлено | splat(c.from.x, c.from.z, 3.4, 0.08, 0.1, 0.5, c.seed % 97); |
| +добавлено | } else if (name === 'done') { |
| +добавлено | releaseAll(c); |
| +добавлено | fx.arena.setFocus(fx.arena.focus.color, 2.5); |
| +добавлено | if (c === owner) stop(); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | if (c !== owner) { releaseAll(c); return; } |
| +добавлено | const p = S.params, d = c.data, T = c.age; |
| +добавлено | const swoop = SWOOP / Math.sqrt(p.speed), tEnv = CHARGE + swoop, tDis = CHARGE + IMPACT + d.hold, tEnd = tDis + AFTER; |
| +добавлено | const dm = c.target >= 0 ? fx.dummies[c.target] : null; |
| +добавлено | const R = p.radius * (0.8 + 0.2 * Math.cbrt(p.count)); |
| +добавлено | |
| +добавлено | // --- leader, cloud, phase weights --- |
| +добавлено | leadPrev.copy(lead); |
| +добавлено | cloudAt(c, T, lead); |
| +добавлено | const lv = _b.subVectors(lead, leadPrev).multiplyScalar(dt > 0 ? 1 / dt : 0); |
| +добавлено | const env = ease((T - (tEnv - 0.2)) / (DELAY + 0.4)); // the share of the swarm at the target |
| +добавлено | const dis = ease((T - tDis) / 0.5); |
| +добавлено | const shut = ease((T - tDis - 0.1) / 0.3); // the shell's curvature lets go |
| +добавлено | const cloudR = 0.7 + 2.5 * ease(T / 1.8); |
| +добавлено | u.uLeader.value.set(lead.x, lead.y, lead.z, cloudR * Math.cbrt(p.count)); |
| +добавлено | u.uLeaderV.value.set(lv.x, lv.y, lv.z, 0); |
| +добавлено | u.uPathA.value.set(pA.x, pA.y, pA.z, swoop); |
| +добавлено | u.uPathM.value.set(mid.x, mid.y, mid.z, T); |
| +добавлено | u.uPathB.value.set(tc.x, tc.y, tc.z, CHARGE); |
| +добавлено | u.uStream.value.set(DELAY, 0.8 * Math.cbrt(p.count), 0, 0); |
| +добавлено | u.uTarget.value.set(tc.x, tc.y, tc.z, R); |
| +добавлено | u.uMode.value.set(T > CHARGE ? 1 : 0, dis, dm ? p.cling * ease((T - tEnv) / 1.2) : 0, p.shed); |
| +добавлено | u.uFlock.value.set(1.5 * p.flock, 6 * p.flock, 2.2 * p.flock, p.turbulence); |
| +добавлено | const cruise = T < CHARGE ? 3.4 : 6.5; |
| +добавлено | u.uMove.value.set(cruise * p.speed, 7 * p.speed, 2.8, (T < CHARGE ? 80 : 30) * swarm.N / 16384); |
| +добавлено | u.uSpawnP.value.set(c.from.x, c.from.y, c.from.z, 0.22); |
| +добавлено | u.uSpawnF.value.set(c.from.x, c.from.z, 3.4, 0.62); |
| +добавлено | u.uClock.value.x = T > tDis ? T - tDis : -1; |
| +добавлено | u.uClock.value.z = T > tEnv + DELAY ? T - tEnv - DELAY : -1; |
| +добавлено | u.uClock.value.w = d.hold + IMPACT - swoop - DELAY; |
| +добавлено | // they start dull and are fanned as they gather; the shell runs hottest |
| +добавлено | const heat = 0.94 + 0.06 * ease(T / CHARGE) + 0.02 * Math.sin(T * 3.1) + 0.06 * ease((T - tEnv) / 1) * (1 - dis); |
| +добавлено | u.uHeat.value.set(p.temperature, 0.16, heat, 0); |
| +добавлено | u.uLook.value.set(p.size, 6.5 * p.glow * nK, 0.085 * p.exposure * (0.5 + 0.5 * ease((T - CHARGE + 0.3) / 0.5)), p.arcane); |
| +добавлено | u.uShape.value.set(1 - ease((T - tEnd + 0.9) / 0.9), env * (1 - shut), T < CHARGE ? 2.6 : 0.25 + 0.75 * env, 0.35); |
| +добавлено | swarm.simU.uSpawnFrac.value = p.count * ease(T / 1.0); |
| +добавлено | if (dm) { |
| +добавлено | u.uDummyP.value.set(dm.pos.x, 0, dm.pos.z, dis > 0.05 ? 0 : 1); |
| +добавлено | u.uDummyR.value.setFromMatrix4(_m4.makeRotationFromQuaternion(dm.group.quaternion)); |
| +добавлено | } else u.uDummyP.value.w = 0; |
| +добавлено | |
| +добавлено | // where the swarm is: the grid and the floor light follow it |
| +добавлено | const sMid = (T - DELAY * 0.5 - CHARGE) / swoop; |
| +добавлено | if (T < CHARGE) _d.copy(lead); else if (sMid < 1) pathAt(sMid, _d); else _d.copy(tc); |
| +добавлено | centroid.lerp(_d, 1 - Math.exp(-dt * 4)); |
| +добавлено | const gs = T < CHARGE ? Math.min(14, 5 + cloudR * 3) : 16 - 9.5 * env, gb = swarm.gridBox; |
| +добавлено | gb.set(centroid.x - gs / 2, Math.max(centroid.y - gs * 0.42, -0.3), centroid.z - gs / 2, gs / 32); |
| +добавлено | const lb = swarm.lightBox, snap = lb.z / 64; |
| +добавлено | lb.x = Math.round((centroid.x - lb.z / 2) / snap) * snap; |
| +добавлено | lb.y = Math.round((centroid.z - lb.z / 2) / snap) * snap; |
| +добавлено | swarm.show(true); |
| +добавлено | |
| +добавлено | // the orb feeds them, then settles |
| +добавлено | if (T < CHARGE + 0.7) fx.arena.setFocus(fx.arena.focus.color, 2.5 + 3 * ease(T / CHARGE) * (1 - ease((T - CHARGE) / 0.7))); |
| +добавлено | |
| +добавлено | // --- light: the murmuration's glow, the ribbon as a line light, three lights on the shell --- |
| +добавлено | const alive = Math.min(1, swarm.simU.uSpawnFrac.value / Math.max(p.count, 1e-3)) * (1 - ease((T - tDis - 0.15) / 1.3)); |
| +добавлено | const base = 12 * p.glow * Math.sqrt(p.count) * alive * (0.85 + 0.15 * Math.sin(T * 13.7 + d.ph)); |
| +добавлено | const K = p.temperature * heat * 0.95, burst = 1 + 1.5 * Math.max(0, 1 - Math.abs(T - tDis - 0.12) / 0.25); |
| +добавлено | if (d.l0) { |
| +добавлено | const L = d.l0; |
| +добавлено | if (T < CHARGE) { L.type = 0; L.pos.copy(centroid); L.range = 7 + cloudR; L.intensity = base * 0.9; } |
| +добавлено | else if (env < 0.5) { |
| +добавлено | L.type = 1; L.range = 9; L.intensity = base * 1.1; |
| +добавлено | pathAt((T - CHARGE) / swoop, L.pos); pathAt((T - CHARGE - DELAY) / swoop, L.end); |
| +добавлено | } else { |
| +добавлено | const a = T * 2.7 + d.ph; |
| +добавлено | L.type = 0; L.range = 8; L.intensity = base * 0.75 * burst; |
| +добавлено | L.pos.set(tc.x + Math.cos(a) * R * 0.75, tc.y + 0.5 + 0.3 * Math.sin(T * 1.3), tc.z + Math.sin(a) * R * 0.75); |
| +добавлено | } |
| +добавлено | lightColor(K, L.color); |
| +добавлено | } |
| +добавлено | if (env > 0.5 && !d.l1 && !d.dispersed) { |
| +добавлено | d.l1 = fx.lights.acquire(); |
| +добавлено | if (d.l1) { d.l1.range = 8; d.l1.shadow = true; } |
| +добавлено | d.l2 = fx.lights.acquire(); |
| +добавлено | if (d.l2) d.l2.range = 6; |
| +добавлено | } |
| +добавлено | if (d.l1) { |
| +добавлено | const a = T * 2.7 + d.ph + 2.09; |
| +добавлено | d.l1.pos.set(tc.x + Math.cos(a) * R * 0.75, tc.y + 0.1, tc.z + Math.sin(a) * R * 0.75); |
| +добавлено | d.l1.intensity = base * 0.75 * env * burst; |
| +добавлено | lightColor(K, d.l1.color); |
| +добавлено | } |
| +добавлено | if (d.l2) { |
| +добавлено | const a = T * 2.7 + d.ph + 4.19; |
| +добавлено | d.l2.pos.set(tc.x + Math.cos(a) * R * 0.75, tc.y - 0.5, tc.z + Math.sin(a) * R * 0.75); |
| +добавлено | d.l2.intensity = base * 0.5 * env * burst; |
| +добавлено | lightColor(K * 1.1, d.l2.color); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- arrival: the shell closes, the scorching starts --- |
| +добавлено | if (!d.arrived && T > tEnv - 0.1) { |
| +добавлено | d.arrived = true; |
| +добавлено | fx.rig.shake(0.1, 22); |
| +добавлено | fx.impacts.add('scorch', tc, 1.5 * R, 0.8 * p.scorch, d.hold + AFTER, lightColor(K, _col)); |
| +добавлено | d.vortex = fx.forces.acquire(); |
| +добавлено | if (d.vortex) { |
| +добавлено | const v = d.vortex; |
| +добавлено | v.type = 1; v.pos.set(tc.x, 0, tc.z); v.dir.set(0, 1, 0); v.w = 3; |
| +добавлено | v.p[0] = 0.9 * R; v.p[1] = 5 * p.speed; v.p[2] = 0.7; v.p[3] = 0.2; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | const burning = env > 0.5 && dis < 0.5; |
| +добавлено | if (burning) { |
| +добавлено | const k = Math.min(1, (T - tEnv) / d.hold); |
| +добавлено | d.nextSplat -= dt; |
| +добавлено | if (d.nextSplat <= 0) { |
| +добавлено | d.nextSplat = 0.3; |
| +добавлено | splat(tc.x, tc.z, (0.8 + 0.7 * k) * R, 0.1 * p.scorch, 0.14 * p.scorch, 0.2 * p.scorch, (c.seed + T * 31) % 97); |
| +добавлено | } |
| +добавлено | if (dm) { |
| +добавлено | dm.status.burn = Math.max(dm.status.burn, Math.min(1, 0.35 + 0.65 * k) * p.scorch); |
| +добавлено | d.nextHit -= dt; |
| +добавлено | if (d.nextHit <= 0) { |
| +добавлено | d.nextHit = 0.2 + 0.3 * fx.rand(); |
| +добавлено | const a = fx.rand() * TAU; |
| +добавлено | dm.hit(_a.set(Math.cos(a), 0, Math.sin(a)), 0.02 + 0.03 * fx.rand()); |
| +добавлено | } |
| +добавлено | E.smoke.pos.set(dm.pos.x, 0.7, dm.pos.z); E.smoke.p.set(dm.pos.x, 1.6, dm.pos.z); E.smoke.vel.set(0, 0.5, 0); E.smoke.rate(26 * p.smoke * (0.3 + 0.7 * k), dt); |
| +добавлено | E.spark.pos.set(dm.pos.x, 1.1, dm.pos.z); E.spark.vel.set(0, 0.6, 0); E.spark.rate(55 * k, dt); |
| +добавлено | } |
| +добавлено | E.haze.pos.copy(tc); E.haze.radius = R * 1.2; E.haze.vel.set(0, 1.0, 0); E.haze.size = R; E.haze.rate(30 * p.haze, dt); |
| +добавлено | } |
| +добавлено | if (dm && T > tDis) dm.status.burn = Math.max(dm.status.burn, 0.85 * p.scorch); // it stays charred to the end |
| +добавлено | if (dm && T > tDis && T < tDis + 3) { // and keeps smoking |
| +добавлено | E.smoke.pos.set(dm.pos.x, 0.8, dm.pos.z); E.smoke.p.set(dm.pos.x, 1.7, dm.pos.z); E.smoke.vel.set(0, 0.4, 0); E.smoke.rate(18 * p.smoke * (1 - (T - tDis) / 3), dt); |
| +добавлено | } |
| +добавлено | if (d.vortex) d.vortex.p[1] = 5 * p.speed * env * (1 - dis); |
| +добавлено | |
| +добавлено | // --- the burst --- |
| +добавлено | if (dis > 0.02 && !d.dispersed) { |
| +добавлено | d.dispersed = true; |
| +добавлено | d.vortex?.release(); d.vortex = null; |
| +добавлено | fx.flash(0.035); |
| +добавлено | fx.rig.shake(0.22, 14); |
| +добавлено | SHOCK.pos.copy(tc); SHOCK.radius1 = 4.5 * R; SHOCK.distortion = 0.015 * p.haze; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | d.shock = fx.forces.acquire(); |
| +добавлено | if (d.shock) { const s = d.shock; s.type = 4; s.pos.copy(tc); s.p[0] = 0.3; s.p[1] = 1.2; s.p[2] = 40; d.tShock = 0; } |
| +добавлено | E.burst.pos.copy(tc); E.burst.vel.set(0, 0.7, 0); E.burst.burst(50 * p.smoke); |
| +добавлено | // char lasts on the dummy; the floor mark is ragged lobes, not a disc |
| +добавлено | fx.impacts.add('scorch', tc, 1.8 * R, p.scorch, AFTER + 4, lightColor(K, _col)); |
| +добавлено | splat(tc.x, tc.z, 1.5 * R, 0.12 * p.scorch, 0.7 * p.scorch, 0.35 * p.scorch, c.seed % 89); |
| +добавлено | for (let i = 0; i < 4; i++) { |
| +добавлено | const a = fx.rand() * TAU, r = (0.7 + 0.9 * fx.rand()) * R; |
| +добавлено | splat(tc.x + Math.cos(a) * r, tc.z + Math.sin(a) * r, (0.5 + 0.6 * fx.rand()) * R, 0.05 * p.scorch, 0.5 * p.scorch, 0.15 * p.scorch, (c.seed + i * 13) % 89); |
| +добавлено | } |
| +добавлено | if (dm) { dm.status.burn = Math.max(dm.status.burn, p.scorch); dm.hit(_a.set(c.seed % 3 - 1, 0, 1).normalize(), 0.5); } |
| +добавлено | } |
| +добавлено | if (d.shock) { |
| +добавлено | d.tShock += dt; |
| +добавлено | const a = d.tShock; |
| +добавлено | if (a > 0.5) { d.shock.release(); d.shock = null; } |
| +добавлено | else { d.shock.p[0] = 0.3 + 9 * a; d.shock.p[2] = 40 * (1 - a / 0.5); } |
| +добавлено | } |
| +добавлено | if (T > tDis && alive <= 0.01) { d.l0?.release(); d.l1?.release(); d.l2?.release(); d.l0 = d.l1 = d.l2 = null; } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import { NOISE_GLSL } from '../../vfx/common.js'; |
| +добавлено | |
| +добавлено | // Phoenix shaders (phoenix.js). Two AnalyticRibbons path/shade pairs: |
| +добавлено | // BIRD every ribbon of a bird (body, wing edges, wing feathers, tail streamers, crest) is posed in |
| +добавлено | // the bird's local frame (x right, y up, z forward; metres = local units x scale) from the |
| +добавлено | // per-bird uniform slots, so the CPU writes 5 vec4s per bird per frame and nothing per ribbon. |
| +добавлено | // A flat wing is folded (spread), swept back (dive) and flapped (a rotation about the body |
| +добавлено | // axis that grows and lags towards the tip and the trailing edge), then mirrored for the |
| +добавлено | // left wing. phoenix.js mirrors armFlat/foldW/flapW to place emitters on the wing. |
| +добавлено | // FEATHER stateless burning feathers: ballistic with drag, settling at a terminal speed with a |
| +добавлено | // flutter, tumbling about a random axis, and burning from the tip down. |
| +добавлено | // Both layers share the GLSL: RIBBON_FX (half-res glow + heat haze) vs the full-res core lines. |
| +добавлено | |
| +добавлено | const BIRD_HEAD = /* glsl */` |
| +добавлено | uniform vec4 uBirdA[4]; // pos xyz, scale (m per local unit) |
| +добавлено | uniform vec4 uBirdF[4]; // forward xyz, flap phase (rad) |
| +добавлено | uniform vec4 uBirdU[4]; // up xyz, flap amplitude (rad) |
| +добавлено | uniform vec4 uBirdW[4]; // spread 0..1, sweep 0..1, dihedral (rad), - |
| +добавлено | uniform vec4 uBirdT[4]; // tail bend x, y (local, per unit length), -, - |
| +добавлено | const float SX = 0.12; // shoulder offset from the spine |
| +добавлено | vec3 armFlat(float s, float k) { |
| +добавлено | return vec3(SX + 1.4 * s * mix(0.3, 1.0, k), 0.02 * s, 0.1 + 0.2 * sin(3.14159265 * min(s * 1.15, 1.0)) - 0.45 * s * s * s); |
| +добавлено | } |
| +добавлено | // fold back in the wing plane about the shoulder (beta > 0 moves the tip backwards) |
| +добавлено | vec3 foldW(vec3 p, float beta) { |
| +добавлено | float dx = p.x - SX, dz = p.z - 0.1, c = cos(beta), s = sin(beta); |
| +добавлено | return vec3(SX + dx * c + dz * s, p.y, 0.1 - dx * s + dz * c); |
| +добавлено | } |
| +добавлено | // flap about the body axis through the shoulder; amplitude grows to the tip, phase lags along the |
| +добавлено | // span and behind the leading edge (lag = metres behind it), so the wing whips instead of hinging |
| +добавлено | vec3 flapW(vec3 p, float lag, float ph, float amp, float dih) { |
| +добавлено | float dx = p.x - SX, se = clamp(dx / 1.7, 0.0, 1.0); |
| +добавлено | float x = ph - 1.1 * se - 1.6 * lag; |
| +добавлено | float th = dih * (0.6 + 0.4 * se) + amp * (sin(x) + 0.22 * sin(2.0 * x)) * (0.45 + 0.75 * se); |
| +добавлено | float c = cos(th), s = sin(th); |
| +добавлено | return vec3(SX + dx * c - p.y * s, dx * s + p.y * c, p.z); |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | // p0 = (mode, slot, side, -). mode 0 body, 1 wing leading edge, 2 wing feather, 3 tail, 4 crest. |
| +добавлено | // p1 = (span s | lateral, length, width, splay angle | phase), p2 = (seed, heat, -, -) |
| +добавлено | export const BIRD_PATH = BIRD_HEAD + /* glsl */` |
| +добавлено | vec3 ribbonPath(float t, vec4 p0, vec4 p1, vec4 p2, vec4 p3, out float width) { |
| +добавлено | int mode = int(p0.x + 0.5), b = int(p0.y + 0.5); |
| +добавлено | vec4 A = uBirdA[b], F = uBirdF[b], Uu = uBirdU[b], W = uBirdW[b], T = uBirdT[b]; |
| +добавлено | float k = W.x, beta = (1.0 - k) * 1.3 + W.y * 0.6; |
| +добавлено | float bob = 0.035 * sin(F.w + 1.7); // the body rises on the downstroke |
| +добавлено | vec3 q; |
| +добавлено | if (mode == 0) { |
| +добавлено | q = vec3(0.0, 0.2 * smoothstep(0.5, 0.82, t) - 0.06 * smoothstep(0.84, 1.0, t) - 0.04 * exp(-pow((t - 0.35) / 0.2, 2.0)) + bob * smoothstep(0.4, 1.0, t), -0.5 + 1.34 * t); |
| +добавлено | width = (0.09 * (1.0 - t) + 0.26 * exp(-pow((t - 0.36) / 0.17, 2.0)) + 0.19 * exp(-pow((t - 0.8) / 0.075, 2.0))) * (1.0 - smoothstep(0.86, 1.0, t)); |
| +добавлено | } else if (mode <= 2) { |
| +добавлено | vec3 f; |
| +добавлено | float lag = 0.0; |
| +добавлено | if (mode == 1) { |
| +добавлено | f = armFlat(t, k); |
| +добавлено | width = p1.z * (1.0 - 0.6 * t); |
| +добавлено | } else { |
| +добавлено | float L = p1.y * mix(0.55, 1.0, k); |
| +добавлено | float an = p1.w * mix(0.15, 1.0, k) + 0.18 * t; |
| +добавлено | f = armFlat(p1.x, k) + vec3(0.0, -0.01, -0.02) + vec3(sin(an), -0.05 * t, -cos(an)) * (L * t); |
| +добавлено | lag = L * t; |
| +добавлено | width = p1.z * smoothstep(0.0, 0.2, t) * (1.0 - 0.75 * t * t); |
| +добавлено | } |
| +добавлено | q = flapW(foldW(f, beta), lag, F.w, Uu.w, W.z); |
| +добавлено | q.y += bob * 0.3; |
| +добавлено | q.x *= p0.z; |
| +добавлено | } else if (mode == 3) { |
| +добавлено | float L = p1.y, ph = p1.w + uTime * 5.0; |
| +добавлено | q = vec3(p1.x * t * 1.3, -0.05 * t, -0.45 - L * t); |
| +добавлено | q.x += sin(4.5 * t - ph) * 0.16 * t * t * (1.0 + abs(p1.x) * 4.0); |
| +добавлено | q.y += cos(3.5 * t - ph * 0.8) * 0.12 * t * t; |
| +добавлено | q.xy += T.xy * (t * t * L); |
| +добавлено | width = p1.z * (0.45 * (1.0 - t) + 1.25 * exp(-pow((t - 0.8) / 0.12, 2.0))) * (1.0 - smoothstep(0.92, 1.0, t)); |
| +добавлено | } else { |
| +добавлено | float ph = p1.w + uTime * 9.0; |
| +добавлено | q = vec3(0.0, 0.21 + bob, 0.56) + vec3(p1.x * t, 0.07 * t + 0.14 * t * t + 0.035 * sin(t * 6.0 - ph) * t, -p1.y * t); |
| +добавлено | width = p1.z * pow(1.0 - t, 0.8) * smoothstep(0.0, 0.12, t); |
| +добавлено | } |
| +добавлено | #ifdef RIBBON_FX |
| +добавлено | width *= 1.6 * A.w; |
| +добавлено | #else |
| +добавлено | width *= 0.12 * A.w; |
| +добавлено | #endif |
| +добавлено | vec3 Fw = F.xyz, Uw = Uu.xyz, Rw = cross(Uw, Fw); |
| +добавлено | return A.xyz + (Rw * q.x + Uw * q.y + Fw * q.z) * A.w; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const RAMP = NOISE_GLSL + /* glsl */` |
| +добавлено | uniform vec3 uHot; |
| +добавлено | uniform vec3 uMid; |
| +добавлено | uniform vec3 uCool; |
| +добавлено | uniform float uHaze; |
| +добавлено | vec3 fireRamp(float h) { return h < 0.5 ? mix(uCool, uMid, h * 2.0) : mix(uMid, uHot, h * 2.0 - 1.0); } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | export const BIRD_SHADE = RAMP + /* glsl */` |
| +добавлено | vec3 ribbonShade(RibbonFrag r, out vec3 media, out float alpha, out float distortion) { |
| +добавлено | int mode = int(r.p0.x + 0.5); |
| +добавлено | float u = r.uv.x, v = r.uv.y, av = abs(v), seed = r.p2.x; |
| +добавлено | float h; |
| +добавлено | if (mode == 0) h = 0.97 - 0.3 * smoothstep(0.85, 1.0, u); |
| +добавлено | else if (mode == 1) h = 0.95 - 0.3 * u; |
| +добавлено | else if (mode == 2) h = mix(0.78, 0.12, pow(u, 0.8)) * r.p2.y; |
| +добавлено | else if (mode == 3) h = mix(0.75, 0.1, u) + 0.35 * exp(-pow((u - 0.8) / 0.08, 2.0)); |
| +добавлено | else h = 0.8 - 0.65 * u; |
| +добавлено | // flame streaming from root to tip |
| +добавлено | float n = noise3(vec3(u * 0.45 - uTime * 0.42 + seed, av * 0.06 + seed * 0.37, uTime * 0.03)); |
| +добавлено | float n2 = noise3(vec3(u * 1.1 - uTime * 0.9 + seed * 1.7, v * 0.09, 0.5 + seed)); |
| +добавлено | float fl = n * 0.6 + n2 * 0.4; |
| +добавлено | media = vec3(0.0); alpha = 0.0; distortion = 0.0; |
| +добавлено | #ifdef RIBBON_FX |
| +добавлено | float edge = smoothstep(0.0, 0.45, (1.0 - av) - (fl - 0.45) * 1.2); |
| +добавлено | float g = edge * (0.45 + 1.1 * fl) * exp(-1.4 * v * v); |
| +добавлено | h = clamp(h + (fl - 0.5) * 0.45 - av * 0.45, 0.0, 1.0); |
| +добавлено | distortion = uHaze * v * exp(-3.0 * v * v) * 2.33; |
| +добавлено | // flame is far brighter than the sky behind it: let it cover the background instead of only |
| +добавлено | // adding to it (pure additive orange over blue sky reads pink), with sooty cool fringes |
| +добавлено | media = vec3(0.05, 0.032, 0.02); |
| +добавлено | alpha = min(g * 0.42, 0.7); |
| +добавлено | return fireRamp(h) * r.color * g; |
| +добавлено | #else |
| +добавлено | float core = exp(-5.0 * v * v) * (0.55 + 0.7 * fl); |
| +добавлено | return fireRamp(clamp(h + 0.1, 0.0, 1.0)) * r.color * core; |
| +добавлено | #endif |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // p0 = (origin xyz, birth time), p1 = (initial velocity xyz, life), p2 = (terminal fall m/s, |
| +добавлено | // tumble rad, -, drag 1/s), p3 = (length m, width m, seed, heat 0..1) |
| +добавлено | export const FEATHER_PATH = /* glsl */` |
| +добавлено | vec3 rotAxis(vec3 v, vec3 k, float a) { float c = cos(a), s = sin(a); return v * c + cross(k, v) * s + k * dot(k, v) * (1.0 - c); } |
| +добавлено | vec3 ribbonPath(float t, vec4 p0, vec4 p1, vec4 p2, vec4 p3, out float width) { |
| +добавлено | float age = mod(uTime - p0.w + 3600.0, 3600.0), kd = p2.w; |
| +добавлено | float e = (1.0 - exp(-kd * age)) / kd; |
| +добавлено | float s = p3.z; |
| +добавлено | vec3 c = p0.xyz + p1.xyz * e; |
| +добавлено | c.y -= p2.x * (age - e); |
| +добавлено | float fl = 1.0 - exp(-age * 1.5); |
| +добавлено | c.x += sin(age * 3.1 + s * 7.0) * 0.25 * fl; |
| +добавлено | c.z += cos(age * 2.7 + s * 5.0) * 0.25 * fl; |
| +добавлено | c.y = max(c.y, 0.06); |
| +добавлено | vec3 sp = normalize(hash31(s * 17.0 + 3.1) - 0.5 + 1e-3); |
| +добавлено | vec3 ax = rotAxis(normalize(p1.xyz + vec3(0.0, 1e-3, 0.0)), sp, p2.y * e + 0.7 * sin(age * 2.3 + s * 3.0) * fl); |
| +добавлено | vec3 bend = normalize(cross(ax, sp) + 1e-4); |
| +добавлено | float L = p3.x; |
| +добавлено | width = p3.y * smoothstep(0.0, 0.25, t) * (1.0 - t * t * t); |
| +добавлено | #ifdef RIBBON_FX |
| +добавлено | width *= 1.25; |
| +добавлено | #else |
| +добавлено | width *= 0.12; |
| +добавлено | #endif |
| +добавлено | return c + ax * ((t - 0.55) * L) + bend * ((t - 0.5) * (t - 0.5) * L * 0.35); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const FEATHER_SHADE = RAMP + /* glsl */` |
| +добавлено | vec3 ribbonShade(RibbonFrag r, out vec3 media, out float alpha, out float distortion) { |
| +добавлено | float age = mod(uTime - r.p0.w + 3600.0, 3600.0), life = r.p1.w, b = clamp(age / life, 0.0, 1.0); |
| +добавлено | float u = r.uv.x, v = r.uv.y, av = abs(v); |
| +добавлено | float n = noise3(vec3(u * 0.35 + r.p3.z, v * 0.05, age * 0.08)); |
| +добавлено | // burns from the tip down to the quill, with a glowing ember front |
| +добавлено | float front = 1.08 - 1.3 * b + (n - 0.5) * 0.35; |
| +добавлено | float vis = smoothstep(front + 0.03, front - 0.08, u) * smoothstep(0.0, 0.06, age); |
| +добавлено | float ember = exp(-pow((u - front) / 0.05, 2.0)) * smoothstep(0.0, 0.15, b) * (1.0 - smoothstep(0.85, 1.0, b)); |
| +добавлено | float heat = clamp(r.p3.w * (0.62 - 0.4 * b) + (n - 0.5) * 0.3, 0.0, 1.0); |
| +добавлено | media = vec3(0.0); alpha = 0.0; distortion = 0.0; |
| +добавлено | #ifdef RIBBON_FX |
| +добавлено | // vane with diagonal barbs either side of the rachis |
| +добавлено | float barbs = 0.62 + 0.38 * sin((u * 9.0 - av * 1.6) * 6.2831853 + r.p3.z * 3.0); |
| +добавлено | float vane = exp(-1.6 * v * v) * mix(1.0, barbs, smoothstep(0.08, 0.3, av)); |
| +добавлено | // burnt-out barbs behind the front: a brief curl of black char that crumbles away |
| +добавлено | float charB = smoothstep(front - 0.02, front + 0.04, u) * (1.0 - smoothstep(front + 0.06, front + 0.3 + 0.2 * n, u)); |
| +добавлено | float flake = step(0.38, noise3(vec3(u * 3.0 + r.p3.z, v * 0.25, 0.0))); |
| +добавлено | media = vec3(0.035, 0.028, 0.024); |
| +добавлено | alpha = (vane * vis * 0.4 + vane * charB * flake * 0.75) * smoothstep(0.0, 0.06, age); |
| +добавлено | distortion = uHaze * 0.5 * v * exp(-3.0 * v * v) * 2.33 * vis; |
| +добавлено | return (fireRamp(heat) * vane * vis * 0.8 + uHot * ember * 0.9) * r.color; |
| +добавлено | #else |
| +добавлено | return (fireRamp(clamp(heat + 0.2, 0.0, 1.0)) * vis + uHot * ember * 2.0) * r.color * exp(-4.0 * v * v); |
| +добавлено | #endif |
| +добавлено | }`; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { blackbodyRGB } from '../../vfx/noise.js'; |
| +добавлено | import { BIRD_PATH, BIRD_SHADE, FEATHER_PATH, FEATHER_SHADE } from './phoenix.glsl.js'; |
| +добавлено | |
| +добавлено | // Phoenix (SPELLBOOK, Fire hero). Anticipation: embers spiral into the focus where a firebird forms |
| +добавлено | // with its wings wrapped round it. Release: the wings snap open and it launches up and out on an |
| +добавлено | // arcing Bezier, beating hard; it glides wide-winged over the apex and stoops into the target with |
| +добавлено | // its wings raised in a V. The bird is ~29 analytic ribbons per layer posed from 5 uniforms per bird |
| +добавлено | // (half-res flame glow with heat haze + full-res cores), with media flames shed off the trailing |
| +добавлено | // edges, wingtip wisps, a hot path trail and burning feathers that peel off and flutter down. |
| +добавлено | // Impact: the bird bursts apart into a storm of burning feathers (stateless ribbons that tumble, |
| +добавлено | // settle and burn from the tip down), a flame blast, sparks, a pressure shell that shoves loose |
| +добавлено | // particles, a shock ring, and its spread-wing silhouette scorched into the floor (hot enough to |
| +добавлено | // grow ground flames). Aftermath: smoke, light dying with the flames, and the ashes stir: a spiral |
| +добавлено | // of embers rises from the centre. |
| +добавлено | |
| +добавлено | const NF = 9; // flight feathers per wing |
| +добавлено | const NCV = 6; // coverts per wing (short, broad, hot: they fill the inner wing) |
| +добавлено | const NT = 5; // tail streamers |
| +добавлено | const NCR = 3; // crest plumes |
| +добавлено | const SLOTS = 4; // birds alive at once (bird uniform slots) |
| +добавлено | const NFB = 128; // burning-feather ribbons (per layer) |
| +добавлено | const TAU = Math.PI * 2; |
| +добавлено | const SX = 0.12; |
| +добавлено | |
| +добавлено | // static ribbon table for one bird: [mode, side, p1 (s|lateral, length, width, splay|phase), seed, heat] |
| +добавлено | // FEAT: the flight feathers' (s, length, splay), mirrored by featherPoint() |
| +добавлено | const RIBS = [], FEAT = []; |
| +добавлено | const ss = THREE.MathUtils.smoothstep; |
| +добавлено | for (let k = 0; k < NF; k++) { |
| +добавлено | const s = (k + 0.5) / NF; |
| +добавлено | FEAT.push([s, 0.6 + 0.5 * ss(s, 0.3, 0.85) - 0.12 * ss(s, 0.9, 1), 0.06 + 1.05 * s ** 1.6]); |
| +добавлено | } |
| +добавлено | RIBS.push([0, 1, [0, 0, 0, 0], 0.1, 1]); |
| +добавлено | for (const side of [1, -1]) { |
| +добавлено | RIBS.push([1, side, [0, 0, 0.14, 0], 0.3 + side * 0.1, 1]); |
| +добавлено | for (let k = 0; k < NF; k++) RIBS.push([2, side, [FEAT[k][0], FEAT[k][1], 0.28 - 0.06 * FEAT[k][0], FEAT[k][2]], 0.7 + k * 0.53 + side * 0.21, 1 - 0.1 * FEAT[k][0]]); |
| +добавлено | for (let k = 0; k < NCV; k++) { |
| +добавлено | const s = (k + 0.3) / NCV * 0.8; |
| +добавлено | RIBS.push([2, side, [s, 0.34 + 0.08 * s, 0.32, 0.12 + 0.5 * s], 4.1 + k * 0.61 + side * 0.33, 1.05]); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | for (let i = 0; i < NT; i++) { |
| +добавлено | const o = (i - (NT - 1) / 2) / ((NT - 1) / 2); // -1..1 |
| +добавлено | RIBS.push([3, 1, [o * 0.17, 2.6 - 0.9 * Math.abs(o) ** 1.2, 0.14 - 0.035 * Math.abs(o), i * 1.3], 1.1 + i * 0.7, 1]); |
| +добавлено | } |
| +добавлено | for (let i = 0; i < NCR; i++) RIBS.push([4, 1, [(i - 1) * 0.07, 0.42 + 0.12 * (1 - Math.abs(i - 1)), 0.09, i * 2.1], 2.3 + i * 0.4, 1]); |
| +добавлено | const NR = RIBS.length; // 41 |
| +добавлено | |
| +добавлено | // flame-ish kinds, retinted from temperature + tint like fire.js: [kind, hot x T, cold x T] |
| +добавлено | const HOT_KINDS = [['flame', 1.25, 0.38], ['ember', 1.1, 0.5], ['spark', 1.45, 0.6], ['wisp', 1.35, 0.55]]; |
| +добавлено | |
| +добавлено | // JS mirrors of the GLSL wing (phoenix.glsl.js): place emitters on the wing |
| +добавлено | const clamp01 = (x) => Math.min(1, Math.max(0, x)); |
| +добавлено | function armFlat(s, k, out) { |
| +добавлено | return out.set(SX + 1.4 * s * (0.3 + 0.7 * k), 0.02 * s, 0.1 + 0.2 * Math.sin(Math.PI * Math.min(s * 1.15, 1)) - 0.45 * s * s * s); |
| +добавлено | } |
| +добавлено | function foldW(p, beta) { |
| +добавлено | const dx = p.x - SX, dz = p.z - 0.1, c = Math.cos(beta), s = Math.sin(beta); |
| +добавлено | p.x = SX + dx * c + dz * s; p.z = 0.1 - dx * s + dz * c; |
| +добавлено | return p; |
| +добавлено | } |
| +добавлено | function flapW(p, lag, ph, amp, dih) { |
| +добавлено | const dx = p.x - SX, se = clamp01(dx / 1.7), x = ph - 1.1 * se - 1.6 * lag; |
| +добавлено | const th = dih * (0.6 + 0.4 * se) + amp * (Math.sin(x) + 0.22 * Math.sin(2 * x)) * (0.45 + 0.75 * se); |
| +добавлено | const c = Math.cos(th), s = Math.sin(th), y = p.y; |
| +добавлено | p.x = SX + dx * c - y * s; p.y = dx * s + y * c; |
| +добавлено | return p; |
| +добавлено | } |
| +добавлено | const smooth = (a, b, x) => { const t = clamp01((x - a) / (b - a)); return t * t * (3 - 2 * t); }; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'phoenix', name: 'Phoenix', element: 'fire', order: 10, hue: '#ff8a1e', |
| +добавлено | shape: 'projectile', radius: 0.4, |
| +добавлено | params: { size: 1, speed: 9, arc: 1, swing: 1, flap: 1, temperature: 1900, intensity: 1, flames: 1, feathers: 1, embers: 1, tail: 1, smoke: 1, haze: 1, scorch: 1, tint: '#ffffff' }, |
| +добавлено | ranges: { |
| +добавлено | size: [0.4, 2, 0.01], speed: [4, 20, 0.1], arc: [0.3, 2, 0.01], swing: [-2, 2, 0.01], flap: [0, 2, 0.01], temperature: [900, 6500, 10], |
| +добавлено | intensity: [0, 3, 0.01], flames: [0, 3, 0.01], feathers: [0, 3, 0.01], embers: [0, 3, 0.01], tail: [0.3, 2, 0.01], smoke: [0, 3, 0.01], |
| +добавлено | haze: [0, 3, 0.01], scorch: [0, 2, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | 'sun phoenix': { temperature: 2600, intensity: 1.3, tail: 1.3, smoke: 0.4 }, |
| +добавлено | 'azure phoenix': { temperature: 6500, tint: '#8fb8ff', smoke: 0.3, embers: 1.4 }, |
| +добавлено | 'ember hawk': { size: 0.7, speed: 13, arc: 0.6, tail: 0.7, flap: 1.4, feathers: 0.6, temperature: 1500, tint: '#ff7a5a' }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 0.9, impact: 0.35, linger: 3.6, sustain: 6 }, |
| +добавлено | kinds: { |
| +добавлено | flame: { style: 'media', drag: 3.6, buoyancy: 3.8, curl: 3.2, curlScale: 0.6, life: [0.16, 0.36], size: [0.09, 0.32], sizePow: 0.6, emissive: 1.3, temp: [2375, 720], toSmokeAt: 0.5, smokeAlbedo: 0.04, alpha: [0.75, 0], density: 9, distortion: 0.6, erosion: 1.6, fadeIn: 0.06, fadeOut: 0.3 }, |
| +добавлено | ember: { style: 'glow', drag: 0.6, buoyancy: 1.7, gravity: 1.2, curl: 2.8, curlScale: 0.5, life: [1.0, 2.6], size: [0.018, 0.006], stretch: 0.02, emissive: 5, temp: [2090, 950], restitution: 0.3, friction: 0.4, paints: 'char', paintAmount: 0.05 }, |
| +добавлено | spark: { style: 'glow', drag: 0.5, gravity: 9.8, restitution: 0.4, friction: 0.3, life: [0.3, 1.0], size: [0.026, 0.008], stretch: 0.02, emissive: 6, temp: [2750, 1140], paints: 'heat', paintAmount: 0.04 }, |
| +добавлено | // wingtip vortices: thin hot streaks peeling off the tips |
| +добавлено | wisp: { style: 'glow', drag: 2.4, buoyancy: 1.4, curl: 2.2, curlScale: 0.45, life: [0.25, 0.6], size: [0.045, 0.012], stretch: 0.07, emissive: 3, temp: [2560, 1045], fadeIn: 0.1, fadeOut: 0.5 }, |
| +добавлено | // burst smoke: lit grey-brown soot that billows, curls and thins out (not dark balls) |
| +добавлено | smoke: { style: 'media', emissive: 0, drag: 1.6, buoyancy: 1.9, curl: 2.4, curlScale: 0.4, life: [2.4, 4.8], size: [0.3, 2.4], sizePow: 0.5, color: ['#2b2520', '#5d554e'], alpha: [0.55, 0], density: 2.4, erosion: 1.3, fadeIn: 0.18, fadeOut: 0.5 }, |
| +добавлено | // floor dust thrown out by the blast, rolling up as it slows |
| +добавлено | dust: { style: 'media', emissive: 0, drag: 2.6, buoyancy: 0.55, curl: 1.4, curlScale: 0.5, life: [0.9, 2.0], size: [0.25, 1.5], sizePow: 0.6, color: ['#7a6a5a', '#5a4f45'], alpha: [0.3, 0], density: 1.2, erosion: 1.0, fadeIn: 0.05, fadeOut: 0.5 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const E = { |
| +добавлено | gather: P.emitter('phoenix.ember', { shape: 'sphere', radius: 0.2, spread: 0.6 }), |
| +добавлено | orb: P.emitter('phoenix.flame', { shape: 'sphere', radius: 0.35, spread: 0.5 }), |
| +добавлено | launch: P.emitter('phoenix.flame', { shape: 'shell', radius: 0.5, spread: 1.0, radial: 4 }), |
| +добавлено | edge: P.emitter('phoenix.flame', { shape: 'line', radius: 0.06, spread: 0.5 }), |
| +добавлено | tip: P.emitter('phoenix.wisp', { shape: 'sphere', radius: 0.08, spread: 0.6 }), |
| +добавлено | shed: P.emitter('phoenix.wisp', { shape: 'line', radius: 0.12, spread: 1.6 }), |
| +добавлено | ember: P.emitter('phoenix.ember', { shape: 'sphere', radius: 0.25, spread: 1.0 }), |
| +добавлено | blast: P.emitter('phoenix.flame', { shape: 'sphere', radius: 0.5, spread: 2.0, radial: 5.5, life: 2.4 }), |
| +добавлено | spark: P.emitter('phoenix.spark', { shape: 'sphere', radius: 0.3, spread: 7, radial: 4 }), |
| +добавлено | smoke: P.emitter('phoenix.smoke', { shape: 'sphere', radius: 0.6, spread: 0.6 }), |
| +добавлено | dust: P.emitter('phoenix.dust', { shape: 'ring', radius: 0.8, spread: 0.6, radial: 5 }), |
| +добавлено | rise: P.emitter('phoenix.wisp', { shape: 'sphere', radius: 0.06, spread: 0.25 }), |
| +добавлено | }; |
| +добавлено | |
| +добавлено | // --- flame mass: every combusting volume of the spell goes through these four calls (the |
| +добавлено | // charge orb, the launch flare, the body's flame stream, the impact blast), so the whole mass |
| +добавлено | // can move onto the shared FluidVolume combustion solver in one place. Particles meanwhile. |
| +добавлено | const flameMass = { |
| +добавлено | orb(pos, k, p, dt) { |
| +добавлено | E.orb.pos.copy(pos); E.orb.radius = 0.15 + 0.3 * k; E.orb.vel.set(0, 0.5, 0); E.orb.size = 0.6 * p.size * (0.4 + k); |
| +добавлено | E.orb.rate(160 * p.flames * (0.3 + k), dt); |
| +добавлено | }, |
| +добавлено | launch(pos, p) { |
| +добавлено | E.launch.pos.copy(pos); E.launch.size = p.size; E.launch.vel.set(0, 1.5, 0); E.launch.burst(90 * p.flames); |
| +добавлено | }, |
| +добавлено | stream(a, b, vel, p, rate, dt) { // a line source from a to b moving with vel |
| +добавлено | E.edge.pos.copy(a); E.edge.p.copy(b); |
| +добавлено | E.edge.vel.copy(vel).multiplyScalar(0.7); E.edge.size = 0.9 * p.size; E.edge.rate(420 * rate, dt); |
| +добавлено | }, |
| +добавлено | blast(pos, p) { |
| +добавлено | E.blast.pos.copy(pos); E.blast.radius = 0.6 * p.size; E.blast.vel.set(0, 1.8, 0); E.blast.size = 1.7 * p.size; E.blast.burst(150 * p.flames * p.size); |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | // --- shared uniforms: bird slots + fire ramp --- |
| +добавлено | const slotV = () => Array.from({ length: SLOTS }, () => new THREE.Vector4(0, -100, 0, 0)); |
| +добавлено | const UA = slotV(), UF = slotV(), UU = slotV(), UW = slotV(), UT = slotV(); |
| +добавлено | const hot = new THREE.Color(), mid = new THREE.Color(), cool = new THREE.Color(); |
| +добавлено | const shared = { |
| +добавлено | uBirdA: { value: UA }, uBirdF: { value: UF }, uBirdU: { value: UU }, uBirdW: { value: UW }, uBirdT: { value: UT }, |
| +добавлено | uHot: { value: hot }, uMid: { value: mid }, uCool: { value: cool }, uHaze: { value: 0.04 }, |
| +добавлено | }; |
| +добавлено | const birdFx = new fx.AnalyticRibbons({ max: NR * SLOTS, segments: 40, pathGLSL: BIRD_PATH, shadeGLSL: BIRD_SHADE, target: 'fx', uniforms: shared }); |
| +добавлено | const birdLn = new fx.AnalyticRibbons({ max: NR * SLOTS, segments: 40, pathGLSL: BIRD_PATH, shadeGLSL: BIRD_SHADE, target: 'lines', uniforms: shared }); |
| +добавлено | const featFx = new fx.AnalyticRibbons({ max: NFB, segments: 12, pathGLSL: FEATHER_PATH, shadeGLSL: FEATHER_SHADE, target: 'fx', uniforms: shared }); |
| +добавлено | const featLn = new fx.AnalyticRibbons({ max: NFB, segments: 12, pathGLSL: FEATHER_PATH, shadeGLSL: FEATHER_SHADE, target: 'lines', uniforms: shared }); |
| +добавлено | fx.scenes.fx.add(birdFx.mesh, featFx.mesh); |
| +добавлено | fx.scenes.lines.add(birdLn.mesh, featLn.mesh); |
| +добавлено | |
| +добавлено | // burning feathers: fire and forget, released by tick() when burnt out (or by their cast's 'done') |
| +добавлено | const feathers = Array.from({ length: NFB }, () => ({ fx: null, ln: null, birth: 0, life: 0, owner: -1 })); |
| +добавлено | |
| +добавлено | // per-cast state, indexed by the cast record (16) |
| +добавлено | const state = Array.from({ length: 16 }, () => ({ |
| +добавлено | slot: -1, fx: new Array(NR).fill(null), ln: new Array(NR).fill(null), light: null, trail: null, shock: null, |
| +добавлено | P0: new THREE.Vector3(), P1: new THREE.Vector3(), P2: new THREE.Vector3(), P3: new THREE.Vector3(), |
| +добавлено | pos: new THREE.Vector3(), F: new THREE.Vector3(0, 0, 1), U: new THREE.Vector3(0, 1, 0), vel: new THREE.Vector3(), acc: new THREE.Vector3(), |
| +добавлено | dive: new THREE.Vector3(0, 0, -1), |
| +добавлено | s: 0, ph: 0, amp: 0, spread: 0, sweep: 0, dih: 0, bx: 0, by: 0, scale: 1, I: 0, shed: 0, flyT: 0, age: 0, splat: 0, side: 1, |
| +добавлено | })); |
| +добавлено | const slotOwner = new Array(SLOTS).fill(-1); |
| +добавлено | |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _c = new THREE.Vector3(), _q = new THREE.Vector3(), _R = new THREE.Vector3(); |
| +добавлено | const _up = new THREE.Vector3(0, 1, 0), _dF = new THREE.Vector3(), _v = new THREE.Vector3(); |
| +добавлено | const col = new THREE.Color(), tintCol = new THREE.Color(); |
| +добавлено | const SPLAT = { x: 0, z: 0, radius: 1, heat: 0, char: 0, glow: 0, pattern: 'radial', seed: 0, x2: 0, z2: 0, width: 0 }; |
| +добавлено | const SHOCK = { pos: new THREE.Vector3(), radius0: 0.3, radius1: 6, duration: 0.5, thickness: 0.8, distortion: 0.03, intensity: 0, ground: false }; |
| +добавлено | |
| +добавлено | // --- colour: fire ramp + particle kinds follow temperature and tint --- |
| +добавлено | let lastT = -1, lastTint = ''; |
| +добавлено | const HOT = HOT_KINDS.map(([k, a, b]) => ({ name: `phoenix.${k}`, a, b, white: { temp: [0, 0] }, tinted: { temp: null, color: [new THREE.Color(), new THREE.Color()] } })); |
| +добавлено | function retint() { |
| +добавлено | const p = S.params; |
| +добавлено | if (p.temperature === lastT && p.tint === lastTint) return; |
| +добавлено | lastT = p.temperature; lastTint = p.tint; |
| +добавлено | tintCol.set(p.tint); |
| +добавлено | const T = p.temperature; |
| +добавлено | blackbodyRGB(T * 1.45, hot).multiply(tintCol).multiplyScalar(0.95); |
| +добавлено | blackbodyRGB(T * 1.0, mid).multiply(tintCol).multiplyScalar(0.5); |
| +добавлено | blackbodyRGB(T * 0.7, cool).multiply(tintCol).multiplyScalar(0.2); |
| +добавлено | const white = tintCol.r > 0.97 && tintCol.g > 0.97 && tintCol.b > 0.97; |
| +добавлено | for (const h of HOT) { |
| +добавлено | if (white) { h.white.temp[0] = T * h.a; h.white.temp[1] = T * h.b; P.setKind(h.name, h.white); } |
| +добавлено | else { |
| +добавлено | blackbodyRGB(T * h.a, h.tinted.color[0]).multiply(tintCol); |
| +добавлено | blackbodyRGB(T * h.b, h.tinted.color[1]).multiply(tintCol).multiplyScalar((h.b / h.a) ** 3); |
| +добавлено | P.setKind(h.name, h.tinted); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | retint(); |
| +добавлено | const lightColor = (T, out) => blackbodyRGB(T, out).multiply(tintCol); |
| +добавлено | |
| +добавлено | // --- bird --- |
| +добавлено | function writeBird(st) { |
| +добавлено | const i = st.slot; |
| +добавлено | if (i < 0) return; |
| +добавлено | UA[i].set(st.pos.x, st.pos.y, st.pos.z, st.scale); |
| +добавлено | UF[i].set(st.F.x, st.F.y, st.F.z, st.ph); |
| +добавлено | UU[i].set(st.U.x, st.U.y, st.U.z, st.amp); |
| +добавлено | UW[i].set(st.spread, st.sweep, st.dih, 0); |
| +добавлено | UT[i].set(st.bx, st.by, 0, 0); |
| +добавлено | const I = st.I * S.params.intensity; |
| +добавлено | for (let r = 0; r < NR; r++) { |
| +добавлено | if (st.fx[r]) st.fx[r].intensity = I * (RIBS[r][0] === 3 ? 0.9 : 1); |
| +добавлено | if (st.ln[r]) st.ln[r].intensity = I * 1.3; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | function acquireBird(st, ci) { |
| +добавлено | st.slot = slotOwner.indexOf(-1); |
| +добавлено | if (st.slot < 0) return; |
| +добавлено | slotOwner[st.slot] = ci; |
| +добавлено | for (let r = 0; r < NR; r++) { |
| +добавлено | st.fx[r] = setRib(birdFx.acquire(), RIBS[r], st.slot); |
| +добавлено | st.ln[r] = setRib(birdLn.acquire(), RIBS[r], st.slot); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | function setRib(h, R, slot) { |
| +добавлено | if (!h) return null; |
| +добавлено | const p1 = R[2]; |
| +добавлено | h.p0.set(R[0], slot, R[1], 0); |
| +добавлено | h.p1.set(p1[0], p1[1] * (R[0] === 3 ? S.params.tail : 1), p1[2], p1[3]); |
| +добавлено | h.p2.set(R[3], R[4], 0, 0); |
| +добавлено | h.intensity = 0; |
| +добавлено | return h; |
| +добавлено | } |
| +добавлено | function releaseBird(st) { |
| +добавлено | for (let r = 0; r < NR; r++) { st.fx[r]?.release(); st.ln[r]?.release(); st.fx[r] = st.ln[r] = null; } |
| +добавлено | if (st.slot >= 0) { slotOwner[st.slot] = -1; UA[st.slot].set(0, -100, 0, 0); } |
| +добавлено | st.slot = -1; |
| +добавлено | } |
| +добавлено | // local bird point -> world |
| +добавлено | function toWorld(st, q, out) { |
| +добавлено | _R.crossVectors(st.U, st.F); |
| +добавлено | return out.copy(st.pos).addScaledVector(_R, q.x * st.scale).addScaledVector(st.U, q.y * st.scale).addScaledVector(st.F, q.z * st.scale); |
| +добавлено | } |
| +добавлено | // a point on wing feather k (side +-1) at fraction t along it, world space |
| +добавлено | function featherPoint(st, side, k, t, out) { |
| +добавлено | const s = FEAT[k][0], L = FEAT[k][1] * (0.55 + 0.45 * st.spread), an = FEAT[k][2] * (0.15 + 0.85 * st.spread) + 0.18 * t; |
| +добавлено | armFlat(s, st.spread, _q); |
| +добавлено | _q.x += Math.sin(an) * L * t; _q.y += -0.01 - 0.05 * t * L * t; _q.z += -0.02 - Math.cos(an) * L * t; |
| +добавлено | flapW(foldW(_q, (1 - st.spread) * 1.3 + st.sweep * 0.6), L * t, st.ph, st.amp, st.dih); |
| +добавлено | _q.x *= side; |
| +добавлено | return toWorld(st, _q, out); |
| +добавлено | } |
| +добавлено | function tailPoint(st, t, out) { |
| +добавлено | const L = 2.6 * S.params.tail; |
| +добавлено | _q.set(st.bx * t * t * L, -0.05 * t + st.by * t * t * L, -0.45 - L * t); |
| +добавлено | return toWorld(st, _q, out); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function spawnFeather(owner, pos, vel, life, len, width, heat) { |
| +добавлено | let f = null; |
| +добавлено | for (let i = 0; i < NFB && !f; i++) if (!feathers[i].fx && !feathers[i].ln) f = feathers[i]; |
| +добавлено | if (!f) return; |
| +добавлено | f.fx = featFx.acquire(); f.ln = featLn.acquire(); |
| +добавлено | f.birth = fx.U.uTime.value; f.life = life; f.owner = owner; |
| +добавлено | const seed = fx.rand() * 100, fall = 0.5 + 0.7 * fx.rand(), spin = (fx.rand() - 0.5) * 9, drag = 2.2 + 1.6 * fx.rand(); |
| +добавлено | setFeather(f.fx, f, pos, vel, len, width, heat, seed, fall, spin, drag, 1); |
| +добавлено | setFeather(f.ln, f, pos, vel, len, width, heat, seed, fall, spin, drag, 1.4); |
| +добавлено | } |
| +добавлено | function setFeather(h, f, pos, vel, len, width, heat, seed, fall, spin, drag, gain) { |
| +добавлено | if (!h) return; |
| +добавлено | h.p0.set(pos.x, pos.y, pos.z, f.birth); |
| +добавлено | h.p1.set(vel.x, vel.y, vel.z, f.life); |
| +добавлено | h.p2.set(fall, spin, 0, drag); |
| +добавлено | h.p3.set(len, width, seed, heat); |
| +добавлено | h.intensity = S.params.intensity * gain; |
| +добавлено | } |
| +добавлено | function releaseFeather(f) { f.fx?.release(); f.ln?.release(); f.fx = f.ln = null; f.owner = -1; } |
| +добавлено | |
| +добавлено | function splat(x, z, radius, heat, char, pattern, seed, x2 = x, z2 = z, width = 0) { |
| +добавлено | SPLAT.x = x; SPLAT.z = z; SPLAT.radius = radius; SPLAT.heat = heat; SPLAT.char = char; SPLAT.pattern = pattern; |
| +добавлено | SPLAT.seed = seed; SPLAT.x2 = x2; SPLAT.z2 = z2; SPLAT.width = width; |
| +добавлено | fx.ground.splat(SPLAT); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- flight --- |
| +добавлено | function bezier(st, s, out) { |
| +добавлено | const u = 1 - s; |
| +добавлено | return out.copy(st.P0).multiplyScalar(u * u * u).addScaledVector(st.P1, 3 * u * u * s).addScaledVector(st.P2, 3 * u * s * s).addScaledVector(st.P3, s * s * s); |
| +добавлено | } |
| +добавлено | function bezierD(st, s, out) { |
| +добавлено | const u = 1 - s; |
| +добавлено | out.subVectors(st.P1, st.P0).multiplyScalar(3 * u * u); |
| +добавлено | out.addScaledVector(_c.subVectors(st.P2, st.P1), 6 * u * s); |
| +добавлено | return out.addScaledVector(_c.subVectors(st.P3, st.P2), 3 * s * s); |
| +добавлено | } |
| +добавлено | // forward = velocity; up banks into the turn; the tail lags the turn |
| +добавлено | function orient(st, dt, rate) { |
| +добавлено | _dF.copy(st.F); |
| +добавлено | if (st.vel.lengthSq() > 1e-6) st.F.lerp(_a.copy(st.vel).normalize(), 1 - Math.exp(-dt * 14)).normalize(); |
| +добавлено | // level 'up' (world up across the flight direction; the last up when flying straight up/down), |
| +добавлено | // rolled into the turn by the sideways acceleration, at most ~50 degrees |
| +добавлено | _b.copy(_up).addScaledVector(st.F, -st.F.y); |
| +добавлено | if (_b.lengthSq() < 0.04) _b.copy(st.U).addScaledVector(st.F, -st.U.dot(st.F)); |
| +добавлено | _b.normalize(); |
| +добавлено | _R.crossVectors(_b, st.F); |
| +добавлено | const roll = THREE.MathUtils.clamp(Math.atan2(st.acc.dot(_R), 9.8) * 0.8, -0.9, 0.9); |
| +добавлено | _b.multiplyScalar(Math.cos(roll)).addScaledVector(_R, Math.sin(roll)); |
| +добавлено | st.U.lerp(_b, 1 - Math.exp(-dt * rate)); |
| +добавлено | st.U.addScaledVector(st.F, -st.U.dot(st.F)).normalize(); |
| +добавлено | if (dt > 0) { |
| +добавлено | _dF.subVectors(st.F, _dF).divideScalar(dt); |
| +добавлено | _R.crossVectors(st.U, st.F); |
| +добавлено | const k = 1 - Math.exp(-dt * 5); |
| +добавлено | st.bx += (THREE.MathUtils.clamp(-_dF.dot(_R) * 0.18, -0.4, 0.4) - st.bx) * k; |
| +добавлено | st.by += (THREE.MathUtils.clamp(-_dF.dot(st.U) * 0.18, -0.4, 0.4) - st.by) * k; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function charge(c, st, p, dt) { |
| +добавлено | const k = clamp01(c.t / c.durations.charge), f = c.from; |
| +добавлено | // embers spiral into the focus |
| +добавлено | const a = c.age * 8; |
| +добавлено | E.gather.pos.copy(f).addScaledVector(_a.set(Math.sin(a), 0.25 * Math.sin(a * 0.5), Math.cos(a)), 1.2 - 0.5 * k); |
| +добавлено | E.gather.vel.subVectors(f, E.gather.pos).multiplyScalar(2.8).addScaledVector(_a.set(Math.cos(a), 0, -Math.sin(a)), 2.4); |
| +добавлено | E.gather.rate(220 * p.embers, dt); |
| +добавлено | flameMass.orb(f, k, p, dt); |
| +добавлено | // the bird forms in the fire, wings wrapped about it, rocking with its first slow beats |
| +добавлено | st.pos.copy(f).addScaledVector(_up, 0.1 * k); |
| +добавлено | bezierD(st, 0, st.F).normalize().lerp(_up, 0.55).normalize(); |
| +добавлено | st.U.copy(_up).addScaledVector(st.F, -_up.dot(st.F)).normalize(); |
| +добавлено | st.scale = 1.2 * p.size * (0.55 + 0.45 * smooth(0, 1, k)); |
| +добавлено | st.spread = 0.1 * k; st.sweep = 0; st.dih = 1.15; st.amp = 0.12 * k; st.ph += TAU * 1.2 * dt; |
| +добавлено | st.I = 1.1 * smooth(0.05, 1, k) ** 1.5; |
| +добавлено | writeBird(st); |
| +добавлено | const L = st.light; |
| +добавлено | if (L) { L.pos.copy(f); L.intensity = 14 * k * p.intensity; } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function fly(c, st, p, dt) { |
| +добавлено | const s = st.s; |
| +добавлено | st.flyT += dt; |
| +добавлено | // beat hard on the climb, soar past the target, beat round the hook with the wings raised, |
| +добавлено | // then strike with them thrown wide and forward (a raptor's flare): the thumbnail pose |
| +добавлено | const climb = 1 - smooth(0.24, 0.36, s), hook = smooth(0.56, 0.66, s) * (1 - smooth(0.78, 0.86, s)), strike = smooth(0.8, 0.9, s); |
| +добавлено | const open = smooth(0, 0.3, st.flyT); |
| +добавлено | st.spread = Math.min(1.08, open * 1.12 - 0.08 * smooth(0.25, 0.45, st.flyT)) + 0.06 * strike; |
| +добавлено | st.sweep = 0.35 * hook - 0.8 * strike; |
| +добавлено | st.dih = (1.15 * (1 - open) + 0.12) + 0.35 * hook + 0.12 * strike; |
| +добавлено | st.amp = p.flap * (0.95 * climb + 0.14 * (1 - climb) * (1 - hook) * (1 - strike) + 0.55 * hook) * open * (1 - strike); |
| +добавлено | st.ph += TAU * dt * (2.4 * climb + 1.0 * (1 - climb) + 1.2 * hook) * Math.max(0.3, p.flap); |
| +добавлено | st.I = 1.1; |
| +добавлено | st.scale = 1.2 * p.size; |
| +добавлено | orient(st, dt, 7); |
| +добавлено | writeBird(st); |
| +добавлено | |
| +добавлено | // shed flame off the trailing edges, wisps off the tips, embers off the tail |
| +добавлено | const fr = p.flames; |
| +добавлено | for (let side = -1; side <= 1; side += 2) { |
| +добавлено | featherPoint(st, side, 0, 0.95, E.shed.pos); featherPoint(st, side, NF - 1, 0.95, E.shed.p); |
| +добавлено | E.shed.vel.copy(st.vel).multiplyScalar(0.12); E.shed.rate(150 * fr, dt); |
| +добавлено | featherPoint(st, side, NF - 1, 1, E.tip.pos); |
| +добавлено | E.tip.vel.copy(st.vel).multiplyScalar(0.35); E.tip.rate(70 * p.embers, dt); |
| +добавлено | } |
| +добавлено | tailPoint(st, 0.85, E.ember.pos); |
| +добавлено | E.ember.radius = 0.35 * p.size; E.ember.vel.copy(st.vel).multiplyScalar(0.2); E.ember.rate(120 * p.embers, dt); |
| +добавлено | // the body burns: a dense stream of small flames from the chest back along the tail root |
| +добавлено | flameMass.stream(toWorld(st, _q.set(0, 0.02, 0.2), _b), toWorld(st, _q.set(0, 0, -0.5), _c), st.vel, p, fr, dt); |
| +добавлено | // burning feathers peel off and flutter down |
| +добавлено | st.shed -= dt * 7 * p.feathers; |
| +добавлено | if (st.shed <= 0) { |
| +добавлено | st.shed += 1; |
| +добавлено | const side = fx.rand() < 0.5 ? -1 : 1, k = Math.floor(fx.rand() * NF); |
| +добавлено | featherPoint(st, side, k, 0.6, _b); |
| +добавлено | _v.copy(st.vel).multiplyScalar(0.2).add(_a.set(fx.rand() - 0.5, 0.6 + fx.rand(), fx.rand() - 0.5)); |
| +добавлено | spawnFeather(c.index, _b, _v, 1.1 + 0.6 * fx.rand(), 0.32 * p.size, 0.09 * p.size, 0.85); |
| +добавлено | } |
| +добавлено | st.trail?.push(st.pos); |
| +добавлено | const L = st.light; |
| +добавлено | if (L) { L.pos.copy(st.pos); L.intensity = 18 * p.intensity * (0.85 + 0.15 * Math.sin(c.age * 23)); } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function explode(c, st, p) { |
| +добавлено | const pos = c.pos, x = pos.x, z = pos.z; |
| +добавлено | flameMass.blast(pos, p); |
| +добавлено | E.spark.pos.copy(pos); E.spark.vel.set(0, 1.5, 0); E.spark.burst(600 * p.embers); |
| +добавлено | E.ember.pos.copy(pos); E.ember.radius = 0.6 * p.size; E.ember.vel.set(0, 2.5, 0); E.ember.burst(350 * p.embers); |
| +добавлено | E.smoke.pos.copy(pos).setY(pos.y + 0.6 * p.size); E.smoke.radius = 1.0 * p.size; E.smoke.vel.set(0, 0.9, 0); E.smoke.size = p.size; E.smoke.burst(110 * p.smoke); |
| +добавлено | E.dust.pos.set(x, 0.15, z); E.dust.size = p.size; E.dust.burst(110 * p.size); |
| +добавлено | // the bird bursts into burning feathers |
| +добавлено | const n = Math.round(44 * p.feathers); |
| +добавлено | for (let i = 0; i < n; i++) { |
| +добавлено | const a = fx.rand() * TAU, y = 0.15 + 0.85 * fx.rand(); |
| +добавлено | _v.set(Math.cos(a) * Math.sqrt(1 - y * y), y, Math.sin(a) * Math.sqrt(1 - y * y)).multiplyScalar((5 + 7 * fx.rand()) * Math.sqrt(p.size)); |
| +добавлено | _v.addScaledVector(st.dive, 3); |
| +добавлено | spawnFeather(c.index, _b.copy(pos).addScaledVector(_v, 0.03), _v, 1.7 + 1.5 * fx.rand(), (0.38 + 0.25 * fx.rand()) * p.size, (0.1 + 0.06 * fx.rand()) * p.size, 0.8 + 0.2 * fx.rand()); |
| +добавлено | } |
| +добавлено | // pressure shell: shoves loose particles (dust, leaves, embers) outwards |
| +добавлено | st.shock = fx.forces.acquire(); |
| +добавлено | if (st.shock) { st.shock.type = 4; st.shock.pos.copy(pos); st.shock.p[0] = 0.3; st.shock.p[1] = 1.2; st.shock.p[2] = 60 * p.size; st.age = 0; } |
| +добавлено | SHOCK.pos.set(x, 0.05, z); SHOCK.radius0 = 0.4; SHOCK.radius1 = 7 * p.size; SHOCK.duration = 0.55; SHOCK.thickness = 0.8; SHOCK.distortion = 0.04 * p.haze; SHOCK.ground = true; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | SHOCK.pos.copy(pos); SHOCK.radius1 = 5 * p.size; SHOCK.duration = 0.4; SHOCK.ground = false; SHOCK.distortion = 0.03 * p.haze; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | // the spread-wing silhouette scorched into the floor, along the dive |
| +добавлено | const sc = p.scorch * p.size, fwd = st.dive, rx = fwd.z, rz = -fwd.x; |
| +добавлено | splat(x, z, 1.6 * sc, 1.1, 0.9, 'noise', c.seed % 97); |
| +добавлено | for (let side = -1; side <= 1; side += 2) { |
| +добавлено | for (let i = 0; i < 5; i++) { |
| +добавлено | const ang = -0.2 + i * 0.28, len = (3.1 - 0.35 * i) * sc; |
| +добавлено | const ax = rx * side * Math.cos(ang) - fwd.x * Math.sin(ang), az = rz * side * Math.cos(ang) - fwd.z * Math.sin(ang); |
| +добавлено | splat(x + ax * 0.4 * sc, z + az * 0.4 * sc, 0.2 * sc, 0.75, 1.0, 'line', (c.seed + i * 7 + side) % 89, x + ax * len, z + az * len, (0.26 - 0.03 * i) * sc); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | splat(x - fwd.x * 0.3, z - fwd.z * 0.3, 0.2 * sc, 0.6, 0.9, 'line', c.seed % 61, x - fwd.x * 2.6 * sc, z - fwd.z * 2.6 * sc, 0.22 * sc); |
| +добавлено | fx.impacts.add('scorch', pos, 3.2 * p.size, 1, 9, lightColor(p.temperature, col)); |
| +добавлено | if (st.light) { st.light.intensity = 45 * p.intensity; st.light.range = 15 * p.size; st.light.pos.copy(pos).setY(pos.y + 0.5); } |
| +добавлено | fx.flash(0.08 * p.intensity); |
| +добавлено | fx.rig.shake(0.55, 12); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(3.5, 0.09); fx.rig.hitstop(0.06); } |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const d = Math.hypot(dm.pos.x - x, dm.pos.z - z), R = 3.2 * p.size; |
| +добавлено | if (d < R) { dm.status.burn = Math.max(dm.status.burn, 1.3 - d / R); dm.hit(_a.subVectors(dm.pos, pos).setY(0).addScaledVector(fwd, 0.8).normalize(), 1.2 * (1 - d / R)); } |
| +добавлено | } |
| +добавлено | // the pool ripples when the burst lands near it |
| +добавлено | const pool = fx.ARENA.pool; |
| +добавлено | const qx = Math.max(Math.abs(x - pool.center.x) - pool.half.x, 0), qz = Math.max(Math.abs(z - pool.center.y) - pool.half.y, 0); |
| +добавлено | if (Math.hypot(qx, qz) < 4 && fx.pool) { |
| +добавлено | fx.pool.ripple(THREE.MathUtils.clamp(x, pool.center.x - pool.half.x, pool.center.x + pool.half.x), THREE.MathUtils.clamp(z, pool.center.y - pool.half.y, pool.center.y + pool.half.y), 0.05, 0.8); |
| +добавлено | fx.pool.flash(lightColor(p.temperature, col), 0.6); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function releaseAll(st, ci) { |
| +добавлено | releaseBird(st); |
| +добавлено | st.light?.release(); st.light = null; |
| +добавлено | st.trail?.release(); st.trail = null; |
| +добавлено | st.shock?.release(); st.shock = null; |
| +добавлено | for (const f of feathers) if (f.owner === ci) releaseFeather(f); |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'tint' || k === 'temperature') retint(); }, |
| +добавлено | tick() { |
| +добавлено | const now = fx.U.uTime.value; |
| +добавлено | for (const f of feathers) if ((f.fx || f.ln) && (now - f.birth + 3600) % 3600 > f.life) releaseFeather(f); |
| +добавлено | birdFx.update(); birdLn.update(); featFx.update(); featLn.update(); |
| +добавлено | }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, st = state[c.index]; |
| +добавлено | retint(); |
| +добавлено | if (name === 'charge') { |
| +добавлено | releaseAll(st, c.index); |
| +добавлено | st.s = 0; st.ph = 0; st.flyT = 0; st.bx = st.by = 0; st.shed = 0; st.splat = 0; st.age = 0; |
| +добавлено | st.vel.set(0, 0, 0); st.acc.set(0, 0, 0); |
| +добавлено | // the arc: up and out to one side, sweeping past the target and hooking round behind it, |
| +добавлено | // so it strikes coming back towards the caster: the cameras get its spread wings head-on |
| +добавлено | const D = Math.max(2, c.from.distanceTo(c.to)); |
| +добавлено | _a.subVectors(c.to, c.from).setY(0); |
| +добавлено | if (_a.lengthSq() < 1e-4) _a.set(0, 0, -1); |
| +добавлено | _a.normalize(); |
| +добавлено | st.dive.copy(_a).negate(); |
| +добавлено | _R.crossVectors(_a, _up).normalize(); // to the right of travel |
| +добавлено | const sw = p.swing, up = p.arc; |
| +добавлено | st.P0.copy(c.from); |
| +добавлено | st.P1.copy(c.from).addScaledVector(_up, 3.0 * up).addScaledVector(_R, 3.6 * sw).addScaledVector(_a, 0.85 * D); |
| +добавлено | st.P2.copy(c.to).addScaledVector(_up, 2.8 * up).addScaledVector(_R, 1.6 * sw).addScaledVector(_a, 5.5); |
| +добавлено | const r2 = Math.hypot(st.P2.x, st.P2.z), lim = fx.ARENA.radius - 2; |
| +добавлено | if (r2 > lim) { st.P2.x *= lim / r2; st.P2.z *= lim / r2; } |
| +добавлено | st.P3.copy(c.to); |
| +добавлено | st.pos.copy(c.from); |
| +добавлено | acquireBird(st, c.index); |
| +добавлено | st.light = fx.lights.acquire(); |
| +добавлено | if (st.light) { st.light.range = 10; st.light.intensity = 0; st.light.shadow = true; lightColor(p.temperature, st.light.color); } |
| +добавлено | } else if (name === 'release') { |
| +добавлено | fx.rig.shake(0.14, 9); |
| +добавлено | flameMass.launch(c.from, p); |
| +добавлено | E.ember.pos.copy(c.from); E.ember.vel.set(0, 2, 0); E.ember.burst(120 * p.embers); |
| +добавлено | st.trail = fx.trails.acquire(); |
| +добавлено | const t = st.trail; |
| +добавлено | if (t) { t.width = 0.16 * p.size; t.intensity = 2.2 * p.intensity; t.glow = 3.5; t.glowIntensity = 0.25; t.fadeTime = 0.45; t.spacing = 0.15; t.distortion = 0.08 * p.haze; lightColor(p.temperature * 1.2, t.color); } |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | st.trail?.release(); st.trail = null; |
| +добавлено | st.age = 0; |
| +добавлено | explode(c, st, p); |
| +добавлено | } else if (name === 'linger') { |
| +добавлено | releaseBird(st); |
| +добавлено | } else if (name === 'done') releaseAll(st, c.index); |
| +добавлено | }, |
| +добавлено | |
| +добавлено | // own flight along the arc; faster as it stoops |
| +добавлено | travel(c, dt) { |
| +добавлено | const p = S.params, st = state[c.index]; |
| +добавлено | const ds = bezierD(st, st.s, _b).length(); |
| +добавлено | const v = p.speed * (0.8 + 0.6 * st.s * st.s) * (0.55 + 0.45 * smooth(0, 0.35, c.t)) * (1 - 0.35 * smooth(0.85, 1, st.s)); |
| +добавлено | st.s = Math.min(1, st.s + v * dt / Math.max(ds, 1e-3)); |
| +добавлено | _a.copy(st.pos); |
| +добавлено | bezier(st, st.s, st.pos); |
| +добавлено | _v.subVectors(st.pos, _a).divideScalar(Math.max(dt, 1e-4)); |
| +добавлено | if (dt > 0) st.acc.lerp(_c.subVectors(_v, st.vel).divideScalar(dt), 1 - Math.exp(-dt * 6)); |
| +добавлено | st.vel.copy(_v); |
| +добавлено | c.pos.copy(st.pos); c.vel.copy(st.vel); |
| +добавлено | return st.s >= 1 || c.t > c.durations.travel; |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, st = state[c.index]; |
| +добавлено | if (c.phase === 'charge') { charge(c, st, p, dt); return; } |
| +добавлено | if (c.phase === 'travel') { fly(c, st, p, dt); return; } |
| +добавлено | st.age += dt; |
| +добавлено | const a = st.age, L = st.light; |
| +добавлено | if (c.phase === 'impact' && st.slot >= 0) { |
| +добавлено | // the bird flies apart: its silhouette flares out huge, wings flung wide, and burns away |
| +добавлено | const k = clamp01(a / c.durations.impact), g = 1 - (1 - k) * (1 - k); |
| +добавлено | st.scale = 1.2 * p.size * (1 + 1.5 * g); |
| +добавлено | st.spread = 1.12; st.sweep = -0.95 * (1 - g) - 0.2 * g; st.dih = 0.3 + 0.25 * g; st.amp = 0; |
| +добавлено | st.pos.addScaledVector(_up, dt * 2.5 * (1 - k)); |
| +добавлено | st.I = 1.5 * (1 - k * k) * (1 - k); |
| +добавлено | writeBird(st); |
| +добавлено | } |
| +добавлено | const sh = st.shock; |
| +добавлено | if (sh) { |
| +добавлено | if (a > 0.6) { sh.release(); st.shock = null; } |
| +добавлено | else { sh.p[0] = 0.3 + 11 * p.size * a; sh.p[2] = 60 * p.size * (1 - a / 0.6); } |
| +добавлено | } |
| +добавлено | if (L) { |
| +добавлено | L.intensity *= Math.exp(-dt / 0.32); |
| +добавлено | if (c.phase === 'linger') L.intensity = Math.max(L.intensity, 4 * p.intensity * (0.85 + 0.15 * Math.sin(c.age * 17)) * Math.max(0, 1 - c.t / c.durations.linger)); |
| +добавлено | } |
| +добавлено | if (c.phase !== 'linger') return; |
| +добавлено | const t = c.t; |
| +добавлено | if (t < 2.2) { |
| +добавлено | E.smoke.pos.copy(c.pos).setY(c.pos.y + 1.2 * p.size + 0.8 * t); E.smoke.radius = 0.6 * p.size; E.smoke.vel.set(0, 0.8, 0); E.smoke.size = p.size; E.smoke.rate(12 * p.smoke, dt); |
| +добавлено | } |
| +добавлено | // the ashes stir: a spiral of embers lifts from the centre |
| +добавлено | if (t > 0.6 && t < 2.4) { |
| +добавлено | const k = smooth(0.6, 1.2, t) * (1 - smooth(1.8, 2.4, t)), ang = t * 9, r = 0.5 * p.size * (1 - 0.4 * (t - 0.6)); |
| +добавлено | E.rise.pos.set(c.pos.x + Math.cos(ang) * r, 0.15 + 0.9 * (t - 0.6), c.pos.z + Math.sin(ang) * r); |
| +добавлено | E.rise.vel.set(-Math.sin(ang) * 1.5, 2.2, Math.cos(ang) * 1.5); |
| +добавлено | E.rise.rate(140 * p.embers * k, dt); |
| +добавлено | E.ember.pos.set(c.pos.x, 0.2, c.pos.z); E.ember.radius = 0.5 * p.size; E.ember.vel.set(0, 1.4, 0); E.ember.rate(60 * p.embers * k, dt); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | |
| +добавлено | // Black Flame (hybrid, void + fire). Fire that drinks light instead of giving it: the reference is |
| +добавлено | // the sodium-lamp "black fire" (a sodium-laden flame under monochromatic sodium light absorbs it |
| +добавлено | // and burns as an opaque black flame with fire's own motion), with a violet corona and embers. |
| +добавлено | // Charge: the caster's orb dims as if its light were being drawn off, violet motes converge on |
| +добавлено | // the target and black licks start to smoulder at its feet while a void sigil draws itself. |
| +добавлено | // Release: the flame erupts into a roaring column round the target (overshooting, then settling), |
| +добавлено | // cinders burst out with weight and paint corruption where they land. |
| +добавлено | // Impact: the column burns. Its body is a cold, opaque black medium (faintly sun-lit, so it keeps |
| +добавлено | // its form); only its thin outer skin burns violet, so it reads black face-on and glows at the |
| +добавлено | // silhouette and the tongue tips, white-violet at the roots. Black licks tear off it, violet embers |
| +добавлено | // spiral up a vortex, black ash rises, a violet line light inside throws the dummy's shadow, an |
| +добавлено | // attractor drags loose particles in and swallows them, the dummy burns and corrupts. |
| +добавлено | // Aftermath: the column gutters from the top down into low black flames on a scar of char and |
| +добавлено | // corruption, with absorbing smoke and the last embers drifting. |
| +добавлено | // |
| +добавлено | // The flame mass is isolated in flameMass below (acquire / drive / release) so it can move onto |
| +добавлено | // the shared FluidVolume's absorbing mode when that is ready; everything else drives it through |
| +добавлено | // those three calls. |
| +добавлено | |
| +добавлено | const MAX = 2; |
| +добавлено | |
| +добавлено | // --- flame mass: raymarched capsule (fx.volumes) ----------------------------------------------- |
| +добавлено | const MASS_GLSL = /* glsl */` |
| +добавлено | uniform vec4 uBF; // radius m, height m, time s, seed |
| +добавлено | uniform vec4 uBF2; // skin gain, swirl, die (0 -> 1), body density |
| +добавлено | uniform vec3 uBFBase; // floor centre |
| +добавлено | uniform vec3 uBFRim; // corona colour (linear) |
| +добавлено | uniform vec3 uBFCore; // root colour (linear) |
| +добавлено | void volumeSample(vec3 pw, vec3 pl, out float sigma, out vec3 albedo, out vec3 emission) { |
| +добавлено | sigma = 0.0; albedo = vec3(0.0); emission = vec3(0.0); |
| +добавлено | float R = uBF.x, H = uBF.y, time = uBF.z, die = uBF2.z; |
| +добавлено | vec3 q = pw - uBFBase; |
| +добавлено | float h = q.y / H; |
| +добавлено | if (h < -0.05 || h > 1.35) return; |
| +добавлено | // profile: a wide root hugging the target, necking in, tongues tearing off above |
| +добавлено | float rad = R * (0.62 - 0.42 * h + 0.3 * exp(-h * 6.0)); |
| +добавлено | float amp = R * (0.28 + 0.55 * h); |
| +добавлено | float d0 = rad - length(q.xz); |
| +добавлено | if (d0 < -amp) return; |
| +добавлено | // a frame that turns with the vortex, faster low down; band-limited rising noise as fire.js |
| +добавлено | float ang = uBF2.y * time * (1.5 - 0.8 * h); |
| +добавлено | float c = cos(ang), s = sin(ang); |
| +добавлено | vec2 xz = vec2(c * q.x - s * q.z, s * q.x + c * q.z); |
| +добавлено | // stretched upwards (flame tongues are tall), rising fast; a slow lobe octave and a detail octave |
| +добавлено | float f = 0.36 / R; |
| +добавлено | vec3 np = vec3(xz.x, q.y * 0.4 - time * 2.2, xz.y) * f + uBF.w; |
| +добавлено | np += curl3(np * 0.45 + vec3(0.0, -0.3 * time, uBF.w)) * 0.35; |
| +добавлено | float n1 = noise3(np), n2 = noise3(np * vec3(2.2, 1.6, 2.2) + vec3(0.31, -0.7 * f * time, 0.17)); |
| +добавлено | float detail = 1.0 - smoothstep(0.35, 0.9, gVolDt / R); |
| +добавлено | float d = d0 + (n1 - 0.5) * 2.2 * amp + (n2 - 0.5) * 0.7 * amp * detail - die * R * 0.9; |
| +добавлено | // the top tears into separate tongues: the cut-off height itself is noise |
| +добавлено | float top = 1.0 - smoothstep(0.55, 1.0, h + (n1 - 0.5) * 1.1 + (n2 - 0.5) * 0.35 + die * 0.6); |
| +добавлено | float body = smoothstep(0.0, 0.3 * R, d) * top; |
| +добавлено | // the burning skin: a thin glow just outside the black surface (sharp on the inside) |
| +добавлено | float skin = (d < 0.0 ? exp(d / (0.1 * R)) : exp(-d / (0.02 * R))) * top; |
| +добавлено | if (body + skin < 2e-3) return; |
| +добавлено | // it burns at the column's outline, the tongue tips and the roots, and hardly where the camera |
| +добавлено | // looks straight into the black (so the body reads black face-on, like the sodium flame) |
| +добавлено | vec2 vd = normalize(cameraPosition.xz - pw.xz + 1e-4), rd = q.xz / max(length(q.xz), 1e-3); |
| +добавлено | float sil = 1.0 - abs(dot(rd, vd)); |
| +добавлено | float root = exp(-max(q.y, 0.0) / 0.22); |
| +добавлено | float sil2 = sil * sil; |
| +добавлено | float burn = 2.4 * sil2 * sil2 + 0.6 * sil2 * smoothstep(0.6, 1.0, h) + 1.6 * root; |
| +добавлено | float sb = body * 11.0 * uBF2.w, ss = skin * 0.5; |
| +добавлено | sigma = (sb + ss) / R; |
| +добавлено | vec3 L = mix(uBFRim, uBFCore, 0.6 * root) * (0.3 + 1.4 * n2 * n2) * burn; |
| +добавлено | emission = ss * L * uBF2.x / R; |
| +добавлено | // albedo stays 0: a pure absorber (the violet light inside would otherwise light it up), and |
| +добавлено | // zero-albedo samples skip the lighting taps entirely |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | function makeFlameMass(fx, count) { |
| +добавлено | const rim = new THREE.Color(), core = new THREE.Color(); |
| +добавлено | const list = []; |
| +добавлено | for (let i = 0; i < count; i++) { |
| +добавлено | const h = fx.volumes.add({ |
| +добавлено | shape: 'capsule', glsl: MASS_GLSL, maxSteps: 28, |
| +добавлено | uniforms: { uBF: { value: new THREE.Vector4() }, uBF2: { value: new THREE.Vector4() }, uBFBase: { value: new THREE.Vector3() }, uBFRim: { value: rim }, uBFCore: { value: core } }, |
| +добавлено | }); |
| +добавлено | h.g = 0.2; h.ambient = 1; h.sun = 1; |
| +добавлено | h.busy = false; |
| +добавлено | list.push(h); |
| +добавлено | } |
| +добавлено | return { |
| +добавлено | rim, core, |
| +добавлено | acquire() { for (const h of list) if (!h.busy) { h.busy = true; h.visible = false; return h; } return null; }, |
| +добавлено | // base: floor point; R radius, H height (m); die 0..1 gutters it from the top down |
| +добавлено | drive(h, base, R, H, time, seed, skin, swirl, die, density) { |
| +добавлено | h.visible = H > 0.05 && die < 1; |
| +добавлено | if (!h.visible) return; |
| +добавлено | const ra = R * 1.6, hy = Math.max((1.3 * H + ra) * 0.5, ra); |
| +добавлено | h.position.set(base.x, 1.3 * H - hy, base.z); |
| +добавлено | h.scale.set(ra, hy, ra); |
| +добавлено | h.uniforms.uBF.value.set(R, H, time, seed); |
| +добавлено | h.uniforms.uBF2.value.set(skin, swirl, die, density); |
| +добавлено | h.uniforms.uBFBase.value.set(base.x, 0, base.z); |
| +добавлено | }, |
| +добавлено | release(h) { h.visible = false; h.busy = false; }, |
| +добавлено | }; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- module scratch ------------------------------------------------------------------------------ |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _col = new THREE.Color(); |
| +добавлено | const SPLAT = { x: 0, z: 0, radius: 1, char: 0, corruption: 0, glow: 0, pattern: 'noise', seed: 0, width: 0 }; |
| +добавлено | const SHOCK = { pos: new THREE.Vector3(), radius0: 0.3, radius1: 4, duration: 0.5, thickness: 0.7, distortion: 0.05, intensity: 0, ground: true, color: null }; |
| +добавлено | const SIGIL = { pos: new THREE.Vector3(), radius: 1.8, style: 'void', intensity: 0, reveal: 0, seed: 0, color: null }; |
| +добавлено | const clamp01 = (x) => Math.min(1, Math.max(0, x)); |
| +добавлено | const smooth = (a, b, x) => { const t = clamp01((x - a) / (b - a)); return t * t * (3 - 2 * t); }; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'black-flame', name: 'Black Flame', element: 'hybrid', order: 70, hue: '#7a2dff', |
| +добавлено | shape: 'area', radius: 0.4, |
| +добавлено | params: { size: 1, height: 1, intensity: 1, absorb: 1, embers: 1, swirl: 1, smoke: 1, corruption: 1, knockback: 1, rim: '#8a3dff', core: '#efdcff' }, |
| +добавлено | ranges: { size: [0.5, 1.8, 0.01], height: [0.5, 1.8, 0.01], intensity: [0, 3, 0.01], absorb: [0.2, 2, 0.01], embers: [0, 3, 0.01], swirl: [0, 3, 0.01], smoke: [0, 3, 0.01], corruption: [0, 3, 0.01], knockback: [0, 3, 0.01] }, |
| +добавлено | presets: { |
| +добавлено | hellmouth: { rim: '#ff3a1e', core: '#ffd9b8', embers: 1.6, swirl: 1.4 }, |
| +добавлено | 'soul fire': { rim: '#2dffc0', core: '#dafff2', absorb: 0.8, smoke: 0.5 }, |
| +добавлено | eclipse: { rim: '#ffae3a', core: '#fff3d6', absorb: 1.6, intensity: 1.4, height: 1.25, embers: 0.6 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 1.0, impact: 3.0, linger: 2.2 }, |
| +добавлено | kinds: { |
| +добавлено | // black licks that tear off the column: absorbing, no glow, buoyant, curling, eroding away |
| +добавлено | lick: { style: 'media', absorbing: true, lightResponse: 0.1, drag: 2.6, buoyancy: 4.4, curl: 3.0, curlScale: 0.55, forceResponse: 0, life: [0.3, 0.7], size: [0.16, 0.42], sizePow: 0.6, color: ['#030206', '#010102'], alpha: [0.95, 0], density: 9, erosion: 1.7, fadeIn: 0.08, fadeOut: 0.45 }, |
| +добавлено | // the corona round the licks: violet glowing media, short-lived |
| +добавлено | corona: { style: 'media', emissive: 3, drag: 3.2, buoyancy: 3.6, curl: 3.2, curlScale: 0.5, forceResponse: 0, life: [0.2, 0.5], size: [0.14, 0.32], sizePow: 0.7, color: ['#c9a0ff', '#6a20ff'], alpha: [0.7, 0], density: 4, erosion: 1.6, fadeIn: 0.05, fadeOut: 0.5 }, |
| +добавлено | // violet embers, as bonfire embers: light, buoyant, carried on the curling draught (high drag, so |
| +добавлено | // the air not their launch decides where they go), twinkling as they tumble, and cooling the way |
| +добавлено | // a blackbody does (bright, then fast to a dull deep violet) instead of fading flat |
| +добавлено | ember: { style: 'glow', drag: 1.7, buoyancy: 2.3, gravity: 1.1, curl: 5.5, curlScale: 0.75, restitution: 0.3, friction: 0.4, life: [1.4, 3.2], size: [0.026, 0.01], sizePow: 0.5, stretch: 0.03, emissive: 16, flicker: 0.65, color: ['#b47dff', '#2a0a5c'], alpha: [1, 0.15], fadeIn: 0.1, fadeOut: 0.35, paints: 'corruption', paintAmount: 0.04 }, |
| +добавлено | // heavy cinders flung by the eruption: violet coals with weight, short motion blur, no white |
| +добавлено | cinder: { style: 'glow', drag: 0.6, gravity: 9.8, restitution: 0.35, friction: 0.3, forceResponse: 0.4, life: [0.6, 1.5], size: [0.03, 0.012], stretch: 0.012, emissive: 11, flicker: 0.5, color: ['#9a55ff', '#260850'], alpha: [1, 0.2], paints: 'corruption', paintAmount: 0.06 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const mass = makeFlameMass(fx, MAX); |
| +добавлено | const lickEm = P.emitter('black-flame.lick', { shape: 'shell', radius: 0.45, spread: 0.5, radial: 0.7 }); |
| +добавлено | const coronaEm = P.emitter('black-flame.corona', { shape: 'shell', radius: 0.5, spread: 0.5, radial: 0.8 }); |
| +добавлено | const emberEm = P.emitter('black-flame.ember', { shape: 'shell', radius: 0.6, spread: 0.8, radial: 1.1 }); |
| +добавлено | const cinderEm = P.emitter('black-flame.cinder', { shape: 'sphere', radius: 0.4, spread: 1.6, radial: 3.2 }); |
| +добавлено | const groundEm = P.emitter('black-flame.lick', { shape: 'disk', radius: 1.1, spread: 0.3 }); |
| +добавлено | const moteIn = P.emitter('void.mote', { shape: 'shell', radius: 2.6, spread: 0.3, radial: -2.2 }); |
| +добавлено | const ashEm = P.emitter('void.ash', { shape: 'sphere', radius: 0.6, spread: 0.6 }); |
| +добавлено | const smokeEm = P.emitter('void.smoke', { shape: 'sphere', radius: 0.6, spread: 0.5 }); |
| +добавлено | |
| +добавлено | const rim = mass.rim, core = mass.core; |
| +добавлено | // per-cast state, allocated here (the cast record only holds a slot) |
| +добавлено | const slots = Array.from({ length: MAX }, () => ({ busy: false, base: new THREE.Vector3() })); |
| +добавлено | const takeSlot = () => { for (const s of slots) if (!s.busy) { s.busy = true; return s; } return null; }; |
| +добавлено | // kind colours follow the rim / core params (texels, no recompile) |
| +добавлено | const KC = { corona: { color: [new THREE.Color(), new THREE.Color()] }, ember: { color: [new THREE.Color(), new THREE.Color()] }, cinder: { color: [new THREE.Color(), new THREE.Color()] } }; |
| +добавлено | let lastRim = '', lastCore = ''; |
| +добавлено | function retint() { |
| +добавлено | const p = S.params; |
| +добавлено | if (p.rim === lastRim && p.core === lastCore) return; |
| +добавлено | lastRim = p.rim; lastCore = p.core; |
| +добавлено | rim.set(p.rim); core.set(p.core); |
| +добавлено | KC.corona.color[0].copy(core).lerp(rim, 0.5); KC.corona.color[1].copy(rim).multiplyScalar(0.7); |
| +добавлено | // embers and cinders burn in the rim's own hue (the core colour only tints the hottest); they |
| +добавлено | // cool to a dark ember of it, never to white |
| +добавлено | KC.ember.color[0].copy(rim).lerp(core, 0.3); KC.ember.color[1].copy(rim).multiplyScalar(0.18); |
| +добавлено | KC.cinder.color[0].copy(rim).lerp(core, 0.1); KC.cinder.color[1].copy(rim).multiplyScalar(0.15); |
| +добавлено | for (const k in KC) P.setKind(`black-flame.${k}`, KC[k]); |
| +добавлено | } |
| +добавлено | retint(); |
| +добавлено | |
| +добавлено | function splat(x, z, radius, char, corruption, pattern, seed, width = 0) { |
| +добавлено | SPLAT.x = x; SPLAT.z = z; SPLAT.radius = radius; SPLAT.char = char; SPLAT.corruption = corruption; SPLAT.glow = 0; |
| +добавлено | SPLAT.pattern = pattern; SPLAT.seed = seed % 100; SPLAT.width = width; |
| +добавлено | fx.ground.splat(SPLAT); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the column's height over the cast's clock: a low smoulder late in the charge, an overshooting |
| +добавлено | // eruption, a roar, then it gutters |
| +добавлено | function heightAt(c, p) { |
| +добавлено | const T = c.age, Tc = c.durations.charge, Hm = 4.4 * p.height * p.size; |
| +добавлено | if (T < Tc) return 0.45 * p.size * smooth(0.45, 1.0, T); |
| +добавлено | const r = T - Tc; |
| +добавлено | return Hm * (1 - Math.exp(-r * 7)) * (1 + 0.18 * Math.exp(-r * 2.5) * Math.sin(r * 8)); |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'rim' || k === 'core') retint(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = c.data; |
| +добавлено | retint(); |
| +добавлено | if (name === 'charge') { |
| +добавлено | d.s = takeSlot(); |
| +добавлено | d.base = d.s ? d.s.base.set(c.to.x, 0, c.to.z) : null; |
| +добавлено | if (!d.base) return; |
| +добавлено | d.mass = mass.acquire(); d.light = fx.lights.acquire(); |
| +добавлено | d.vortex = fx.forces.acquire(); d.attr = fx.forces.acquire(); |
| +добавлено | SIGIL.pos.set(d.base.x, 0.03, d.base.z); SIGIL.radius = 1.9 * p.size; SIGIL.seed = c.seed % 997; SIGIL.color = rim; SIGIL.intensity = 0; SIGIL.reveal = 0; |
| +добавлено | d.sigil = fx.sigils.acquire(SIGIL); |
| +добавлено | d.hitT = 0.1; d.ring = false; d.flick = 0; |
| +добавлено | if (d.light) { d.light.type = 1; d.light.range = 8 * p.size; d.light.intensity = 0; d.light.shadow = true; d.light.color.copy(rim); } |
| +добавлено | if (d.vortex) { d.vortex.type = 1; d.vortex.dir.set(0, 1, 0); d.vortex.p[0] = 0.6 * p.size; d.vortex.p[1] = 0; d.vortex.p[2] = 0; d.vortex.p[3] = 0; d.vortex.w = 5; } |
| +добавлено | if (d.attr) { d.attr.type = 2; d.attr.p[0] = 0; d.attr.p[1] = 0.5; d.attr.p[2] = 0.35; d.attr.p[3] = 4.5 * p.size; } |
| +добавлено | } else if (name === 'release') { |
| +добавлено | const b = d.base; |
| +добавлено | if (!b) return; |
| +добавлено | fx.rig.shake(0.3, 10); |
| +добавлено | if (!fx.reduced) fx.rig.punch(1.2, 0.08); |
| +добавлено | SHOCK.pos.copy(b); SHOCK.radius1 = 4.5 * p.size; SHOCK.distortion = 0.05; fx.shocks?.spawn(SHOCK); |
| +добавлено | cinderEm.pos.set(b.x, 0.4, b.z); cinderEm.vel.set(0, 3.5, 0); cinderEm.burst(90 * p.embers); |
| +добавлено | emberEm.pos.set(b.x, 0.5, b.z); emberEm.vel.set(0, 2.6, 0); emberEm.radius = 0.6 * p.size; emberEm.burst(260 * p.embers); |
| +добавлено | lickEm.pos.set(b.x, 0.5, b.z); lickEm.vel.set(0, 3.5, 0); lickEm.radius = 0.5 * p.size; lickEm.size = p.size; lickEm.burst(60); |
| +добавлено | splat(b.x, b.z, 2.2 * p.size, 0.6, 1.4 * p.corruption, 'radial', c.seed); |
| +добавлено | fx.impacts.add('corruption', _a.set(b.x, 0.4, b.z), 2.4 * p.size, 1, 8, rim); |
| +добавлено | if (c.target >= 0) { |
| +добавлено | const dm = fx.dummies[c.target]; |
| +добавлено | dm.hit(_a.copy(c.dir).setY(0).normalize(), 0.35 * p.knockback); |
| +добавлено | } |
| +добавлено | } else if (name === 'done') { |
| +добавлено | if (d.mass) mass.release(d.mass); |
| +добавлено | d.light?.release(); d.vortex?.release(); d.attr?.release(); d.sigil?.release(); |
| +добавлено | d.mass = d.light = d.vortex = d.attr = d.sigil = null; |
| +добавлено | if (d.s) d.s.busy = false; |
| +добавлено | d.s = d.base = null; |
| +добавлено | fx.arena.setFocus(_col.set(S.hue), 2.5); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = c.data, b = d.base; |
| +добавлено | if (!b) return; |
| +добавлено | const T = c.age, Tc = c.durations.charge, Ti = c.durations.impact, R = 0.8 * p.size; |
| +добавлено | const rel = T - Tc, tl = T - Tc - Ti; // since release; since the burn ended |
| +добавлено | const die = smooth(0, 1.5, tl), on = 1 - die; |
| +добавлено | const H = heightAt(c, p) * (1 - 0.55 * die); |
| +добавлено | d.flick = 0.85 + 0.15 * Math.sin(T * 31 + Math.sin(T * 13) * 2) * Math.sin(T * 7.3); |
| +добавлено | if (d.mass) mass.drive(d.mass, b, R, Math.max(H, 0.06), T, (c.seed % 83) * 0.41, 4 * p.intensity * d.flick, 1.3 * p.swirl, die, p.absorb); |
| +добавлено | |
| +добавлено | // the orb's light is drawn off while the flame burns, and comes back as it dies |
| +добавлено | fx.arena.setFocus(rim, 0.15 + 2.4 * (1 - smooth(0.05, 0.85, T)) + 2.2 * smooth(0.6, 2.0, tl)); |
| +добавлено | |
| +добавлено | if (d.sigil) { |
| +добавлено | d.sigil.reveal = smooth(0, 0.85, T); |
| +добавлено | d.sigil.intensity = 1.6 * smooth(0, 0.3, T) * (1 - smooth(0.2, 1.6, tl)); |
| +добавлено | d.sigil.rotation += dt * 0.35; |
| +добавлено | } |
| +добавлено | if (d.light) { |
| +добавлено | d.light.pos.set(b.x, 0.25, b.z); d.light.end.set(b.x, 0.25 + Math.max(0.2, H * 0.55), b.z); |
| +добавлено | d.light.color.copy(rim); |
| +добавлено | d.light.intensity = (T < Tc ? 4 * smooth(0.4, 1, T) : 16 * p.intensity * smooth(0, 0.12, rel) * on) * d.flick; |
| +добавлено | } |
| +добавлено | if (d.vortex) { d.vortex.pos.copy(b); d.vortex.p[1] = 7 * p.swirl * on * smooth(0, 0.3, rel); d.vortex.p[2] = 3.5 * on * smooth(0, 0.3, rel); d.vortex.p[3] = 1.4 * on; d.vortex.w = H + 1; } |
| +добавлено | if (d.attr) { d.attr.pos.set(b.x, 0.9, b.z); d.attr.p[0] = 4 * on * smooth(0.3, 1.0, T); } |
| +добавлено | |
| +добавлено | if (T < Tc) { |
| +добавлено | // anticipation: motes converge, the ground starts to smoulder, corruption creeps in |
| +добавлено | const k = smooth(0.15, 1, T / Tc); |
| +добавлено | moteIn.pos.set(b.x, 0.5, b.z); moteIn.radius = 2.6 * p.size; moteIn.rate(700 * k, dt); |
| +добавлено | groundEm.pos.set(b.x, 0.05, b.z); groundEm.radius = 0.9 * p.size; groundEm.vel.set(0, 0.8, 0); groundEm.size = 0.6 * p.size; groundEm.rate(40 * k, dt); |
| +добавлено | if (!d.ring && T > 0.25) { d.ring = true; splat(b.x, b.z, 1.9 * p.size, 0, 0.9 * p.corruption, 'ring', c.seed + 7, 0.3); } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the burn: licks and corona tear off the column at random heights, embers spiral up, ash rises |
| +добавлено | const burn = on * smooth(0, 0.1, rel); |
| +добавлено | if (burn > 0.01) { |
| +добавлено | // licks leave from the upper half, where the tongues tear off |
| +добавлено | const y = H * (0.6 + 0.4 * fx.rand()), w = Math.max(0.15, 1 - y / Math.max(H, 0.1)); |
| +добавлено | lickEm.pos.set(b.x, y, b.z); lickEm.radius = R * (0.3 + 0.4 * w); lickEm.vel.set(0, 2.4, 0); lickEm.size = p.size; lickEm.rate(40 * burn, dt); |
| +добавлено | coronaEm.pos.set(b.x, H * (0.75 + 0.3 * fx.rand()), b.z); coronaEm.radius = 0.35 * R; coronaEm.vel.set(0, 2.2, 0); coronaEm.size = 0.7 * p.size; coronaEm.rate(30 * burn * p.intensity, dt); |
| +добавлено | emberEm.pos.set(b.x, 0.3 + fx.rand() * H * 0.5, b.z); emberEm.radius = 0.7 * R; emberEm.vel.set(0, 1.4, 0); emberEm.rate(150 * p.embers * burn, dt); |
| +добавлено | ashEm.pos.set(b.x, H * 0.75, b.z); ashEm.vel.set(0, 1.6, 0); ashEm.rate(30 * p.smoke * burn, dt); |
| +добавлено | } |
| +добавлено | if (tl > 0) { |
| +добавлено | // aftermath: low black flames on the scar, smoke, the last embers |
| +добавлено | const k = 1 - smooth(0.3, 2.0, tl); |
| +добавлено | groundEm.pos.set(b.x, 0.05, b.z); groundEm.radius = 1.0 * p.size; groundEm.vel.set(0, 0.9, 0); groundEm.size = 0.7 * p.size; groundEm.rate(55 * k, dt); |
| +добавлено | emberEm.pos.set(b.x, 0.2, b.z); emberEm.radius = 0.9 * R; emberEm.vel.set(0, 0.8, 0); emberEm.rate(80 * p.embers * k, dt); |
| +добавлено | smokeEm.pos.set(b.x, Math.max(0.6, H * 0.8), b.z); smokeEm.vel.set(0, 1.1, 0); smokeEm.size = 0.35 * p.size; smokeEm.rate(4 * p.smoke * (1 - smooth(0.4, 1.6, tl)), dt); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the dummy burns: corrupt, char, short shoves as the flame flares |
| +добавлено | d.hitT -= dt; |
| +добавлено | if (d.hitT <= 0 && burn > 0.2) { |
| +добавлено | d.hitT = 0.3; |
| +добавлено | if (c.target >= 0) { |
| +добавлено | const dm = fx.dummies[c.target]; |
| +добавлено | if (Math.hypot(dm.pos.x - b.x, dm.pos.z - b.z) < 1.6 * p.size) { |
| +добавлено | dm.status.corrupt = Math.max(dm.status.corrupt, 1); |
| +добавлено | dm.status.burn = Math.max(dm.status.burn, 0.3); // char, little ember heat |
| +добавлено | dm.hit(_b.set(fx.rand() - 0.5, 0, fx.rand() - 0.5).normalize(), 0.08 * p.knockback); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | splat(b.x + (fx.rand() - 0.5) * 1.6 * R, b.z + (fx.rand() - 0.5) * 1.6 * R, 0.9 * p.size, 0.35, 0.5 * p.corruption, 'noise', c.seed + T * 37); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { blackbodyRGB } from '../../vfx/noise.js'; |
| +добавлено | import { NOISE_GLSL } from '../../vfx/common.js'; |
| +добавлено | |
| +добавлено | // Fire Tornado (SPELLBOOK hybrid, fire + wind). Anticipation: a two-armed spiral burns inward across |
| +добавлено | // the floor at the target like a fuse (heat + char, so ground flames sprout along it), hot air |
| +добавлено | // spirals in at floor level and embers lift. Eruption: the funnel bursts up out of the spiral's |
| +добавлено | // eye with a ground shock. Sustain: a Rankine vortex joins the shared force field (every loose |
| +добавлено | // particle of every spell is dragged round and up), 12 flame-shaded analytic helix ribbons wind up |
| +добавлено | // the funnel (licking tongues racing upward, cooling to soot at the top; half-res glow + heat haze, |
| +добавлено | // crisp cores at full res), media flames are flung round and up it, embers spiral up and rain out |
| +добавлено | // of the top, smoke rolls off the crown, heat-haze sprites spiral round it and a dust skirt churns |
| +добавлено | // at its foot. It wanders a little, scorches a ring under itself, sets dummies burning and spins |
| +добавлено | // them, and swirls the pool if it gets close. Death: the spin winds down, the flame fails from the |
| +добавлено | // top, the crown turns to smoke and the embers fall. |
| +добавлено | |
| +добавлено | const NH = 12; // helix flame ribbons per tornado (fx: glow + haze) |
| +добавлено | const NC = 6; // of those, crisp full-res cores |
| +добавлено | const NA = 2; // spiral arms scorched into the floor |
| +добавлено | const TAU = Math.PI * 2; |
| +добавлено | const TH = 3.3 * Math.PI; // spiral sweep (rad) |
| +добавлено | |
| +добавлено | // p0 = foot xyz, p1 = (height, radius foot, radius top, turns), p2 = (angle, lean x, lean z, width), |
| +добавлено | // p3 = (seed, reveal, distortion m, heat) |
| +добавлено | const PATH_GLSL = /* glsl */` |
| +добавлено | vec3 ribbonPath(float t, vec4 p0, vec4 p1, vec4 p2, vec4 p3, out float width) { |
| +добавлено | float r = mix(p1.y, p1.z, pow(t, 1.5)) * (1.0 + 0.1 * sin(t * 7.0 + p3.x * 6.2831853 + uTime * 3.1)); |
| +добавлено | float th = p2.x + t * p1.w * 6.2831853; |
| +добавлено | width = p2.w * mix(0.55, 1.5, t) * smoothstep(0.0, 0.05, t); |
| +добавлено | return p0.xyz + vec3(p2.y, 0.0, p2.z) * (t * t) + vec3(cos(th) * r, t * p1.x, sin(th) * r); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const SHADE_GLSL = NOISE_GLSL + /* glsl */` |
| +добавлено | uniform vec3 uHot; |
| +добавлено | uniform vec3 uMid; |
| +добавлено | uniform vec3 uCool; |
| +добавлено | vec3 fireRamp(float h) { return h < 0.5 ? mix(uCool, uMid, h * 2.0) : mix(uMid, uHot, h * 2.0 - 1.0); } |
| +добавлено | vec3 ribbonShade(RibbonFrag r, out vec3 media, out float alpha, out float distortion) { |
| +добавлено | float u = r.uv.x, v = r.uv.y, av = abs(v), seed = r.p3.x; |
| +добавлено | // tongues of flame racing up the ribbon |
| +добавлено | float n = noise3(vec3(u * 0.8 - uTime * 0.7 + seed, av * 0.08 + seed * 0.3, uTime * 0.05)); |
| +добавлено | float n2 = noise3(vec3(u * 2.1 - uTime * 1.6 + seed * 1.9, v * 0.12, 0.4 + seed)); |
| +добавлено | float fl = n * 0.6 + n2 * 0.4; |
| +добавлено | float ends = smoothstep(0.0, 0.04, u) * (1.0 - smoothstep(r.p3.y - 0.25, r.p3.y, u)); |
| +добавлено | float h = clamp(((1.0 - u) * 1.05 + (fl - 0.5) * 0.6 - av * 0.35) * r.p3.w, 0.0, 1.0); |
| +добавлено | media = vec3(0.0); alpha = 0.0; distortion = 0.0; |
| +добавлено | #ifdef RIBBON_FX |
| +добавлено | float tongue = smoothstep(0.32, 0.72, fl + (1.0 - u) * 0.22); |
| +добавлено | float edge = smoothstep(0.0, 0.5, (1.0 - av) - (fl - 0.45) * 1.2); |
| +добавлено | float g = edge * tongue * exp(-1.2 * v * v) * ends; |
| +добавлено | // where the flame has cooled at the crown it is soot: dark, occluding |
| +добавлено | // and the flame itself covers what is behind it (additive-only orange over sky reads pink) |
| +добавлено | media = vec3(0.03, 0.025, 0.021); |
| +добавлено | alpha = max(0.3 * smoothstep(0.5, 0.95, u) * edge * ends * smoothstep(0.35, 0.65, n), min(g * 0.4, 0.65)); |
| +добавлено | distortion = r.p3.z * v * exp(-3.0 * v * v) * 2.33 * ends; |
| +добавлено | return fireRamp(h) * r.color * g; |
| +добавлено | #else |
| +добавлено | float core = exp(-5.0 * v * v) * smoothstep(0.38, 0.8, fl + (1.0 - u) * 0.2) * ends; |
| +добавлено | return fireRamp(clamp(h + 0.2, 0.0, 1.0)) * r.color * core; |
| +добавлено | #endif |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // flame-ish kinds, retinted from temperature + tint like fire.js: [kind, hot x T, cold x T] |
| +добавлено | const HOT_KINDS = [['flame', 1.25, 0.38], ['ember', 1.1, 0.5], ['draft', 1.2, 0.55], ['spark', 1.45, 0.6]]; |
| +добавлено | const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); }; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'fire-tornado', name: 'Fire Tornado', element: 'hybrid', order: 20, hue: '#ff6a1a', |
| +добавлено | shape: 'area', radius: 0.5, |
| +добавлено | params: { size: 1, height: 1, spin: 1, duration: 4.5, wander: 1, temperature: 1900, intensity: 1, flames: 1, embers: 1, smoke: 1, dust: 1, haze: 1, scorch: 1, tint: '#ffffff' }, |
| +добавлено | ranges: { |
| +добавлено | size: [0.4, 2, 0.01], height: [0.4, 1.8, 0.01], spin: [0.2, 2.5, 0.01], duration: [1.5, 10, 0.1], wander: [0, 3, 0.01], temperature: [900, 6500, 10], |
| +добавлено | intensity: [0, 3, 0.01], flames: [0, 3, 0.01], embers: [0, 3, 0.01], smoke: [0, 3, 0.01], dust: [0, 3, 0.01], haze: [0, 3, 0.01], scorch: [0, 2, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | firestorm: { size: 1.35, height: 1.3, spin: 1.4, embers: 1.8, smoke: 1.5, duration: 6 }, |
| +добавлено | 'blue whirl': { temperature: 6500, tint: '#8fb8ff', smoke: 0.2, dust: 0.4, spin: 1.6, height: 0.8 }, |
| +добавлено | 'hell spout': { temperature: 1450, tint: '#ff5a4a', smoke: 2.2, flames: 1.4, haze: 1.6 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 1.0, impact: 0.3, linger: 4.5 }, |
| +добавлено | kinds: { |
| +добавлено | // big licks the vortex flings round and up the funnel |
| +добавлено | flame: { style: 'media', drag: 1.6, buoyancy: 3.0, curl: 2.4, curlScale: 0.45, forceResponse: 1.6, life: [0.3, 0.65], size: [0.14, 0.5], sizePow: 0.6, emissive: 1.1, temp: [2375, 720], toSmokeAt: 0.5, smokeAlbedo: 0.04, alpha: [0.8, 0], density: 8, distortion: 0.8, erosion: 1.8, fadeIn: 0.06, fadeOut: 0.3 }, |
| +добавлено | ember: { style: 'glow', drag: 0.4, buoyancy: 0.9, gravity: 1.6, curl: 1.6, curlScale: 0.5, forceResponse: 2.2, life: [1.4, 3.2], size: [0.03, 0.012], stretch: 0.04, emissive: 4, temp: [2090, 950], restitution: 0.3, friction: 0.4, paints: 'char', paintAmount: 0.04 }, |
| +добавлено | // hot air drawn in along the floor while it gathers |
| +добавлено | draft: { style: 'glow', drag: 0.6, curl: 1.0, curlScale: 0.3, forceResponse: 2.2, life: [0.35, 0.7], size: [0.028, 0.008], stretch: 0.07, emissive: 2.4, temp: [2280, 1045], alpha: [0.9, 0], fadeIn: 0.25, fadeOut: 0.4 }, |
| +добавлено | spark: { style: 'glow', drag: 0.5, gravity: 9.8, restitution: 0.4, friction: 0.3, forceResponse: 1.5, life: [0.3, 1.0], size: [0.024, 0.008], stretch: 0.02, emissive: 6, temp: [2750, 1140], paints: 'heat', paintAmount: 0.04 }, |
| +добавлено | // the smoke column the flame turns into: lit grey-brown soot that keeps rotating as it rises |
| +добавлено | smoke: { style: 'media', emissive: 0, drag: 1.2, buoyancy: 1.8, curl: 1.8, curlScale: 0.35, forceResponse: 1.2, life: [2.4, 4.4], size: [0.5, 2.6], sizePow: 0.5, color: ['#231e1a', '#4f4741'], alpha: [0.5, 0], density: 2.2, erosion: 1.2, fadeIn: 0.2, fadeOut: 0.45 }, |
| +добавлено | // refraction-only shimmer round the column (density 0: distortion only) |
| +добавлено | haze: { style: 'media', drag: 0.3, forceResponse: 2.5, life: [0.9, 1.5], size: [0.9, 1.8], alpha: [1, 0], density: 0, distortion: 2.6, fadeIn: 0.25, fadeOut: 0.4 }, |
| +добавлено | // ash and grit churned up round the foot: dark, so the fire's own light doesn't turn it into glowing puffs |
| +добавлено | dust: { style: 'media', emissive: 0, drag: 1.2, buoyancy: 0.2, curl: 1.0, curlScale: 0.25, forceResponse: 1.3, life: [0.9, 1.7], size: [0.25, 0.95], color: ['#2e2721', '#463c33'], alpha: [0.3, 0], density: 1.6, erosion: 1.1, fadeIn: 0.15 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const E = { |
| +добавлено | draft: P.emitter('fire-tornado.draft', { shape: 'ring', radius: 3, spread: 0.4, radial: -3 }), |
| +добавлено | lift: P.emitter('fire-tornado.ember', { shape: 'disk', radius: 2.5, spread: 0.6 }), |
| +добавлено | flame: P.emitter('fire-tornado.flame', { shape: 'ring', radius: 1, spread: 0.6, radial: 0.3 }), |
| +добавлено | foot: P.emitter('fire-tornado.flame', { shape: 'ring', radius: 0.7, spread: 0.8, radial: -0.4 }), |
| +добавлено | ember: P.emitter('fire-tornado.ember', { shape: 'ring', radius: 1, spread: 0.8 }), |
| +добавлено | burst: P.emitter('fire-tornado.flame', { shape: 'disk', radius: 0.8, spread: 1.6, radial: 2 }), |
| +добавлено | spark: P.emitter('fire-tornado.spark', { shape: 'sphere', radius: 0.4, spread: 6, radial: 4 }), |
| +добавлено | smoke: P.emitter('fire-tornado.smoke', { shape: 'ring', radius: 1.5, spread: 0.6, radial: 0.8 }), |
| +добавлено | haze: P.emitter('fire-tornado.haze', { shape: 'ring', radius: 1, spread: 0.4 }), |
| +добавлено | skirt: P.emitter('fire-tornado.dust', { shape: 'ring', radius: 1.6, spread: 0.5, radial: 1.2 }), |
| +добавлено | }; |
| +добавлено | |
| +добавлено | const hot = new THREE.Color(), mid = new THREE.Color(), cool = new THREE.Color(); |
| +добавлено | const shared = { uHot: { value: hot }, uMid: { value: mid }, uCool: { value: cool } }; |
| +добавлено | const ribs = new fx.AnalyticRibbons({ max: NH * 3, segments: 64, pathGLSL: PATH_GLSL, shadeGLSL: SHADE_GLSL, target: 'fx', uniforms: shared }); |
| +добавлено | const cores = new fx.AnalyticRibbons({ max: NC * 3, segments: 64, pathGLSL: PATH_GLSL, shadeGLSL: SHADE_GLSL, target: 'lines', uniforms: shared }); |
| +добавлено | fx.scenes.fx.add(ribs.mesh); |
| +добавлено | fx.scenes.lines.add(cores.mesh); |
| +добавлено | |
| +добавлено | const state = Array.from({ length: 16 }, () => ({ |
| +добавлено | helix: new Array(NH).fill(null), cores: new Array(NC).fill(null), vortex: null, light: null, |
| +добавлено | hp: new Float32Array(NH * 5), arm: new Float32Array(NA * 2), |
| +добавлено | centre: new THREE.Vector3(), base: new THREE.Vector3(), prev: new THREE.Vector3(), vel: new THREE.Vector3(), lean: new THREE.Vector3(), |
| +добавлено | env: 0, age: 0, th: TH, nextSplat: 0, hitT: 0, seedF: 0, phase0: 0, H: 0, |
| +добавлено | })); |
| +добавлено | |
| +добавлено | const _a = new THREE.Vector3(), _up = new THREE.Vector3(0, 1, 0), col = new THREE.Color(), tintCol = new THREE.Color(); |
| +добавлено | const SPLAT = { x: 0, z: 0, radius: 1, heat: 0, char: 0, pattern: 'radial', seed: 0, x2: 0, z2: 0, width: 0 }; |
| +добавлено | const SHOCK = { pos: new THREE.Vector3(), radius0: 0.4, radius1: 6, duration: 0.6, thickness: 0.7, distortion: 0.04, intensity: 0, ground: true }; |
| +добавлено | |
| +добавлено | let lastT = -1, lastTint = ''; |
| +добавлено | const HOT = HOT_KINDS.map(([k, a, b]) => ({ name: `fire-tornado.${k}`, a, b, white: { temp: [0, 0] }, tinted: { temp: null, color: [new THREE.Color(), new THREE.Color()] } })); |
| +добавлено | function retint() { |
| +добавлено | const p = S.params; |
| +добавлено | if (p.temperature === lastT && p.tint === lastTint) return; |
| +добавлено | lastT = p.temperature; lastTint = p.tint; |
| +добавлено | tintCol.set(p.tint); |
| +добавлено | const T = p.temperature; |
| +добавлено | blackbodyRGB(T * 1.45, hot).multiply(tintCol).multiplyScalar(0.95); |
| +добавлено | blackbodyRGB(T * 1.0, mid).multiply(tintCol).multiplyScalar(0.5); |
| +добавлено | blackbodyRGB(T * 0.7, cool).multiply(tintCol).multiplyScalar(0.2); |
| +добавлено | const white = tintCol.r > 0.97 && tintCol.g > 0.97 && tintCol.b > 0.97; |
| +добавлено | for (const h of HOT) { |
| +добавлено | if (white) { h.white.temp[0] = T * h.a; h.white.temp[1] = T * h.b; P.setKind(h.name, h.white); } |
| +добавлено | else { |
| +добавлено | blackbodyRGB(T * h.a, h.tinted.color[0]).multiply(tintCol); |
| +добавлено | blackbodyRGB(T * h.b, h.tinted.color[1]).multiply(tintCol).multiplyScalar((h.b / h.a) ** 3); |
| +добавлено | P.setKind(h.name, h.tinted); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | retint(); |
| +добавлено | const lightColor = (T, out) => blackbodyRGB(T, out).multiply(tintCol); |
| +добавлено | |
| +добавлено | function splat(x, z, radius, heat, char, pattern, seed, x2 = x, z2 = z, width = 0) { |
| +добавлено | SPLAT.x = x; SPLAT.z = z; SPLAT.radius = radius; SPLAT.heat = heat; SPLAT.char = char; SPLAT.pattern = pattern; |
| +добавлено | SPLAT.seed = seed; SPLAT.x2 = x2; SPLAT.z2 = z2; SPLAT.width = width; |
| +добавлено | fx.ground.splat(SPLAT); |
| +добавлено | } |
| +добавлено | // funnel radius at normalised height (before size) |
| +добавлено | const rAt = (hn) => 0.4 + 0.85 * Math.pow(hn, 1.5); |
| +добавлено | // --- flame mass: every combusting volume of the spell goes through these three calls (the |
| +добавлено | // eruption, the flames the vortex carries up the column, the roaring foot), so the whole mass |
| +добавлено | // can move onto the shared FluidVolume combustion solver in one place. Particles meanwhile. |
| +добавлено | const flameMass = { |
| +добавлено | erupt(x, z, p) { |
| +добавлено | const sz = p.size; |
| +добавлено | E.burst.pos.set(x, 0.3, z); E.burst.radius = 0.6 * sz; E.burst.vel.set(0, 7, 0); E.burst.size = sz; E.burst.burst(220 * p.flames); |
| +добавлено | }, |
| +добавлено | column(b, lean, H, p, e, dt) { // flames riding the vortex at random heights |
| +добавлено | const sz = p.size; |
| +добавлено | for (let j = 0; j < 3; j++) { |
| +добавлено | const hn = j === 0 ? fx.rand() * 0.25 : fx.rand() * 0.85; |
| +добавлено | E.flame.pos.set(b.x + lean.x * hn * hn, hn * H, b.z + lean.z * hn * hn); |
| +добавлено | E.flame.radius = rAt(hn) * sz * 0.6; |
| +добавлено | E.flame.vel.set(0, 2.5, 0); E.flame.size = sz * (0.7 + 0.8 * hn); |
| +добавлено | E.flame.rate(260 * p.flames * e, dt); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | foot(b, p, e, dt) { |
| +добавлено | const sz = p.size; |
| +добавлено | E.foot.pos.set(b.x, 0.15, b.z); E.foot.radius = 0.75 * sz; E.foot.vel.set(0, 3.5, 0); E.foot.size = sz; |
| +добавлено | E.foot.rate(300 * p.flames * e, dt); |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | // the spiral scorch: radius at angle th (0 = the eye) |
| +добавлено | const spiralR = (th, sz) => (0.3 + 0.9 * th / Math.PI) * sz; |
| +добавлено | |
| +добавлено | function releaseAll(st) { |
| +добавлено | for (let i = 0; i < NH; i++) { st.helix[i]?.release(); st.helix[i] = null; } |
| +добавлено | for (let i = 0; i < NC; i++) { st.cores[i]?.release(); st.cores[i] = null; } |
| +добавлено | st.vortex?.release(); st.vortex = null; |
| +добавлено | st.light?.release(); st.light = null; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- anticipation: the spiral burns inward like a fuse; hot air and embers are drawn in --- |
| +добавлено | function gather(c, st, p, dt) { |
| +добавлено | const k = Math.min(1, c.t / c.durations.charge), sz = p.size, cx = st.centre.x, cz = st.centre.z; |
| +добавлено | const th = TH * (1 - smooth(0, 0.95, k)); |
| +добавлено | // one segment per arm every ~50 ms, overlapping the last so the joins don't bead |
| +добавлено | if (st.th - th > 0.32 || (k >= 1 && st.th > 0)) { |
| +добавлено | for (let a = 0; a < NA; a++) { |
| +добавлено | const o = st.phase0 + a * Math.PI, t0 = Math.min(TH, st.th + 0.12); |
| +добавлено | const r0 = spiralR(t0, sz), r1 = spiralR(th, sz); |
| +добавлено | splat(cx + Math.cos(t0 + o) * r0, cz + Math.sin(t0 + o) * r0, 0.3, 0.95 * p.scorch, 0.85 * p.scorch, 'line', (c.seed + a * 13 + th * 7) % 97, |
| +добавлено | cx + Math.cos(th + o) * r1, cz + Math.sin(th + o) * r1, (0.12 + 0.1 * th / TH) * sz); |
| +добавлено | // sparks spit where the fuse burns |
| +добавлено | E.spark.pos.set(cx + Math.cos(th + o) * r1, 0.1, cz + Math.sin(th + o) * r1); E.spark.vel.set(0, 2, 0); E.spark.burst(14 * p.embers); |
| +добавлено | } |
| +добавлено | st.th = th; |
| +добавлено | } |
| +добавлено | E.draft.pos.set(cx, 0.15, cz); E.draft.radius = 3.2 * sz; E.draft.rate(500 * p.flames * (0.3 + 0.7 * k), dt); |
| +добавлено | E.lift.pos.set(cx, 0.1, cz); E.lift.radius = 2.4 * sz; E.lift.vel.set(0, 1.4, 0); E.lift.rate(160 * p.embers * k, dt); |
| +добавлено | const V = st.vortex; |
| +добавлено | if (V) { V.pos.set(cx, 0, cz); V.dir.copy(_up); V.p[0] = 1.0 * sz; V.p[1] = 10 * sz * k * p.spin; V.p[2] = 1.5 * k; V.p[3] = 1.5 * k; V.w = 3; } |
| +добавлено | if (st.light) { st.light.pos.set(cx, 0.4, cz); st.light.end.set(cx, 0.6, cz); st.light.intensity = 10 * k * p.intensity; } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- the funnel --- |
| +добавлено | function place(c, st, p, dt) { |
| +добавлено | const b = st.base, w = p.wander * p.size, a = st.age; |
| +добавлено | st.prev.copy(b); |
| +добавлено | b.set(st.centre.x + Math.sin(a * 0.9 + st.seedF * 6) * 0.45 * w * smooth(0, 1.5, a), 0, st.centre.z + Math.sin(a * 0.7 + st.seedF * 4 + 1.3) * 0.45 * w * smooth(0, 1.5, a)); |
| +добавлено | if (dt > 0) st.vel.lerp(_a.subVectors(b, st.prev).divideScalar(dt), 1 - Math.exp(-dt * 4)); |
| +добавлено | c.pos.copy(b); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function funnel(c, st, p, dt) { |
| +добавлено | const sz = p.size, e = st.env, b = st.base, a = st.age; |
| +добавлено | const H = st.H = Math.max(0.4, 7 * sz * p.height * (1 - Math.exp(-a * 4)) * (0.6 + 0.4 * e)); |
| +добавлено | const V = st.vortex; |
| +добавлено | if (V) { |
| +добавлено | V.pos.copy(b); V.dir.copy(_up); |
| +добавлено | V.p[0] = 0.85 * sz; V.p[1] = 30 * p.spin * sz * e; V.p[2] = 7 * e; V.p[3] = 1.0 * e; |
| +добавлено | V.w = H * 1.1; |
| +добавлено | } |
| +добавлено | // the top lags its motion and sways |
| +добавлено | st.lean.set(-st.vel.x * 0.35 + Math.sin(a * 1.1 + st.seedF * 4) * 0.45 * sz, 0, -st.vel.z * 0.35 + Math.cos(a * 0.8 + st.seedF * 2) * 0.45 * sz); |
| +добавлено | const reveal = 1.15 * smooth(0, 0.6, a) * (1 - 0.45 * (1 - e)), unwind = 1 + (1 - e) * 0.7, omega = 3.4 * p.spin * (0.4 + 0.6 * e); |
| +добавлено | for (let i = 0; i < NH; i++) { |
| +добавлено | const o = i * 5, hp = st.hp, h = st.helix[i]; |
| +добавлено | const ang = hp[o + 4] - a * omega * (0.8 + 0.25 * hp[o]); |
| +добавлено | if (h) { |
| +добавлено | h.p0.set(b.x, 0.02, b.z, 0); |
| +добавлено | h.p1.set(H * hp[o + 3], hp[o + 1] * sz, hp[o + 2] * sz * unwind, -hp[o]); |
| +добавлено | h.p2.set(ang, st.lean.x, st.lean.z, 0.45 * sz); |
| +добавлено | h.p3.set(hp[o + 4] / TAU, reveal, 0.06 * p.haze * e, 0.6 + 0.4 * e); |
| +добавлено | h.intensity = 1.1 * p.intensity * Math.min(1, e * 1.4); |
| +добавлено | } |
| +добавлено | const k = i < NC ? st.cores[i] : null; |
| +добавлено | if (k && h) { |
| +добавлено | k.p0.copy(h.p0); k.p1.copy(h.p1); k.p2.copy(h.p2); k.p3.copy(h.p3); |
| +добавлено | k.p2.w = 0.05 * sz; |
| +добавлено | k.intensity = 0.55 * p.intensity * Math.min(1, e * 1.4); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // what the vortex carries: flames round the funnel, a roaring foot, embers, smoke, haze, dust |
| +добавлено | const lean = st.lean; |
| +добавлено | flameMass.column(b, lean, H, p, e, dt); |
| +добавлено | flameMass.foot(b, p, e, dt); |
| +добавлено | const hn = fx.rand(); |
| +добавлено | E.ember.pos.set(b.x + lean.x * hn * hn, hn * H * 0.7, b.z + lean.z * hn * hn); E.ember.radius = rAt(hn) * sz; |
| +добавлено | E.ember.vel.set(0, 2.5, 0); E.ember.rate(380 * p.embers * e, dt); |
| +добавлено | E.lift.pos.set(b.x, 0.1, b.z); E.lift.radius = 2.2 * sz; E.lift.vel.set(0, 1.2, 0); E.lift.rate(160 * p.embers * e, dt); |
| +добавлено | // the flame sheds a sheath of soot over its upper half that keeps turning as it rises |
| +добавлено | const hs = 0.55 + 0.45 * fx.rand(); |
| +добавлено | E.smoke.pos.set(b.x + lean.x * hs * hs, H * hs, b.z + lean.z * hs * hs); E.smoke.radius = rAt(hs) * sz * 0.85; E.smoke.vel.set(0, 1.6, 0); E.smoke.size = sz * (0.6 + 0.7 * hs); |
| +добавлено | E.smoke.rate((34 + 30 * (1 - e)) * p.smoke * Math.min(1, a * 2) * (c.phase === 'linger' && c.t > c.durations.linger - 0.5 ? 0 : 1), dt); |
| +добавлено | const hh = 0.1 + 0.7 * fx.rand(); |
| +добавлено | E.haze.pos.set(b.x + lean.x * hh * hh, hh * H, b.z + lean.z * hh * hh); E.haze.radius = rAt(hh) * sz * 0.8; E.haze.size = sz; E.haze.vel.set(0, 1.5, 0); |
| +добавлено | E.haze.rate(40 * p.haze * e, dt); |
| +добавлено | E.skirt.pos.set(b.x, 0.15, b.z); E.skirt.radius = 1.7 * sz; E.skirt.size = sz; |
| +добавлено | E.skirt.rate(70 * p.dust * e, dt); |
| +добавлено | // a scorched ring under it, and its wandering track |
| +добавлено | if (a >= st.nextSplat && e > 0.3) { |
| +добавлено | st.nextSplat = a + 0.25; |
| +добавлено | // little heat per splat: they pile up (heat lasts ~2.5 s), and the floor should smoulder and |
| +добавлено | // char under the foot rather than sprout a carpet of ground flames |
| +добавлено | splat(b.x, b.z, 1.3 * sz, 0.03 * p.scorch, 0.35 * p.scorch, 'ring', (c.seed + a * 11) % 97, b.x, b.z, 0.5 * sz); |
| +добавлено | } |
| +добавлено | // dummies in reach burn and are dragged round the funnel |
| +добавлено | st.hitT -= dt; |
| +добавлено | if (st.hitT <= 0) { |
| +добавлено | st.hitT = 0.25; |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dx = dm.pos.x - b.x, dz = dm.pos.z - b.z, d = Math.hypot(dx, dz), R = 2.4 * sz; |
| +добавлено | if (d < R) { |
| +добавлено | dm.status.burn = Math.max(dm.status.burn, (1.2 - d / R) * e); |
| +добавлено | dm.hit(_a.set(dz, 0, -dx).normalize(), 0.4 * e * p.spin * (1 - d / R)); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // the pool swirls when it passes near |
| +добавлено | const pool = fx.ARENA.pool; |
| +добавлено | const qx = Math.max(Math.abs(b.x - pool.center.x) - pool.half.x, 0), qz = Math.max(Math.abs(b.z - pool.center.y) - pool.half.y, 0); |
| +добавлено | const pd = Math.hypot(qx, qz), reach = 3 * sz; |
| +добавлено | if (pd < reach && fx.pool) { |
| +добавлено | fx.pool.swirl(THREE.MathUtils.clamp(b.x, pool.center.x - pool.half.x, pool.center.x + pool.half.x), THREE.MathUtils.clamp(b.z, pool.center.y - pool.half.y, pool.center.y + pool.half.y), 3 * e * p.spin * (1 - pd / reach)); |
| +добавлено | } |
| +добавлено | const L = st.light; |
| +добавлено | if (L) { |
| +добавлено | const flick = 0.8 + 0.2 * Math.sin(c.age * 29 + c.seed) * Math.sin(c.age * 11.3) + 0.06 * Math.sin(c.age * 67); |
| +добавлено | L.pos.set(b.x, 0.3, b.z); L.end.set(b.x + lean.x * 0.4, Math.max(0.6, H * 0.6), b.z + lean.z * 0.4); |
| +добавлено | L.intensity = 34 * p.intensity * flick * e + 2.5 * p.intensity * (1 - e) * Math.max(0, 1 - c.t / c.durations.linger); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'tint' || k === 'temperature') retint(); }, |
| +добавлено | tick() { ribs.update(); cores.update(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, st = state[c.index]; |
| +добавлено | retint(); |
| +добавлено | if (name === 'charge') { |
| +добавлено | releaseAll(st); |
| +добавлено | c.durations.linger = p.duration; |
| +добавлено | st.centre.set(c.to.x, 0, c.to.z); |
| +добавлено | st.base.copy(st.centre); st.prev.copy(st.centre); st.vel.set(0, 0, 0); |
| +добавлено | st.env = 0; st.age = 0; st.th = TH; st.nextSplat = 0; st.hitT = 0; st.H = 0; |
| +добавлено | st.seedF = (c.seed % 997) / 997; st.phase0 = st.seedF * TAU; |
| +добавлено | st.vortex = fx.forces.acquire(); |
| +добавлено | if (st.vortex) st.vortex.type = 1; |
| +добавлено | st.light = fx.lights.acquire(); |
| +добавлено | if (st.light) { st.light.type = 1; st.light.range = 12 * p.size; st.light.shadow = true; st.light.intensity = 0; lightColor(p.temperature, st.light.color); } |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | // eruption out of the spiral's eye |
| +добавлено | const x = st.centre.x, z = st.centre.z, sz = p.size; |
| +добавлено | st.age = 0; |
| +добавлено | for (let i = 0; i < NH; i++) { |
| +добавлено | const o = i * 5; |
| +добавлено | st.hp[o] = 2.2 + 1.1 * fx.rand(); // turns |
| +добавлено | st.hp[o + 1] = 0.22 + 0.28 * fx.rand(); // foot radius |
| +добавлено | st.hp[o + 2] = 0.95 + 0.55 * fx.rand(); // top radius |
| +добавлено | st.hp[o + 3] = 0.75 + 0.3 * fx.rand(); // height fraction |
| +добавлено | st.hp[o + 4] = (i + 0.35 * fx.rand()) * TAU / NH; |
| +добавлено | st.helix[i] = ribs.acquire(); |
| +добавлено | if (st.helix[i]) st.helix[i].intensity = 0; |
| +добавлено | if (i < NC) { st.cores[i] = cores.acquire(); if (st.cores[i]) st.cores[i].intensity = 0; } |
| +добавлено | } |
| +добавлено | flameMass.erupt(x, z, p); |
| +добавлено | E.spark.pos.set(x, 0.4, z); E.spark.vel.set(0, 4, 0); E.spark.burst(500 * p.embers); |
| +добавлено | E.skirt.pos.set(x, 0.15, z); E.skirt.radius = 1.2 * sz; E.skirt.burst(40 * p.dust); |
| +добавлено | SHOCK.pos.set(x, 0.05, z); SHOCK.radius1 = 6 * sz; SHOCK.distortion = 0.05 * p.haze; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | splat(x, z, 1.6 * sz, 0.45 * p.scorch, 0.7 * p.scorch, 'noise', c.seed % 89); |
| +добавлено | fx.impacts.add('scorch', _a.set(x, 1, z), 2.8 * sz, 1, p.duration + 3, lightColor(p.temperature, col)); |
| +добавлено | fx.flash(0.12 * p.intensity); |
| +добавлено | fx.rig.shake(0.4, 8); |
| +добавлено | if (!fx.reduced) fx.rig.punch(1.5, 0.1); |
| +добавлено | } else if (name === 'done') releaseAll(st); |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, st = state[c.index]; |
| +добавлено | if (c.phase === 'charge') { gather(c, st, p, dt); return; } |
| +добавлено | st.age += dt; |
| +добавлено | st.env = c.phase === 'linger' ? 1 - smooth(c.durations.linger - 1.4, c.durations.linger, c.t) : smooth(0, c.durations.impact, c.t); |
| +добавлено | place(c, st, p, dt); |
| +добавлено | funnel(c, st, p, dt); |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import { COMMON_GLSL, NOISE_GLSL, WORLD_GLSL, LIGHTS_GLSL } from '../../vfx/common.js'; |
| +добавлено | import { REFRACT_GLSL } from '../../vfx/Refraction.js'; |
| +добавлено | |
| +добавлено | // Frozen lightning lattice: one crystal (a hexagonal bipyramid) per bolt segment, built on the CPU |
| +добавлено | // once per cast into world space. The vertex shader grows each crystal when the freeze front |
| +добавлено | // reaches it (ease-out-back about its centre) and later throws it off the lattice (ballistic, |
| +добавлено | // tumbling, shrinking, resting on the floor). The fragment shader is the ice family's look kept |
| +добавлено | // cheap: flat facets, analytic-ish thickness, refraction of the opaque scene, Beer-Lambert tint, |
| +добавлено | // Fresnel sky, rime, glints, plus the lightning trapped inside (a core line, pulses running along |
| +добавлено | // the lattice, the freshly frozen front, and stress cracks before the shatter). |
| +добавлено | |
| +добавлено | export const CRYSTAL_VERT = COMMON_GLSL + /* glsl */` |
| +добавлено | in vec4 aChunk; // crystal centre xyz, freeze-front time (s) |
| +добавлено | in vec4 aAxis; // crystal axis xyz, radius m |
| +добавлено | in vec4 aInfo; // seed 0..1, half length m, kind (0 channel, 1 branch, 2 spur), - |
| +добавлено | in vec3 aVel; // shatter velocity m/s |
| +добавлено | uniform vec4 uFreeze; // front time now, grow time, shatter age (< 0: whole), shatter delay per front second |
| +добавлено | uniform vec4 uShatter; // spin rad/s, gravity, -, - |
| +добавлено | out vec3 vWorld; |
| +добавлено | out vec3 vRest; // undeformed, relative to the crystal centre (noise lives here) |
| +добавлено | out float vFront; // seconds since the front passed this crystal |
| +добавлено | out float vRadius; |
| +добавлено | out float vSeed; |
| +добавлено | flat out vec3 vCentre; // crystal centre and axis after the shatter motion (the trapped filament) |
| +добавлено | flat out vec3 vAxis; |
| +добавлено | flat out float vKind; |
| +добавлено | vec3 rotateAxis(vec3 p, vec3 k, float a) { float c = cos(a), s = sin(a); return p * c + cross(k, p) * s + k * dot(k, p) * (1.0 - c); } |
| +добавлено | void main() { |
| +добавлено | vec3 c = aChunk.xyz, p = position - c; |
| +добавлено | vRest = p; vRadius = aAxis.w; vSeed = aInfo.x; vKind = aInfo.z; |
| +добавлено | vec3 ax = aAxis.xyz; |
| +добавлено | vFront = uFreeze.x - aChunk.w; |
| +добавлено | float g = clamp(vFront / uFreeze.y, 0.0, 1.0), u = g - 1.0; |
| +добавлено | p *= max(1.0 + 2.70158 * u * u * u + 1.70158 * u * u, 0.0); // ease-out-back |
| +добавлено | float s = uFreeze.z - aChunk.w * uFreeze.w; |
| +добавлено | if (uFreeze.z >= 0.0 && s > 0.0) { |
| +добавлено | vec3 h = hash31(aInfo.x * 917.0 + 3.1); |
| +добавлено | vec3 k = normalize(h - 0.5 + vec3(1e-3)); |
| +добавлено | float ang = uShatter.x * s * (0.4 + h.x); |
| +добавлено | p = rotateAxis(p, k, ang); |
| +добавлено | ax = rotateAxis(ax, k, ang); |
| +добавлено | p *= 1.0 - smoothstep(0.45, 1.25, s + 0.35 * h.y); |
| +добавлено | c += aVel * s; |
| +добавлено | c.y -= 0.5 * uShatter.y * s * s; |
| +добавлено | c.y = max(c.y, aAxis.w * 0.5); |
| +добавлено | } |
| +добавлено | vWorld = c + p; |
| +добавлено | vCentre = c; vAxis = ax; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(vWorld, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const CRYSTAL_FRAG = COMMON_GLSL + NOISE_GLSL + WORLD_GLSL + LIGHTS_GLSL + REFRACT_GLSL + /* glsl */` |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | uniform vec4 uLook; // ior, frost, glints, lens |
| +добавлено | uniform vec4 uTrap; // core glow, pulse position (front s), crack glow, front glow |
| +добавлено | uniform vec3 uAbsorb; // Beer-Lambert sigma per metre |
| +добавлено | uniform vec3 uInscat; |
| +добавлено | uniform vec3 uGlow; // trapped light (linear HDR) |
| +добавлено | uniform vec3 uCoreCol; // the filament's white-hot core |
| +добавлено | in vec3 vWorld; |
| +добавлено | in vec3 vRest; |
| +добавлено | in float vFront; |
| +добавлено | in float vRadius; |
| +добавлено | in float vSeed; |
| +добавлено | flat in vec3 vCentre; |
| +добавлено | flat in vec3 vAxis; |
| +добавлено | flat in float vKind; |
| +добавлено | out vec4 fragColor; |
| +добавлено | |
| +добавлено | void main() { |
| +добавлено | vec3 N = normalize(cross(dFdx(vWorld), dFdy(vWorld))); // flat facets |
| +добавлено | vec3 V = normalize(cameraPosition - vWorld); |
| +добавлено | if (dot(N, V) < 0.0) N = -N; |
| +добавлено | float NoV = max(dot(N, V), 1e-3); |
| +добавлено | // thickness of a thin prism: about a diameter seen face on, less at grazing |
| +добавлено | float thick = 2.0 * vRadius * (0.35 + 0.65 * NoV); |
| +добавлено | float behind; |
| +добавлено | vec3 bg = sceneRefract(vWorld, N, V, uLook.x, thick * uLook.w, behind); |
| +добавлено | vec3 Tr = beerLambert(uAbsorb, thick); |
| +добавлено | |
| +добавлено | vec3 sd, ss; |
| +добавлено | gLightNoShadow = true; |
| +добавлено | spellLightPBR(vWorld, N, V, 0.04, vec3(0.02), vec3(1.0), sd, ss); |
| +добавлено | float NoL = max(dot(N, uSunDir), 0.0); |
| +добавлено | vec3 H = normalize(uSunDir + V); |
| +добавлено | float sunSpec = spellD_GGX(max(dot(N, H), 0.0), 0.0016) * spellV_Smith(NoV, NoL, 0.0016) * NoL * PI; |
| +добавлено | vec3 irr = uAmbient + uSunColor * NoL + sd; |
| +добавлено | vec3 irrOmni = uAmbient + uSunColor * 0.5 + sd; |
| +добавлено | |
| +добавлено | // the lightning trapped inside: a filament along each crystal's axis (the crystals join end to |
| +добавлено | // end, so the filaments trace the frozen bolt), pulses running along the lattice, the white-hot |
| +добавлено | // freshly frozen front, and stress cracks before it shatters. Widths keep a one-pixel floor |
| +добавлено | // (energy kept) so the thin core never shimmers. |
| +добавлено | vec3 rd = -V, w = vCentre - cameraPosition, cx = cross(rd, vAxis); |
| +добавлено | float lc = length(cx); |
| +добавлено | float dist = lc > 1e-4 ? abs(dot(w, cx)) / lc : length(cross(w, rd)); |
| +добавлено | float pix = length(fwidth(vWorld)); |
| +добавлено | float isFil = step(vKind, 1.5); |
| +добавлено | float fw0 = vRadius * (vKind > 0.5 ? 0.24 : 0.32), fw = max(fw0, pix); |
| +добавлено | float hw0 = fw0 * 0.3, hw = max(hw0, pix * 0.7); |
| +добавлено | float fil = exp(-dist * dist / (fw * fw)) * (fw0 / fw) * isFil; |
| +добавлено | float hot = exp(-dist * dist / (hw * hw)) * (hw0 / hw) * isFil; |
| +добавлено | float pulse = exp(-pow((vFront - uTrap.y) * 6.0, 2.0)); |
| +добавлено | float fresh = exp(-max(vFront, 0.0) * 7.0) * uTrap.w; |
| +добавлено | float trap = uTrap.x + 4.0 * pulse; |
| +добавлено | |
| +добавлено | // rime: fine patches, heavier at grazing angles; it scatters the trapped light, so it glows |
| +добавлено | vec4 n = noise3v(vRest * 8.0 + vSeed * 7.0); |
| +добавлено | float frost = smoothstep(0.55, 0.85, n.r + 0.25 * pow(1.0 - NoV, 2.0)) * uLook.y; |
| +добавлено | frost = clamp(frost + pow(1.0 - NoV, 4.0) * 0.4 * uLook.y, 0.0, 1.0); |
| +добавлено | |
| +добавлено | float F = fresnel(NoV, 0.02); |
| +добавлено | vec3 refl = envReflect(reflect(-V, N), 0.04); |
| +добавлено | vec3 body = bg * Tr + uInscat * irrOmni * 0.5 * (1.0 - dot(Tr, vec3(0.3333))); |
| +добавлено | vec3 col = body * (1.0 - F) + (refl * F + ss + uSunColor * F * sunSpec) * (1.0 - frost * 0.7); |
| +добавлено | vec3 rime = vec3(0.86, 0.94, 1.0) * irr * 0.8 + uGlow * (0.35 * trap + fresh); |
| +добавлено | col = mix(col, rime, frost * 0.7); |
| +добавлено | |
| +добавлено | // glints: view-dependent micro-facets, faded when sub-pixel |
| +добавлено | vec3 gq = vRest * 60.0; |
| +добавлено | vec3 gh = hash33(floor(gq) + floor(V * 5.0) * 17.0 + vSeed * 31.0); |
| +добавлено | float tw = pow(0.5 + 0.5 * sin(uTime * 7.0 + gh.y * 60.0), 6.0); |
| +добавлено | float spot = smoothstep(0.35, 0.0, length(fract(gq) - 0.5 - (gh - 0.5) * 0.4)); |
| +добавлено | col += step(0.9, gh.x) * spot * tw * smoothstep(1.2, 0.4, length(fwidth(gq))) * (irr * 6.0 + ss * 0.5) * uLook.z; |
| +добавлено | |
| +добавлено | float crack = smoothstep(0.035, 0.0, abs(n.g - 0.5)) * smoothstep(0.3, 0.6, n.r) * uTrap.z; |
| +добавлено | float inner = pow(NoV, 3.0) * 0.3; |
| +добавлено | col += uGlow * ((fil + inner) * trap + fresh * (1.5 + 4.0 * fil) + crack * 9.0) |
| +добавлено | + uCoreCol * hot * (uTrap.x * 2.0 + 6.0 * pulse + 3.0 * fresh); |
| +добавлено | fragColor = vec4(col, 1.0); |
| +добавлено | }`; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { CRYSTAL_VERT, CRYSTAL_FRAG } from './frozen-lightning.glsl.js'; |
| +добавлено | |
| +добавлено | // Frozen Lightning (SPELLBOOK hybrid, ice + lightning): a bolt that freezes into a crystal lattice, |
| +добавлено | // then shatters. Charge: micro-arcs and ice motes gather on the focus while a frost ring creeps in |
| +добавлено | // at the target. Strike: a stepped leader runs focus -> target, return stroke and one restrike on |
| +добавлено | // the SAME branches (sparks, shock, Lichtenberg glow). Freeze: a front races back from the target |
| +добавлено | // along the channel and out along every branch and sub-branch; behind it each bolt segment becomes |
| +добавлено | // a faceted ice crystal (rime spurs sprout off it), so the exact bolt you saw hangs in the air as |
| +добавлено | // ice with its light trapped inside, pulses running through it, mist pouring off it, frost on the |
| +добавлено | // floor beneath it and ice spikes caging the target. Shatter: stress cracks flare, then the lattice |
| +добавлено | // bursts from the target end back to the caster into tumbling crystals, shards, glints and mist, |
| +добавлено | // and the trapped charge escapes as floor crawl arcs. |
| +добавлено | // |
| +добавлено | // The lattice geometry is written into preallocated buffers once per cast. The branch layout is a |
| +добавлено | // JS mirror of Bolts.js (same PCG hashes and lattice noise), so the ice lands on the bolt's branches. |
| +добавлено | |
| +добавлено | const MAIN_SEG = 28, BR_SEG = 8, SUB_SEG = 4, MAX_BR = 8, MAX_SUB = 6, MAX_SPUR = 44; |
| +добавлено | const MAX_CRYSTALS = MAIN_SEG + MAX_BR * BR_SEG + MAX_SUB * SUB_SEG + MAX_SPUR; |
| +добавлено | const VPC = 72; // vertices per crystal: 24 flat triangles |
| +добавлено | const LATTICES = 2, N_CHARGE = 3, MAX_CRAWL = 6; |
| +добавлено | const STRIKE = 0.16; // leader + return stroke + restrike, then the freeze starts |
| +добавлено | const TAU = Math.PI * 2; |
| +добавлено | const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); }; |
| +добавлено | |
| +добавлено | // --- JS mirror of Bolts.js' path (PCG hashes, piecewise-linear lattice noise) --------------- |
| +добавлено | function pcg(v) { |
| +добавлено | const s = (Math.imul(v, 747796405) + 2891336453) >>> 0; |
| +добавлено | const w = Math.imul((s >>> ((s >>> 28) + 4)) ^ s, 277803737) >>> 0; |
| +добавлено | return ((w >>> 22) ^ w) >>> 0; |
| +добавлено | } |
| +добавлено | const urand = (v) => pcg(v >>> 0) / 4294967296; |
| +добавлено | function lnoise(x, salt) { |
| +добавлено | const i = Math.floor(x), k = (i + salt) >>> 0; |
| +добавлено | const a = urand(k), b = urand((k + 1) >>> 0); |
| +добавлено | return (a + (b - a) * (x - i)) * 2 - 1; |
| +добавлено | } |
| +добавлено | const _T = new THREE.Vector3(), _Ub = new THREE.Vector3(), _Vb = new THREE.Vector3(); |
| +добавлено | const UP = new THREE.Vector3(0, 1, 0), RIGHT = new THREE.Vector3(1, 0, 0); |
| +добавлено | function frame(T) { _Ub.crossVectors(T, Math.abs(T.y) < 0.99 ? UP : RIGHT).normalize(); _Vb.crossVectors(T, _Ub); } |
| +добавлено | function boltPath(A, B, t, seed, jag, freq, octaves, freeEnd, out) { |
| +добавлено | _T.subVectors(B, A); |
| +добавлено | const L = _T.length(); |
| +добавлено | _T.multiplyScalar(1 / Math.max(L, 1e-6)); |
| +добавлено | frame(_T); |
| +добавлено | let n1 = 0, n2 = 0, amp = 1, f = freq; |
| +добавлено | for (let k = 0; k < octaves; k++) { |
| +добавлено | const s = pcg((seed + Math.imul(k, 0x9E3779B9)) >>> 0); |
| +добавлено | n1 += amp * lnoise(t * f, s); |
| +добавлено | n2 += amp * lnoise(t * f, (s ^ 0x5bd1e995) >>> 0); |
| +добавлено | amp *= 0.5; f *= 2; |
| +добавлено | } |
| +добавлено | const env = L * jag * Math.sqrt(Math.max(Math.sin(Math.PI * t * (1 - 0.5 * freeEnd)), 0)); |
| +добавлено | return out.lerpVectors(A, B, t).addScaledVector(_Ub, n1 * env).addScaledVector(_Vb, n2 * env); |
| +добавлено | } |
| +добавлено | // 25-50 degrees off T, random azimuth |
| +добавлено | function deviate(T, h, out) { |
| +добавлено | const th = (25 + 25 * urand(h + 1)) * Math.PI / 180, ph = TAU * urand(h + 2); |
| +добавлено | frame(T); |
| +добавлено | return out.copy(T).multiplyScalar(Math.cos(th)).addScaledVector(_Ub, Math.sin(th) * Math.cos(ph)).addScaledVector(_Vb, Math.sin(th) * Math.sin(ph)); |
| +добавлено | } |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'frozen-lightning', name: 'Frozen Lightning', element: 'hybrid', order: 10, |
| +добавлено | shape: 'strike', |
| +добавлено | params: { |
| +добавлено | intensity: 1, branches: 6, jag: 0.12, thickness: 1, spurs: 1, freeze: 1, hold: 1, shatter: 1, mist: 1, glints: 1, frost: 1, |
| +добавлено | core: '#e6f4ff', glow: '#4fc8ff', tint: '#a8dcff', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | intensity: [0.1, 3, 0.01], branches: [0, 8, 1], jag: [0.04, 0.25, 0.005], thickness: [0.4, 2.5, 0.01], spurs: [0, 2, 0.01], |
| +добавлено | freeze: [0.4, 3, 0.01], hold: [0.2, 3, 0.01], shatter: [0, 3, 0.01], mist: [0, 3, 0.01], glints: [0, 3, 0.01], frost: [0, 2, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | aurora: { glow: '#3dffb0', core: '#e8fff4', tint: '#b8ffe8' }, |
| +добавлено | 'void ice': { glow: '#9a5bff', core: '#f2e8ff', tint: '#c8b8ff', jag: 0.16 }, |
| +добавлено | glacier: { glow: '#2f7dff', tint: '#5fa8ff', thickness: 1.5, spurs: 1.6, branches: 8 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 0.8, impact: 2, linger: 2.4 }, |
| +добавлено | kinds: {}, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const mote = P.emitter('ice.mote', { shape: 'shell', radius: 1.3, spread: 0.1, radial: -2.2 }); |
| +добавлено | const eMote = P.emitter('lightning.mote', { shape: 'shell', radius: 1.8, spread: 0.2 }); |
| +добавлено | const tMote = P.emitter('ice.mote', { shape: 'shell', radius: 1.6, spread: 0.3, radial: -1.2 }); |
| +добавлено | const mist = P.emitter('ice.mist', { shape: 'sphere', radius: 0.3, spread: 0.3 }); |
| +добавлено | const lineMist = P.emitter('ice.mist', { shape: 'line', radius: 0.12, spread: 0.25 }); |
| +добавлено | const glint = P.emitter('ice.glint', { shape: 'sphere', radius: 0.2, spread: 3.5, radial: 2 }); |
| +добавлено | const lineGlint = P.emitter('ice.glint', { shape: 'line', radius: 0.15, spread: 2.5 }); |
| +добавлено | const shard = P.emitter('ice.shard', { shape: 'line', radius: 0.12, spread: 1.8, radial: 2.5 }); |
| +добавлено | const chip = P.emitter('ice.chip', { shape: 'line', radius: 0.1, spread: 2.2, radial: 2 }); |
| +добавлено | const spark = P.emitter('lightning.spark', { shape: 'sphere', radius: 0.15, spread: 6, radial: 3 }); |
| +добавлено | const lineSpark = P.emitter('lightning.spark', { shape: 'line', radius: 0.05, spread: 2.5 }); |
| +добавлено | |
| +добавлено | // --- look --- |
| +добавлено | const core = new THREE.Color(), glow = new THREE.Color(), tint = new THREE.Color(), glowDim = new THREE.Color(), lightCol = new THREE.Color(); |
| +добавлено | const absorb = new THREE.Vector3(), inscat = new THREE.Vector3(), glowV = new THREE.Vector3(), coreV = new THREE.Vector3(), look = new THREE.Vector4(); |
| +добавлено | function applyLook() { |
| +добавлено | const p = S.params; |
| +добавлено | core.set(p.core); glow.set(p.glow); tint.set(p.tint); |
| +добавлено | glowDim.copy(glow).multiplyScalar(0.08); |
| +добавлено | lightCol.copy(glow).lerp(core, 0.4); |
| +добавлено | absorb.set(-Math.log(Math.max(tint.r, 0.01)), -Math.log(Math.max(tint.g, 0.01)), -Math.log(Math.max(tint.b, 0.01))).multiplyScalar(6); |
| +добавлено | inscat.set(tint.r * tint.r, tint.g * tint.g, tint.b * tint.b).multiplyScalar(0.6); |
| +добавлено | glowV.set(glow.r, glow.g, glow.b).multiplyScalar(3); |
| +добавлено | coreV.set(core.r, core.g, core.b).multiplyScalar(4); |
| +добавлено | look.set(1.31, p.frost, p.glints, 3); |
| +добавлено | } |
| +добавлено | applyLook(); |
| +добавлено | |
| +добавлено | // --- lattice meshes: preallocated buffers, rewritten once per cast --- |
| +добавлено | const NV = MAX_CRYSTALS * VPC; |
| +добавлено | const lattices = []; |
| +добавлено | for (let i = 0; i < LATTICES; i++) { |
| +добавлено | const geo = new THREE.BufferGeometry(); |
| +добавлено | const attr = (name, size) => { const a = new THREE.BufferAttribute(new Float32Array(NV * size), size); a.setUsage(THREE.DynamicDrawUsage); geo.setAttribute(name, a); return a; }; |
| +добавлено | const L = { |
| +добавлено | pos: attr('position', 3), chunk: attr('aChunk', 4), axis: attr('aAxis', 4), info: attr('aInfo', 4), vel: attr('aVel', 3), |
| +добавлено | n: 0, free: true, maxFront: 0, attrs: null, |
| +добавлено | u: { uFreeze: { value: new THREE.Vector4(0, 0.12, -1, 0.25) }, uShatter: { value: new THREE.Vector4(7, 9.8, 0, 0) }, uTrap: { value: new THREE.Vector4() } }, |
| +добавлено | }; |
| +добавлено | geo.setDrawRange(0, 0); |
| +добавлено | L.mesh = new THREE.Mesh(geo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: CRYSTAL_VERT, fragmentShader: CRYSTAL_FRAG, |
| +добавлено | uniforms: { ...fx.U, ...L.u, uLook: { value: look }, uAbsorb: { value: absorb }, uInscat: { value: inscat }, uGlow: { value: glowV }, uCoreCol: { value: coreV } }, |
| +добавлено | })); |
| +добавлено | L.mesh.frustumCulled = false; |
| +добавлено | L.mesh.visible = false; |
| +добавлено | L.geo = geo; |
| +добавлено | L.attrs = [L.pos, L.chunk, L.axis, L.info, L.vel]; |
| +добавлено | fx.scenes.refract.add(L.mesh); |
| +добавлено | lattices.push(L); |
| +добавлено | } |
| +добавлено | const takeLattice = () => { for (let i = 0; i < LATTICES; i++) if (lattices[i].free) { lattices[i].free = false; return lattices[i]; } return null; }; |
| +добавлено | const dropLattice = (L) => { if (L) { L.mesh.visible = false; L.free = true; } }; |
| +добавлено | |
| +добавлено | // --- crystal writer --- |
| +добавлено | const V3 = () => new THREE.Vector3(); |
| +добавлено | const ring0 = Array.from({ length: 6 }, V3), ring1 = Array.from({ length: 6 }, V3); |
| +добавлено | const _u = V3(), _c = V3(), _e1 = V3(), _e2 = V3(), _t0 = V3(), _t1 = V3(), _n = V3(), _ab = V3(), _ac = V3(), _cv = V3(), _vel = V3(); |
| +добавлено | let W = null, Wv = 0, Wtf = 0, Wseed = 0, Wkind = 0, Wr = 0, Whl = 0; // current crystal (writer state) |
| +добавлено | function vert(p) { |
| +добавлено | const i = Wv++; |
| +добавлено | W.pos.array[i * 3] = p.x; W.pos.array[i * 3 + 1] = p.y; W.pos.array[i * 3 + 2] = p.z; |
| +добавлено | W.chunk.array[i * 4] = _c.x; W.chunk.array[i * 4 + 1] = _c.y; W.chunk.array[i * 4 + 2] = _c.z; W.chunk.array[i * 4 + 3] = Wtf; |
| +добавлено | W.axis.array[i * 4] = _u.x; W.axis.array[i * 4 + 1] = _u.y; W.axis.array[i * 4 + 2] = _u.z; W.axis.array[i * 4 + 3] = Wr; |
| +добавлено | W.info.array[i * 4] = Wseed; W.info.array[i * 4 + 1] = Whl; W.info.array[i * 4 + 2] = Wkind; W.info.array[i * 4 + 3] = 0; |
| +добавлено | W.vel.array[i * 3] = _vel.x; W.vel.array[i * 3 + 1] = _vel.y; W.vel.array[i * 3 + 2] = _vel.z; |
| +добавлено | } |
| +добавлено | // outward winding (front faces face away from the crystal's centre) |
| +добавлено | function tri(a, b, c) { |
| +добавлено | _n.crossVectors(_ab.subVectors(b, a), _ac.subVectors(c, a)); |
| +добавлено | _cv.addVectors(a, b).add(c).multiplyScalar(1 / 3).sub(_c); |
| +добавлено | if (_n.dot(_cv) >= 0) { vert(a); vert(b); vert(c); } else { vert(a); vert(c); vert(b); } |
| +добавлено | } |
| +добавлено | // hexagonal bipyramid from a to b, radius r; front time tf; burst velocity from the lattice line |
| +добавлено | function crystal(L, a, b, r, tf, kind, rnd, lineA, lineB, force) { |
| +добавлено | if (L.n >= MAX_CRYSTALS) return; |
| +добавлено | _u.subVectors(b, a); |
| +добавлено | const len = Math.max(_u.length(), 1e-3); |
| +добавлено | _u.multiplyScalar(1 / len); |
| +добавлено | _c.addVectors(a, b).multiplyScalar(0.5); |
| +добавлено | const hl = len * 0.5, tl = r * (kind === 2 ? 2.5 : 1.1), tw = rnd() * TAU; |
| +добавлено | frame(_u); _e1.copy(_Ub); _e2.copy(_Vb); |
| +добавлено | for (let k = 0; k < 6; k++) { |
| +добавлено | const ang = tw + k * Math.PI / 3, rr = r * (0.88 + 0.24 * rnd()); |
| +добавлено | ring0[k].copy(_c).addScaledVector(_u, -hl).addScaledVector(_e1, Math.cos(ang) * rr).addScaledVector(_e2, Math.sin(ang) * rr); |
| +добавлено | ring1[k].copy(_c).addScaledVector(_u, hl).addScaledVector(_e1, Math.cos(ang) * rr * (kind === 2 ? 0.6 : 0.92)).addScaledVector(_e2, Math.sin(ang) * rr * (kind === 2 ? 0.6 : 0.92)); |
| +добавлено | } |
| +добавлено | _t0.copy(_c).addScaledVector(_u, -(hl + tl * (0.5 + 0.5 * rnd()))); |
| +добавлено | _t1.copy(_c).addScaledVector(_u, hl + tl * (0.7 + 0.6 * rnd())); |
| +добавлено | // burst: away from the lattice line, a kick up, some scatter |
| +добавлено | _ab.subVectors(lineB, lineA); |
| +добавлено | const h = Math.min(1, Math.max(0, _ac.subVectors(_c, lineA).dot(_ab) / Math.max(_ab.lengthSq(), 1e-6))); |
| +добавлено | _vel.copy(_c).sub(_ab.multiplyScalar(h).add(lineA)).setY(0); |
| +добавлено | if (_vel.lengthSq() < 1e-4) _vel.set(rnd() - 0.5, 0, rnd() - 0.5); |
| +добавлено | _vel.normalize().multiplyScalar(1.2 + 2.4 * rnd()); |
| +добавлено | _vel.x += (rnd() - 0.5) * 1.6; _vel.z += (rnd() - 0.5) * 1.6; _vel.y = 0.8 + 2.2 * rnd(); |
| +добавлено | _vel.multiplyScalar(force); |
| +добавлено | W = L; Wv = L.n * VPC; Wtf = tf; Wseed = rnd(); Wkind = kind; Wr = r; Whl = hl + tl; |
| +добавлено | for (let k = 0; k < 6; k++) { |
| +добавлено | const j = (k + 1) % 6; |
| +добавлено | tri(ring0[k], ring0[j], ring1[j]); tri(ring0[k], ring1[j], ring1[k]); |
| +добавлено | tri(ring1[k], ring1[j], _t1); tri(ring0[j], ring0[k], _t0); |
| +добавлено | } |
| +добавлено | L.n++; |
| +добавлено | L.maxFront = Math.max(L.maxFront, tf); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- per-cast state --- |
| +добавлено | const MP = Array.from({ length: MAIN_SEG + 1 }, V3), BP = Array.from({ length: BR_SEG + 1 }, V3); |
| +добавлено | const brA = Array.from({ length: MAX_BR }, V3), brB = Array.from({ length: MAX_BR }, V3), brSeed = new Uint32Array(MAX_BR), brLen = new Float32Array(MAX_BR), brTf = new Float32Array(MAX_BR); |
| +добавлено | const st = Array.from({ length: 16 }, () => ({ |
| +добавлено | lattice: null, bolt: null, charge: [null, null, null], crawl: new Array(MAX_CRAWL).fill(null), nc: 0, |
| +добавлено | L1: null, L2: null, A: V3(), B: V3(), g: V3(), front: V3(), |
| +добавлено | speed: 10, mainLen: 1, strikes: 0, frozen: false, shattered: false, reseed: 0, nextSplat: 0, pulseT: 0, |
| +добавлено | })); |
| +добавлено | const _a = V3(), _b = V3(), _d = V3(), _q0 = V3(), _sB = V3(), _dir = V3(); |
| +добавлено | const seedInt = () => (fx.rand() * 16777215) | 0; |
| +добавлено | // the lattice's own deterministic stream (keyed to c.seed, so a shot rebuilds the same ice) |
| +добавлено | let rs = 1; |
| +добавлено | const rnd = () => ((rs = (Math.imul(rs, 1664525) + 1013904223) >>> 0) / 4294967296); |
| +добавлено | const BOLT = { a: V3(), b: V3(), branches: 6, subBranches: 6, jag: 0.12, width: 0.03, color: core, glowColor: glowDim, intensity: 0, glowWidth: 12, reveal: 0, snapToArena: false, seed: 1, branchSeed: 1 }; |
| +добавлено | function bolt(a, b, branches, sub, jag, width, glowWidth, reveal, snap) { |
| +добавлено | BOLT.a.copy(a); BOLT.b.copy(b); BOLT.branches = branches; BOLT.subBranches = sub; BOLT.jag = jag; BOLT.width = width; |
| +добавлено | BOLT.glowWidth = glowWidth; BOLT.reveal = reveal; BOLT.snapToArena = snap; BOLT.seed = seedInt(); BOLT.branchSeed = seedInt(); |
| +добавлено | return fx.bolts.acquire(BOLT); |
| +добавлено | } |
| +добавлено | const SPL = { x: 0, z: 0, radius: 1, frost: 0, glow: 0, char: 0, pattern: 'radial', seed: 0, x2: 0, z2: 0, width: 0 }; |
| +добавлено | function splat(x, z, radius, frost, glowAmt, char, pattern, seed, x2 = x, z2 = z, width = 0) { |
| +добавлено | SPL.x = x; SPL.z = z; SPL.radius = radius; SPL.frost = frost; SPL.glow = glowAmt; SPL.char = char; SPL.pattern = pattern; |
| +добавлено | SPL.seed = seed % 1000; SPL.x2 = x2; SPL.z2 = z2; SPL.width = width; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | const SHK = { pos: V3(), radius0: 0.3, radius1: 4, duration: 0.5, thickness: 0.5, distortion: 0.04, color: glow, intensity: 0, ground: false }; |
| +добавлено | function shock(pos, r0, r1, dur, thick, dist, intensity, ground) { |
| +добавлено | SHK.pos.copy(pos); SHK.radius0 = r0; SHK.radius1 = r1; SHK.duration = dur; SHK.thickness = thick; |
| +добавлено | SHK.distortion = dist; SHK.intensity = intensity; SHK.ground = ground; |
| +добавлено | fx.shocks.spawn(SHK); |
| +добавлено | } |
| +добавлено | const SPK = { pos: V3(), dir: V3(), height: 1, radius: 0.3, material: 'ice', delay: 0, rise: 0.18, life: 2, crumble: 0.7, seed: 1 }; |
| +добавлено | |
| +добавлено | // the frozen lattice of bolt h (main channel, branches, sub-branches, rime spurs) |
| +добавлено | function buildLattice(L, h, A, B, p, d, rnd) { |
| +добавлено | L.n = 0; L.maxFront = 0; |
| +добавлено | const jag = h.jag, L0 = A.distanceTo(B), seed = pcg(h.seed >>> 0), bseed = h.branchSeed >>> 0; |
| +добавлено | const nb = Math.min(MAX_BR, h.branches), ns = nb ? Math.min(MAX_SUB, h.subBranches) : 0; |
| +добавлено | const th = p.thickness, force = p.shatter; |
| +добавлено | for (let i = 0; i <= MAIN_SEG; i++) boltPath(A, B, i / MAIN_SEG, seed, jag, 2, 7, 0, MP[i]); |
| +добавлено | let len = 0; |
| +добавлено | for (let i = 0; i < MAIN_SEG; i++) len += MP[i].distanceTo(MP[i + 1]); |
| +добавлено | d.mainLen = len; |
| +добавлено | d.speed = len / (0.55 / p.freeze); // the main channel freezes in 0.55 s whatever its length |
| +добавлено | // channel: the front starts at the target end |
| +добавлено | let fromB = len; |
| +добавлено | let spurs = 0; |
| +добавлено | for (let i = 0; i < MAIN_SEG; i++) { |
| +добавлено | const sl = MP[i].distanceTo(MP[i + 1]); |
| +добавлено | fromB -= sl; |
| +добавлено | const taper = 0.7 + 0.3 * Math.min(1, i / 6); // thinner where it leaves the focus |
| +добавлено | crystal(L, MP[i], MP[i + 1], 0.11 * th * taper * (0.85 + 0.3 * rnd()), (fromB + 0.5 * sl) / d.speed, 0, rnd, A, B, force); |
| +добавлено | if (spurs < MAX_SPUR * 0.6 && rnd() < 0.75 * p.spurs) { spur(L, MP[i], MP[i + 1], 0.1 * th, (fromB + 0.5 * sl) / d.speed + 0.03, rnd, A, B, force); spurs++; } |
| +добавлено | } |
| +добавлено | // branches (exactly where Bolts.js draws them) |
| +добавлено | for (let bi = 0; bi < nb; bi++) { |
| +добавлено | const bh = pcg((bseed + Math.imul(bi, 0x632BE5AB)) >>> 0); |
| +добавлено | const tb = 0.05 + 0.7 * urand(bh); |
| +добавлено | boltPath(A, B, tb, seed, jag, 2, 7, 0, brA[bi]); |
| +добавлено | boltPath(A, B, tb + 0.05, seed, jag, 2, 7, 0, _dir); |
| +добавлено | _dir.sub(brA[bi]).normalize(); |
| +добавлено | const bl = (0.15 + 0.25 * urand(bh + 3)) * L0; |
| +добавлено | brB[bi].copy(brA[bi]).addScaledVector(deviate(_dir, bh, _d), bl); |
| +добавлено | brSeed[bi] = pcg(bh + 4); brLen[bi] = bl; brTf[bi] = (1 - tb) * len / d.speed; |
| +добавлено | for (let k = 0; k <= BR_SEG; k++) boltPath(brA[bi], brB[bi], k / BR_SEG, brSeed[bi], jag * 1.3, 2, 5, 1, BP[k]); |
| +добавлено | for (let k = 0; k < BR_SEG; k++) { |
| +добавлено | const tf = brTf[bi] + (k + 0.5) / BR_SEG * bl / d.speed; |
| +добавлено | crystal(L, BP[k], BP[k + 1], 0.065 * th * (1 - 0.55 * k / BR_SEG), tf, 1, rnd, A, B, force); |
| +добавлено | if (spurs < MAX_SPUR && rnd() < 0.35 * p.spurs) { spur(L, BP[k], BP[k + 1], 0.06 * th, tf + 0.03, rnd, A, B, force); spurs++; } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // sub-branches off the branches |
| +добавлено | for (let j = 0; j < ns; j++) { |
| +добавлено | const strand = 9 + j, bi = j % nb; |
| +добавлено | const sh = pcg((bseed + Math.imul(strand, 0x2C1B3C6D)) >>> 0); |
| +добавлено | const ts = 0.1 + 0.6 * urand(sh); |
| +добавлено | boltPath(brA[bi], brB[bi], ts, brSeed[bi], jag * 1.3, 2, 5, 1, _q0); |
| +добавлено | boltPath(brA[bi], brB[bi], ts + 0.1, brSeed[bi], jag * 1.3, 2, 5, 1, _dir); |
| +добавлено | _dir.sub(_q0).normalize(); |
| +добавлено | const l2 = (0.3 + 0.3 * urand(sh + 3)) * brLen[bi]; |
| +добавлено | _sB.copy(_q0).addScaledVector(deviate(_dir, sh, _d), l2); |
| +добавлено | const s2 = pcg(sh + 4), tf0 = brTf[bi] + ts * brLen[bi] / d.speed; |
| +добавлено | for (let k = 0; k <= SUB_SEG; k++) boltPath(_q0, _sB, k / SUB_SEG, s2, jag * 1.3, 1.5, 3, 1, BP[k]); |
| +добавлено | for (let k = 0; k < SUB_SEG; k++) crystal(L, BP[k], BP[k + 1], 0.042 * th * (1 - 0.5 * k / SUB_SEG), tf0 + (k + 0.5) / SUB_SEG * l2 / d.speed, 1, rnd, A, B, force); |
| +добавлено | } |
| +добавлено | for (let i = 0; i < L.attrs.length; i++) { const a = L.attrs[i]; a.clearUpdateRanges(); a.addUpdateRange(0, L.n * VPC * a.itemSize); a.needsUpdate = true; } |
| +добавлено | L.geo.setDrawRange(0, L.n * VPC); |
| +добавлено | } |
| +добавлено | // a rime spur growing off a crystal at 35-70 degrees |
| +добавлено | function spur(L, a, b, r, tf, rnd, A, B, force) { |
| +добавлено | _u.subVectors(b, a).normalize(); |
| +добавлено | frame(_u); |
| +добавлено | const ph = rnd() * TAU, el = 0.6 + 0.6 * rnd(); |
| +добавлено | _dir.copy(_Ub).multiplyScalar(Math.cos(ph)).addScaledVector(_Vb, Math.sin(ph)).multiplyScalar(Math.sin(el)).addScaledVector(_u, Math.cos(el)); |
| +добавлено | _a.lerpVectors(a, b, 0.2 + 0.6 * rnd()); |
| +добавлено | _b.copy(_a).addScaledVector(_dir, (2.2 + 3 * rnd()) * r); |
| +добавлено | crystal(L, _a, _b, r * (0.3 + 0.15 * rnd()), tf, 2, rnd, A, B, force); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function releaseAll(d) { |
| +добавлено | d.bolt?.release(); d.bolt = null; |
| +добавлено | for (let i = 0; i < N_CHARGE; i++) { d.charge[i]?.release(); d.charge[i] = null; } |
| +добавлено | for (let i = 0; i < d.nc; i++) d.crawl[i]?.release(); |
| +добавлено | d.crawl.fill(null); d.nc = 0; |
| +добавлено | d.L1?.release(); d.L2?.release(); d.L1 = d.L2 = null; |
| +добавлено | dropLattice(d.lattice); d.lattice = null; |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam() { applyLook(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = st[c.index]; |
| +добавлено | if (name === 'charge') { |
| +добавлено | applyLook(); |
| +добавлено | releaseAll(d); |
| +добавлено | c.durations.impact = STRIKE + 0.75 / p.freeze + p.hold; // strike, freeze, hold; the shatter opens linger |
| +добавлено | for (let i = 0; i < N_CHARGE; i++) d.charge[i] = bolt(c.from, c.from, 0, 0, 0.25, 0.012, 8, 1, false); |
| +добавлено | d.L1 = fx.lights.acquire(); |
| +добавлено | if (d.L1) { d.L1.range = 7; d.L1.intensity = 0; d.L1.color.copy(lightCol); d.L1.type = 0; d.L1.shadow = true; } |
| +добавлено | d.L2 = fx.lights.acquire(); |
| +добавлено | if (d.L2) { d.L2.range = 8; d.L2.intensity = 0; d.L2.color.copy(glow).lerp(core, 0.2); d.L2.type = 0; } |
| +добавлено | d.strikes = 0; d.frozen = false; d.shattered = false; d.reseed = 0; d.nextSplat = 0; d.pulseT = 0; |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | for (let i = 0; i < N_CHARGE; i++) { d.charge[i]?.release(); d.charge[i] = null; } |
| +добавлено | strike(c, p, d); |
| +добавлено | } else if (name === 'linger') { |
| +добавлено | shatterLattice(c, p, d); |
| +добавлено | } else if (name === 'done') { |
| +добавлено | releaseAll(d); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = st[c.index], L1 = d.L1, L2 = d.L2; |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge; |
| +добавлено | // micro-arcs over the focus and cold motes streaming in; a frost ring creeps in at the target |
| +добавлено | d.reseed -= dt; |
| +добавлено | for (let i = 0; i < N_CHARGE; i++) { |
| +добавлено | const h = d.charge[i]; |
| +добавлено | if (!h) continue; |
| +добавлено | if (d.reseed <= 0) { |
| +добавлено | h.a.copy(c.from).add(_a.set(fx.rand() - 0.5, fx.rand() - 0.5, fx.rand() - 0.5).normalize().multiplyScalar(0.3)); |
| +добавлено | h.b.copy(c.from).add(_a.set(fx.rand() - 0.5, fx.rand() - 0.5, fx.rand() - 0.5).normalize().multiplyScalar(0.3 + 0.5 * k * fx.rand())); |
| +добавлено | h.seed = seedInt(); |
| +добавлено | } |
| +добавлено | h.intensity = 26 * p.intensity * (0.4 + 0.6 * k) * (fx.rand() < 0.75 ? 1 : 0); |
| +добавлено | } |
| +добавлено | if (d.reseed <= 0) d.reseed = 0.05; |
| +добавлено | mote.pos.copy(c.from); mote.vel.set(0, 0, 0); mote.rate(1100 * p.glints * k, dt); |
| +добавлено | eMote.pos.copy(c.from); eMote.vel.set(0, 0, 0); eMote.rate(900 * k, dt); |
| +добавлено | mist.pos.copy(c.from); mist.vel.set(0, -0.4, 0); mist.size = 0.6; mist.rate(60 * p.mist, dt); |
| +добавлено | if (c.t >= d.nextSplat) { d.nextSplat = c.t + 0.12; splat(c.to.x, c.to.z, 2.4 - 1.5 * k, 0.9 * p.frost * k, 0, 0, 'ring', c.seed + c.t * 100, c.to.x, c.to.z, 0.35); } |
| +добавлено | tMote.pos.set(c.to.x, 0.5, c.to.z); tMote.vel.set(0, 0.3, 0); tMote.rate(500 * p.glints * k, dt); |
| +добавлено | if (L1) { L1.pos.copy(c.from); L1.intensity = 11 * k * p.intensity * (0.8 + 0.2 * Math.sin(c.age * 41)); } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | const m = d.bolt, Lt = d.lattice; |
| +добавлено | if (c.phase === 'impact') { |
| +добавлено | const t = c.t; |
| +добавлено | // strike: leader, return stroke, one restrike on the same channel and branches |
| +добавлено | if (m) { |
| +добавлено | if (t < 0.07) { m.reveal = t / 0.07; m.intensity = 8 * p.intensity; } |
| +добавлено | else { |
| +добавлено | m.reveal = 1; |
| +добавлено | if (d.strikes === 0) { d.strikes = 1; m.intensity = 75 * p.intensity; strikeBurst(c, p, d); } |
| +добавлено | else if (d.strikes === 1 && t >= 0.12) { d.strikes = 2; m.intensity = 55 * p.intensity; fx.flash(0.2 * p.intensity); } |
| +добавлено | else if (t < STRIKE) m.intensity *= Math.exp(-dt * 18); |
| +добавлено | else { |
| +добавлено | // the light drains into the ice as the front passes, down to a trapped glow that |
| +добавлено | // keeps the bolt's scatter halo around the lattice; it strains before the shatter |
| +добавлено | const fr = (t - STRIKE) * d.speed / Math.max(d.mainLen, 1); |
| +добавлено | const strain = smooth(c.durations.impact - 0.35, c.durations.impact, t); |
| +добавлено | const hold = 2.4 * p.intensity * (0.75 + 0.25 * Math.sin(c.age * 23) * Math.sin(c.age * 7.1)) * (1 + 2.5 * strain * fx.rand()); |
| +добавлено | m.intensity = Math.max(hold, Math.min(m.intensity, 9 * p.intensity * (1 - fr)) * (0.75 + 0.25 * fx.rand())); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | const ft = Math.max(0, t - STRIKE); |
| +добавлено | if (Lt) { |
| +добавлено | Lt.mesh.visible = true; |
| +добавлено | const u = Lt.u; |
| +добавлено | u.uFreeze.value.x = t < STRIKE ? -1 : ft; |
| +добавлено | u.uFreeze.value.z = -1; |
| +добавлено | d.pulseT += dt; |
| +добавлено | const frozenAll = ft > Lt.maxFront + 0.15; |
| +добавлено | if (frozenAll && !d.frozen) d.frozen = true; |
| +добавлено | // trapped light: a slow pulse runs target -> caster every ~1.1 s once frozen |
| +добавлено | u.uTrap.value.set((0.7 + 0.35 * Math.sin(c.age * 23) * Math.sin(c.age * 7.1)) * p.intensity, d.frozen ? (d.pulseT * 1.4) % (Lt.maxFront + 0.8) - 0.3 : -9, smooth(c.durations.impact - 0.35, c.durations.impact, t), 1.6 * p.intensity); |
| +добавлено | // where the front is now: glints burst off it, mist pours off the frozen channel |
| +добавлено | const fr = Math.min(1, ft * d.speed / Math.max(d.mainLen, 1)); |
| +добавлено | if (t >= STRIKE && fr < 1) { |
| +добавлено | d.front.lerpVectors(d.B, d.A, fr); |
| +добавлено | glint.pos.copy(d.front); glint.vel.set(0, 0.5, 0); glint.rate(900 * p.glints, dt); |
| +добавлено | mist.pos.copy(d.front); mist.vel.set(0, -0.6, 0); mist.size = 0.8; mist.rate(90 * p.mist, dt); |
| +добавлено | } |
| +добавлено | if (t >= STRIKE) { |
| +добавлено | lineMist.pos.copy(d.B); lineMist.p.copy(d.front); lineMist.vel.set(0, -0.35, 0); lineMist.size = 0.9; lineMist.rate(70 * p.mist, dt); |
| +добавлено | lineGlint.pos.copy(d.B); lineGlint.p.copy(d.front); lineGlint.vel.set(0, 0.3, 0); lineGlint.rate(140 * p.glints, dt); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // floor frost follows the front under the channel |
| +добавлено | if (t >= STRIKE && t >= d.nextSplat) { |
| +добавлено | d.nextSplat = t + 0.1; |
| +добавлено | const fr = Math.min(1, ft * d.speed / Math.max(d.mainLen, 1)); |
| +добавлено | _a.lerpVectors(d.B, d.A, fr); |
| +добавлено | splat(d.B.x, d.B.z, 0.5, 0.55 * p.frost, 0, 0, 'line', c.seed + 3, _a.x, _a.z, 0.5); |
| +добавлено | } |
| +добавлено | if (L1) { |
| +добавлено | if (t < STRIKE) L1.intensity = (m ? m.intensity : 0) * 3; |
| +добавлено | else if (L1.type === 1) { |
| +добавлено | // a line light this close to the polished floor mirrors as a hard streak: strike only, |
| +добавлено | // then a point light in the middle of the lattice carries its trapped glow |
| +добавлено | L1.intensity *= Math.exp(-dt / 0.05); |
| +добавлено | if (t > STRIKE + 0.2) { L1.type = 0; L1.intensity = 0; L1.range = 7; L1.pos.lerpVectors(d.A, d.B, 0.55).y += 0.2; } |
| +добавлено | } else { |
| +добавлено | const fr = Math.min(1, ft * d.speed / Math.max(d.mainLen, 1)); |
| +добавлено | L1.intensity = 4 * p.intensity * fr * (0.8 + 0.2 * Math.sin(c.age * 23) * Math.sin(c.age * 7.1)); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // the cage glows from inside while the lattice holds |
| +добавлено | if (L2) L2.intensity = Math.max(L2.intensity * Math.exp(-dt / 0.2), 5 * p.intensity * (0.8 + 0.2 * Math.sin(c.age * 19) * Math.sin(c.age * 7.3))); |
| +добавлено | // the dummy stays frozen while caged |
| +добавлено | if (c.target >= 0) fx.dummies[c.target].status.frozen = 1; |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | // linger: the lattice bursts and falls; the escaped charge crawls over the floor |
| +добавлено | const fade = Math.max(0, 1 - c.t / c.durations.linger); |
| +добавлено | if (Lt) { |
| +добавлено | Lt.u.uFreeze.value.z = c.t; |
| +добавлено | Lt.u.uTrap.value.x *= Math.exp(-dt * 6); Lt.u.uTrap.value.z *= Math.exp(-dt * 10); Lt.u.uTrap.value.w = 0; |
| +добавлено | if (c.t > 1.4 + Lt.maxFront * 0.25) { dropLattice(Lt); d.lattice = null; } |
| +добавлено | } |
| +добавлено | for (let i = 0; i < d.nc; i++) { |
| +добавлено | const h = d.crawl[i]; |
| +добавлено | if (!h) continue; |
| +добавлено | h.intensity = fx.rand() < 0.45 * fade + 0.1 ? 30 * p.intensity * fade * fade : 0; |
| +добавлено | if (fx.rand() < 0.25) h.branchSeed = seedInt(); |
| +добавлено | } |
| +добавлено | if (c.t < 1.6) { mist.pos.copy(d.g).setY(0.3); mist.vel.set(0, 0, 0); mist.size = 1.1; mist.rate(40 * p.mist * (1 - c.t / 1.6), dt); } |
| +добавлено | if (L1) L1.intensity = L1.intensity > 0.05 ? L1.intensity * Math.exp(-dt / 0.12) : 0; |
| +добавлено | if (L2) L2.intensity = L2.intensity > 0.05 ? L2.intensity * Math.exp(-dt / 0.12) : 0; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | function strike(c, p, d) { |
| +добавлено | d.A.copy(c.from); |
| +добавлено | d.B.copy(c.to); |
| +добавлено | if (c.target < 0) d.B.y = Math.max(d.B.y, 0.02); |
| +добавлено | d.g.set(d.B.x, 0, d.B.z); |
| +добавлено | const nb = Math.round(p.branches); |
| +добавлено | d.bolt = bolt(d.A, d.B, nb, Math.min(nb, MAX_SUB), p.jag, 0.03, 12, 0, false); |
| +добавлено | if (d.L1) { d.L1.type = 1; d.L1.pos.copy(d.A); d.L1.end.copy(d.B); d.L1.range = 14; d.L1.color.copy(lightCol); } |
| +добавлено | d.lattice = takeLattice(); |
| +добавлено | if (d.bolt && d.lattice) { |
| +добавлено | rs = (Math.imul(c.seed | 0, 2654435761) + 12345) >>> 0; |
| +добавлено | buildLattice(d.lattice, d.bolt, d.A, d.B, p, d, rnd); |
| +добавлено | d.lattice.u.uFreeze.value.set(-1, 0.12 / p.freeze, -1, 0.22); |
| +добавлено | d.lattice.u.uTrap.value.set(0, -9, 0, 0); |
| +добавлено | } else { dropLattice(d.lattice); d.lattice = null; d.speed = 10; d.mainLen = d.A.distanceTo(d.B); } // bolt pool full: no ice |
| +добавлено | } |
| +добавлено | |
| +добавлено | function strikeBurst(c, p, d) { |
| +добавлено | const B = d.B, g = d.g; |
| +добавлено | fx.flash(0.4 * p.intensity); |
| +добавлено | spark.pos.copy(B); spark.vel.set(0, 1, 0); spark.burst(1400 * p.intensity); |
| +добавлено | if (d.bolt) { lineSpark.pos.copy(d.bolt.point(0.75, _a)); lineSpark.p.copy(B); lineSpark.burst(300); } |
| +добавлено | splat(g.x, g.z, 2.2, 1.6 * p.frost, 0.9, 0.4, 'lichtenberg', c.seed); |
| +добавлено | splat(g.x, g.z, 1.3, 1.8 * p.frost, 0, 0, 'radial', c.seed + 11); |
| +добавлено | fx.impacts.add('electric', B, 2.2, 1, 2, core); |
| +добавлено | fx.impacts.add('frost', g, 3.4, 1, 7, glow); |
| +добавлено | shock(_a.copy(g).setY(0.04), 0.3, 4.5, 0.45, 0.5, 0.05, 1.1, true); |
| +добавлено | fx.rig.shake(0.45, 24); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(2, 0.06); fx.rig.hitstop(0.05); } |
| +добавлено | if (d.L2) { d.L2.pos.copy(B).setY(Math.max(B.y, 0.5) + 0.3); d.L2.range = 7; d.L2.intensity = 40 * p.intensity; } |
| +добавлено | // ice cage: spikes erupt around the target as the front arrives there |
| +добавлено | const n = 5, a0 = fx.rand() * TAU; |
| +добавлено | for (let i = 0; i < n; i++) { |
| +добавлено | const ang = a0 + i * TAU / n + (fx.rand() - 0.5) * 0.4, r = 0.75 + 0.25 * fx.rand(); |
| +добавлено | SPK.pos.set(g.x + Math.cos(ang) * r, 0, g.z + Math.sin(ang) * r); |
| +добавлено | SPK.dir.set(-Math.cos(ang) * 0.35, 1, -Math.sin(ang) * 0.35); // leaning in over the target |
| +добавлено | SPK.height = 0.9 + 0.6 * fx.rand(); SPK.radius = 0.24 + 0.08 * fx.rand(); |
| +добавлено | SPK.delay = STRIKE + 0.04 * i; SPK.rise = 0.16; SPK.life = c.durations.impact - SPK.delay + 0.08; SPK.crumble = 0.6; |
| +добавлено | SPK.seed = (c.seed * 31 + i * 7919) & 0xffffff; |
| +добавлено | fx.spikes.spawn(SPK); |
| +добавлено | } |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i], dist = Math.hypot(dm.pos.x - g.x, dm.pos.z - g.z); |
| +добавлено | if (i === c.target || dist < 2.2) { dm.status.shock = 1; dm.status.frozen = Math.max(dm.status.frozen, 1 - 0.3 * dist / 2.2); } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function shatterLattice(c, p, d) { |
| +добавлено | const A = d.A, B = d.B, g = d.g; |
| +добавлено | if (d.bolt) { d.bolt.release(); d.bolt = null; } |
| +добавлено | lineGlint.pos.copy(A); lineGlint.p.copy(B); lineGlint.vel.set(0, 1.5, 0); lineGlint.burst(700 * p.glints); |
| +добавлено | shard.pos.copy(B); shard.p.copy(A); shard.vel.set(0, 1.5, 0); shard.size = 0.8; shard.burst(70 * p.shatter); |
| +добавлено | chip.pos.copy(B); chip.p.copy(A); chip.vel.set(0, 1, 0); chip.burst(160 * p.shatter); |
| +добавлено | lineMist.pos.copy(A); lineMist.p.copy(B); lineMist.vel.set(0, -0.2, 0); lineMist.size = 1.2; lineMist.burst(90 * p.mist); |
| +добавлено | lineSpark.pos.copy(A); lineSpark.p.copy(B); lineSpark.burst(400 * p.intensity); |
| +добавлено | glint.pos.copy(B); glint.vel.set(0, 2, 0); glint.burst(300 * p.glints); |
| +добавлено | shock(_a.copy(g).setY(0.04), 0.3, 3.6, 0.4, 0.45, 0.04, 0.9, true); |
| +добавлено | shock(B, 0.2, 2.6, 0.3, 0.4, 0.05, 0, false); |
| +добавлено | fx.impacts.add('frost', g, 2.6, 0.8, 5, glow); |
| +добавлено | if (d.L1) d.L1.intensity = 28 * p.intensity; |
| +добавлено | if (d.L2) { d.L2.pos.copy(B); d.L2.intensity = 25 * p.intensity; } |
| +добавлено | fx.flash(0.14 * p.intensity); |
| +добавлено | fx.rig.shake(0.35, 18); |
| +добавлено | if (!fx.reduced) fx.rig.punch(1.2, 0.06); |
| +добавлено | // the trapped charge escapes along the floor |
| +добавлено | d.nc = 0; |
| +добавлено | for (let i = 0; i < MAX_CRAWL; i++) { |
| +добавлено | const ang = (i / MAX_CRAWL) * TAU + fx.rand(), len = 1.2 + 1.6 * fx.rand(); |
| +добавлено | _a.copy(g).setY(0.05); |
| +добавлено | _b.set(g.x + Math.cos(ang) * len, 0.02, g.z + Math.sin(ang) * len); |
| +добавлено | const h = bolt(_a, _b, 2, 2, 0.18, 0.012, 8, 1, true); |
| +добавлено | if (h) d.crawl[d.nc++] = h; |
| +добавлено | } |
| +добавлено | splat(g.x, g.z, 1.8, 0, 0.7, 0, 'lichtenberg', c.seed + 5); |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i], dist = Math.hypot(dm.pos.x - g.x, dm.pos.z - g.z); |
| +добавлено | if (i === c.target || dist < 2.5) { dm.status.shock = 1; dm.hit(_d.subVectors(dm.pos, A).setY(0).normalize(), 0.7 * Math.max(0.3, 1 - dist / 2.5)); } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { COMMON_GLSL, NOISE_GLSL, WORLD_GLSL, LIGHTS_GLSL, U, arenaSDF } from '../vfx/common.js'; |
| +добавлено | import { REFRACT_GLSL } from '../vfx/Refraction.js'; |
| +добавлено | |
| +добавлено | // Ice (PLAN 3). Glacial Spike: a faceted crystal grows at the focus (vertices clamped behind a |
| +добавлено | // growth front whose cap glows), cold mist sinks off it and ~1k motes stream in. It flies as a |
| +добавлено | // ~190-triangle hexagonal shard in the refract pass (IOR 1.31, analytic ellipsoid thickness, |
| +добавлено | // Beer-Lambert cyan, flat facets, a fake interior of bubbles / veils / cracks from 3 noise taps |
| +добавлено | // along the refracted ray, rime on the crystal edges and a frosty rim), shedding mist and chips. |
| +добавлено | // Impact = Frost Nova: shard debris, a ring of 8 ice spikes, a frost splat of potential 1.8 whose |
| +добавлено | // front crawls out as fern fingers (wet ground freezes to a sheet), a nova shock + hoarfrost |
| +добавлено | // ring, cold mist, frozen dummies. The field then melts the frost into wet; mist lingers on it. |
| +добавлено | // Ice Wall: a frost line races along the floor and a curved, staggered row of large ice spikes |
| +добавлено | // erupts across the caster -> target line (the spikes are occluders, so particles stop at it). |
| +добавлено | |
| +добавлено | const SHARD_VERT = /* glsl */` |
| +добавлено | in vec4 aGrowA; // crystal base (local), crystal length |
| +добавлено | in vec4 aGrowB; // crystal axis (local), growth start 0..1 |
| +добавлено | in vec3 aEdge; // distance to the triangle's 3 edges (local units), +8 on edges that aren't crystal edges |
| +добавлено | uniform vec4 uShard; // growth 0..1, front glow, seed, inner glow |
| +добавлено | out vec3 vWorld; |
| +добавлено | out vec3 vLocal; // deformed (grown) local position |
| +добавлено | out vec3 vRest; // undeformed: interior noise and frost live here, so they don't swim while growing |
| +добавлено | out vec3 vEdge; |
| +добавлено | out float vFront; // local units below this crystal's growth front (0 on the forming cap) |
| +добавлено | void main() { |
| +добавлено | vec3 p = position; |
| +добавлено | float g = clamp((uShard.x - aGrowB.w) / max(1.0 - aGrowB.w, 1e-3), 0.0, 1.0); |
| +добавлено | // vertices past the front are clamped onto it: the tip triangles fold into a flat cap |
| +добавлено | float h = dot(p - aGrowA.xyz, aGrowB.xyz), hmax = g * aGrowA.w; |
| +добавлено | p -= aGrowB.xyz * max(h - hmax, 0.0); |
| +добавлено | vec3 rad = p - aGrowA.xyz - aGrowB.xyz * min(h, hmax); |
| +добавлено | p -= rad * (1.0 - mix(0.3, 1.0, sqrt(g))); // thin while young |
| +добавлено | vFront = hmax - min(h, hmax); |
| +добавлено | vLocal = p; vRest = position; vEdge = aEdge; |
| +добавлено | vec4 wp = modelMatrix * vec4(p, 1.0); |
| +добавлено | vWorld = wp.xyz; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * wp; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const SHARD_FRAG = COMMON_GLSL + NOISE_GLSL + WORLD_GLSL + LIGHTS_GLSL + REFRACT_GLSL + /* glsl */` |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | uniform vec4 uShard; // growth 0..1, front glow, seed, inner glow |
| +добавлено | uniform vec4 uLook; // ior, frost, interior, glints |
| +добавлено | uniform vec4 uLook2; // lens, metres per local unit, rim frost, - |
| +добавлено | uniform vec3 uAbsorb; // Beer-Lambert sigma per metre |
| +добавлено | uniform vec3 uInscat; // in-scatter colour of the body |
| +добавлено | uniform vec3 uGlow; // growth-front / core emission (linear HDR) |
| +добавлено | uniform mat3 uToLocal; // world -> shard rotation |
| +добавлено | in vec3 vWorld; |
| +добавлено | in vec3 vLocal; |
| +добавлено | in vec3 vRest; |
| +добавлено | in vec3 vEdge; |
| +добавлено | in float vFront; |
| +добавлено | out vec4 fragColor; |
| +добавлено | |
| +добавлено | // exit distance from o (on or inside) along d through an axis-aligned ellipsoid (centre c, semi-axes ax) |
| +добавлено | float ellipsoidExit(vec3 o, vec3 d, vec3 c, vec3 ax) { |
| +добавлено | vec3 oo = (o - c) / ax, dd = d / ax; |
| +добавлено | float a = dot(dd, dd), b = dot(oo, dd), cc = dot(oo, oo) - 1.0; |
| +добавлено | return max((-b + sqrt(max(b * b - a * cc, 0.0))) / a, 0.0); |
| +добавлено | } |
| +добавлено | |
| +добавлено | void main() { |
| +добавлено | vec3 N = normalize(cross(dFdx(vWorld), dFdy(vWorld))); // flat facets |
| +добавлено | vec3 V = normalize(cameraPosition - vWorld); |
| +добавлено | if (dot(N, V) < 0.0) N = -N; |
| +добавлено | float NoV = max(dot(N, V), 1e-3); |
| +добавлено | float ior = uLook.x, S = uLook2.y, g = clamp(uShard.x, 0.05, 1.0), seed = uShard.z; |
| +добавлено | |
| +добавлено | // --- through the body: analytic convex thickness of the main crystal's ellipsoid (grown with it) |
| +добавлено | vec3 R = refract(-V, N, 1.0 / ior); |
| +добавлено | vec3 ld = uToLocal * R; |
| +добавлено | float sg = mix(0.3, 1.0, sqrt(g)); |
| +добавлено | float tL = max(ellipsoidExit(vLocal, ld, vec3(0.0, 0.0, -1.0 + 0.5 * g), vec3(0.1 * sg, 0.1 * sg, 0.5 * g)), 0.03); |
| +добавлено | float thick = tL * S; |
| +добавлено | float behind; |
| +добавлено | vec3 bg = sceneRefract(vWorld, N, V, ior, thick * uLook2.x, behind); |
| +добавлено | vec3 Tr = beerLambert(uAbsorb, thick); |
| +добавлено | |
| +добавлено | // --- light: spell lights (GGX on the flat facet), sun, sky |
| +добавлено | vec3 sd, ss; |
| +добавлено | gLightNoShadow = true; |
| +добавлено | spellLightPBR(vWorld, N, V, 0.03, vec3(0.018), vec3(1.0), sd, ss); |
| +добавлено | float NoL = max(dot(N, uSunDir), 0.0); |
| +добавлено | vec3 H = normalize(uSunDir + V); |
| +добавлено | float sunSpec = spellD_GGX(max(dot(N, H), 0.0), 0.0009) * spellV_Smith(NoV, NoL, 0.0009) * NoL * PI; |
| +добавлено | vec3 irr = uAmbient + uSunColor * NoL + sd; |
| +добавлено | vec3 irrOmni = uAmbient + uSunColor * 0.5 + sd; |
| +добавлено | |
| +добавлено | // --- fake interior: 3 taps along the refracted ray read as bubbles, veils and cracks |
| +добавлено | float scat = 0.0; |
| +добавлено | for (int i = 1; i <= 3; i++) { |
| +добавлено | float f = float(i) * 0.25; |
| +добавлено | vec3 q = (vRest + ld * tL * f) * S + seed; |
| +добавлено | vec4 n = noise3v(q * 1.1); // veils (fbm) and cracks (worley iso-sheets) |
| +добавлено | float b = noise3v(q * 4.5 + 0.37).a; // bubbles: small worley cells |
| +добавлено | float veil = smoothstep(0.62, 0.9, n.r) * 0.25; |
| +добавлено | float bubble = smoothstep(0.9, 0.97, b) * 2.2; |
| +добавлено | float crack = smoothstep(0.025, 0.0, abs(n.g - 0.55)) * smoothstep(0.35, 0.6, n.r) * 1.6; |
| +добавлено | scat += (veil + bubble + crack) * exp(-uAbsorb.g * thick * f); |
| +добавлено | } |
| +добавлено | scat *= uLook.z / 3.0; |
| +добавлено | |
| +добавлено | // --- frost coat: rime on the crystal edges, a rough white rim at grazing angles, a frosted tail |
| +добавлено | vec3 fp = vRest * S; |
| +добавлено | vec4 fe = noise3v(fp * 2.6 + seed * 1.7); |
| +добавлено | float ed = min(min(vEdge.x, vEdge.y), vEdge.z) * S; |
| +добавлено | float ew = 0.014 * (0.25 + 1.75 * fe.r * fe.r); |
| +добавлено | float frost = smoothstep(ew, ew * 0.1, ed - (fe.g - 0.5) * 0.008); |
| +добавлено | frost = max(frost, pow(1.0 - NoV, 2.5) * smoothstep(0.35, 0.75, fe.b + 0.15) * uLook2.z); |
| +добавлено | frost = max(frost, smoothstep(-0.6, -0.95, vRest.z + (fe.a - 0.5) * 0.25) * 0.9); |
| +добавлено | frost = clamp(frost * uLook.y, 0.0, 1.0); |
| +добавлено | |
| +добавлено | // --- compose: transmitted scene + cyan in-scatter + interior, Fresnel sky + highlights, frost |
| +добавлено | float F = fresnel(NoV, 0.018); |
| +добавлено | vec3 refl = envReflect(reflect(-V, N), 0.03); |
| +добавлено | // in-scatter keeps its own (cyan) colour: weighting it per channel by 1 - Tr would leave only green |
| +добавлено | vec3 body = bg * Tr + uInscat * irrOmni * 0.5 * (1.0 - dot(Tr, vec3(0.3333))) + vec3(0.8, 0.92, 1.0) * irrOmni * scat * 0.25; |
| +добавлено | vec3 col = body * (1.0 - F) + (refl * F + ss + uSunColor * F * sunSpec) * (1.0 - frost * 0.8); |
| +добавлено | col = mix(col, vec3(0.85, 0.93, 1.0) * irr, frost * 0.85); |
| +добавлено | |
| +добавлено | // glints: random micro-facets whose hash changes with the view, twinkling; faded when sub-pixel |
| +добавлено | vec3 gq = fp * 42.0; |
| +добавлено | vec3 gh = hash33(floor(gq) + floor(V * 5.0) * 17.0 + seed); |
| +добавлено | float tw = pow(0.5 + 0.5 * sin(uTime * 7.0 + gh.y * 60.0), 6.0); |
| +добавлено | float spot = smoothstep(0.35, 0.0, length(fract(gq) - 0.5 - (gh - 0.5) * 0.4)); |
| +добавлено | float glint = step(0.9, gh.x) * spot * tw * smoothstep(1.2, 0.4, length(fwidth(gq))) * (0.35 + frost); |
| +добавлено | col += glint * (irr * 6.0 + ss * 0.5) * uLook.w; |
| +добавлено | |
| +добавлено | // growth front: the forming cap and a band under it glow; a faint cold core in flight |
| +добавлено | float band = exp(-vFront * 45.0) * uShard.y; |
| +добавлено | col += uGlow * (band * 5.0 + uShard.w * (0.35 * scat + 0.6 * pow(1.0 - NoV, 3.0) + 0.04)); |
| +добавлено | fragColor = vec4(col, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | function lcg(seed) { |
| +добавлено | let s = (seed >>> 0) || 1; |
| +добавлено | return () => ((s = (Math.imul(s, 1664525) + 1013904223) >>> 0) / 4294967296); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // Glacial spike: a main hexagonal crystal along +z (back point z = -1, tip exactly at the origin, |
| +добавлено | // so the mesh position is the tip) and 7 small crystals sprouting off its back half. Non-indexed; |
| +добавлено | // per vertex the crystal's growth axis and the distance to each crystal edge (quad diagonals |
| +добавлено | // don't count). ~190 triangles. |
| +добавлено | function shardGeometry(seed) { |
| +добавлено | const rand = lcg(seed); |
| +добавлено | const pos = [], ga = [], gb = [], edge = []; |
| +добавлено | const e1 = new THREE.Vector3(), e2 = new THREE.Vector3(), ab = new THREE.Vector3(); |
| +добавлено | function tri(A, B, C, cr, f0, f1, f2) { // fk: the edge opposite vertex k is a crystal edge |
| +добавлено | pos.push(A.x, A.y, A.z, B.x, B.y, B.z, C.x, C.y, C.z); |
| +добавлено | const area2 = e1.subVectors(B, A).cross(e2.subVectors(C, A)).length(); |
| +добавлено | const h = [area2 / Math.max(ab.subVectors(B, C).length(), 1e-6), area2 / Math.max(ab.subVectors(A, C).length(), 1e-6), area2 / Math.max(ab.subVectors(A, B).length(), 1e-6)]; |
| +добавлено | const off = [f0 ? 0 : 8, f1 ? 0 : 8, f2 ? 0 : 8]; |
| +добавлено | for (let v = 0; v < 3; v++) { |
| +добавлено | edge.push((v === 0 ? h[0] : 0) + off[0], (v === 1 ? h[1] : 0) + off[1], (v === 2 ? h[2] : 0) + off[2]); |
| +добавлено | ga.push(cr.base.x, cr.base.y, cr.base.z, cr.len); |
| +добавлено | gb.push(cr.axis.x, cr.axis.y, cr.axis.z, cr.start); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // rings: [height along the axis, radius]; back: close the base with a point (main crystal) |
| +добавлено | function crystal(base, axis, rings, tip, start, back) { |
| +добавлено | const s = new THREE.Vector3(rand() - 0.5, rand() - 0.5, rand() - 0.5).cross(axis).normalize(); |
| +добавлено | const u = new THREE.Vector3().crossVectors(axis, s); |
| +добавлено | const tw = rand() * Math.PI * 2; |
| +добавлено | const R = rings.map(([hh, r]) => Array.from({ length: 6 }, (_, k) => { |
| +добавлено | const t = tw + k * Math.PI / 3 + (rand() - 0.5) * 0.1, rr = r * (0.92 + rand() * 0.16); |
| +добавлено | return base.clone().addScaledVector(axis, hh).addScaledVector(s, Math.cos(t) * rr).addScaledVector(u, Math.sin(t) * rr); |
| +добавлено | })); |
| +добавлено | const cr = { base, axis, len: tip, start }; |
| +добавлено | const last = R.length - 1; |
| +добавлено | for (let i = 0; i < last; i++) { |
| +добавлено | const a = R[i], b = R[i + 1], ringA = i === 0, ringB = i + 1 === last; |
| +добавлено | for (let k = 0; k < 6; k++) { |
| +добавлено | const j = (k + 1) % 6; |
| +добавлено | tri(a[k], a[j], b[j], cr, true, false, ringA); |
| +добавлено | tri(a[k], b[j], b[k], cr, ringB, true, false); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | const apex = base.clone().addScaledVector(axis, tip); |
| +добавлено | for (let k = 0; k < 6; k++) tri(R[last][k], R[last][(k + 1) % 6], apex, cr, true, true, true); |
| +добавлено | if (back) for (let k = 0; k < 6; k++) tri(base, R[0][(k + 1) % 6], R[0][k], cr, true, true, true); |
| +добавлено | } |
| +добавлено | const Z = new THREE.Vector3(0, 0, 1); |
| +добавлено | crystal(new THREE.Vector3(0, 0, -1), Z, [[0.1, 0.06], [0.28, 0.095], [0.48, 0.1], [0.66, 0.085], [0.8, 0.06]], 1, 0, true); |
| +добавлено | for (let i = 0; i < 7; i++) { |
| +добавлено | const phi = (i / 7) * Math.PI * 2 + rand() * 0.6, radial = new THREE.Vector3(Math.cos(phi), Math.sin(phi), 0); |
| +добавлено | const fwd = i % 3 === 2; // a few lie along the body, the rest flare backwards |
| +добавлено | const axis = radial.clone().multiplyScalar(fwd ? 0.4 : 0.8).add(new THREE.Vector3(0, 0, fwd ? 0.9 : -0.35 - rand() * 0.4)).normalize(); |
| +добавлено | const base = radial.clone().multiplyScalar(0.04).setZ(-0.92 + rand() * 0.38); |
| +добавлено | const w = 0.022 + rand() * 0.024, L = (fwd ? 0.22 : 0.1) + rand() * 0.12; |
| +добавлено | crystal(base, axis, [[0, w * 1.1], [L, w]], L + w * (1.3 + rand() * 0.9), 0.3 + rand() * 0.4, false); |
| +добавлено | } |
| +добавлено | const g = new THREE.BufferGeometry(); |
| +добавлено | g.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3)); |
| +добавлено | g.setAttribute('aGrowA', new THREE.Float32BufferAttribute(ga, 4)); |
| +добавлено | g.setAttribute('aGrowB', new THREE.Float32BufferAttribute(gb, 4)); |
| +добавлено | g.setAttribute('aEdge', new THREE.Float32BufferAttribute(edge, 3)); |
| +добавлено | g.computeBoundingSphere(); |
| +добавлено | return g; |
| +добавлено | } |
| +добавлено | |
| +добавлено | const MAX_SHARDS = 3; |
| +добавлено | const ZAXIS = new THREE.Vector3(0, 0, 1); |
| +добавлено | const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); }; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'ice', name: 'Ice', key: '4', hue: '#9ff0ff', |
| +добавлено | shape: 'projectile', forms: ['Glacial Spike', 'Ice Wall'], radius: 0.12, |
| +добавлено | params: { |
| +добавлено | size: 1, speed: 24, ior: 1.31, clarity: 1, frost: 1, interior: 1, glints: 1, mist: 1, motes: 1, shards: 1, |
| +добавлено | nova: 1, spikes: 8, wallSpikes: 11, wallHeight: 3, wallWidth: 7.5, wallCurve: 1, tint: '#a8dcff', glow: '#5fd0ff', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | size: [0.4, 2.5, 0.01], speed: [6, 50, 0.1], ior: [1, 2.4, 0.01], clarity: [0.2, 4, 0.01], frost: [0, 2, 0.01], |
| +добавлено | interior: [0, 3, 0.01], glints: [0, 3, 0.01], mist: [0, 3, 0.01], motes: [0, 3, 0.01], shards: [0, 3, 0.01], |
| +добавлено | nova: [0.4, 2, 0.01], spikes: [0, 8, 1], wallSpikes: [3, 15, 1], wallHeight: [1, 4.5, 0.01], wallWidth: [3, 12, 0.1], wallCurve: [-1, 2, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | glacier: { tint: '#3fa6ff', clarity: 1.8, glow: '#3f86ff', frost: 0.7 }, |
| +добавлено | rime: { tint: '#d2f2ff', clarity: 0.6, frost: 1.8, interior: 1.8, mist: 1.7, glow: '#bfefff' }, |
| +добавлено | 'black ice': { tint: '#2f7d8f', clarity: 3, glints: 2.2, frost: 0.4, mist: 0.6, glow: '#6ff6ff' }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 0.8, impact: 0.3, linger: 6.5 }, |
| +добавлено | kinds: { |
| +добавлено | // sinking cold mist: negative buoyancy, slides along the floor (restitution 0, no friction) |
| +добавлено | mist: { style: 'media', drag: 1.8, buoyancy: -0.9, curl: 0.5, curlScale: 0.22, restitution: 0, friction: 0.02, life: [1.6, 2.8], size: [0.3, 1.2], sizePow: 0.6, color: ['#e2f5ff', '#bcd9ec'], alpha: [0.05, 0], density: 1.1, erosion: 0.6, fadeIn: 0.2, fadeOut: 0.45, forceResponse: 0.5, lightResponse: 1.6 }, |
| +добавлено | groundmist: { style: 'media', drag: 2.2, buoyancy: -0.12, curl: 0.45, curlScale: 0.2, life: [2, 3.6], size: [0.6, 1.9], color: ['#e8f7ff', '#c4dfef'], alpha: [0.025, 0], density: 0.9, erosion: 0.55, fadeIn: 0.35, fadeOut: 0.45, forceResponse: 0.8, lightResponse: 1.4 }, |
| +добавлено | mote: { style: 'glow', drag: 0.25, curl: 0.8, curlScale: 0.5, life: [0.5, 0.75], size: [0.012, 0.006], stretch: 0.008, emissive: 9, color: ['#ffffff', '#8fe6ff'], alpha: [0.25, 1], fadeIn: 0.35, fadeOut: 0.12, forceResponse: 0 }, |
| +добавлено | glint: { style: 'glow', drag: 0.6, gravity: 6.5, restitution: 0.3, friction: 0.45, life: [0.6, 1.6], size: [0.02, 0.006], stretch: 0.02, emissive: 7, color: ['#ffffff', '#86ddff'], alpha: [1, 0] }, |
| +добавлено | shard: { style: 'debris', mesh: 'shard', drag: 0.25, gravity: 9.8, restitution: 0.3, friction: 0.55, life: [3.2, 5.5], size: [0.3, 0.1], sizePow: 1.6, spin: 9, color: ['#e6f8ff', '#1d5f86'], alpha: [1, 0.04], fadeOut: 0.25 }, |
| +добавлено | chip: { style: 'debris', mesh: 'shard', drag: 0.4, gravity: 9.8, restitution: 0.25, friction: 0.5, life: [0.8, 1.6], size: [0.09, 0.03], spin: 14, color: ['#e6f8ff', '#1d5f86'], alpha: [1, 0.04], fadeOut: 0.3 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const mist = P.emitter('ice.mist', { shape: 'sphere', radius: 0.25, spread: 0.25 }); |
| +добавлено | const trailMist = P.emitter('ice.mist', { shape: 'sphere', radius: 0.1, spread: 0.35 }); |
| +добавлено | const novaMist = P.emitter('ice.mist', { shape: 'ring', radius: 0.7, spread: 0.35, radial: 2.4 }); |
| +добавлено | const lineMist = P.emitter('ice.mist', { shape: 'line', radius: 0.35, spread: 0.6 }); |
| +добавлено | const mote = P.emitter('ice.mote', { shape: 'shell', radius: 1.3, spread: 0.1, radial: -2.2 }); |
| +добавлено | const glint = P.emitter('ice.glint', { shape: 'sphere', radius: 0.25, spread: 4.5, radial: 3 }); |
| +добавлено | const lineGlint = P.emitter('ice.glint', { shape: 'line', radius: 0.3, spread: 2.5 }); |
| +добавлено | const shard = P.emitter('ice.shard', { shape: 'sphere', radius: 0.25, spread: 2, radial: 4 }); |
| +добавлено | const chip = P.emitter('ice.chip', { shape: 'sphere', radius: 0.05, spread: 1.4 }); |
| +добавлено | // GPU-driven: frost on the floor breathes mist until it melts (also frost left by combos) |
| +добавлено | P.fieldEmitter('ice.groundmist', { slots: 768, a: [0, 0, 0, 1], threshold: 0.3, rate: 6, up: 0.15 }); |
| +добавлено | |
| +добавлено | // --- look, shared by every shard (and pushed to the ice spikes) --- |
| +добавлено | const tint = new THREE.Color(), glowCol = new THREE.Color(), lightCol = new THREE.Color(), white = new THREE.Color(1, 1, 1); |
| +добавлено | const absorb = new THREE.Vector3(), inscat = new THREE.Vector3(), glowV = new THREE.Vector3(), look = new THREE.Vector4(); |
| +добавлено | const SP = fx.spikes.uniforms; |
| +добавлено | function applyLook() { |
| +добавлено | const p = S.params; |
| +добавлено | tint.set(p.tint); glowCol.set(p.glow); |
| +добавлено | const k = 6 * p.clarity; // tint = transmittance through ~17 cm of shard |
| +добавлено | absorb.set(-Math.log(Math.max(tint.r, 0.01)), -Math.log(Math.max(tint.g, 0.01)), -Math.log(Math.max(tint.b, 0.01))).multiplyScalar(k); |
| +добавлено | inscat.set(tint.r * tint.r, tint.g * tint.g, tint.b * tint.b).multiplyScalar(0.6); |
| +добавлено | glowV.set(glowCol.r, glowCol.g, glowCol.b).multiplyScalar(3); |
| +добавлено | look.set(p.ior, p.frost, p.interior, p.glints); |
| +добавлено | SP.uIceAbsorb.value.copy(absorb).multiplyScalar(0.35); // spikes: a much thicker body |
| +добавлено | SP.uIceLook.value.set(p.ior, p.frost, p.interior, p.glints); |
| +добавлено | SP.uIceGlow.value.copy(glowV).multiplyScalar(0.87); |
| +добавлено | lightCol.copy(glowCol).lerp(white, 0.35); |
| +добавлено | } |
| +добавлено | applyLook(); |
| +добавлено | |
| +добавлено | // --- shard meshes: one program (identical source), per-mesh uniforms --- |
| +добавлено | const geo = shardGeometry(4242); |
| +добавлено | const shards = []; |
| +добавлено | for (let i = 0; i < MAX_SHARDS; i++) { |
| +добавлено | const m = new THREE.Mesh(geo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: SHARD_VERT, fragmentShader: SHARD_FRAG, |
| +добавлено | uniforms: { |
| +добавлено | ...U, uShard: { value: new THREE.Vector4() }, uLook: { value: look }, uLook2: { value: new THREE.Vector4(3, 1, 1, 0) }, |
| +добавлено | uAbsorb: { value: absorb }, uInscat: { value: inscat }, uGlow: { value: glowV }, uToLocal: { value: new THREE.Matrix3() }, |
| +добавлено | }, |
| +добавлено | })); |
| +добавлено | m.visible = false; |
| +добавлено | m.userData.free = true; |
| +добавлено | fx.scenes.refract.add(m); |
| +добавлено | shards.push(m); |
| +добавлено | } |
| +добавлено | const takeShard = () => { for (const m of shards) if (m.userData.free) { m.userData.free = false; return m; } return null; }; |
| +добавлено | const dropShard = (m) => { if (m) { m.visible = false; m.userData.free = true; } }; |
| +добавлено | |
| +добавлено | // per-cast state by cast index (the cast pool is 16): nothing allocated per cast |
| +добавлено | const st = Array.from({ length: 16 }, () => ({ |
| +добавлено | shard: null, light: null, trail: null, spin: 0, boom: 0, ring: false, |
| +добавлено | ground: new THREE.Vector3(), last: new THREE.Vector3(), wallC: new THREE.Vector3(), |
| +добавлено | wallA: new THREE.Vector3(), wallB: new THREE.Vector3(), dirH: new THREE.Vector3(), |
| +добавлено | })); |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _d = new THREE.Vector3(), _perp = new THREE.Vector3(); |
| +добавлено | const _q = new THREE.Quaternion(), _q2 = new THREE.Quaternion(), _m4 = new THREE.Matrix4(); |
| +добавлено | // reused option objects (every field set on every call) |
| +добавлено | const SPK = { pos: new THREE.Vector3(), dir: new THREE.Vector3(), height: 2, radius: 0.4, material: 'ice', delay: 0, rise: 0.18, life: 4.5, crumble: 0.9, seed: 1 }; |
| +добавлено | const SPL = { x: 0, z: 0, radius: 1, frost: 0, wet: 0, pattern: 'radial', seed: 0, x2: 0, z2: 0, width: 0, lobe: 0 }; |
| +добавлено | const SHK = { pos: new THREE.Vector3(), radius0: 0.3, radius1: 4, duration: 0.5, thickness: 0.5, distortion: 0.04, color: glowCol, intensity: 0, ground: false }; |
| +добавлено | function splat(x, z, radius, frost, pattern, seed, x2 = x, z2 = z, width = 0, lobe = 0) { |
| +добавлено | SPL.x = x; SPL.z = z; SPL.radius = radius; SPL.frost = frost; SPL.pattern = pattern; SPL.seed = seed; |
| +добавлено | SPL.x2 = x2; SPL.z2 = z2; SPL.width = width; SPL.lobe = lobe; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | function shock(pos, r0, r1, dur, thick, dist, intensity, ground) { |
| +добавлено | SHK.pos.copy(pos); |
| +добавлено | SHK.radius0 = r0; SHK.radius1 = r1; SHK.duration = dur; SHK.thickness = thick; SHK.distortion = dist; |
| +добавлено | SHK.intensity = intensity; SHK.ground = ground; |
| +добавлено | fx.shocks.spawn(SHK); |
| +добавлено | } |
| +добавлено | function spike(pos, dir, height, radius, delay, rise, life, crumble, seed) { |
| +добавлено | SPK.pos.copy(pos); SPK.dir.copy(dir); |
| +добавлено | SPK.height = height; SPK.radius = radius; SPK.delay = delay; SPK.rise = rise; SPK.life = life; |
| +добавлено | SPK.crumble = crumble; SPK.seed = seed & 0xffffff; |
| +добавлено | return fx.spikes.spawn(SPK); |
| +добавлено | } |
| +добавлено | // a point of the wall: parabolic arc through C (bowing toward the target for curve > 0) |
| +добавлено | function wallPoint(C, perp, dirH, s, curve, D, out) { |
| +добавлено | return out.copy(C).addScaledVector(perp, s).addScaledVector(dirH, -curve * s * s / (2 * Math.max(D, 1))); |
| +добавлено | } |
| +добавлено | // orient the shard (tip at pos) along dir, spun about its axis |
| +добавлено | function poseShard(m, pos, dir, spin, size) { |
| +добавлено | m.position.copy(pos); |
| +добавлено | _q.setFromUnitVectors(ZAXIS, dir); |
| +добавлено | _q2.setFromAxisAngle(ZAXIS, spin); |
| +добавлено | m.quaternion.multiplyQuaternions(_q, _q2); |
| +добавлено | m.scale.setScalar(size); |
| +добавлено | m.material.uniforms.uToLocal.value.setFromMatrix4(_m4.makeRotationFromQuaternion(m.quaternion)).transpose(); |
| +добавлено | m.material.uniforms.uLook2.value.y = size; |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam() { applyLook(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = st[c.index]; |
| +добавлено | if (name === 'charge') { |
| +добавлено | applyLook(); |
| +добавлено | d.shard = takeShard(); |
| +добавлено | if (d.shard) { d.shard.visible = true; d.shard.material.uniforms.uShard.value.set(0, 1, (c.seed % 97) * 0.37, 0); } |
| +добавлено | d.light = fx.lights.acquire(); |
| +добавлено | if (d.light) { d.light.range = 7; d.light.intensity = 0; d.light.color.copy(lightCol); d.light.shadow = true; } |
| +добавлено | d.trail = null; d.boom = 0; d.ring = false; d.spin = (c.seed % 628) / 100; |
| +добавлено | } else if (name === 'release') { |
| +добавлено | fx.rig.shake(0.1, 12); |
| +добавлено | if (c.form === 'Ice Wall') { |
| +добавлено | // the crystal dives into the floor: a puff of glints and mist where it was |
| +добавлено | _a.copy(c.from).addScaledVector(c.dir, 0.6 * p.size); |
| +добавлено | glint.pos.copy(_a); glint.vel.set(0, -1, 0); glint.burst(120 * p.glints); |
| +добавлено | mist.pos.copy(_a); mist.vel.set(0, -1.5, 0); mist.size = p.size; mist.burst(30 * p.mist); |
| +добавлено | dropShard(d.shard); d.shard = null; |
| +добавлено | } else { |
| +добавлено | d.trail = fx.trails.acquire(); |
| +добавлено | const t = d.trail; |
| +добавлено | if (t) { t.width = 0.05 * p.size; t.intensity = 1.2; t.glow = 5; t.glowIntensity = 0.2; t.distortion = 0; t.fadeTime = 0.35; t.color.copy(lightCol); } |
| +добавлено | mist.pos.copy(c.from); mist.vel.copy(c.dir).multiplyScalar(2); mist.size = p.size; mist.burst(25 * p.mist); |
| +добавлено | } |
| +добавлено | } else if (name === 'travel') { |
| +добавлено | if (c.form === 'Ice Wall') { |
| +добавлено | // frost line from under the focus to the wall's centre |
| +добавлено | d.dirH.set(c.to.x - c.from.x, 0, c.to.z - c.from.z); |
| +добавлено | const D = d.dirH.length(); |
| +добавлено | d.dirH.multiplyScalar(1 / Math.max(D, 1e-3)); |
| +добавлено | const wd = Math.min(Math.max(D - 2.5, 3), D * 0.85); |
| +добавлено | d.wallC.set(c.from.x, 0, c.from.z).addScaledVector(d.dirH, wd); |
| +добавлено | c.pos.set(c.from.x, 0.05, c.from.z).addScaledVector(d.dirH, 0.8); |
| +добавлено | d.last.copy(c.pos); |
| +добавлено | } else c.pos.addScaledVector(c.dir, 1.15 * p.size); // the grown crystal's tip |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | d.trail?.release(); d.trail = null; |
| +добавлено | dropShard(d.shard); d.shard = null; |
| +добавлено | if (c.form === 'Ice Wall') wall(c, p, d); else nova(c, p, d); |
| +добавлено | } else if (name === 'done') { |
| +добавлено | d.light?.release(); d.light = null; |
| +добавлено | d.trail?.release(); d.trail = null; |
| +добавлено | dropShard(d.shard); d.shard = null; |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | // own integration: the spike flies straight (same tests as cast.js); the wall's frost line |
| +добавлено | // runs along the floor to the wall centre |
| +добавлено | travel(c, dt) { |
| +добавлено | const d = st[c.index]; |
| +добавлено | if (c.form === 'Ice Wall') { |
| +добавлено | d.last.copy(c.pos); |
| +добавлено | const step = 30 * dt, rem = _d.subVectors(d.wallC, c.pos).setY(0).length(); |
| +добавлено | if (rem <= step) { c.pos.set(d.wallC.x, 0.05, d.wallC.z); return true; } |
| +добавлено | c.pos.addScaledVector(d.dirH, step); |
| +добавлено | return false; |
| +добавлено | } |
| +добавлено | c.pos.addScaledVector(c.vel, dt); |
| +добавлено | c.travelled += c.vel.length() * dt; |
| +добавлено | const past = _d.subVectors(c.to, c.pos).dot(c.dir) <= 0; |
| +добавлено | if ((c.travelled > 1 && arenaSDF(c.pos) < S.radius) || past || c.t > c.durations.travel) { |
| +добавлено | if (past && c.t <= c.durations.travel) c.pos.copy(c.to); |
| +добавлено | return true; |
| +добавлено | } |
| +добавлено | return false; |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = st[c.index], L = d.light, m = d.shard; |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge, grow = smooth(0.04, 0.92, k); |
| +добавлено | d.spin += dt * 0.8; |
| +добавлено | _a.copy(c.from).addScaledVector(c.dir, 1.15 * p.size); // tip; the crystal grows back to front |
| +добавлено | if (m) { poseShard(m, _a, c.dir, d.spin, p.size); m.material.uniforms.uShard.value.x = grow; m.material.uniforms.uShard.value.w = 1.6 * k; } |
| +добавлено | _b.copy(c.from).addScaledVector(c.dir, 0.6 * p.size); // the crystal's middle |
| +добавлено | mist.pos.copy(_b); mist.vel.set(0, -0.5, 0); mist.size = 0.7 * p.size; mist.rate(150 * p.mist, dt); |
| +добавлено | mote.pos.copy(_b); mote.vel.set(0, 0, 0); mote.rate(1300 * p.motes, dt); |
| +добавлено | if (L) { L.pos.copy(_b); L.intensity = 9 * k * (0.85 + 0.15 * Math.sin(c.age * 23)); } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (c.phase === 'travel') { |
| +добавлено | if (c.form === 'Ice Wall') { |
| +добавлено | // the frost line: a jagged line splat per frame behind the runner, mist and glints off it |
| +добавлено | splat(d.last.x, d.last.z, 0.3, 0.5 * p.frost, 'line', c.seed % 100, c.pos.x, c.pos.z, 0.2); |
| +добавлено | lineMist.pos.copy(d.last).setY(0.2); lineMist.p.copy(c.pos).setY(0.2); lineMist.vel.set(0, 0.3, 0); lineMist.size = 0.8; lineMist.rate(260 * p.mist, dt); |
| +добавлено | glint.pos.copy(c.pos); glint.vel.set(0, 2.5, 0); glint.rate(500 * p.glints, dt); |
| +добавлено | if (L) { L.pos.copy(c.pos).setY(0.5); L.intensity = 10; } |
| +добавлено | } else { |
| +добавлено | d.spin += dt * 9; |
| +добавлено | _d.copy(c.vel).normalize(); |
| +добавлено | if (m) { poseShard(m, c.pos, _d, d.spin, p.size); m.material.uniforms.uShard.value.set(1, Math.max(0, m.material.uniforms.uShard.value.y - dt * 6), m.material.uniforms.uShard.value.z, 0.6); } |
| +добавлено | _a.copy(c.pos).addScaledVector(_d, -0.75 * p.size); // the frosted tail |
| +добавлено | d.trail?.push(_a); |
| +добавлено | trailMist.pos.copy(_a); trailMist.vel.copy(c.vel).multiplyScalar(0.06); trailMist.size = 0.55 * p.size; trailMist.rate(130 * p.mist, dt); |
| +добавлено | chip.pos.copy(_a); chip.vel.copy(c.vel).multiplyScalar(0.5); chip.rate(30 * p.shards, dt); |
| +добавлено | glint.pos.copy(_a); glint.vel.copy(c.vel).multiplyScalar(0.25); glint.rate(160 * p.glints, dt); |
| +добавлено | if (L) { L.pos.copy(c.pos); L.intensity = 8; } |
| +добавлено | } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | // impact + linger |
| +добавлено | d.boom += dt; |
| +добавлено | const g = d.ground; |
| +добавлено | if (L) { |
| +добавлено | L.intensity *= Math.exp(-dt / 0.25); |
| +добавлено | if (c.phase === 'linger') L.intensity = Math.max(L.intensity, 1.5 * Math.max(0, 1 - c.t / 2)); |
| +добавлено | } |
| +добавлено | if (c.form === 'Ice Wall') { |
| +добавлено | // cold air pours off the wall for a while |
| +добавлено | if (d.boom < 3) { lineMist.pos.copy(d.wallA).setY(1.2); lineMist.p.copy(d.wallB).setY(1.2); lineMist.vel.set(0, -0.4, 0); lineMist.size = 1; lineMist.rate(70 * p.mist * (1 - d.boom / 3), dt); } |
| +добавлено | } else { |
| +добавлено | // the hoarfrost ring lands where the nova's shell passes |
| +добавлено | // (lobed and broken: hoarfrost grows unevenly, further where the stone is colder) |
| +добавлено | if (!d.ring && d.boom > 0.12) { d.ring = true; splat(g.x, g.z, 3.4 * p.nova, 0.45 * p.frost, 'ring', (c.seed + 7) % 100, g.x, g.z, 0.32 * p.nova, 0.22); } |
| +добавлено | if (d.boom < 2.5) { mist.pos.copy(g).setY(0.35); mist.vel.set(0, 0, 0); mist.size = 1.2; mist.rate(45 * p.mist * (1 - d.boom / 2.5), dt); } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | function nova(c, p, d) { |
| +добавлено | const g = d.ground.set(c.pos.x, 0, c.pos.z), s = p.nova, rnd = fx.rand; |
| +добавлено | // 8 spikes in a ring, leaning out, erupting almost together |
| +добавлено | const n = Math.round(p.spikes), a0 = rnd() * Math.PI * 2; |
| +добавлено | for (let i = 0; i < n; i++) { |
| +добавлено | const ang = a0 + (i / Math.max(n, 1)) * Math.PI * 2 + (rnd() - 0.5) * 0.3, r = (1.45 + rnd() * 0.4) * s; |
| +добавлено | _a.set(g.x + Math.cos(ang) * r, 0, g.z + Math.sin(ang) * r); |
| +добавлено | _b.set(Math.cos(ang) * (0.35 + rnd() * 0.25), 1, Math.sin(ang) * (0.35 + rnd() * 0.25)); |
| +добавлено | spike(_a, _b, (1.4 + rnd() * 0.9) * s, (0.34 + rnd() * 0.14) * s, 0.02 + rnd() * 0.07, 0.16, 4.4 + rnd() * 0.8, 0.9, c.seed * 31 + i * 7919); |
| +добавлено | } |
| +добавлено | shard.pos.copy(c.pos); shard.vel.set(0, 2, 0); shard.size = p.size; shard.burst(48 * p.shards); |
| +добавлено | glint.pos.copy(c.pos); glint.vel.set(0, 2.5, 0); glint.burst(280 * p.glints); |
| +добавлено | novaMist.pos.copy(g).setY(0.3); novaMist.radius = 0.7 * s; novaMist.size = 1.2 * s; novaMist.burst(42 * p.mist); |
| +добавлено | mist.pos.copy(c.pos); mist.vel.set(0, -1, 0); mist.size = s; mist.burst(18 * p.mist); |
| +добавлено | // frost potential 1.8: the front crawls out over ~1 s as fern fingers, puddles freeze to a sheet |
| +добавлено | splat(g.x, g.z, 1.2 * s, 1.8 * p.frost, 'radial', c.seed % 100); |
| +добавлено | shock(_a.copy(g).setY(0.03), 0.4, 4.4 * s, 0.55, 0.5, 0.05, 1.2, true); |
| +добавлено | shock(c.pos, 0.2, 2.4 * s, 0.3, 0.4, 0.035, 0, false); |
| +добавлено | fx.impacts.add('frost', g, 3.6 * s, 1, 7, glowCol); |
| +добавлено | if (d.light) { d.light.intensity = 28; d.light.range = 11 * s; d.light.pos.copy(c.pos).setY(Math.max(c.pos.y, 0.8)); } |
| +добавлено | fx.flash(0.16); |
| +добавлено | fx.rig.shake(0.35, 16); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(1.5, 0.06); fx.rig.hitstop(0.04); } |
| +добавлено | const R = 2.6 * s; |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i], dist = Math.hypot(dm.pos.x - g.x, dm.pos.z - g.z); |
| +добавлено | if (i === c.target || dist < R) { |
| +добавлено | dm.status.frozen = Math.max(dm.status.frozen, i === c.target ? 1 : 1 - 0.5 * dist / R); |
| +добавлено | dm.hit(_d.subVectors(dm.pos, g).setY(0).normalize(), 0.5 * Math.max(0, 1 - dist / R)); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function wall(c, p, d) { |
| +добавлено | const C = d.wallC, dirH = d.dirH, rnd = fx.rand; |
| +добавлено | const n = Math.round(p.wallSpikes), half = p.wallWidth / 2; |
| +добавлено | const D = Math.hypot(C.x - c.from.x, C.z - c.from.z); |
| +добавлено | _perp.set(-dirH.z, 0, dirH.x); |
| +добавлено | // a curved, staggered row: alternate spikes stand back and lower |
| +добавлено | wallPoint(C, _perp, dirH, -half, p.wallCurve, D, d.wallA); wallPoint(C, _perp, dirH, half, p.wallCurve, D, d.wallB); |
| +добавлено | for (let i = 0; i < n; i++) { |
| +добавлено | const u = n > 1 ? i / (n - 1) : 0.5, s = (u - 0.5) * 2 * half, e = Math.abs(u - 0.5) * 2, back = i % 2 === 1; |
| +добавлено | wallPoint(C, _perp, dirH, s, p.wallCurve, D, _a).addScaledVector(dirH, back ? -0.45 : 0.1); |
| +добавлено | const h = p.wallHeight * (1 - 0.35 * e * e) * (back ? 0.78 : 1) * (0.9 + 0.2 * rnd()); |
| +добавлено | // lean toward the target and outward along the wall |
| +добавлено | _b.copy(dirH).multiplyScalar(0.18 + 0.1 * rnd()).addScaledVector(_perp, (u - 0.5) * 0.5 + (rnd() - 0.5) * 0.15).setY(1); |
| +добавлено | spike(_a, _b, h, (0.42 + 0.12 * rnd()) * h / 2.8 + 0.12, 0.04 + e * 0.22 + rnd() * 0.03, 0.22, 5 + rnd() * 0.4, 1, c.seed * 17 + i * 104729); |
| +добавлено | if (i < 14) splat(_a.x, _a.z, 0.8, 0.85 * p.frost, 'radial', (c.seed + i * 13) % 100); |
| +добавлено | } |
| +добавлено | lineMist.pos.copy(d.wallA).setY(0.4); lineMist.p.copy(d.wallB).setY(0.4); lineMist.vel.copy(dirH).multiplyScalar(-0.8); lineMist.size = 1.3; lineMist.burst(180 * p.mist); |
| +добавлено | lineGlint.pos.copy(d.wallA).setY(0.6); lineGlint.p.copy(d.wallB).setY(0.6); lineGlint.vel.set(0, 3, 0); lineGlint.burst(500 * p.glints); |
| +добавлено | shard.pos.copy(C).setY(0.3); shard.vel.set(0, 3, 0); shard.size = 0.6 * p.size; shard.burst(20 * p.shards); |
| +добавлено | shock(_a.copy(C).setY(0.03), 0.3, 3.2, 0.45, 0.45, 0.035, 0.8, true); |
| +добавлено | fx.impacts.add('frost', C, half + 1, 1, 7, glowCol); |
| +добавлено | if (d.light) { d.light.intensity = 14; d.light.range = 9; d.light.pos.copy(C).setY(1.8); } |
| +добавлено | fx.rig.shake(0.4, 9); |
| +добавлено | if (!fx.reduced) fx.rig.punch(1, 0.08); |
| +добавлено | // anything standing in the wall's footprint is caught in it |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | _d.subVectors(dm.pos, C).setY(0); |
| +добавлено | const along = _d.dot(_perp), across = _d.dot(dirH); |
| +добавлено | if (Math.abs(along) < half + 0.5 && Math.abs(across + p.wallCurve * along * along / (2 * Math.max(D, 1))) < 1.2) dm.status.frozen = Math.max(dm.status.frozen, 0.7); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js'; |
| +добавлено | import { U, COMMON_GLSL, NOISE_GLSL, WORLD_GLSL, LIGHTS_GLSL } from '../../vfx/common.js'; |
| +добавлено | import { REFRACT_GLSL } from '../../vfx/Refraction.js'; |
| +добавлено | |
| +добавлено | // Absolute Zero (ice hero). Cold motes pour into the caster's orb while a bell of freezing fog |
| +добавлено | // condenses high over the target, hangs, then drops onto the floor (reference: ice fog rolling in, |
| +добавлено | // diamond dust in low sun, the cold mist that spills off liquid nitrogen). The bell is a short |
| +добавлено | // refract-pass march through a noise density, never a surface: thickest and whitest at the base, |
| +добавлено | // near clear at the crown, its outline eroded and drifting so it breathes and tears, and once down |
| +добавлено | // a skirt of it rolls out along the floor. Inside, the world drains to a cold monochrome by how much |
| +добавлено | // frozen air the view ray crosses, time thickens (a damping force: every spell's particles hang), |
| +добавлено | // diamond dust twinkles, frost crawls in from the rim and out from the centre as fern fingers and |
| +добавлено | // the dummies ice over. Then the fog churns, flips briefly to a photo-negative and bursts: a blast |
| +добавлено | // of fog and ice shards, glints, a nova shock, a frost front thrown outward, hitstop. The field |
| +добавлено | // thaws the frost into puddles afterwards. (The lens param no longer does anything: fog doesn't |
| +добавлено | // refract.) |
| +добавлено | |
| +добавлено | const DOME_VERT = /* glsl */` |
| +добавлено | out vec3 vWorld; |
| +добавлено | void main() { |
| +добавлено | vec4 w = modelMatrix * vec4(position, 1.0); |
| +добавлено | vWorld = w.xyz; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * w; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const DOME_FRAG = COMMON_GLSL + NOISE_GLSL + WORLD_GLSL + LIGHTS_GLSL + REFRACT_GLSL + /* glsl */` |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | uniform vec4 uDome; // centre xz on the floor, y = base height; w = radius |
| +добавлено | uniform vec4 uDomeA; // grade 0..1, roll-out along the floor 0..1, agitation 0..1 (before it bursts), fog amount 0..1 |
| +добавлено | uniform vec4 uDomeB; // desaturate, negative 0..1, -, seed |
| +добавлено | uniform vec4 uDomeC; // haze, glints, -, march steps |
| +добавлено | uniform vec3 uTint; // cold grade (linear) |
| +добавлено | uniform vec3 uGlow; // the bell's cold light (linear HDR) |
| +добавлено | in vec3 vWorld; |
| +добавлено | out vec4 fragColor; |
| +добавлено | |
| +добавлено | const vec3 LUM = vec3(0.2126, 0.7152, 0.0722); |
| +добавлено | const vec3 BOX = vec3(1.75, 1.2, 1.75); // the proxy's extent in bell radii: nothing below reaches past it |
| +добавлено | |
| +добавлено | // Freezing fog in bell space (q = (p - C) / R): thickest and whitest at the base, thinning to almost |
| +добавлено | // clear at the crown; the outline is a drifting fbm radius eroded by worley octaves, so it breathes |
| +добавлено | // and tears instead of ending on a sphere; once down, a skirt rolls out along the floor. |
| +добавлено | float fogD(vec3 q, vec3 drift) { |
| +добавлено | vec4 n = textureLod(uNoise3D, q * 0.55 + drift, 0.0); // explicit LOD: sampled in a varying-length loop |
| +добавлено | float wf = n.g * 0.625 + n.b * 0.25 + n.a * 0.125; |
| +добавлено | float h = max(q.y, 0.0), r = length(q); |
| +добавлено | float edge = 0.84 + (n.r - 0.5) * 0.4; |
| +добавлено | float bell = smoothstep(edge + 0.14, edge - 0.3, r) * mix(1.0, 0.07, smoothstep(0.0, 0.9, h)); |
| +добавлено | float skirt = exp(-h * 7.0) * smoothstep(1.0 + 0.6 * uDomeA.y, 0.8, length(q.xz) + (n.r - 0.5) * 0.6) * uDomeA.y; |
| +добавлено | float d = max(bell, skirt); |
| +добавлено | return clamp((d - (1.0 - wf) * 0.5 * (1.0 - 0.6 * d)) * 1.6, 0.0, 1.0); |
| +добавлено | } |
| +добавлено | |
| +добавлено | void main() { |
| +добавлено | vec3 C = uDome.xyz; |
| +добавлено | float R = uDome.w; |
| +добавлено | vec3 ro = cameraPosition, rd = normalize(vWorld - ro); |
| +добавлено | // ray / the proxy's half ellipsoid (unit sphere in BOX-scaled bell space), above the base plane |
| +добавлено | vec3 o = (ro - C) / (BOX * R), dv = rd / (BOX * R); |
| +добавлено | float a = dot(dv, dv), b = dot(o, dv), h = b * b - a * (dot(o, o) - 1.0); |
| +добавлено | if (h <= 0.0) discard; |
| +добавлено | h = sqrt(h); |
| +добавлено | float t0 = (-b - h) / a, t1 = (-b + h) / a; |
| +добавлено | float tp = (C.y - ro.y) / (abs(rd.y) > 1e-6 ? rd.y : 1e-6); |
| +добавлено | if (rd.y > 0.0) t0 = max(t0, tp); else t1 = min(t1, tp); |
| +добавлено | float cosV = dot(rd, -vec3(viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2])); |
| +добавлено | float tScene = linearDepth(texelFetch(uSceneDepth, ivec2(gl_FragCoord.xy), 0).r) / cosV; |
| +добавлено | // tighten to where fog can be (fewer, denser samples: less dither): the hull of the bell's |
| +добавлено | // sphere (r < 1.2) and the skirt's slab (0 < h < 0.5) |
| +добавлено | vec3 oq = (ro - C) / R; |
| +добавлено | float bq = dot(oq, rd), hq = bq * bq - dot(oq, oq) + 1.44, sA = 1e9, sB = -1e9; |
| +добавлено | if (hq > 0.0) { hq = sqrt(hq); sA = (-bq - hq) * R; sB = (-bq + hq) * R; } |
| +добавлено | float ty = (C.y + 0.5 * R - ro.y) / (abs(rd.y) > 1e-6 ? rd.y : 1e-6); |
| +добавлено | float tIn = max(max(t0, 0.0), min(sA, min(tp, ty))), tOut = min(min(t1, tScene), max(sB, max(tp, ty))); |
| +добавлено | if (tOut <= tIn) discard; |
| +добавлено | |
| +добавлено | // march (white-noise jitter: grain, where IGN's lattice showed as a hatch and moiréd when scaled): |
| +добавлено | // optical depth for the grade, lit fog for the colour |
| +добавлено | int N = int(uDomeC.w); |
| +добавлено | float dt = (tOut - tIn) / float(N); |
| +добавлено | float t = tIn + dt * hash33(vec3(gl_FragCoord.xy, 17.0)).x; |
| +добавлено | vec3 drift = vec3(uDomeB.w, -uTime * 0.07, uTime * 0.03) + vec3(0.0, -uTime * 0.25, 0.0) * uDomeA.z; |
| +добавлено | float sigma = 0.75 * uDomeC.x * uDomeA.w, T = 1.0, tau = 0.0, scat = 0.0, hS = 0.0, tM = 0.0; |
| +добавлено | for (int i = 0; i < 16; i++) { |
| +добавлено | if (i >= N || T < 0.02) break; |
| +добавлено | vec3 q = (ro + rd * t - C) / R; |
| +добавлено | float s = fogD(q, drift) * sigma * dt; |
| +добавлено | float A = T * (1.0 - exp(-s)); |
| +добавлено | scat += A; hS += A * clamp(q.y, 0.0, 1.0); tM += A * t; |
| +добавлено | tau += s; T -= A; |
| +добавлено | t += dt; |
| +добавлено | } |
| +добавлено | float wS = max(scat, 1e-4); |
| +добавлено | vec3 Pm = ro + rd * (scat > 1e-4 ? tM / wS : 0.5 * (tIn + tOut)); |
| +добавлено | // light on the fog: sky, low sun (forward-scattered a little), the bell's own cold light; it |
| +добавлено | // reads cold even at sunset, a touch darker down in the thick base |
| +добавлено | gLightNoShadow = true; |
| +добавлено | float mu = dot(rd, uSunDir), hg = 0.08 * (1.0 - 0.09) / pow(1.09 - 0.6 * mu, 1.5); |
| +добавлено | vec3 irr = uAmbient * 1.15 + uSunColor * (0.18 + hg) + spellLightDiffuse(Pm, normalize(Pm - C + vec3(0.0, 1e-3, 0.0))) * 0.15; |
| +добавлено | irr = mix(irr, vec3(dot(irr, LUM)) * vec3(0.86, 0.95, 1.1), 0.7) * (0.8 + 0.3 * hS / wS); |
| +добавлено | vec3 fog = (vec3(0.84, 0.91, 0.97) * irr + uGlow * (0.03 + 0.35 * uDomeA.z * uDomeA.z)) * scat; |
| +добавлено | |
| +добавлено | // grade: cold monochrome (luminance kept) by how much frozen air the ray crossed, and a log-space |
| +добавлено | // negative about the scene's mean level for the burst |
| +добавлено | vec3 Spix = texelFetch(uSceneColor, ivec2(gl_FragCoord.xy), 0).rgb; |
| +добавлено | float lum = dot(Spix, LUM); |
| +добавлено | float Lm = max(dot(uAmbient + uSunColor * 0.25, LUM) * 0.35, 1e-4); |
| +добавлено | float lneg = min(Lm * Lm / max(lum, Lm * 0.15), Lm * 6.0); |
| +добавлено | vec3 mono = uTint * mix(lum, lneg, uDomeB.y) * 1.1; |
| +добавлено | // (the clear-air part by the ray's closest approach to the centre: no edge at the march hull) |
| +добавлено | float dmin = length(cross(oq, rd)); |
| +добавлено | float gd = uDomeA.x * (1.0 - exp(-tau * 1.5 - 0.5 * uDomeA.w * smoothstep(1.15, 0.3, dmin))) * max(uDomeB.x, uDomeB.y); |
| +добавлено | |
| +добавлено | // diamond dust: three nested layers of 4 cm cells, one facet in twelve catching the sun or the |
| +добавлено | // bell's light and twinkling (view- and time-hashed), faded before a cell shrinks under a pixel |
| +добавлено | vec3 dust = vec3(0.0); |
| +добавлено | float pixA = length(fwidth(rd)); // pixel footprint per metre of depth (no derivatives inside the loop) |
| +добавлено | for (int k = 0; k < 3; k++) { |
| +добавлено | float rr = (0.3 + 0.27 * float(k)) * R; |
| +добавлено | vec3 oc = ro - C - vec3(0.0, 0.15 * R, 0.0); |
| +добавлено | float bb = dot(oc, rd), hh = bb * bb - dot(oc, oc) + rr * rr; |
| +добавлено | if (hh <= 0.0) continue; |
| +добавлено | float tk = -bb - sqrt(hh); |
| +добавлено | if (tk < tIn || tk > tOut) tk = -bb + sqrt(hh); |
| +добавлено | if (tk < tIn || tk > tOut) continue; |
| +добавлено | vec3 P = ro + rd * tk, gq = (P - C) * 25.0 + float(k) * 7.31 + uDomeB.w; |
| +добавлено | vec3 gh = hash33(floor(gq)); |
| +добавлено | vec3 nf = normalize(gh * 2.0 - 1.0); |
| +добавлено | float spec = pow(max(dot(nf, normalize(uSunDir - rd)), 0.0), 300.0); |
| +добавлено | float tw = pow(0.5 + 0.5 * sin(uTime * (4.0 + 6.0 * gh.z) + gh.y * 60.0), 8.0); |
| +добавлено | float spot = smoothstep(0.3, 0.0, length(fract(gq) - 0.5 - (gh - 0.5) * 0.4)); |
| +добавлено | float fade = smoothstep(1.2, 0.4, 25.0 * tk * pixA); |
| +добавлено | dust += step(0.92, gh.x) * spot * fade * (uSunColor * spec * 40.0 + (irr * 2.0 + uGlow * 0.5) * tw); |
| +добавлено | } |
| +добавлено | dust *= uDomeC.y * uDomeA.w * smoothstep(0.0, 0.15, uDomeA.x + uDomeA.w * 0.1); |
| +добавлено | |
| +добавлено | // out = add + dst * keep (destination: everything the refract pass drew so far gets the grade) |
| +добавлено | float keep = T * (1.0 - gd); |
| +добавлено | vec3 add = fog + T * mono * gd + dust * mix(1.0, T, 0.5); |
| +добавлено | fragColor = vec4(add, keep); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | |
| +добавлено | // closed half-ellipsoid proxy (curved top + base disk) the size of the fog's reach (BOX in the shader); |
| +добавлено | // drawn back faces only, depth test off: every ray through the bell gets exactly one fragment |
| +добавлено | function domeGeometry() { |
| +добавлено | const top = new THREE.SphereGeometry(1, 40, 12, 0, Math.PI * 2, 0, Math.PI / 2); |
| +добавлено | const base = new THREE.CircleGeometry(1, 40).rotateX(Math.PI / 2); |
| +добавлено | const g = mergeGeometries([top, base]); |
| +добавлено | g.deleteAttribute('normal'); g.deleteAttribute('uv'); |
| +добавлено | g.scale(1.75, 1.2, 1.75); |
| +добавлено | return g; |
| +добавлено | } |
| +добавлено | |
| +добавлено | const MAX_DOMES = 2; |
| +добавлено | const RINGS = [0.1, 0.36, 0.62, 0.88, 1.14, 1.4]; // shatter rings: elevation (rad) on the dome |
| +добавлено | const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); }; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'absolute-zero', name: 'Absolute Zero', element: 'ice', order: 10, hue: '#9ff0ff', |
| +добавлено | shape: 'area', |
| +добавлено | params: { |
| +добавлено | radius: 3.6, drop: 2.6, hold: 2.6, desaturate: 0.85, negative: 1, haze: 1, lens: 1, |
| +добавлено | frost: 1, cracks: 1, shards: 1, dust: 1, mist: 1, glints: 1, tint: '#a9d8ff', glow: '#62d4ff', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | radius: [2, 6, 0.01], drop: [2, 10, 0.1], hold: [1, 5, 0.05], desaturate: [0, 1, 0.01], negative: [0, 1, 0.01], |
| +добавлено | haze: [0, 3, 0.01], lens: [0, 3, 0.01], frost: [0, 2, 0.01], cracks: [0, 3, 0.01], shards: [0, 3, 0.01], |
| +добавлено | dust: [0, 3, 0.01], mist: [0, 3, 0.01], glints: [0, 3, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | glacier: { tint: '#6fb4ff', glow: '#3f86ff', desaturate: 0.7, haze: 1.5 }, |
| +добавлено | whiteout: { tint: '#e6f4ff', glow: '#bfefff', desaturate: 1, haze: 2.4, mist: 2, dust: 2.2 }, |
| +добавлено | cryo: { tint: '#8ffff0', glow: '#2fffd8', cracks: 2.2, radius: 4.6, hold: 3.2 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 1.3, impact: 0.25, linger: 6 }, |
| +добавлено | kinds: { |
| +добавлено | // diamond dust: tiny crystals that hang in the frozen air and sparkle |
| +добавлено | dust: { style: 'glow', drag: 2.4, gravity: 0.1, curl: 0.35, curlScale: 0.35, forceResponse: 0.8, life: [1.6, 3], size: [0.013, 0.008], emissive: 7, color: ['#ffffff', '#a6e8ff'], alpha: [1, 0.5], fadeIn: 0.3, fadeOut: 0.35 }, |
| +добавлено | // the bell's fragments: pale lit ice, with a little of the shell's cyan still in them (debris: color[1] |
| +добавлено | // is the emission, fading as (1 - t)^2; alpha[1] the roughness) |
| +добавлено | pane: { style: 'debris', mesh: 'shard', drag: 0.3, gravity: 9.8, restitution: 0.3, friction: 0.55, life: [1.6, 3.2], size: [0.17, 0.08], sizePow: 1.5, spin: 11, color: ['#e2f6ff', '#2a7fa8'], alpha: [1, 0.05], fadeOut: 0.25 }, |
| +добавлено | // cold air: lit fog (the bell's cold light tints it), sinks and slides along the floor |
| +добавлено | chill: { style: 'media', drag: 1.6, buoyancy: -0.8, curl: 0.5, curlScale: 0.25, restitution: 0, friction: 0.02, forceResponse: 0.5, life: [1.4, 2.6], size: [0.35, 1.2], sizePow: 0.6, color: ['#e4f2ff', '#bcd6ec'], alpha: [0.13, 0], density: 1.1, erosion: 0.6, lightResponse: 1.5, fadeIn: 0.2, fadeOut: 0.45 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const mote = P.emitter('ice.mote', { shape: 'shell', radius: 1.3, spread: 0.1, radial: -2.2 }); |
| +добавлено | const rimMist = P.emitter('absolute-zero.chill', { shape: 'ring', radius: 3, spread: 0.3 }); |
| +добавлено | const blastMist = P.emitter('absolute-zero.chill', { shape: 'ring', radius: 3, spread: 0.4, radial: 2.8 }); |
| +добавлено | const floorMist = P.emitter('absolute-zero.chill', { shape: 'disk', radius: 3, spread: 0.15 }); |
| +добавлено | const dust = P.emitter('absolute-zero.dust', { shape: 'sphere', radius: 2, spread: 0.12 }); |
| +добавлено | const glint = P.emitter('ice.glint', { shape: 'ring', radius: 3, spread: 2.5 }); |
| +добавлено | const pane = P.emitter('absolute-zero.pane', { shape: 'ring', radius: 3, spread: 2.5 }); |
| +добавлено | const chip = P.emitter('ice.chip', { shape: 'ring', radius: 3, spread: 2 }); |
| +добавлено | |
| +добавлено | // --- look --- |
| +добавлено | const tint = new THREE.Vector3(), glowV = new THREE.Vector3(), lightCol = new THREE.Color(), glowCol = new THREE.Color(), _c = new THREE.Color(); |
| +добавлено | function applyLook() { |
| +добавлено | const p = S.params; |
| +добавлено | _c.set(p.tint); tint.set(_c.r, _c.g, _c.b); |
| +добавлено | glowCol.set(p.glow); glowV.set(glowCol.r, glowCol.g, glowCol.b).multiplyScalar(2.2); |
| +добавлено | lightCol.copy(glowCol).lerp(_c.setRGB(1, 1, 1), 0.4); |
| +добавлено | } |
| +добавлено | applyLook(); |
| +добавлено | |
| +добавлено | // --- domes: one program, per-mesh uniforms --- |
| +добавлено | const geo = domeGeometry(), domes = []; |
| +добавлено | for (let i = 0; i < MAX_DOMES; i++) { |
| +добавлено | const m = new THREE.Mesh(geo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: DOME_VERT, fragmentShader: DOME_FRAG, |
| +добавлено | side: THREE.BackSide, depthTest: false, depthWrite: false, |
| +добавлено | // out = src.rgb + dst * src.a; destination alpha kept (the reflection's 'drawn' mask) |
| +добавлено | blending: THREE.CustomBlending, blendEquation: THREE.AddEquation, blendSrc: THREE.OneFactor, blendDst: THREE.SrcAlphaFactor, |
| +добавлено | blendEquationAlpha: THREE.AddEquation, blendSrcAlpha: THREE.ZeroFactor, blendDstAlpha: THREE.OneFactor, |
| +добавлено | uniforms: { |
| +добавлено | ...U, uDome: { value: new THREE.Vector4() }, uDomeA: { value: new THREE.Vector4() }, uDomeB: { value: new THREE.Vector4() }, |
| +добавлено | uDomeC: { value: new THREE.Vector4() }, uTint: { value: tint }, uGlow: { value: glowV }, |
| +добавлено | }, |
| +добавлено | })); |
| +добавлено | m.renderOrder = 10; // after the spikes, ice shells and pool: it grades them too |
| +добавлено | m.frustumCulled = false; |
| +добавлено | m.visible = false; |
| +добавлено | fx.scenes.refract.add(m); |
| +добавлено | domes.push(m); |
| +добавлено | } |
| +добавлено | const takeDome = () => { for (const m of domes) if (!m.visible) { m.visible = true; return m; } return null; }; |
| +добавлено | |
| +добавлено | const st = Array.from({ length: 16 }, () => ({ |
| +добавлено | dome: null, light: null, force: null, g: new THREE.Vector3(), a: 0, y: 0, R: 1, seed: 0, |
| +добавлено | next: 0, shattered: false, sh: 0, fill: false, forceT: -1, |
| +добавлено | })); |
| +добавлено | const _a = new THREE.Vector3(), _d = new THREE.Vector3(); |
| +добавлено | const SPL = { x: 0, z: 0, radius: 1, frost: 0, pattern: 'radial', seed: 0, width: 0 }; |
| +добавлено | const SHK = { pos: new THREE.Vector3(), radius0: 0.3, radius1: 4, duration: 0.5, thickness: 0.5, distortion: 0.04, color: glowCol, intensity: 0, ground: false }; |
| +добавлено | function splat(x, z, radius, frost, pattern, seed, width = 0) { |
| +добавлено | SPL.x = x; SPL.z = z; SPL.radius = radius; SPL.frost = frost; SPL.pattern = pattern; SPL.seed = seed % 100; SPL.width = width; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | function shock(x, y, z, r0, r1, dur, thick, dist, intensity, ground) { |
| +добавлено | SHK.pos.set(x, y, z); SHK.radius0 = r0; SHK.radius1 = r1; SHK.duration = dur; SHK.thickness = thick; |
| +добавлено | SHK.distortion = dist; SHK.intensity = intensity; SHK.ground = ground; |
| +добавлено | fx.shocks.spawn(SHK); |
| +добавлено | } |
| +добавлено | function setDome(d, y, R, grade, rime, crack, shellA, neg) { |
| +добавлено | const m = d.dome; |
| +добавлено | if (!m) return; |
| +добавлено | const p = S.params, u = m.material.uniforms; |
| +добавлено | m.position.set(d.g.x, y, d.g.z); m.scale.setScalar(R); |
| +добавлено | u.uDome.value.set(d.g.x, y, d.g.z, R); |
| +добавлено | u.uDomeA.value.set(grade, rime, crack, shellA); |
| +добавлено | u.uDomeB.value.set(p.desaturate, neg, p.lens, d.seed); |
| +добавлено | u.uDomeC.value.set(p.haze, p.glints, 0, fx.Q.tier === 'low' ? 7 : 12); |
| +добавлено | } |
| +добавлено | // the pool's surface answers a slam or a blast whose ring reaches it |
| +добавлено | function poolKick(g, R, amp) { |
| +добавлено | const pool = fx.ARENA.pool; |
| +добавлено | const cx = Math.min(Math.max(g.x, pool.center.x - pool.half.x), pool.center.x + pool.half.x); |
| +добавлено | const cz = Math.min(Math.max(g.z, pool.center.y - pool.half.y), pool.center.y + pool.half.y); |
| +добавлено | if (Math.hypot(cx - g.x, cz - g.z) < R + 1) fx.pool?.ripple(cx, cz, amp, 0.6); |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam() { applyLook(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = st[c.index]; |
| +добавлено | if (name === 'charge') { |
| +добавлено | applyLook(); |
| +добавлено | d.g.set(c.to.x, 0, c.to.z); |
| +добавлено | d.R = p.radius; d.a = 0; d.y = p.drop; d.next = 0; d.shattered = false; d.sh = 0; d.fill = false; d.forceT = -1; |
| +добавлено | d.seed = (c.seed % 97) * 0.37; |
| +добавлено | c.durations.linger = p.hold + 3.4; |
| +добавлено | d.dome = takeDome(); |
| +добавлено | setDome(d, d.y, d.R * 0.75, 0, 0, 0, 0, 0); |
| +добавлено | d.light = fx.lights.acquire(); |
| +добавлено | if (d.light) { d.light.range = 9; d.light.intensity = 0; d.light.color.copy(lightCol); d.light.shadow = true; } |
| +добавлено | d.force = fx.forces.acquire(); |
| +добавлено | if (d.force) { d.force.type = 5; d.force.p[0] = 0; d.force.p[3] = 0; } |
| +добавлено | } else if (name === 'release') { |
| +добавлено | fx.rig.shake(0.08, 10); |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | slam(c, p, d); |
| +добавлено | } else if (name === 'done') { |
| +добавлено | if (d.dome) d.dome.visible = false; |
| +добавлено | d.dome = null; |
| +добавлено | d.light?.release(); d.light = null; |
| +добавлено | d.force?.release(); d.force = null; |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = st[c.index], g = d.g, R = d.R, L = d.light; |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const t = c.t, T = c.durations.charge; |
| +добавлено | // the orb drinks the warmth out of the air |
| +добавлено | if (t < 0.75) { mote.pos.copy(c.from); mote.vel.set(0, 0, 0); mote.rate(1100 * p.dust * (1 - t / 0.75), dt); } |
| +добавлено | // the bell condenses over the target (swelling out of a point), hangs, then falls |
| +добавлено | // (ease-in: it slams) |
| +добавлено | const form = smooth(0.05, 0.6, t), u = Math.min(1, Math.max(0, (t - 0.7) / (T - 0.7))); |
| +добавлено | const sw = 1 - Math.pow(1 - form, 3); |
| +добавлено | d.y = p.drop * (1 - Math.pow(u, 2.5)); |
| +добавлено | setDome(d, d.y, R * (0.25 + 0.75 * sw), 0.25 * form, 0, 0, form, 0); |
| +добавлено | // cold air pours off the rim; dust glitters inside |
| +добавлено | rimMist.pos.set(g.x, d.y + 0.1, g.z); rimMist.radius = R * 0.95 * sw; rimMist.vel.set(0, -1.5 - 4 * u, 0); rimMist.size = 0.8; |
| +добавлено | rimMist.rate(120 * p.mist * form, dt); |
| +добавлено | dust.pos.set(g.x, d.y + 0.45 * R, g.z); dust.radius = 0.6 * R * sw; dust.vel.set(0, -3 * u, 0); dust.rate(220 * p.dust * form, dt); |
| +добавлено | // the landing zone: a ring of rime draws itself on the floor under the hanging bell |
| +добавлено | if (t > 0.2 && t < 0.75 && t >= d.next) { d.next = t + 0.12; splat(g.x, g.z, R, 0.55 * smooth(0.2, 0.7, t) * p.frost, 'ring', c.seed + 3, 0.1 * R); } |
| +добавлено | if (L) { L.pos.set(g.x, d.y + 0.3 * R, g.z); L.range = 12; L.intensity = 22 * form; } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | // impact + linger: a = seconds since the slam |
| +добавлено | d.a += dt; |
| +добавлено | const a = d.a, hold = p.hold; |
| +добавлено | if (!d.shattered) { |
| +добавлено | const grade = smooth(0, 0.8 * hold, a) * 0.75 + 0.25, rime = smooth(0.1, hold, a); |
| +добавлено | const crack = p.cracks > 0 ? Math.pow(smooth(hold - 0.75, hold, a), 1.5) : 0; |
| +добавлено | const neg = p.negative * smooth(hold - 0.12, hold, a); |
| +добавлено | setDome(d, 0, R, grade, rime, crack, 1, neg); |
| +добавлено | // a second wash of frost once the fronts have met: nothing inside stays bare |
| +добавлено | if (!d.fill && a > 0.5 * hold) { d.fill = true; splat(g.x, g.z, R * 0.95, 0.7 * p.frost, 'noise', c.seed + 11); } |
| +добавлено | // diamond dust hangs in the bell, cold mist pools on the floor |
| +добавлено | dust.pos.set(g.x, 0.45 * R, g.z); dust.radius = 0.55 * R; dust.vel.set(0, 0, 0); dust.rate(320 * p.dust, dt); |
| +добавлено | floorMist.pos.set(g.x, 0.1, g.z); floorMist.radius = 0.85 * R; floorMist.vel.set(0, 0.05, 0); floorMist.rate(60 * p.mist, dt); |
| +добавлено | // time thickens: a damping column (every responsive particle of every spell slows) |
| +добавлено | const F = d.force; |
| +добавлено | if (F && d.forceT < 0) { |
| +добавлено | F.type = 5; F.pos.set(g.x, -0.3, g.z); F.dir.set(0, 1, 0); |
| +добавлено | F.p[0] = 0; F.p[1] = R; F.p[2] = R + 0.6; F.p[3] = 2.5 * smooth(0.2, 0.8, a); |
| +добавлено | } |
| +добавлено | // the dummies ice over from the feet up while the bell holds |
| +добавлено | for (const dm of fx.dummies) if (Math.hypot(dm.pos.x - g.x, dm.pos.z - g.z) < R) dm.status.frozen = Math.max(dm.status.frozen, Math.min(1, 0.2 + a / hold)); |
| +добавлено | // creaks before it goes |
| +добавлено | if (crack > 0.02 && c.t >= d.next) { d.next = c.t + 0.22; fx.rig.shake(0.06 + 0.12 * crack, 22); } |
| +добавлено | if (L) { L.pos.set(g.x, 0.6 * R, g.z); L.intensity = Math.max(L.intensity * Math.exp(-dt / 0.2), 26 + 30 * crack); L.range = 15; } |
| +добавлено | if (a >= hold) shatter(c, p, d); |
| +добавлено | } else { |
| +добавлено | d.sh += dt; |
| +добавлено | // the cracks flare as the shell lets go, then the colour floods back in (eased) |
| +добавлено | // the fog bursts outward and thins as the colour floods back in (eased) |
| +добавлено | const back = 1 - smooth(0.02, 0.3, d.sh); |
| +добавлено | setDome(d, 0, R * (1 + 0.7 * smooth(0, 0.5, d.sh)), back, 1, 1 - smooth(0, 0.15, d.sh), 1 - smooth(0.05, 0.7, d.sh), p.negative * Math.max(0, 1 - d.sh / 0.15)); |
| +добавлено | if (d.sh > 0.7 && d.dome) { d.dome.visible = false; d.dome = null; } |
| +добавлено | if (L) L.intensity *= Math.exp(-dt / 0.3); |
| +добавлено | if (d.sh < 2.5) { floorMist.pos.set(g.x, 0.1, g.z); floorMist.radius = R; floorMist.vel.set(0, 0.05, 0); floorMist.rate(30 * p.mist * (1 - d.sh / 2.5), dt); } |
| +добавлено | } |
| +добавлено | // the force: shock shells after the slam and the shatter, then let go |
| +добавлено | const F = d.force; |
| +добавлено | if (F && d.forceT >= 0) { |
| +добавлено | d.forceT += dt; |
| +добавлено | const k = d.forceT / 0.45; |
| +добавлено | if (k >= 1) { d.forceT = -1; if (d.shattered) { F.release(); d.force = null; } } |
| +добавлено | else { F.type = 4; F.pos.set(g.x, 0.6, g.z); F.p[0] = R * (d.shattered ? 0.9 + 1.6 * Math.pow(k, 0.4) : 1 + 0.8 * k); F.p[1] = 1.4; F.p[2] = (d.shattered ? 55 : 30) * (1 - k); F.p[3] = 0; } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | function slam(c, p, d) { |
| +добавлено | const g = d.g, R = d.R; |
| +добавлено | d.a = 0; d.next = 0; |
| +добавлено | setDome(d, 0, R, 0.25, 0, 0, 1, 0); |
| +добавлено | // the rim bites the floor: frost potential in a ring (fingers crawl in and out), and from |
| +добавлено | // the centre, reaching just past the rim |
| +добавлено | splat(g.x, g.z, R, 1.35 * p.frost, 'ring', c.seed + 5, 0.3); |
| +добавлено | splat(g.x, g.z, 1, (0.25 + 0.4 * (R - 0.6)) * p.frost, 'radial', c.seed + 9); |
| +добавлено | fx.impacts.add('frost', _a.set(g.x, 0.8, g.z), R * 1.15, 1, 8, glowCol); |
| +добавлено | blastMist.pos.set(g.x, 0.25, g.z); blastMist.radius = R; blastMist.radial = 2.8; blastMist.size = 1.1; blastMist.vel.set(0, 0.2, 0); blastMist.burst(60 * p.mist); |
| +добавлено | glint.pos.set(g.x, 0.1, g.z); glint.radius = R; glint.radial = 1.5; glint.vel.set(0, 2.2, 0); glint.burst(320 * p.glints); |
| +добавлено | chip.pos.set(g.x, 0.08, g.z); chip.radius = R; chip.radial = 1.2; chip.vel.set(0, 2.5, 0); chip.size = 1; chip.burst(50 * p.shards); |
| +добавлено | shock(g.x, 0.03, g.z, R, R + 3.2, 0.55, 0.5, 0.05, 0.9, true); |
| +добавлено | poolKick(g, R, 0.04); |
| +добавлено | if (d.light) { d.light.intensity = 70; d.light.range = 15; } |
| +добавлено | if (d.force) d.forceT = 0; |
| +добавлено | fx.flash(0.08); |
| +добавлено | fx.rig.shake(0.35, 14); |
| +добавлено | if (!fx.reduced) fx.rig.punch(1.2, 0.07); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function shatter(c, p, d) { |
| +добавлено | const g = d.g, R = d.R; |
| +добавлено | d.shattered = true; d.sh = 0; |
| +добавлено | // the bell breaks into six rings of self-lit panes (the shell's own glow, cooling as they |
| +добавлено | // fall), flung out from its centre; chips and glints from every other ring |
| +добавлено | for (let i = 0; i < RINGS.length; i++) { |
| +добавлено | const ph = RINGS[i], cr = Math.cos(ph), sr = Math.sin(ph), k = 0.3 + 0.7 * cr; |
| +добавлено | pane.pos.set(g.x, R * sr, g.z); pane.radius = R * cr; pane.radial = 6.5 * cr + 1; pane.vel.set(0, 6 * sr + 1.5, 0); |
| +добавлено | pane.burst(70 * k * p.shards); |
| +добавлено | if (i & 1) continue; |
| +добавлено | chip.pos.copy(pane.pos); chip.radius = pane.radius; chip.radial = 8 * cr + 1; chip.vel.set(0, 7 * sr + 2, 0); chip.size = 1; |
| +добавлено | chip.burst(80 * k * p.shards); |
| +добавлено | glint.pos.copy(pane.pos); glint.radius = pane.radius; glint.radial = 9 * cr + 2; glint.vel.set(0, 8 * sr + 2, 0); |
| +добавлено | glint.burst(420 * k * p.glints); |
| +добавлено | } |
| +добавлено | blastMist.pos.set(g.x, 0.3, g.z); blastMist.radius = 0.8 * R; blastMist.radial = 5; blastMist.size = 1.3; blastMist.vel.set(0, 0.6, 0); blastMist.burst(70 * p.mist); |
| +добавлено | // frost blasted outward past the old rim |
| +добавлено | splat(g.x, g.z, R + 0.4, 1.25 * p.frost, 'ring', c.seed + 17, 0.35); |
| +добавлено | shock(g.x, 0.45 * R, g.z, 0.9 * R, 2.3 * R, 0.5, 0.7, 0.07, 1.4, false); |
| +добавлено | shock(g.x, 0.03, g.z, 0.8 * R, 2.6 * R, 0.7, 0.6, 0.05, 0.8, true); |
| +добавлено | fx.impacts.add('frost', _a.set(g.x, 0.8, g.z), R * 1.6, 1, 7, glowCol); |
| +добавлено | poolKick(g, R * 2, 0.05); |
| +добавлено | if (d.light) { d.light.intensity = 90; d.light.range = 18; d.light.pos.set(g.x, 0.5 * R, g.z); } |
| +добавлено | if (d.force) d.forceT = 0; |
| +добавлено | fx.flash(0.15); |
| +добавлено | fx.rig.shake(0.55, 12); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(2.5, 0.08); fx.rig.hitstop(0.06); } |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dist = Math.hypot(dm.pos.x - g.x, dm.pos.z - g.z); |
| +добавлено | if (dist < 2 * R) { |
| +добавлено | dm.status.frozen = Math.max(dm.status.frozen, dist < R ? 1 : 0.6); |
| +добавлено | dm.hit(_d.subVectors(dm.pos, g).setY(0), 0.9 * Math.max(0.2, 1 - dist / (2 * R))); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { COMMON_GLSL, NOISE_GLSL, FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL, FX_BLENDING } from '../../vfx/common.js'; |
| +добавлено | |
| +добавлено | // Blizzard (wind + ice). Reference: whiteout and ground-blizzard footage (Antarctic katabatic storms, |
| +добавлено | // prairie blizzards): snow driven sideways in dense streaks at every depth, motion-blurred near the |
| +добавлено | // lens and soft far off; drift snow snaking along the ground in streams; gusts that thicken and thin |
| +добавлено | // the air; visibility collapsing to a bright, cold grey; frost building on the floor and the dummies. |
| +добавлено | // Nothing glows: it is all lit by the low sun (forward-scattered into the camera) and the sky. |
| +добавлено | // streaks createStreakField (exported; the shelved Sandstorm's grains used it too): N instanced flakes and no |
| +добавлено | // sim. Hashed start points in a box are advected by the wind analytically and wrapped into |
| +добавлено | // the box, drawn velocity-stretched over an exposure (long faint streaks near the lens, |
| +добавлено | // short sharp ones far off) into the half-res WBOIT pass, lit like a medium. |
| +добавлено | // air the whiteout: an analytic medium in a back-faced cylinder round the storm (8 taps), a |
| +добавлено | // plume stretched downwind and banded by gusts advected with the wind, plus the drift |
| +добавлено | // layer: an exponential sheet on the floor torn into snaking streams. |
| +добавлено | // world a push gust (and a gentle vortex) drags every loose particle downwind; lit flakes and |
| +добавлено | // spindrift veils, a little hail; the floor frosts from the heart out with drift streaks |
| +добавлено | // in the lee, dummies ice over and are buffeted; the pool swirls; a flash-freeze as it dies. |
| +добавлено | |
| +добавлено | const NB = 2; // storms that can run at once (each owns an air cylinder and a streak field) |
| +добавлено | const TOP = 7.5; // the storm's ceiling (m) |
| +добавлено | const TAU = Math.PI * 2; |
| +добавлено | |
| +добавлено | // --- stateless streak field ---------------------------------------------------------------------- |
| +добавлено | const SF_U = /* glsl */` |
| +добавлено | uniform vec4 uSF0; // box centre xyz, clock s |
| +добавлено | uniform vec4 uSF1; // box size xyz, advection m (integral of the wind speed) |
| +добавлено | uniform vec4 uSF2; // wind direction xyz (y = fall), streak length m (speed x exposure) |
| +добавлено | uniform vec4 uSF3; // diameter m, opacity, flutter m, speed spread 0..1 |
| +добавлено | uniform vec4 uSF4; // sun gain, sky gain, near blur m, near fade m |
| +добавлено | uniform vec4 uSF5; // light desaturation 0..1 (an overcast storm greys the low sun), -, -, - |
| +добавлено | uniform vec3 uSFColor; |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | `; |
| +добавлено | const SF_VERT = (gate) => FX_VERT_HEAD + COMMON_GLSL + NOISE_GLSL + SF_U + gate + /* glsl */` |
| +добавлено | out vec2 vUv; |
| +добавлено | out vec3 vCol; |
| +добавлено | out float vA; |
| +добавлено | out float vViewZ; |
| +добавлено | out float vSoft; |
| +добавлено | vec3 sfFlutter(float t, vec3 ph) { |
| +добавлено | return uSF3.z * vec3(sin(t * 2.3 + ph.x * 6.283), 0.6 * sin(t * 3.7 + ph.y * 6.283), sin(t * 1.9 + ph.z * 6.283)); |
| +добавлено | } |
| +добавлено | void main() { |
| +добавлено | gl_Position = vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | vUv = vec2(0.0); vCol = vec3(0.0); vA = 0.0; vViewZ = 1.0; vSoft = 0.0; |
| +добавлено | float id = float(gl_InstanceID); |
| +добавлено | vec3 h = hash31(id * 0.7131 + 0.5), g = hash31(id * 1.731 + 19.19); |
| +добавлено | float k = 1.0 - uSF3.w + 2.0 * uSF3.w * g.x; // this flake's share of the wind |
| +добавлено | vec3 fl = sfFlutter(uSF0.w, g); |
| +добавлено | vec3 p0 = h * uSF1.xyz + uSF2.xyz * (uSF1.w * k) + fl; |
| +добавлено | vec3 lo = uSF0.xyz - 0.5 * uSF1.xyz; |
| +добавлено | vec3 p = lo + mod(p0 - lo, uSF1.xyz); // wrapped into the (moving) box |
| +добавлено | vec3 q = abs(p - uSF0.xyz) / (0.5 * uSF1.xyz); |
| +добавлено | float gate = (1.0 - smoothstep(0.7, 1.0, max(q.x, max(q.y, q.z)))) * streakGate(p); |
| +добавлено | if (gate < 0.004 || p.y < 0.01) return; |
| +добавлено | vec3 tail = p - uSF2.xyz * (uSF2.w * k); |
| +добавлено | vec3 hv = (viewMatrix * vec4(p, 1.0)).xyz, tv = (viewMatrix * vec4(tail, 1.0)).xyz; |
| +добавлено | float viewZ = -hv.z; |
| +добавлено | if (viewZ < uSF4.w * 0.3 || -tv.z < 0.05) return; |
| +добавлено | float pxW = 2.0 * viewZ / (projectionMatrix[1][1] * uFxResolution.y); // metres per FX pixel |
| +добавлено | float r = 0.5 * uSF3.x * (0.55 + 0.9 * g.y); |
| +добавлено | float w = max(r + uSF4.z * max(0.0, 1.6 - viewZ), 0.7 * pxW); // defocus near the lens |
| +добавлено | float aa = r / w; |
| +добавлено | vec2 axis = tv.xy - hv.xy; |
| +добавлено | float len = length(axis); |
| +добавлено | vec2 dir = len > 1e-5 ? axis / len : vec2(1.0, 0.0), perp = vec2(-dir.y, dir.x); |
| +добавлено | vec2 off = dir * (position.x < 0.0 ? -w : len + w) + perp * position.y * w; |
| +добавлено | gl_Position = projectionMatrix * vec4(hv + vec3(off, 0.0), 1.0); |
| +добавлено | vUv = vec2(position.x < 0.0 ? -1.0 : 1.0, position.y); |
| +добавлено | vSoft = len / (len + 2.0 * w); |
| +добавлено | vViewZ = viewZ; |
| +добавлено | // the streak spreads the flake's coverage along its length (sqrt: long near streaks stay visible) |
| +добавлено | vA = uSF3.y * gate * aa * sqrt(2.0 * w / (len + 2.0 * w)) * smoothstep(uSF4.w * 0.3, uSF4.w, viewZ); |
| +добавлено | vec3 v = normalize(cameraPosition - p); |
| +добавлено | vec3 L = uSunColor * (hgPhase(dot(uSunDir, -v), 0.6) * 4.0 * PI * uSF4.x * streakSun(p)) + uAmbient * uSF4.y; |
| +добавлено | vCol = uSFColor * mix(L, vec3(dot(L, vec3(0.2126, 0.7152, 0.0722))), uSF5.x); |
| +добавлено | }`; |
| +добавлено | const SF_FRAG = FX_FRAG_HEAD + COMMON_GLSL + FX_OUT_GLSL + /* glsl */` |
| +добавлено | in vec2 vUv; |
| +добавлено | in vec3 vCol; |
| +добавлено | in float vA; |
| +добавлено | in float vViewZ; |
| +добавлено | in float vSoft; |
| +добавлено | void main() { |
| +добавлено | if (vA <= 0.002) discard; |
| +добавлено | float x = vUv.x * 0.5 + 0.5, e = 0.5 * (1.0 - vSoft); |
| +добавлено | float dx = x < e ? (e - x) / max(e, 1e-3) : x > 1.0 - e ? (x - 1.0 + e) / max(e, 1e-3) : 0.0; |
| +добавлено | float a = vA * exp(-(dx * dx + vUv.y * vUv.y) * 3.0) * mix(1.0, 0.45, x); |
| +добавлено | fxOut(vCol, a, vec3(0.0), vec2(0.0), vViewZ); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // gateGLSL defines `float streakGate(vec3 p)` (0..1 presence) and `float streakSun(vec3 p)` (sun |
| +добавлено | // visibility) with whatever uniforms it declares (pass them in `uniforms`). Returns |
| +добавлено | // { mesh, u, set(count) }; fill u.uSF0..4 / uSFColor each frame. |
| +добавлено | export function createStreakField(fx, { max, gateGLSL, uniforms = {} }) { |
| +добавлено | const plane = new THREE.PlaneGeometry(2, 2); |
| +добавлено | const geo = new THREE.InstancedBufferGeometry(); |
| +добавлено | geo.index = plane.index; |
| +добавлено | geo.setAttribute('position', plane.getAttribute('position')); |
| +добавлено | geo.instanceCount = 0; |
| +добавлено | const u = { |
| +добавлено | uSF0: { value: new THREE.Vector4() }, uSF1: { value: new THREE.Vector4(1, 1, 1, 0) }, uSF2: { value: new THREE.Vector4(1, 0, 0, 0.1) }, |
| +добавлено | uSF3: { value: new THREE.Vector4(0.01, 1, 0, 0) }, uSF4: { value: new THREE.Vector4(0.5, 1, 0, 0.4) }, uSF5: { value: new THREE.Vector4() }, uSFColor: { value: new THREE.Color(1, 1, 1) }, |
| +добавлено | }; |
| +добавлено | const mesh = new THREE.Mesh(geo, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: SF_VERT(gateGLSL), fragmentShader: SF_FRAG, |
| +добавлено | uniforms: { ...fx.U, ...uniforms, ...u }, ...FX_BLENDING, side: THREE.DoubleSide, |
| +добавлено | })); |
| +добавлено | mesh.frustumCulled = false; |
| +добавлено | mesh.visible = false; |
| +добавлено | fx.scenes.fx.add(mesh); |
| +добавлено | return { |
| +добавлено | mesh, u, max, |
| +добавлено | // instance count at this tier (emission follows the quality tier and the auto ladder) |
| +добавлено | set(n) { |
| +добавлено | const c = Math.min(max, Math.round(n * fx.Q.emission * fx.Q.auto.emission)); |
| +добавлено | geo.instanceCount = c; mesh.visible = c > 0; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- the whiteout ---------------------------------------------------------------------------------- |
| +добавлено | const AIR_U = /* glsl */` |
| +добавлено | uniform vec4 uBz0; // centre x, z, radius m, clock s |
| +добавлено | uniform vec4 uBz1; // wind dir x, z, advection m, envelope 0..1 |
| +добавлено | uniform vec4 uBz2; // air density 1/m, drift density 1/m, scale height m, gust 0..1 |
| +добавлено | uniform vec4 uBz3; // proxy radius m, top m, plume stretch downwind, seed |
| +добавлено | // storm frame: x along the wind, y across it, from the centre |
| +добавлено | vec2 bzFrame(vec3 p) { vec2 q = p.xz - uBz0.xy; return vec2(dot(q, uBz1.xy), q.x * uBz1.y - q.y * uBz1.x); } |
| +добавлено | // a round heart with a plume streaming downwind, 0..1 |
| +добавлено | float bzShape(vec2 f, float r) { return 1.0 - smoothstep(0.4, 1.0, length(vec2(f.x > 0.0 ? f.x / uBz3.z : f.x, f.y)) / r); } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | // the proxy is a unit cylinder (y 0..1) scaled to (radius, top, radius): pass its centre and size on |
| +добавлено | const AIR_VERT = FX_VERT_HEAD + /* glsl */` |
| +добавлено | out vec3 vW; |
| +добавлено | flat out vec4 vCyl; // centre x, z, radius, top |
| +добавлено | void main() { |
| +добавлено | vec4 w = modelMatrix * vec4(position, 1.0); |
| +добавлено | vW = w.xyz; |
| +добавлено | vCyl = vec4(modelMatrix[3].x, modelMatrix[3].z, length(modelMatrix[0].xyz), length(modelMatrix[1].xyz)); |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * w; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const AIR_FRAG = FX_FRAG_HEAD + '#define texture(s, p) textureLod(s, p, 0.0)\n' + COMMON_GLSL + NOISE_GLSL + FX_OUT_GLSL + AIR_U + /* glsl */` |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | uniform vec3 uBzAir; // snow albedo |
| +добавлено | uniform vec2 uBzLight; // sun gain, sky gain |
| +добавлено | in vec3 vW; |
| +добавлено | flat in vec4 vCyl; |
| +добавлено | float bzAir(vec3 p) { |
| +добавлено | vec2 f = bzFrame(p); |
| +добавлено | float s = bzShape(f, 1.7 * uBz0.z); |
| +добавлено | if (s <= 0.0) return 0.0; |
| +добавлено | float y = max(p.y, 0.0); |
| +добавлено | // gust bands: dense and thin sheets of driven snow streaming downwind |
| +добавлено | float n = noise3(vec3((f.x - uBz1.z) * 0.035, y * 0.22 - uBz0.w * 0.02, f.y * 0.16) + uBz3.w); |
| +добавлено | float gust = mix(1.0, 0.1 + 3.6 * n * n, uBz2.w); |
| +добавлено | return uBz2.x * uBz1.w * s * gust * exp(-y / uBz2.z) * (1.0 - smoothstep(0.5 * uBz3.y, uBz3.y, y)); |
| +добавлено | } |
| +добавлено | // drift streams on the floor, 0..1: torn by noise racing downwind, snaking across it |
| +добавлено | float bzDrift(vec2 xz) { |
| +добавлено | vec2 f = bzFrame(vec3(xz.x, 0.0, xz.y)); |
| +добавлено | float s = bzShape(f, 2.0 * uBz0.z); |
| +добавлено | if (s <= 0.0) return 0.0; |
| +добавлено | // soft veils streaming along the wind (stretched ~4:1), snaking slowly, racing past |
| +добавлено | float b = f.y + 0.6 * sin(f.x * 0.3 + uBz0.w * 0.9 + uBz3.w * 5.0); |
| +добавлено | float n = noise3(vec3((f.x - 1.6 * uBz1.z) * 0.07, uBz0.w * 0.05, b * 0.28) + uBz3.w * 1.7); |
| +добавлено | return s * smoothstep(0.3, 0.75, n); |
| +добавлено | } |
| +добавлено | void main() { |
| +добавлено | vec3 o = cameraPosition, rd = normalize(vW - o); |
| +добавлено | float cosV = dot(rd, -vec3(viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2])); |
| +добавлено | float tS = sceneViewZ() / cosV, t0 = 0.0, t1 = tS; |
| +добавлено | vec2 oc = o.xz - vCyl.xy; |
| +добавлено | float A = dot(rd.xz, rd.xz), B = dot(oc, rd.xz), C = dot(oc, oc) - vCyl.z * vCyl.z; |
| +добавлено | if (A > 1e-6) { |
| +добавлено | float disc = B * B - A * C; |
| +добавлено | if (disc <= 0.0) discard; |
| +добавлено | float sq = sqrt(disc); |
| +добавлено | t0 = max((-B - sq) / A, 0.0); t1 = min(t1, (-B + sq) / A); |
| +добавлено | } else if (C > 0.0) discard; |
| +добавлено | if (rd.y > 1e-4) t1 = min(t1, (vCyl.w - o.y) / rd.y); |
| +добавлено | else if (rd.y < -1e-4) t0 = max(t0, (vCyl.w - o.y) / rd.y); |
| +добавлено | else if (o.y > vCyl.w) discard; |
| +добавлено | if (t1 <= t0) discard; |
| +добавлено | // an overcast storm: the low sun comes through greyed and diffused, the sky light is cold |
| +добавлено | vec3 sunC = mix(uSunColor, vec3(dot(uSunColor, vec3(0.2126, 0.7152, 0.0722))), 0.85); |
| +добавлено | vec3 sunL = sunC * (hgPhase(dot(uSunDir, rd), 0.4) * 4.0 * PI * uBzLight.x); |
| +добавлено | vec3 sky = vec3(dot(uAmbient, vec3(0.2126, 0.7152, 0.0722))) * vec3(0.88, 0.95, 1.1) * uBzLight.y; |
| +добавлено | vec3 col = vec3(0.0); |
| +добавлено | float T = 1.0, zs = 0.0, ws = 0.0, dt = (t1 - t0) / 8.0, sP = bzAir(o + rd * t0); |
| +добавлено | for (int i = 1; i <= 8; i++) { |
| +добавлено | float t = t0 + dt * float(i); |
| +добавлено | float s = bzAir(o + rd * t), sg = 0.5 * (s + sP); |
| +добавлено | sP = s; |
| +добавлено | if (sg < 1e-5) continue; |
| +добавлено | float a = T * (1.0 - exp(-sg * dt)); |
| +добавлено | // the sun dims through the storm's bulk; the sky light reaches everywhere, a little less deep in |
| +добавлено | col += a * (sunL * exp(-sg * 7.0) + sky * (0.55 + 0.45 * exp(-sg * 4.0))); |
| +добавлено | zs += a * (t - 0.5 * dt); ws += a; |
| +добавлено | T *= exp(-sg * dt); |
| +добавлено | } |
| +добавлено | // the drift sheet where the ray lands on the floor inside the storm |
| +добавлено | if (tS <= t1 + 0.05 && rd.y < -0.01 && o.y + rd.y * tS < 0.1) { |
| +добавлено | float L = min(0.25 / -rd.y, 2.5); |
| +добавлено | float d = 0.6 * bzDrift((o + rd * tS).xz) + 0.4 * bzDrift((o + rd * (tS - L)).xz); |
| +добавлено | float a = T * (1.0 - exp(-uBz2.y * uBz1.w * d * L)); |
| +добавлено | col += a * (sunL * 0.5 + sky * 1.35); |
| +добавлено | zs += a * tS; ws += a; |
| +добавлено | T *= 1.0 - a / max(T, 1e-4); |
| +добавлено | } |
| +добавлено | float alpha = 1.0 - T; |
| +добавлено | if (alpha < 0.003) discard; |
| +добавлено | fxOut(uBzAir * col / max(alpha, 1e-4), alpha, vec3(0.0), vec2(0.0), (ws > 0.0 ? zs / ws : t0) * cosV); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // the snow field's presence follows the air: thick in the heart, a plume downwind |
| +добавлено | const SNOW_GATE = AIR_U + /* glsl */` |
| +добавлено | float streakGate(vec3 p) { |
| +добавлено | float s = sqrt(bzShape(bzFrame(p), 2.6 * uBz0.z)); // blown snow reaches well past the heart |
| +добавлено | return uBz1.w * s * (0.35 + 0.65 * exp(-p.y / (1.6 * uBz2.z))) * (1.0 - smoothstep(0.6 * uBz3.y, uBz3.y, p.y)); |
| +добавлено | } |
| +добавлено | float streakSun(vec3 p) { return 1.0 - 0.45 * uBz1.w * bzShape(bzFrame(p), 1.6 * uBz0.z); } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); }; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'blizzard', name: 'Blizzard', element: 'ice', order: 20, hue: '#dff4ff', |
| +добавлено | shape: 'area', |
| +добавлено | params: { |
| +добавлено | radius: 4, duration: 6, strength: 1, wind: 10, angle: 25, snow: 1, visibility: 1, spindrift: 1, streaks: 1, hail: 1, |
| +добавлено | refraction: 1, frost: 1, freeze: 1, air: '#dcecff', glow: '#86d2ff', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | radius: [2, 6, 0.01], duration: [2, 12, 0.1], strength: [0.2, 2.5, 0.01], wind: [3, 20, 0.1], angle: [-90, 90, 1], |
| +добавлено | snow: [0, 3, 0.01], visibility: [0, 3, 0.01], spindrift: [0, 3, 0.01], streaks: [0, 3, 0.01], hail: [0, 3, 0.01], |
| +добавлено | refraction: [0, 3, 0.01], frost: [0, 2, 0.01], freeze: [0, 2, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | whiteout: { radius: 5, snow: 1.8, visibility: 1.9, spindrift: 2, streaks: 0.8, hail: 0.4, air: '#eef6ff' }, |
| +добавлено | 'polar vortex': { strength: 1.8, wind: 15, streaks: 1.6, visibility: 1.2, spindrift: 1.4, air: '#c8e2ff', glow: '#4f9dff' }, |
| +добавлено | sleet: { hail: 2.6, snow: 0.7, visibility: 0.7, spindrift: 0.6, strength: 1.3, air: '#d2e6f0', glow: '#9fd8e6' }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 1.0, impact: 0.35, linger: 7.6 }, |
| +добавлено | kinds: { |
| +добавлено | // driven snow near the heart: small lit flakes swept round by the gusts (white albedo, lit by the |
| +добавлено | // sun, the sky and the spell lights: peach on the sun side, blue in shadow) |
| +добавлено | flake: { style: 'media', drag: 1.2, gravity: 1.1, curl: 1.1, curlScale: 0.45, forceResponse: 3, restitution: 0, friction: 0.9, life: [1.6, 3], size: [0.035, 0.025], color: ['#ffffff', '#e8f4ff'], alpha: [1, 0.85], density: 4, erosion: 0, lightResponse: 1.2, fadeIn: 0.2, fadeOut: 0.3 }, |
| +добавлено | // spindrift: thin lit veils of blown snow hugging the floor |
| +добавлено | drift: { style: 'media', drag: 0.6, gravity: 0.15, curl: 0.9, curlScale: 0.3, forceResponse: 2.5, restitution: 0, friction: 0.05, life: [0.9, 1.7], size: [0.25, 0.8], sizePow: 0.6, color: ['#eef4fa', '#d6e2ee'], alpha: [0.16, 0], density: 1.6, erosion: 0.65, lightResponse: 1, fadeIn: 0.25, fadeOut: 0.45 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const E = { |
| +добавлено | gather: P.emitter('wind.streak', { shape: 'shell', radius: 1.7, spread: 0.4, radial: -3.5 }), |
| +добавлено | mote: P.emitter('ice.mote', { shape: 'shell', radius: 1.3, spread: 0.1, radial: -2.2 }), |
| +добавлено | snow: P.emitter('blizzard.flake', { shape: 'disk', radius: 3, spread: 0.9 }), |
| +добавлено | drift: P.emitter('blizzard.drift', { shape: 'line', radius: 0.3, spread: 0.5 }), |
| +добавлено | hail: P.emitter('ice.chip', { shape: 'disk', radius: 3, spread: 1.2 }), |
| +добавлено | glint: P.emitter('ice.glint', { shape: 'disk', radius: 3, spread: 1.5 }), |
| +добавлено | }; |
| +добавлено | |
| +добавлено | // per storm slot: the air cylinder and the streak field share one set of storm uniforms |
| +добавлено | const slots = []; |
| +добавлено | const cyl = new THREE.CylinderGeometry(1, 1, 1, 40, 1, false).translate(0, 0.5, 0); |
| +добавлено | for (let i = 0; i < NB; i++) { |
| +добавлено | const su = { |
| +добавлено | uBz0: { value: new THREE.Vector4() }, uBz1: { value: new THREE.Vector4(1, 0, 0, 0) }, |
| +добавлено | uBz2: { value: new THREE.Vector4(0.1, 1, 2, 1) }, uBz3: { value: new THREE.Vector4(10, 7, 2, 0) }, |
| +добавлено | }; |
| +добавлено | const air = new THREE.Mesh(cyl, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: AIR_VERT, fragmentShader: AIR_FRAG, ...FX_BLENDING, |
| +добавлено | side: THREE.BackSide, depthTest: false, depthWrite: false, |
| +добавлено | uniforms: { ...fx.U, ...su, uBzAir: { value: new THREE.Color(1, 1, 1) }, uBzLight: { value: new THREE.Vector2(0.22, 1.3) } }, |
| +добавлено | })); |
| +добавлено | air.frustumCulled = false; air.visible = false; |
| +добавлено | fx.scenes.fx.add(air); |
| +добавлено | const field = createStreakField(fx, { max: 20000, gateGLSL: SNOW_GATE, uniforms: su }); |
| +добавлено | slots.push({ busy: false, su, air, field }); |
| +добавлено | } |
| +добавлено | |
| +добавлено | const state = Array.from({ length: 16 }, () => ({ |
| +добавлено | slot: null, push: null, vortex: null, light: null, g: new THREE.Vector3(), W: new THREE.Vector3(1, 0, 0), |
| +добавлено | age: 0, env: 0, adv: 0, clock: 0, gust: 1, seedF: 0, nextSplat: 0, nextHit: 0, nextLee: 0, frozeAll: false, |
| +добавлено | })); |
| +добавлено | |
| +добавлено | const air = new THREE.Color(), glow = new THREE.Color(), tmp = new THREE.Color(), _white = new THREE.Color(1, 1, 1); |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _up = new THREE.Vector3(0, 1, 0); |
| +добавлено | // the snow takes the air colour (setKind keeps the arrays, so one per kind) |
| +добавлено | const look = { air: '' }, flakeCol = ['', ''], driftCol = ['', '']; |
| +добавлено | function applyLook(p) { |
| +добавлено | air.set(p.air); glow.set(p.glow); |
| +добавлено | if (look.air === p.air) return; |
| +добавлено | look.air = p.air; |
| +добавлено | flakeCol[0] = `#${tmp.set(p.air).lerp(_white, 0.6).getHexString()}`; flakeCol[1] = p.air; |
| +добавлено | P.setKind('blizzard.flake', { color: flakeCol }); |
| +добавлено | driftCol[0] = `#${tmp.set(p.air).lerp(_white, 0.3).getHexString()}`; driftCol[1] = `#${tmp.set(p.air).multiplyScalar(0.86).getHexString()}`; |
| +добавлено | P.setKind('blizzard.drift', { color: driftCol }); |
| +добавлено | } |
| +добавлено | const SPL = { x: 0, z: 0, x2: 0, z2: 0, radius: 1, frost: 0, pattern: 'radial', seed: 0, width: 0 }; |
| +добавлено | function splat(x, z, radius, frost, pattern, seed, width = 0, x2 = x, z2 = z) { |
| +добавлено | SPL.x = x; SPL.z = z; SPL.x2 = x2; SPL.z2 = z2; SPL.radius = radius; SPL.frost = frost; SPL.pattern = pattern; SPL.seed = seed % 100; SPL.width = width; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function releaseAll(st) { |
| +добавлено | if (st.slot) { st.slot.busy = false; st.slot.air.visible = false; st.slot.field.set(0); st.slot = null; } |
| +добавлено | st.push?.release(); st.push = null; |
| +добавлено | st.vortex?.release(); st.vortex = null; |
| +добавлено | st.light?.release(); st.light = null; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // storm uniforms for this frame: e = how much storm there is (0..1) |
| +добавлено | function frame(st, p, dt, e) { |
| +добавлено | const s = st.slot; |
| +добавлено | if (!s) return; |
| +добавлено | const g = st.g, W = st.W, R = p.radius, sp = p.strength; |
| +добавлено | // gusts: the wind surges and slackens (two slow beats and a sharper one) |
| +добавлено | st.gust = 0.78 + 0.22 * Math.sin(st.clock * 1.13 + st.seedF * 9) * Math.sin(st.clock * 0.41 + 1) + 0.18 * Math.max(0, Math.sin(st.clock * 2.7 + st.seedF * 4)) ** 6; |
| +добавлено | const v = p.wind * sp * st.gust * (0.25 + 0.75 * e); |
| +добавлено | st.adv += v * dt; st.clock += dt; |
| +добавлено | const u = s.su, rh = 3.3 * R + 1; // the proxy cylinder: round the heart and the plume downwind |
| +добавлено | u.uBz0.value.set(g.x, g.z, R, st.clock); |
| +добавлено | u.uBz1.value.set(W.x, W.z, st.adv, e); |
| +добавлено | u.uBz2.value.set(0.09 * p.visibility * (0.7 + 0.5 * st.gust), 1.4 * p.spindrift, 4.5, 1); |
| +добавлено | u.uBz3.value.set(0, TOP, 2.2, st.seedF * 13); |
| +добавлено | s.air.position.set(g.x + W.x * 1.2 * R, 0, g.z + W.z * 1.2 * R); |
| +добавлено | s.air.scale.set(rh, TOP, rh); |
| +добавлено | s.air.material.uniforms.uBzAir.value.copy(air); |
| +добавлено | s.air.visible = e > 0.01; |
| +добавлено | // the snow: a box reaching from the lens towards the storm (dense near streaks wherever the |
| +добавлено | // camera is; the gate keeps them in the storm); the flakes ride the wind with a little fall |
| +добавлено | const f = s.field, fu = f.u, cam = fx.camera.position; |
| +добавлено | const cx = g.x + W.x * 1.1 * R - cam.x, cz = g.z + W.z * 1.1 * R - cam.z, cd = Math.hypot(cx, cz), ck = cd > 5 ? 5 / cd : 1; |
| +добавлено | fu.uSF0.value.set(cam.x + cx * ck, 0.5 * TOP, cam.z + cz * ck, st.clock); |
| +добавлено | fu.uSF1.value.set(20, TOP, 20, st.adv); |
| +добавлено | fu.uSF2.value.set(W.x, -0.16, W.z, v * (1 / 40) * p.streaks + 0.01); |
| +добавлено | fu.uSF3.value.set(0.02, 1, 0.12, 0.3); |
| +добавлено | fu.uSF4.value.set(0.4, 2.6, 0.03 * p.refraction, 0.3); |
| +добавлено | fu.uSF5.value.set(0.85, 0, 0, 0); |
| +добавлено | fu.uSFColor.value.copy(air).lerp(_white, 0.75); |
| +добавлено | f.set(e > 0.01 ? 16000 * p.snow : 0); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- charge: the orb drinks in wind and cold; over the target the wind gets up --- |
| +добавлено | function charge(c, st, p, dt) { |
| +добавлено | const T = c.durations.charge, k = Math.min(1, c.t / T), f = c.from, g = st.g, R = p.radius; |
| +добавлено | E.gather.pos.copy(f); E.gather.rate(260 * p.streaks * (0.3 + 0.7 * k), dt); |
| +добавлено | if (k < 0.8) { E.mote.pos.copy(f); E.mote.vel.set(0, 0, 0); E.mote.rate(600 * (1 - k / 0.8), dt); } |
| +добавлено | const e = 0.3 * k * k; |
| +добавлено | st.env = e; |
| +добавлено | gusts(st, p, e, R); |
| +добавлено | // the first spindrift lifts off the floor upwind |
| +добавлено | if (driftLine(st, R, 1.1)) { E.drift.vel.set(st.W.x * 4, 0.3, st.W.z * 4); E.drift.size = 0.8; E.drift.rate(30 * p.spindrift * k, dt); } |
| +добавлено | if (st.light) { st.light.pos.set(g.x, 1.6, g.z); st.light.intensity = 1.5 * k; } |
| +добавлено | frame(st, p, dt, e); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // a line across the wind, upwind of the heart, for spindrift (endpoints into E.drift.pos / .p) |
| +добавлено | function driftLine(st, R, back) { |
| +добавлено | const g = st.g, W = st.W; |
| +добавлено | const cx = g.x - W.x * R * back, cz = g.z - W.z * R * back; |
| +добавлено | E.drift.pos.set(cx + W.z * R, 0.12, cz - W.x * R); |
| +добавлено | E.drift.p.set(cx - W.z * R, 0.12, cz + W.x * R); |
| +добавлено | return true; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the push gust and the vortex follow the storm |
| +добавлено | function gusts(st, p, e, R) { |
| +добавлено | const g = st.g, W = st.W, sp = p.strength; |
| +добавлено | if (st.push) { |
| +добавлено | st.push.pos.set(g.x - W.x * 2.2 * R, 0.8, g.z - W.z * 2.2 * R); st.push.dir.copy(W); |
| +добавлено | st.push.p[0] = p.wind * sp * st.gust; st.push.p[1] = 2.4 * R; st.push.p[2] = 6 * R; st.push.p[3] = 1.6 * e; |
| +добавлено | } |
| +добавлено | const V = st.vortex; |
| +добавлено | if (V) { V.pos.copy(g); V.dir.copy(_up); V.p[0] = 0.45 * R; V.p[1] = 10 * R * sp * e; V.p[2] = 0.4 * e; V.p[3] = 0.6 * e; V.w = 5; } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- the storm --- |
| +добавлено | function storm(c, st, p, dt) { |
| +добавлено | const g = st.g, W = st.W, R = p.radius, e = st.env, sp = p.strength; |
| +добавлено | const k = Math.min(1, st.age / Math.max(p.duration, 0.1)); // how far the freeze has got |
| +добавлено | gusts(st, p, e, R); |
| +добавлено | // lit flakes swirling in the heart, spindrift veils racing over the floor, a little hail |
| +добавлено | E.snow.pos.set(g.x - W.x * 0.5 * R, 0.6 + 3.2 * fx.rand(), g.z - W.z * 0.5 * R); E.snow.radius = 0.9 * R; |
| +добавлено | E.snow.vel.set(W.x * 5, -0.8, W.z * 5); E.snow.rate(700 * p.snow * e, dt); |
| +добавлено | if (driftLine(st, R, 0.9)) { |
| +добавлено | E.drift.vel.set(W.x * p.wind * 0.8, 0.25, W.z * p.wind * 0.8); E.drift.size = 0.7 + 0.4 * fx.rand(); |
| +добавлено | E.drift.rate(45 * p.spindrift * e * st.gust, dt); |
| +добавлено | } |
| +добавлено | E.hail.pos.set(g.x, 4.2, g.z); E.hail.radius = 0.9 * R; E.hail.vel.set(W.x * 3, -7, W.z * 3); E.hail.size = 0.6; |
| +добавлено | E.hail.rate(8 * p.hail * e, dt); |
| +добавлено | E.glint.pos.set(g.x, 3.0, g.z); E.glint.radius = 0.9 * R; E.glint.vel.set(W.x * 4, -2, W.z * 4); |
| +добавлено | E.glint.rate(24 * p.hail * e, dt); |
| +добавлено | |
| +добавлено | // the floor freezes from the heart outwards (the frost potential at the centre rises with the |
| +добавлено | // storm's age, so the field's front creeps out), rime lands inside, and snow streaks in the lee |
| +добавлено | if (p.frost > 0 && st.age >= st.nextSplat && e > 0.2) { |
| +добавлено | st.nextSplat = st.age + 0.3; |
| +добавлено | splat(g.x, g.z, 1, (0.3 + 0.4 * Math.max(0, R * (0.25 + 0.75 * k) - 0.6)) * p.frost, 'radial', c.seed + 3); |
| +добавлено | const a = fx.rand() * TAU, r = R * Math.sqrt(fx.rand()) * (0.3 + 0.7 * k); |
| +добавлено | const x = g.x + Math.cos(a) * r, z = g.z + Math.sin(a) * r, len = 1.2 + 2.5 * fx.rand(); |
| +добавлено | splat(x, z, 0.9, 1.1 * p.frost * e, 'line', c.seed + 7 + (st.nextSplat * 10 | 0), 0.12 + 0.12 * fx.rand(), x + W.x * len, z + W.z * len); |
| +добавлено | } |
| +добавлено | // the dummies inside ice over (deeper at the heart), are buffeted downwind, and snow drifts in their lee |
| +добавлено | st.nextHit -= dt; |
| +добавлено | const hit = st.nextHit <= 0; |
| +добавлено | if (hit) st.nextHit = 0.35; |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i], dx = dm.pos.x - g.x, dz = dm.pos.z - g.z, d = Math.hypot(dx, dz); |
| +добавлено | if (d > 1.6 * R) continue; |
| +добавлено | const near = 1 - 0.45 * Math.min(1, d / R); |
| +добавлено | if (p.freeze > 0) dm.status.frozen = Math.max(dm.status.frozen, Math.min(1, k * p.freeze * near * 1.2)); |
| +добавлено | if (hit && e > 0.1) dm.hit(_b.set(W.x + 0.3 * (fx.rand() - 0.5), 0, W.z + 0.3 * (fx.rand() - 0.5)), 0.2 * e * sp * st.gust * near); |
| +добавлено | if (p.frost > 0 && st.age >= st.nextLee && e > 0.3) splat(dm.pos.x + W.x * 0.4, dm.pos.z + W.z * 0.4, 0.5, 1.2 * p.frost, 'line', c.seed + 31 + i, 0.18, dm.pos.x + W.x * 2.4, dm.pos.z + W.z * 2.4); |
| +добавлено | } |
| +добавлено | if (st.age >= st.nextLee) st.nextLee = st.age + 0.9; |
| +добавлено | // the pool's ripple sim swirls under the storm |
| +добавлено | const pool = fx.ARENA.pool; |
| +добавлено | const qx = Math.max(Math.abs(g.x - pool.center.x) - pool.half.x, 0), qz = Math.max(Math.abs(g.z - pool.center.y) - pool.half.y, 0); |
| +добавлено | const pd = Math.hypot(qx, qz), reach = 1.6 * R; |
| +добавлено | if (pd < reach && fx.pool) { |
| +добавлено | const cx = Math.min(Math.max(g.x, pool.center.x - pool.half.x), pool.center.x + pool.half.x); |
| +добавлено | const cz = Math.min(Math.max(g.z, pool.center.y - pool.half.y), pool.center.y + pool.half.y); |
| +добавлено | fx.pool.swirl(cx, cz, 2.0 * e * sp * (1 - pd / reach)); |
| +добавлено | } |
| +добавлено | // a faint cold fill inside the storm: sky light bounced round in the snow |
| +добавлено | if (st.light) { st.light.pos.set(g.x, 1.4, g.z); st.light.range = 1.8 * R + 2; st.light.intensity = 2.5 * e; } |
| +добавлено | frame(st, p, dt, e); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- as the wind dies, everything still inside snaps to ice --- |
| +добавлено | function flashFreeze(c, st, p) { |
| +добавлено | const g = st.g, R = p.radius; |
| +добавлено | st.frozeAll = true; |
| +добавлено | if (p.frost > 0) { |
| +добавлено | splat(g.x, g.z, R * 0.95, 1.2 * p.frost, 'noise', c.seed + 19); |
| +добавлено | splat(g.x + st.W.x * R * 0.5, g.z + st.W.z * R * 0.5, R * 1.1, 1.0 * p.frost, 'noise', c.seed + 23); |
| +добавлено | } |
| +добавлено | fx.impacts.add('frost', _a.set(g.x, 0.8, g.z), R * 1.3, 1, 7, glow); |
| +добавлено | E.glint.pos.set(g.x, 0.15, g.z); E.glint.radius = R; E.glint.vel.set(0, 1.6, 0); E.glint.burst(220 * p.hail); |
| +добавлено | for (const dm of fx.dummies) if (Math.hypot(dm.pos.x - g.x, dm.pos.z - g.z) < 1.2 * R && p.freeze > 0) dm.status.frozen = 1; |
| +добавлено | fx.rig.shake(0.15, 14); |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'air' || k === 'glow') applyLook(S.params); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, st = state[c.index]; |
| +добавлено | applyLook(p); |
| +добавлено | if (name === 'charge') { |
| +добавлено | releaseAll(st); |
| +добавлено | c.durations.linger = p.duration + 1.6; |
| +добавлено | st.g.set(c.to.x, 0, c.to.z); |
| +добавлено | // the wind blows across the cast, swung towards the caster's side by `angle` |
| +добавлено | let dx = c.to.x - c.from.x, dz = c.to.z - c.from.z; |
| +добавлено | const l = Math.hypot(dx, dz) || 1, an = p.angle * Math.PI / 180; |
| +добавлено | dx /= l; dz /= l; |
| +добавлено | st.W.set(-dz * Math.cos(an) - dx * Math.sin(an), 0, dx * Math.cos(an) - dz * Math.sin(an)).normalize(); |
| +добавлено | st.age = 0; st.env = 0; st.adv = 0; st.clock = 0; st.gust = 1; st.nextSplat = 0; st.nextHit = 0; st.nextLee = 0.6; st.frozeAll = false; |
| +добавлено | st.seedF = (c.seed % 997) / 997; |
| +добавлено | st.slot = null; |
| +добавлено | for (const s of slots) if (!s.busy) { st.slot = s; s.busy = true; break; } |
| +добавлено | st.push = fx.forces.acquire(); |
| +добавлено | if (st.push) { st.push.type = 5; st.push.p[3] = 0; } |
| +добавлено | st.vortex = fx.forces.acquire(); |
| +добавлено | if (st.vortex) { st.vortex.type = 1; st.vortex.p[1] = 0; } |
| +добавлено | st.light = fx.lights.acquire(); |
| +добавлено | if (st.light) { st.light.range = 9; st.light.intensity = 0; st.light.color.copy(glow).lerp(_white, 0.55); st.light.shadow = false; } |
| +добавлено | } else if (name === 'release') { |
| +добавлено | fx.rig.shake(0.08, 9); |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | // the storm hits: a gust front of snow rolls over the target, frost rings the heart |
| +добавлено | st.age = 0; |
| +добавлено | const g = st.g, R = p.radius, W = st.W; |
| +добавлено | E.snow.pos.set(g.x - W.x * R, 1.2, g.z - W.z * R); E.snow.radius = R; E.snow.vel.set(W.x * 9, 0.5, W.z * 9); E.snow.burst(500 * p.snow); |
| +добавлено | if (driftLine(st, R, 1.0)) { E.drift.vel.set(W.x * 10, 0.6, W.z * 10); E.drift.size = 1.1; E.drift.burst(60 * p.spindrift); } |
| +добавлено | if (p.frost > 0) splat(g.x, g.z, 0.55 * R, 0.9 * p.frost, 'noise', c.seed + 5); |
| +добавлено | fx.impacts.add('frost', _a.set(g.x, 0.8, g.z), R * 1.1, 0.5, p.duration + 3, glow); |
| +добавлено | for (const dm of fx.dummies) if (Math.hypot(dm.pos.x - g.x, dm.pos.z - g.z) < 1.4 * R) dm.hit(_b.copy(W).setY(0.1), 0.45 * p.strength); |
| +добавлено | fx.rig.shake(0.25, 7); |
| +добавлено | if (!fx.reduced) fx.rig.punch(0.6, 0.12); |
| +добавлено | } else if (name === 'done') releaseAll(st); |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, st = state[c.index]; |
| +добавлено | if (c.phase === 'charge') { st.age += dt; charge(c, st, p, dt); return; } |
| +добавлено | st.age += dt; |
| +добавлено | const L = c.durations.linger; |
| +добавлено | // the storm comes in hard on impact, holds, and dies over the last 1.6 s |
| +добавлено | st.env = c.phase === 'linger' ? 1 - smooth(L - 1.6, L - 0.1, c.t) : 0.3 + 0.7 * smooth(0, 0.35, c.t); |
| +добавлено | if (c.phase === 'linger' && !st.frozeAll && c.t >= L - 1.4) flashFreeze(c, st, p); |
| +добавлено | storm(c, st, p, dt); |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | // The spell registry. Every module under src/spells (and its subfolders) whose default export |
| +добавлено | // has a create() is a spell; nothing to edit here when adding one (CONTRIBUTING.md). |
| +добавлено | // A module belongs to `element` (defaults to its own id, for the ten founding elements) and |
| +добавлено | // sorts by `order`, then name. Each of its `forms` is one entry in the spell browser. |
| +добавлено | export const ELEMENTS = [ |
| +добавлено | { id: 'lightning', name: 'Lightning', hue: '#7fb4ff', key: '1' }, |
| +добавлено | { id: 'fire', name: 'Fire', hue: '#ff7a1a', key: '2' }, |
| +добавлено | { id: 'water', name: 'Water', hue: '#2fa8ff', key: '3' }, |
| +добавлено | { id: 'ice', name: 'Ice', hue: '#9ff0ff', key: '4' }, |
| +добавлено | { id: 'earth', name: 'Earth', hue: '#c08a4a', key: '5' }, |
| +добавлено | { id: 'wind', name: 'Wind', hue: '#c8f5e4', key: '6' }, |
| +добавлено | { id: 'void', name: 'Void', hue: '#8a3dff', key: '7' }, |
| +добавлено | { id: 'light', name: 'Light', hue: '#ffe08a', key: '8' }, |
| +добавлено | { id: 'acid', name: 'Acid', hue: '#8aff3c', key: '9' }, |
| +добавлено | { id: 'arcane', name: 'Arcane', hue: '#ff4fd8', key: '0' }, |
| +добавлено | { id: 'hybrid', name: 'Hybrid', hue: '#ffffff', key: 'h' }, |
| +добавлено | ]; |
| +добавлено | |
| +добавлено | const modules = import.meta.glob(['./*.js', './*/*.js', '!./index.js', '!./cast.js', '!./combos.js', '!./_template.js'], { eager: true }); |
| +добавлено | const rank = Object.fromEntries(ELEMENTS.map((e, i) => [e.id, i])); |
| +добавлено | |
| +добавлено | export const SPELLS = Object.values(modules) |
| +добавлено | .map((m) => m.default) |
| +добавлено | .filter((s) => s && typeof s.create === 'function') |
| +добавлено | .map((s) => { s.element ??= s.id; return s; }) |
| +добавлено | .filter((s) => s.element in rank) |
| +добавлено | .sort((a, b) => rank[a.element] - rank[b.element] || (a.order ?? 0) - (b.order ?? 0) || a.name.localeCompare(b.name)); |
| +добавлено | |
| +добавлено | // one browser entry per (module, form) |
| +добавлено | export const ENTRIES = SPELLS.flatMap((s) => (s.forms?.length ? s.forms : [s.name]).map((form) => ({ |
| +добавлено | spell: s, form: s.forms?.length ? form : '', name: s.forms?.length ? form : s.name, element: s.element, key: `${s.id}:${form}`, |
| +добавлено | }))); |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { ARENA, arenaRaycast, COMMON_GLSL, NOISE_GLSL, FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL, FX_BLENDING } from '../vfx/common.js'; |
| +добавлено | import { arenaUniforms } from '../scene/arenaMaterial.js'; |
| +добавлено | |
| +добавлено | // Light (PLAN 3). Judgment, matched against crepuscular rays through a storm shelf and film "wrath |
| +добавлено | // of god" shots: a storm deck races out from over the target and the sun goes (a raymarched slab of |
| +добавлено | // cloud with a billowed underside, dimming the shared sun and sky light); it spins, a hole tears open |
| +добавлено | // in it and blazes, a thin ray drops through, then the shaft slams open. The shaft is two volumes: a |
| +добавлено | // wide sheath of dust lit through the cloud gaps (god-ray blades diverging from the hole, a rolling |
| +добавлено | // 3D dust field, HG in-scatter) and a kernel of braided plasma lanes racing down it (structure, not a |
| +добавлено | // clipped stripe), plus full-res helical threads. The light itself hits the arena: one shadowed point |
| +добавлено | // light on the axis (hard capsule shadows off every pillar and dummy), the deck lit from its hole. |
| +добавлено | // Impact: an air and a ground shock, a force shell, a rolling dust ring lit from inside, rock thrown, |
| +добавлено | // molten spatter, the floor fractured and glowing (ground-field heat, crack, char, magma impacts). |
| +добавлено | // Radiant Lance: a held beam from the focus: a full-res core ribbon, the same volume as a soft |
| +добавлено | // sheath, motes streaming along it, a contact flare, glow splats where it lands, a line light. |
| +добавлено | |
| +добавлено | // shape +y = the direction the light travels; pl.xz is the cross-section (|pl.xz| = 1 at radius) |
| +добавлено | const HALO = 1.6; // proxy radius / beam radius |
| +добавлено | const BEAM_GLSL = /* glsl */` |
| +добавлено | #define HALO ${HALO.toFixed(2)} |
| +добавлено | uniform vec4 uBeam; // radiance per metre, god-ray contrast, HG g, head (m from the source) |
| +добавлено | uniform vec4 uBeam2; // radius m, length m (source -> contact), source fade (fraction), contact flare |
| +добавлено | uniform vec3 uBeamSrc; |
| +добавлено | uniform vec3 uBeamDir; |
| +добавлено | uniform vec3 uBeamCore; |
| +добавлено | uniform vec3 uBeamGlow; |
| +добавлено | uniform vec4 uOccA[4]; // occluders crossing the beam: a.xyz, radius |
| +добавлено | uniform vec4 uOccB[4]; // b.xyz, - |
| +добавлено | uniform float uOccN; |
| +добавлено | uniform float uBeamSeed; |
| +добавлено | void volumeSample(vec3 pw, vec3 pl, out float sigma, out vec3 albedo, out vec3 emission) { |
| +добавлено | // pure emission. sigma stays under the integrator's 1e-4 'occupied' test but isn't a literal 0: |
| +добавлено | // fxc folds the dead e / sigma branch and warns about a division by zero |
| +добавлено | sigma = 1e-6 * uBeam2.x; albedo = vec3(0.0); emission = vec3(0.0); |
| +добавлено | // the proxy is HALO x wider than the beam: r is in beam radii, the rim beyond 1 is a soft halo |
| +добавлено | float R = uBeam2.x, L = uBeam2.y; |
| +добавлено | float r = length(pl.xz) * uVolScale.x / R; |
| +добавлено | if (r >= HALO) return; |
| +добавлено | float s = dot(pw - uBeamSrc, uBeamDir); |
| +добавлено | // cross-section in metres, turning slowly; streaks keep their size while the pillar slams open |
| +добавлено | float a = uTime * 0.13; |
| +добавлено | vec2 q = mat2(cos(a), -sin(a), sin(a), cos(a)) * (pl.xz * uVolScale.x); |
| +добавлено | // god rays: noise over the cross-section, stretched ~100x along the axis, pouring downstream |
| +добавлено | vec4 n = noise3v(vec3(q * 0.42, s * 0.006 - uTime * 0.045) + uBeamSeed); |
| +добавлено | float rays = clamp((n.r * 0.6 + n.b * 0.4) * 2.0 - 0.55, 0.0, 1.0); |
| +добавлено | rays = mix(1.0, 0.18 + 1.7 * rays * rays, uBeam.y); |
| +добавлено | float body = (0.55 + 0.45 * exp(-r * r * 3.0)) * smoothstep(1.0, 0.55, r) |
| +добавлено | + 0.2 * exp(-max(r - 0.6, 0.0) * 3.0) * smoothstep(HALO, HALO - 0.3, r); |
| +добавлено | float core = exp(-r * r * 30.0); |
| +добавлено | float along = smoothstep(0.0, uBeam2.z, s / L) * smoothstep(uBeam.w, uBeam.w - 0.8, s) |
| +добавлено | * smoothstep(L + 0.6 * R, L, s); |
| +добавлено | // occluders between this sample and the source shadow it (capsule vs light path, soft edge) |
| +добавлено | float vis = 1.0; |
| +добавлено | int no = int(uOccN); |
| +добавлено | for (int i = 0; i < 4; i++) { |
| +добавлено | if (i >= no) break; |
| +добавлено | float sp, d = segSegDist(pw, pw - uBeamDir * max(s, 0.0), uOccA[i].xyz, uOccB[i].xyz, sp); |
| +добавлено | // near-parallel light: a fixed soft edge (the closest-point parameter is unstable when the |
| +добавлено | // light path runs along a capsule, so it can't drive the penumbra) |
| +добавлено | vis *= smoothstep(uOccA[i].w - 0.03, uOccA[i].w + 0.08, d); |
| +добавлено | } |
| +добавлено | // HG in-scatter of the beam's own light towards the eye, normalised to 1 seen side-on. Looking |
| +добавлено | // down a long column the chord grows ~L/R; ease that back (taste, not physics) |
| +добавлено | float mu = dot(uBeamDir, normalize(cameraPosition - pw)); |
| +добавлено | float g = uBeam.z, g2 = g * g; |
| +добавлено | float phase = pow((1.0 + g2) / max(1.0 + g2 - 2.0 * g * mu, 1e-3), 1.5) / (1.0 + 5.0 * mu * mu * mu * mu); |
| +добавлено | // flare where the light lands |
| +добавлено | vec3 dc = pw - uBeamSrc - uBeamDir * L; |
| +добавлено | float flare = uBeam2.w * exp(-dot(dc, dc) / (R * R * 0.3)) * step(s, uBeam.w + 0.01); |
| +добавлено | float e = (body * rays + core * 1.4) * along * vis * phase + flare; |
| +добавлено | emission = mix(uBeamGlow, uBeamCore, clamp(core * 1.3 + flare * 0.5, 0.0, 1.0)) * (e * uBeam.x); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // full-res core: a straight strip, p0 = (source, width), p1 = (end, -), |
| +добавлено | // p2 = (seed, length m, source fade fraction, head m) |
| +добавлено | const CORE_PATH = /* glsl */` |
| +добавлено | vec3 ribbonPath(float t, vec4 p0, vec4 p1, vec4 p2, vec4 p3, out float width) { |
| +добавлено | width = p0.w; |
| +добавлено | return mix(p0.xyz, p1.xyz, t); |
| +добавлено | }`; |
| +добавлено | const CORE_SHADE = /* glsl */` |
| +добавлено | vec3 ribbonShade(RibbonFrag r, out vec3 media, out float alpha, out float distortion) { |
| +добавлено | media = vec3(0.0); alpha = 0.0; distortion = 0.0; |
| +добавлено | float y = r.uv.y, s = r.uv.x * r.p2.y; |
| +добавлено | float hot = exp(-y * y * 22.0), soft = exp(-y * y * 3.5) * 0.3; |
| +добавлено | // energy racing along the beam |
| +добавлено | float flow = 0.82 + 0.18 * sin(s * 2.7 - uTime * 40.0 + r.p2.x) * sin(s * 1.1 - uTime * 21.0 + r.p2.x * 1.7); |
| +добавлено | float k = smoothstep(0.0, r.p2.z, r.uv.x) * smoothstep(r.p2.w, r.p2.w - 0.5, s); |
| +добавлено | vec3 c = r.color; |
| +добавлено | float peak = max(c.r, max(c.g, c.b)); |
| +добавлено | return (mix(c, vec3(peak), 0.65) * hot + c * soft) * flow * k; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // Judgment's kernel: braided plasma lanes. Same uniforms as BEAM_GLSL (placeBeam fills them) |
| +добавлено | const KERNEL_GLSL = /* glsl */` |
| +добавлено | #define HALO ${HALO.toFixed(2)} |
| +добавлено | uniform vec4 uBeam; // radiance per metre, lane contrast, HG g, head (m from the source) |
| +добавлено | uniform vec4 uBeam2; // radius m, length m, -, contact flare |
| +добавлено | uniform vec3 uBeamSrc; |
| +добавлено | uniform vec3 uBeamDir; |
| +добавлено | uniform vec3 uBeamCore; |
| +добавлено | uniform vec3 uBeamGlow; |
| +добавлено | uniform vec4 uOccA[4]; |
| +добавлено | uniform vec4 uOccB[4]; |
| +добавлено | uniform float uOccN; |
| +добавлено | uniform float uBeamSeed; |
| +добавлено | void volumeSample(vec3 pw, vec3 pl, out float sigma, out vec3 albedo, out vec3 emission) { |
| +добавлено | sigma = 1e-6 * uBeam2.x; albedo = vec3(0.0); emission = vec3(0.0); |
| +добавлено | float R = uBeam2.x, L = uBeam2.y; |
| +добавлено | float r = length(pl.xz) * uVolScale.x / R; |
| +добавлено | if (r >= HALO) return; |
| +добавлено | float s = dot(pw - uBeamSrc, uBeamDir); |
| +добавлено | float along = smoothstep(0.0, 1.5, s) * smoothstep(uBeam.w, uBeam.w - 0.8, s) * smoothstep(L + 0.6 * R, L, s); |
| +добавлено | if (along <= 0.0) return; |
| +добавлено | // lanes: a twisting cross-section, stretched ~30x along the axis and pouring downstream |
| +добавлено | float a = s * 0.2 + uTime * 1.1, ca = cos(a), sa = sin(a); |
| +добавлено | vec2 q = mat2(ca, -sa, sa, ca) * (pl.xz * uVolScale.x); |
| +добавлено | vec4 n = noise3v(vec3(q * 0.42, s * 0.022 - uTime * 0.7) + uBeamSeed); |
| +добавлено | float lane = 1.0 - abs(2.0 * n.r - 1.0); |
| +добавлено | lane *= lane * lane; |
| +добавлено | // knots of brighter plasma racing down the lanes |
| +добавлено | float knot = noise3(vec3(q * 0.16, s * 0.05 - uTime * 2.1) + uBeamSeed * 1.7); |
| +добавлено | float lanes = mix(1.0, 0.16 + 3.4 * lane * (0.35 + 1.6 * knot), uBeam.y); |
| +добавлено | float env = exp(-r * r * 2.0) * smoothstep(HALO, 0.75, r); |
| +добавлено | float hot = exp(-r * r * 7.0); |
| +добавлено | // occluders between this sample and the source shadow it |
| +добавлено | float vis = 1.0; |
| +добавлено | int no = int(uOccN); |
| +добавлено | for (int i = 0; i < 4; i++) { |
| +добавлено | if (i >= no) break; |
| +добавлено | float sp, d = segSegDist(pw, pw - uBeamDir * max(s, 0.0), uOccA[i].xyz, uOccB[i].xyz, sp); |
| +добавлено | vis *= smoothstep(uOccA[i].w - 0.03, uOccA[i].w + 0.08, d); |
| +добавлено | } |
| +добавлено | float mu = dot(uBeamDir, normalize(cameraPosition - pw)); |
| +добавлено | float g = uBeam.z, g2 = g * g; |
| +добавлено | float phase = pow((1.0 + g2) / max(1.0 + g2 - 2.0 * g * mu, 1e-3), 1.5) / (1.0 + 5.0 * mu * mu * mu * mu); |
| +добавлено | vec3 dc = pw - uBeamSrc - uBeamDir * L; |
| +добавлено | float flare = uBeam2.w * exp(-dot(dc, dc) / (R * R * 0.5)) * step(s, uBeam.w + 0.01); |
| +добавлено | float e = (env * (0.2 + 0.8 * lanes) + hot * lanes * 0.9) * along * vis * phase + flare; |
| +добавлено | // amber in the thin gas, white only in the hottest lanes |
| +добавлено | emission = mix(uBeamGlow, uBeamCore, clamp(hot * lanes * 0.75 + flare * 0.4 + 0.15, 0.0, 1.0)) * (e * uBeam.x); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // Judgment's sheath: dust in the light that gets through the cloud gaps. Pure in-scatter, so it costs |
| +добавлено | // two noise taps a sample and never occludes the world. The fan spreads from the hole: the |
| +добавлено | // cross-section is normalised by the local radius, so each blade is a true diverging ray. |
| +добавлено | const SHEATH_GLSL = /* glsl */` |
| +добавлено | #define HALO ${HALO.toFixed(2)} |
| +добавлено | uniform vec4 uBeam; // radiance per metre, ray contrast, HG g, head (m from the source) |
| +добавлено | uniform vec4 uBeam2; // radius m (x HALO = outer edge), length m, dust boil at the floor 0..1, - |
| +добавлено | uniform vec3 uBeamSrc; |
| +добавлено | uniform vec3 uBeamDir; |
| +добавлено | uniform vec3 uBeamCore; |
| +добавлено | uniform vec3 uBeamGlow; |
| +добавлено | uniform float uBeamSeed; |
| +добавлено | void volumeSample(vec3 pw, vec3 pl, out float sigma, out vec3 albedo, out vec3 emission) { |
| +добавлено | sigma = 1e-6 * uBeam2.x; albedo = vec3(0.0); emission = vec3(0.0); |
| +добавлено | float R = uBeam2.x * HALO, L = uBeam2.y; |
| +добавлено | float s = dot(pw - uBeamSrc, uBeamDir), k = clamp(s / L, 0.0, 1.0); |
| +добавлено | float Rz = R * (0.42 + 0.58 * k); |
| +добавлено | vec2 q = pl.xz * uVolScale.x / Rz; |
| +добавлено | float r = length(q); |
| +добавлено | if (r >= 1.0) return; |
| +добавлено | float along = smoothstep(0.0, 0.05, k) * smoothstep(uBeam.w, uBeam.w - 1.0, s) * smoothstep(L + 0.5, L, s); |
| +добавлено | if (along <= 0.0) return; |
| +добавлено | // the gaps in the cloud: two scales of blade, nearly frozen, they only slide as the deck turns |
| +добавлено | vec4 n = noise3v(vec3(q * 0.34 + uBeamSeed, uTime * 0.03)); |
| +добавлено | float blades = smoothstep(0.32, 0.72, n.r * 0.7 + n.g * 0.3); |
| +добавлено | blades = mix(1.0, 0.03 + 1.7 * blades * blades, uBeam.y); |
| +добавлено | // dust: rolling, falling, bunched: fades out where the march can't resolve it |
| +добавлено | vec3 dp = pw * 0.2 + vec3(0.13 * sin(pw.y * 0.4 + uTime * 0.5), uTime * 0.2, 0.0) + uBeamSeed; |
| +добавлено | float dust = noise3(dp); |
| +добавлено | dust = 0.12 + 2.5 * dust * dust + uBeam2.z * 1.8 * exp(-(L - s) * 0.5) * smoothstep(0.25, 0.7, dust); |
| +добавлено | float body = smoothstep(1.0, 0.62, r) * (0.3 + 0.7 * exp(-r * r * 2.6)); |
| +добавлено | float mu = dot(uBeamDir, normalize(cameraPosition - pw)); |
| +добавлено | float g = uBeam.z, g2 = g * g; |
| +добавлено | float phase = pow((1.0 + g2) / max(1.0 + g2 - 2.0 * g * mu, 1e-3), 1.5) / (1.0 + 3.0 * mu * mu * mu * mu); |
| +добавлено | emission = mix(uBeamGlow, uBeamCore, exp(-r * r * 5.0) * 0.55) * (body * blades * dust * along * phase * uBeam.x); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // full-res helical threads wound round the kernel: p0 = source, width; p1 = end, seed; |
| +добавлено | // p2 = helix radius, twist rad/m, -, head m; p3 = spin rad/s, length m, -, - |
| +добавлено | const THREAD_PATH = /* glsl */` |
| +добавлено | vec3 ribbonPath(float t, vec4 p0, vec4 p1, vec4 p2, vec4 p3, out float width) { |
| +добавлено | vec3 ax = p1.xyz - p0.xyz, dir = ax / max(p3.y, 1e-3); |
| +добавлено | mat3 B = basisFrom(dir); |
| +добавлено | float s = t * p3.y, seed = p1.w; |
| +добавлено | float ph = s * p2.y + seed * 6.283 + uTime * p3.x; |
| +добавлено | float rr = p2.x * (0.5 + 0.4 * sin(s * 0.41 + seed * 5.0 + uTime * 2.3) + 0.1 * sin(s * 1.7 - uTime * 8.0 + seed * 9.0)); |
| +добавлено | width = p0.w * (0.6 + 0.4 * sin(s * 0.9 + seed * 3.0 - uTime * 5.0)); |
| +добавлено | return p0.xyz + ax * t + (B[0] * cos(ph) + B[1] * sin(ph)) * rr; |
| +добавлено | }`; |
| +добавлено | const THREAD_SHADE = /* glsl */` |
| +добавлено | vec3 ribbonShade(RibbonFrag r, out vec3 media, out float alpha, out float distortion) { |
| +добавлено | media = vec3(0.0); alpha = 0.0; distortion = 0.0; |
| +добавлено | float y = r.uv.y, s = r.uv.x * r.p3.y; |
| +добавлено | float prof = exp(-y * y * 5.0); |
| +добавлено | float flow = 0.25 + 0.75 * pow(0.5 + 0.5 * sin(s * 1.3 - uTime * 34.0 + r.p1.w * 19.0) * sin(s * 0.37 - uTime * 11.0 + r.p1.w * 7.0), 2.0); |
| +добавлено | float k = smoothstep(0.5, 3.0, s) * smoothstep(r.p2.w, r.p2.w - 0.5, s); |
| +добавлено | return r.color * prof * flow * k; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // the storm deck: a slab of cloud raymarched from its underside (half-res, one quad). Lit from the |
| +добавлено | // hole with one shadow tap towards it; the sky behind it is hidden, the bright horizon band under |
| +добавлено | // the deck stays (the shelf-cloud look). Nothing else writes the sun: see stormApply. |
| +добавлено | const DECK_VERT = FX_VERT_HEAD + /* glsl */` |
| +добавлено | out vec3 vW; |
| +добавлено | void main() { |
| +добавлено | vec4 w = modelMatrix * vec4(position, 1.0); |
| +добавлено | vW = w.xyz; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * w; |
| +добавлено | }`; |
| +добавлено | const DECK_FRAG = FX_FRAG_HEAD + '#define texture(s, p) textureLod(s, p, 0.0)\n' + COMMON_GLSL + NOISE_GLSL + FX_OUT_GLSL + /* glsl */` |
| +добавлено | uniform vec4 uDeck; // base y, thickness, cover 0..1, front radius m |
| +добавлено | uniform vec4 uDeck2; // spin angle rad (integrated), drift, steps, - |
| +добавлено | uniform vec2 uDeckC; // swirl centre xz |
| +добавлено | uniform vec4 uHole[2]; // x, z, radius m, light |
| +добавлено | uniform vec3 uDeckLit; |
| +добавлено | uniform vec3 uDeckHot; |
| +добавлено | uniform vec3 uDeckDark; |
| +добавлено | in vec3 vW; |
| +добавлено | float deckD(vec3 p) { |
| +добавлено | vec2 dv = p.xz - uDeckC; |
| +добавлено | float rc = length(dv), a = uDeck2.x / (1.0 + rc * 0.085), ca = cos(a), sa = sin(a); |
| +добавлено | vec2 q = vec2(ca * dv.x - sa * dv.y, sa * dv.x + ca * dv.y) + uDeckC; |
| +добавлено | float h = (p.y - uDeck.x) / uDeck.y; |
| +добавлено | vec4 n = noise3v(vec3(q * 0.011, uDeck2.y)); |
| +добавлено | float m = noise3(vec3(q * 0.03, h * 0.12 + uDeck2.y * 1.7)); |
| +добавлено | float d = n.r * 0.5 + m * 0.62 - 0.56 + (h - 0.5) * 1.05; |
| +добавлено | d += (smoothstep(uDeck.w, uDeck.w - 24.0, rc) * uDeck.z - 1.0) * 1.3; |
| +добавлено | for (int i = 0; i < 2; i++) { |
| +добавлено | float R = uHole[i].z; |
| +добавлено | if (R <= 0.01) continue; |
| +добавлено | float r = distance(p.xz, uHole[i].xy), rim = R * (0.72 + 0.6 * n.g); |
| +добавлено | d -= (1.0 - smoothstep(rim, rim + 3.5 + 0.35 * R, r)) * 1.4; |
| +добавлено | } |
| +добавлено | return d; |
| +добавлено | } |
| +добавлено | void main() { |
| +добавлено | vec3 rd = normalize(vW - cameraPosition); |
| +добавлено | if (rd.y < 0.012) discard; |
| +добавлено | // stacked horizontal slices, front to back: a sample plane is fixed in the world, so each layer is |
| +добавлено | // continuous however far away it is (a march along the ray shows its steps as contour bands) |
| +добавлено | int N = int(uDeck2.z); |
| +добавлено | float tAll = uDeck.y / rd.y, dt = tAll / float(N); |
| +добавлено | vec3 p = vW + rd * (dt * 0.5); |
| +добавлено | float T = 1.0, zS = 0.0, zW = 0.0; |
| +добавлено | vec3 scat = vec3(0.0); |
| +добавлено | // billows finer than the slices (shading only), and the broad light and dark of the storm |
| +добавлено | float fine = noise3(vec3(vW.xz * 0.11, uDeck2.y * 3.0)), big = noise3(vec3(vW.xz * 0.007 + 0.3, uDeck2.y * 0.5)); |
| +добавлено | for (int i = 0; i < 16; i++) { |
| +добавлено | if (i >= N || T < 0.03) break; |
| +добавлено | float d = deckD(p); |
| +добавлено | if (d > 0.0) { |
| +добавлено | float sg = 2.2 * clamp(d * 4.0, 0.0, 1.0); |
| +добавлено | float h = clamp((p.y - uDeck.x) / uDeck.y, 0.0, 1.0); |
| +добавлено | vec3 lit = vec3(0.0); |
| +добавлено | for (int k = 0; k < 2; k++) { |
| +добавлено | float I = uHole[k].w; |
| +добавлено | if (I <= 0.0) continue; |
| +добавлено | vec3 v = vec3(uHole[k].x, uDeck.x + 0.35 * uDeck.y, uHole[k].y) - p; |
| +добавлено | float l = length(v), tau = max(deckD(p + v / max(l, 1e-3) * min(2.6, l)), 0.0) * 2.4; |
| +добавлено | lit += uDeckLit * (I * (exp(-l / 5.0) * 1.1 + 0.12 * exp(-l / 14.0)) * (exp(-tau) * 0.85 + 0.15)); |
| +добавлено | } |
| +добавлено | vec3 c = (uDeckDark * (1.2 - 1.05 * h) * (0.3 + 1.6 * big) + lit) * (0.7 + 0.6 * fine); |
| +добавлено | float tr = exp(-sg * min(dt, 5.0)), a = T * (1.0 - tr); |
| +добавлено | scat += a * c; |
| +добавлено | zS += a * (dot(p - vW, rd)); zW += a; |
| +добавлено | T *= tr; |
| +добавлено | } |
| +добавлено | p += rd * dt; |
| +добавлено | } |
| +добавлено | // through the hole: the blaze above the cloud, streaked by angle |
| +добавлено | vec3 em = vec3(0.0); |
| +добавлено | vec3 pt = vW + rd * tAll; |
| +добавлено | for (int k = 0; k < 2; k++) { |
| +добавлено | float R = uHole[k].z; |
| +добавлено | if (R <= 0.01) continue; |
| +добавлено | vec2 dv = pt.xz - uHole[k].xy; |
| +добавлено | float l = length(dv), r = l / R, st = noise3(vec3(dv / max(l, 1e-3) * 0.6, uTime * 0.05)); |
| +добавлено | em += uDeckHot * (uHole[k].w * smoothstep(1.3, 0.15, r) * (0.3 + 1.7 * st * st)); |
| +добавлено | } |
| +добавлено | float far = smoothstep(150.0, 70.0, distance(vW.xz, cameraPosition.xz)); |
| +добавлено | float alpha = (1.0 - T) * far; |
| +добавлено | if (alpha < 0.002 && dot(em, em) < 1e-6) discard; |
| +добавлено | vec3 fwd = -vec3(viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2]); |
| +добавлено | float z = dot(vW - cameraPosition, fwd) + (zW > 0.0 ? zS / zW : 0.0) * dot(rd, fwd); |
| +добавлено | fxOut(scat / max(1.0 - T, 1e-3), alpha, em * T * far, vec2(0.0), z); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const SKY = 22; // m: the shaft's source (above the deck) |
| +добавлено | const DECK_Y = 13, DECK_H = 8; // storm deck base, thickness (m); a low ceiling keeps the hole in frame |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _c = new THREE.Vector3(); |
| +добавлено | const _up = new THREE.Vector3(0, 1, 0); |
| +добавлено | const _d1 = new THREE.Vector3(), _d2 = new THREE.Vector3(), _r = new THREE.Vector3(), _c1 = new THREE.Vector3(), _c2 = new THREE.Vector3(); |
| +добавлено | const clamp = THREE.MathUtils.clamp, sstep = THREE.MathUtils.smoothstep; |
| +добавлено | const easeOutBack = (x) => 1 + 2.70158 * (x - 1) ** 3 + 1.70158 * (x - 1) ** 2; |
| +добавлено | // reused option objects (the systems copy what they need) |
| +добавлено | const SPLAT = { x: 0, z: 0, radius: 1, glow: 0, heat: 0, char: 0, crack: 0, pattern: 'radial', seed: 0 }; |
| +добавлено | const SHOCK = { pos: _c, radius0: 0.5, radius1: 7, duration: 0.8, thickness: 0.7, distortion: 0.035, color: null, intensity: 3, ground: true }; |
| +добавлено | const SIGIL = { pos: _a, normal: _b, radius: 1.5, color: null, intensity: 0, reveal: 0, rotation: 0, style: 'holy', seed: 0 }; |
| +добавлено | |
| +добавлено | // distance between segments p1-q1 and p2-q2 (Ericson 5.1.9), for culling occluders to the beam |
| +добавлено | function segSeg(p1, q1, p2, q2) { |
| +добавлено | _d1.subVectors(q1, p1); _d2.subVectors(q2, p2); _r.subVectors(p1, p2); |
| +добавлено | const a = _d1.dot(_d1), e = _d2.dot(_d2), f = _d2.dot(_r); |
| +добавлено | let s = 0, t = 0; |
| +добавлено | if (a < 1e-9) t = e < 1e-9 ? 0 : clamp(f / e, 0, 1); |
| +добавлено | else { |
| +добавлено | const c = _d1.dot(_r); |
| +добавлено | if (e < 1e-9) s = clamp(-c / a, 0, 1); |
| +добавлено | else { |
| +добавлено | const b = _d1.dot(_d2), den = a * e - b * b; |
| +добавлено | s = den > 1e-9 ? clamp((b * f - c * e) / den, 0, 1) : 0; |
| +добавлено | t = (b * s + f) / e; |
| +добавлено | if (t < 0) { t = 0; s = clamp(-c / a, 0, 1); } else if (t > 1) { t = 1; s = clamp((b - c) / a, 0, 1); } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | return _c1.copy(p1).addScaledVector(_d1, s).distanceTo(_c2.copy(p2).addScaledVector(_d2, t)); |
| +добавлено | } |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'light', name: 'Light', key: '8', hue: '#ffe08a', |
| +добавлено | shape: 'beam', formShapes: { Judgment: 'strike' }, forms: ['Judgment', 'Radiant Lance'], |
| +добавлено | params: { intensity: 1, width: 2, sigil: 1.5, tilt: 18, rays: 0.85, scatter: 0.35, motes: 1, feathers: 1, glow: 1, storm: 1, dust: 1, molten: 1, lance: 0.6, core: '#fff4dc', color: '#ffb84a' }, |
| +добавлено | ranges: { |
| +добавлено | intensity: [0, 3, 0.01], width: [0.5, 4, 0.01], sigil: [0.6, 3, 0.01], tilt: [0, 25, 0.1], rays: [0, 1, 0.01], |
| +добавлено | scatter: [0, 0.9, 0.01], motes: [0, 3, 0.01], feathers: [0, 3, 0.01], glow: [0, 3, 0.01], storm: [0, 1, 0.01], dust: [0, 3, 0.01], molten: [0, 3, 0.01], lance: [0.2, 1.5, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | dawn: { core: '#fff0e8', color: '#ff8a5c', tilt: 16, rays: 0.9 }, |
| +добавлено | seraphic: { core: '#ffffff', color: '#bcd4ff', intensity: 1.25, feathers: 2.2, scatter: 0.5 }, |
| +добавлено | 'solar flare': { core: '#fff3c0', color: '#ff9a12', width: 3.2, intensity: 1.5, rays: 1, motes: 1.8 }, |
| +добавлено | }, |
| +добавлено | // per form in phase('charge'); sustain is the lance's minimum channel time |
| +добавлено | timing: { charge: 1.3, impact: 0.55, linger: 3.4, sustain: 2 }, |
| +добавлено | kinds: { |
| +добавлено | mote: { style: 'glow', drag: 1.3, buoyancy: 1.5, curl: 1.1, curlScale: 0.45, forceResponse: 0.7, life: [1.6, 3.4], size: [0.026, 0.01], stretch: 0.012, emissive: 4.5, color: ['#ffe7b0', '#ffa83a'], alpha: [1, 0], fadeIn: 0.12, fadeOut: 0.45 }, |
| +добавлено | feather: { style: 'glow', drag: 3, gravity: 0.4, curl: 2.6, curlScale: 1.2, forceResponse: 0.3, life: [2.4, 4.2], size: [0.045, 0.03], stretch: 0.16, emissive: 3.5, color: ['#fffaf0', '#ffe0a0'], alpha: [1, 0], fadeIn: 0.15, fadeOut: 0.35, restitution: 0.05, friction: 0.9 }, |
| +добавлено | stream: { style: 'glow', drag: 0.5, life: [0.25, 0.6], size: [0.022, 0.006], stretch: 0.03, emissive: 7, color: ['#fff8e6', '#ffb040'], alpha: [1, 0], fadeIn: 0.05, restitution: 0.25, friction: 0.5 }, |
| +добавлено | spark: { style: 'glow', drag: 0.9, gravity: 5, restitution: 0.35, friction: 0.35, life: [0.3, 0.9], size: [0.024, 0.008], stretch: 0.03, emissive: 9, color: ['#ffffff', '#ffbf50'], alpha: [1, 0] }, |
| +добавлено | // Judgment: dust caught in the shaft (flecks), the rolling shock ring, loose rock, molten spatter |
| +добавлено | fleck: { style: 'glow', drag: 2.2, gravity: 0.15, curl: 1.6, curlScale: 0.5, forceResponse: 0.8, life: [0.9, 2.2], size: [0.012, 0.007], emissive: 3, color: ['#fff0d0', '#ffc070'], alpha: [1, 0], fadeIn: 0.25, fadeOut: 0.35 }, |
| +добавлено | dust: { style: 'media', drag: 3.4, buoyancy: 0.22, curl: 1.8, curlScale: 0.42, life: [1.2, 2.8], size: [0.24, 1.25], sizePow: 0.5, color: ['#6a5f55', '#35302b'], alpha: [0.5, 0], fadeIn: 0.03, fadeOut: 0.55, density: 1.6, erosion: 0.9, lightResponse: 0.07, forceResponse: 0.7, restitution: 0, friction: 0.05 }, |
| +добавлено | rock: { style: 'debris', mesh: 'rock', gravity: 10.5, drag: 0.1, restitution: 0.3, friction: 0.6, life: [1.8, 3.2], size: [0.055, 0.055], spin: 8, color: ['#14110e', new THREE.Color(0.5, 0.12, 0.02)], alpha: [1, 0.85], fadeOut: 0.14, forceResponse: 0.2 }, |
| +добавлено | molten: { style: 'glow', drag: 0.45, gravity: 9.8, restitution: 0.2, friction: 0.7, life: [0.5, 1.5], size: [0.03, 0.009], stretch: 0.028, emissive: 6, temp: [2500, 1000], paints: 'heat', paintAmount: 0.05 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const mote = P.emitter('light.mote', { shape: 'disk', radius: 1, spread: 0.6 }); |
| +добавлено | const ringMote = P.emitter('light.mote', { shape: 'ring', radius: 1.5, spread: 0.25 }); |
| +добавлено | const gather = P.emitter('light.mote', { shape: 'shell', radius: 1, spread: 0.2, radial: -2.4, life: 0.25 }); |
| +добавлено | const feather = P.emitter('light.feather', { shape: 'line', radius: 0.8, spread: 0.6 }); |
| +добавлено | const stream = P.emitter('light.stream', { shape: 'line', radius: 0.12, spread: 0.6 }); |
| +добавлено | const spark = P.emitter('light.spark', { shape: 'sphere', radius: 0.25, spread: 3, radial: 4 }); |
| +добавлено | const fleck = P.emitter('light.fleck', { shape: 'line', radius: 1.5, spread: 0.25 }); |
| +добавлено | const dust = P.emitter('light.dust', { shape: 'ring', radius: 1, spread: 0.4, radial: 11 }); |
| +добавлено | const rock = P.emitter('light.rock', { shape: 'disk', radius: 1, spread: 2, radial: 4.5 }); |
| +добавлено | const molten = P.emitter('light.molten', { shape: 'disk', radius: 0.8, spread: 3, radial: 3.5 }); |
| +добавлено | |
| +добавлено | // colours: parsed only when the param string changes (Color.set allocates) |
| +добавлено | const coreCol = new THREE.Color(), glowCol = new THREE.Color(), lightCol = new THREE.Color(), sigCol = new THREE.Color(); |
| +добавлено | const coreV = new THREE.Vector3(), glowV = new THREE.Vector3(), deckLit = new THREE.Vector3(), deckHot = new THREE.Vector3(); |
| +добавлено | let coreHex = '', glowHex = ''; |
| +добавлено | function colours(p) { |
| +добавлено | if (p.core === coreHex && p.color === glowHex) return; |
| +добавлено | coreHex = p.core; glowHex = p.color; |
| +добавлено | coreCol.set(p.core); glowCol.set(p.color); |
| +добавлено | coreV.set(coreCol.r, coreCol.g, coreCol.b); glowV.set(glowCol.r, glowCol.g, glowCol.b); |
| +добавлено | lightCol.copy(glowCol).lerp(coreCol, 0.4); |
| +добавлено | sigCol.copy(glowCol).lerp(coreCol, 0.25); |
| +добавлено | deckLit.set(lightCol.r, lightCol.g, lightCol.b); |
| +добавлено | deckHot.set(coreCol.r, coreCol.g, coreCol.b); |
| +добавлено | } |
| +добавлено | SHOCK.color = glowCol; |
| +добавлено | SIGIL.color = sigCol; |
| +добавлено | |
| +добавлено | // beam volumes: one program, four handles (3 pillars or lances at once + 1) |
| +добавлено | const beams = []; |
| +добавлено | for (let i = 0; i < 4; i++) { |
| +добавлено | const v = fx.volumes.add({ |
| +добавлено | shape: 'capsule', glsl: BEAM_GLSL, maxSteps: 16, |
| +добавлено | uniforms: { |
| +добавлено | uBeam: { value: new THREE.Vector4() }, uBeam2: { value: new THREE.Vector4(1, 1, 0.5, 0) }, // safe for the warm-up frame |
| +добавлено | uBeamSrc: { value: new THREE.Vector3() }, uBeamDir: { value: new THREE.Vector3(0, -1, 0) }, |
| +добавлено | uBeamCore: { value: coreV }, uBeamGlow: { value: glowV }, |
| +добавлено | uOccA: { value: [0, 1, 2, 3].map(() => new THREE.Vector4()) }, uOccB: { value: [0, 1, 2, 3].map(() => new THREE.Vector4()) }, |
| +добавлено | uOccN: { value: 0 }, uBeamSeed: { value: 0 }, |
| +добавлено | }, |
| +добавлено | }); |
| +добавлено | v.busy = false; |
| +добавлено | beams.push(v); |
| +добавлено | } |
| +добавлено | const cores = new fx.AnalyticRibbons({ max: 6, segments: 12, pathGLSL: CORE_PATH, shadeGLSL: CORE_SHADE, target: 'lines' }); |
| +добавлено | fx.scenes.lines.add(cores.mesh); |
| +добавлено | |
| +добавлено | // Judgment: kernel + sheath volumes, helical threads, the storm deck (one per arena, two holes) |
| +добавлено | const beamU = () => ({ |
| +добавлено | uBeam: { value: new THREE.Vector4() }, uBeam2: { value: new THREE.Vector4(1, 1, 0.5, 0) }, |
| +добавлено | uBeamSrc: { value: new THREE.Vector3() }, uBeamDir: { value: new THREE.Vector3(0, -1, 0) }, |
| +добавлено | uBeamCore: { value: coreV }, uBeamGlow: { value: glowV }, uBeamSeed: { value: 0 }, |
| +добавлено | }); |
| +добавлено | const kernels = [], sheaths = []; |
| +добавлено | for (let i = 0; i < 2; i++) { |
| +добавлено | const ke = fx.volumes.add({ |
| +добавлено | shape: 'capsule', glsl: KERNEL_GLSL, maxSteps: 16, |
| +добавлено | uniforms: { ...beamU(), uOccA: { value: [0, 1, 2, 3].map(() => new THREE.Vector4()) }, uOccB: { value: [0, 1, 2, 3].map(() => new THREE.Vector4()) }, uOccN: { value: 0 } }, |
| +добавлено | }); |
| +добавлено | const sh = fx.volumes.add({ shape: 'capsule', glsl: SHEATH_GLSL, maxSteps: 12, uniforms: beamU() }); |
| +добавлено | ke.busy = sh.busy = false; |
| +добавлено | kernels.push(ke); sheaths.push(sh); |
| +добавлено | } |
| +добавлено | const threads = new fx.AnalyticRibbons({ max: 8, segments: 48, pathGLSL: THREAD_PATH, shadeGLSL: THREAD_SHADE, target: 'lines' }); |
| +добавлено | fx.scenes.lines.add(threads.mesh); |
| +добавлено | const holes = [new THREE.Vector4(), new THREE.Vector4()]; |
| +добавлено | const deckU = { |
| +добавлено | uDeck: { value: new THREE.Vector4(DECK_Y, DECK_H, 0, 0) }, uDeck2: { value: new THREE.Vector4(0, 0.37, fx.Q.mobile ? 5 : 9, 0) }, uDeckC: { value: new THREE.Vector2() }, |
| +добавлено | uHole: { value: holes }, uDeckLit: { value: deckLit }, uDeckHot: { value: deckHot }, uDeckDark: { value: new THREE.Vector3() }, |
| +добавлено | }; |
| +добавлено | const deck = new THREE.Mesh(new THREE.PlaneGeometry(2, 2).rotateX(Math.PI / 2), new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: DECK_VERT, fragmentShader: DECK_FRAG, uniforms: { ...fx.U, ...deckU }, ...FX_BLENDING, side: THREE.DoubleSide, |
| +добавлено | })); |
| +добавлено | deck.position.set(0, DECK_Y, 0); deck.scale.set(170, 1, 170); |
| +добавлено | deck.frustumCulled = false; deck.visible = false; |
| +добавлено | fx.scenes.fx.add(deck); |
| +добавлено | // under the deck the floor and the pool stop mirroring the bright sky (uEnvScale, stormApply) |
| +добавлено | const holeBusy = [false, false]; |
| +добавлено | let cover = 0, front = 0, dim = 0, spin = 0; // this frame's strongest cast, written by update() |
| +добавлено | // the deck takes the sun and most of the sky light; written back as the storm clears. Whoever |
| +добавлено | // sets the sun while it's dimmed (a time-of-day change) wins: the new values become the base. |
| +добавлено | const sunBase = new THREE.Vector3(), ambBase = new THREE.Vector3(), sunW = new THREE.Vector3(), ambW = new THREE.Vector3(); |
| +добавлено | let dimmed = false; |
| +добавлено | // x: how far the sun and sky light are taken; cv: how much sky the deck hides (the floor mirrors |
| +добавлено | // the deck from the moment it is overhead until it has gone, longer than the sun stays out) |
| +добавлено | function stormApply(x, cv) { |
| +добавлено | const Su = fx.U.uSunColor.value, Am = fx.U.uAmbient.value, e = Math.max(x, 0.9 * cv); |
| +добавлено | if (!dimmed) { if (e <= 0) return; sunBase.copy(Su); ambBase.copy(Am); dimmed = true; } |
| +добавлено | else if (!Su.equals(sunW) || !Am.equals(ambW)) { sunBase.copy(Su); ambBase.copy(Am); } |
| +добавлено | Su.copy(sunBase).multiplyScalar(1 - 0.9 * x); Am.copy(ambBase).multiplyScalar(1 - 0.72 * x); |
| +добавлено | sunW.copy(Su); ambW.copy(Am); |
| +добавлено | // what the floor mirrors is the deck's dark underside now, not the sky |
| +добавлено | arenaUniforms.uEnvScale.value = 1 - 0.86 * e; |
| +добавлено | if (e <= 0) dimmed = false; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // per cast record (cast.js keeps 16): no allocation per cast |
| +добавлено | const ST = Array.from({ length: 16 }, () => ({ |
| +добавлено | src: new THREE.Vector3(), end: new THREE.Vector3(), g: new THREE.Vector3(), dir: new THREE.Vector3(), |
| +добавлено | vol: null, core: null, sigil: null, light: null, vortex: null, holy: null, |
| +добавлено | sheath: null, fill: null, blast: null, thr: [null, null, null, null], hole: -1, blastR: 0, |
| +добавлено | R: 0, R1: 0, L: 0, splatT: 0, hitT: 0, rot: 0, |
| +добавлено | })); |
| +добавлено | |
| +добавлено | // place a beam handle along src -> end (contact), radius R; cull occluders crossing it |
| +добавлено | function placeBeam(v, src, end, R) { |
| +добавлено | _a.subVectors(end, src); |
| +добавлено | const L = Math.max(_a.length(), 1e-3); |
| +добавлено | _a.divideScalar(L); |
| +добавлено | v.quaternion.setFromUnitVectors(_up, _a); |
| +добавлено | v.position.copy(src).addScaledVector(_a, 0.5 * L); |
| +добавлено | v.scale.set(R * HALO, 0.5 * L + R * HALO, R * HALO); |
| +добавлено | const u = v.uniforms; |
| +добавлено | u.uBeamSrc.value.copy(src); |
| +добавлено | u.uBeamDir.value.copy(_a); |
| +добавлено | u.uBeam2.value.x = R; u.uBeam2.value.y = L; |
| +добавлено | if (!u.uOccN) return; |
| +добавлено | let n = 0; |
| +добавлено | for (const o of fx.occluders.list) { |
| +добавлено | if (n >= 4) break; |
| +добавлено | if (segSeg(src, end, o.a, o.b) > R * HALO + o.radius + 0.1) continue; |
| +добавлено | u.uOccA.value[n].set(o.a.x, o.a.y, o.a.z, o.radius); |
| +добавлено | u.uOccB.value[n].set(o.b.x, o.b.y, o.b.z, 0); |
| +добавлено | n++; |
| +добавлено | } |
| +добавлено | u.uOccN.value = n; |
| +добавлено | } |
| +добавлено | function coreRibbon(h, src, end, width, intensity, fade, head) { |
| +добавлено | if (!h) return; |
| +добавлено | h.p0.set(src.x, src.y, src.z, width); |
| +добавлено | h.p1.set(end.x, end.y, end.z, 0); |
| +добавлено | h.p2.x = (h.index * 7.31) % 6.28; h.p2.y = src.distanceTo(end); h.p2.z = fade; h.p2.w = head; |
| +добавлено | h.color.copy(coreCol); |
| +добавлено | // the strip degenerates seen end-on: fade it there |
| +добавлено | _c.subVectors(end, src).normalize(); |
| +добавлено | const al = Math.abs(_c.dot(_b.subVectors(src, fx.camera.position).normalize())); |
| +добавлено | h.intensity = intensity * (1 - al ** 6); |
| +добавлено | } |
| +добавлено | function splat(x, z, radius, glow, pattern, seed, heat = 0, char = 0, crack = 0) { |
| +добавлено | SPLAT.x = x; SPLAT.z = z; SPLAT.radius = radius; SPLAT.glow = glow; SPLAT.pattern = pattern; SPLAT.seed = seed % 100; |
| +добавлено | SPLAT.heat = heat; SPLAT.char = char; SPLAT.crack = crack; |
| +добавлено | fx.ground.splat(SPLAT); |
| +добавлено | } |
| +добавлено | const focus = (k) => fx.arena.setFocus(fx.arena.focus.color, 2.5 + k); |
| +добавлено | // the point on the pillar's axis at height y |
| +добавлено | const axisAt = (d, y, out) => out.copy(d.g).lerp(d.src, (y - d.g.y) / (d.src.y - d.g.y)); |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'core' || k === 'color') colours(S.params); }, |
| +добавлено | tick(dt) { |
| +добавлено | cores.update(); threads.update(); |
| +добавлено | // the deck follows the strongest live Judgment; nothing to draw once it has cleared |
| +добавлено | const camY = fx.camera.position.y, seen = cover * (1 - sstep(camY, DECK_Y - 3, DECK_Y - 1)); |
| +добавлено | deck.visible = seen > 0.002; |
| +добавлено | if (deck.visible) { |
| +добавлено | spin += dt * (0.14 + 0.45 * dim); |
| +добавлено | deckU.uDeck.value.z = seen; deckU.uDeck.value.w = front; |
| +добавлено | deckU.uDeck2.value.x = spin; deckU.uDeck2.value.y = 0.37 + spin * 0.012; |
| +добавлено | const q = deckU.uDeckDark.value, Am = fx.U.uAmbient.value; |
| +добавлено | q.set(Am.x * 0.45 + 0.012, Am.y * 0.5 + 0.014, Am.z * 0.6 + 0.02); |
| +добавлено | } |
| +добавлено | stormApply(dim, seen); |
| +добавлено | cover = 0; dim = 0; front = 0; |
| +добавлено | }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = ST[c.index], J = c.form !== 'Radiant Lance'; |
| +добавлено | colours(p); |
| +добавлено | if (name === 'charge') { |
| +добавлено | const T = c.durations; |
| +добавлено | if (J) { T.charge = 1.3; T.impact = 0.55; T.linger = 3.4; } else { T.charge = 0.45; T.impact = 0.3; T.linger = 1.4; } |
| +добавлено | d.R = 0; d.splatT = 0; d.hitT = 0; d.rot = (c.seed % 628) / 100; d.holy = null; d.vortex = null; |
| +добавлено | d.vol = d.sheath = d.core = d.fill = d.blast = null; d.hole = -1; |
| +добавлено | if (J) { |
| +добавлено | for (let i = 0; i < kernels.length; i++) if (!kernels[i].busy) { |
| +добавлено | d.vol = kernels[i]; d.sheath = sheaths[i]; d.vol.busy = d.sheath.busy = true; d.vol.visible = d.sheath.visible = false; |
| +добавлено | d.vol.uniforms.uBeamSeed.value = (c.seed % 997) / 97; d.sheath.uniforms.uBeamSeed.value = (c.seed % 389) / 41; |
| +добавлено | break; |
| +добавлено | } |
| +добавлено | for (let j = 0; j < 4; j++) { const h = d.thr[j] = threads.acquire(); if (h) h.intensity = 0; } |
| +добавлено | for (let i = 0; i < 2; i++) if (!holeBusy[i]) { holeBusy[i] = true; d.hole = i; holes[i].set(0, 0, 0, 0); break; } |
| +добавлено | d.fill = fx.lights.acquire(); |
| +добавлено | if (d.fill) { d.fill.color.setRGB(1, 0.42, 0.12); d.fill.intensity = 0; d.fill.range = 13; } |
| +добавлено | } else { |
| +добавлено | for (const v of beams) if (!v.busy) { d.vol = v; break; } |
| +добавлено | if (d.vol) { d.vol.busy = true; d.vol.visible = false; d.vol.uniforms.uBeamSeed.value = (c.seed % 997) / 97; } |
| +добавлено | d.core = cores.acquire(); |
| +добавлено | if (d.core) d.core.intensity = 0; |
| +добавлено | } |
| +добавлено | d.light = fx.lights.acquire(); |
| +добавлено | if (d.light) { d.light.color.copy(lightCol); d.light.intensity = 0; d.light.shadow = true; } |
| +добавлено | if (J) { |
| +добавлено | // ground point under the target, and a sky source tilted sideways to the cast direction |
| +добавлено | const pool = ARENA.pool; |
| +добавлено | const inPool = Math.abs(c.to.x - pool.center.x) < pool.half.x && Math.abs(c.to.z - pool.center.y) < pool.half.y; |
| +добавлено | d.g.set(c.to.x, inPool ? pool.water : 0, c.to.z); |
| +добавлено | // the source leans away from the caster, a little sideways: the hole stays in frame |
| +добавлено | const side = (c.seed & 1) ? 1 : -1, tan = Math.tan(THREE.MathUtils.degToRad(p.tilt)) * SKY; |
| +добавлено | _a.set(-c.dir.z, 0, c.dir.x).normalize().multiplyScalar(side * tan * 0.35); |
| +добавлено | d.src.set(d.g.x + _a.x + c.dir.x * tan, d.g.y + SKY, d.g.z + _a.z + c.dir.z * tan); |
| +добавлено | d.dir.subVectors(d.g, d.src).normalize(); |
| +добавлено | d.end.copy(d.g); |
| +добавлено | d.L = d.src.distanceTo(d.g); |
| +добавлено | d.R1 = 0.5 * p.width; |
| +добавлено | _a.copy(d.g); _b.set(0, 1, 0); |
| +добавлено | SIGIL.radius = p.sigil; SIGIL.seed = c.seed % 1000; SIGIL.rotation = d.rot; |
| +добавлено | d.sigil = fx.sigils.acquire(SIGIL); |
| +добавлено | if (d.sigil) { d.sigil.reveal = 0; d.sigil.intensity = 0; } |
| +добавлено | if (d.light) { d.light.type = 2; d.light.range = 11; d.light.pos.copy(d.g).setY(d.g.y + 2.8); d.light.cookie[0] = 2; d.light.cookie[1] = 1 / p.sigil; d.light.cookie[2] = -d.rot; } |
| +добавлено | } else { |
| +добавлено | d.src.copy(c.from); |
| +добавлено | d.dir.subVectors(c.to, c.from).normalize(); |
| +добавлено | _a.copy(c.from).addScaledVector(d.dir, 0.32); _b.copy(d.dir); |
| +добавлено | SIGIL.radius = 0.5; SIGIL.seed = c.seed % 1000; SIGIL.rotation = d.rot; |
| +добавлено | d.sigil = fx.sigils.acquire(SIGIL); |
| +добавлено | if (d.sigil) { d.sigil.reveal = 0; d.sigil.intensity = 0; } |
| +добавлено | if (d.light) { d.light.type = 0; d.light.range = 6; d.light.pos.copy(c.from); } |
| +добавлено | } |
| +добавлено | } else if (name === 'travel') { |
| +добавлено | // the lance lands on the first surface between the focus and the target |
| +добавлено | const dist = c.from.distanceTo(c.to); |
| +добавлено | const hit = arenaRaycast(c.from, d.dir, dist + 0.6); |
| +добавлено | if (hit > 0.3) d.end.copy(c.from).addScaledVector(d.dir, hit); else d.end.copy(c.to); |
| +добавлено | d.L = c.from.distanceTo(d.end); |
| +добавлено | d.R1 = 0.5 * p.lance; |
| +добавлено | if (d.light) { d.light.type = 1; d.light.range = 9; d.light.pos.copy(c.from); d.light.end.copy(d.end); } |
| +добавлено | d.holy = fx.impacts.add('holy', d.end, 1.1, 1.2 * p.glow, 1.2, lightCol); |
| +добавлено | fx.flash(0.12 * p.intensity); |
| +добавлено | fx.rig.shake(0.15, 16); |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | if (J) slam(c, p, d); |
| +добавлено | else { |
| +добавлено | // the lance lets go: a last burst where it landed |
| +добавлено | spark.pos.copy(d.end); spark.vel.set(0, 1.5, 0); spark.burst(260 * p.motes); |
| +добавлено | mote.pos.copy(d.end); mote.radius = 0.3; mote.vel.set(0, 2.2, 0); mote.burst(200 * p.motes); |
| +добавлено | splat(d.end.x, d.end.z, 1.4, 0.8 * p.glow, 'ring', c.seed + 3); |
| +добавлено | fx.rig.shake(0.18, 14); |
| +добавлено | } |
| +добавлено | } else if (name === 'done') { |
| +добавлено | if (d.vol) { d.vol.visible = false; d.vol.busy = false; } |
| +добавлено | if (d.sheath) { d.sheath.visible = false; d.sheath.busy = false; } |
| +добавлено | for (let j = 0; j < 4; j++) { d.thr[j]?.release(); d.thr[j] = null; } |
| +добавлено | if (d.hole >= 0) { holeBusy[d.hole] = false; holes[d.hole].set(0, 0, 0, 0); d.hole = -1; } |
| +добавлено | d.core?.release(); d.sigil?.release(); d.light?.release(); d.vortex?.release(); d.fill?.release(); d.blast?.release(); |
| +добавлено | d.vol = d.sheath = d.core = d.sigil = d.light = d.vortex = d.holy = d.fill = d.blast = null; |
| +добавлено | focus(0); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = ST[c.index], v = d.vol, L = d.light; |
| +добавлено | colours(p); |
| +добавлено | d.rot += dt * (c.form === 'Radiant Lance' ? 2.6 : 0.35); |
| +добавлено | if (d.sigil) d.sigil.rotation = d.rot; |
| +добавлено | if (c.form === 'Radiant Lance') return lance(c, p, d, dt); |
| +добавлено | |
| +добавлено | const base = 4.4 * p.intensity, F = d.fill, st = p.storm; |
| +добавлено | let Rk = 0, Rs = 0, Ik = 0, Is = 0, head = d.L + 5, thr = 0, thrW = 0.035, holeR = 0, holeI = 0, cov = 0, fr = 200, dm = 0; |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge; |
| +добавлено | if (d.sigil) { d.sigil.reveal = sstep(k, 0, 0.85); d.sigil.intensity = (2 + 4 * k) * p.intensity; } |
| +добавлено | if (L) { L.intensity = 30 * k * k * p.intensity; L.cookie[2] = -d.rot; } |
| +добавлено | ringMote.pos.copy(d.g).setY(d.g.y + 0.05); ringMote.radius = p.sigil * 0.92; ringMote.vel.set(0, 0.7, 0); |
| +добавлено | ringMote.rate(450 * k * p.motes, dt); |
| +добавлено | // the air pulled in under it: dust creeping towards the sigil |
| +добавлено | dust.pos.copy(d.g).setY(d.g.y + 0.1); dust.radius = p.sigil * 2.2; dust.radial = -2.6; dust.vel.set(0, 0.3, 0); dust.size = 0.45; |
| +добавлено | dust.rate(40 * k * p.dust, dt); |
| +добавлено | // the storm races out from over the target; the sun goes; the hole tears open and burns |
| +добавлено | cov = st; fr = 10 + 170 * k * k; dm = st * sstep(k, 0.05, 0.8); |
| +добавлено | const x = clamp((k - 0.4) / 0.6, 0, 1); |
| +добавлено | holeR = 3.4 * x * x; holeI = 9 * x * x * x * p.intensity; |
| +добавлено | // the pilot ray: 0.24 m wide, descending through the hole over the last 40% |
| +добавлено | if (k > 0.6) { |
| +добавлено | const y = (k - 0.6) / 0.4; |
| +добавлено | Rk = 0.12; Ik = base * 3.5; head = d.L * y * y + 0.6; thr = 26 * y * p.intensity; thrW = 0.02; |
| +добавлено | } |
| +добавлено | focus(4 * k); |
| +добавлено | } else if (c.phase === 'impact') { |
| +добавлено | const t = c.t; |
| +добавлено | Rk = Math.max(0.12, d.R1 * easeOutBack(Math.min(t / 0.12, 1))); |
| +добавлено | Rs = d.R1 * 3.1 * (1 - Math.exp(-t * 14)); |
| +добавлено | Ik = base * (1 + 2.0 * Math.exp(-t / 0.07)); |
| +добавлено | Is = 0.42 * p.intensity * (1 + 1.3 * Math.exp(-t / 0.14)); |
| +добавлено | thr = 16 * p.intensity * (1 + 1.5 * Math.exp(-t / 0.07)); |
| +добавлено | cov = st; dm = st; holeR = 3.4 + 4 * (1 - Math.exp(-t * 6)); holeI = 9 * p.intensity * (1 + 2 * Math.exp(-t / 0.1)); |
| +добавлено | if (L) { L.intensity = 3000 * p.intensity * (1 + 1.2 * Math.exp(-t / 0.09)); } |
| +добавлено | if (F) F.intensity = 70 * p.molten * (1 + Math.exp(-t / 0.2)); |
| +добавлено | if (d.sigil) d.sigil.intensity = 7 * p.intensity * Math.exp(-t / 0.15) + 1.5 * p.intensity; |
| +добавлено | if (d.holy) d.holy.age = 0; |
| +добавлено | beamParticles(d, p, Rs, dt, 1); |
| +добавлено | focus(4); |
| +добавлено | } else { |
| +добавлено | // linger: the shaft holds, collapses to a thread and goes; the hole closes, the storm clears |
| +добавлено | const t = c.t, Lg = c.durations.linger, y = clamp((t - 1.0) / 0.5, 0, 1), q = (1 - y) * (1 - y); |
| +добавлено | Rk = y < 1 ? THREE.MathUtils.lerp(d.R1, 0.04, y ** 1.5) : 0; |
| +добавлено | Rs = d.R1 * 3.1 * (1 - 0.65 * y); |
| +добавлено | Ik = base * q; Is = 0.42 * p.intensity * q; thr = 16 * p.intensity * q; |
| +добавлено | holeR = Math.max(0, 7.4 * (1 - sstep(t, 0.9, 2.1))); holeI = 9 * p.intensity * q; |
| +добавлено | cov = st * (1 - sstep(t, 1.5, Lg)); dm = st * (1 - sstep(t, 1.1, Lg - 0.8)); |
| +добавлено | if (L) L.intensity = 3000 * p.intensity * q + 14 * p.intensity * Math.max(0, 1 - t / 2); |
| +добавлено | if (F) F.intensity = 70 * p.molten * Math.exp(-t / 1.1); |
| +добавлено | if (d.sigil) { d.sigil.intensity = 1.5 * p.intensity * (1 - t / Lg); d.sigil.reveal = 1 - sstep(t / Lg, 0.3, 0.8); } |
| +добавлено | if (d.vortex && t > 1.6) { d.vortex.release(); d.vortex = null; } |
| +добавлено | beamParticles(d, p, Math.max(Rs, 0.3), dt, q); |
| +добавлено | focus(4 * (1 - y)); |
| +добавлено | } |
| +добавлено | if (d.blast) { |
| +добавлено | d.blastR += dt * 21; |
| +добавлено | d.blast.p[0] = d.blastR; d.blast.p[2] = 130 * Math.max(0, 1 - d.blastR / 17); |
| +добавлено | if (d.blastR > 17) { d.blast.release(); d.blast = null; } |
| +добавлено | } |
| +добавлено | // the deck |
| +добавлено | cover = Math.max(cover, cov); front = Math.max(front, fr); dim = Math.max(dim, dm); |
| +добавлено | if (d.hole >= 0) { |
| +добавлено | axisAt(d, DECK_Y + 0.4 * DECK_H, _a); |
| +добавлено | holes[d.hole].set(_a.x, _a.z, holeR, holeI); |
| +добавлено | if (d.hole === 0 || !holeBusy[0]) deckU.uDeckC.value.set(_a.x, _a.z); |
| +добавлено | } |
| +добавлено | // the shaft's volumes and threads |
| +добавлено | const ke = d.vol, sh = d.sheath; |
| +добавлено | if (ke) { |
| +добавлено | ke.visible = Rk > 0.01 && Ik > 0.01; |
| +добавлено | if (ke.visible) { |
| +добавлено | placeBeam(ke, d.src, d.g, Rk); |
| +добавлено | ke.uniforms.uBeam.value.set(Ik, p.rays, p.scatter, head); |
| +добавлено | ke.uniforms.uBeam2.value.w = 0.18; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | if (sh) { |
| +добавлено | sh.visible = Rs > 0.05 && Is > 0.005; |
| +добавлено | if (sh.visible) { |
| +добавлено | placeBeam(sh, d.src, d.g, Rs / HALO); |
| +добавлено | sh.uniforms.uBeam.value.set(Is, p.rays, p.scatter, head); |
| +добавлено | sh.uniforms.uBeam2.value.z = c.phase === 'charge' ? 0 : clamp(p.dust, 0, 2); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | for (let j = 0; j < 4; j++) { |
| +добавлено | const h = d.thr[j]; |
| +добавлено | if (!h) continue; |
| +добавлено | h.intensity = thr * (0.6 + 0.4 * ((j * 2.3) % 1)); |
| +добавлено | if (thr <= 0.01) continue; |
| +добавлено | h.p0.set(d.src.x, d.src.y, d.src.z, thrW * (j === 0 ? 1.3 : 1)); |
| +добавлено | h.p1.set(d.g.x, d.g.y, d.g.z, ((c.seed % 83) * 0.137 + j * 0.31) % 1); |
| +добавлено | h.p2.set(Math.max(Rk, 0.05) * (0.16 + 0.11 * j), (j & 1 ? -1 : 1) * (0.5 + 0.13 * j), 0, head); |
| +добавлено | h.p3.set(1.2 + j * 0.4, d.L, 0, 0); |
| +добавлено | h.color.copy(coreCol); |
| +добавлено | } |
| +добавлено | if (c.phase !== 'charge' && Rk > 0.2) { |
| +добавлено | d.splatT -= dt; |
| +добавлено | if (d.splatT <= 0) { |
| +добавлено | d.splatT = 0.2; |
| +добавлено | const m = p.molten; |
| +добавлено | splat(d.g.x, d.g.z, Rk * 2.2, 0.3 * p.glow, 'noise', c.seed + c.age * 10, 0.8 * m, 0.3 * m, 0); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | // Judgment lands: the pillar slams open (the rest animates in update) |
| +добавлено | function slam(c, p, d) { |
| +добавлено | const g = d.g, R1 = d.R1, m = p.molten; |
| +добавлено | fx.flash(0.5 * p.intensity); |
| +добавлено | fx.rig.shake(0.6, 13); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(2.6, 0.08); fx.rig.hitstop(0.05); } |
| +добавлено | // the floor: fused and fractured under the contact, a wide scorch and glow round it |
| +добавлено | splat(g.x, g.z, R1 * 2.4, 0.9 * p.glow, 'radial', c.seed, 1.8 * m, 0.9 * m, 0); |
| +добавлено | splat(g.x, g.z, R1 * 3.6, 0.3 * p.glow, 'noise', c.seed + 7, 1.0 * m, 0.8 * m, 0); |
| +добавлено | splat(g.x, g.z, R1 * 2.0, 0, 'lichtenberg', c.seed + 3, 0, 0.2, 3.2 * m); |
| +добавлено | splat(g.x, g.z, p.sigil * 1.05, 1.2 * p.glow, 'ring', c.seed + 1); |
| +добавлено | d.holy = fx.impacts.add('holy', _a.copy(g).setY(g.y - 1), 2.4, 0.5 * p.glow, 3.5, lightCol); |
| +добавлено | // the shock: one along the floor out to the wall, one in the air |
| +добавлено | _c.copy(g).setY(g.y + 0.02); SHOCK.radius0 = R1 * 0.6; SHOCK.radius1 = 15; SHOCK.duration = 0.75; SHOCK.thickness = 0.9; SHOCK.distortion = 0.05; SHOCK.intensity = 2.6 * p.intensity; SHOCK.ground = true; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | _c.setY(g.y + 1.4); SHOCK.radius0 = R1; SHOCK.radius1 = 9; SHOCK.duration = 0.4; SHOCK.thickness = 1.1; SHOCK.distortion = 0.1; SHOCK.intensity = 0; SHOCK.ground = false; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | d.blast = fx.forces.acquire(); |
| +добавлено | if (d.blast) { d.blast.type = 4; d.blast.pos.copy(g).setY(g.y + 0.3); d.blast.p[0] = R1; d.blast.p[1] = 2.2; d.blast.p[2] = 130; d.blast.p[3] = 0; d.blastR = R1; } |
| +добавлено | // the dust ring: a fast thin front and a slower heavy bank, both lit from inside by the shaft |
| +добавлено | dust.pos.copy(g).setY(g.y + 0.12); dust.radius = R1 * 1.25; dust.vel.set(0, 0.5, 0); |
| +добавлено | dust.radial = 13; dust.size = 1; dust.burst(150 * p.dust); |
| +добавлено | dust.radial = 6; dust.size = 1.3; dust.burst(70 * p.dust); |
| +добавлено | rock.pos.copy(g).setY(g.y + 0.1); rock.radius = R1 * 1.2; rock.vel.set(0, 5.5, 0); rock.burst(44 * m); |
| +добавлено | molten.pos.copy(g).setY(g.y + 0.06); molten.radius = R1 * 0.9; molten.vel.set(0, 3.2, 0); molten.burst(420 * m); |
| +добавлено | spark.pos.copy(g).setY(g.y + 0.1); spark.radius = R1 * 0.5; spark.vel.set(0, 3, 0); spark.burst(300 * p.motes); |
| +добавлено | mote.pos.copy(g).setY(g.y + 0.05); mote.radius = R1; mote.vel.set(0, 3.5, 0); mote.spread = 1.6; mote.radial = 1.2; mote.burst(400 * p.motes); |
| +добавлено | mote.spread = 0.6; mote.radial = 0; |
| +добавлено | feather.pos.copy(axisAt(d, g.y + 2, _a)); feather.p.copy(axisAt(d, g.y + 9, _b)); feather.radius = R1 * 0.8; feather.vel.set(0, -0.2, 0); |
| +добавлено | feather.burst(16 * p.feathers); |
| +добавлено | fx.pool?.ripple?.(g.x, g.z, 0.05, 2); |
| +добавлено | // the light stops being a rune projector and hits the whole arena from the axis, hard |
| +добавлено | if (d.light) { d.light.type = 0; d.light.cookie[0] = 0; d.light.range = 44; axisAt(d, g.y + 11, d.light.pos); d.light.color.copy(lightCol); } |
| +добавлено | if (d.fill) d.fill.pos.copy(g).setY(g.y + 0.9); |
| +добавлено | d.vortex = fx.forces.acquire(); |
| +добавлено | if (d.vortex) { d.vortex.type = 1; d.vortex.w = 9; d.vortex.p[0] = R1 * 0.7; d.vortex.p[1] = 3.5; d.vortex.p[2] = 1.4; d.vortex.p[3] = 0.4; d.vortex.pos.copy(g); d.vortex.dir.set(0, 1, 0); } |
| +добавлено | // smite + cleanse whatever stands in or near it |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dist = Math.hypot(dm.pos.x - g.x, dm.pos.z - g.z); |
| +добавлено | if (dist > R1 + 1.2) continue; |
| +добавлено | dm.status.corrupt = 0; |
| +добавлено | dm.hit(dist > 0.05 ? _a.subVectors(dm.pos, g) : c.dir, 0.35 * (1 - dist / (R1 + 1.2)) + 0.15); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // motes rising through the pillar, feathers tumbling down it |
| +добавлено | function beamParticles(d, p, R, dt, k) { |
| +добавлено | if (k <= 0) return; |
| +добавлено | mote.pos.copy(d.g).setY(d.g.y + 0.05); mote.radius = R * 0.28; mote.vel.set(0, 1.2, 0); |
| +добавлено | mote.rate(420 * p.motes * k, dt); |
| +добавлено | feather.pos.copy(axisAt(d, d.g.y + 2, _a)); feather.p.copy(axisAt(d, d.g.y + 9, _b)); feather.radius = R * 0.25; feather.vel.set(0, -0.3, 0); |
| +добавлено | feather.rate(45 * p.feathers * k, dt); |
| +добавлено | // dust glinting in the shaft |
| +добавлено | fleck.pos.copy(axisAt(d, d.g.y + 0.5, _a)); fleck.p.copy(axisAt(d, d.g.y + 11, _b)); fleck.radius = R * 0.62; fleck.vel.set(0, -0.12, 0); |
| +добавлено | fleck.rate(1300 * p.dust * k, dt); |
| +добавлено | dust.pos.copy(d.g).setY(d.g.y + 0.1); dust.radius = Math.max(R * 0.32, 0.4); dust.radial = 3; dust.vel.set(0, 0.8, 0); dust.size = 0.8; |
| +добавлено | dust.rate(34 * p.dust * k, dt); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function lance(c, p, d, dt) { |
| +добавлено | const v = d.vol, L = d.light, base = 30 * p.intensity; |
| +добавлено | let R = 0, I = 0, core = 0, head = d.L + 1; |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge; |
| +добавлено | if (d.sigil) { d.sigil.reveal = k ** 0.7; d.sigil.intensity = 5 * k * p.intensity; } |
| +добавлено | gather.pos.copy(c.from); gather.rate(900 * k * p.motes, dt); |
| +добавлено | if (L) L.intensity = 14 * k * p.intensity; |
| +добавлено | focus(5 * k); |
| +добавлено | } else if (c.phase === 'travel') { |
| +добавлено | const t = c.t; |
| +добавлено | head = Math.min(d.L + 1, t * 160); |
| +добавлено | R = d.R1 * (1 + 0.05 * Math.sin(c.age * 47) * Math.sin(c.age * 13)); |
| +добавлено | I = base * (1 + 1.2 * Math.exp(-t / 0.08)); |
| +добавлено | core = 16 * p.intensity * (1 + Math.exp(-t / 0.08)); |
| +добавлено | if (d.sigil) { d.sigil.reveal = 1; d.sigil.intensity = 7 * p.intensity; } |
| +добавлено | if (L) L.intensity = 45 * p.intensity * (1 + Math.exp(-t / 0.1)) * (0.92 + 0.08 * Math.sin(c.age * 31)); |
| +добавлено | if (d.holy) d.holy.age = 0; |
| +добавлено | if (head >= d.L) { |
| +добавлено | stream.pos.copy(c.from); stream.p.copy(d.end); stream.vel.copy(d.dir).multiplyScalar(9); stream.rate(1500 * p.motes, dt); |
| +добавлено | spark.pos.copy(d.end); spark.radius = 0.15; spark.vel.copy(d.dir).multiplyScalar(-1.5); spark.rate(350 * p.motes, dt); |
| +добавлено | mote.pos.copy(d.end); mote.radius = 0.25; mote.vel.set(0, 1.4, 0); mote.rate(250 * p.motes, dt); |
| +добавлено | d.splatT -= dt; |
| +добавлено | if (d.splatT <= 0) { d.splatT = 0.15; splat(d.end.x, d.end.z, d.end.y < 0.4 ? 1.1 : 0.8, 0.3 * p.glow, 'radial', c.seed + c.age * 10); } |
| +добавлено | d.hitT -= dt; |
| +добавлено | if (d.hitT <= 0 && c.target >= 0) { |
| +добавлено | d.hitT = 0.35; |
| +добавлено | const dm = fx.dummies[c.target]; |
| +добавлено | if (dm && dm.pos.distanceTo(_a.copy(d.end).setY(0)) < 1.2) { dm.status.corrupt = 0; dm.hit(d.dir, 0.12); } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | focus(5); |
| +добавлено | } else { |
| +добавлено | const t = c.t, y = c.phase === 'impact' ? clamp(t / c.durations.impact, 0, 1) : 1; |
| +добавлено | R = y < 1 ? d.R1 * (1 - y) ** 2 + 0.01 : 0; |
| +добавлено | I = base * (1 - y) ** 2; |
| +добавлено | core = 16 * p.intensity * (1 - y) ** 3; |
| +добавлено | if (L) L.intensity *= Math.exp(-dt / 0.12); |
| +добавлено | if (d.sigil) { d.sigil.intensity = 7 * p.intensity * (1 - y * 0.5); d.sigil.reveal = c.phase === 'linger' ? 1 - sstep(t / c.durations.linger, 0, 0.7) : 1; } |
| +добавлено | focus(5 * (1 - y)); |
| +добавлено | } |
| +добавлено | if (v) { |
| +добавлено | v.visible = R > 0.01 && I > 0.01; |
| +добавлено | if (v.visible) { |
| +добавлено | placeBeam(v, c.from, d.end, R); |
| +добавлено | v.uniforms.uBeam.value.set(I, p.rays * 0.6, p.scatter, head); |
| +добавлено | v.uniforms.uBeam2.value.z = Math.min(0.5, 0.35 / d.L); v.uniforms.uBeam2.value.w = 1.6; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | coreRibbon(d.core, c.from, d.end, 0.2 * p.lance, core, Math.min(0.5, 0.2 / d.L), head); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { U, COMMON_GLSL, FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL, FX_BLENDING } from '../../vfx/common.js'; |
| +добавлено | import { instanced } from '../../vfx/Ribbons.js'; |
| +добавлено | |
| +добавлено | // Aurora Veil (Light + Arcane hybrid). A holy circle draws itself under the target and motes rise |
| +добавлено | // from it along the field lines; high above, a faint quiet arc of aurora forms. Then the substorm |
| +добавлено | // breaks: the arc brightens, folds deepen and travel along it, rays race sideways, more curtains |
| +добавлено | // unroll from the middle out, and a magenta fringe flares along the razor-sharp lower border. |
| +добавлено | // Each curtain is one instanced strip (fx pass, half-res emission only): the vertex shader folds a |
| +добавлено | // vertical sheet with three travelling waves (the top lags a little, like drapery); the fragment |
| +добавлено | // shader builds vertical rays from drifting noise along the sheet, a sharp bright lower border |
| +добавлено | // with a thin pink fringe, a green body fading upward into magenta / purple tops, and brightness |
| +добавлено | // ~ 1 / |n.v| so the folds seen edge-on blaze. Under them the light is what real aurora gives a |
| +добавлено | // snowfield or a lake (timelapse reference): faint, soft, slowly wandering. A green line light along |
| +добавлено | // every lower border (one shadowed), each sliding along its curtain with the brightest folds and |
| +добавлено | // breathing with the pulsating patches, and a dim magenta light up high: they light the floor, |
| +добавлено | // pillars and dummies and mirror in the polished floor as soft streaks. Nothing is painted with |
| +добавлено | // glow (it would light the grout lines); the cleansing is a negative corruption splat under each |
| +добавлено | // curtain. Faint green precipitation drifts down; a gentle vortex lifts loose particles along the |
| +добавлено | // "field lines"; the pool lights up; dummies under the veil are cleansed of corruption. |
| +добавлено | |
| +добавлено | const MAX_CASTS = 16, MAX_CURTAINS = 4, MAX_INST = 16, COLS = 112, ROWS = 9; |
| +добавлено | const V0 = -0.15, V1 = 1.2; // strip height range in curtain heights: veil skirt below the border, margin above the tops |
| +добавлено | |
| +добавлено | const CURTAIN_VERT = /* glsl */` |
| +добавлено | in vec4 aP0; // centre xyz, length m |
| +добавлено | in vec4 aP1; // axis xz, lower border y, height m |
| +добавлено | in vec4 aP2; // seed, fold amplitude m, fold clock, ray clock |
| +добавлено | in vec4 aP3; // energy, reveal 0..1, fringe, rays |
| +добавлено | out vec3 vW; |
| +добавлено | out vec3 vN; |
| +добавлено | out vec2 vUV; |
| +добавлено | out float vS; |
| +добавлено | out float vViewZ; |
| +добавлено | out vec4 vP2; |
| +добавлено | out vec4 vP3; |
| +добавлено | // three travelling waves along the sheet: a long S, a medium fold, small curls |
| +добавлено | float fold(float s, float tau, float ph, out float d) { |
| +добавлено | const float k0 = 0.17, k1 = 0.43, k2 = 1.05; |
| +добавлено | float x0 = k0 * s + tau * 0.31 + ph, x1 = k1 * s - tau * 0.83 + ph * 2.3, x2 = k2 * s + tau * 1.7 + ph * 4.1; |
| +добавлено | d = 1.6 * k0 * cos(x0) + 1.25 * k1 * cos(x1) + 0.45 * k2 * cos(x2); |
| +добавлено | return 1.6 * sin(x0) + 1.25 * sin(x1) + 0.45 * sin(x2); |
| +добавлено | } |
| +добавлено | void main() { |
| +добавлено | vUV = position.xy; vP2 = aP2; vP3 = aP3; vW = vec3(0.0); vN = vec3(0.0, 0.0, 1.0); vS = 0.0; vViewZ = 1.0; |
| +добавлено | if (aP3.x <= 0.0 || aP3.y <= 0.0) { gl_Position = vec4(2.0, 2.0, 2.0, 1.0); return; } |
| +добавлено | float u = position.x, v = position.y, L = aP0.w; |
| +добавлено | float s = (u - 0.5) * L; |
| +добавлено | vec3 ax = vec3(aP1.x, 0.0, aP1.y), pp = vec3(-aP1.y, 0.0, aP1.x); |
| +добавлено | // the top lags the bottom a little and swings wider |
| +добавлено | float A = aP2.y * (1.0 + 0.3 * v), df; |
| +добавлено | float f = fold(s, aP2.z - v * 0.8, aP2.x, df) * A; |
| +добавлено | df *= A; |
| +добавлено | // a gentle bow away from the caster at the ends |
| +добавлено | float b = 0.24 * L * (u - 0.5) * (u - 0.5), db = 0.48 * (u - 0.5); |
| +добавлено | vec3 w = aP0.xyz + ax * s + pp * (f + b); |
| +добавлено | // small curls ripple along the lower border |
| +добавлено | w.y = aP1.z + (0.22 * sin(s * 0.9 - aP2.z * 1.7 + aP2.x) + 0.1 * sin(s * 2.7 + aP2.z * 3.1)) * (1.0 - v) + v * aP1.w; |
| +добавлено | vN = normalize(pp - ax * (df + db)); |
| +добавлено | vW = w; vS = s; |
| +добавлено | vec4 view = viewMatrix * vec4(w, 1.0); |
| +добавлено | vViewZ = -view.z; |
| +добавлено | gl_Position = projectionMatrix * view; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const CURTAIN_FRAG = /* glsl */` |
| +добавлено | uniform vec3 uAvBody; |
| +добавлено | uniform vec3 uAvTop; |
| +добавлено | uniform vec3 uAvFringe; |
| +добавлено | uniform float uAvScale; |
| +добавлено | uniform float uAvVeil; |
| +добавлено | in vec3 vW; |
| +добавлено | in vec3 vN; |
| +добавлено | in vec2 vUV; |
| +добавлено | in float vS; |
| +добавлено | in float vViewZ; |
| +добавлено | in vec4 vP2; |
| +добавлено | in vec4 vP3; |
| +добавлено | float vnoise(float x) { float i = floor(x), f = fract(x); f = f * f * (3.0 - 2.0 * f); return mix(hash11(i), hash11(i + 1.0), f); } |
| +добавлено | void main() { |
| +добавлено | float u = vUV.x, v = vUV.y; |
| +добавлено | float E = vP3.x, reveal = vP3.y, fringe = vP3.z, rays = vP3.w, drift = vP2.w; |
| +добавлено | // unrolls from the middle outwards, soft ends |
| +добавлено | float du = abs(u - 0.5) * 2.0; |
| +добавлено | float ends = smoothstep(1.0, 0.6, du) * smoothstep(reveal * 1.2, reveal * 1.2 - 0.3, du); |
| +добавлено | if (ends <= 0.0) discard; |
| +добавлено | // rays: drifting noise along the sheet (metres), sharpened into thin vertical streaks |
| +добавлено | float x = vS + vP2.x * 17.0; |
| +добавлено | float r = vnoise(x * 1.7 + drift) * 0.5 + vnoise(x * 4.3 - drift * 1.7 + 7.0) * 0.33 + vnoise(x * 11.0 + drift * 2.9 + 3.0) * 0.17; |
| +добавлено | r = smoothstep(0.18, 0.82, r) * (0.82 + 0.36 * vnoise(x * 27.0 + drift * 4.0)); // fine striation |
| +добавлено | float sharp = 1.5 + 2.0 * rays; |
| +добавлено | float ray = mix(0.8, pow(r, sharp) * (0.6 + sharp), min(rays, 1.0)); |
| +добавлено | // some rays reach much higher than others |
| +добавлено | float top = 0.4 + 0.6 * vnoise(x * 0.8 + drift * 0.4 + 11.0); |
| +добавлено | // altitude: razor lower border, bright green base falling off upward, magenta-purple tops |
| +добавлено | float border = smoothstep(0.0, 0.025, v); |
| +добавлено | float body = exp(-v * 1.5 / top) * (0.45 + 0.55 * exp(-v * 5.0)); |
| +добавлено | float upper = smoothstep(0.4, 0.75, v) * smoothstep(top + 0.3, top - 0.1, v); |
| +добавлено | // rays carry the green high; the faint red-purple only shows where the green has died away, and |
| +добавлено | // each ray frays out upward at its own height |
| +добавлено | float fray = 0.55 + 0.45 * vnoise(x * 6.0 + v * 9.0 - drift * 3.0); |
| +добавлено | vec3 col = uAvBody * body * (0.2 + 0.8 * ray) + uAvTop * upper * (0.15 + 0.85 * ray) * fray * 0.07; |
| +добавлено | col = col * border + uAvFringe * exp(-v * v * 1800.0) * fringe * (0.5 + ray) * 0.6; |
| +добавлено | // pulsating patches |
| +добавлено | float cell = floor(x * 0.55); |
| +добавлено | float puls = 0.78 + 0.22 * sin(uTime * (1.2 + 2.2 * hash11(cell)) + hash11(cell + 5.0) * 6.283); |
| +добавлено | // a thin glowing sheet: path length through it ~ 1 / |cos|, so folds seen edge-on blaze |
| +добавлено | float nv = abs(dot(vN, normalize(cameraPosition - vW))); |
| +добавлено | float edge = clamp(0.32 / sqrt(nv * nv + 0.03), 0.35, 1.8); |
| +добавлено | float soft = softFade(vViewZ, 0.6); |
| +добавлено | vec3 e = col * (E * ends * puls * edge * uAvScale) * soft; |
| +добавлено | // the veil: a thin dusk-blue dimming of the sky around the curtains so they read in daylight |
| +добавлено | // dimming most where the sheet glows, so it never reads as a dark panel |
| +добавлено | float a = uAvVeil * min(E, 1.0) * ends * smoothstep(-0.15, 0.06, v) * smoothstep(0.95, 0.3, v) * soft * (0.3 + 0.7 * min(body * (0.4 + ray), 1.0)); |
| +добавлено | fxOut(vec3(0.006, 0.01, 0.03), a, e, vec2(0.0), vViewZ); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // module scratch (nothing is allocated after create) |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(); |
| +добавлено | const UP = new THREE.Vector3(0, 1, 0); |
| +добавлено | const sstep = THREE.MathUtils.smoothstep; |
| +добавлено | // per-curtain layout: offset along the cast (m), shift along the curtain (fraction of length), |
| +добавлено | // border altitude offset (m), height and length scale, energy weight |
| +добавлено | const LAYOUT = [ |
| +добавлено | [0, 0, 0, 1, 1, 1], |
| +добавлено | [-2.8, 0.12, 0.6, 0.85, 0.8, 0.7], |
| +добавлено | [2.6, -0.14, 1.2, 0.9, 0.72, 0.8], |
| +добавлено | [-5.4, 0.05, 2.3, 0.8, 0.9, 0.55], |
| +добавлено | ]; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'aurora-veil', name: 'Aurora Veil', element: 'light', order: 20, hue: '#2dff8a', |
| +добавлено | shape: 'area', |
| +добавлено | params: { |
| +добавлено | curtains: 3, length: 18, height: 8, base: 3.4, fold: 1, rays: 1, speed: 1, duration: 5.5, |
| +добавлено | intensity: 1, light: 1, precipitation: 1, veil: 1, |
| +добавлено | color: '#19ff66', top: '#b23cff', fringe: '#ff3fa8', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | curtains: [1, MAX_CURTAINS, 1], length: [8, 26, 0.1], height: [4, 14, 0.1], base: [2.5, 8, 0.05], |
| +добавлено | fold: [0, 2.5, 0.01], rays: [0, 2, 0.01], speed: [0.2, 3, 0.01], duration: [2, 12, 0.1], |
| +добавлено | intensity: [0.1, 3, 0.01], light: [0, 3, 0.01], precipitation: [0, 3, 0.01], veil: [0, 2, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | substorm: { curtains: 4, fold: 1.7, rays: 1.6, speed: 2, intensity: 1.4, light: 1.3 }, |
| +добавлено | 'quiet arc': { curtains: 1, length: 24, fold: 0.45, rays: 0.6, speed: 0.45, duration: 8, intensity: 0.8 }, |
| +добавлено | 'blood aurora': { color: '#ff2b47', top: '#a01cff', fringe: '#ffb0d0', rays: 1.3 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 1.2, impact: 1, linger: 5.5 }, |
| +добавлено | kinds: { |
| +добавлено | // faint green precipitation drifting down from the lower borders, painting glow where it lands |
| +добавлено | rain: { style: 'glow', drag: 1.5, gravity: 0.6, curl: 1.1, curlScale: 0.6, forceResponse: 0.8, life: [2.2, 3.6], size: [0.022, 0.008], stretch: 0.04, emissive: 4, color: ['#d8fff0', '#2dff8a'], alpha: [1, 0], fadeIn: 0.25, fadeOut: 0.5 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | // founding kinds by full name: light's rising motes (charge), arcane's sparkles (breakup fringe) |
| +добавлено | const motes = P.emitter('light.mote', { shape: 'ring', radius: 2.4, spread: 0.3 }); |
| +добавлено | const sparkle = P.emitter('arcane.sparkle', { shape: 'line', radius: 0.25, spread: 1.2 }); |
| +добавлено | const rain = P.emitter('aurora-veil.rain', { shape: 'line', radius: 0.35, spread: 0.35 }); |
| +добавлено | |
| +добавлено | // colours (linear), shared by reference with the material |
| +добавлено | const body = new THREE.Color(), topC = new THREE.Color(), fringe = new THREE.Color(), poolCol = new THREE.Color(); |
| +добавлено | const vBody = new THREE.Vector3(), vTop = new THREE.Vector3(), vFringe = new THREE.Vector3(); |
| +добавлено | let cB = '', cT = '', cF = ''; |
| +добавлено | function syncColors() { |
| +добавлено | const p = S.params; |
| +добавлено | if (p.color === cB && p.top === cT && p.fringe === cF) return; |
| +добавлено | cB = p.color; cT = p.top; cF = p.fringe; |
| +добавлено | body.set(cB); topC.set(cT); fringe.set(cF); |
| +добавлено | vBody.set(body.r, body.g, body.b); vTop.set(topC.r, topC.g, topC.b); vFringe.set(fringe.r, fringe.g, fringe.b); |
| +добавлено | poolCol.copy(body).lerp(fringe, 0.15); |
| +добавлено | P.setKind('aurora-veil.rain', { color: ['#e0fff2', cB] }); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // reusable option objects (the systems copy what they need) |
| +добавлено | const SIG = { pos: new THREE.Vector3(), normal: new THREE.Vector3(0, 1, 0), radius: 2.4, color: body, intensity: 0, reveal: 0, rotation: 0, style: 'holy', seed: 0 }; |
| +добавлено | const SPLAT = { x: 0, z: 0, x2: 0, z2: 0, radius: 1.6, width: 1.6, corruption: 0, pattern: 'line', seed: 0 }; |
| +добавлено | |
| +добавлено | // --- curtains: one instanced strip mesh (u along, v up) --- |
| +добавлено | const pos = [], idx = []; |
| +добавлено | for (let j = 0; j <= ROWS; j++) for (let i = 0; i <= COLS; i++) pos.push(i / COLS, V0 + (V1 - V0) * j / ROWS, 0); |
| +добавлено | for (let j = 0; j < ROWS; j++) for (let i = 0; i < COLS; i++) { |
| +добавлено | const a = j * (COLS + 1) + i, b = a + 1, c2 = a + COLS + 1, d2 = c2 + 1; |
| +добавлено | idx.push(a, b, d2, a, d2, c2); |
| +добавлено | } |
| +добавлено | const geo = new THREE.InstancedBufferGeometry(); |
| +добавлено | geo.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3)); |
| +добавлено | geo.setIndex(idx); |
| +добавлено | const a0 = instanced(geo, 'aP0', MAX_INST), a1 = instanced(geo, 'aP1', MAX_INST), a2 = instanced(geo, 'aP2', MAX_INST), a3 = instanced(geo, 'aP3', MAX_INST); |
| +добавлено | geo.instanceCount = 0; |
| +добавлено | const mat = new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, side: THREE.DoubleSide, ...FX_BLENDING, |
| +добавлено | uniforms: { ...U, uAvBody: { value: vBody }, uAvTop: { value: vTop }, uAvFringe: { value: vFringe }, uAvScale: { value: 6 }, uAvVeil: { value: 0.4 } }, |
| +добавлено | vertexShader: FX_VERT_HEAD + COMMON_GLSL + CURTAIN_VERT, |
| +добавлено | fragmentShader: FX_FRAG_HEAD + COMMON_GLSL + FX_OUT_GLSL + CURTAIN_FRAG, |
| +добавлено | }); |
| +добавлено | const mesh = new THREE.Mesh(geo, mat); |
| +добавлено | mesh.frustumCulled = false; |
| +добавлено | mesh.visible = false; |
| +добавлено | fx.scenes.fx.add(mesh); |
| +добавлено | |
| +добавлено | // --- per cast record (cast.js keeps 16) --- |
| +добавлено | const ST = Array.from({ length: MAX_CASTS }, () => ({ |
| +добавлено | on: false, centre: new THREE.Vector3(), dir: new THREE.Vector3(), axis: new THREE.Vector3(), |
| +добавлено | n: 0, E: 0, fold: 0, fringe: 0, tau: 0, drift: 0, driftV: 0, rise: 0, splatT: 0, |
| +добавлено | sigil: null, topLight: null, vortex: null, |
| +добавлено | cur: Array.from({ length: MAX_CURTAINS }, () => ({ |
| +добавлено | c: new THREE.Vector3(), y0: 0, H: 0, L: 0, w: 1, seed: 0, reveal: 0, light: null, |
| +добавлено | })), |
| +добавлено | })); |
| +добавлено | |
| +добавлено | function release(d) { |
| +добавлено | for (const k of d.cur) { k.light?.release(); k.light = null; k.reveal = 0; } |
| +добавлено | d.sigil?.release(); d.topLight?.release(); d.vortex?.release(); |
| +добавлено | d.sigil = d.topLight = d.vortex = null; |
| +добавлено | d.on = false; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // ends of curtain k's lower border (fold ignored) into _a, _b |
| +добавлено | function borderEnds(d, k, y) { |
| +добавлено | const h = k.L * 0.42; |
| +добавлено | _a.copy(k.c).addScaledVector(d.axis, -h).setY(y); |
| +добавлено | _b.copy(k.c).addScaledVector(d.axis, h).setY(y); |
| +добавлено | } |
| +добавлено | |
| +добавлено | syncColors(); |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(key) { if (key === 'color' || key === 'top' || key === 'fringe') syncColors(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = ST[c.index]; |
| +добавлено | syncColors(); |
| +добавлено | if (name === 'charge') { |
| +добавлено | d.on = true; |
| +добавлено | c.durations.charge = 1.2; c.durations.impact = 1; c.durations.linger = p.duration; |
| +добавлено | if (c.target >= 0) d.centre.copy(fx.dummies[c.target].pos).setY(0); else d.centre.set(c.to.x, 0, c.to.z); |
| +добавлено | d.dir.set(c.dir.x, 0, c.dir.z); |
| +добавлено | if (d.dir.lengthSq() < 1e-6) d.dir.set(0, 0, -1); |
| +добавлено | d.dir.normalize(); |
| +добавлено | d.axis.set(-d.dir.z, 0, d.dir.x); |
| +добавлено | d.n = Math.round(p.curtains); |
| +добавлено | d.E = 0; d.fold = 0.35; d.fringe = 0; d.rise = 0; d.splatT = 0; |
| +добавлено | d.tau = fx.rand() * 20; d.drift = fx.rand() * 20; d.driftV = 0.3; |
| +добавлено | for (let i = 0; i < MAX_CURTAINS; i++) { |
| +добавлено | const k = d.cur[i], L = LAYOUT[i]; |
| +добавлено | k.L = p.length * L[4]; k.H = p.height * L[3]; k.y0 = p.base + L[2]; k.w = L[5]; |
| +добавлено | k.c.copy(d.centre).addScaledVector(d.dir, L[0]).addScaledVector(d.axis, L[1] * p.length); |
| +добавлено | k.seed = fx.rand() * 10; k.reveal = 0; k.light = null; |
| +добавлено | if (i < d.n) { |
| +добавлено | k.light = fx.lights.acquire(); |
| +добавлено | if (k.light) { k.light.type = 1; k.light.intensity = 0; k.light.color.copy(body); k.light.range = k.y0 + 6; k.light.shadow = i === 0; } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | d.topLight = fx.lights.acquire(); |
| +добавлено | if (d.topLight) { d.topLight.intensity = 0; d.topLight.color.copy(topC); d.topLight.range = 16; } |
| +добавлено | SIG.pos.set(d.centre.x, 0.02, d.centre.z); SIG.intensity = 0; SIG.reveal = 0; SIG.rotation = d.tau; SIG.seed = c.seed % 1000; |
| +добавлено | d.sigil = fx.sigils.acquire(SIG); |
| +добавлено | d.vortex = null; |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | // the substorm breaks |
| +добавлено | const I = p.intensity; |
| +добавлено | fx.flash(0.1 * I); |
| +добавлено | fx.rig.shake(0.16, 6); |
| +добавлено | fx.pool?.flash(poolCol, 0.5 * I * p.light); |
| +добавлено | for (let i = 0; i < d.n; i++) { |
| +добавлено | const k = d.cur[i]; |
| +добавлено | borderEnds(d, k, k.y0); |
| +добавлено | sparkle.pos.copy(_a); sparkle.p.copy(_b); sparkle.vel.set(0, -0.6, 0); |
| +добавлено | sparkle.burst(220 * p.precipitation * k.w); |
| +добавлено | } |
| +добавлено | d.vortex = fx.forces.acquire(); |
| +добавлено | if (d.vortex) { d.vortex.type = 1; d.vortex.pos.copy(d.centre); d.vortex.dir.copy(UP); d.vortex.w = p.base + 2; d.vortex.p[0] = 2; d.vortex.p[1] = 0; d.vortex.p[2] = 0; d.vortex.p[3] = 0.25; } |
| +добавлено | } else if (name === 'done') release(d); |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = ST[c.index], I = p.intensity; |
| +добавлено | if (!d.on) return; |
| +добавлено | let Et = 0, foldT = 0.35, fringeT = 0, sigI = 0, sigR = 1; |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | // anticipation: a faint quiet arc high up, the circle draws, motes rise along the field |
| +добавлено | const k = c.t / c.durations.charge; |
| +добавлено | Et = 0.55 * sstep(k, 0, 0.9); |
| +добавлено | d.cur[0].reveal = sstep(k, 0, 0.85); |
| +добавлено | d.driftV = 0.3; |
| +добавлено | sigR = sstep(k, 0, 0.8); sigI = (0.5 + 1.3 * k) * I; |
| +добавлено | motes.pos.set(d.centre.x, 0.05, d.centre.z); motes.vel.set(0, 2 + 3 * k, 0); |
| +добавлено | motes.rate(380 * k * p.precipitation, dt); |
| +добавлено | } else if (c.phase === 'impact') { |
| +добавлено | // breakup: brighten, fold, rays race, the other curtains unroll from the middle out |
| +добавлено | const t = c.t / c.durations.impact; |
| +добавлено | Et = t < 0.3 ? 1.8 : 1.25; |
| +добавлено | for (let i = 0; i < d.n; i++) d.cur[i].reveal = Math.max(d.cur[i].reveal, sstep(t, 0.06 * i, 0.45 + 0.13 * i)); |
| +добавлено | foldT = 1.2 + 0.8 * (1 - sstep(t, 0.2, 1)); |
| +добавлено | fringeT = 1 - 0.6 * t; |
| +добавлено | d.driftV = 0.6 + 4 * (1 - sstep(t, 0, 0.8)); |
| +добавлено | sigI = (1.0 + 1.0 * (1 - t)) * I; |
| +добавлено | } else { |
| +добавлено | // linger: ripples and pulsates, rays drift, the lower border climbs as it fades |
| +добавлено | const t = c.t / c.durations.linger; |
| +добавлено | for (let i = 0; i < d.n; i++) d.cur[i].reveal = 1; |
| +добавлено | Et = 1.15 * (1 - sstep(t, 0.55, 1)); |
| +добавлено | foldT = 1.2; fringeT = 0.35 * (1 - t); |
| +добавлено | d.driftV = 0.6; |
| +добавлено | d.rise = 1.6 * sstep(t, 0.45, 1); |
| +добавлено | sigR = 1 - sstep(t, 0.1, 0.6); sigI = 1.0 * I * (1 - sstep(t, 0, 0.4)); |
| +добавлено | } |
| +добавлено | // eased (never a pop): fast up, slow down |
| +добавлено | d.E += (Et - d.E) * (1 - Math.exp(-dt / (Et > d.E ? 0.12 : 0.6))); |
| +добавлено | d.fold += (foldT - d.fold) * (1 - Math.exp(-dt / 0.3)); |
| +добавлено | d.fringe += (fringeT - d.fringe) * (1 - Math.exp(-dt / 0.15)); |
| +добавлено | d.tau += dt * p.speed * (0.5 + 0.6 * d.fold); |
| +добавлено | d.drift += dt * p.speed * d.driftV; |
| +добавлено | if (d.sigil) { d.sigil.reveal = sigR; d.sigil.intensity = sigI; d.sigil.rotation = d.tau * 0.15; } |
| +добавлено | |
| +добавлено | // lights: a green line light along each lower border, a magenta one up high |
| +добавлено | const E = d.E, live = c.phase !== 'charge'; |
| +добавлено | // the light on the ground: a shorter segment of each border that slides along it with the |
| +добавлено | // folds and breathes with the pulsations, so the glow on the floor wanders slowly |
| +добавлено | for (let i = 0; i < d.n; i++) { |
| +добавлено | const k = d.cur[i]; |
| +добавлено | if (!k.light) continue; |
| +добавлено | const slide = 0.22 * Math.sin(d.tau * 0.35 + i * 2.1 + k.seed), breath = 0.7 + 0.3 * Math.sin(d.tau * 0.9 + i * 1.7) * Math.sin(d.tau * 0.37 + k.seed); |
| +добавлено | borderEnds(d, k, k.y0 + d.rise - 0.2); |
| +добавлено | _a.lerp(_b, 0.18 + slide); _b.lerp(_a, (0.18 - slide) / (0.82 - slide)); |
| +добавлено | k.light.pos.copy(_a); k.light.end.copy(_b); |
| +добавлено | k.light.intensity = 4.5 * E * k.w * k.reveal * I * p.light * breath; |
| +добавлено | } |
| +добавлено | if (d.topLight) { |
| +добавлено | d.topLight.pos.copy(d.centre).setY(p.base + d.rise + p.height * 0.55); |
| +добавлено | d.topLight.intensity = 2.2 * E * I * p.light; |
| +добавлено | } |
| +добавлено | // the veil burns corruption away under each curtain (a negative splat: no glow painted) |
| +добавлено | d.splatT -= dt; |
| +добавлено | if (live && d.splatT <= 0 && E > 0.3) { |
| +добавлено | d.splatT = 0.35; |
| +добавлено | for (let i = 0; i < d.n; i++) { |
| +добавлено | const k = d.cur[i]; |
| +добавлено | borderEnds(d, k, 0); |
| +добавлено | SPLAT.x = _a.x; SPLAT.z = _a.z; SPLAT.x2 = _b.x; SPLAT.z2 = _b.z; |
| +добавлено | SPLAT.corruption = -0.25 * E * k.reveal; SPLAT.seed = (c.seed + i * 13) % 100; |
| +добавлено | fx.ground.splat(SPLAT); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // precipitation from the lower borders |
| +добавлено | if (live) for (let i = 0; i < d.n; i++) { |
| +добавлено | const k = d.cur[i]; |
| +добавлено | borderEnds(d, k, k.y0 + d.rise); |
| +добавлено | rain.pos.copy(_a); rain.p.copy(_b); rain.vel.set(0, -0.9, 0); |
| +добавлено | rain.rate(90 * p.precipitation * E * k.w * k.reveal, dt); |
| +добавлено | } |
| +добавлено | // dummies under the veil are cleansed |
| +добавлено | if (live && E > 0.3) for (const dm of fx.dummies) { |
| +добавлено | const r = Math.hypot(dm.pos.x - d.centre.x, dm.pos.z - d.centre.z); |
| +добавлено | if (r < p.length * 0.5) dm.status.corrupt = Math.max(0, dm.status.corrupt - dt * 0.9 * E); |
| +добавлено | } |
| +добавлено | // a gentle updraft lifts loose particles along the field lines |
| +добавлено | if (d.vortex) { d.vortex.p[1] = 5 * E; d.vortex.p[2] = 1.4 * E; } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | tick() { |
| +добавлено | syncColors(); |
| +добавлено | const A0 = a0.array, A1 = a1.array, A2 = a2.array, A3 = a3.array, p = S.params; |
| +добавлено | let m = 0; |
| +добавлено | for (const d of ST) { |
| +добавлено | if (!d.on || d.E < 0.002) continue; |
| +добавлено | for (let i = 0; i < d.n && m < MAX_INST; i++) { |
| +добавлено | const k = d.cur[i]; |
| +добавлено | if (k.reveal < 0.002) continue; |
| +добавлено | const o = m * 4; |
| +добавлено | A0[o] = k.c.x; A0[o + 1] = 0; A0[o + 2] = k.c.z; A0[o + 3] = k.L; |
| +добавлено | A1[o] = d.axis.x; A1[o + 1] = d.axis.z; A1[o + 2] = k.y0 + d.rise; A1[o + 3] = k.H; |
| +добавлено | A2[o] = k.seed; A2[o + 1] = 1.1 * p.fold * d.fold; A2[o + 2] = d.tau + i * 3.1; A2[o + 3] = d.drift + i * 5.3; |
| +добавлено | A3[o] = d.E * k.w * p.intensity; A3[o + 1] = k.reveal; A3[o + 2] = d.fringe; A3[o + 3] = p.rays; |
| +добавлено | m++; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | mat.uniforms.uAvVeil.value = 0.65 * p.veil; |
| +добавлено | geo.instanceCount = m; |
| +добавлено | mesh.visible = m > 0; |
| +добавлено | if (m > 0) a0.needsUpdate = a1.needsUpdate = a2.needsUpdate = a3.needsUpdate = true; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { U, COMMON_GLSL, NOISE_GLSL, FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL, FX_BLENDING, LINES_BLENDING } from '../../vfx/common.js'; |
| +добавлено | |
| +добавлено | // Shaders for Solar Flare (and Supernova's star, which imports them). |
| +добавлено | // |
| +добавлено | // References: SDO/HMI and Hinode SOT for the photosphere, SDO/AIA 171/304 for the loops. |
| +добавлено | // Star impostor: two camera-facing quads per star, both ray-traced against the true sphere. |
| +добавлено | // core (lines pass, full res): the photosphere. Turbulent granulation (inverted Worley cells |
| +добавлено | // warped by the larger flows, two generations cross-fading so it boils, a finer octave), |
| +добавлено | // active regions from the fbm channel (plage and faculae brightest towards the limb, spots |
| +добавлено | // with a penumbra, thin dark filaments), limb darkening (redder at the limb, where the eye |
| +добавлено | // sees cooler, higher gas). It writes the sphere's true depth, so loops behind it, smoke |
| +добавлено | // behind it and the void lens all treat it as a solid ball. |
| +добавлено | // corona (fx pass, half res): a ragged chromosphere rim of spicules, inner and outer corona from |
| +добавлено | // the impact parameter of each view ray, streamers from noise over direction, a pulse |
| +добавлено | // ring running outwards (Supernova), and heat-haze refraction over the inner corona. |
| +добавлено | // Loops: AnalyticRibbons, ONE fine strand each; a loop is a bundle of them. Each strand sits at its |
| +добавлено | // own place in the flux tube's cross-section and twists along the loop; its brightness is patchy |
| +добавлено | // along its length, plasma knots drain from the apex down both legs, and as it cools (heat 1 -> 0) |
| +добавлено | // the light gathers in the legs. |
| +добавлено | // Burst: an emissive volume (Volumes.js) for the moment the sun bursts on impact: a turbulent |
| +добавлено | // plasma bubble that hollows out as it grows and cools from the core colour to the limb's. |
| +добавлено | |
| +добавлено | const STAR_U = /* glsl */` |
| +добавлено | uniform vec4 uStarA; // centre xyz, photosphere radius m (0 = hidden) |
| +добавлено | uniform vec4 uStarB; // granule cells per radius, spin rad/s, pulse 0..1, intensity |
| +добавлено | uniform vec4 uStarC; // corona gain, corona extent (radii), streamer contrast, heat haze (m) |
| +добавлено | uniform vec4 uStarD; // seed, limb darkening 0..1, spots 0..1, pulse ring radius (radii, 0 = none) |
| +добавлено | uniform vec3 uStarCore; |
| +добавлено | uniform vec3 uStarLimb; |
| +добавлено | `; |
| +добавлено | |
| +добавлено | // quad at the centre plane, sized to the silhouette cone of a sphere ext x the photosphere |
| +добавлено | const quadVert = (ext) => /* glsl */` |
| +добавлено | out vec3 vW; |
| +добавлено | void main() { |
| +добавлено | vW = vec3(0.0); |
| +добавлено | float R = uStarA.w * ${ext}; |
| +добавлено | vec3 c = uStarA.xyz, toC = c - cameraPosition; |
| +добавлено | float D = length(toC); |
| +добавлено | if (uStarA.w <= 0.0 || D < R * 1.05) { gl_Position = vec4(2.0, 2.0, 2.0, 1.0); return; } |
| +добавлено | vec3 f = toC / D, r = normalize(cross(f, abs(f.y) < 0.99 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0))), u = cross(r, f); |
| +добавлено | vec3 p = c + (r * position.x + u * position.y) * (R / sqrt(1.0 - (R / D) * (R / D))); |
| +добавлено | vW = p; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(p, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const CORE_VERT = COMMON_GLSL + STAR_U + quadVert('1.04'); |
| +добавлено | const CORE_FRAG = COMMON_GLSL + NOISE_GLSL + STAR_U + /* glsl */` |
| +добавлено | uniform mat4 projectionMatrix; |
| +добавлено | in vec3 vW; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | vec3 c = uStarA.xyz; |
| +добавлено | float R = uStarA.w; |
| +добавлено | vec3 rd = normalize(vW - cameraPosition), oc = cameraPosition - c; |
| +добавлено | float b = dot(oc, rd), rho = sqrt(max(dot(oc, oc) - b * b, 0.0)); |
| +добавлено | float cover = clamp((R - rho) / max(fwidth(rho), 1e-5) + 0.5, 0.0, 1.0); // analytic limb AA |
| +добавлено | if (cover <= 0.0) discard; |
| +добавлено | float h = sqrt(max(R * R - rho * rho, 0.0)), mu = h / R; |
| +добавлено | vec3 p = cameraPosition + rd * (-b - h), n = (p - c) / R; |
| +добавлено | // the star turns about its local y, so the granules drift across the disk |
| +добавлено | float a = uStarB.y * uTime, ca = cos(a), sa = sin(a); |
| +добавлено | vec3 q = vec3(ca * n.x - sa * n.z, n.y, sa * n.x + ca * n.z); |
| +добавлено | // turbulent granulation (Hinode SOT): bright convection cells, dark intergranular lanes, warped by |
| +добавлено | // the larger flows, boiling (two generations of cells cross-fade) with a finer octave on top |
| +добавлено | vec3 wq = q * 0.8 + vec3(uStarD.x, uTime * 0.02, 0.0); |
| +добавлено | vec2 warp = vec2(texture(uNoise3D, wq).r, texture(uNoise3D, wq + vec3(0.37, 0.19, 0.53)).r) - 0.5; |
| +добавлено | vec3 gp = (q + vec3(warp, -warp.x) * 0.3) * (uStarB.x * 1.6 / 8.0) + vec3(uStarD.x, 0.0, uStarD.x * 0.7); // .a: 8 cells per tile |
| +добавлено | float gen = uTime * 0.6, gf = smoothstep(0.0, 1.0, fract(gen)); |
| +добавлено | float g1 = texture(uNoise3D, gp + vec3(0.0, floor(gen) * 0.413, 0.0)).a; |
| +добавлено | float g2 = texture(uNoise3D, gp + vec3(0.0, floor(gen + 1.0) * 0.413, 0.0)).a; |
| +добавлено | float g3 = texture(uNoise3D, gp * 2.2 + vec3(0.31, -uTime * 0.03, 0.17)).a; |
| +добавлено | float gran = mix(g1, g2, gf) * 0.78 + g3 * 0.22; |
| +добавлено | // active regions (SDO HMI / AIA 1600): bright plage and faculae (strongest towards the limb), |
| +добавлено | // spots with a penumbra inside them, and thin dark filaments snaking between |
| +добавлено | float sup = texture(uNoise3D, q * 0.32 + vec3(uStarD.x * 0.37, uTime * 0.006, 0.0)).r; |
| +добавлено | float fil = texture(uNoise3D, q * 0.5 + vec3(0.71, uStarD.x * 0.53, uTime * 0.004)).r; |
| +добавлено | float plage = smoothstep(0.56, 0.7, sup); |
| +добавлено | float filament = (1.0 - smoothstep(0.012, 0.04, abs(fil - 0.5))) * smoothstep(0.42, 0.58, sup) * (1.0 - plage * 0.6); |
| +добавлено | float pen = smoothstep(0.69, 0.73, sup) * uStarD.z, umb = smoothstep(0.735, 0.76, sup) * uStarD.z; |
| +добавлено | float heat = (0.3 + 1.05 * gran * gran) * (1.0 + plage * (0.18 + 0.55 * (1.0 - mu))) * (1.0 - 0.5 * filament) * (1.0 - 0.45 * pen - 0.4 * umb); |
| +добавлено | // limb darkening, I ~ 1 - u (1 - sqrt(mu)), and the limb runs redder (cooler, higher gas) |
| +добавлено | float limb = 1.0 - uStarD.y * (1.0 - pow(mu, 0.5)); |
| +добавлено | vec3 col = mix(uStarLimb, uStarCore, clamp(0.25 + 0.75 * mu + 0.25 * (heat - 0.6), 0.0, 1.0)) * heat * limb; |
| +добавлено | col *= uStarB.w * (1.0 + uStarB.z * (0.5 + mu)); |
| +добавлено | vec4 cl = projectionMatrix * viewMatrix * vec4(p, 1.0); |
| +добавлено | gl_FragDepth = clamp(cl.z / cl.w * 0.5 + 0.5, 0.0, 1.0); |
| +добавлено | fragColor = vec4(col, cover); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const CORONA_VERT = FX_VERT_HEAD + COMMON_GLSL + STAR_U + quadVert('uStarC.y'); |
| +добавлено | const CORONA_FRAG = FX_FRAG_HEAD + COMMON_GLSL + NOISE_GLSL + FX_OUT_GLSL + STAR_U + /* glsl */` |
| +добавлено | uniform mat4 projectionMatrix; |
| +добавлено | in vec3 vW; |
| +добавлено | void main() { |
| +добавлено | vec3 c = uStarA.xyz; |
| +добавлено | float R = uStarA.w, E = uStarC.y; |
| +добавлено | vec3 rd = normalize(vW - cameraPosition); |
| +добавлено | vec3 qp = cameraPosition + rd * dot(c - cameraPosition, rd), dv = qp - c; // closest approach |
| +добавлено | float rho = length(dv), x = rho / R; |
| +добавлено | if (x > E) discard; |
| +добавлено | vec3 dir = dv / max(rho, 1e-5); |
| +добавлено | float viewZ = -(viewMatrix * vec4(qp, 1.0)).z; |
| +добавлено | float xo = max(x - 1.0, 0.0); |
| +добавлено | // the chromosphere: a thin ragged rim of spicules standing off the limb, then the inner and the |
| +добавлено | // faint outer corona |
| +добавлено | float n1 = noise3(dir * 1.4 + uStarD.x), n2 = noise3(dir * 3.7 + vec3(0.0, xo * 0.1 - uTime * 0.05, uStarD.x)); |
| +добавлено | float spic = noise3(dir * 11.0 + vec3(uStarD.x, uTime * 0.15, 0.0)); |
| +добавлено | // spicule forest: ragged in height and patchy round the limb (clusters, gaps), never a clean ring |
| +добавлено | float rim = exp(-max(xo - 0.07 * smoothstep(0.25, 0.85, spic), 0.0) / 0.014) * (0.15 + 1.1 * smoothstep(0.35, 0.75, n2)); |
| +добавлено | float prof = 0.5 * rim + 0.4 * exp(-xo / 0.2) + 0.1 * exp(-xo / 0.8); |
| +добавлено | // streamers: noise over the direction (constant along a ray from the star), drifting outwards |
| +добавлено | float st = mix(1.0, 0.2 + 1.7 * n1 * n1 + 0.8 * (n2 - 0.5), uStarC.z * smoothstep(0.0, 0.3, xo)); |
| +добавлено | // pulse front: a ragged shell of brighter plasma running out through the corona |
| +добавлено | float ring = uStarD.w > 0.0 ? exp(-pow((x - uStarD.w + 0.25 * (n2 - 0.5)) / (0.1 + 0.08 * uStarD.w), 2.0)) * smoothstep(E * 0.8, 1.2, uStarD.w) * (0.3 + 1.2 * n1) : 0.0; |
| +добавлено | float edge = smoothstep(E, E * 0.55, x) * smoothstep(0.9, 1.0, x); |
| +добавлено | vec3 col = mix(uStarLimb * vec3(1.0, 0.8, 0.7), uStarCore, 0.5 * exp(-xo / 0.06) * (1.0 - 0.5 * rim)); |
| +добавлено | vec3 em = col * (prof * st + 0.5 * ring) * edge * uStarC.x * uStarB.w * (1.0 + 0.6 * uStarB.z); |
| +добавлено | // heat haze over the inner corona (metres of apparent displacement -> uv) |
| +добавлено | vec3 hq = dv / R * 0.6 + vec3(0.0, -uTime * 0.7, uStarD.x); |
| +добавлено | vec2 hz = vec2(noise3(hq), noise3(hq + vec3(0.37, 0.11, 0.71))) - 0.5; |
| +добавлено | float hpx = 0.5 * projectionMatrix[1][1] / max(viewZ, 1e-3); |
| +добавлено | vec2 dist = hz * vec2(hpx * uFxResolution.y / uFxResolution.x, hpx) * (uStarC.w * R * exp(-xo / 0.7) * smoothstep(E, E * 0.6, x)); |
| +добавлено | float s = softFade(viewZ, 0.35); |
| +добавлено | fxOut(vec3(0.0), 0.0, em * s, dist * s, viewZ); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // the photosphere is opaque: it covers what's behind it (alpha = limb coverage) and keeps the |
| +добавлено | // destination alpha, as LINES_BLENDING does (the planar reflection reads it as a mask) |
| +добавлено | const OPAQUE_EMISSIVE = { |
| +добавлено | ...LINES_BLENDING, depthWrite: true, |
| +добавлено | blendSrc: THREE.SrcAlphaFactor, blendDst: THREE.OneMinusSrcAlphaFactor, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | // n star impostors, each { core, corona, u, busy }. Hidden until u.uStarA.w > 0 and visible. |
| +добавлено | export function createStars(fx, n) { |
| +добавлено | const geo = new THREE.PlaneGeometry(2, 2); |
| +добавлено | return Array.from({ length: n }, () => { |
| +добавлено | const u = { |
| +добавлено | uStarA: { value: new THREE.Vector4() }, uStarB: { value: new THREE.Vector4(6, 0.3, 0, 1) }, |
| +добавлено | uStarC: { value: new THREE.Vector4(1, 3, 0.8, 0.05) }, uStarD: { value: new THREE.Vector4(0, 0.6, 0.4, 0) }, |
| +добавлено | uStarCore: { value: new THREE.Color(1, 0.95, 0.85) }, uStarLimb: { value: new THREE.Color(1, 0.5, 0.1) }, |
| +добавлено | }; |
| +добавлено | const core = new THREE.Mesh(geo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: CORE_VERT, fragmentShader: CORE_FRAG, |
| +добавлено | uniforms: { ...U, ...u }, ...OPAQUE_EMISSIVE, side: THREE.DoubleSide, |
| +добавлено | })); |
| +добавлено | core.frustumCulled = false; |
| +добавлено | core.visible = false; |
| +добавлено | core.renderOrder = -1; // first in the lines pass: its depth hides whatever lines lie behind it |
| +добавлено | core.layers.enable(1); // in the floor reflection too |
| +добавлено | fx.scenes.lines.add(core); |
| +добавлено | const corona = new THREE.Mesh(geo, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: CORONA_VERT, fragmentShader: CORONA_FRAG, |
| +добавлено | uniforms: { ...U, ...u }, ...FX_BLENDING, side: THREE.DoubleSide, |
| +добавлено | })); |
| +добавлено | corona.frustumCulled = false; |
| +добавлено | corona.visible = false; |
| +добавлено | fx.scenes.fx.add(corona); |
| +добавлено | return { core, corona, u, busy: false }; |
| +добавлено | }); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // loop strands: p0 = foot A, strand width (m) p1 = foot B, apex height above the chord (m) |
| +добавлено | // p2 = apex direction (unit), flux-tube radius (m) |
| +добавлено | // p3 = strand seed 0..1 (its place in the tube), twist (turns along the loop), top bulge, heat 0..1 |
| +добавлено | export const LOOP_PATH = /* glsl */` |
| +добавлено | vec3 ribbonPath(float t, vec4 p0, vec4 p1, vec4 p2, vec4 p3, out float width) { |
| +добавлено | vec3 A = p0.xyz, ch = p1.xyz - A, U = p2.xyz; |
| +добавлено | float s = sin(PI * t), lift = pow(s, 0.6); |
| +добавлено | vec3 p = A + ch * (t + p3.z * (t - 0.5) * lift) + U * (p1.w * lift); |
| +добавлено | vec3 side = normalize(cross(ch, U) + vec3(1e-6)); |
| +добавлено | // this strand's place in the tube (p3.x = radius 0..1, angle hashed from it), braided round the |
| +добавлено | // axis along the loop (p3.y turns, either hand) and writhing slowly; the tube pinches at the feet |
| +добавлено | float ph = fract(sin(p3.x * 91.7) * 4375.85) * 6.2832 + t * p3.y * 6.2832 + uTime * 0.25; |
| +добавлено | p += (side * cos(ph) + U * sin(ph)) * (p2.w * p3.x * (0.25 + 0.75 * s)); |
| +добавлено | width = p0.w * (0.55 + 0.45 * s); |
| +добавлено | return p; |
| +добавлено | }`; |
| +добавлено | export const LOOP_SHADE = /* glsl */` |
| +добавлено | uniform highp sampler3D uNoise3D; |
| +добавлено | vec3 ribbonShade(RibbonFrag r, out vec3 media, out float alpha, out float distortion) { |
| +добавлено | media = vec3(0.0); alpha = 0.0; distortion = 0.0; |
| +добавлено | float y = r.uv.y, s = abs(2.0 * r.uv.x - 1.0); // s: 0 at the apex .. 1 at the feet |
| +добавлено | float sd = r.p3.x * 13.0, heat = r.p3.w; |
| +добавлено | float core = exp(-y * y * 4.0); |
| +добавлено | // fine structure: patchy brightness along the strand, slowly evolving |
| +добавлено | // two scales, so a thread breaks into bright fragments and gaps (AIA: no strand glows evenly end to end) |
| +добавлено | float patchy = smoothstep(0.38, 0.8, texture(uNoise3D, vec3(r.uv.x * 2.3 + sd, sd * 0.37, uTime * 0.04)).r) |
| +добавлено | * (0.35 + 0.65 * smoothstep(0.3, 0.7, texture(uNoise3D, vec3(r.uv.x * 7.1 + sd * 1.7, sd * 0.53 + 0.4, uTime * 0.07)).r)); |
| +добавлено | // plasma knots sliding from the apex down both legs (draining under gravity) |
| +добавлено | float k = smoothstep(0.55, 0.85, texture(uNoise3D, vec3(s * 2.0 - uTime * 0.45, sd * 0.71, 0.5 + sd * 0.11)).r); |
| +добавлено | // brightest at the footpoints and the loop top; a hot loop glows along its length, a cooling one |
| +добавлено | // drains: the top fades and the light gathers in the knots and the legs |
| +добавлено | float feet = exp(-(1.0 - s) * 10.0), top = exp(-s * s * 14.0); |
| +добавлено | float body = (0.06 + 0.94 * patchy) * mix(0.25 + 0.75 * s * s, 1.0, heat); |
| +добавлено | return r.color * core * (0.7 * body + (0.3 + 0.7 * heat) * top + 1.1 * feet + 1.6 * k * (1.0 - 0.5 * heat)); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // JS mirror of LOOP_PATH's centreline without the strand offset (coronal rain, sparks at the feet) |
| +добавлено | export function loopPoint(h, t, out) { |
| +добавлено | const s = Math.sin(Math.PI * t), lift = Math.pow(s, 0.6), k = t + h.p3.z * (t - 0.5) * lift; |
| +добавлено | return out.set( |
| +добавлено | h.p0.x + (h.p1.x - h.p0.x) * k + h.p2.x * h.p1.w * lift, |
| +добавлено | h.p0.y + (h.p1.y - h.p0.y) * k + h.p2.y * h.p1.w * lift, |
| +добавлено | h.p0.z + (h.p1.z - h.p0.z) * k + h.p2.z * h.p1.w * lift, |
| +добавлено | ); |
| +добавлено | } |
| +добавлено | |
| +добавлено | export const BURST_GLSL = /* glsl */` |
| +добавлено | uniform vec4 uBurst; // radius m, age s, intensity, seed |
| +добавлено | uniform vec3 uBurstPos; |
| +добавлено | uniform vec3 uBurstCore; |
| +добавлено | uniform vec3 uBurstCol; |
| +добавлено | void volumeSample(vec3 pw, vec3 pl, out float sigma, out vec3 albedo, out vec3 emission) { |
| +добавлено | // pure emission (sigma under the integrator's occupied test, but not a literal 0 for fxc) |
| +добавлено | sigma = 1e-6 * uBurst.x; albedo = vec3(0.0); emission = vec3(0.0); |
| +добавлено | float R = uBurst.x, age = uBurst.y; |
| +добавлено | vec3 q = (pw - uBurstPos) / R; |
| +добавлено | float r = length(q); |
| +добавлено | if (r > 1.0) return; |
| +добавлено | // turbulent plasma: billowy fbm, band-limited to what the march can resolve (no radial filaments) |
| +добавлено | vec3 np = q * 0.45 + vec3(uBurst.w, -age * 0.25, uBurst.w * 0.61); |
| +добавлено | float n1 = noise3(np), n2 = noise3(np * 1.9 + vec3(0.13, 0.4, 0.7)); |
| +добавлено | float b = smoothstep(0.3, 0.75, n1 * 0.7 + n2 * 0.3); |
| +добавлено | float hollow = smoothstep(0.0, 0.35, age), rr = r + (n2 - 0.5) * 0.3; |
| +добавлено | float shell = smoothstep(1.0, 0.75, rr) * smoothstep(0.05 + 0.55 * hollow, 0.35 + 0.45 * hollow, rr); |
| +добавлено | float heat = clamp(1.15 - age * 2.6 + 0.45 * b - 0.3 * r, 0.0, 1.0); |
| +добавлено | emission = mix(uBurstCol, uBurstCore, heat * heat) * ((0.12 + 0.88 * heat) * shell * (0.15 + 1.6 * b) * uBurst.z / R); |
| +добавлено | }`; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { ARENA, arenaSDF } from '../../vfx/common.js'; |
| +добавлено | import { createStars, LOOP_PATH, LOOP_SHADE, BURST_GLSL, loopPoint } from './solar-flare.glsl.js'; |
| +добавлено | |
| +добавлено | // Solar Flare (SPELLBOOK, Light hero). References: SDO/AIA 171 and 304 movies of active-region |
| +добавлено | // flares and prominence eruptions, Hinode SOT granulation. A miniature sun ignites in front of a holy |
| +добавлено | // sigil at the focus: motes pour in, it swells, breathes in, and launches. In flight it is a |
| +добавлено | // ray-traced photosphere (boiling granulation, plage, filaments, spots, limb darkening) inside a |
| +добавлено | // spicule rim and a faint corona with heat haze; bundles of fine plasma strands arch off its limb, |
| +добавлено | // brighten and drain. It sheds a plasma wake and lights the floor and pillars as it passes. |
| +добавлено | // On impact the sun bursts and the flare erupts, as in the standard (CSHKP) flare picture: |
| +добавлено | // - the prominence lifts off: a twisted flux rope of red-orange strands, anchored at both ends |
| +добавлено | // of the polarity line, rises, swells and accelerates away as it fades |
| +добавлено | // - beneath it an arcade of post-flare loops forms along the line, one after another, each a |
| +добавлено | // bundle of fine strands: hot and white-gold along their length, then cooling, the light |
| +добавлено | // draining into their legs, coronal rain dripping off their tops and painting the floor |
| +добавлено | // - two flare ribbons burn on the floor under the footpoints and spread apart (glow, two line |
| +добавлено | // lights), footpoints spit sparks, a shock runs out, then an updraft lifts motes through it. |
| +добавлено | |
| +добавлено | const MAX_SUNS = 3; |
| +добавлено | const SURF = 4, SS = 5; // surface loops on the star, strands per loop |
| +добавлено | const ARC = 9, AS = 10, RS = 20; // arcade loops, strands per arcade loop (1 haze + 9 threads), flux-rope strands |
| +добавлено | const ROPE = ARC; // loop index of the flux rope |
| +добавлено | const NL = ARC + 1, MAXSTR = ARC * AS + RS; |
| +добавлено | const UP = new THREE.Vector3(0, 1, 0); |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _c = new THREE.Vector3(), _m = new THREE.Vector3(), _e = new THREE.Vector3(), _v = new THREE.Vector3(); |
| +добавлено | const WHITE = new THREE.Color(1, 1, 1); |
| +добавлено | const clamp = THREE.MathUtils.clamp, sstep = THREE.MathUtils.smoothstep; |
| +добавлено | const easeOutBack = (x) => 1 + 2.70158 * (x - 1) ** 3 + 1.70158 * (x - 1) ** 2; |
| +добавлено | // reused option objects (the systems copy what they need) |
| +добавлено | const SPLAT = { x: 0, z: 0, x2: 0, z2: 0, radius: 1, heat: 0, char: 0, glow: 0, pattern: 'radial', seed: 0, width: 0 }; |
| +добавлено | const SHOCK = { pos: new THREE.Vector3(), radius0: 0.3, radius1: 6, duration: 0.6, thickness: 0.6, distortion: 0.05, color: null, intensity: 0, ground: true }; |
| +добавлено | const SIGIL = { pos: new THREE.Vector3(), normal: new THREE.Vector3(), radius: 0.9, color: null, intensity: 0, reveal: 0, rotation: 0, style: 'holy', seed: 0 }; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'solar-flare', name: 'Solar Flare', element: 'light', order: 10, hue: '#ffc04a', |
| +добавлено | shape: 'projectile', speed: 8, radius: 0.4, |
| +добавлено | params: { size: 1, speed: 8, intensity: 1, pulse: 1, loops: 1, flare: 1, granules: 8, corona: 1, sparks: 1, glow: 1, haze: 1, core: '#ffd27a', color: '#ff560c' }, |
| +добавлено | ranges: { |
| +добавлено | size: [0.5, 2, 0.01], speed: [3, 20, 0.1], intensity: [0, 3, 0.01], pulse: [0, 2, 0.01], loops: [0, 3, 0.01], flare: [0.4, 2, 0.01], |
| +добавлено | granules: [2, 14, 0.1], corona: [0, 3, 0.01], sparks: [0, 3, 0.01], glow: [0, 3, 0.01], haze: [0, 3, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | 'red dwarf': { core: '#ffd2a0', color: '#ff3a12', size: 0.8, speed: 6, pulse: 1.6, granules: 5 }, |
| +добавлено | 'blue giant': { core: '#f0f5ff', color: '#4a86ff', size: 1.35, speed: 7, flare: 1.25, granules: 11, corona: 1.4 }, |
| +добавлено | 'white dwarf': { core: '#ffffff', color: '#ffd090', size: 0.65, speed: 12, intensity: 1.6, pulse: 2, flare: 0.8 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 0.9, impact: 0.5, linger: 2.8 }, |
| +добавлено | kinds: { |
| +добавлено | plasma: { style: 'glow', drag: 1.6, buoyancy: 0.6, curl: 1.8, curlScale: 0.7, forceResponse: 0.6, life: [0.3, 0.75], size: [0.014, 0.004], stretch: 0.03, emissive: 3, color: ['#ffd890', '#ff4a0c'], alpha: [1, 0], fadeIn: 0.08, fadeOut: 0.4 }, |
| +добавлено | ember: { style: 'glow', drag: 0.9, buoyancy: 1.1, curl: 2.4, curlScale: 0.5, forceResponse: 0.8, life: [1.2, 2.8], size: [0.024, 0.006], stretch: 0.015, emissive: 4, color: ['#ffd890', '#ff5a12'], alpha: [1, 0], fadeIn: 0.1, fadeOut: 0.5, restitution: 0.3, friction: 0.5 }, |
| +добавлено | // coronal rain: drips off the loop tops, falls, and paints glow where it lands |
| +добавлено | rain: { style: 'glow', drag: 0.3, gravity: 7, life: [0.5, 1.0], size: [0.03, 0.012], stretch: 0.045, emissive: 8, color: ['#fff4d0', '#ff8a20'], alpha: [1, 0.2], fadeIn: 0.05, restitution: 0.15, friction: 0.7, paints: 'glow', paintAmount: 0.06 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const gather = P.emitter('light.mote', { shape: 'shell', radius: 1.6, spread: 0.2, radial: -2.8, life: 0.32 }); |
| +добавлено | const motes = P.emitter('light.mote', { shape: 'disk', radius: 1.5, spread: 0.6 }); |
| +добавлено | const spark = P.emitter('light.spark', { shape: 'sphere', radius: 0.25, spread: 3, radial: 5 }); |
| +добавлено | const footSpark = P.emitter('light.spark', { shape: 'sphere', radius: 0.08, spread: 2.2, radial: 2.5 }); |
| +добавлено | const plasma = P.emitter('solar-flare.plasma', { shape: 'shell', radius: 0.4, spread: 0.5, radial: 0.3 }); |
| +добавлено | const ember = P.emitter('solar-flare.ember', { shape: 'disk', radius: 1.2, spread: 0.6 }); |
| +добавлено | const rain = P.emitter('solar-flare.rain', { shape: 'sphere', radius: 0.05, spread: 0.3 }); |
| +добавлено | |
| +добавлено | // colours: parsed only when the param strings change (Color.set allocates) |
| +добавлено | const coreCol = new THREE.Color(), glowCol = new THREE.Color(), loopCol = new THREE.Color(), lightCol = new THREE.Color(), hotCol = new THREE.Color(), promCol = new THREE.Color(), hue = new THREE.Color(this.hue); |
| +добавлено | let coreHex = '', glowHex = ''; |
| +добавлено | function colours(p) { |
| +добавлено | if (p.core === coreHex && p.color === glowHex) return; |
| +добавлено | coreHex = p.core; glowHex = p.color; |
| +добавлено | coreCol.set(p.core); glowCol.set(p.color); |
| +добавлено | loopCol.copy(glowCol).lerp(coreCol, 0.45); |
| +добавлено | lightCol.copy(glowCol).lerp(coreCol, 0.5); |
| +добавлено | hotCol.copy(coreCol); |
| +добавлено | promCol.copy(glowCol).lerp(coreCol, 0.2); |
| +добавлено | } |
| +добавлено | SHOCK.color = glowCol; |
| +добавлено | SIGIL.color = loopCol; |
| +добавлено | |
| +добавлено | const stars = createStars(fx, MAX_SUNS); |
| +добавлено | const bursts = []; |
| +добавлено | for (let i = 0; i < MAX_SUNS; i++) { |
| +добавлено | const v = fx.volumes.add({ |
| +добавлено | shape: 'sphere', glsl: BURST_GLSL, maxSteps: 16, |
| +добавлено | uniforms: { uBurst: { value: new THREE.Vector4(1, 0, 0, 0) }, uBurstPos: { value: new THREE.Vector3() }, uBurstCore: { value: coreCol }, uBurstCol: { value: glowCol } }, |
| +добавлено | }); |
| +добавлено | v.busy = false; |
| +добавлено | bursts.push(v); |
| +добавлено | } |
| +добавлено | const loops = new fx.AnalyticRibbons({ max: 224, segments: 40, pathGLSL: LOOP_PATH, shadeGLSL: LOOP_SHADE, target: 'lines' }); |
| +добавлено | fx.scenes.lines.add(loops.mesh); |
| +добавлено | |
| +добавлено | // per cast record (cast.js keeps 16): no allocation per cast |
| +добавлено | const v3s = (n) => Array.from({ length: n }, () => new THREE.Vector3()); |
| +добавлено | const ST = Array.from({ length: 16 }, () => ({ |
| +добавлено | star: null, burst: null, light: null, sigil: null, force: null, rib: [null, null], |
| +добавлено | str: new Array(MAXSTR).fill(null), sl: new Uint8Array(MAXSTR), ss: new Float32Array(MAXSTR), nStr: 0, |
| +добавлено | la: v3s(SURF), lb: v3s(SURF), lt0: new Float64Array(SURF), llife: new Float32Array(SURF), lh: new Float32Array(SURF), |
| +добавлено | fa: v3s(NL), fb: v3s(NL), fu: v3s(NL), fh: new Float32Array(NL), fw: new Float32Array(NL), fdelay: new Float32Array(NL), nLoop: 0, |
| +добавлено | G: new THREE.Vector3(), prev: new THREE.Vector3(), P: new THREE.Vector3(), Q: new THREE.Vector3(), |
| +добавлено | R0: 0, dist: 0, total: 0, ph: 0, pulseN: -1, pulse: 0, splatT: 0, frame: 0, age: 0, shockR: 0, rot: 0, inPool: false, ribT: 0, |
| +добавлено | })); |
| +добавлено | |
| +добавлено | function splat(x, z, radius, heat, char, glow, pattern, seed, width = 0, x2 = x, z2 = z) { |
| +добавлено | SPLAT.x = x; SPLAT.z = z; SPLAT.x2 = x2; SPLAT.z2 = z2; SPLAT.radius = radius; SPLAT.heat = heat; SPLAT.char = char; SPLAT.glow = glow; |
| +добавлено | SPLAT.pattern = pattern; SPLAT.seed = seed % 100; SPLAT.width = width; |
| +добавлено | fx.ground.splat(SPLAT); |
| +добавлено | } |
| +добавлено | const focus = (k) => fx.arena.setFocus(hue, k); |
| +добавлено | |
| +добавлено | function placeStar(st, pos, R, I, p, d) { |
| +добавлено | const u = st.u; |
| +добавлено | st.core.visible = st.corona.visible = R > 1e-3; |
| +добавлено | u.uStarA.value.set(pos.x, pos.y, pos.z, R); |
| +добавлено | u.uStarB.value.set(p.granules, 0.3, d.pulse * p.pulse * 0.35, I); |
| +добавлено | u.uStarC.value.set(0.9 * p.corona, 2.6, 0.3, 0.06 * p.haze); |
| +добавлено | u.uStarD.value.w = 0; |
| +добавлено | u.uStarCore.value.copy(coreCol); |
| +добавлено | u.uStarLimb.value.copy(glowCol); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the star's slow flicker: brightenings every half second or so, never a strobe |
| +добавлено | function pulseClock(c, d) { |
| +добавлено | const T = 0.55, n = Math.floor(c.age / T), tp = c.age - n * T; |
| +добавлено | if (n !== d.pulseN) d.pulseN = n; |
| +добавлено | d.pulse = tp < 0.08 ? tp / 0.08 : Math.exp(-(tp - 0.08) / 0.2); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // a surface loop in the star's turning frame, biased to the limb so it reads in silhouette |
| +добавлено | function spawnLoop(c, d, i) { |
| +добавлено | const z = 2 * fx.rand() - 1, az = fx.rand() * 6.2832, s = Math.sqrt(1 - z * z); |
| +добавлено | _m.set(s * Math.cos(az), z, s * Math.sin(az)); |
| +добавлено | _v.subVectors(c.pos, fx.camera.position).normalize(); |
| +добавлено | _m.addScaledVector(_v, -_v.dot(_m) * 0.7).normalize(); |
| +добавлено | const a = 0.3 * fx.U.uTime.value, ca = Math.cos(a), sa = Math.sin(a); // world -> star frame |
| +добавлено | _m.set(ca * _m.x - sa * _m.z, _m.y, sa * _m.x + ca * _m.z); |
| +добавлено | _e.set(fx.rand() - 0.5, fx.rand() - 0.5, fx.rand() - 0.5).cross(_m); |
| +добавлено | if (_e.lengthSq() < 1e-6) _e.set(1, 0, 0).cross(_m); |
| +добавлено | _e.normalize(); |
| +добавлено | const ang = 0.14 + 0.22 * fx.rand(); |
| +добавлено | d.la[i].copy(_m).multiplyScalar(Math.cos(ang)).addScaledVector(_e, -Math.sin(ang)); |
| +добавлено | d.lb[i].copy(_m).multiplyScalar(Math.cos(ang)).addScaledVector(_e, Math.sin(ang)); |
| +добавлено | d.lt0[i] = c.age; |
| +добавлено | d.llife[i] = 0.8 + 0.7 * fx.rand(); |
| +добавлено | d.lh[i] = 0.3 + 0.5 * fx.rand(); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function surfaceLoops(c, d, p, R, gate) { |
| +добавлено | const a = 0.3 * fx.U.uTime.value, ca = Math.cos(a), sa = Math.sin(a), C = c.pos; |
| +добавлено | for (let i = 0; i < SURF; i++) { |
| +добавлено | let x = (c.age - d.lt0[i]) / d.llife[i]; |
| +добавлено | if (x >= 1 || x < 0) { spawnLoop(c, d, i); x = 0; } |
| +добавлено | // rises, holds while it brightens and drains, sags a little as it fades |
| +добавлено | const h = d.lh[i] * (1 - (1 - Math.min(x / 0.3, 1)) ** 3) * (1 - 0.2 * sstep(x, 0.6, 1)); |
| +добавлено | const vis = sstep(x, 0, 0.18) * (1 - sstep(x, 0.65, 1)), heat = 1 - sstep(x, 0.2, 0.9); |
| +добавлено | // star frame -> world (the inverse of the shader's spin) |
| +добавлено | const A = d.la[i], B = d.lb[i]; |
| +добавлено | _a.set(ca * A.x + sa * A.z, A.y, -sa * A.x + ca * A.z); |
| +добавлено | _b.set(ca * B.x + sa * B.z, B.y, -sa * B.x + ca * B.z); |
| +добавлено | _c.addVectors(_a, _b); |
| +добавлено | const sag = R * (1 - 0.5 * _c.length()); |
| +добавлено | _c.normalize(); |
| +добавлено | for (let s = 0; s < SS; s++) { |
| +добавлено | const hd = d.str[i * SS + s]; |
| +добавлено | if (!hd) continue; |
| +добавлено | hd.p0.set(C.x + _a.x * R, C.y + _a.y * R, C.z + _a.z * R, 0.014 * R); |
| +добавлено | hd.p1.set(C.x + _b.x * R, C.y + _b.y * R, C.z + _b.z * R, sag + h * R); |
| +добавлено | hd.p2.set(_c.x, _c.y, _c.z, 0.07 * R); |
| +добавлено | hd.p3.set(d.ss[i * SS + s], 0.6, 0.35, heat); |
| +добавлено | hd.color.copy(glowCol).lerp(hotCol, heat); |
| +добавлено | hd.intensity = 5 * p.loops * p.intensity * vis * gate * (0.75 + 0.25 * Math.sin(c.age * (5 + s) + s * 2.1)); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function dropStrands(d) { |
| +добавлено | for (let k = 0; k < d.nStr; k++) { d.str[k]?.release(); d.str[k] = null; } |
| +добавлено | d.nStr = 0; |
| +добавлено | } |
| +добавлено | function takeStrands(d, n) { |
| +добавлено | dropStrands(d); |
| +добавлено | for (let k = 0; k < n; k++) { |
| +добавлено | const h = loops.acquire(); |
| +добавлено | if (!h) break; |
| +добавлено | h.intensity = 0; d.str[k] = h; d.ss[k] = fx.rand(); d.nStr = k + 1; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'core' || k === 'color') colours(S.params); }, |
| +добавлено | tick() { loops.update(); }, |
| +добавлено | |
| +добавлено | travel(c, dt) { |
| +добавлено | const p = S.params, d = ST[c.index]; |
| +добавлено | d.dist += p.speed * dt; |
| +добавлено | d.prev.copy(c.pos); |
| +добавлено | c.pos.copy(c.from).addScaledVector(c.dir, Math.min(d.dist, d.total)).addScaledVector(UP, Math.sin(c.t * 5.3 + d.ph) * 0.06 * Math.min(1, d.dist)); |
| +добавлено | if (dt > 0) c.vel.subVectors(c.pos, d.prev).divideScalar(dt); |
| +добавлено | c.travelled = d.dist; |
| +добавлено | return d.dist >= d.total || (d.dist > 1 && arenaSDF(c.pos) < 0.8 * d.R0) || c.t > 10; |
| +добавлено | }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = ST[c.index]; |
| +добавлено | colours(p); |
| +добавлено | if (name === 'charge') { |
| +добавлено | d.R0 = 0.5 * p.size; d.dist = 0; d.total = c.from.distanceTo(c.to); d.ph = fx.rand() * 6.28; |
| +добавлено | d.pulseN = -1; d.pulse = 0; d.splatT = 0; d.frame = 0; d.age = 0; d.shockR = 0; d.nLoop = 0; d.ribT = 0; |
| +добавлено | d.rot = (c.seed % 628) / 100; |
| +добавлено | d.star = null; |
| +добавлено | for (const st of stars) if (!st.busy) { d.star = st; break; } |
| +добавлено | if (d.star) { d.star.busy = true; d.star.u.uStarD.value.set((c.seed % 97) * 0.173, 0.66, 0.5, 0); placeStar(d.star, c.from, 0, 0, p, d); } |
| +добавлено | d.light = fx.lights.acquire(); |
| +добавлено | if (d.light) { d.light.type = 0; d.light.range = 7; d.light.intensity = 0; d.light.shadow = true; d.light.color.copy(lightCol); d.light.pos.copy(c.from); } |
| +добавлено | SIGIL.pos.copy(c.from).addScaledVector(c.dir, 0.05); SIGIL.normal.copy(c.dir); SIGIL.radius = 0.95 * p.size; SIGIL.seed = c.seed % 1000; SIGIL.rotation = d.rot; |
| +добавлено | d.sigil = fx.sigils.acquire(SIGIL); |
| +добавлено | if (d.sigil) { d.sigil.reveal = 0; d.sigil.intensity = 0; } |
| +добавлено | d.force = null; d.rib[0] = d.rib[1] = null; |
| +добавлено | for (let i = 0; i < SURF; i++) { d.lt0[i] = -99; d.llife[i] = 1; } |
| +добавлено | takeStrands(d, SURF * SS); |
| +добавлено | } else if (name === 'release') { |
| +добавлено | fx.flash(0.08 * p.intensity); |
| +добавлено | fx.rig.shake(0.15, 10); |
| +добавлено | spark.pos.copy(c.from); spark.radius = d.R0; spark.vel.copy(c.dir).multiplyScalar(4); spark.burst(120 * p.sparks); |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | slam(c, p, d); |
| +добавлено | } else if (name === 'done') { |
| +добавлено | if (d.star) { d.star.busy = false; d.star.core.visible = d.star.corona.visible = false; d.star.u.uStarA.value.w = 0; } |
| +добавлено | if (d.burst) { d.burst.visible = false; d.burst.busy = false; } |
| +добавлено | dropStrands(d); |
| +добавлено | d.light?.release(); d.sigil?.release(); d.force?.release(); d.rib[0]?.release(); d.rib[1]?.release(); |
| +добавлено | d.star = d.burst = d.light = d.sigil = d.force = d.rib[0] = d.rib[1] = null; |
| +добавлено | focus(2.5); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = ST[c.index], L = d.light, I = p.intensity; |
| +добавлено | colours(p); |
| +добавлено | d.frame++; |
| +добавлено | d.rot += dt * (c.phase === 'charge' ? 1.4 : 3); |
| +добавлено | if (d.sigil) d.sigil.rotation = d.rot; |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge; |
| +добавлено | // ignite, swell with an overshoot, then breathe in before the launch |
| +добавлено | const R = d.R0 * easeOutBack(sstep(k, 0.12, 0.72)) * (1 - 0.12 * sstep(k, 0.82, 1)) * (1 + 0.03 * Math.sin(c.age * 31) * k); |
| +добавлено | pulseClock(c, d); |
| +добавлено | d.pulse *= sstep(k, 0.4, 0.8); |
| +добавлено | if (d.star) placeStar(d.star, c.from, R, (2 + 3 * k) * I, p, d); |
| +добавлено | surfaceLoops(c, d, p, R, sstep(k, 0.4, 0.85)); |
| +добавлено | gather.pos.copy(c.from); gather.rate(700 * (1 - k) * p.sparks, dt); |
| +добавлено | if (L) { L.pos.copy(c.from); L.intensity = 16 * k * k * I; } |
| +добавлено | if (d.sigil) { d.sigil.reveal = sstep(k, 0, 0.8); d.sigil.intensity = (1 + 2.5 * k) * I; } |
| +добавлено | focus(2.5 * (1 - k) * (1 - k) + 0.15); // the orb's light is drawn into the sun |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (c.phase === 'travel') { |
| +добавлено | pulseClock(c, d); |
| +добавлено | const R = d.R0 * (0.88 + 0.12 * easeOutBack(Math.min(c.t / 0.18, 1))) * (1 + 0.03 * d.pulse * p.pulse); |
| +добавлено | if (d.star) placeStar(d.star, c.pos, R, 4 * I, p, d); |
| +добавлено | surfaceLoops(c, d, p, R, 1); |
| +добавлено | // a wake of plasma shed behind it, not a halo round it |
| +добавлено | plasma.pos.copy(c.pos); plasma.radius = R * 0.9; plasma.vel.copy(c.vel).multiplyScalar(0.45); plasma.rate(70 * p.sparks, dt); |
| +добавлено | spark.pos.copy(c.pos); spark.radius = R * 0.8; spark.vel.copy(c.vel).multiplyScalar(0.3); spark.rate(18 * p.sparks * (1 + 2 * d.pulse), dt); |
| +добавлено | if (L) { L.pos.copy(c.pos); L.range = 9; L.intensity = (18 + 8 * d.pulse * p.pulse) * I; } |
| +добавлено | focus(Math.min(2.5, 0.15 + c.t * 3)); |
| +добавлено | if (d.sigil) { // the sigil unwinds behind the launch |
| +добавлено | const x = c.t / 0.5; |
| +добавлено | d.sigil.reveal = 1 - sstep(x, 0, 1); d.sigil.intensity = 3 * I * (1 - x); |
| +добавлено | if (x >= 1) { d.sigil.release(); d.sigil = null; } |
| +добавлено | } |
| +добавлено | // the floor glows under the sun as it passes |
| +добавлено | d.splatT -= dt; |
| +добавлено | if (d.splatT <= 0 && c.pos.y < 3.2) { d.splatT = 0.1; splat(c.pos.x, c.pos.z, 0.75 * p.size, 0.08, 0, 0.35 * p.glow * (1 - c.pos.y / 3.2), 'radial', c.seed + d.frame); } |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dist = Math.hypot(dm.pos.x - c.pos.x, dm.pos.z - c.pos.z); |
| +добавлено | if (dist < 1.6 * p.size) dm.status.burn = Math.max(dm.status.burn, 0.35 * (1 - dist / (1.6 * p.size))); |
| +добавлено | } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | // impact + linger: the burst, the eruption, the arcade |
| +добавлено | d.age += dt; |
| +добавлено | const tt = d.age, lx = c.phase === 'linger' ? c.t / c.durations.linger : 0; |
| +добавлено | if (d.star) { |
| +добавлено | const R = d.R0 * (1 + 0.5 * sstep(tt, 0, 0.04)) * (1 - sstep(tt, 0.03, 0.14)); |
| +добавлено | placeStar(d.star, c.pos, R, 6 * I * (1 + 3 * sstep(tt, 0, 0.05)), p, d); |
| +добавлено | if (R <= 1e-3) { d.star.busy = false; d.star.core.visible = d.star.corona.visible = false; d.star.u.uStarA.value.w = 0; d.star = null; } |
| +добавлено | } |
| +добавлено | const v = d.burst; |
| +добавлено | if (v) { |
| +добавлено | if (tt > 0.6) { v.visible = false; v.busy = false; d.burst = null; } |
| +добавлено | else { |
| +добавлено | const fl = p.flare * p.size, R = 0.3 + 0.65 * fl * (1 - Math.exp(-tt * 9)); |
| +добавлено | v.position.copy(c.pos); v.scale.setScalar(R * 1.02); |
| +добавлено | v.uniforms.uBurst.value.set(R, tt, 1.5 * I * (1 - sstep(tt, 0.08, 0.45)), (c.seed % 89) * 0.37); |
| +добавлено | v.uniforms.uBurstPos.value.copy(c.pos); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | flare(c, d, p, tt, lx, dt); |
| +добавлено | // the shock shell runs out and lets go, then the handle becomes an updraft over the arcade |
| +добавлено | const F = d.force; |
| +добавлено | if (F && F.type === 4) { |
| +добавлено | d.shockR += dt * 14; |
| +добавлено | F.p[0] = d.shockR; F.p[2] = 60 * p.flare * Math.max(0, 1 - d.shockR / 8); |
| +добавлено | if (d.shockR > 8) { F.type = 1; F.pos.copy(d.G); F.dir.set(0, 1, 0); F.p[0] = 1.1 * p.flare * p.size; F.p[1] = 3; F.p[2] = 2.4; F.p[3] = 0.6; F.w = 4 * p.flare * p.size; } |
| +добавлено | } |
| +добавлено | if (F && F.type === 1) F.p[2] = 2.4 * (1 - lx); |
| +добавлено | const G = d.G, fl = p.flare * p.size; |
| +добавлено | if (L) { |
| +добавлено | L.pos.copy(G).setY(G.y + 1.4 * fl + 1.5 * sstep(tt, 0, 1.2)); |
| +добавлено | const flick = 0.85 + 0.15 * Math.sin(c.age * 29 + c.seed) * Math.sin(c.age * 11.3); |
| +добавлено | L.intensity = (100 * Math.exp(-tt / 0.14) + 34 * (1 - lx) ** 1.5 * flick) * I; |
| +добавлено | } |
| +добавлено | if (lx < 0.8) { |
| +добавлено | motes.pos.copy(G).setY(G.y + 0.05); motes.radius = 1.4 * fl; motes.vel.set(0, 1.4, 0); motes.rate(60 * p.sparks * (1 - lx), dt); |
| +добавлено | ember.pos.copy(G).setY(G.y + 0.1); ember.radius = 1.2 * fl; ember.vel.set(0, 1.2, 0); ember.rate(60 * p.sparks * (1 - lx), dt); |
| +добавлено | } |
| +добавлено | for (const dm of fx.dummies) if (Math.hypot(dm.pos.x - G.x, dm.pos.z - G.z) < 2 * fl) dm.status.burn = Math.max(dm.status.burn, 0.6 * (1 - lx)); |
| +добавлено | focus(2.5); |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | // the sun lands: the burst, the eruption and the arcade, the shock, the world's response |
| +добавлено | function slam(c, p, d) { |
| +добавлено | const I = p.intensity, fl = p.flare * p.size, pos = c.pos; |
| +добавлено | const pool = ARENA.pool; |
| +добавлено | d.inPool = Math.abs(pos.x - pool.center.x) < pool.half.x && Math.abs(pos.z - pool.center.y) < pool.half.y; |
| +добавлено | d.G.set(pos.x, d.inPool ? pool.water : 0, pos.z); |
| +добавлено | d.age = 0; d.ribT = 0; |
| +добавлено | const G = d.G; |
| +добавлено | fx.flash(0.35 * I); |
| +добавлено | fx.rig.shake(0.5, 12); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(2.5, 0.08); fx.rig.hitstop(0.06); } |
| +добавлено | // the polarity line runs along the flight; the arcade's loops span across it |
| +добавлено | d.P.set(c.dir.x, 0, c.dir.z); |
| +добавлено | if (d.P.lengthSq() < 1e-6) d.P.set(0, 0, -1); |
| +добавлено | d.P.normalize(); d.Q.set(-d.P.z, 0, d.P.x); |
| +добавлено | layout(d, fl, 0); |
| +добавлено | takeStrands(d, MAXSTR); |
| +добавлено | for (let k = 0; k < d.nStr; k++) d.sl[k] = k < ARC * AS ? Math.floor(k / AS) : ROPE; |
| +добавлено | // each arcade thread's place across its flux tube, stratified core -> rim (strand 0 is the haze) |
| +добавлено | for (let k = 0; k < Math.min(d.nStr, ARC * AS); k++) if (k % AS) d.ss[k] = (k % AS - 1 + fx.rand()) / (AS - 1); |
| +добавлено | d.nLoop = NL; |
| +добавлено | // the sun bursts |
| +добавлено | d.burst = null; |
| +добавлено | for (const v of bursts) if (!v.busy) { d.burst = v; break; } |
| +добавлено | if (d.burst) { d.burst.busy = true; d.burst.visible = true; d.burst.position.copy(pos); d.burst.scale.setScalar(0.4); d.burst.uniforms.uBurst.value.set(0.35, 0, 0, 0); d.burst.uniforms.uBurstPos.value.copy(pos); } |
| +добавлено | spark.pos.copy(pos); spark.radius = 0.3; spark.vel.set(0, 2, 0); spark.burst(30 * p.sparks); |
| +добавлено | plasma.pos.copy(pos); plasma.radius = 0.5; plasma.vel.set(0, 1.5, 0); plasma.radial = 2.5; plasma.burst(160 * p.sparks); plasma.radial = 0.3; |
| +добавлено | motes.pos.copy(G).setY(G.y + 0.05); motes.radius = 1.6 * fl; motes.vel.set(0, 3, 0); motes.spread = 1.4; motes.burst(90 * p.sparks); motes.spread = 0.6; |
| +добавлено | SHOCK.pos.copy(G).setY(G.y + 0.02); SHOCK.radius0 = 0.5; SHOCK.radius1 = 7 * fl; SHOCK.duration = 0.7; SHOCK.thickness = 0.7; SHOCK.distortion = 0.04 * p.haze; SHOCK.intensity = 0.25 * I; SHOCK.ground = true; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | SHOCK.pos.copy(pos); SHOCK.radius0 = 0.3; SHOCK.radius1 = 4.5 * fl; SHOCK.duration = 0.45; SHOCK.thickness = 0.9; SHOCK.distortion = 0.09 * p.haze; SHOCK.intensity = 0; SHOCK.ground = false; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | // the floor: a scorched, glowing kernel where it landed |
| +добавлено | splat(G.x, G.z, 1.2 * fl, 0.9, 0.35, 0, 'noise', c.seed); |
| +добавлено | splat(G.x, G.z, 0.9 * fl, 0, 0, 1.6 * p.glow, 'radial', c.seed + 2); |
| +добавлено | fx.impacts.add('holy', _a.copy(G).setY(G.y + 0.8), 3.2 * fl, 1.3 * p.glow, 4, lightCol); |
| +добавлено | fx.impacts.add('scorch', G, 1.6 * fl, 0.6, 6, glowCol); |
| +добавлено | d.force = fx.forces.acquire(); |
| +добавлено | if (d.force) { const F = d.force; F.type = 4; F.pos.copy(pos); F.p[0] = 0.3; F.p[1] = 1.2; F.p[2] = 60 * p.flare; F.p[3] = 0; d.shockR = 0.3; } |
| +добавлено | if (d.light) { d.light.range = 12 * Math.sqrt(fl); d.light.pos.copy(G).setY(G.y + 1.4); } |
| +добавлено | // the two flare ribbons light the arena from the floor (point lights: a line light's glint on |
| +добавлено | // the polished floor reads as a hard neon bar) |
| +добавлено | for (let r = 0; r < 2; r++) { |
| +добавлено | const h = fx.lights.acquire(); |
| +добавлено | d.rib[r] = h; |
| +добавлено | if (h) { h.type = 0; h.range = 6 * Math.sqrt(fl); h.intensity = 0; h.shadow = false; h.color.copy(lightCol); } |
| +добавлено | } |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dist = Math.hypot(dm.pos.x - pos.x, dm.pos.z - pos.z), reach = 3.2 * fl; |
| +добавлено | if (dist > reach) continue; |
| +добавлено | dm.status.burn = Math.max(dm.status.burn, 1 - dist / reach + 0.3); |
| +добавлено | dm.hit(dist > 0.05 ? _a.subVectors(dm.pos, pos).setY(0) : c.dir, 0.9 * (1 - dist / reach) + 0.2); |
| +добавлено | } |
| +добавлено | // the pool lights up from inside and ripples |
| +добавлено | const px = clamp(pos.x, pool.center.x - pool.half.x, pool.center.x + pool.half.x), pz = clamp(pos.z, pool.center.y - pool.half.y, pool.center.y + pool.half.y); |
| +добавлено | if (Math.hypot(px - pos.x, pz - pos.z) < 3.5) { fx.pool.ripple(px, pz, 0.05, 1.2); fx.pool.flash(lightCol, 0.8 * I); } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // ribbon half-separation at age tt: the ribbons spread apart as reconnection climbs |
| +добавлено | function ribW(fl, tt) { return fl * (0.45 + 0.55 * sstep(tt, 0.1, 2.6)); } |
| +добавлено | |
| +добавлено | // feet, apex direction, height, tube radius and onset of every loop (arcade by order of formation) |
| +добавлено | function layout(d, fl, tt) { |
| +добавлено | const G = d.G, Pv = d.P, Qv = d.Q; |
| +добавлено | // a packed row of loops (TRACE / AIA post-flare arcades): the whole arcade leans one way out of |
| +добавлено | // its loops' planes, each loop with its own height, span and a little in-plane asymmetry; they |
| +добавлено | // form one after another along the row |
| +добавлено | const lean = 0.2 + 0.25 * fx.rand(); |
| +добавлено | for (let j = 0; j < ARC; j++) { |
| +добавлено | const u = j / (ARC - 1); |
| +добавлено | const along = (u - 0.5) * 2.8 * fl + 0.06 * fl * (fx.rand() - 0.5); |
| +добавлено | const w = fl * (0.4 + 0.14 * fx.rand() + 0.1 * u), sh = 0.18 * fl * (1 - u); |
| +добавлено | d.fa[j].copy(G).addScaledVector(Pv, along + sh).addScaledVector(Qv, -w); |
| +добавлено | d.fb[j].copy(G).addScaledVector(Pv, along - sh).addScaledVector(Qv, w); |
| +добавлено | d.fu[j].copy(UP).addScaledVector(Pv, lean + 0.1 * (fx.rand() - 0.5)).addScaledVector(Qv, 0.16 * (fx.rand() - 0.5)).normalize(); |
| +добавлено | d.fh[j] = fl * (0.9 + 0.3 * u + 0.45 * fx.rand()); |
| +добавлено | d.fw[j] = 0.06 * fl; |
| +добавлено | d.fdelay[j] = 0.1 + 0.11 * j; |
| +добавлено | } |
| +добавлено | // the flux rope: anchored at both ends of the line, lying along it above the arcade |
| +добавлено | d.fa[ROPE].copy(G).addScaledVector(Pv, -2.1 * fl).addScaledVector(Qv, 0.15 * fl); |
| +добавлено | d.fb[ROPE].copy(G).addScaledVector(Pv, 2.1 * fl).addScaledVector(Qv, -0.15 * fl); |
| +добавлено | d.fu[ROPE].copy(UP).addScaledVector(Qv, 0.12).normalize(); |
| +добавлено | d.fdelay[ROPE] = 0; |
| +добавлено | } |
| +добавлено | |
| +добавлено | function flare(c, d, p, tt, lx, dt) { |
| +добавлено | const fl = p.flare * p.size, fade = 1 - sstep(lx, 0.55, 1), I = p.intensity; |
| +добавлено | const xr = tt / 1.5; // the eruption's clock |
| +добавлено | for (let k = 0; k < d.nStr; k++) { |
| +добавлено | const h = d.str[k], j = d.sl[k], sd = d.ss[k]; |
| +добавлено | const A = d.fa[j], B = d.fb[j], Uv = d.fu[j]; |
| +добавлено | if (j === ROPE) { |
| +добавлено | // lifts off, swells, accelerates away and fades: a twisted prominence of fine threads |
| +добавлено | const hgt = fl * (0.6 + 7.5 * Math.pow(Math.min(xr, 1.3), 1.8)); |
| +добавлено | h.p0.set(A.x, A.y, A.z, 0.06 * fl * (1 + xr) * (1.4 - 0.8 * sd)); // thick, merging threads: a body of plasma, not wires |
| +добавлено | h.p1.set(B.x, B.y, B.z, hgt); |
| +добавлено | h.p2.set(Uv.x, Uv.y, Uv.z, fl * (0.18 + 0.5 * xr)); |
| +добавлено | h.p3.set(sd, 1.6, 0.55, 0.55); |
| +добавлено | h.color.copy(promCol).lerp(coreCol, 0.6 * (1 - sd * sd)); // golden heart, red outer threads |
| +добавлено | h.intensity = 1.3 * (1 - 0.5 * sd * sd) * p.loops * I * sstep(tt, 0, 0.08) * (1 - sstep(xr, 0.4, 0.9)) * (0.7 + 0.3 * Math.sin(tt * 9 + sd * 20)); |
| +добавлено | continue; |
| +добавлено | } |
| +добавлено | // an arcade loop: a bundle of fine threads braided round its flux tube (sd: the thread's place, |
| +добавлено | // core 0 .. rim 1) and a faint haze strand; it forms, rises to its height, burns golden-white, |
| +добавлено | // then cools and drains. Hot golden-white cores; the faint outer threads run cooler and redder |
| +добавлено | const x = tt - d.fdelay[j], g = easeOutBack(clamp(x / 0.45, 0, 1)), on = sstep(x, 0, 0.1); |
| +добавлено | const heat = 1 - sstep(x, 0.8, 2.6), haze = k % AS === 0; |
| +добавлено | // threads thick enough to merge into a glowing tube with fine structure (AIA 171 loops), not wires |
| +добавлено | h.p0.set(A.x, A.y, A.z, (haze ? 0.42 : 0.009 + 0.016 * (1 - sd)) * fl); |
| +добавлено | h.p1.set(B.x, B.y, B.z, d.fh[j] * g * (1 + 0.06 * lx)); |
| +добавлено | h.p2.set(Uv.x, Uv.y, Uv.z, haze ? 0 : d.fw[j] * (1 + 0.5 * lx)); |
| +добавлено | h.p3.set(sd, (k % 7 === 3 ? -1 : 1) * (0.7 + 0.9 * ((k * 0.618) % 1)), 0.25, heat); |
| +добавлено | h.color.copy(glowCol).lerp(hotCol, heat * (1 - 0.8 * sd * sd)); |
| +добавлено | h.intensity = (haze ? 3 : 0.75 * (1 - 0.75 * sd * sd)) * p.loops * I * on * fade * (1 + 1.5 * Math.exp(-Math.max(x, 0) / 0.2)) * (0.75 + 0.25 * Math.sin(tt * (3 + 4 * sd) + k * 1.7)); |
| +добавлено | } |
| +добавлено | // the ribbons: burning lines on the floor under the footpoints, spreading apart |
| +добавлено | const G = d.G, Pv = d.P, Qv = d.Q, w = ribW(fl, tt), Lr = 1.6 * fl; |
| +добавлено | for (let r = 0; r < 2; r++) { |
| +добавлено | const lt = d.rib[r], sgn = r ? 1 : -1; |
| +добавлено | if (!lt) continue; |
| +добавлено | lt.pos.copy(G).addScaledVector(Qv, sgn * w).setY(G.y + 0.5); |
| +добавлено | lt.intensity = 18 * I * p.glow * sstep(tt, 0.05, 0.3) * (1 - sstep(lx, 0.35, 0.95)) * (0.85 + 0.15 * Math.sin(tt * 13 + r * 2)); |
| +добавлено | } |
| +добавлено | d.ribT -= dt; |
| +добавлено | if (d.ribT <= 0 && lx < 0.6) { |
| +добавлено | d.ribT = 0.12; |
| +добавлено | // each ribbon burns in ragged, wandering kernels (a few short dashes along it), not one line |
| +добавлено | for (let r = 0; r < 2; r++) { |
| +добавлено | const sgn = r ? 1 : -1, u0 = (fx.rand() - 0.65) * 2 * Lr, len = (0.25 + 0.5 * fx.rand()) * Lr; |
| +добавлено | const q0 = sgn * w + 0.12 * fl * (fx.rand() - 0.5), q1 = sgn * w + 0.12 * fl * (fx.rand() - 0.5); |
| +добавлено | _a.copy(G).addScaledVector(Pv, u0).addScaledVector(Qv, q0); |
| +добавлено | _b.copy(G).addScaledVector(Pv, u0 + len).addScaledVector(Qv, q1); |
| +добавлено | splat(_a.x, _a.z, 0.3, 0.12, 0.04, (0.5 + 0.9 * fx.rand()) * p.glow * (1 - lx), 'line', c.seed + 40 + r + (d.frame & 31), (0.05 + 0.07 * fx.rand()) * fl, _b.x, _b.z); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | if (lx < 0.6) { |
| +добавлено | // footpoint kernels spit sparks, two feet a frame |
| +добавлено | for (let s = 0; s < 2; s++) { |
| +добавлено | const f = (d.frame * 2 + s) % (ARC * 2), j = f >> 1; |
| +добавлено | if (tt < d.fdelay[j]) continue; |
| +добавлено | footSpark.pos.copy(f & 1 ? d.fb[j] : d.fa[j]).setY(d.G.y + 0.05); |
| +добавлено | footSpark.vel.set(0, 2.2, 0); |
| +добавлено | footSpark.rate(160 * p.sparks * (1 - lx / 0.6), dt); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | if (tt > 0.6 && lx < 0.85 && d.nStr) { |
| +добавлено | // coronal rain: drips from near the top of a cooling arcade loop |
| +добавлено | const k = Math.floor(fx.rand() * Math.min(d.nStr, ARC * AS)), h = d.str[k]; |
| +добавлено | if (h && tt > d.fdelay[d.sl[k]] + 0.5) { |
| +добавлено | loopPoint(h, 0.3 + 0.4 * fx.rand(), rain.pos); |
| +добавлено | rain.vel.set(0, -0.5, 0); |
| +добавлено | rain.rate(110 * p.loops * (1 - lx), dt); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { U, COMMON_GLSL, NOISE_GLSL, FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL, FX_BLENDING } from '../../vfx/common.js'; |
| +добавлено | |
| +добавлено | // Shaders for Supernova (the star itself is Solar Flare's impostor, imported from there). |
| +добавлено | // |
| +добавлено | // Explosion mass (isolated so it can move onto the shared FluidVolume later): a raymarched |
| +добавлено | // pyroclastic fireball (Volumes.js). Reference: high-speed nuclear-test film (Upshot-Knothole, |
| +добавлено | // Trinity) and large fuel-air fireballs: a white-hot ball whose surface boils into lobes, |
| +добавлено | // optically thick so you see the surface temperature (bright lobes, cooler lanes between them), |
| +добавлено | // cooling outside-in white -> yellow -> orange -> dull red, swallowed by soot that the sun |
| +добавлено | // lights from above while it climbs. Emission = sigma * L(T) with L ~ blackbody(T) (T/Tref)^4, |
| +добавлено | // so nothing is a flat colour and the hot core keeps its structure. |
| +добавлено | // Shell (fx pass, half res): one camera-facing quad per explosion, ray-traced analytically. |
| +добавлено | // - the condensation dome (Wilson cloud, as in the Beirut 2020 footage): a thin lit misty shell |
| +добавлено | // riding the shock front for a fraction of a second, patchy, both walls sampled once at the |
| +добавлено | // middle of their chord (so it is limb-brightened like a real shell of fog). |
| +добавлено | // - the flash glare and a few soft shafts at the instant of the bang. |
| +добавлено | // - the pulsar's wind nebula: a wispy blue-white glow around the remnant. |
| +добавлено | // The quad sits just in front of the sphere so the depth test never cuts the front wall; with |
| +добавлено | // the camera inside it becomes a full-screen quad at the near plane. |
| +добавлено | // Arcs (AnalyticRibbons, fx pass): mode 1 the condensation ring (a tilted circle of lit mist, |
| +добавлено | // clumped around its length, refracting, with a shock-heated glow only in its first instant), |
| +добавлено | // mode 0 a pulsar beam: a soft searchlight shaft through the smoke. |
| +добавлено | |
| +добавлено | // --- explosion mass --------------------------------------------------------------------------- |
| +добавлено | const FIRE_L = /* glsl */` |
| +добавлено | vec3 fireL(float T, float Tref) { float k = T / Tref; k *= k; return blackbody(T) * (k * k * smoothstep(700.0, 1000.0, T)); } |
| +добавлено | `; |
| +добавлено | export const FIREBALL_GLSL = FIRE_L + /* glsl */` |
| +добавлено | uniform vec4 uFb; // radius m, age s, intensity, seed |
| +добавлено | uniform vec4 uFb2; // core temperature K, soot, fade, vortex ring 0..1 |
| +добавлено | uniform vec3 uFbPos; |
| +добавлено | uniform vec3 uFbGlow; // late glow of the ionised filaments |
| +добавлено | void volumeSample(vec3 pw, vec3 pl, out float sigma, out vec3 albedo, out vec3 emission) { |
| +добавлено | sigma = 0.0; albedo = vec3(0.0); emission = vec3(0.0); |
| +добавлено | float R = uFb.x, age = uFb.y, sd = uFb.w, m = uFb2.w; |
| +добавлено | vec3 q = pw - uFbPos; |
| +добавлено | // as it climbs the ball turns into a vortex ring (the mushroom cap): distance is measured from |
| +добавлено | // the ring's core circle in the poloidal plane, and the noise is rolled around that core (up the |
| +добавлено | // middle, out over the top, down the outside) |
| +добавлено | float rh = length(q.xz), a = 0.55 * R * m, b = R * (1.0 - 0.4 * m); |
| +добавлено | vec2 w = vec2(rh - a, q.y * (1.0 + 0.35 * m)); |
| +добавлено | float r = length(w) / b; |
| +добавлено | if (r > 1.45) return; |
| +добавлено | float ang = -age * 1.7 * m, ca = cos(ang), sa = sin(ang); |
| +добавлено | vec2 wr = vec2(ca * w.x - sa * w.y, sa * w.x + ca * w.y); |
| +добавлено | vec3 hd = rh > 1e-4 ? vec3(q.x, 0.0, q.z) / rh : vec3(1.0, 0.0, 0.0); |
| +добавлено | vec3 qr = hd * (wr.x + a) + vec3(0.0, wr.y, 0.0); |
| +добавлено | // the noise rides the expanding body (q / R); it boils by drifting through a slow curl field |
| +добавлено | vec3 np = qr / R * 0.4 + vec3(sd, -0.1 * age, sd * 0.61); |
| +добавлено | np += curl3(np * 0.5 + vec3(0.0, 0.05 * age, sd)) * 0.16; |
| +добавлено | float n1 = noise3(np); |
| +добавлено | float detail = 1.0 - smoothstep(0.25, 0.7, gVolDt / R); |
| +добавлено | float n2 = noise3(np * 2.1 + vec3(0.37, 0.11, 0.73)); |
| +добавлено | float d = (1.0 - r) + 0.8 * (n1 - 0.5) + 0.22 * (n2 - 0.5) * detail; |
| +добавлено | d *= smoothstep(-0.2, 0.6, pw.y); // squashes onto the floor rather than cutting through it |
| +добавлено | if (d <= 0.0) return; |
| +добавлено | float body = smoothstep(0.0, 0.25, d); |
| +добавлено | // temperature: deep = hot; the surface cools first; lobes stay hotter than the lanes between them |
| +добавлено | float heat = clamp(d * 2.5 + 0.6 * (n2 - 0.5) + 0.4 * (n1 - 0.5), 0.0, 1.0); |
| +добавлено | float Ts = 1000.0 + 2000.0 * exp(-age / 0.5); |
| +добавлено | float Tc = 1500.0 + (uFb2.x - 1500.0) * exp(-age / 0.7); |
| +добавлено | float T = mix(Ts, Tc, heat * heat); |
| +добавлено | float sf = body * 3.5 / R; |
| +добавлено | // soot: where it has cooled, thickening with age |
| +добавлено | float cold = 1.0 - smoothstep(1100.0, 1700.0, T); |
| +добавлено | float soot = body * cold * smoothstep(0.7, 2.0, age) * uFb2.y; |
| +добавлено | float ss = soot * 7.0 / R; |
| +добавлено | sigma = (sf * (1.0 - 0.75 * cold) + ss) * uFb2.z; |
| +добавлено | emission = sf * uFb2.z * fireL(T, 2500.0) * uFb.z; |
| +добавлено | // ionised filaments that glow on, thin and sparse, in the cooling cloud |
| +добавлено | float ridge = 1.0 - abs(2.0 * n2 - 1.0); |
| +добавлено | ridge *= ridge; ridge *= ridge; ridge *= ridge; |
| +добавлено | emission += uFbGlow * (ridge * body * smoothstep(0.6, 1.6, age) * uFb2.z * uFb.z * 0.35 / R); |
| +добавлено | albedo = vec3(0.028, 0.025, 0.023) * (ss / max(sigma, 1e-4)); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // a pool of n fireball volumes { v, busy } (Volumes.add only works in create()) |
| +добавлено | export function createFireballs(fx, n) { |
| +добавлено | return Array.from({ length: n }, () => { |
| +добавлено | const v = fx.volumes.add({ |
| +добавлено | shape: 'sphere', glsl: FIREBALL_GLSL, maxSteps: 28, |
| +добавлено | uniforms: { uFb: { value: new THREE.Vector4(1, 0, 0, 0) }, uFb2: { value: new THREE.Vector4(9000, 1, 1, 0) }, uFbPos: { value: new THREE.Vector3() }, uFbGlow: { value: new THREE.Color(1, 0.3, 0.2) } }, |
| +добавлено | }); |
| +добавлено | v.visible = false; v.g = 0.3; |
| +добавлено | return { v, busy: false }; |
| +добавлено | }); |
| +добавлено | } |
| +добавлено | // pos (centre), radius m, age s, intensity, core temperature K, soot 0..1, fade 0..1, seed, |
| +добавлено | // filament glow colour, ring (0 a ball .. 1 a rolling vortex ring) |
| +добавлено | export function placeFireball(f, pos, R, age, I, Tcore, soot, fade, seed, glow, ring) { |
| +добавлено | const v = f.v, u = v.uniforms; |
| +добавлено | v.visible = R > 1e-3 && fade > 1e-3; |
| +добавлено | v.position.copy(pos); |
| +добавлено | v.scale.setScalar(R * 1.45); |
| +добавлено | u.uFb.value.set(R, age, I, seed); |
| +добавлено | u.uFb2.value.set(Tcore, soot, fade, ring); |
| +добавлено | u.uFbPos.value.copy(pos); |
| +добавлено | u.uFbGlow.value.copy(glow); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- shell, glare, wind nebula ---------------------------------------------------------------- |
| +добавлено | const NEB_U = /* glsl */` |
| +добавлено | uniform vec4 uNebA; // centre xyz, condensation shell radius m (0 = none) |
| +добавлено | uniform vec4 uNebB; // age since the bang s, emission intensity, shell thickness (fraction), seed |
| +добавлено | uniform vec4 uNebC; // glare, shafts, glow extent m, wind nebula |
| +добавлено | uniform vec4 uNebD; // condensation opacity, blast light on the shell, -, - |
| +добавлено | uniform vec3 uNebHot; |
| +добавлено | uniform vec3 uNebShock; |
| +добавлено | uniform vec3 uNebCore; |
| +добавлено | `; |
| +добавлено | |
| +добавлено | const NEB_VERT = FX_VERT_HEAD + COMMON_GLSL + NEB_U + /* glsl */` |
| +добавлено | out vec3 vW; |
| +добавлено | void main() { |
| +добавлено | vW = vec3(0.0); |
| +добавлено | float R = max(uNebA.w, uNebC.z); |
| +добавлено | vec3 c = uNebA.xyz, toC = c - cameraPosition; |
| +добавлено | float D = length(toC); |
| +добавлено | if (R <= 0.0) { gl_Position = vec4(2.0, 2.0, 2.0, 1.0); return; } |
| +добавлено | if (D < R * 1.02) { |
| +добавлено | // inside: full screen at the near plane, world rays through the corners |
| +добавлено | vec4 w = inverse(projectionMatrix * viewMatrix) * vec4(position.xy, 0.5, 1.0); |
| +добавлено | vW = w.xyz / w.w; |
| +добавлено | gl_Position = vec4(position.xy, -0.999, 1.0); |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | vec3 f = toC / D, r = normalize(cross(f, abs(f.y) < 0.99 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0))), u = cross(r, f); |
| +добавлено | float Dn = D - R * 0.98; |
| +добавлено | vec3 p = cameraPosition + f * Dn + (r * position.x + u * position.y) * (Dn * R / sqrt(D * D - R * R) * 1.02); |
| +добавлено | vW = p; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(p, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const NEB_FRAG = FX_FRAG_HEAD + COMMON_GLSL + NOISE_GLSL + FX_OUT_GLSL + NEB_U + /* glsl */` |
| +добавлено | uniform mat4 projectionMatrix; |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | in vec3 vW; |
| +добавлено | float viewDepth(vec3 p) { return -(viewMatrix * vec4(p, 1.0)).z; } |
| +добавлено | void main() { |
| +добавлено | vec3 c = uNebA.xyz; |
| +добавлено | float R = uNebA.w, age = uNebB.x, L = uNebC.z, sd = uNebB.w; |
| +добавлено | vec3 rd = normalize(vW - cameraPosition); |
| +добавлено | float tc = dot(c - cameraPosition, rd); |
| +добавлено | vec3 dv = cameraPosition + rd * tc - c; // closest approach, from the centre |
| +добавлено | float rho = length(dv); |
| +добавлено | if (rho > max(R, L)) discard; |
| +добавлено | float zs = sceneViewZ(); |
| +добавлено | float zc = viewDepth(c + dv); |
| +добавлено | float visC = clamp((zs - zc) / 0.6, 0.0, 1.0) * step(0.0, tc); |
| +добавлено | vec3 em = vec3(0.0), media = vec3(0.0); |
| +добавлено | float alpha = 0.0, zm = max(zc, 0.05); |
| +добавлено | |
| +добавлено | // the condensation dome: front and back walls of a thin patchy shell of lit fog |
| +добавлено | float th = uNebB.z, Ri = R * (1.0 - th); |
| +добавлено | if (R > 0.0 && rho < R && uNebD.x > 0.0) { |
| +добавлено | float ho = sqrt(R * R - rho * rho), hi = rho < Ri ? sqrt(Ri * Ri - rho * rho) : 0.0; |
| +добавлено | float tm = 0.5 * (ho + hi), seg = min((ho - hi) / (R * th), 5.0); |
| +добавлено | float keep = 1.0; |
| +добавлено | for (int k = 0; k < 2; k++) { |
| +добавлено | float s = k == 0 ? -1.0 : 1.0; |
| +добавлено | if (tc + s * tm < 0.05) continue; // that wall is behind the camera |
| +добавлено | vec3 pw = c + dv + rd * (s * tm), n = (pw - c) / R; |
| +добавлено | float z = viewDepth(pw); |
| +добавлено | float vis = clamp((zs - z) / 0.4, 0.0, 1.0); |
| +добавлено | if (vis <= 0.0) continue; |
| +добавлено | vec3 np = n * 1.1 + vec3(sd, age * 0.25, sd * 0.7); |
| +добавлено | float f1 = noise3(np), f2 = noise3(np * 2.7 + 0.31); |
| +добавлено | float fog = smoothstep(0.45, 0.85, f1 * 0.7 + f2 * 0.45); |
| +добавлено | // denser low down, where the dome meets the ground's moist air |
| +добавлено | fog *= 0.55 + 0.45 * smoothstep(0.6, -0.4, n.y); |
| +добавлено | float a = clamp(seg * 0.1 * fog * uNebD.x, 0.0, 0.6) * vis; |
| +добавлено | vec3 lit = uSunColor * (0.18 + 0.4 * max(dot(n, uSunDir), 0.0)) + uAmbient * 2.2 + uNebShock * uNebD.y; |
| +добавлено | media += lit * a * keep; |
| +добавлено | alpha = alpha + a * (1.0 - alpha); |
| +добавлено | if (k == 0) { keep = 1.0 - a; zm = max(z, 0.05); } |
| +добавлено | } |
| +добавлено | media /= max(alpha, 1e-4); |
| +добавлено | } |
| +добавлено | // the flash: glare and a few soft shafts from the centre |
| +добавлено | if (uNebC.x > 0.0 || uNebC.y > 0.0) { |
| +добавлено | vec3 dir = dv / max(rho, 1e-4); |
| +добавлено | float a1 = noise3(dir * 2.2 + sd), a2 = noise3(dir * 5.1 + sd * 1.3); |
| +добавлено | float x = rho / L; |
| +добавлено | float shafts = (1.5 * a1 * a1 * a1 + 2.0 * pow(a2, 5.0)) * (exp(-x / 0.1) + 0.25 * exp(-x / 0.3)) * smoothstep(1.0, 0.5, x); |
| +добавлено | float glare = 2.0 * exp(-x / 0.015) + 0.5 * exp(-x / 0.06) + 0.12 * exp(-x / 0.25); |
| +добавлено | em += uNebHot * ((glare * uNebC.x + shafts * uNebC.y) * visC); |
| +добавлено | } |
| +добавлено | // the pulsar's wind nebula |
| +добавлено | if (uNebC.w > 0.0) { |
| +добавлено | float q = rho / (0.3 * L); |
| +добавлено | float wisp = noise3(dv / L * 2.4 + vec3(sd, -age * 0.15, age * 0.06)); |
| +добавлено | em += uNebCore * (exp(-q * q) * (0.25 + 1.8 * wisp * wisp) * uNebC.w * visC); |
| +добавлено | } |
| +добавлено | fxOut(media, alpha, em * uNebB.y, vec2(0.0), zm); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // n shell impostors, each { mesh, u, busy } |
| +добавлено | export function createShells(fx, n) { |
| +добавлено | const geo = new THREE.PlaneGeometry(2, 2); |
| +добавлено | return Array.from({ length: n }, () => { |
| +добавлено | const u = { |
| +добавлено | uNebA: { value: new THREE.Vector4() }, uNebB: { value: new THREE.Vector4(0, 1, 0.2, 0) }, |
| +добавлено | uNebC: { value: new THREE.Vector4() }, uNebD: { value: new THREE.Vector4() }, |
| +добавлено | uNebHot: { value: new THREE.Color(1, 1, 1) }, uNebShock: { value: new THREE.Color(0.5, 0.9, 1) }, uNebCore: { value: new THREE.Color(0.4, 0.6, 1) }, |
| +добавлено | }; |
| +добавлено | const mesh = new THREE.Mesh(geo, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: NEB_VERT, fragmentShader: NEB_FRAG, |
| +добавлено | uniforms: { ...U, ...u }, ...FX_BLENDING, side: THREE.DoubleSide, |
| +добавлено | })); |
| +добавлено | mesh.frustumCulled = false; |
| +добавлено | mesh.visible = false; |
| +добавлено | fx.scenes.fx.add(mesh); |
| +добавлено | return { mesh, u, busy: false }; |
| +добавлено | }); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // ring: p0 = centre, radius p1 = in-plane axis e1, width p2 = in-plane axis e2, wobble p3 = seed, 1, opacity, hot glow |
| +добавлено | // beam: p0 = start, width p1 = end, width p2 = - p3 = seed, 0, -, - |
| +добавлено | export const ARC_PATH = /* glsl */` |
| +добавлено | vec3 ribbonPath(float t, vec4 p0, vec4 p1, vec4 p2, vec4 p3, out float width) { |
| +добавлено | if (p3.y > 0.5) { |
| +добавлено | float a = t * 6.2831853; |
| +добавлено | float wob = 1.0 + p2.w * (0.6 * sin(a * 3.0 + p3.x * 6.2831) + 0.4 * sin(a * 7.0 + p3.x * 11.0)); |
| +добавлено | width = p1.w; |
| +добавлено | return p0.xyz + (p1.xyz * cos(a) + p2.xyz * sin(a)) * (p0.w * wob); |
| +добавлено | } |
| +добавлено | width = mix(p0.w, p1.w, t); |
| +добавлено | return mix(p0.xyz, p1.xyz, t); |
| +добавлено | }`; |
| +добавлено | export const ARC_SHADE = NOISE_GLSL + /* glsl */` |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | vec3 ribbonShade(RibbonFrag r, out vec3 media, out float alpha, out float distortion) { |
| +добавлено | media = vec3(0.0); alpha = 0.0; distortion = 0.0; |
| +добавлено | float y = r.uv.y; |
| +добавлено | if (r.p3.y > 0.5) { |
| +добавлено | // condensation ring: patchy lit mist, thicker on its inner side, refracting at its edge |
| +добавлено | float a = r.uv.x * 6.2831853; |
| +добавлено | vec3 d = vec3(cos(a), sin(a), r.p3.x); |
| +добавлено | float n = noise3(d * 1.1 + vec3(0.0, y * 0.12, uTime * 0.05)), n2 = noise3(d * 3.1 + vec3(0.5, y * 0.3, 0.2)); |
| +добавлено | float clump = smoothstep(0.25, 0.75, 0.75 * n + 0.45 * n2); |
| +добавлено | float prof = exp(-y * y * 2.2) * (0.75 + 0.25 * sin(y * 3.0 + n2 * 6.0)); |
| +добавлено | alpha = clamp(prof * clump * r.p3.z, 0.0, 0.9); |
| +добавлено | media = uSunColor * 0.42 + uAmbient * 2.2 + r.color * (0.6 * r.p3.w); |
| +добавлено | distortion = y * exp(-3.0 * y * y) * 0.08 * r.p3.z; |
| +добавлено | return r.color * (r.p3.w * exp(-y * y * 5.0) * (0.3 + clump)); |
| +добавлено | } |
| +добавлено | // a searchlight through smoke: soft, brightest near the star, grainy with the smoke it lights |
| +добавлено | float t = r.uv.x; |
| +добавлено | float along = smoothstep(0.0, 0.03, t) * pow(1.0 - t, 1.6); |
| +добавлено | float smoke = 0.45 + 0.9 * noise3(vec3(t * 2.4 - uTime * 0.2, y * 0.15, r.p3.x)); |
| +добавлено | vec3 c = r.color; |
| +добавлено | return (mix(c, vec3(max(c.r, max(c.g, c.b))), 0.5) * exp(-y * y * 9.0) * (0.3 + 0.7 * exp(-t * 6.0)) + c * exp(-y * y * 1.8) * 0.35 * smoke) * along; |
| +добавлено | }`; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { ARENA } from '../../vfx/common.js'; |
| +добавлено | import { createStars } from '../light/solar-flare.glsl.js'; |
| +добавлено | import { createFireballs, placeFireball, createShells, ARC_PATH, ARC_SHADE } from './supernova.glsl.js'; |
| +добавлено | |
| +добавлено | // Supernova (SPELLBOOK hybrid: light + fire + void). Over the target a red giant is born: motes |
| +добавлено | // pour into a seed of light over a holy sigil and it swells into a ray-traced star (giant |
| +добавлено | // convection cells, spots, deep limb darkening, a streamered corona) shedding a stellar wind, then |
| +добавлено | // pulsates, faster and harder. It implodes: the core falls in with an accelerating collapse and |
| +добавлено | // heats from red to white-blue, a contracting distortion shell and an attractor pull the wind, void |
| +добавлено | // motes and dark drain smoke in, the sigil is dragged inwards, the lights die, the gravitational |
| +добавлено | // lens (Lens.js, slot 1 preferred) bends the arena around it and for an instant it is a black hole. |
| +добавлено | // Then it explodes, matched to real high-speed explosion film: hitstop and a flash, a white-hot |
| +добавлено | // fireball that boils into lobes and cools outside-in through yellow, orange and red into |
| +добавлено | // sunlit soot as it climbs (the explosion mass: see supernova.glsl.js, kept separate so it can move |
| +добавлено | // onto the shared FluidVolume), a condensation dome riding the shock front for a fraction of a |
| +добавлено | // second (Beirut 2020), a misty condensation ring racing out, a refracting blast sphere, a dust skirt |
| +добавлено | // across the floor, blackbody ejecta arcing down and scorching, sparks that bounce. In the smoke the |
| +добавлено | // remnant pulsar sweeps two searchlight beams round and gently lenses the cloud behind it, while |
| +добавлено | // ionised filaments glow on in the cooling gas. Dummies lean into the collapse and are thrown as the |
| +добавлено | // front reaches them; the pool swirls, then ripples and flashes; the floor is scorched. |
| +добавлено | |
| +добавлено | const MAX_N = 2; // live supernovae at once (each holds a star, a fireball, a shell, four arcs) |
| +добавлено | const TI = 0.55; // implosion length (s) at the start of the impact phase; then the bang |
| +добавлено | const UP = new THREE.Vector3(0, 1, 0); |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _v = new THREE.Vector3(), _fp = new THREE.Vector3(); |
| +добавлено | const _white = new THREE.Color(1, 1, 1), _fire = new THREE.Color(1, 0.42, 0.13); |
| +добавлено | const clamp = THREE.MathUtils.clamp, sstep = THREE.MathUtils.smoothstep; |
| +добавлено | const easeOutBack = (x) => 1 + 2.70158 * (x - 1) ** 3 + 1.70158 * (x - 1) ** 2; |
| +добавлено | const SPLAT = { x: 0, z: 0, radius: 1, heat: 0, char: 0, glow: 0, pattern: 'radial', seed: 0, width: 0 }; |
| +добавлено | const SHOCK = { pos: new THREE.Vector3(), radius0: 0.3, radius1: 6, duration: 0.6, thickness: 0.6, distortion: 0.05, color: null, intensity: 0, ground: true }; |
| +добавлено | const SIGIL = { pos: new THREE.Vector3(), normal: new THREE.Vector3(0, 1, 0), radius: 2, color: null, intensity: 0, reveal: 0, rotation: 0, style: 'holy', seed: 0 }; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'supernova', name: 'Supernova', element: 'light', order: 30, hue: '#ff8a3a', |
| +добавлено | shape: 'area', radius: 4, |
| +добавлено | params: { |
| +добавлено | size: 1, intensity: 1, collapse: 1, blast: 1, fireball: 1, soot: 1, rays: 1, lensing: 1, pulsar: 1, sparks: 1, smoke: 1, granules: 3, |
| +добавлено | star: '#ffb468', limb: '#d42a08', shock: '#bfe9ff', filament: '#ff4a2a', void: '#8a3dff', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | size: [0.6, 1.6, 0.01], intensity: [0, 3, 0.01], collapse: [0, 2, 0.01], blast: [0.4, 2, 0.01], fireball: [0, 3, 0.01], soot: [0, 2, 0.01], |
| +добавлено | rays: [0, 3, 0.01], lensing: [0, 3, 0.01], pulsar: [0, 2, 0.01], sparks: [0, 3, 0.01], smoke: [0, 3, 0.01], granules: [1.5, 10, 0.1], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | betelgeuse: { star: '#ffb468', limb: '#d42a08', shock: '#bfe9ff', filament: '#ff4a2a', void: '#8a3dff', size: 1, granules: 3, pulsar: 1, collapse: 1, soot: 1 }, |
| +добавлено | 'blue hypergiant': { star: '#eef3ff', limb: '#3d6dff', shock: '#ffffff', filament: '#ff4fb0', void: '#5a3dff', size: 1.2, granules: 5, pulsar: 1.4, soot: 0.7 }, |
| +добавлено | kilonova: { star: '#fff2c4', limb: '#ff9a10', shock: '#fff0c8', filament: '#ffc040', void: '#ff5a2a', size: 0.8, granules: 2.2, pulsar: 0, collapse: 1.5, rays: 1.6, soot: 1.4 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 1.8, impact: 1.4, linger: 3.4 }, |
| +добавлено | kinds: { |
| +добавлено | // stellar wind off the giant; the collapse sucks it back in |
| +добавлено | wind: { style: 'glow', drag: 1.2, buoyancy: 0.4, curl: 2, curlScale: 0.6, forceResponse: 1, life: [0.6, 1.4], size: [0.022, 0.006], stretch: 0.02, emissive: 3.5, color: ['#ffcf8a', '#ff4a14'], alpha: [1, 0], fadeIn: 0.1, fadeOut: 0.4 }, |
| +добавлено | // blackbody ejecta: white streaks cooling to orange as they arc down, scorching where they land |
| +добавлено | ejecta: { style: 'glow', drag: 0.45, gravity: 7, restitution: 0.25, friction: 0.5, forceResponse: 0.3, life: [0.8, 2.0], size: [0.04, 0.01], stretch: 0.025, emissive: 7, temp: [4500, 1200], alpha: [1, 0], fadeIn: 0.02, fadeOut: 0.3, paints: 'heat', paintAmount: 0.05 }, |
| +добавлено | // fallback fireball (no volume free): blackbody flame that turns to smoke |
| +добавлено | flame: { style: 'media', drag: 2.6, buoyancy: 1.6, curl: 2.4, curlScale: 0.6, forceResponse: 0.5, life: [0.45, 1.0], size: [0.3, 1.3], sizePow: 0.6, emissive: 1.3, temp: [4200, 800], toSmokeAt: 0.55, smokeAlbedo: 0.05, alpha: [0.8, 0], density: 7, distortion: 0.4, erosion: 1.5, fadeIn: 0.04, fadeOut: 0.3 }, |
| +добавлено | // the dust skirt the shock drives across the floor (lit, not glowing) |
| +добавлено | dust: { style: 'media', emissive: 0, drag: 1.6, buoyancy: 0.3, curl: 0.9, curlScale: 0.3, forceResponse: 0.8, life: [1.2, 2.4], size: [0.35, 1.8], sizePow: 0.5, color: ['#6a5b4e', '#4a4038'], alpha: [0.42, 0], density: 1.4, erosion: 0.7, fadeIn: 0.05 }, |
| +добавлено | // soot that keeps climbing off the cloud (lit, not glowing) |
| +добавлено | smoke: { style: 'media', emissive: 0, drag: 1.2, buoyancy: 1.3, curl: 1.5, curlScale: 0.35, life: [2.2, 4.0], size: [0.5, 2.2], sizePow: 0.55, color: ['#191513', '#2c2826'], alpha: [0.5, 0], density: 2.2, erosion: 0.8, fadeIn: 0.15, fadeOut: 0.45 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const gather = P.emitter('light.mote', { shape: 'shell', radius: 2.2, spread: 0.2, radial: -3.2, life: 0.45 }); |
| +добавлено | const windE = P.emitter('supernova.wind', { shape: 'shell', radius: 1, spread: 0.3, radial: 1.4 }); |
| +добавлено | const inMote = P.emitter('void.mote', { shape: 'shell', radius: 3.2, spread: 0.3, radial: -7 }); |
| +добавлено | const sparkE = P.emitter('light.spark', { shape: 'shell', radius: 0.4, spread: 3, radial: 12 }); |
| +добавлено | const ejectaE = P.emitter('supernova.ejecta', { shape: 'shell', radius: 0.5, spread: 2.5, radial: 10 }); |
| +добавлено | const flameE = P.emitter('supernova.flame', { shape: 'shell', radius: 0.5, spread: 1.2, radial: 8 }); |
| +добавлено | const dustE = P.emitter('supernova.dust', { shape: 'ring', radius: 1.2, spread: 0.9, radial: 11 }); |
| +добавлено | const smokeE = P.emitter('supernova.smoke', { shape: 'sphere', radius: 1, spread: 0.4 }); |
| +добавлено | const moteE = P.emitter('light.mote', { shape: 'disk', radius: 2.5, spread: 0.5 }); |
| +добавлено | |
| +добавлено | // colours: parsed only when the param strings change (Color.set allocates) |
| +добавлено | const starCol = new THREE.Color(), limbCol = new THREE.Color(), shockCol = new THREE.Color(), filCol = new THREE.Color(), voidCol = new THREE.Color(); |
| +добавлено | const giantLight = new THREE.Color(), coreCol = new THREE.Color(), hue = new THREE.Color(this.hue); |
| +добавлено | const coreTmp = new THREE.Color(), limbTmp = new THREE.Color(); |
| +добавлено | let hex = ''; |
| +добавлено | function colours(p) { |
| +добавлено | const h = p.star + p.limb + p.shock + p.filament + p.void; |
| +добавлено | if (h === hex) return; |
| +добавлено | hex = h; |
| +добавлено | starCol.set(p.star); limbCol.set(p.limb); shockCol.set(p.shock); filCol.set(p.filament); voidCol.set(p.void); |
| +добавлено | giantLight.copy(limbCol).lerp(starCol, 0.5); |
| +добавлено | coreCol.copy(shockCol).lerp(voidCol, 0.35); |
| +добавлено | } |
| +добавлено | SHOCK.color = shockCol; |
| +добавлено | |
| +добавлено | const stars = createStars(fx, MAX_N); |
| +добавлено | const balls = createFireballs(fx, MAX_N); |
| +добавлено | const shells = createShells(fx, MAX_N); |
| +добавлено | const arcs = new fx.AnalyticRibbons({ max: MAX_N * 4, segments: 96, pathGLSL: ARC_PATH, shadeGLSL: ARC_SHADE, target: 'fx' }); |
| +добавлено | fx.scenes.fx.add(arcs.mesh); |
| +добавлено | |
| +добавлено | // per cast record (cast.js keeps 16): no allocation per cast |
| +добавлено | const ST = Array.from({ length: 16 }, () => ({ |
| +добавлено | live: false, star: null, fb: null, sh: null, light: null, force: null, sigil: null, arcs: [null, null, null, null], nArc: 0, |
| +добавлено | S: new THREE.Vector3(), G: new THREE.Vector3(), e1: new THREE.Vector3(), e2: new THREE.Vector3(), ax: new THREE.Vector3(), |
| +добавлено | Rg: 0, ph: 0, ring: 0, lastPeak: 0, rot: 0, banged: false, shockR: 0, hitMask: 0, pullMask: 0, poolHit: false, pool: false, |
| +добавлено | lens: 0, E: 0, hz: 0, seed: 0, |
| +добавлено | })); |
| +добавлено | |
| +добавлено | function splat(x, z, radius, heat, char, glow, pattern, seed, width = 0) { |
| +добавлено | SPLAT.x = x; SPLAT.z = z; SPLAT.radius = radius; SPLAT.heat = heat; SPLAT.char = char; SPLAT.glow = glow; |
| +добавлено | SPLAT.pattern = pattern; SPLAT.seed = seed % 100; SPLAT.width = width; |
| +добавлено | fx.ground.splat(SPLAT); |
| +добавлено | } |
| +добавлено | const focus = (k) => fx.arena.setFocus(hue, k); |
| +добавлено | |
| +добавлено | // the star impostor: centre, radius, intensity, pulse, ring, corona extent (radii), spots, core/limb colours |
| +добавлено | function placeStar(st, pos, R, I, p, pulse, ring, ext, spots, core, limb, cor = 1.2) { |
| +добавлено | const u = st.u; |
| +добавлено | st.core.visible = st.corona.visible = R > 1e-3; |
| +добавлено | u.uStarA.value.set(pos.x, pos.y, pos.z, R); |
| +добавлено | u.uStarB.value.set(p.granules, 0.12, pulse, I); |
| +добавлено | u.uStarC.value.set(cor, ext, 0.9, 0.05); |
| +добавлено | u.uStarD.value.y = 0.78; u.uStarD.value.z = spots; u.uStarD.value.w = ring; |
| +добавлено | u.uStarCore.value.copy(core); |
| +добавлено | u.uStarLimb.value.copy(limb); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the lens: ours prefers slot 1, takes slot 0 only when 1 is held by someone else (the void |
| +добавлено | // ticks first and rewrites its slots every frame), and only ever clears a slot still holding |
| +добавлено | // what we wrote |
| +добавлено | let lensSlot = -1, lensStr = 0; |
| +добавлено | const lensPos = new THREE.Vector3(); |
| +добавлено | const lensMine = (i) => i === lensSlot && fx.lens.lenses[i].strength === lensStr && fx.lens.lenses[i].pos.equals(lensPos); |
| +добавлено | const lensFree = (i) => !(fx.lens.lenses[i].strength > 1e-4) || lensMine(i); |
| +добавлено | |
| +добавлено | function arcsOff(d) { |
| +добавлено | for (let k = 0; k < d.nArc; k++) { d.arcs[k]?.release(); d.arcs[k] = null; } |
| +добавлено | d.nArc = 0; |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'star' || k === 'limb' || k === 'shock' || k === 'filament' || k === 'void') colours(S.params); }, |
| +добавлено | |
| +добавлено | tick() { |
| +добавлено | arcs.update(); |
| +добавлено | let best = null; |
| +добавлено | for (const d of ST) if (d.live && d.lens > 1e-3 && (!best || d.lens > best.lens)) best = d; |
| +добавлено | const L = fx.lens; |
| +добавлено | if (best) { |
| +добавлено | const slot = lensFree(1) ? 1 : lensFree(0) ? 0 : -1; |
| +добавлено | if (lensSlot >= 0 && slot !== lensSlot && lensMine(lensSlot)) L.lenses[lensSlot].strength = 0; |
| +добавлено | lensSlot = slot; |
| +добавлено | if (slot >= 0) { |
| +добавлено | L.set(slot, best.S, Math.max(best.E, 1e-3), best.hz, best.lens); |
| +добавлено | lensPos.copy(best.S); lensStr = best.lens; |
| +добавлено | L.active = true; |
| +добавлено | } |
| +добавлено | } else if (lensSlot >= 0) { |
| +добавлено | if (lensMine(lensSlot)) L.lenses[lensSlot].strength = 0; |
| +добавлено | lensSlot = -1; |
| +добавлено | if (!L.warming && !(L.lenses[0].strength > 1e-4) && !(L.lenses[1].strength > 1e-4)) L.active = false; |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = ST[c.index]; |
| +добавлено | colours(p); |
| +добавлено | if (name === 'charge') { |
| +добавлено | d.live = true; |
| +добавлено | const pool = ARENA.pool; |
| +добавлено | d.pool = Math.abs(c.to.x - pool.center.x) < pool.half.x && Math.abs(c.to.z - pool.center.y) < pool.half.y; |
| +добавлено | d.G.set(c.to.x, d.pool ? pool.water : 0, c.to.z); |
| +добавлено | d.Rg = 0.9 * p.size; |
| +добавлено | d.S.set(c.to.x, d.G.y + 2.0 + d.Rg, c.to.z); |
| +добавлено | d.ph = 0; d.ring = 0; d.lastPeak = -9; d.rot = (c.seed % 628) / 100; d.banged = false; d.shockR = 0; |
| +добавлено | d.hitMask = 0; d.pullMask = 0; d.poolHit = false; d.lens = 0; d.E = 0; d.hz = 0; d.seed = (c.seed % 89) * 0.37; |
| +добавлено | d.star = null; |
| +добавлено | for (const st of stars) if (!st.busy) { d.star = st; break; } |
| +добавлено | if (d.star) { d.star.busy = true; d.star.u.uStarD.value.x = (c.seed % 97) * 0.173; placeStar(d.star, d.S, 0, 0, p, 0, 0, 2.6, 0.6, starCol, limbCol); } |
| +добавлено | d.fb = null; d.sh = null; |
| +добавлено | d.light = fx.lights.acquire(); |
| +добавлено | if (d.light) { d.light.type = 0; d.light.range = 10; d.light.intensity = 0; d.light.shadow = true; d.light.color.copy(giantLight); d.light.pos.copy(d.S); } |
| +добавлено | d.force = null; |
| +добавлено | SIGIL.pos.copy(d.G).setY(d.G.y + 0.03); SIGIL.radius = 2.3 * p.size; SIGIL.color = starCol; SIGIL.seed = c.seed % 1000; SIGIL.rotation = d.rot; |
| +добавлено | d.sigil = fx.sigils.acquire(SIGIL); |
| +добавлено | if (d.sigil) { d.sigil.reveal = 0; d.sigil.intensity = 0; } |
| +добавлено | arcsOff(d); |
| +добавлено | // the ring's plane leans towards the camera so it opens into an ellipse from any view |
| +добавлено | _a.subVectors(fx.camera.position, d.S).setY(0); |
| +добавлено | if (_a.lengthSq() < 1e-4) _a.set(0, 0, 1); |
| +добавлено | _a.normalize(); |
| +добавлено | _b.copy(UP).addScaledVector(_a, 0.3).normalize(); |
| +добавлено | d.e1.crossVectors(_b, _a).normalize(); |
| +добавлено | d.e2.crossVectors(_b, d.e1); |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | // the collapse begins: the pull and the contracting shell |
| +добавлено | d.force = fx.forces.acquire(); |
| +добавлено | if (d.force) { const F = d.force; F.type = 2; F.pos.copy(d.S); F.p[0] = 0; F.p[1] = 0.4; F.p[2] = 0.15; F.p[3] = 9; } |
| +добавлено | SHOCK.pos.copy(d.S); SHOCK.radius0 = 4 * p.size; SHOCK.radius1 = 0.3; SHOCK.duration = TI; SHOCK.thickness = 1.0; |
| +добавлено | SHOCK.distortion = 0.035 * p.collapse; SHOCK.intensity = 0; SHOCK.ground = false; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | fx.rig.shake(0.2, 5); |
| +добавлено | } else if (name === 'done') { |
| +добавлено | d.live = false; |
| +добавлено | if (d.star) { d.star.busy = false; d.star.core.visible = d.star.corona.visible = false; d.star.u.uStarA.value.w = 0; } |
| +добавлено | if (d.fb) { d.fb.busy = false; d.fb.v.visible = false; } |
| +добавлено | if (d.sh) { d.sh.busy = false; d.sh.mesh.visible = false; d.sh.u.uNebA.value.w = 0; d.sh.u.uNebC.value.z = 0; } |
| +добавлено | arcsOff(d); |
| +добавлено | d.light?.release(); d.sigil?.release(); d.force?.release(); |
| +добавлено | d.star = d.fb = d.sh = d.light = d.sigil = d.force = null; |
| +добавлено | d.lens = 0; |
| +добавлено | focus(2.5); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = ST[c.index], L = d.light, I = p.intensity, st = d.star; |
| +добавлено | colours(p); |
| +добавлено | d.rot += dt * (c.phase === 'charge' ? 0.8 : 6); |
| +добавлено | if (d.sigil) d.sigil.rotation = d.rot; |
| +добавлено | |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge; |
| +добавлено | // a seed of light, then the giant swells; then it pulsates, faster and harder |
| +добавлено | const grow = easeOutBack(sstep(k, 0.08, 0.5)); |
| +добавлено | const amp = 0.06 * sstep(k, 0.45, 1); |
| +добавлено | d.ph += dt * (4 + 22 * k * k); |
| +добавлено | const wob = Math.sin(d.ph); |
| +добавлено | if (wob > 0.97 && c.age - d.lastPeak > 0.2) d.lastPeak = c.age; |
| +добавлено | const since = c.age - d.lastPeak; |
| +добавлено | d.ring = k > 0.45 && since < 0.7 ? 1 + since * 3 : 0; |
| +добавлено | const R = Math.max(d.Rg * grow * (1 + amp * wob), 0.04 * sstep(k, 0, 0.08)); |
| +добавлено | const pulse = amp * 8 * Math.max(wob, 0); |
| +добавлено | if (st) placeStar(st, d.S, R, (1.1 + 1.2 * k) * I * (1 + 0.5 * pulse), p, pulse, d.ring, 2.6, 0.6, starCol, limbCol); |
| +добавлено | gather.pos.copy(d.S); gather.rate(700 * (1 - sstep(k, 0.2, 0.6)) * p.sparks, dt); |
| +добавлено | windE.pos.copy(d.S); windE.radius = R; windE.rate(140 * p.sparks * sstep(k, 0.3, 0.6), dt); |
| +добавлено | if (L) { L.pos.copy(d.S); L.range = 10; L.color.copy(giantLight); L.intensity = (26 * grow * (1 + 0.6 * pulse)) * I; } |
| +добавлено | if (d.sigil) { d.sigil.reveal = sstep(k, 0, 0.6); d.sigil.intensity = (1.5 + 2.5 * k) * I; d.sigil.radius = 2.3 * p.size; } |
| +добавлено | focus(2.5 * (1 - k) * (1 - k) + 0.2); // the caster's orb gives its light to the star |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | |
| +добавлено | const t = c.phase === 'impact' ? c.t : c.durations.impact + c.t; |
| +добавлено | const lx = c.phase === 'linger' ? c.t / c.durations.linger : 0; |
| +добавлено | if (t < TI) { implode(c, p, d, t / TI, dt); return; } |
| +добавлено | if (!d.banged) bang(c, p, d); |
| +добавлено | afterglow(c, p, d, t - TI, lx, dt); |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | // --- the collapse -------------------------------------------------------------------------- |
| +добавлено | function implode(c, p, d, x, dt) { |
| +добавлено | const I = p.intensity, st = d.star, L = d.light, cl = p.collapse; |
| +добавлено | const R = d.Rg * Math.max(0.06, 1 - Math.pow(x, 2.2)); |
| +добавлено | if (st) { |
| +добавлено | const core = coreTmp.copy(starCol).lerp(_white, sstep(x, 0.2, 0.9)); |
| +добавлено | const limb = limbTmp.copy(limbCol).lerp(shockCol, sstep(x, 0.3, 1)); |
| +добавлено | placeStar(st, d.S, R, (2 + 10 * x * x * x) * I, p, 0.4 * x, 0, 2.6 + 1.5 * x, 0.6 * (1 - x), core, limb); |
| +добавлено | } |
| +добавлено | // the lens grows as the mass gets compact; at the very end, an instant of black hole |
| +добавлено | d.E = (0.25 + 0.4 * x) * p.size; |
| +добавлено | d.hz = 0.2 * p.size * sstep(x, 0.85, 1) * Math.min(cl, 1); |
| +добавлено | d.lens = p.lensing * cl * (0.1 + 0.7 * x * x); |
| +добавлено | if (d.force) d.force.p[0] = 80 * cl * x * x; |
| +добавлено | // everything falls in: the wind and the void's motes |
| +добавлено | windE.pos.copy(d.S); windE.radius = R; windE.rate(100 * p.sparks * (1 - x), dt); |
| +добавлено | inMote.pos.copy(d.S); inMote.radius = 3.2 * p.size; inMote.rate(1400 * x * cl * p.sparks, dt); |
| +добавлено | if (L) { L.pos.copy(d.S); L.color.copy(giantLight).lerp(voidCol, x); L.intensity = 26 * I * (1 - sstep(x, 0, 0.8)) + 1; } |
| +добавлено | if (d.sigil) { d.sigil.radius = 2.3 * p.size * (1 - 0.85 * Math.pow(x, 1.5)); d.sigil.intensity = (4 + 10 * x) * I; d.sigil.color.copy(starCol).lerp(voidCol, x); } |
| +добавлено | focus(0.2 * (1 - x) + 0.02); |
| +добавлено | fx.rig.shake(0.04 + 0.2 * x * x, 7); |
| +добавлено | // the dummies lean into the pull, and are tainted by it |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i]; |
| +добавлено | const dist = dm.pos.distanceTo(d.S); |
| +добавлено | if (dist > 7 * p.size) continue; |
| +добавлено | dm.status.corrupt = Math.max(dm.status.corrupt, 0.5 * x * (1 - dist / (7 * p.size))); |
| +добавлено | if (x > 0.35 && !(d.pullMask & (1 << i))) { d.pullMask |= 1 << i; dm.hit(_a.subVectors(d.S, dm.pos).setY(0), 0.3 * cl * (1 - dist / (7 * p.size)) + 0.05); } |
| +добавлено | } |
| +добавлено | if (x > 0.5) { |
| +добавлено | const pool = ARENA.pool; |
| +добавлено | const px = clamp(d.S.x, pool.center.x - pool.half.x, pool.center.x + pool.half.x), pz = clamp(d.S.z, pool.center.y - pool.half.y, pool.center.y + pool.half.y); |
| +добавлено | if (Math.hypot(px - d.S.x, pz - d.S.z) < 4) fx.pool.swirl(px, pz, 0.3 * x * cl); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- the explosion --------------------------------------------------------------------------- |
| +добавлено | function bang(c, p, d) { |
| +добавлено | d.banged = true; |
| +добавлено | const I = p.intensity, s = p.size, bl = p.blast, G = d.G, Sx = d.S; |
| +добавлено | fx.flash(0.35 * I); |
| +добавлено | fx.rig.shake(1.0, 12); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(5, 0.25); fx.rig.hitstop(0.08); } |
| +добавлено | if (d.sigil) { d.sigil.release(); d.sigil = null; } |
| +добавлено | // the explosion mass (FLUID PORT: acquire a FluidVolume domain here and emit the core) |
| +добавлено | d.fb = null; |
| +добавлено | for (const f of balls) if (!f.busy) { d.fb = f; break; } |
| +добавлено | if (d.fb) d.fb.busy = true; |
| +добавлено | else { flameE.pos.copy(Sx); flameE.vel.set(0, 0.5, 0); flameE.burst(160 * bl); } |
| +добавлено | d.sh = null; |
| +добавлено | for (const sh of shells) if (!sh.busy) { d.sh = sh; break; } |
| +добавлено | if (d.sh) { d.sh.busy = true; d.sh.mesh.visible = true; } |
| +добавлено | // the condensation ring (a dense strand and a thin wide one) and the pulsar's beams |
| +добавлено | arcsOff(d); |
| +добавлено | for (let k = 0; k < 4; k++) { const h = arcs.acquire(); if (!h) break; h.intensity = 0; d.arcs[k] = h; d.nArc = k + 1; } |
| +добавлено | _a.set(Math.cos(d.rot), 0, Math.sin(d.rot)); |
| +добавлено | d.ax.copy(UP).addScaledVector(_a, 0.55).normalize(); |
| +добавлено | // blast sphere (refraction) and a ground ring (refraction only) |
| +добавлено | SHOCK.pos.copy(Sx); SHOCK.radius0 = 0.4; SHOCK.radius1 = 7 * s * bl; SHOCK.duration = 0.9; SHOCK.thickness = 1.2; SHOCK.distortion = 0.12 * bl; SHOCK.intensity = 0; SHOCK.ground = false; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | SHOCK.pos.copy(G).setY(G.y + 0.02); SHOCK.radius0 = 0.8; SHOCK.radius1 = 10 * s * bl; SHOCK.duration = 1.1; SHOCK.thickness = 1.0; SHOCK.distortion = 0.05 * bl; SHOCK.intensity = 0; SHOCK.ground = true; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | // ejecta, sparks, the dust skirt |
| +добавлено | sparkE.pos.copy(Sx); sparkE.vel.set(0, 2, 0); sparkE.burst(120 * p.sparks); |
| +добавлено | ejectaE.pos.copy(Sx); ejectaE.vel.set(0, 3, 0); ejectaE.burst(320 * p.sparks * bl); |
| +добавлено | dustE.pos.copy(G).setY(G.y + 0.2); dustE.radius = 1.2 * s; dustE.vel.set(0, 0.5, 0); dustE.burst(d.pool ? 0 : 260 * p.smoke * bl); |
| +добавлено | // the floor: a scorched blast pattern and a burnt ring |
| +добавлено | splat(G.x, G.z, 3 * s, 0.35, 0.8, 0, 'noise', c.seed); |
| +добавлено | splat(G.x, G.z, 4.4 * s * bl, 0, 0.45, 0, 'radial', c.seed + 1); |
| +добавлено | splat(G.x, G.z, 2.6 * s, 0.25, 0.4, 0.5, 'ring', c.seed + 2, 0.5 * s); |
| +добавлено | fx.impacts.add('scorch', G, 3.5 * s, 0.8, 6, _fire); |
| +добавлено | if (d.force) { const F = d.force; F.type = 4; F.pos.copy(Sx); F.p[0] = 0.4; F.p[1] = 1.4; F.p[2] = 0; F.p[3] = 0; } |
| +добавлено | d.shockR = 0.4; |
| +добавлено | if (d.light) { d.light.range = 14; d.light.pos.copy(Sx); } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function afterglow(c, p, d, tb, lx, dt) { |
| +добавлено | const I = p.intensity, s = p.size, bl = p.blast, st = d.star, L = d.light; |
| +добавлено | const fade = 1 - sstep(lx, 0.55, 1); |
| +добавлено | const pulsarOn = sstep(tb, 0.9, 1.4) * p.pulsar * (1 - sstep(lx, 0.7, 1)); |
| +добавлено | // the fireball: fast Sedov-like growth, then it climbs as it cools |
| +добавлено | const Rf = 0.25 + 1.7 * s * Math.sqrt(bl) * Math.pow(1 - Math.exp(-tb * 5.5), 0.55) + 0.12 * tb; |
| +добавлено | _fp.copy(d.S); _fp.y += 0.45 * Math.pow(Math.max(tb - 0.3, 0), 1.4); |
| +добавлено | if (d.fb) { |
| +добавлено | placeFireball(d.fb, _fp, Rf, tb, 1.0 * p.fireball * I, 5500, p.soot, fade, d.seed, filCol, sstep(tb, 0.8, 2.6)); |
| +добавлено | if (fade <= 0) { d.fb.busy = false; d.fb.v.visible = false; d.fb = null; } |
| +добавлено | } |
| +добавлено | // the remnant: a flash that shrinks to a pulsar, hidden in the cloud until it thins |
| +добавлено | if (st) { |
| +добавлено | const R = (0.07 + 0.35 * Math.exp(-tb / 0.05)) * s * (1 - sstep(lx, 0.85, 1)); |
| +добавлено | placeStar(st, d.S, R, (10 + 60 * Math.exp(-tb / 0.08)) * I, p, 0.5, 0, 8, 0, _white, shockCol, 1.2 * Math.max(Math.exp(-tb / 0.1), pulsarOn)); |
| +добавлено | } |
| +добавлено | // the bang releases the lens |
| +добавлено | d.lens = 0; |
| +добавлено | // condensation dome, glare and shafts, wind nebula |
| +добавлено | const front = 0.4 + (7 * s * bl - 0.4) * Math.pow(Math.min(tb / 0.9, 1), 0.4); |
| +добавлено | const cond = sstep(tb, 0.02, 0.05) * (1 - sstep(tb, 0.08, 0.22)); |
| +добавлено | const sh = d.sh; |
| +добавлено | if (sh) { |
| +добавлено | const u = sh.u, glare = 2.5 * Math.exp(-tb / 0.07), shafts = 1.2 * p.rays * Math.exp(-tb / 0.15); |
| +добавлено | u.uNebA.value.set(d.S.x, d.S.y, d.S.z, cond > 0 ? front : 0); |
| +добавлено | u.uNebB.value.set(tb, I, 0.14, d.seed); |
| +добавлено | u.uNebC.value.set(glare, shafts, (1.6 + 4.4 * Math.exp(-tb / 0.15)) * s, 0.45 * pulsarOn); |
| +добавлено | u.uNebD.value.set(cond * bl, 2 * Math.exp(-tb / 0.1), 0, 0); |
| +добавлено | u.uNebHot.value.copy(_white).lerp(shockCol, 0.3); |
| +добавлено | u.uNebShock.value.copy(shockCol); |
| +добавлено | u.uNebCore.value.copy(coreCol); |
| +добавлено | sh.mesh.visible = cond > 0 || glare + shafts > 0.01 || pulsarOn > 0.001; |
| +добавлено | } |
| +добавлено | // the condensation ring and the pulsar's beams |
| +добавлено | const xr = Math.min(tb / 1.1, 1), rr = 0.4 + 9 * s * bl * Math.pow(xr, 0.45); |
| +добавлено | const ring = d.arcs[0], wide = d.arcs[1]; |
| +добавлено | if (ring) { |
| +добавлено | ring.p0.set(d.S.x, d.S.y, d.S.z, rr); ring.p1.set(d.e1.x, d.e1.y, d.e1.z, 0.22 * s * (1 + 2.5 * xr)); ring.p2.set(d.e2.x, d.e2.y, d.e2.z, 0.03); |
| +добавлено | ring.p3.set(d.seed * 0.1, 1, 0.75 * sstep(tb, 0.02, 0.08) * (1 - xr) ** 1.2, 3 * Math.exp(-tb / 0.08)); |
| +добавлено | ring.color.copy(shockCol); ring.intensity = I; |
| +добавлено | } |
| +добавлено | if (wide) { |
| +добавлено | wide.p0.set(d.S.x, d.S.y, d.S.z, rr * 0.9); wide.p1.set(d.e1.x, d.e1.y, d.e1.z, 0.6 * s * (1 + 2 * xr)); wide.p2.set(d.e2.x, d.e2.y, d.e2.z, 0.05); |
| +добавлено | wide.p3.set(d.seed * 0.1 + 0.3, 1, 0.3 * sstep(tb, 0.04, 0.12) * (1 - xr) ** 1.5, 0); |
| +добавлено | wide.color.copy(shockCol); wide.intensity = I; |
| +добавлено | } |
| +добавлено | // lighthouse: the spin axis precesses about the vertical |
| +добавлено | const om = c.age * 7.5, ca = Math.cos(om), sa = Math.sin(om); |
| +добавлено | _a.set(d.ax.x * ca - d.ax.z * sa, d.ax.y, d.ax.x * sa + d.ax.z * ca); |
| +добавлено | const len = 5.5 * s * sstep(tb, 0.9, 1.3); |
| +добавлено | for (let k = 0; k < 2; k++) { |
| +добавлено | const h = d.arcs[2 + k]; |
| +добавлено | if (!h) continue; |
| +добавлено | const sg = k ? -1 : 1; |
| +добавлено | h.p0.set(d.S.x + _a.x * sg * 0.05, d.S.y + _a.y * sg * 0.05, d.S.z + _a.z * sg * 0.05, 0.03 * s); |
| +добавлено | h.p1.set(d.S.x + _a.x * sg * len, d.S.y + _a.y * sg * len, d.S.z + _a.z * sg * len, 0.3 * s); |
| +добавлено | h.p2.set(0, 0, 0, 0); |
| +добавлено | h.p3.set(k * 0.5, 0, 0, 0); |
| +добавлено | h.color.copy(shockCol).lerp(voidCol, 0.2); h.intensity = 1.2 * I * pulsarOn; |
| +добавлено | } |
| +добавлено | // light: the blast, cooling like the fireball, then the pulsar's beat |
| +добавлено | if (L) { |
| +добавлено | _v.subVectors(fx.camera.position, d.S).normalize(); |
| +добавлено | const beat = Math.abs(_a.dot(_v)); |
| +добавлено | L.pos.copy(_fp); |
| +добавлено | L.color.copy(shockCol).lerp(_fire, sstep(tb, 0.03, 0.6)); |
| +добавлено | L.intensity = (150 * Math.exp(-tb / 0.08) + 70 * Math.exp(-tb / 0.6) + 14 * Math.exp(-tb / 2) * fade + 8 * pulsarOn * (0.6 + 0.8 * beat)) * I; |
| +добавлено | L.range = 14 - 4 * sstep(tb, 0.3, 1.5); |
| +добавлено | } |
| +добавлено | // the force shell runs out with the blast and lets go |
| +добавлено | const F = d.force; |
| +добавлено | if (F && F.type === 4) { |
| +добавлено | d.shockR += dt * 16; |
| +добавлено | F.p[0] = d.shockR; F.p[2] = 150 * bl * Math.max(0, 1 - d.shockR / 14); |
| +добавлено | if (d.shockR > 14) { F.release(); d.force = null; } |
| +добавлено | } |
| +добавлено | // the front reaches the dummies and the pool |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i]; |
| +добавлено | if (d.hitMask & (1 << i)) continue; |
| +добавлено | const dist = dm.pos.distanceTo(d.S); |
| +добавлено | if (dist > front) continue; |
| +добавлено | d.hitMask |= 1 << i; |
| +добавлено | const reach = 10 * s * bl; |
| +добавлено | if (dist > reach) continue; |
| +добавлено | const k = 1 - dist / reach; |
| +добавлено | _b.subVectors(dm.pos, d.S).setY(0); |
| +добавлено | if (_b.lengthSq() < 1e-4) _b.copy(d.e1); |
| +добавлено | dm.hit(_b, 1.3 * k + 0.25); |
| +добавлено | dm.status.burn = Math.max(dm.status.burn, 0.9 * k + 0.2); |
| +добавлено | } |
| +добавлено | if (!d.poolHit) { |
| +добавлено | const pool = ARENA.pool; |
| +добавлено | const px = clamp(d.S.x, pool.center.x - pool.half.x, pool.center.x + pool.half.x), pz = clamp(d.S.z, pool.center.y - pool.half.y, pool.center.y + pool.half.y); |
| +добавлено | if (Math.hypot(px - d.S.x, pz - d.S.z, pool.water - d.S.y) < front) { |
| +добавлено | d.poolHit = true; |
| +добавлено | if (front < 10 * s * bl) { fx.pool.ripple(px, pz, 0.1 * bl, 2.5); fx.pool.flash(shockCol, 1.2 * I); } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // soot keeps climbing off the top of the cloud; motes rise off the scorch |
| +добавлено | if (tb > 0.5 && lx < 0.8) { |
| +добавлено | smokeE.pos.copy(_fp).setY(_fp.y + 0.6 * Rf); smokeE.radius = 0.6 * Rf; smokeE.vel.set(0, 1.0, 0); smokeE.rate(14 * p.smoke * (1 - lx), dt); |
| +добавлено | } |
| +добавлено | if (lx > 0 && lx < 0.85) { |
| +добавлено | moteE.pos.copy(d.G).setY(d.G.y + 0.05); moteE.radius = 2.6 * s; moteE.vel.set(0, 1.2, 0); moteE.rate(80 * p.sparks * (1 - lx), dt); |
| +добавлено | } |
| +добавлено | focus(Math.min(2.5, 0.02 + tb * 1.5)); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | |
| +добавлено | // Lightning (PLAN 3), timed against high-speed strike footage (Dan Robinson / Tom Warner style). |
| +добавлено | // Thunderstrike: charge (micro-arcs over the focus, ionisation motes falling in, a cloud cap |
| +добавлено | // gathering over the target, the floor dampening under it); a dim stepped leader with branches |
| +добавлено | // feeling their way down in pulses, upward streamers rising to meet it; the blinding return stroke |
| +добавлено | // climbing the main channel; a few restrikes, each preceded by a dart leader down the same channel |
| +добавлено | // with the branches gone; then the residual channel, cooling from blue-white to rose and breaking |
| +добавлено | // into beads. A line light hangs on the channel (never inside the target's own capsule, or the |
| +добавлено | // target's shadow swallows the whole floor), so floor, pillars and dummies pulse with each stroke |
| +добавлено | // and throw shadows. Ionised air and smoke at the strike point, Lichtenberg scorch, blue sparks |
| +добавлено | // and heavy molten spatter, arcs crawling over the floor (further on wet ground), thunder |
| +добавлено | // arriving distance / 343 m/s after the stroke. |
| +добавлено | // Chain Arc: a thick channelled arc that jumps from dummy to dummy. |
| +добавлено | |
| +добавлено | const SOUND_SPEED = 343; |
| +добавлено | const LEADER = 0.26, LEADER_STEPS = 16, RISE = 1 / 30, DART = 1 / 30; // seconds |
| +добавлено | const PEAK = [1, 0.62, 0.42, 0.3, 0.22]; // later strokes are weaker |
| +добавлено | const CORE_WIDTH = [0.16, 0.1, 0.075, 0.06, 0.05]; |
| +добавлено | const BOLT_INTENSITY = 38, LIGHT_GAIN = 60; |
| +добавлено | const MAX_CRAWL = 6, MAX_CHAIN = 3, MAX_STREAMERS = 3, CHARGE_ARCS = 4; |
| +добавлено | const GLOOM = 0.45; // storm light: exposure the strike takes away, so a dusk sky doesn't wash the bolt out |
| +добавлено | const SHOCK = 0.15; // the dummy's crackle: at 1 it clips to white marble |
| +добавлено | const clamp01 = (x) => Math.min(1, Math.max(0, x)); |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'lightning', name: 'Lightning', key: '1', hue: '#7fb4ff', |
| +добавлено | shape: 'strike', forms: ['Thunderstrike', 'Chain Arc'], |
| +добавлено | params: { intensity: 1, branches: 8, jag: 0.085, restrikes: 3, sparks: 1, crawl: 1, cloud: 1, damp: 1, core: '#d6e6ff', glow: '#5b78ff' }, |
| +добавлено | ranges: { intensity: [0.1, 3, 0.01], branches: [0, 8, 1], jag: [0.02, 0.3, 0.005], restrikes: [1, 5, 1], sparks: [0, 3, 0.01], crawl: [0, 3, 0.01], cloud: [0, 2, 0.01], damp: [0, 2, 0.01] }, |
| +добавлено | presets: { |
| +добавлено | crimson: { core: '#ffd0d0', glow: '#ff2a3c' }, |
| +добавлено | violet: { core: '#efd8ff', glow: '#8a3dff' }, |
| +добавлено | 'white hot': { core: '#ffffff', glow: '#b8c8ff', intensity: 1.6 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 0.6, impact: 0.8, linger: 1.6, sustain: 1.8 }, |
| +добавлено | kinds: { |
| +добавлено | spark: { style: 'glow', drag: 0.4, gravity: 9.8, restitution: 0.4, friction: 0.35, life: [0.12, 0.6], size: [0.016, 0.005], stretch: 0.04, emissive: 3, color: ['#dfe9ff', '#5f7cff'], alpha: [1, 0], paints: 'heat', paintAmount: 0.03 }, |
| +добавлено | // molten spatter: low drag, heavy, skitters over the floor and burns in |
| +добавлено | ember: { style: 'glow', drag: 0.22, gravity: 9.8, restitution: 0.45, friction: 0.45, life: [0.5, 1.5], size: [0.02, 0.007], stretch: 0.03, emissive: 4, color: ['#fff0d0', '#ff5a14'], alpha: [1, 0], fadeOut: 0.35, paints: 'heat', paintAmount: 0.02 }, |
| +добавлено | mote: { style: 'glow', drag: 1.2, life: [0.35, 0.7], size: [0.018, 0.006], stretch: 0.02, emissive: 5, color: ['#cfe0ff', '#7a9bff'], alpha: [0, 1], fadeIn: 0.5, fadeOut: 0.2, forceResponse: 1 }, |
| +добавлено | ozone: { style: 'glow', drag: 2, buoyancy: 0.4, curl: 1.2, curlScale: 0.6, life: [0.8, 1.6], size: [0.015, 0.005], emissive: 2, color: ['#9fc0ff', '#4a6aff'], alpha: [0.8, 0] }, |
| +добавлено | // ionised air: a soft violet-blue bloom hugging the strike, gone in a few frames |
| +добавлено | ion: { style: 'media', emissive: 2.2, drag: 2.5, buoyancy: 0.25, curl: 0.8, curlScale: 0.35, life: [0.12, 0.3], size: [0.2, 0.85], color: ['#8fa4ff', '#4a35ff'], alpha: [0.2, 0], density: 0.35, erosion: 0.7, fadeIn: 0.03, fadeOut: 0.6 }, |
| +добавлено | cloud: { style: 'media', drag: 1.5, curl: 0.4, curlScale: 0.08, life: [2.2, 3], size: [2.6, 4.2], color: ['#1b1f2a', '#141720'], alpha: [0.1625, 0], density: 0.9, erosion: 0.4, fadeIn: 0.35, fadeOut: 0.4, forceResponse: 0.2, lightResponse: 2.5 }, |
| +добавлено | smoke: { style: 'media', drag: 1.5, buoyancy: 0.9, curl: 1.3, life: [1.1, 2.4], size: [0.25, 1.2], color: ['#3a3d45', '#26282e'], alpha: [0.09, 0], density: 1.2, erosion: 0.65, lightResponse: 1.2 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const spark = P.emitter('lightning.spark', { shape: 'sphere', radius: 0.06, spread: 7, radial: 2 }); |
| +добавлено | const ember = P.emitter('lightning.ember', { shape: 'sphere', radius: 0.08, spread: 3.6, radial: 2.4 }); |
| +добавлено | const mote = P.emitter('lightning.mote', { shape: 'shell', radius: 2.2, spread: 0.2 }); |
| +добавлено | const ozone = P.emitter('lightning.ozone', { shape: 'sphere', radius: 1.4, spread: 0.3 }); |
| +добавлено | const ion = P.emitter('lightning.ion', { shape: 'sphere', radius: 0.45, spread: 0.7 }); |
| +добавлено | const cloud = P.emitter('lightning.cloud', { shape: 'disk', radius: 4, spread: 0.3 }); |
| +добавлено | const smoke = P.emitter('lightning.smoke', { shape: 'disk', radius: 1.2, spread: 0.4 }); |
| +добавлено | const V3 = () => new THREE.Vector3(); |
| +добавлено | const core = new THREE.Color(), glow = new THREE.Color(), glowDim = new THREE.Color(), lightCol = new THREE.Color(); |
| +добавлено | const leaderCol = new THREE.Color(), WARM = new THREE.Color('#ffa487'), EMBER = new THREE.Color('#ff7a2e'), warmGlow = new THREE.Color(); |
| +добавлено | const _a = V3(), _b = V3(), _c = V3(); |
| +добавлено | const SCORCH = new THREE.Color(1, 0.4, 0.1); // impacts.add allocates its default colour when given none |
| +добавлено | |
| +добавлено | function look(p) { |
| +добавлено | core.set(p.core); glow.set(p.glow); |
| +добавлено | glowDim.copy(glow).multiplyScalar(0.11); |
| +добавлено | lightCol.copy(core).lerp(glow, 0.35); |
| +добавлено | leaderCol.copy(core).lerp(glow, 0.6); |
| +добавлено | warmGlow.copy(WARM).lerp(glow, 0.5).multiplyScalar(0.05); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // per-cast state by cast index: nothing allocated per cast |
| +добавлено | const st = Array.from({ length: 16 }, () => ({ |
| +добавлено | arcs: new Array(CHARGE_ARCS).fill(null), crawl: new Array(MAX_CRAWL).fill(null), nc: 0, |
| +добавлено | streamers: new Array(MAX_STREAMERS).fill(null), ns: 0, tips: Array.from({ length: MAX_STREAMERS }, V3), |
| +добавлено | main: null, L: null, hot: null, attract: null, sky: V3(), g: V3(), f: V3(), |
| +добавлено | at: new Float32Array(PEAK.length), rev: new Float32Array(LEADER_STEPS), strikes: 0, n: 0, shR: 0, shC: V3(), hp: V3(), |
| +добавлено | order: new Int8Array(MAX_CHAIN), hops: 0, reseed: 0, damp: 0, thunderAt: 0, thunder: false, punched: false, flash: 0, |
| +добавлено | })); |
| +добавлено | const BOLT = { a: V3(), b: V3(), branches: 8, subBranches: 12, jag: 0.085, width: 0.03, color: core, glowColor: glowDim, intensity: 0, glowWidth: 12, reveal: 1, snapToArena: false, seed: 1, branchSeed: 1, rough: 0.62, fine: 1, vary: 1, branchLevel: 1 }; |
| +добавлено | function bolt(a, b, branches, sub, jag, width, glowWidth, reveal, snap, seed, branchSeed, branchLevel) { |
| +добавлено | BOLT.a.copy(a); BOLT.b.copy(b); BOLT.branches = branches; BOLT.subBranches = sub; BOLT.jag = jag; BOLT.width = width; |
| +добавлено | BOLT.glowWidth = glowWidth; BOLT.reveal = reveal; BOLT.snapToArena = snap; BOLT.seed = seed; BOLT.branchSeed = branchSeed; BOLT.branchLevel = branchLevel; |
| +добавлено | return fx.bolts.acquire(BOLT); |
| +добавлено | } |
| +добавлено | const seedInt = () => (fx.rand() * 16777215) | 0; |
| +добавлено | const seeded = (c, k) => ((c.seed * 2654435761 + k * 40503) >>> 0) & 0xffffff; |
| +добавлено | const SPL = { x: 0, z: 0, radius: 1, heat: 0, char: 0, wet: 0, glow: 0, pattern: 'lichtenberg', seed: 0 }; |
| +добавлено | function splat(x, z, radius, heat, char, wet, glowAmt, pattern, seed) { |
| +добавлено | SPL.x = x; SPL.z = z; SPL.radius = radius; SPL.heat = heat; SPL.char = char; SPL.wet = wet; SPL.glow = glowAmt; SPL.pattern = pattern; SPL.seed = seed; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | const SHK = { pos: V3(), radius0: 0.2, radius1: 5, duration: 0.35, thickness: 0.5, distortion: 0.05, ground: true, intensity: 0 }; |
| +добавлено | |
| +добавлено | // chain targets: the hit dummy, then the nearest unvisited ones within 9 m |
| +добавлено | function chain(d, c) { |
| +добавлено | let cur = c.to, used = 0; |
| +добавлено | d.hops = 0; |
| +добавлено | if (c.target >= 0) { d.order[d.hops++] = c.target; used = 1 << c.target; cur = fx.dummies[c.target].pos; } |
| +добавлено | while (d.hops < MAX_CHAIN) { |
| +добавлено | let best = -1, bd = 9; |
| +добавлено | for (let j = 0; j < fx.dummies.length; j++) { |
| +добавлено | if (used & (1 << j)) continue; |
| +добавлено | const dd = fx.dummies[j].pos.distanceTo(cur); |
| +добавлено | if (dd < bd) { bd = dd; best = j; } |
| +добавлено | } |
| +добавлено | if (best < 0) break; |
| +добавлено | used |= 1 << best; |
| +добавлено | d.order[d.hops++] = best; |
| +добавлено | cur = fx.dummies[best].pos; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | function releaseAll(d) { |
| +добавлено | for (let i = 0; i < CHARGE_ARCS; i++) { d.arcs[i]?.release(); d.arcs[i] = null; } |
| +добавлено | for (let i = 0; i < MAX_CRAWL; i++) { d.crawl[i]?.release(); d.crawl[i] = null; } |
| +добавлено | for (let i = 0; i < MAX_STREAMERS; i++) { d.streamers[i]?.release(); d.streamers[i] = null; } |
| +добавлено | d.nc = d.ns = 0; |
| +добавлено | d.main?.release(); d.L?.release(); d.hot?.release(); d.attract?.release(); |
| +добавлено | d.main = d.L = d.hot = d.attract = null; |
| +добавлено | } |
| +добавлено | // the line light hangs on the channel, ending above the target's capsule |
| +добавлено | function hangLight(L, m) { |
| +добавлено | m.point(0.85, L.end); |
| +добавлено | L.end.y = Math.max(L.end.y, 2.8); |
| +добавлено | // on past the bolt's top: the top's mirror image is a blob on wet floor, this puts it behind the camera |
| +добавлено | L.pos.copy(m.a).sub(L.end).multiplyScalar(1.8).add(L.end); |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = st[c.index]; |
| +добавлено | look(p); |
| +добавлено | if (name === 'charge') { |
| +добавлено | releaseAll(d); |
| +добавлено | c.data.shielded = false; |
| +добавлено | for (let i = 0; i < CHARGE_ARCS; i++) d.arcs[i] = bolt(fx.ARENA.focus, _a.copy(fx.ARENA.focus).setY(2), 0, 0, 0.25, 0.012, 8, 1, false, seedInt(), seedInt(), 1); |
| +добавлено | d.L = fx.lights.acquire(); |
| +добавлено | if (d.L) { d.L.range = 7; d.L.color.copy(lightCol); d.L.intensity = 0; } |
| +добавлено | d.hot = fx.lights.acquire(); |
| +добавлено | if (d.hot) { d.hot.range = 9; d.hot.intensity = 0; } |
| +добавлено | d.attract = fx.forces.acquire(); |
| +добавлено | if (d.attract) { Object.assign(d.attract, { type: 2 }); d.attract.p[0] = 30; d.attract.p[1] = 0.6; d.attract.p[2] = 0.12; d.attract.p[3] = 3.5; d.attract.pos.copy(c.from); } |
| +добавлено | d.sky.set(c.to.x + (fx.rand() - 0.5) * 5, 18, c.to.z + (fx.rand() - 0.5) * 5); |
| +добавлено | d.strikes = 0; d.reseed = 0; d.damp = 0; d.thunder = false; d.punched = false; d.flash = 0; |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | for (let i = 0; i < CHARGE_ARCS; i++) { d.arcs[i]?.release(); d.arcs[i] = null; } |
| +добавлено | d.attract?.release(); d.attract = null; |
| +добавлено | if (c.form === 'Thunderstrike') startStrike(c, p, d); |
| +добавлено | else startChain(c, p, d); |
| +добавлено | } else if (name === 'done') { releaseAll(d); c.data.shielded = false; } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = st[c.index]; |
| +добавлено | look(p); |
| +добавлено | if (c.phase === 'charge') updateCharge(c, p, d, dt); |
| +добавлено | else if (c.form === 'Thunderstrike') updateStrike(c, p, d, dt); |
| +добавлено | else updateChain(c, p, d, dt); |
| +добавлено | // storm light: the frame dims with the charge and comes back through the aftermath; the |
| +добавлено | // flash decays on top of it. main clamps post.params.flash to >= 0 after every frame, so |
| +добавлено | // this is written per frame and dies with the cast. |
| +добавлено | // ponytail: last writer wins, another spell's flash during this cast is lost; add a shared gloom term if casts overlap often |
| +добавлено | const amount = c.phase === 'charge' ? c.t / c.durations.charge : c.phase === 'linger' ? 1 - c.t / c.durations.linger : 1; |
| +добавлено | d.flash *= Math.exp(-dt * 7); |
| +добавлено | fx.post.params.flash = d.flash - GLOOM * (c.form === 'Thunderstrike' ? 1 : 0.5) * amount; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | function updateCharge(c, p, d, dt) { |
| +добавлено | const k = c.t / c.durations.charge, L = d.L; |
| +добавлено | // micro-arcs over the orb, reseeded every 50 ms |
| +добавлено | d.reseed -= dt; |
| +добавлено | for (let i = 0; i < CHARGE_ARCS; i++) { |
| +добавлено | const h = d.arcs[i]; |
| +добавлено | if (!h) continue; |
| +добавлено | if (d.reseed <= 0) { |
| +добавлено | h.a.copy(c.from).add(_a.set(fx.rand() - 0.5, fx.rand() - 0.5, fx.rand() - 0.5).normalize().multiplyScalar(0.28)); |
| +добавлено | h.b.copy(c.from).add(_a.set(fx.rand() - 0.5, fx.rand() - 0.5, fx.rand() - 0.5).normalize().multiplyScalar(0.28 + 0.5 * k * fx.rand())); |
| +добавлено | h.seed = seedInt(); |
| +добавлено | } |
| +добавлено | h.intensity = 25 * p.intensity * (0.4 + 0.6 * k) * (fx.rand() < 0.75 ? 1 : 0); |
| +добавлено | } |
| +добавлено | if (d.reseed <= 0) d.reseed = 0.05; |
| +добавлено | mote.pos.copy(c.from); mote.vel.set(0, 0, 0); mote.rate(2400 * p.sparks * k, dt); |
| +добавлено | if (p.cloud > 0) { cloud.pos.copy(c.to).setY(15); cloud.radius = 5; cloud.rate(110 * p.cloud, dt); } |
| +добавлено | if (L) { L.pos.copy(c.from); L.intensity = 10 * p.intensity * k * (0.6 + 0.4 * fx.rand()); } |
| +добавлено | // the storm dampens the floor under it, up to a slick, not a flood |
| +добавлено | d.damp -= dt; |
| +добавлено | if (p.damp > 0 && c.form === 'Thunderstrike' && d.damp <= 0) { |
| +добавлено | d.damp = 0.25; |
| +добавлено | if ((fx.groundAt(c.to.x, c.to.z)?.[2] ?? 0) < 0.55) splat(c.to.x, c.to.z, 7, 0, 0, 0.25 * p.damp, 0, 'noise', c.seed % 1000); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- Thunderstrike --------------------------------------------------------------------- |
| +добавлено | function startStrike(c, p, d) { |
| +добавлено | // it strikes the highest thing there: the crown of a dummy, then the current runs down to the floor. |
| +добавлено | // Under an Aegis it is the shield's crown, and the current skates over the shell to the floor ring |
| +добавлено | d.g.copy(c.to); |
| +добавлено | d.shR = fx.spells.arcane?.inst?.shieldOver?.(c.to.x, c.to.z, d.shC) ?? 0; |
| +добавлено | if (d.shR > 0) { |
| +добавлено | c.data.shielded = true; // Aegis: this strike reports its own hits (at the return strokes) |
| +добавлено | d.g.copy(d.shC).y += d.shR; |
| +добавлено | const a = fx.rand() * Math.PI * 2, rf = Math.sqrt(Math.max(d.shR * d.shR - d.shC.y * d.shC.y, 0.01)); |
| +добавлено | d.f.set(d.shC.x + Math.cos(a) * rf, 0, d.shC.z + Math.sin(a) * rf); |
| +добавлено | } else { |
| +добавлено | if (c.target >= 0) d.g.y = fx.ARENA.dummyHeight + 0.02; |
| +добавлено | else d.g.y = Math.max(d.g.y, 0); |
| +добавлено | d.f.copy(d.g).setY(0); |
| +добавлено | } |
| +добавлено | const g = d.g; |
| +добавлено | const m = d.main = bolt(d.sky, g, p.branches, Math.round(p.branches * 1.5), p.jag, 0.028, 9, 0, false, seeded(c, 1), seeded(c, 2), 0.8); |
| +добавлено | // leader: uneven steps, the pauses and lunges of the real thing |
| +добавлено | let s = 0; |
| +добавлено | for (let i = 0; i < LEADER_STEPS; i++) { s += 0.35 + 1.3 * fx.rand(); d.rev[i] = s; } |
| +добавлено | for (let i = 0; i < LEADER_STEPS; i++) d.rev[i] *= 0.9 / s; |
| +добавлено | // strokes |
| +добавлено | d.n = Math.min(p.restrikes, PEAK.length); |
| +добавлено | let t = LEADER; |
| +добавлено | for (let i = 0; i < d.n; i++) { d.at[i] = t; t += (i === 0 ? 0.12 : 0.09) + 0.05 * fx.rand(); } |
| +добавлено | if (d.L) { d.L.type = 1; d.L.range = 28; d.L.shadow = true; if (m) hangLight(d.L, m); } |
| +добавлено | // the strike point is a point source low down: hard shadows thrown away from it, stroke by stroke. |
| +добавлено | // Off the struck dummy's axis (inside its capsule its own shadow would swallow the floor) |
| +добавлено | if (d.shR > 0) _a.subVectors(d.f, d.shC); else _a.subVectors(d.sky, d.f); |
| +добавлено | _a.y = 0; if (_a.lengthSq() < 1e-6) _a.set(1, 0, 0); |
| +добавлено | d.hp.copy(d.f).addScaledVector(_a.normalize(), d.shR > 0 ? 0.3 : 0.7).setY(0.5); |
| +добавлено | if (d.hot) { d.hot.type = 0; d.hot.shadow = true; } |
| +добавлено | d.thunderAt = LEADER + c.to.distanceTo(fx.camera.position) / SOUND_SPEED; |
| +добавлено | // upward streamers: from the strike point to the leader's tip, and failed ones off the nearest other dummies |
| +добавлено | d.ns = 0; |
| +добавлено | if (m) { |
| +добавлено | m.point(0.86, d.tips[0]); |
| +добавлено | d.streamers[d.ns++] = bolt(g, d.tips[0], 1, 1, 0.13, 0.012, 7, 0, false, seeded(c, 3), seeded(c, 4), 0.6); |
| +добавлено | for (let j = 0; j < fx.dummies.length && d.ns < MAX_STREAMERS; j++) { |
| +добавлено | const dm = fx.dummies[j].pos; |
| +добавлено | if (j === c.target || dm.distanceTo(g) > 9) continue; |
| +добавлено | _a.copy(dm).setY(fx.ARENA.dummyHeight); |
| +добавлено | d.tips[d.ns].copy(_a).add(_b.set((fx.rand() - 0.5) * 0.8, 1.1 + 0.9 * fx.rand(), (fx.rand() - 0.5) * 0.8)); |
| +добавлено | d.streamers[d.ns] = bolt(_a, d.tips[d.ns], 0, 0, 0.14, 0.009, 7, 0, false, seeded(c, 5 + j), seeded(c, 8 + j), 0); |
| +добавлено | d.ns++; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function updateStrike(c, p, d, dt) { |
| +добавлено | const m = d.main, L = d.L, hot = d.hot, T = c.phase === 'impact' ? c.t : c.durations.impact + c.t; |
| +добавлено | const dim = fx.reduced ? 0.35 : 1; |
| +добавлено | if (!m) return; |
| +добавлено | if (T < LEADER) { |
| +добавлено | // stepped leader: dim and violet, every step a pulse at the tip |
| +добавлено | const x = (T / LEADER) * LEADER_STEPS, k = Math.min(x | 0, LEADER_STEPS - 1); |
| +добавлено | const len = d.rev[k] - (k ? d.rev[k - 1] : 0), pulse = Math.exp(-(x - k) * 2.2); |
| +добавлено | m.reveal = d.rev[k]; |
| +добавлено | m.intensity = (2.5 + 130 * len * pulse) * p.intensity; |
| +добавлено | m.color.copy(leaderCol); |
| +добавлено | if (L) { |
| +добавлено | m.point(m.reveal, L.end); |
| +добавлено | L.end.y = Math.max(L.end.y, 2.8); |
| +добавлено | L.color.copy(leaderCol); |
| +добавлено | L.intensity = m.intensity * 7 * dim; |
| +добавлено | } |
| +добавлено | const u = clamp01((T - (LEADER - 0.075)) / 0.075); |
| +добавлено | for (let i = 0; i < d.ns; i++) { |
| +добавлено | const h = d.streamers[i]; |
| +добавлено | if (!h) continue; |
| +добавлено | h.reveal = u * (i ? 0.75 : 1); |
| +добавлено | h.intensity = 5 * p.intensity * (0.6 + 0.4 * pulse); |
| +добавлено | h.color.copy(leaderCol); |
| +добавлено | } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | for (let i = 0; i < d.ns; i++) { d.streamers[i]?.release(); d.streamers[i] = null; } |
| +добавлено | d.ns = 0; |
| +добавлено | while (d.strikes < d.n && T >= d.at[d.strikes]) { d.strikes++; strikeBurst(c, p, d, m); } |
| +добавлено | m.reveal = 1; |
| +добавлено | const i = d.strikes - 1, since = T - d.at[i], next = d.strikes < d.n ? d.at[d.strikes] - T : 1e9, last = d.strikes === d.n; |
| +добавлено | const peak = PEAK[i], env = 0.75 * Math.exp(-since / 0.028) + 0.25 * Math.exp(-since / 0.1); |
| +добавлено | // the channel keeps glowing between strokes: the residual, cooling |
| +добавлено | const glowing = 8 * Math.exp(-since / (last ? 0.5 : 0.3)); |
| +добавлено | const cool = last ? clamp01((since - 0.08) / 0.55) : 0; |
| +добавлено | m.color.copy(core).lerp(WARM, cool); |
| +добавлено | m.glowColor.copy(glowDim).lerp(warmGlow, cool); |
| +добавлено | m.residual = last ? clamp01((since - 0.12) / 1.25) : 0; |
| +добавлено | if (next < DART) { |
| +добавлено | // dart leader: a bright head running down the same channel, no branches |
| +добавлено | m.dart = 1 - next / DART; m.stroke = 1; m.branchLevel = 0; |
| +добавлено | m.intensity = (glowing + 4) * p.intensity; |
| +добавлено | m.width = 0.04; m.glowWidth = 9; |
| +добавлено | } else { |
| +добавлено | m.dart = -1; |
| +добавлено | m.stroke = Math.min(1, since / RISE); |
| +добавлено | m.branchLevel = i === 0 ? 0.05 + 0.5 * Math.exp(-since / 0.07) : 0.1 * Math.exp(-since / 0.03); |
| +добавлено | m.intensity = (BOLT_INTENSITY * peak * env + glowing) * p.intensity; |
| +добавлено | m.width = 0.04 + (CORE_WIDTH[i] - 0.04) * env + 0.035 * cool; |
| +добавлено | m.glowWidth = 9 + (i === 0 ? 18 : 11) * env; // the air around the stroke glows metres wide, for a frame or two |
| +добавлено | } |
| +добавлено | if (m.intensity < 0.03) m.intensity = 0; |
| +добавлено | if (!d.punched && since >= RISE) { d.punched = true; if (!fx.reduced) { fx.rig.punch(2, 0.06); fx.rig.hitstop(0.03); } } |
| +добавлено | if (L) { |
| +добавлено | // never quite black between strokes, the channel still glows; a smear of the stroke's rise |
| +добавлено | L.color.copy(lightCol).lerp(WARM, cool); |
| +добавлено | L.intensity = (m.dart >= 0 ? m.intensity * 3 : m.intensity * LIGHT_GAIN * Math.min(1, m.stroke + 0.25)) * dim; |
| +добавлено | } |
| +добавлено | if (hot) { |
| +добавлено | const ember = 5 * Math.exp(-since / 0.25); |
| +добавлено | hot.pos.copy(d.hp); |
| +добавлено | hot.color.copy(lightCol).lerp(EMBER, clamp01(since / 0.12)); |
| +добавлено | hot.intensity = (BOLT_INTENSITY * peak * env * 1.5 + ember) * p.intensity * dim; |
| +добавлено | } |
| +добавлено | if (!d.thunder && T >= d.thunderAt) { d.thunder = true; fx.rig.shake(0.55, 30); } |
| +добавлено | // crawl arcs flicker and fade, residual micro-arcs at decaying frequency |
| +добавлено | const fade = Math.max(0, 1 - (T - LEADER) / (c.durations.impact + c.durations.linger - LEADER)); |
| +добавлено | for (let j = 0; j < d.nc; j++) { |
| +добавлено | const h = d.crawl[j]; |
| +добавлено | if (!h) continue; |
| +добавлено | h.intensity = (T - LEADER < 0.1 || fx.rand() < 0.35 * fade + 0.1) ? 11 * p.intensity * fade : 0; |
| +добавлено | if (fx.rand() < 0.2) { h.branchSeed = seedInt(); h.seed = seedInt(); } |
| +добавлено | } |
| +добавлено | if (c.phase === 'linger') { |
| +добавлено | ozone.pos.copy(d.f).setY(0.6); ozone.rate(60 * fade, dt); |
| +добавлено | smoke.pos.copy(d.f).setY(0.15); smoke.radius = 0.5; smoke.rate(26 * fade * fade, dt); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function strikeBurst(c, p, d, m) { |
| +добавлено | const g = d.g, f = d.f, first = d.strikes === 1, floor = g.y > 0.5; |
| +добавлено | // three flashes at most, whatever the slider says (WCAG 2.3.1) |
| +добавлено | if (d.strikes <= 3) { fx.flash((first ? 1.1 : d.strikes === 2 ? 0.6 : 0.35) * p.intensity); d.flash = fx.post.params.flash; } |
| +добавлено | spark.pos.copy(g).setY(Math.max(g.y, 0.05)); spark.burst((first ? 130 : 40) * p.sparks); |
| +добавлено | ember.pos.copy(g).setY(Math.max(g.y, 0.05)); ember.burst((first ? 90 : 16) * p.sparks); |
| +добавлено | if (floor) { spark.pos.copy(f).setY(0.05); spark.burst((first ? 90 : 20) * p.sparks); ember.pos.copy(f).setY(0.05); ember.burst((first ? 60 : 10) * p.sparks); } |
| +добавлено | // ionised air around the contacts, and glowing round the lower channel: the halo has body |
| +добавлено | ion.pos.copy(g).setY(Math.max(g.y, 0.3)); ion.burst(first ? 18 : 7); |
| +добавлено | for (let k = 0; k < 6; k++) { m.point(0.62 + 0.06 * k, ion.pos); ion.burst(first ? 3 : 1); } |
| +добавлено | if (floor) { ion.pos.copy(f).setY(0.35); ion.burst(first ? 18 : 7); } |
| +добавлено | if (d.shR > 0) fx.spells.arcane?.inst?.hit?.(g, first ? 2 : 1.1, glow); |
| +добавлено | if (!first) return; |
| +добавлено | // the strike blasts a dust / steam puff up off the floor: a rising column, not a flat layer |
| +добавлено | smoke.pos.copy(f).setY(0.25); smoke.radius = 0.4; smoke.vel.set(0, 2.2, 0); smoke.burst(55); |
| +добавлено | smoke.radius = 0.9; smoke.vel.set(0, 0.6, 0); smoke.burst(25); smoke.vel.set(0, 0, 0); |
| +добавлено | // the channels flash the film of water off: a wet floor's mirror would otherwise hide the burn |
| +добавлено | splat(f.x, f.z, 3, 0.5, 1, -1, 0, 'lichtenberg', c.seed % 1000); |
| +добавлено | // a brief blue crawl on the surfaces, not a mark: at full strength it clips white |
| +добавлено | fx.impacts.add('electric', f, 2.2, 0.1, 0.5, glow); |
| +добавлено | fx.impacts.add('scorch', f, 1.5, 0.45, 8, SCORCH); |
| +добавлено | SHK.pos.copy(f).setY(0.05); |
| +добавлено | fx.shocks?.spawn(SHK); |
| +добавлено | // floor crawl: short snapped arcs, long ones over wet ground |
| +добавлено | const wet = isWet(f); |
| +добавлено | const n = Math.min(MAX_CRAWL, Math.round(6 * p.crawl)); |
| +добавлено | d.nc = 0; |
| +добавлено | if (d.shR > 0) { |
| +добавлено | // over the shell: three chords of the great circle from the crown down to the floor ring |
| +добавлено | _c.subVectors(f, d.shC).normalize(); |
| +добавлено | const end = Math.acos(Math.max(-1, -d.shC.y / d.shR)); |
| +добавлено | for (let i = 0; i < 3 && d.nc < MAX_CRAWL; i++) { |
| +добавлено | const t0 = (i / 3) * end, t1 = ((i + 1) / 3) * end; |
| +добавлено | _a.copy(d.shC).addScaledVector(_c, Math.sin(t0) * d.shR * 1.02).y += Math.cos(t0) * d.shR * 1.02; |
| +добавлено | _b.copy(d.shC).addScaledVector(_c, Math.sin(t1) * d.shR * 1.02).y += Math.cos(t1) * d.shR * 1.02; |
| +добавлено | _b.y = Math.max(_b.y, 0.03); |
| +добавлено | const h = bolt(_a, _b, 1, 1, 0.12, 0.02, 9, 1, false, seeded(c, 70 + i), seeded(c, 80 + i), 0.5); |
| +добавлено | if (h) d.crawl[d.nc++] = h; |
| +добавлено | } |
| +добавлено | _c.set(0, 0, 0); |
| +добавлено | } |
| +добавлено | for (let i = 0; d.nc < n; i++) { |
| +добавлено | const ang = (i / Math.max(n, 1)) * Math.PI * 2 + fx.rand(); |
| +добавлено | const len = (wet ? 4 + 5 * fx.rand() : 1 + 1.6 * fx.rand()) * p.crawl; |
| +добавлено | _b.set(f.x + Math.cos(ang) * len, 0.02, f.z + Math.sin(ang) * len); |
| +добавлено | const h = bolt(_a.copy(f).setY(0.05), _b, 2, 2, 0.16, 0.01, 7, 1, true, seeded(c, 40 + i), seeded(c, 60 + i), 0.7); |
| +добавлено | if (!h) break; |
| +добавлено | d.crawl[d.nc++] = h; |
| +добавлено | } |
| +добавлено | if (wet) { |
| +добавлено | fx.pool?.flash?.(glow, 1.5); |
| +добавлено | fx.pool?.ripple?.(f.x, f.z, 0.05, 0.5); |
| +добавлено | splat(f.x, f.z, 6, 0, 0, 0, 0.25, 'lichtenberg', c.seed % 777); |
| +добавлено | } |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dist = Math.hypot(dm.pos.x - f.x, dm.pos.z - f.z); |
| +добавлено | if (d.shR > 0 && Math.hypot(dm.pos.x - d.shC.x, dm.pos.z - d.shC.z) < d.shR) continue; // the shield holds |
| +добавлено | if (dist < (wet ? 7 : 2.5)) { dm.status.shock = SHOCK; dm.status.burn = Math.max(dm.status.burn, 0.4); dm.hit(_c.subVectors(dm.pos, f).setY(0).normalize(), 0.6); } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // wet = inside the pool or soaked (the storm's own damp stays below this) |
| +добавлено | function isWet(g) { |
| +добавлено | const pool = fx.ARENA.pool; |
| +добавлено | if (Math.abs(g.x - pool.center.x) < pool.half.x && Math.abs(g.z - pool.center.y) < pool.half.y) return true; |
| +добавлено | return (fx.groundAt(g.x, g.z)?.[2] ?? 0) > 0.65; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- Chain Arc: focus -> target -> nearest dummies ------------------------------------- |
| +добавлено | function startChain(c, p, d) { |
| +добавлено | chain(d, c); |
| +добавлено | d.nc = 0; |
| +добавлено | _a.copy(c.from); |
| +добавлено | for (let i = 0; i < d.hops; i++) { |
| +добавлено | _b.copy(fx.dummies[d.order[i]].pos).setY(1.1); |
| +добавлено | const h = bolt(_a, _b, 3, 4, p.jag * 1.25, 0.03, 8, 0, false, seeded(c, 10 + i), seeded(c, 20 + i), 0.5); |
| +добавлено | if (h) d.crawl[d.nc++] = h; |
| +добавлено | _a.copy(_b); |
| +добавлено | } |
| +добавлено | if (!d.nc) return; |
| +добавлено | // focus -> first dummy casts the shadows; the jumps beyond it just add light |
| +добавлено | if (d.L) { |
| +добавлено | const f = d.crawl[0]; |
| +добавлено | d.L.type = 1; d.L.range = 20; d.L.shadow = true; |
| +добавлено | d.L.pos.copy(f.a); |
| +добавлено | d.L.end.lerpVectors(f.a, f.b, 0.86).setY(Math.max(f.a.y, 2.2)); |
| +добавлено | } |
| +добавлено | if (d.hot) { |
| +добавлено | const l = d.crawl[d.nc - 1]; |
| +добавлено | d.hot.type = 1; d.hot.range = 14; d.hot.shadow = false; |
| +добавлено | d.hot.pos.copy(d.crawl[0].b).setY(2.2); d.hot.end.copy(l.b).setY(2.2); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function updateChain(c, p, d, dt) { |
| +добавлено | const on = c.phase === 'impact', L = d.L, hot = d.hot, dim = fx.reduced ? 0.35 : 1; |
| +добавлено | if (!d.nc) return; |
| +добавлено | d.reseed -= dt; |
| +добавлено | const reseed = d.reseed <= 0; |
| +добавлено | if (reseed) d.reseed = 0.06; |
| +добавлено | const flick = on ? 0.6 + 0.4 * fx.rand() : 0; |
| +добавлено | const decay = on ? 0 : clamp01(c.t / 0.45); |
| +добавлено | for (let i = 0; i < d.nc; i++) { |
| +добавлено | const h = d.crawl[i]; |
| +добавлено | if (!h) continue; |
| +добавлено | // the arc jumps on from dummy to dummy |
| +добавлено | h.reveal = on ? clamp01((c.t - i * 0.07) / 0.034) : 1; |
| +добавлено | if (reseed && on) { h.seed = seedInt(); h.branchSeed = seedInt(); } |
| +добавлено | h.width = on ? 0.026 + 0.018 * flick : 0.026; |
| +добавлено | h.residual = decay; |
| +добавлено | h.color.copy(core).lerp(WARM, decay); |
| +добавлено | h.glowColor.copy(glowDim).lerp(warmGlow, decay); |
| +добавлено | h.intensity = on ? 30 * flick * p.intensity : Math.max(0, h.intensity * Math.exp(-dt * 9)); |
| +добавлено | if (reseed && on && h.reveal >= 1) { |
| +добавлено | const dm = fx.dummies[d.order[i]]; |
| +добавлено | spark.pos.copy(dm.pos).setY(1.1); spark.burst(26 * p.sparks); |
| +добавлено | ember.pos.copy(dm.pos).setY(1.1); ember.burst(6 * p.sparks); |
| +добавлено | ion.pos.copy(dm.pos).setY(1.1); ion.burst(3); |
| +добавлено | dm.status.shock = SHOCK; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | const I = d.crawl[0]?.intensity ?? 0; |
| +добавлено | // an arc is a thin channel: bright, but it lights the floor around it, not the whole arena flat blue |
| +добавлено | if (L) L.intensity = I * 15 * dim; |
| +добавлено | if (hot) { hot.color.copy(lightCol); hot.intensity = I * 8 * dim; } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import { COMMON_GLSL, NOISE_GLSL, FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL } from '../../vfx/common.js'; |
| +добавлено | |
| +добавлено | // Ball lightning shaders. The body is an emissive shell drawn in the lines pass (full res, |
| +добавлено | // additive, depth tested on its own wobbling surface); the corona is a camera-facing quad in the |
| +добавлено | // half-res fx pass (emission + a heat shimmer, soft-faded against the scene). |
| +добавлено | |
| +добавлено | // three's ShaderMaterial supplies projectionMatrix, viewMatrix, cameraPosition and position |
| +добавлено | export const BODY_VERT = /* glsl */` |
| +добавлено | uniform vec4 uBall; // centre xyz, radius m |
| +добавлено | uniform vec4 uBall2; // time, wobble, seed, instability |
| +добавлено | out vec3 vWorld; |
| +добавлено | void main() { |
| +добавлено | vec3 n = normalize(position); |
| +добавлено | float t = uBall2.x, s = uBall2.z; |
| +добавлено | // three travelling sine lobes: a plasma blob that never holds still |
| +добавлено | float w = sin(dot(n, vec3(2.1, 1.3, -1.7)) * 2.0 + t * 5.3 + s) |
| +добавлено | + 0.6 * sin(dot(n, vec3(-1.1, 2.4, 0.9)) * 3.1 - t * 7.9 + s * 2.0) |
| +добавлено | + 0.35 * sin(dot(n, vec3(1.9, -2.2, 2.6)) * 4.7 + t * 12.3); |
| +добавлено | vec3 p = uBall.xyz + n * uBall.w * (1.0 + uBall2.y * w * 0.5); |
| +добавлено | vWorld = p; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(p, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const BODY_FRAG = COMMON_GLSL + NOISE_GLSL + /* glsl */` |
| +добавлено | uniform vec4 uBall; |
| +добавлено | uniform vec4 uBall2; // time, wobble, seed, instability |
| +добавлено | uniform vec4 uLook; // intensity, core size, veins, white flash |
| +добавлено | uniform vec3 uCore; |
| +добавлено | uniform vec3 uGlow; |
| +добавлено | uniform vec3 uVein; |
| +добавлено | in vec3 vWorld; |
| +добавлено | out vec4 fragColor; |
| +добавлено | |
| +добавлено | // arcs crawling round the shell: 4 tumbling great circles, each pushed off its plane by fbm so |
| +добавлено | // it reads as a jagged discharge; the line width follows the screen derivative (no aliasing) |
| +добавлено | float shellArcs(vec3 q, float t, float s) { |
| +добавлено | float acc = 0.0; |
| +добавлено | for (int k = 0; k < 4; k++) { |
| +добавлено | float fk = float(k); |
| +добавлено | vec3 h = hash31(fk * 7.13 + s * 3.1); |
| +добавлено | float a1 = t * (0.6 + 1.1 * h.x) + h.y * 6.2832, a2 = t * (0.4 + 0.7 * h.z) + fk * 1.7; |
| +добавлено | vec3 N = vec3(cos(a1) * sin(a2), cos(a2), sin(a1) * sin(a2)); |
| +добавлено | float jit = noise3(q * 0.42 + vec3(fk * 0.31, t * 0.35, s)) - 0.5; |
| +добавлено | float d = dot(q, N) + jit * 0.5; |
| +добавлено | float w = max(0.022, 1.2 * fwidth(d)); |
| +добавлено | // each arc strobes on and off on its own clock |
| +добавлено | float on = smoothstep(-0.2, 0.6, sin(t * (9.0 + 5.0 * h.y) + fk * 2.3)); |
| +добавлено | acc += exp(-d * d / (w * w)) * on * (0.022 / w); |
| +добавлено | } |
| +добавлено | return acc; |
| +добавлено | } |
| +добавлено | |
| +добавлено | void main() { |
| +добавлено | vec3 C = uBall.xyz; |
| +добавлено | vec3 V = normalize(cameraPosition - vWorld); |
| +добавлено | vec3 n = normalize(vWorld - C); |
| +добавлено | float NoV = clamp(dot(n, V), 0.0, 1.0); |
| +добавлено | float t = uBall2.x, s = uBall2.z, inst = uBall2.w; |
| +добавлено | // the view ray enters the shell at n and leaves it at nb; chord = 2R NoV, closest approach^2 = 1 - NoV^2 |
| +добавлено | vec3 nb = n - 2.0 * NoV * V; |
| +добавлено | float dc2 = 1.0 - NoV * NoV; |
| +добавлено | // the skin turns and churns, faster as it destabilises |
| +добавлено | float sp = 1.0 + 1.5 * inst, ca = cos(t * 0.6 * sp), sa = sin(t * 0.6 * sp); |
| +добавлено | mat2 rot = mat2(ca, -sa, sa, ca); |
| +добавлено | vec3 qf = n, qb = nb; |
| +добавлено | qf.xz = rot * qf.xz; qb.xz = rot * qb.xz; |
| +добавлено | float vf = shellArcs(qf, t * sp, s), vb = shellArcs(qb, t * sp, s); |
| +добавлено | // churning interior: one tap at the chord's midpoint modulates the glow along the chord |
| +добавлено | float churn = noise3((qf + qb) * 0.3 + vec3(0.0, -t * 0.25 * sp, s)); |
| +добавлено | float body = pow(NoV, 1.5) * (0.3 + 1.5 * churn); |
| +добавлено | float core = exp(-dc2 / (uLook.y * uLook.y)); |
| +добавлено | float edge = smoothstep(0.0, 0.12, NoV); |
| +добавлено | float sheath = smoothstep(0.0, 0.12, NoV) * (1.0 - smoothstep(0.12, 0.5, NoV)); // a bright skin just inside the limb |
| +добавлено | vec3 col = uGlow * (0.4 + 4.5 * body + 2.6 * sheath * (0.6 + 0.8 * churn)) |
| +добавлено | + uCore * core * (22.0 + 16.0 * inst) |
| +добавлено | + mix(uVein, uCore, 0.35) * (vf * 14.0 + vb * 5.0) * uLook.z; |
| +добавлено | col = (col * uLook.x + uCore * uLook.w * 30.0) * edge; |
| +добавлено | fragColor = vec4(col, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const HALO_VERT = FX_VERT_HEAD + /* glsl */` |
| +добавлено | uniform vec4 uBall; // centre, radius |
| +добавлено | uniform vec4 uHalo; // extent (ball radii), intensity, shimmer (m), time |
| +добавлено | out vec2 vUv; |
| +добавлено | out float vViewZ; |
| +добавлено | out vec2 vPerM; // fx-target uv per metre at this depth |
| +добавлено | void main() { |
| +добавлено | vec3 C = uBall.xyz; |
| +добавлено | vec3 f = normalize(cameraPosition - C); |
| +добавлено | vec3 r = normalize(cross(abs(f.y) < 0.99 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0), f)); |
| +добавлено | vec3 u = cross(f, r); |
| +добавлено | vec3 w = C + (r * position.x + u * position.y) * (uBall.w * uHalo.x); |
| +добавлено | vec4 v = viewMatrix * vec4(w, 1.0); |
| +добавлено | vUv = position.xy; vViewZ = -v.z; |
| +добавлено | vPerM = 0.5 * vec2(projectionMatrix[0][0], projectionMatrix[1][1]) / max(-v.z, 0.1); |
| +добавлено | gl_Position = projectionMatrix * v; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const HALO_FRAG = FX_FRAG_HEAD + COMMON_GLSL + NOISE_GLSL + FX_OUT_GLSL + /* glsl */` |
| +добавлено | uniform vec4 uHalo; |
| +добавлено | uniform vec3 uGlow; |
| +добавлено | uniform vec3 uCore; |
| +добавлено | in vec2 vUv; |
| +добавлено | in float vViewZ; |
| +добавлено | in vec2 vPerM; |
| +добавлено | void main() { |
| +добавлено | float r = length(vUv), rr = r * uHalo.x; // rr in ball radii |
| +добавлено | float rim = 1.0 - smoothstep(0.55, 1.0, r); |
| +добавлено | float halo = exp(-max(rr - 0.8, 0.0) * 2.0) * rim; |
| +добавлено | float inner = exp(-rr * rr * 2.0); |
| +добавлено | vec3 e = (uGlow * halo + uCore * inner * 0.25) * (uHalo.y * 2.2); |
| +добавлено | // ionised air shimmers just outside the shell |
| +добавлено | float ring = smoothstep(0.8, 1.4, rr) * rim; |
| +добавлено | vec3 np = vec3(vUv * 2.2, uHalo.w * 0.6); |
| +добавлено | vec2 dn = vec2(noise3(np), noise3(np.yxz + 0.37)) - 0.5; |
| +добавлено | fxOut(vec3(0.0), 0.0, e * softFade(vViewZ, 0.35), dn * (uHalo.z * ring) * vPerM, vViewZ); |
| +добавлено | }`; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { arenaSDF, LINES_BLENDING, FX_BLENDING } from '../../vfx/common.js'; |
| +добавлено | import { BODY_VERT, BODY_FRAG, HALO_VERT, HALO_FRAG } from './ball-lightning.glsl.js'; |
| +добавлено | |
| +добавлено | // Ball Lightning (SPELLBOOK, Lightning hero). Charge: micro-arcs converge on the focus and a |
| +добавлено | // plasma sphere swells out of them (overshoot), motes streaming in. Travel: a slow, wandering |
| +добавлено | // drift (two-sine lateral weave, bob, jitter, speed surges). The sphere is an analytic shell in |
| +добавлено | // the lines pass (hot white core, a chord-weighted body, ridged-noise veins crawling over the |
| +добавлено | // front AND back of the shell, a soft sheath, vertex wobble) plus a half-res corona with a heat |
| +добавлено | // shimmer. Three bolt filaments churn inside it. It throws short snapped arcs to whatever is near: |
| +добавлено | // dummies (shock), pillars (electric scorch), the pool (flash + ripple) or the floor (a small |
| +добавлено | // Lichtenberg burn), each with a line light along the arc. Close to the target it destabilises: |
| +добавлено | // wobble, flicker and arc rate climb, it shudders and swells. Impact: it collapses for 0.1 s, |
| +добавлено | // then detonates: a stepped leader of 10+ branching bolts out from the centre (to the floor, the |
| +добавлено | // air, any dummy or pillar in reach), a restrike, an expanding flash shell, a ground shock ring, |
| +добавлено | // a shock force on loose particles, sparks and molten plasma beads, a Lichtenberg scorch, floor |
| +добавлено | // crawl arcs (much longer over wet ground or the pool) that flicker out over the aftermath. |
| +добавлено | |
| +добавлено | const MAX_BALLS = 3, N_FIL = 3, N_ARC = 2, MAX_BLAST = 14, MAX_CRAWL = 8, COLLAPSE = 0.1; |
| +добавлено | const TAU = Math.PI * 2; |
| +добавлено | const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); }; |
| +добавлено | const backOut = (x) => { const u = x - 1; return 1 + 2.70158 * u * u * u + 1.70158 * u * u; }; |
| +добавлено | const flicker = (t, s) => 0.8 + 0.2 * Math.sin(t * 31 + s) * Math.sin(t * 13.7 + s * 0.3) + 0.07 * Math.sin(t * 71 + s); |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'ball-lightning', name: 'Ball Lightning', element: 'lightning', order: 10, |
| +добавлено | shape: 'projectile', radius: 0.3, |
| +добавлено | params: { |
| +добавлено | size: 1, speed: 3.6, wander: 1, intensity: 1, arcs: 1, reach: 5.5, instability: 1, blast: 1, bolts: 12, sparks: 1, |
| +добавлено | core: '#eef3ff', glow: '#5b78ff', plasma: '#a48dff', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | size: [0.4, 2.5, 0.01], speed: [1, 12, 0.1], wander: [0, 3, 0.01], intensity: [0.1, 3, 0.01], arcs: [0, 3, 0.01], |
| +добавлено | reach: [1, 9, 0.1], instability: [0, 2, 0.01], blast: [0.3, 2.5, 0.01], bolts: [0, 14, 1], sparks: [0, 3, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | 'st elmo': { core: '#e8fffb', glow: '#1fd6c8', plasma: '#7fffe6', wander: 1.4 }, |
| +добавлено | ember: { core: '#fff2dc', glow: '#ff7a1f', plasma: '#ffc35a', speed: 2.6, instability: 1.4 }, |
| +добавлено | 'violet storm': { core: '#f5e8ff', glow: '#8a3dff', plasma: '#ff6bf0', arcs: 1.8, blast: 1.4, intensity: 1.3 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 0.9, impact: 0.5, linger: 2.6, sustain: 8 }, |
| +добавлено | kinds: { |
| +добавлено | // tintable copies of lightning.spark (presets recolour them; lightning's own stay blue) |
| +добавлено | spark: { style: 'glow', drag: 0.4, gravity: 9.8, restitution: 0.4, friction: 0.35, life: [0.25, 0.9], size: [0.025, 0.008], stretch: 0.035, emissive: 6, color: ['#e8f0ff', '#6f8cff'], alpha: [1, 0], paints: 'heat', paintAmount: 0.03 }, |
| +добавлено | // molten plasma beads: dribble off the ball, bounce and skitter on the floor, burning in |
| +добавлено | bead: { style: 'glow', drag: 0.25, gravity: 9.8, restitution: 0.5, friction: 0.25, life: [0.7, 1.7], size: [0.034, 0.012], stretch: 0.012, emissive: 8, color: ['#ffffff', '#8a7dff'], alpha: [1, 0], fadeOut: 0.4, paints: 'glow', paintAmount: 0.06 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const spark = P.emitter('ball-lightning.spark', { shape: 'sphere', radius: 0.12, spread: 5, radial: 3 }); |
| +добавлено | const drip = P.emitter('ball-lightning.spark', { shape: 'sphere', radius: 0.25, spread: 1.2 }); |
| +добавлено | const bead = P.emitter('ball-lightning.bead', { shape: 'sphere', radius: 0.2, spread: 3.5, radial: 2.5 }); |
| +добавлено | const mote = P.emitter('lightning.mote', { shape: 'shell', radius: 1.5, spread: 0.15 }); |
| +добавлено | const ozone = P.emitter('lightning.ozone', { shape: 'sphere', radius: 0.35, spread: 0.3 }); |
| +добавлено | const smoke = P.emitter('lightning.smoke', { shape: 'disk', radius: 1.1, spread: 0.4 }); |
| +добавлено | const lineSpark = P.emitter('ball-lightning.spark', { shape: 'line', radius: 0.04, spread: 2 }); |
| +добавлено | |
| +добавлено | // --- look --- |
| +добавлено | const core = new THREE.Color(), glow = new THREE.Color(), plasma = new THREE.Color(), glowDim = new THREE.Color(), lightCol = new THREE.Color(); |
| +добавлено | const uCore = new THREE.Vector3(), uGlow = new THREE.Vector3(), uVein = new THREE.Vector3(); |
| +добавлено | let lastLook = ''; |
| +добавлено | function applyLook() { |
| +добавлено | const p = S.params, key = p.core + p.glow + p.plasma; |
| +добавлено | core.set(p.core); glow.set(p.glow); plasma.set(p.plasma); |
| +добавлено | glowDim.copy(glow).multiplyScalar(0.08); |
| +добавлено | lightCol.copy(core).lerp(glow, 0.55); |
| +добавлено | uCore.set(core.r, core.g, core.b); uGlow.set(glow.r, glow.g, glow.b); uVein.set(plasma.r, plasma.g, plasma.b); |
| +добавлено | if (key === lastLook) return; |
| +добавлено | lastLook = key; |
| +добавлено | P.setKind('ball-lightning.spark', { color: [core.clone().lerp(glow, 0.15), glow.clone().lerp(core, 0.25)] }); |
| +добавлено | P.setKind('ball-lightning.bead', { color: [core.clone(), plasma.clone().lerp(glow, 0.4)] }); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- plasma bodies (lines, full res) + coronas (fx, half res): one program each --- |
| +добавлено | const sphereGeo = new THREE.IcosahedronGeometry(1, 4); |
| +добавлено | const quadGeo = new THREE.PlaneGeometry(2, 2); |
| +добавлено | const balls = []; |
| +добавлено | for (let i = 0; i < MAX_BALLS; i++) { |
| +добавлено | const uBall = { value: new THREE.Vector4() }; |
| +добавлено | const body = new THREE.Mesh(sphereGeo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: BODY_VERT, fragmentShader: BODY_FRAG, ...LINES_BLENDING, |
| +добавлено | uniforms: { ...fx.U, uBall, uBall2: { value: new THREE.Vector4() }, uLook: { value: new THREE.Vector4() }, uCore: { value: uCore }, uGlow: { value: uGlow }, uVein: { value: uVein } }, |
| +добавлено | })); |
| +добавлено | const halo = new THREE.Mesh(quadGeo, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: HALO_VERT, fragmentShader: HALO_FRAG, ...FX_BLENDING, side: THREE.DoubleSide, |
| +добавлено | uniforms: { ...fx.U, uBall, uHalo: { value: new THREE.Vector4() }, uGlow: { value: uGlow }, uCore: { value: uCore } }, |
| +добавлено | })); |
| +добавлено | body.frustumCulled = halo.frustumCulled = false; |
| +добавлено | body.visible = halo.visible = false; |
| +добавлено | fx.scenes.lines.add(body); |
| +добавлено | fx.scenes.fx.add(halo); |
| +добавлено | balls.push({ body, halo, free: true, u: body.material.uniforms, h: halo.material.uniforms.uHalo.value }); |
| +добавлено | } |
| +добавлено | const takeBall = () => { for (const b of balls) if (b.free) { b.free = false; return b; } return null; }; |
| +добавлено | const dropBall = (b) => { if (b) { b.body.visible = b.halo.visible = false; b.free = true; } }; |
| +добавлено | // C centre, R radius, I intensity, wob wobble, inst instability, veins, flash, halo extent / intensity / shimmer |
| +добавлено | function poseBall(b, C, R, I, wob, inst, veins, flash, hx, hi, hd, seed) { |
| +добавлено | b.body.visible = b.halo.visible = true; |
| +добавлено | b.u.uBall.value.set(C.x, C.y, C.z, R); |
| +добавлено | b.u.uBall2.value.set(fx.U.uTime.value, wob, seed, inst); |
| +добавлено | b.u.uLook.value.set(I, 0.36 + 0.14 * inst, veins, flash); |
| +добавлено | b.h.set(hx, hi, hd, fx.U.uTime.value); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- per-cast state by cast index: nothing allocated per cast --- |
| +добавлено | const V3 = () => new THREE.Vector3(); |
| +добавлено | const st = Array.from({ length: 16 }, () => ({ |
| +добавлено | ball: null, fil: [null, null, null], arcs: Array.from({ length: N_ARC }, () => ({ h: null, age: 0, life: 0, end: V3(), dir: V3() })), |
| +добавлено | blast: new Array(MAX_BLAST).fill(null), nb: 0, crawl: new Array(MAX_CRAWL).fill(null), nc: 0, |
| +добавлено | L1: null, L2: null, force: null, trail: null, perp: V3(), g: V3(), arcEnd: V3(), |
| +добавлено | s: 0, D: 1, amp: 0, ph1: 0, ph2: 0, ph3: 0, nextArc: 0, reseed: 0, inst: 0, R: 0, kick: 0, arcLight: 0, |
| +добавлено | det: false, boom: 0, restrike: false, wet: false, bseed: 0, |
| +добавлено | })); |
| +добавлено | const _a = V3(), _b = V3(), _c = V3(), _d = V3(), _up = new THREE.Vector3(0, 1, 0); |
| +добавлено | const seedInt = () => (fx.rand() * 16777215) | 0; |
| +добавлено | const BOLT = { a: V3(), b: V3(), branches: 3, subBranches: 3, jag: 0.17, width: 0.012, color: core, glowColor: glowDim, intensity: 0, glowWidth: 9, reveal: 1, snapToArena: false, seed: 1, branchSeed: 1 }; |
| +добавлено | function bolt(a, b, branches, sub, jag, width, glowWidth, reveal, snap) { |
| +добавлено | BOLT.a.copy(a); BOLT.b.copy(b); BOLT.branches = branches; BOLT.subBranches = sub; BOLT.jag = jag; BOLT.width = width; |
| +добавлено | BOLT.glowWidth = glowWidth; BOLT.reveal = reveal; BOLT.snapToArena = snap; BOLT.seed = seedInt(); BOLT.branchSeed = seedInt(); |
| +добавлено | return fx.bolts.acquire(BOLT); |
| +добавлено | } |
| +добавлено | const SPL = { x: 0, z: 0, radius: 1, heat: 0, char: 0, glow: 0, pattern: 'lichtenberg', seed: 0 }; |
| +добавлено | const SCORCH = new THREE.Color(1, 0.4, 0.1); // impacts.add allocates its default colour when given none |
| +добавлено | function splat(x, z, radius, heat, char, glowAmt, seed) { |
| +добавлено | SPL.x = x; SPL.z = z; SPL.radius = radius; SPL.heat = heat; SPL.char = char; SPL.glow = glowAmt; SPL.seed = seed % 1000; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | const SHK = { pos: V3(), radius0: 0.3, radius1: 4, duration: 0.5, thickness: 0.5, distortion: 0.04, color: glow, intensity: 0, ground: false }; |
| +добавлено | function shock(pos, r0, r1, dur, thick, dist, intensity, ground) { |
| +добавлено | SHK.pos.copy(pos); SHK.radius0 = r0; SHK.radius1 = r1; SHK.duration = dur; SHK.thickness = thick; |
| +добавлено | SHK.distortion = dist; SHK.intensity = intensity; SHK.ground = ground; |
| +добавлено | fx.shocks.spawn(SHK); |
| +добавлено | } |
| +добавлено | const pool = fx.ARENA.pool; |
| +добавлено | const inPool = (x, z, pad) => Math.abs(x - pool.center.x) < pool.half.x + pad && Math.abs(z - pool.center.y) < pool.half.y + pad; |
| +добавлено | const isWet = (x, z) => inPool(x, z, 0) || (fx.groundAt(x, z)?.[2] ?? 0) > 0.3; |
| +добавлено | |
| +добавлено | // the drift: a two-sine weave across the line and a bob, both pinned at the ends |
| +добавлено | function pathAt(c, d, s, out) { |
| +добавлено | const env = Math.sin(Math.PI * Math.min(s, 1)), w = S.params.wander; |
| +добавлено | const lat = d.amp * w * env * (0.65 * Math.sin(TAU * 1.15 * s + d.ph1) + 0.35 * Math.sin(TAU * 2.6 * s + d.ph2)); |
| +добавлено | out.lerpVectors(c.from, c.to, Math.min(s, 1)).addScaledVector(d.perp, lat); |
| +добавлено | out.y += 0.35 * w * env * Math.sin(TAU * 1.7 * s + d.ph3); |
| +добавлено | return out; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // a snapped arc to the nearest conductor in reach (dummy, pillar, pool), else the floor |
| +добавлено | function spawnArc(c, p, d, arc) { |
| +добавлено | const C = c.pos, R = d.R, reach = p.reach * p.size; |
| +добавлено | let best = reach, kind = 0, dmi = -1; |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i].pos; |
| +добавлено | const dist = Math.hypot(dm.x - C.x, dm.z - C.z) - 0.35; |
| +добавлено | if (dist < best && fx.rand() < 0.8) { best = dist; kind = 1; dmi = i; _c.set(dm.x, 0.7 + 0.8 * fx.rand(), dm.z); } |
| +добавлено | } |
| +добавлено | for (const pl of fx.ARENA.pillars) { |
| +добавлено | const dist = Math.hypot(pl.x - C.x, pl.z - C.z) - 0.6; |
| +добавлено | if (dist < best && fx.rand() < 0.7) { |
| +добавлено | best = dist; kind = 2; |
| +добавлено | _d.set(C.x - pl.x, 0, C.z - pl.z).normalize(); |
| +добавлено | _c.set(pl.x + _d.x * 0.6, Math.min(5.5, Math.max(0.3, C.y + (fx.rand() - 0.4) * 2)), pl.z + _d.z * 0.6); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | if (inPool(C.x, C.z, reach * 0.6) && fx.rand() < 0.7) { |
| +добавлено | kind = 3; |
| +добавлено | _c.set(Math.min(Math.max(C.x + (fx.rand() - 0.5), pool.center.x - pool.half.x + 0.2), pool.center.x + pool.half.x - 0.2), pool.water, |
| +добавлено | Math.min(Math.max(C.z + (fx.rand() - 0.5), pool.center.y - pool.half.y + 0.2), pool.center.y + pool.half.y - 0.2)); |
| +добавлено | } |
| +добавлено | if (kind === 0) { |
| +добавлено | const ang = fx.rand() * TAU, r = (0.3 + 1.1 * fx.rand()) * p.size + 0.25 * C.y; |
| +добавлено | _c.set(C.x + Math.cos(ang) * r, 0.01, C.z + Math.sin(ang) * r); |
| +добавлено | } |
| +добавлено | arc.end.copy(_c); |
| +добавлено | arc.dir.subVectors(_c, C).normalize(); |
| +добавлено | _a.copy(C).addScaledVector(arc.dir, R * 0.9); |
| +добавлено | const len = _a.distanceTo(_c); |
| +добавлено | arc.h = bolt(_a, _c, len > 2.5 ? 5 : 2, len > 2.5 ? 6 : 2, 0.15 + 0.04 * fx.rand(), (0.012 + 0.003 * Math.min(len, 5)) * p.size, 10, 1, false); |
| +добавлено | if (!arc.h) return; |
| +добавлено | arc.age = 0; arc.life = 0.1 + 0.08 * fx.rand(); |
| +добавлено | // contact: sparks, a burn, whatever the target does |
| +добавлено | spark.pos.copy(_c); spark.vel.copy(arc.dir).multiplyScalar(-1.5); spark.burst(110 * p.sparks); |
| +добавлено | d.kick = 1; d.arcLight = 1; d.arcEnd.copy(_c); |
| +добавлено | if (kind === 0) { splat(_c.x, _c.z, 0.55 * p.size, 0.25, 0.55, 0.35, seedInt()); } |
| +добавлено | else if (kind === 1) { const dm = fx.dummies[dmi]; dm.status.shock = 1; dm.hit(_d.copy(arc.dir).setY(0).normalize(), 0.18); } |
| +добавлено | else if (kind === 2) { fx.impacts.add('electric', _c, 0.9, 1, 1.2, core); fx.impacts.add('scorch', _c, 0.5, 0.6, 6, SCORCH); } |
| +добавлено | else { fx.pool.flash(glow, 0.8); fx.pool.ripple(_c.x, _c.z, 0.025, 0.5); } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function releaseAll(d) { |
| +добавлено | for (let i = 0; i < N_FIL; i++) { d.fil[i]?.release(); d.fil[i] = null; } |
| +добавлено | for (const a of d.arcs) { a.h?.release(); a.h = null; } |
| +добавлено | for (let i = 0; i < d.nb; i++) d.blast[i]?.release(); |
| +добавлено | for (let i = 0; i < d.nc; i++) d.crawl[i]?.release(); |
| +добавлено | d.blast.fill(null); d.crawl.fill(null); d.nb = d.nc = 0; |
| +добавлено | d.L1?.release(); d.L2?.release(); d.force?.release(); d.trail?.release(); d.L1 = d.L2 = d.force = d.trail = null; |
| +добавлено | dropBall(d.ball); d.ball = null; |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam() { applyLook(); }, |
| +добавлено | |
| +добавлено | // slow wandering flight; detonates on reaching the target or touching anything solid |
| +добавлено | travel(c, dt) { |
| +добавлено | const p = S.params, d = st[c.index]; |
| +добавлено | const surge = Math.min(1, c.t / 0.6) * (1 + 0.28 * Math.sin(c.t * 2.3 + d.ph2)); |
| +добавлено | d.s += p.speed * surge * dt / d.D; |
| +добавлено | _b.copy(c.pos); |
| +добавлено | pathAt(c, d, d.s, c.pos); |
| +добавлено | const j = 0.035 * p.size * (1 + 2.5 * d.inst * p.instability); |
| +добавлено | c.pos.x += j * Math.sin(c.age * 7.3); c.pos.y += j * Math.sin(c.age * 9.1 + 1); c.pos.z += j * Math.sin(c.age * 6.7 + 2); |
| +добавлено | if (dt > 0) c.vel.subVectors(c.pos, _b).divideScalar(dt); |
| +добавлено | c.travelled += _b.distanceTo(c.pos); |
| +добавлено | d.inst = smooth(0, 1, 1 - (1 - d.s) * d.D / 3.2); |
| +добавлено | return d.s >= 1 || (c.travelled > 1 && arenaSDF(c.pos) < d.R * 0.7) || c.t > c.durations.travel; |
| +добавлено | }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = st[c.index]; |
| +добавлено | if (name === 'charge') { |
| +добавлено | applyLook(); |
| +добавлено | releaseAll(d); |
| +добавлено | d.ball = takeBall(); |
| +добавлено | for (let i = 0; i < N_FIL; i++) d.fil[i] = bolt(c.from, c.from, 0, 0, 0.25, 0.008 * p.size, 7, 1, false); |
| +добавлено | d.L1 = fx.lights.acquire(); |
| +добавлено | if (d.L1) { d.L1.range = 9; d.L1.intensity = 0; d.L1.color.copy(lightCol); d.L1.shadow = true; d.L1.type = 0; } |
| +добавлено | d.L2 = fx.lights.acquire(); |
| +добавлено | if (d.L2) { d.L2.range = 8; d.L2.intensity = 0; d.L2.color.copy(core).lerp(glow, 0.3); d.L2.type = 0; } |
| +добавлено | d.force = fx.forces.acquire(); |
| +добавлено | if (d.force) { d.force.type = 2; d.force.p[0] = 20; d.force.p[1] = 0.5; d.force.p[2] = 0.15; d.force.p[3] = 3; d.force.pos.copy(c.from); } |
| +добавлено | d.s = 0; d.inst = 0; d.R = 0; d.kick = 0; d.arcLight = 0; d.reseed = 0; d.det = false; d.boom = 0; d.restrike = false; |
| +добавлено | d.nextArc = 0.25; |
| +добавлено | d.ph1 = fx.rand() * TAU; d.ph2 = fx.rand() * TAU; d.ph3 = fx.rand() * TAU; d.bseed = (c.seed % 97) * 0.37; |
| +добавлено | } else if (name === 'release') { |
| +добавлено | d.D = Math.max(c.from.distanceTo(c.to), 1); |
| +добавлено | d.perp.crossVectors(c.dir, _up); |
| +добавлено | if (d.perp.lengthSq() < 1e-6) d.perp.set(1, 0, 0); |
| +добавлено | d.perp.normalize(); |
| +добавлено | d.amp = Math.min(1.7, 0.16 * d.D); |
| +добавлено | fx.rig.shake(0.1, 14); |
| +добавлено | spark.pos.copy(c.from); spark.vel.copy(c.dir).multiplyScalar(2); spark.burst(80 * p.sparks); |
| +добавлено | if (d.force) { d.force.p[0] = 6; d.force.p[2] = 0; d.force.p[3] = 1.6; } |
| +добавлено | // a faint ionised wake that draws the meander |
| +добавлено | const t = d.trail = fx.trails.acquire(); |
| +добавлено | if (t) { t.width = 0.2 * p.size; t.intensity = 2.2 * p.intensity; t.glow = 4; t.glowIntensity = 0.4; t.fadeTime = 0.5; t.distortion = 0.04; t.color.copy(glow).lerp(core, 0.3); } |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | for (const a of d.arcs) { a.h?.release(); a.h = null; } |
| +добавлено | d.trail?.release(); d.trail = null; |
| +добавлено | } else if (name === 'done') { |
| +добавлено | releaseAll(d); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = st[c.index], b = d.ball, C = c.pos; |
| +добавлено | const fl = flicker(c.age, c.seed % 17), R0 = 0.32 * p.size; |
| +добавлено | d.kick *= Math.exp(-dt * 22); |
| +добавлено | d.arcLight *= Math.exp(-dt * 30); |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge, grow = backOut(smooth(0.08, 0.85, k)); |
| +добавлено | d.R = R0 * Math.max(grow, 0.02); |
| +добавлено | if (b) poseBall(b, C, d.R, (0.35 + 0.75 * k) * fl * p.intensity, 0.08, 0, 0.4 + 0.6 * k, 0, 3.4, 1.6 * k * p.intensity, 0.02 * k, d.bseed); |
| +добавлено | // micro-arcs converge from a shrinking shell into the forming ball, reseeded at 20 Hz |
| +добавлено | d.reseed -= dt; |
| +добавлено | for (let i = 0; i < N_FIL; i++) { |
| +добавлено | const h = d.fil[i]; |
| +добавлено | if (!h) continue; |
| +добавлено | if (d.reseed <= 0) { |
| +добавлено | _a.set(fx.rand() - 0.5, fx.rand() - 0.5, fx.rand() - 0.5).normalize(); |
| +добавлено | h.a.copy(C).addScaledVector(_a, (1.1 - 0.75 * k) * p.size); h.b.copy(C); |
| +добавлено | h.seed = seedInt(); h.branches = 1; h.subBranches = 1; |
| +добавлено | } |
| +добавлено | h.intensity = 26 * p.intensity * (0.3 + 0.7 * k) * (fx.rand() < 0.75 ? 1 : 0); |
| +добавлено | } |
| +добавлено | if (d.reseed <= 0) d.reseed = 0.05; |
| +добавлено | mote.pos.copy(C); mote.vel.set(0, 0, 0); mote.rate(1700 * p.sparks * k, dt); |
| +добавлено | if (d.L1) { d.L1.pos.copy(C); d.L1.intensity = 12 * k * fl * p.intensity; } |
| +добавлено | if (d.force) d.force.pos.copy(C); |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | |
| +добавлено | if (c.phase === 'travel') { |
| +добавлено | const inst = d.inst * p.instability; |
| +добавлено | // shudders, pulses and swells as it destabilises |
| +добавлено | const R = d.R = R0 * (1 + 0.04 * Math.sin(c.age * 11) + 0.1 * inst * Math.sin(c.age * 37) * Math.sin(c.age * 5.3) + 0.12 * inst); |
| +добавлено | const I = (0.9 + 0.35 * inst) * (fl + 0.35 * d.kick) * p.intensity; |
| +добавлено | if (b) poseBall(b, C, R, I, 0.06 + 0.2 * inst, inst, 1 + 1.3 * inst, 0.25 * d.kick, 3.4, (1.5 + 0.8 * d.kick + inst) * p.intensity, 0.03 * p.size, d.bseed); |
| +добавлено | // filaments churn inside the shell |
| +добавлено | d.reseed -= dt; |
| +добавлено | for (let i = 0; i < N_FIL; i++) { |
| +добавлено | const h = d.fil[i]; |
| +добавлено | if (!h) continue; |
| +добавлено | const th = c.age * (1.3 + 0.45 * i) * (1 + inst) + i * 2.1, ph = Math.sin(c.age * (0.9 + 0.3 * i) + i) * 1.2; |
| +добавлено | _a.set(Math.cos(ph) * Math.cos(th), Math.sin(ph), Math.cos(ph) * Math.sin(th)); |
| +добавлено | h.a.copy(C).addScaledVector(_a, -0.25 * R); h.b.copy(C).addScaledVector(_a, 0.95 * R); |
| +добавлено | if (d.reseed <= 0) { h.seed = seedInt(); h.branchSeed = seedInt(); h.branches = 2; h.subBranches = 0; h.jag = 0.3; } |
| +добавлено | h.intensity = (30 + 20 * inst) * p.intensity * (0.6 + 0.4 * fx.rand()); |
| +добавлено | } |
| +добавлено | if (d.reseed <= 0) d.reseed = 0.06 - 0.03 * inst; |
| +добавлено | // snapped arcs |
| +добавлено | d.nextArc -= dt; |
| +добавлено | if (d.nextArc <= 0 && p.arcs > 0) { |
| +добавлено | for (const a of d.arcs) if (!a.h) { spawnArc(c, p, d, a); break; } |
| +добавлено | d.nextArc = (0.36 - 0.24 * Math.min(inst, 1)) * (0.6 + 0.8 * fx.rand()) / p.arcs; |
| +добавлено | } |
| +добавлено | updateArcs(c, p, d, dt); |
| +добавлено | // sheds sparks, beads and ozone along the way |
| +добавлено | drip.pos.copy(C); drip.radius = R; drip.vel.copy(c.vel).multiplyScalar(0.3); drip.rate((30 + 90 * inst) * p.sparks, dt); |
| +добавлено | bead.pos.copy(C); bead.radius = 0.8 * R; bead.vel.copy(c.vel).multiplyScalar(0.2); bead.rate((5 + 20 * inst) * p.sparks, dt); |
| +добавлено | ozone.pos.copy(C); ozone.vel.set(0, 0.2, 0); ozone.rate(45, dt); |
| +добавлено | d.trail?.push(C); |
| +добавлено | if (inPool(C.x, C.z, 0.3)) fx.pool.swirl(C.x, C.z, 0.6); |
| +добавлено | if (d.L1) { d.L1.pos.copy(C); d.L1.intensity = (13 + 8 * inst + 22 * d.kick) * fl * p.intensity; } |
| +добавлено | // a point at the strike end: a line light this low mirrors in the polished floor as a straight CG streak |
| +добавлено | if (d.L2) { d.L2.pos.lerpVectors(C, d.arcEnd, 0.7); d.L2.intensity = 22 * d.arcLight * p.intensity; } |
| +добавлено | if (d.force) d.force.pos.copy(C); |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // impact: collapse, then detonate; impact + linger: the blast plays out |
| +добавлено | if (!d.det) { |
| +добавлено | const k = Math.min(1, c.t / COLLAPSE); |
| +добавлено | const R = d.R * (1 - 0.55 * k * k * k); |
| +добавлено | if (b) poseBall(b, C, R, (1.3 + 3 * k * k) * p.intensity, 0.25 * (1 - k), 1, 2.4 * (1 - k), 0.6 * k, 3.4, (2.5 + 4 * k) * p.intensity, 0.05 * p.size, d.bseed); |
| +добавлено | for (let i = 0; i < N_FIL; i++) { const h = d.fil[i]; if (h) { h.b.copy(C); h.intensity = 30 * p.intensity * (fx.rand() < 0.8 ? 1 : 0); h.seed = seedInt(); } } |
| +добавлено | if (d.L1) d.L1.intensity = (20 + 40 * k) * p.intensity; |
| +добавлено | if (d.force) { d.force.pos.copy(C); d.force.p[0] = 40; d.force.p[3] = 3; } |
| +добавлено | if (c.t >= COLLAPSE) detonate(c, p, d); |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | const a = (d.boom += dt), bl = p.blast, fade = c.phase === 'linger' ? Math.max(0, 1 - c.t / c.durations.linger) : 1; |
| +добавлено | // the flash shell: swells fast, burns out eased |
| +добавлено | if (b) { |
| +добавлено | if (a > 0.55) { dropBall(b); d.ball = null; } |
| +добавлено | else { |
| +добавлено | const out = 1 - smooth(0.12, 0.55, a); |
| +добавлено | poseBall(b, C, d.R * (0.45 + 2.6 * bl * (1 - Math.exp(-a * 13))), (0.5 + 2.2 * Math.exp(-a / 0.05)) * out * p.intensity, 0.3, 1, 2.2 * out, 0.06 * Math.exp(-a / 0.04), 2.6, 3.2 * out * out * p.intensity, 0.06 * out * p.size, d.bseed); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // stepped leader out from the centre, then return stroke, one restrike, decay |
| +добавлено | for (let i = 0; i < d.nb; i++) { |
| +добавлено | const h = d.blast[i]; |
| +добавлено | if (!h) continue; |
| +добавлено | if (a < 0.07) { h.reveal = a / 0.07; h.intensity = 10 * p.intensity; } |
| +добавлено | else { |
| +добавлено | h.reveal = 1; |
| +добавлено | if (!d.restrike && a >= 0.16) { h.branchSeed = seedInt(); h.intensity = 55 * p.intensity; } |
| +добавлено | else if (a < 0.07 + dt * 1.5) h.intensity = 75 * p.intensity; |
| +добавлено | else h.intensity *= Math.exp(-dt * 13); |
| +добавлено | if (h.intensity < 0.2) { h.release(); d.blast[i] = null; } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | if (!d.restrike && a >= 0.16) { |
| +добавлено | d.restrike = true; |
| +добавлено | fx.flash(0.18 * p.intensity); |
| +добавлено | spark.pos.copy(C); spark.vel.set(0, 1, 0); spark.burst(500 * p.sparks); |
| +добавлено | if (d.L1) d.L1.intensity = 130 * p.intensity; |
| +добавлено | } |
| +добавлено | // floor crawl flickers out over the aftermath |
| +добавлено | for (let i = 0; i < d.nc; i++) { |
| +добавлено | const h = d.crawl[i]; |
| +добавлено | if (!h) continue; |
| +добавлено | h.intensity = fx.rand() < 0.4 * fade + 0.1 ? 32 * p.intensity * fade : 0; |
| +добавлено | if (fx.rand() < 0.2) h.branchSeed = seedInt(); |
| +добавлено | } |
| +добавлено | if (d.force) { |
| +добавлено | if (a < 0.45) { d.force.p[0] = 0.5 + 9 * bl * Math.pow(a / 0.45, 0.4); d.force.p[2] = 45 * (1 - a / 0.45); } |
| +добавлено | else { d.force.release(); d.force = null; } |
| +добавлено | } |
| +добавлено | if (d.L1) { |
| +добавлено | d.L1.intensity *= Math.exp(-dt / 0.13); |
| +добавлено | if (c.phase === 'linger') d.L1.intensity = Math.max(d.L1.intensity, 3 * fl * fade * p.intensity); |
| +добавлено | } |
| +добавлено | if (d.L2) d.L2.intensity *= Math.exp(-dt / 0.09); |
| +добавлено | if (c.phase === 'linger' && fade > 0.3) { ozone.pos.copy(d.g).setY(0.4); ozone.radius = 1.2; ozone.vel.set(0, 0.3, 0); ozone.rate(70 * fade, dt); } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | function updateArcs(c, p, d, dt) { |
| +добавлено | for (const a of d.arcs) { |
| +добавлено | const h = a.h; |
| +добавлено | if (!h) continue; |
| +добавлено | a.age += dt; |
| +добавлено | if (a.age > a.life) { h.release(); a.h = null; continue; } |
| +добавлено | h.a.copy(c.pos).addScaledVector(a.dir, d.R * 0.9); |
| +добавлено | h.intensity = (fx.rand() < 0.8 ? 1 : 0.15) * 70 * Math.sqrt(1 - a.age / a.life) * p.intensity; |
| +добавлено | if (fx.rand() < 0.35) h.branchSeed = seedInt(); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function detonate(c, p, d) { |
| +добавлено | d.det = true; d.boom = 0; |
| +добавлено | const C = c.pos, g = d.g.set(C.x, 0, C.z), bl = p.blast, s = p.size; |
| +добавлено | for (let i = 0; i < N_FIL; i++) { d.fil[i]?.release(); d.fil[i] = null; } |
| +добавлено | d.wet = isWet(g.x, g.z); |
| +добавлено | // radial bolts: to every dummy and pillar in reach first, then the floor and the air |
| +добавлено | const n = Math.min(MAX_BLAST, Math.round(p.bolts)); |
| +добавлено | d.nb = 0; |
| +добавлено | let splats = 0; |
| +добавлено | for (let i = 0; i < fx.dummies.length && d.nb < n; i++) { |
| +добавлено | const dm = fx.dummies[i].pos; |
| +добавлено | if (Math.hypot(dm.x - C.x, dm.z - C.z) < 5.5 * bl) { _c.set(dm.x, 0.6 + 0.9 * fx.rand(), dm.z); addBlast(d, C, _c, 0.024); } |
| +добавлено | } |
| +добавлено | for (const pl of fx.ARENA.pillars) { |
| +добавлено | if (d.nb >= n) break; |
| +добавлено | const dist = Math.hypot(pl.x - C.x, pl.z - C.z); |
| +добавлено | if (dist < 7 * bl) { _d.set(C.x - pl.x, 0, C.z - pl.z).normalize(); _c.set(pl.x + _d.x * 0.6, 0.6 + 2.5 * fx.rand(), pl.z + _d.z * 0.6); addBlast(d, C, _c, 0.022); fx.impacts.add('electric', _c, 1.4, 1, 1.6, core); } |
| +добавлено | } |
| +добавлено | const a0 = fx.rand() * TAU; |
| +добавлено | for (let i = d.nb, k = 0; i < n; i++, k++) { |
| +добавлено | const ang = a0 + k * 2.4 + (fx.rand() - 0.5) * 0.5; |
| +добавлено | if (k % 5 < 3) { // grounded |
| +добавлено | const r = (2.2 + 3 * fx.rand()) * bl; |
| +добавлено | _c.set(C.x + Math.cos(ang) * r, 0.01, C.z + Math.sin(ang) * r); |
| +добавлено | if (splats < 6) { splat(_c.x, _c.z, 0.9 * s, 0.3, 0.7, 0.3, seedInt()); splats++; } |
| +добавлено | } else { // into the air |
| +добавлено | const el = 0.1 + 0.55 * fx.rand(), r = (2.4 + 2 * fx.rand()) * bl; |
| +добавлено | _c.set(C.x + Math.cos(ang) * Math.cos(el) * r, C.y + Math.sin(el) * r, C.z + Math.sin(ang) * Math.cos(el) * r); |
| +добавлено | } |
| +добавлено | addBlast(d, C, _c, 0.02); |
| +добавлено | } |
| +добавлено | // floor crawl, much further over wet ground |
| +добавлено | d.nc = 0; |
| +добавлено | const nCrawl = Math.min(MAX_CRAWL, 6); |
| +добавлено | for (let i = 0; i < nCrawl; i++) { |
| +добавлено | const ang = (i / nCrawl) * TAU + fx.rand(), len = (d.wet ? 4 + 5 * fx.rand() : 1.2 + 1.8 * fx.rand()) * bl; |
| +добавлено | _a.copy(g).setY(0.05); |
| +добавлено | _b.set(g.x + Math.cos(ang) * len, 0.02, g.z + Math.sin(ang) * len); |
| +добавлено | const h = bolt(_a, _b, 2, 2, 0.18, 0.012, 8, 1, true); |
| +добавлено | if (h) d.crawl[d.nc++] = h; |
| +добавлено | } |
| +добавлено | // a burn, not a neon decal: real Lichtenberg ground marks glow for milliseconds, then stay as char |
| +добавлено | splat(g.x, g.z, 3.2 * bl, 0.8, 1, 0.3, c.seed); |
| +добавлено | if (d.wet) { fx.pool.flash(glow, 1.6); splat(g.x, g.z, 6 * bl, 0, 0, 0.7, c.seed + 777); } |
| +добавлено | if (inPool(g.x, g.z, 1.5)) fx.pool.ripple(g.x, g.z, 0.06, 1.5); |
| +добавлено | fx.impacts.add('electric', g, 3.4 * bl, 0.25, 0.7, core); |
| +добавлено | fx.impacts.add('scorch', g, 2.4 * bl, 0.9, 9, SCORCH); |
| +добавлено | shock(_a.copy(g).setY(0.04), 0.3, 6 * bl, 0.45, 0.5, 0.05, 1.3, true); |
| +добавлено | shock(C, 0.2, 3.4 * bl, 0.32, 0.5, 0.07, 0, false); |
| +добавлено | spark.pos.copy(C); spark.vel.set(0, 1.5, 0); spark.burst(2000 * p.sparks); |
| +добавлено | bead.pos.copy(C); bead.vel.set(0, 2.5, 0); bead.burst(140 * p.sparks); |
| +добавлено | smoke.pos.copy(g).setY(0.3); smoke.burst(150); |
| +добавлено | ozone.pos.copy(C); ozone.radius = 0.8; ozone.vel.set(0, 0.5, 0); ozone.burst(120); |
| +добавлено | if (d.L1) { d.L1.pos.copy(C); d.L1.range = 18 * bl; d.L1.intensity = 220 * p.intensity; } |
| +добавлено | if (d.L2) { d.L2.type = 0; d.L2.pos.copy(g).setY(0.3); d.L2.range = 9 * bl; d.L2.intensity = 50 * p.intensity; } |
| +добавлено | if (d.force) { d.force.type = 4; d.force.pos.copy(C); d.force.p[0] = 0.5; d.force.p[1] = 1.2; d.force.p[2] = 45; d.force.p[3] = 0; } |
| +добавлено | fx.flash(0.42 * p.intensity); |
| +добавлено | fx.rig.shake(0.6, 22); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(2.5, 0.07); fx.rig.hitstop(0.06); } |
| +добавлено | const RD = 4.5 * bl; |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i], dist = Math.hypot(dm.pos.x - C.x, dm.pos.z - C.z); |
| +добавлено | if (i === c.target || dist < RD) { |
| +добавлено | const k = Math.max(0, 1 - dist / RD); |
| +добавлено | dm.status.shock = 1; dm.status.burn = Math.max(dm.status.burn, 0.35 * k); |
| +добавлено | dm.hit(_d.subVectors(dm.pos, C).setY(0).normalize(), 1.1 * k); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | function addBlast(d, C, end, width) { |
| +добавлено | const p = S.params; |
| +добавлено | _d.subVectors(end, C).normalize(); |
| +добавлено | _a.copy(C).addScaledVector(_d, d.R * 0.4); |
| +добавлено | const h = bolt(_a, end, 5, 7, 0.13, width * p.size, 11, 0, false); |
| +добавлено | if (h) d.blast[d.nb++] = h; |
| +добавлено | // sparks shed down the outer half of each bolt |
| +добавлено | if (h && d.nb <= 6) { lineSpark.pos.copy(h.point(0.5, _b)); lineSpark.p.copy(end); lineSpark.burst(120 * p.sparks); } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import { ARENA, COMMON_GLSL, NOISE_GLSL, GROUND_GLSL, FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL } from '../../vfx/common.js'; |
| +добавлено | import { STRIP_GLSL } from '../../vfx/Ribbons.js'; |
| +добавлено | import { REFRACT_GLSL } from '../../vfx/Refraction.js'; |
| +добавлено | |
| +добавлено | // Thunderstorm's custom draws. All of them read one set of uniforms (the sky, in the module): |
| +добавлено | // storm one back-faced cylinder in the fx pass: a short march through the rain below the deck |
| +добавлено | // (sheets + distance fog + floor mist) and through the cloud deck itself, lit by the sky |
| +добавлено | // from above and by the flash slots from inside. Nothing here emits. |
| +добавлено | // rain instanced streaks in the fx pass, three camera-centred cascades plus splash crowns; |
| +добавлено | // radiance is the light they catch, so they vanish in the dark and go white in a flash. |
| +добавлено | // Written as emission, not media: it must stay in front of the deck, over any sky pixel |
| +добавлено | // (the lines pass is covered by the fx composite there), and it never hides what's behind. |
| +добавлено | // film a floor disc in the refract pass: the sky-cube reflection comes out, the deck goes in, |
| +добавлено | // and rain rings shake everything the wet floor shows. |
| +добавлено | |
| +добавлено | export const NF = 4; // flash slots (slot 0 is the brightest: the one that casts cloud relief) |
| +добавлено | |
| +добавлено | const f = (x) => x.toFixed(4); |
| +добавлено | const POOL = ARENA.pool; |
| +добавлено | // Same as Volumes.js: LOD 0 only, implicit gradients in a data-dependent loop make FXC unroll it. |
| +добавлено | const NO_GRADIENTS = '#define texture(s, p) textureLod(s, p, 0.0)\n'; |
| +добавлено | |
| +добавлено | const SKY_PARS = /* glsl */` |
| +добавлено | uniform vec4 uCell; // centre x, z, core radius m, spin (turns at the core) |
| +добавлено | uniform vec4 uDeck; // base y, top y, cover radius m, erosion 0..1 |
| +добавлено | uniform vec4 uRain; // amount 0..1, wind drift over one fall x, z (m), fall speed m/s |
| +добавлено | uniform vec4 uFlash[${NF}]; // position, radiance |
| +добавлено | uniform vec3 uFlashCol; |
| +добавлено | uniform vec3 uAmb0; // sky light before the storm dimmed it |
| +добавлено | uniform vec3 uSun0; |
| +добавлено | uniform vec3 uCloudCol; |
| +добавлено | vec3 flashAt(vec3 p) { |
| +добавлено | float s = 0.0; |
| +добавлено | for (int i = 0; i < ${NF}; i++) { |
| +добавлено | vec3 d = uFlash[i].xyz - p; |
| +добавлено | float x = 1.0 + dot(d, d) * 0.035; |
| +добавлено | s += uFlash[i].w / (x * sqrt(x)); // ~5 m soft core, a tail for the multiple scattering |
| +добавлено | } |
| +добавлено | return uFlashCol * s; |
| +добавлено | } |
| +добавлено | // rain sheets: long spiral arcs round the cell that lean with the wind, with denser bands falling |
| +добавлено | // through them (tile-seamless: turn wraps by 1, so the tile coordinates jump by whole numbers) |
| +добавлено | float curtain(vec3 p) { |
| +добавлено | float k = 1.0 - clamp(p.y / uDeck.x, 0.0, 1.0); |
| +добавлено | vec2 q = p.xz - uCell.xy - uRain.yz * k; |
| +добавлено | float r = length(q) + 1e-3, rn = r / uCell.z; |
| +добавлено | float turn = atan(q.y, q.x) / 6.2831853 + uCell.w / (1.0 + rn * rn) + 0.3 * k; |
| +добавлено | return texture(uNoise3D, vec3(turn * 3.0, r * 0.075 - turn, p.y * 0.028 + uTime * 0.28)).r; |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | // --- the storm: rain and cloud deck --------------------------------------------------------------- |
| +добавлено | export const STORM_VERT = FX_VERT_HEAD + /* glsl */` |
| +добавлено | out vec3 vW; |
| +добавлено | void main() { |
| +добавлено | vec4 w = modelMatrix * vec4(position, 1.0); |
| +добавлено | vW = w.xyz; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * w; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const STORM_FRAG = FX_FRAG_HEAD + NO_GRADIENTS + COMMON_GLSL + NOISE_GLSL + FX_OUT_GLSL + SKY_PARS + /* glsl */` |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec4 uShape; // wall cloud drop m, outer rise m, underside relief m, plate relief m |
| +добавлено | uniform vec4 uLook; // sigma 1/m, base ambient, seed, proxy radius m |
| +добавлено | uniform vec4 uFog; // sheets 1/m, background 1/m, far fog 1/m, floor mist 1/m |
| +добавлено | uniform vec4 uMarch; // cloud steps, rain taps, fog start m, camera-above fade |
| +добавлено | in vec3 vW; |
| +добавлено | |
| +добавлено | vec3 gScat = vec3(0.0); |
| +добавлено | float gT = 1.0, gZ = 0.0, gW = 0.0, gH = 0.0, gN = 0.5, gPlate = 0.0; |
| +добавлено | |
| +добавлено | float deckSigma(vec3 p) { |
| +добавлено | vec2 q = p.xz - uCell.xy; |
| +добавлено | float r = length(q) + 1e-3, rn = r / uCell.z; |
| +добавлено | float sp = uCell.w / (1.0 + rn * rn), a = sp * 6.2831853, ca = cos(a), sa = sin(a); |
| +добавлено | vec2 qr = vec2(ca * q.x - sa * q.y, sa * q.x + ca * q.y); |
| +добавлено | vec4 n = noise3v(vec3(qr.x, (p.y - uDeck.x) * 1.4, qr.y) * 0.047 + uLook.z); |
| +добавлено | gN = n.b; |
| +добавлено | float base = uDeck.x - uShape.x * exp(-rn * rn * 1.3) + uShape.y * smoothstep(1.2, 3.4, rn) + (n.r - 0.5) * uShape.z; |
| +добавлено | base += gPlate; |
| +добавлено | float top = uDeck.y + (n.g - 0.5) * 5.0; |
| +добавлено | float d = smoothstep(base - 0.3, base + 2.2, p.y) * (1.0 - smoothstep(top - 3.0, top, p.y)); |
| +добавлено | if (d <= 0.0) return 0.0; |
| +добавлено | // it spreads from the cell outwards in billows, and dies eroding billow by billow |
| +добавлено | d *= smoothstep(-6.0, 6.0, uDeck.z - r + (n.r - 0.5) * 18.0); |
| +добавлено | d *= smoothstep(uDeck.w - 0.1, uDeck.w + 0.3, 0.25 + n.r * 0.75 + (1.0 - uDeck.w) * 0.35) * (1.0 - uDeck.w * uDeck.w); |
| +добавлено | gH = (p.y - base) / max(top - base, 1.0); |
| +добавлено | return d * uLook.x * (0.75 + 0.5 * n.b) * uMarch.w; |
| +добавлено | } |
| +добавлено | |
| +добавлено | vec3 cloudLight(vec3 p, float s) { |
| +добавлено | float h = clamp(gH, 0.0, 1.0); |
| +добавлено | // sky light from above; the underside only gets what leaks round its billows |
| +добавлено | vec3 L = uAmb0 * mix(uLook.y * (0.3 + 1.8 * gN * gN), 1.05, h * h); |
| +добавлено | // the low sun only reaches the upper skin; the base sits in the deck's own shadow |
| +добавлено | if (uSunDir.y > 0.0) L += uSun0 * exp(-(1.0 - h) * 1.8 / max(uSunDir.y, 0.15)) * 0.7; |
| +добавлено | vec3 fl = flashAt(p); |
| +добавлено | if (dot(fl, fl) > 1e-4) { |
| +добавлено | // one tap towards the brightest flash: relief on the billows, soft through the multiple-scatter lobe |
| +добавлено | vec3 d = uFlash[0].xyz - p; |
| +добавлено | float dl = length(d) + 1e-3, st = min(2.6, dl * 0.5); |
| +добавлено | float s2 = deckSigma(p + d / dl * st), od = (s + s2) * st * 0.5; |
| +добавлено | fl *= exp(-od) + 0.4 * exp(-0.2 * od); |
| +добавлено | } |
| +добавлено | return uCloudCol * (L + fl); |
| +добавлено | } |
| +добавлено | |
| +добавлено | void marchCloud(vec3 o, vec3 rd, float a, float b) { |
| +добавлено | if (b <= a) return; |
| +добавлено | // stacked plates: laminar bands wound round the cell, the supercell "mothership" look. One tap per |
| +добавлено | // ray where it meets the base: at this relief the parallax of a per-step tap doesn't show. |
| +добавлено | float dyr = abs(rd.y) < 1e-3 ? 1e-3 : rd.y; |
| +добавлено | vec2 q = o.xz + rd.xz * clamp((uDeck.x - o.y) / dyr, a, b) - uCell.xy; |
| +добавлено | float r = length(q) + 1e-3, rn = r / uCell.z; |
| +добавлено | float turn = atan(q.y, q.x) / 6.2831853 + uCell.w * 0.75 / (1.0 + rn * rn); |
| +добавлено | gPlate = (noise3(vec3(turn * 2.0, r * 0.11 - turn, 0.37)) - 0.5) * uShape.w * smoothstep(1.0, 5.0, r); |
| +добавлено | // start where this ray can first meet the base, not where the lowest cloud anywhere could hang: |
| +добавлено | // the steps go where the cloud is |
| +добавлено | float yStart = uDeck.x - uShape.x * exp(-rn * rn * 1.3) + uShape.y * smoothstep(1.2, 3.4, rn) + gPlate - 0.6 * uShape.z - 0.5; |
| +добавлено | if (o.y < yStart && dyr > 0.0) a = max(a, (yStart - o.y) / dyr); |
| +добавлено | float L = min(b - a, 22.0); |
| +добавлено | if (L <= 0.0) return; |
| +добавлено | int N = int(uMarch.x); |
| +добавлено | float dt = L / float(N), t = a + dt * fract(52.9829189 * fract(dot(gl_FragCoord.xy, vec2(0.06711056, 0.00583715)))), len = 0.5 * dt, sP = 0.0; |
| +добавлено | vec3 Lin = uCloudCol * (uAmb0 * uLook.y + flashAt(o + rd * (a + L))); // what a ray that finds nothing in range sees |
| +добавлено | int lit = 0; |
| +добавлено | for (int i = 0; i <= 16; i++) { |
| +добавлено | if (i > N || gT < 0.015) break; |
| +добавлено | float s = sP; |
| +добавлено | if (i < N) { |
| +добавлено | vec3 p = o + rd * t; |
| +добавлено | s = deckSigma(p); |
| +добавлено | if (s > 1e-3) { if ((lit & 1) == 0) Lin = cloudLight(p, s); lit++; } |
| +добавлено | } else len = 0.5 * dt; |
| +добавлено | float sg = i == 0 ? s : 0.5 * (s + sP); |
| +добавлено | sP = s; |
| +добавлено | if (sg > 1e-4) { |
| +добавлено | float A = gT * (1.0 - exp(-sg * len)); |
| +добавлено | gScat += A * Lin; gZ += A * (t - 0.5 * len); gW += A; |
| +добавлено | gT -= A; |
| +добавлено | } |
| +добавлено | t += dt; len = dt; |
| +добавлено | } |
| +добавлено | // past the march the deck just carries on to the horizon |
| +добавлено | if (b - a > L) { float A = gT * (1.0 - exp(-(b - a - L) * 0.25 * uLook.x * smoothstep(0.0, 8.0, uDeck.z - length(o.xz + rd.xz * b - uCell.xy)))); gScat += A * Lin; gZ += A * b; gW += A; gT -= A; } |
| +добавлено | } |
| +добавлено | |
| +добавлено | void addSlab(vec3 p, float t, float od, float lightK) { |
| +добавлено | if (od < 1e-4) return; |
| +добавлено | float A = gT * (1.0 - exp(-od)); |
| +добавлено | gScat += A * uCloudCol * (mix(uAmb0, vec3(dot(uAmb0, vec3(0.3, 0.6, 0.1))), 0.6) * lightK + flashAt(p) * 0.5); |
| +добавлено | gZ += A * t; gW += A; |
| +добавлено | gT -= A; |
| +добавлено | } |
| +добавлено | |
| +добавлено | void marchRain(vec3 o, vec3 rd, float a, float b) { |
| +добавлено | float L = b - a; |
| +добавлено | if (L <= 0.0) return; |
| +добавлено | float n = uMarch.y, rain = uRain.x; |
| +добавлено | for (float i = 0.0; i < n; i += 1.0) { |
| +добавлено | float s0 = i / n, s1 = (i + 1.0) / n; |
| +добавлено | float ta = a + L * s0 * s0, tb = a + L * s1 * s1, tm = 0.5 * (ta + tb); |
| +добавлено | vec3 p = o + rd * tm; |
| +добавлено | float r = length(p.xz - uCell.xy); |
| +добавлено | float under = smoothstep(uDeck.z, uDeck.z - 8.0, r); |
| +добавлено | // a long slab crosses many sheets: it sees their average, not one sheet's edge (but the shafts |
| +добавлено | // keep their edges out to ~30 m slabs: heavy rain hangs in distinct curtains) |
| +добавлено | float sheet = mix(0.3, smoothstep(0.47, 0.7, curtain(p)), exp(-(tb - ta) * 0.035)); |
| +добавлено | addSlab(p, tm, (uFog.y + uFog.x * sheet) * rain * under * (tb - ta), 0.2); |
| +добавлено | } |
| +добавлено | // far fog: the storm swallows the horizon; analytic along the ray beyond the start distance |
| +добавлено | float farL = max(b - max(a, uMarch.z), 0.0); |
| +добавлено | addSlab(o + rd * b, b, farL * uFog.z, 0.1); |
| +добавлено | // floor mist: exact height integral along the ray, one noise tap for the rolling structure |
| +добавлено | float ya = max(o.y + rd.y * a, 0.0), yb = max(o.y + rd.y * b, 0.0), Hs = 0.55; |
| +добавлено | float od = abs(ya - yb) > 1e-3 ? Hs * abs(exp(-ya / Hs) - exp(-yb / Hs)) * L / abs(ya - yb) : exp(-ya / Hs) * L; |
| +добавлено | vec3 pe = o + rd * mix(a, b, 0.85); |
| +добавлено | float nm = noise3(vec3(pe.xz * 0.11 - uRain.yz * uTime * 0.09, uTime * 0.06).xzy); |
| +добавлено | addSlab(pe, mix(a, b, 0.85), od * uFog.w * rain * smoothstep(0.3, 0.75, nm), 0.28); |
| +добавлено | } |
| +добавлено | |
| +добавлено | void main() { |
| +добавлено | vec3 o = cameraPosition, rd = normalize(vW - o); |
| +добавлено | float cosV = dot(rd, -vec3(viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2])); |
| +добавлено | float t0 = 0.0, t1 = sceneViewZ() / cosV; |
| +добавлено | vec2 oc = o.xz - uCell.xy; |
| +добавлено | float qa = dot(rd.xz, rd.xz), qb = dot(oc, rd.xz), qc = dot(oc, oc) - uLook.w * uLook.w; |
| +добавлено | if (qa > 1e-6) { |
| +добавлено | float disc = qb * qb - qa * qc; |
| +добавлено | if (disc <= 0.0) discard; |
| +добавлено | float sq = sqrt(disc); |
| +добавлено | t0 = max((-qb - sq) / qa, 0.0); t1 = min(t1, (-qb + sq) / qa); |
| +добавлено | } else if (qc > 0.0) discard; |
| +добавлено | if (rd.y < -1e-4) t1 = min(t1, -o.y / rd.y); |
| +добавлено | if (t1 <= t0) discard; |
| +добавлено | float dy = abs(rd.y) < 1e-4 ? 1e-4 : rd.y; |
| +добавлено | float yBase = uDeck.x, yLow = yBase - uShape.x - 0.5 * uShape.z - 0.5 * uShape.w - 0.3, yTop = uDeck.y + 3.0; |
| +добавлено | float ty0 = (yLow - o.y) / dy, ty1 = (yTop - o.y) / dy, tb = (yBase - o.y) / dy; |
| +добавлено | float c0 = max(t0, min(ty0, ty1)), c1 = min(t1, max(ty0, ty1)); |
| +добавлено | if (o.y < yBase) { |
| +добавлено | // under the deck: rain, then cloud (the wall cloud hangs into the rain, behind its nearest sheets) |
| +добавлено | marchRain(o, rd, t0, dy > 0.0 ? min(t1, tb) : t1); |
| +добавлено | if (dy > 0.0) marchCloud(o, rd, c0, c1); |
| +добавлено | } else { |
| +добавлено | // in or above it: cloud, then the rain below |
| +добавлено | marchCloud(o, rd, c0, c1); |
| +добавлено | if (dy < 0.0) marchRain(o, rd, max(t0, tb), t1); |
| +добавлено | } |
| +добавлено | float alpha = 1.0 - gT; |
| +добавлено | if (alpha < 0.002) discard; |
| +добавлено | fxOut(gScat / max(alpha, 1e-4), alpha, vec3(0.0), vec2(0.0), (gW > 0.0 ? gZ / gW : t0) * cosV); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- rain: three camera-centred cascades of streaks, plus splash crowns --------------------------- |
| +добавлено | export const RAIN_VERT = FX_VERT_HEAD + COMMON_GLSL + NOISE_GLSL + STRIP_GLSL + SKY_PARS + /* glsl */` |
| +добавлено | in vec4 aSeed; |
| +добавлено | uniform vec4 uRainShape; // shutter s, width m, streak coverage, splash coverage |
| +добавлено | uniform vec4 uBurst; // carried round the cell rad, downburst spread, sheet floor, - |
| +добавлено | out vec2 vUv; |
| +добавлено | out vec3 vCol; |
| +добавлено | flat out float vKind; |
| +добавлено | |
| +добавлено | // a drop on its way down: k = 1 at the deck, 0 at the floor |
| +добавлено | vec3 dropAt(vec2 xz, float k) { |
| +добавлено | float fall = 1.0 - k; |
| +добавлено | vec2 q = xz - uCell.xy; |
| +добавлено | float rn = length(q) / uCell.z; |
| +добавлено | float a = -uBurst.x * fall / (1.0 + rn * rn), ca = cos(a), sa = sin(a); |
| +добавлено | q = vec2(ca * q.x - sa * q.y, sa * q.x + ca * q.y) * (1.0 + uBurst.y * fall * fall * fall); |
| +добавлено | vec2 w = q + uCell.xy + uRain.yz * fall; |
| +добавлено | return vec3(w.x, k * uDeck.x, w.y); |
| +добавлено | } |
| +добавлено | |
| +добавлено | void main() { |
| +добавлено | vUv = position.xy; vCol = vec3(0.0); vKind = 0.0; |
| +добавлено | gl_Position = vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | int ki = gl_InstanceID % 16; |
| +добавлено | float cas = ki < 5 ? 0.0 : ki < 10 ? 1.0 : ki < 14 ? 2.0 : 3.0; |
| +добавлено | vec3 fw = -vec3(viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2]); |
| +добавлено | vec2 fh = normalize(fw.xz + vec2(1e-4, 0.0)); |
| +добавлено | if (cas > 2.5) { |
| +добавлено | // splash crown: a short life on the floor where the camera is looking |
| +добавлено | float ph = aSeed.y + uTime * (1.0 + 0.5 * aSeed.w), cyc = floor(ph), k = (ph - cyc) / 0.16; |
| +добавлено | if (k > 1.0) return; |
| +добавлено | float D = 26.0; |
| +добавлено | vec2 ctr = cameraPosition.xz + fh * min(cameraPosition.y / max(-fw.y, 0.06), 20.0); |
| +добавлено | vec2 h = hash33(vec3(aSeed.xz * 57.0, cyc)).xy; |
| +добавлено | vec2 c = ctr - 0.5 * D + mod(h * D - (ctr - 0.5 * D), D); |
| +добавлено | float cr = length(c - uCell.xy); |
| +добавлено | float fade = smoothstep(uDeck.z, uDeck.z - 8.0, cr) * smoothstep(1.0, 0.8, length(c - ctr) / (0.5 * D)) * uRain.x * (1.0 - k * k); |
| +добавлено | if (fade < 0.01 || length(c) > ${f(ARENA.radius)}) return; |
| +добавлено | vec3 p = vec3(c.x, 0.0, c.y); |
| +добавлено | float viewZ = -(viewMatrix * vec4(p, 1.0)).z; |
| +добавлено | float s = (0.025 + 0.075 * k) * (0.7 + 0.6 * aSeed.w); |
| +добавлено | vec3 side = normalize(vec3(viewMatrix[0][0], viewMatrix[1][0], viewMatrix[2][0])); |
| +добавлено | vKind = 1.0; |
| +добавлено | vCol = (uAmb0 * 0.8 + flashAt(p + vec3(0.0, 0.5, 0.0))) * fade * uRainShape.w; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(p + side * position.x * s + vec3(0.0, position.y * s * 0.9 + 0.01, 0.0), 1.0); |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | float D = 12.0 * pow(2.0, cas); |
| +добавлено | vec2 ctr = cameraPosition.xz + fh * D * 0.4; |
| +добавлено | float v = uRain.w * (0.85 + 0.3 * aSeed.w), H = uDeck.x; |
| +добавлено | float ph = aSeed.y - uTime * v / H, cyc = floor(ph), k = ph - cyc; |
| +добавлено | vec2 h = hash33(vec3(aSeed.xz * 91.0, cyc)).xy; |
| +добавлено | vec2 c = ctr - 0.5 * D + mod(h * D - (ctr - 0.5 * D), D); |
| +добавлено | vec3 p0 = dropAt(c, k), p1 = dropAt(c, k + uRainShape.x * v / H); |
| +добавлено | vec2 de = abs(c - ctr) / (0.5 * D); |
| +добавлено | float fade = smoothstep(1.0, 0.8, max(de.x, de.y)) * smoothstep(uDeck.z, uDeck.z - 8.0, length(p0.xz - uCell.xy)) * uRain.x; |
| +добавлено | fade *= mix(uBurst.z, 1.0, smoothstep(0.42, 0.66, curtain(p0))); |
| +добавлено | // per instance, never per vertex: collapsing one end of a quad stretches it across the screen |
| +добавлено | fade *= smoothstep(0.8, 5.0, -(viewMatrix * vec4(0.5 * (p0 + p1), 1.0)).z); |
| +добавлено | if (fade < 0.012) return; |
| +добавлено | vec3 p = position.y > 0.5 ? p1 : p0; |
| +добавлено | float viewZ = max(-(viewMatrix * vec4(p, 1.0)).z, 0.05); |
| +добавлено | float aa; |
| +добавлено | float hw = wireHalfWidth(uRainShape.y * (1.0 + 1.1 * cas), viewZ, pxPerMetre(uFxResolution.y), aa); |
| +добавлено | vCol = (uAmb0 * 0.24 + flashAt(p0) * 0.3) * fade * aa * uRainShape.z; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(p + stripSide(p, p1 - p0) * position.x * hw, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const RAIN_FRAG = FX_FRAG_HEAD + COMMON_GLSL + FX_OUT_GLSL + /* glsl */` |
| +добавлено | in vec2 vUv; |
| +добавлено | in vec3 vCol; |
| +добавлено | flat in float vKind; |
| +добавлено | void main() { |
| +добавлено | float a; |
| +добавлено | if (vKind < 0.5) { |
| +добавлено | // a motion-blurred drop: soft across, brightest mid-streak, no hard ends |
| +добавлено | float x = 1.0 - vUv.x * vUv.x, y = sin(vUv.y * 3.14159265); |
| +добавлено | a = x * x * y; |
| +добавлено | } else { |
| +добавлено | // a crown from the side: a flat rim on the floor and one jet flung up off it |
| +добавлено | float rim = 1.0 - smoothstep(0.0, 0.22, abs(length(vec2(vUv.x, (vUv.y - 0.1) * 4.0)) - 0.7)); |
| +добавлено | float jet = (1.0 - smoothstep(0.0, 0.2, abs(vUv.x))) * (1.0 - vUv.y); |
| +добавлено | a = rim * 0.7 + jet; |
| +добавлено | } |
| +добавлено | fxOut(vec3(0.0), 0.0, vCol * a, vec2(0.0), 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- wet film: what the rained-on floor shows -------------------------------------------------- |
| +добавлено | const PILLARS = ARENA.pillars.map((p) => `vec2(${f(p.x)}, ${f(p.z)})`).join(', '); |
| +добавлено | export const FILM_VERT = /* glsl */` |
| +добавлено | out vec3 vW; |
| +добавлено | void main() { |
| +добавлено | vec4 w = modelMatrix * vec4(position, 1.0); |
| +добавлено | vW = w.xyz; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * w; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const FILM_FRAG = COMMON_GLSL + NOISE_GLSL + GROUND_GLSL + REFRACT_GLSL + SKY_PARS + /* glsl */` |
| +добавлено | uniform vec4 uFilm; // amount, ripple strength, reflection gain, - |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | uniform vec3 uRuneColor; |
| +добавлено | in vec3 vW; |
| +добавлено | const vec2 PIL[8] = vec2[8](${PILLARS}); |
| +добавлено | out vec4 fragColor; |
| +добавлено | |
| +добавлено | // one drop per cell at a time, each on its own clock: a ring and two crests behind it |
| +добавлено | vec2 rippleLayer(vec2 p, float cell, float seed) { |
| +добавлено | vec2 g = p / cell, id = floor(g), q = g - id - 0.5; |
| +добавлено | vec3 h = hash33(vec3(id, seed)); |
| +добавлено | float ph = fract(uTime * (0.8 + 0.8 * h.z) + h.x * 7.0), size = 0.45 + 0.55 * h.y * h.y; // mostly small drops, the odd big one |
| +добавлено | vec2 d = q - (h.xy - 0.5) * 0.36; |
| +добавлено | float r = length(d), e = (r - ph * 0.44 * size) * 30.0 / size; |
| +добавлено | return d / max(r, 1e-3) * sin(e) * exp(-e * e * 0.13) * (1.0 - ph) * (1.0 - ph) * step(0.3, h.z) * size; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // what the mirror ray meets first: a column (stone, its rune bands, the flash on it), the rim wall, |
| +добавлено | // else the deck (analytic: 8 vertical cylinders and the wall's inner face, no reflection pass) |
| +добавлено | vec3 deckMirror(vec3 p, vec3 R); |
| +добавлено | vec3 mirrorScene(vec3 p, vec3 R) { |
| +добавлено | vec2 d = R.xz; |
| +добавлено | float a = max(dot(d, d), 1e-6), tw = 1e9, ty = 1e9; |
| +добавлено | int hit = -1; |
| +добавлено | for (int i = 0; i < 8; i++) { |
| +добавлено | vec2 oc = p.xz - PIL[i]; |
| +добавлено | float b = dot(oc, d), c = dot(oc, oc) - ${f(ARENA.pillarRadius * ARENA.pillarRadius)}, disc = b * b - a * c; |
| +добавлено | if (disc > 0.0) { float t = (-b - sqrt(disc)) / a; if (t > 0.0 && t < tw) { tw = t; hit = i; } } |
| +добавлено | } |
| +добавлено | float b = dot(p.xz, d), c = dot(p.xz, p.xz) - ${f(ARENA.radius * ARENA.radius)}; |
| +добавлено | float tr = (-b + sqrt(max(b * b - a * c, 0.0))) / a; |
| +добавлено | vec3 stone = vec3(0.30, 0.27, 0.235); |
| +добавлено | if (hit >= 0 && p.y + R.y * tw < ${f(ARENA.pillarHeight)}) { |
| +добавлено | vec3 h = p + R * tw; |
| +добавлено | vec2 q = h.xz - PIL[hit]; |
| +добавлено | float gu = (atan(q.y, q.x) / 6.2831853 + 0.5) * 14.0; |
| +добавлено | float band = step(1.62, h.y) * step(h.y, 1.86) + step(4.34, h.y) * step(h.y, 4.52); |
| +добавлено | float glyph = band * smoothstep(0.42, 0.3, abs(fract(gu) - 0.5)) * (0.25 + 0.2 * sin(gu * 7.1 + float(hit) * 3.0)); |
| +добавлено | return stone * mix(0.5, 1.0, smoothstep(0.0, 1.6, h.y)) * (uAmbient + flashAt(h) * 0.6) + uRuneColor * glyph; |
| +добавлено | } |
| +добавлено | if (p.y + R.y * tr < ${f(ARENA.wallHeight)}) return stone * 0.9 * (uAmbient + flashAt(p + R * tr) * 0.6); |
| +добавлено | return deckMirror(p, R); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the deck overhead, as a mirror sees it: dark, with the flash glowing through its billows |
| +добавлено | vec3 deckMirror(vec3 p, vec3 R) { |
| +добавлено | vec3 h = p + R * ((uDeck.x - p.y) / max(R.y, 0.05)); |
| +добавлено | float n = noise3(vec3(h.x - uCell.x, 0.3, h.z - uCell.y) * 0.03); |
| +добавлено | float all_ = uFlash[0].w + uFlash[1].w + uFlash[2].w + uFlash[3].w; |
| +добавлено | return uCloudCol * (uAmb0 * mix(0.03, 0.16, n) + (flashAt(h) + uFlashCol * all_ * 0.05) * (0.3 + 1.4 * n)); |
| +добавлено | } |
| +добавлено | |
| +добавлено | void main() { |
| +добавлено | vec2 p = vW.xz; |
| +добавлено | vec2 pq = abs(p - vec2(${f(POOL.center.x)}, ${f(POOL.center.y)})); |
| +добавлено | if (pq.x < ${f(POOL.half.x - 0.05)} && pq.y < ${f(POOL.half.y - 0.05)}) discard; // overlaps the rim: stopping on it leaves bright MSAA samples |
| +добавлено | float wet = groundA(p).z, soak = smoothstep(0.02, 0.25, wet); |
| +добавлено | float mask = uFilm.x; |
| +добавлено | if (mask < 0.004) discard; |
| +добавлено | float viewZ = refrViewZ(vW); |
| +добавлено | float px = 2.0 * viewZ / (projectionMatrix[1][1] * uResolution.y); |
| +добавлено | float nl = noise3(vec3(p.x, 0.17, p.y) * 0.05); |
| +добавлено | float puddle = smoothstep(0.34, 0.62, wet * 0.55 + nl * 0.75 - 0.05); |
| +добавлено | // ripples, each layer gone before its rings shrink under the pixel grid |
| +добавлено | vec2 sl = rippleLayer(p, 0.46, 3.0) * (1.0 - smoothstep(0.01, 0.024, px)); |
| +добавлено | sl += rippleLayer(p + 0.31, 0.29, 11.0) * (1.0 - smoothstep(0.006, 0.014, px)); |
| +добавлено | sl *= uFilm.y * uRain.x * mix(0.35, 1.0, puddle) * soak; |
| +добавлено | vec3 n = normalize(vec3(-sl.x, 1.0, -sl.y)); |
| +добавлено | vec3 v = normalize(cameraPosition - vW); |
| +добавлено | // the floor shown through the shaking film, depth-validated at object edges |
| +добавлено | vec2 uv0 = gl_FragCoord.xy / uResolution, uv = uv0 + sl * 0.012; |
| +добавлено | float z1 = linearDepth(textureLod(uSceneDepth, uv, 0.0).r); |
| +добавлено | uv = mix(uv0, uv, smoothstep(viewZ - 0.3, viewZ + 0.3, z1)); |
| +добавлено | vec3 col = textureLod(uSceneColor, uv, 0.0).rgb; |
| +добавлено | // take the bright sky reflection out (the deck is in the way) and put the deck in |
| +добавлено | float rough = mix(0.22, mix(0.2, 0.05, puddle), soak), nv0 = max(v.y, 1e-3); |
| +добавлено | float F0 = fresnelRough(nv0, 0.04, rough); |
| +добавлено | vec3 R0 = reflect(-v, vec3(0.0, 1.0, 0.0)); |
| +добавлено | vec3 sky = envReflect(R0, 0.12) * F0; |
| +добавлено | col = max(col - sky * 1.5, col * 0.05); |
| +добавлено | col = mix(col, vec3(dot(col, vec3(0.3, 0.6, 0.1))) * vec3(0.92, 0.98, 1.1), 0.75); // the sky over it is grey now |
| +добавлено | vec3 R = reflect(-v, n); |
| +добавлено | float F = fresnelRough(max(dot(n, v), 0.0), 0.03, rough) * mix(0.25, mix(0.55, 1.0, puddle), soak); |
| +добавлено | if (F * uFilm.z > 0.004) col += mirrorScene(vW, R) * F * uFilm.z; |
| +добавлено | fragColor = vec4(col, mask); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- the sky behind everything: the deck closes over the horizon, so no sun or bright sky gets past --- |
| +добавлено | export const DOME_VERT = /* glsl */` |
| +добавлено | out vec3 vDir; |
| +добавлено | void main() { |
| +добавлено | vDir = position; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * (modelMatrix * vec4(position, 1.0)); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const DOME_FRAG = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | uniform vec4 uDome; // flash glow, alpha, -, - |
| +добавлено | uniform vec3 uFlashCol; |
| +добавлено | uniform vec3 uAmb0; |
| +добавлено | uniform vec3 uCloudCol; |
| +добавлено | in vec3 vDir; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | float up = normalize(vDir).y; |
| +добавлено | vec3 horizon = uAmb0 * mix(0.05, 0.02, smoothstep(-0.05, 0.4, abs(up))); |
| +добавлено | fragColor = vec4(uCloudCol * (horizon + uFlashCol * uDome.x), uDome.y); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // the pool can't be un-skied from the scene colour (it isn't in it yet): darken what's already drawn |
| +добавлено | export const SHADE_FRAG = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | uniform vec3 uShade; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { fragColor = vec4(uShade, 1.0); }`; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { STORM_VERT, STORM_FRAG, RAIN_VERT, RAIN_FRAG, FILM_VERT, FILM_FRAG, DOME_VERT, DOME_FRAG, SHADE_FRAG, NF } from './thunderstorm.glsl.js'; |
| +добавлено | import { FX_BLENDING } from '../../vfx/common.js'; |
| +добавлено | import { arenaUniforms } from '../../scene/arenaMaterial.js'; |
| +добавлено | |
| +добавлено | // Thunderstorm (wind + lightning + water). Reference: supercell and lightning-storm footage. The |
| +добавлено | // caster's orb drinks in wind and charge while a dark deck spreads out overhead from the target and |
| +добавлено | // starts to turn, flickering from inside. Then the first strike and the gust front land together, |
| +добавлено | // and the rain comes in with it, driven in sheets round the cell. Strikes keep arriving, staggered: |
| +добавлено | // stepped leader, return stroke, restrikes on the same channel, a mirrored channel in the wet floor, |
| +добавлено | // Lichtenberg burns, crawl arcs, sparks, steam, thunder arriving late. Spider lightning crawls along |
| +добавлено | // the base between them. The floor soaks, puddles fill and reflect the flashes, the pool boils with |
| +добавлено | // rain, dummies are soaked and shocked. Finally the rain thins and the deck erodes away. |
| +добавлено | // |
| +добавлено | // Nothing here emits except the bolts: rain and cloud are lit media, lit by the sky and the flashes. |
| +добавлено | // While the deck is overhead the sun and sky are really dimmed (U.uSunColor/uAmbient, restored in |
| +добавлено | // tick) and the exposure key drops, so the arena sits in the storm's shadow and flashes stand out. |
| +добавлено | // ponytail: one sky per arena; overlapping casts share it (the newest sets the centre). |
| +добавлено | |
| +добавлено | const TAU = Math.PI * 2; |
| +добавлено | const MAX_STRIKES = 4, MAX_CRAWL = 3, N_ARCS = 3, FADE = 3.2, RCYL = 75; |
| +добавлено | const STATES = 16, RAIN_MAX = 40000, RAIN_BASE = 13000; |
| +добавлено | const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); }; |
| +добавлено | const V3 = () => new THREE.Vector3(); |
| +добавлено | const _a = V3(), _b = V3(); |
| +добавлено | const SPL = { x: 0, z: 0, radius: 1, heat: 0, char: 0, wet: 0, glow: 0, pattern: 'radial', seed: 0, width: 0 }; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'thunderstorm', name: 'Thunderstorm', element: 'lightning', order: 20, hue: '#8fa6d8', |
| +добавлено | shape: 'area', |
| +добавлено | params: { |
| +добавлено | radius: 9, duration: 8, height: 9.5, strikes: 1, intensity: 1, branches: 7, jag: 0.12, spin: 1, rain: 1, sheets: 1, |
| +добавлено | wind: 1, angle: 195, gust: 1, darkness: 1, wetness: 1, core: '#dfe9ff', glow: '#6f80ff', cloud: '#8b8e92', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | radius: [5, 16, 0.1], duration: [3, 16, 0.1], height: [7.5, 14, 0.1], strikes: [0.2, 2.5, 0.01], intensity: [0.2, 2.5, 0.01], branches: [0, 8, 1], |
| +добавлено | jag: [0.04, 0.25, 0.005], spin: [0, 3, 0.01], rain: [0, 2, 0.01], sheets: [0, 2.5, 0.01], wind: [0, 2.5, 0.01], angle: [0, 360, 1], |
| +добавлено | gust: [0, 2, 0.01], darkness: [0, 1.5, 0.01], wetness: [0, 2, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | supercell: { radius: 13, spin: 1.9, strikes: 0.85, height: 10.5, rain: 0.7, sheets: 1.5, cloud: '#6f7f8c' }, |
| +добавлено | monsoon: { rain: 1.9, sheets: 1.7, strikes: 0.55, wind: 1.5, wetness: 1.6, cloud: '#8a919c' }, |
| +добавлено | 'dry lightning': { rain: 0.08, sheets: 0.3, strikes: 1.9, wetness: 0.1, gust: 1.5, cloud: '#8a7a6c', glow: '#a07dff', core: '#efe6ff' }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 1.6, impact: 0.3, linger: 11 }, |
| +добавлено | kinds: { |
| +добавлено | // the gust front's roller: spray and dust thrown along the floor, lit, never glowing |
| +добавлено | // the front's edge: thin, faint, it catches the light; it does not glow |
| +добавлено | gust: { style: 'glow', drag: 0.4, curl: 0.6, curlScale: 0.3, forceResponse: 1.2, life: [0.25, 0.5], size: [0.008, 0.003], stretch: 0.03, emissive: 0.3, color: ['#c8d4e0', '#8a98a8'], alpha: [0.5, 0], fadeIn: 0.2, fadeOut: 0.5 }, |
| +добавлено | scud: { style: 'media', drag: 1.3, buoyancy: 0.03, curl: 0.9, curlScale: 0.25, forceResponse: 1.4, life: [0.8, 1.6], size: [0.5, 2], sizePow: 0.6, color: ['#aab4c0', '#8e98a4'], alpha: [0.13, 0], density: 1.4, erosion: 0.6, fadeIn: 0.12, fadeOut: 0.5, lightResponse: 2 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles, U = fx.U, ARENA = fx.ARENA, post = fx.post; |
| +добавлено | const E = { |
| +добавлено | gather: P.emitter('wind.streak', { shape: 'shell', radius: 1.7, spread: 0.4, radial: -3.5 }), |
| +добавлено | mote: P.emitter('lightning.mote', { shape: 'shell', radius: 2.2, spread: 0.2 }), |
| +добавлено | spark: P.emitter('lightning.spark', { shape: 'sphere', radius: 0.15, spread: 6, radial: 3 }), |
| +добавлено | smoke: P.emitter('lightning.smoke', { shape: 'disk', radius: 0.8, spread: 0.4 }), |
| +добавлено | spray: P.emitter('water.spray', { shape: 'sphere', radius: 0.3, spread: 2.2 }), |
| +добавлено | steam: P.emitter('water.mist', { shape: 'disk', radius: 1.2, spread: 0.4 }), |
| +добавлено | drop: P.emitter('water.drop', { shape: 'sphere', radius: 0.25, spread: 4, radial: 2.5 }), |
| +добавлено | scud: P.emitter('thunderstorm.scud', { shape: 'ring', radius: 2, spread: 0.5 }), |
| +добавлено | gust: P.emitter('thunderstorm.gust', { shape: 'ring', radius: 1, spread: 1.2, radial: 8 }), |
| +добавлено | leaf: P.emitter('wind.leaf', { shape: 'ring', radius: 2, spread: 1.4, radial: 5 }), |
| +добавлено | }; |
| +добавлено | |
| +добавлено | // --- the sky: every cast feeds it, tick pushes it to the shared uniforms --- |
| +добавлено | const uFlash = Array.from({ length: NF }, () => new THREE.Vector4()); |
| +добавлено | const flashes = Array.from({ length: NF }, () => ({ x: 0, y: 0, z: 0, I: 0, tau: 0.1 })); |
| +добавлено | const u = { |
| +добавлено | uCell: { value: new THREE.Vector4() }, uDeck: { value: new THREE.Vector4() }, uRain: { value: new THREE.Vector4() }, |
| +добавлено | uFlash: { value: uFlash }, uFlashCol: { value: new THREE.Color() }, uAmb0: { value: V3() }, uSun0: { value: V3() }, uCloudCol: { value: new THREE.Color() }, |
| +добавлено | }; |
| +добавлено | const uShape = { value: new THREE.Vector4(1.6, 3, 1.7, 1.2) }; |
| +добавлено | const uLook = { value: new THREE.Vector4(0.5, 0.11, 0.13, RCYL) }; |
| +добавлено | const uFog = { value: new THREE.Vector4() }, uMarch = { value: new THREE.Vector4(12, 5, 26, 1) }; |
| +добавлено | |
| +добавлено | const storm = new THREE.Mesh( |
| +добавлено | new THREE.CylinderGeometry(RCYL, RCYL, 62, 32, 1, false).translate(0, 30, 0), |
| +добавлено | new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: STORM_VERT, fragmentShader: STORM_FRAG, ...FX_BLENDING, |
| +добавлено | side: THREE.BackSide, depthTest: false, depthWrite: false, |
| +добавлено | uniforms: { ...U, ...u, uShape, uLook, uFog, uMarch }, |
| +добавлено | }), |
| +добавлено | ); |
| +добавлено | storm.frustumCulled = false; storm.visible = false; |
| +добавлено | fx.scenes.fx.add(storm); |
| +добавлено | |
| +добавлено | // rain: one quad per streak, position.x across, position.y along |
| +добавлено | const rainGeo = new THREE.InstancedBufferGeometry(); |
| +добавлено | rainGeo.setAttribute('position', new THREE.Float32BufferAttribute([-1, 0, 0, 1, 0, 0, 1, 1, 0, -1, 1, 0], 3)); |
| +добавлено | rainGeo.setIndex([0, 1, 2, 0, 2, 3]); |
| +добавлено | const seeds = new Float32Array(RAIN_MAX * 4); |
| +добавлено | { let s = 12345; for (let i = 0; i < seeds.length; i++) { s = (Math.imul(s, 1664525) + 1013904223) >>> 0; seeds[i] = s / 4294967296; } } |
| +добавлено | rainGeo.setAttribute('aSeed', new THREE.InstancedBufferAttribute(seeds, 4)); |
| +добавлено | rainGeo.instanceCount = 0; |
| +добавлено | const uRainShape = { value: new THREE.Vector4(0.042, 0.005, 1.8, 0.65) }, uBurst = { value: new THREE.Vector4() }; |
| +добавлено | const rain = new THREE.Mesh(rainGeo, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: RAIN_VERT, fragmentShader: RAIN_FRAG, ...FX_BLENDING, side: THREE.DoubleSide, |
| +добавлено | uniforms: { ...U, ...u, uRainShape, uBurst }, |
| +добавлено | })); |
| +добавлено | rain.frustumCulled = false; rain.visible = false; |
| +добавлено | fx.scenes.fx.add(rain); |
| +добавлено | |
| +добавлено | // the wet floor: drawn over the floor before the other translucents, destination alpha kept |
| +добавлено | const uFilm = { value: new THREE.Vector4(0, 1, 1, 0) }; |
| +добавлено | const film = new THREE.Mesh( |
| +добавлено | new THREE.CircleGeometry(ARENA.radius, 72).rotateX(-Math.PI / 2), |
| +добавлено | new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: FILM_VERT, fragmentShader: FILM_FRAG, uniforms: { ...U, ...u, uFilm, uRuneColor: arenaUniforms.uRuneColor }, |
| +добавлено | transparent: true, depthWrite: false, polygonOffset: true, polygonOffsetFactor: -1, polygonOffsetUnits: -1, |
| +добавлено | blending: THREE.CustomBlending, blendEquation: THREE.AddEquation, blendSrc: THREE.SrcAlphaFactor, blendDst: THREE.OneMinusSrcAlphaFactor, |
| +добавлено | blendEquationAlpha: THREE.AddEquation, blendSrcAlpha: THREE.ZeroFactor, blendDstAlpha: THREE.OneFactor, |
| +добавлено | }), |
| +добавлено | ); |
| +добавлено | film.position.y = 0.006; film.renderOrder = -1; film.frustumCulled = false; film.visible = false; |
| +добавлено | fx.scenes.refract.add(film); |
| +добавлено | |
| +добавлено | // the deck closes over the horizon: only sky pixels pass the depth test |
| +добавлено | const uDome = { value: new THREE.Vector4() }; |
| +добавлено | const dome = new THREE.Mesh( |
| +добавлено | new THREE.SphereGeometry(340, 16, 10), |
| +добавлено | new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: DOME_VERT, fragmentShader: DOME_FRAG, uniforms: { ...u, uDome }, side: THREE.BackSide, |
| +добавлено | transparent: true, depthWrite: false, |
| +добавлено | blending: THREE.CustomBlending, blendEquation: THREE.AddEquation, blendSrc: THREE.SrcAlphaFactor, blendDst: THREE.OneMinusSrcAlphaFactor, |
| +добавлено | blendEquationAlpha: THREE.AddEquation, blendSrcAlpha: THREE.ZeroFactor, blendDstAlpha: THREE.OneFactor, |
| +добавлено | }), |
| +добавлено | ); |
| +добавлено | dome.renderOrder = -2; dome.frustumCulled = false; dome.visible = false; |
| +добавлено | fx.scenes.refract.add(dome); |
| +добавлено | |
| +добавлено | // the pool under the deck: it reflects no bright sky either |
| +добавлено | const uShade = { value: new THREE.Color() }; |
| +добавлено | const poolShade = new THREE.Mesh( |
| +добавлено | new THREE.PlaneGeometry(ARENA.pool.half.x * 2, ARENA.pool.half.y * 2).rotateX(-Math.PI / 2), |
| +добавлено | new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, fragmentShader: SHADE_FRAG, vertexShader: 'void main() { gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }', uniforms: { uShade }, |
| +добавлено | transparent: true, depthWrite: false, polygonOffset: true, polygonOffsetFactor: -2, polygonOffsetUnits: -2, |
| +добавлено | blending: THREE.CustomBlending, blendEquation: THREE.AddEquation, blendSrc: THREE.ZeroFactor, blendDst: THREE.SrcColorFactor, |
| +добавлено | blendEquationAlpha: THREE.AddEquation, blendSrcAlpha: THREE.ZeroFactor, blendDstAlpha: THREE.OneFactor, |
| +добавлено | }), |
| +добавлено | ); |
| +добавлено | poolShade.position.set(ARENA.pool.center.x, ARENA.pool.water + 0.012, ARENA.pool.center.y); |
| +добавлено | poolShade.renderOrder = 1; poolShade.frustumCulled = false; poolShade.visible = false; |
| +добавлено | fx.scenes.refract.add(poolShade); |
| +добавлено | |
| +добавлено | const sky = { |
| +добавлено | cover: 0, rain: 0, erode: 0, dim: 0, spin: 0, wet: 0, x: 0, z: 0, burst: 0, |
| +добавлено | wCover: 0, wRain: 0, wErode: 1, wDim: 0, wx: 0, wz: 0, live: 0, |
| +добавлено | baseSun: V3(), baseAmb: V3(), lastSun: V3(), lastAmb: V3(), baseKey: 0.18, lastKey: -1, dimmed: false, time: 0, |
| +добавлено | }; |
| +добавлено | function flashAdd(x, y, z, I, tau) { |
| +добавлено | let best = flashes[0]; |
| +добавлено | for (let i = 1; i < NF; i++) if (flashes[i].I < best.I) best = flashes[i]; |
| +добавлено | best.x = x; best.y = y; best.z = z; best.I = I * (fx.reduced ? 0.45 : 1); best.tau = tau; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // look |
| +добавлено | const core = new THREE.Color(), glow = new THREE.Color(), glowDim = new THREE.Color(); |
| +добавлено | function applyLook() { |
| +добавлено | const p = S.params; |
| +добавлено | core.set(p.core); glow.set(p.glow); glowDim.copy(glow).multiplyScalar(0.08); |
| +добавлено | u.uFlashCol.value.copy(glow).lerp(core, 0.86); |
| +добавлено | u.uCloudCol.value.set(p.cloud); |
| +добавлено | } |
| +добавлено | applyLook(); |
| +добавлено | |
| +добавлено | // --- per-cast state --- |
| +добавлено | const newStrike = () => ({ |
| +добавлено | live: false, hit: false, t: 0, leader: 0.08, left: 0, nextR: 0, bolt: null, light: null, crawl: new Array(MAX_CRAWL).fill(null), nc: 0, |
| +добавлено | a: V3(), g: V3(), dist: 0, power: 1, |
| +добавлено | }); |
| +добавлено | const states = Array.from({ length: STATES }, () => ({ |
| +добавлено | strikes: Array.from({ length: MAX_STRIKES }, newStrike), arcs: new Array(N_ARCS).fill(null), |
| +добавлено | spider: null, spiderT: 0, spiderLen: 0.5, g: V3(), glow: null, wind: null, vortex: null, shock: null, |
| +добавлено | age: 0, next: 0, count: 0, lastTarget: -1, reseed: 0, icT: 0, icLeft: 0, icx: 0, icz: 0, |
| +добавлено | gustR: 0, gustOn: false, gustHit: 0, wetT: 0, buffetT: 0, lastFlash: -1, seedF: 0, |
| +добавлено | })); |
| +добавлено | |
| +добавлено | const seedInt = () => (fx.rand() * 16777215) | 0; |
| +добавлено | const BOLT = { a: V3(), b: V3(), branches: 7, subBranches: 10, jag: 0.12, width: 0.04, color: core, glowColor: glowDim, intensity: 0, glowWidth: 10, reveal: 0, snapToArena: false, seed: 1, branchSeed: 1 }; |
| +добавлено | function bolt(a, b, branches, sub, jag, width, glowWidth, reveal, snap) { |
| +добавлено | BOLT.a.copy(a); BOLT.b.copy(b); BOLT.branches = branches; BOLT.subBranches = sub; BOLT.jag = jag; BOLT.width = width; |
| +добавлено | BOLT.glowWidth = glowWidth; BOLT.reveal = reveal; BOLT.snapToArena = snap; BOLT.seed = seedInt(); BOLT.branchSeed = seedInt(); |
| +добавлено | return fx.bolts.acquire(BOLT); |
| +добавлено | } |
| +добавлено | function splat(x, z, radius, pattern, seed, wet, heat, char, glowAmt, width) { |
| +добавлено | SPL.x = x; SPL.z = z; SPL.radius = radius; SPL.pattern = pattern; SPL.seed = seed % 1000; |
| +добавлено | SPL.wet = wet; SPL.heat = heat; SPL.char = char; SPL.glow = glowAmt; SPL.width = width; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | const SHK = { pos: V3(), radius0: 0.3, radius1: 5, duration: 0.4, thickness: 0.5, distortion: 0.05, ground: true, intensity: 0 }; |
| +добавлено | function shock(x, z, r0, r1, dur, thick, dist) { |
| +добавлено | SHK.pos.set(x, 0.04, z); SHK.radius0 = r0; SHK.radius1 = r1; SHK.duration = dur; SHK.thickness = thick; SHK.distortion = dist; |
| +добавлено | fx.shocks.spawn(SHK); |
| +добавлено | } |
| +добавлено | const inPool = (x, z, m = 0) => { |
| +добавлено | const P0 = ARENA.pool; |
| +добавлено | return Math.abs(x - P0.center.x) < P0.half.x + m && Math.abs(z - P0.center.y) < P0.half.y + m; |
| +добавлено | }; |
| +добавлено | |
| +добавлено | function endStrike(s) { |
| +добавлено | s.live = false; |
| +добавлено | s.bolt?.release(); s.bolt = null; |
| +добавлено | s.light?.release(); s.light = null; |
| +добавлено | for (let i = 0; i < s.nc; i++) s.crawl[i]?.release(); |
| +добавлено | s.crawl.fill(null); s.nc = 0; |
| +добавлено | } |
| +добавлено | function releaseAll(st) { |
| +добавлено | for (let i = 0; i < MAX_STRIKES; i++) endStrike(st.strikes[i]); |
| +добавлено | for (let i = 0; i < N_ARCS; i++) { st.arcs[i]?.release(); st.arcs[i] = null; } |
| +добавлено | st.spider?.release(); st.spider = null; |
| +добавлено | st.glow?.release(); st.glow = null; |
| +добавлено | st.wind?.release(); st.wind = null; |
| +добавлено | st.vortex?.release(); st.vortex = null; |
| +добавлено | st.shock?.release(); st.shock = null; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // where the next bolt lands: mostly the tallest or nearest thing, as lightning does |
| +добавлено | function pickTarget(c, st, p, out) { |
| +добавлено | const g = st.g, R = p.radius, roll = fx.rand(); |
| +добавлено | if (st.count === 0) { out.copy(c.to); out.y = Math.max(out.y, 0.02); return false; } |
| +добавлено | if (roll < 0.38) { |
| +добавлено | for (let k = 0; k < fx.dummies.length; k++) { |
| +добавлено | const i = (st.lastTarget + 1 + k) % fx.dummies.length, dm = fx.dummies[i]; |
| +добавлено | if (i !== st.lastTarget && Math.hypot(dm.pos.x - g.x, dm.pos.z - g.z) < R + 3) { st.lastTarget = i; out.copy(dm.pos).setY(1.2); return false; } |
| +добавлено | } |
| +добавлено | } else if (roll < 0.6) { |
| +добавлено | for (let k = 0, i0 = (fx.rand() * ARENA.pillars.length) | 0; k < ARENA.pillars.length; k++) { |
| +добавлено | const pl = ARENA.pillars[(i0 + k) % ARENA.pillars.length]; |
| +добавлено | if (Math.hypot(pl.x - g.x, pl.z - g.z) < R + 9) { out.set(pl.x, ARENA.pillarHeight, pl.z); return true; } |
| +добавлено | } |
| +добавлено | } else if (roll < 0.74) { |
| +добавлено | const P0 = ARENA.pool; |
| +добавлено | if (Math.hypot(P0.center.x - g.x, P0.center.y - g.z) < R + 7) { |
| +добавлено | out.set(P0.center.x + (fx.rand() - 0.5) * P0.half.x * 1.6, 0, P0.center.y + (fx.rand() - 0.5) * P0.half.y * 1.6); |
| +добавлено | return false; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | const a = fx.rand() * TAU, r = R * (0.25 + 0.85 * Math.sqrt(fx.rand())); |
| +добавлено | out.set(g.x + Math.cos(a) * r, 0.02, g.z + Math.sin(a) * r); |
| +добавлено | if (Math.hypot(out.x, out.z) > ARENA.radius - 1) out.multiplyScalar((ARENA.radius - 1) / Math.hypot(out.x, out.z)); |
| +добавлено | return false; |
| +добавлено | } |
| +добавлено | |
| +добавлено | function startStrike(c, st, p) { |
| +добавлено | let s = null; |
| +добавлено | for (let i = 0; i < MAX_STRIKES; i++) if (!st.strikes[i].live) { s = st.strikes[i]; break; } |
| +добавлено | if (!s) return; |
| +добавлено | pickTarget(c, st, p, s.g); |
| +добавлено | s.a.set(s.g.x + (fx.rand() - 0.5) * 5, p.height + 2.5, s.g.z + (fx.rand() - 0.5) * 5); |
| +добавлено | s.bolt = bolt(s.a, s.g, p.branches, Math.round(p.branches * 1.5), p.jag, 0.042, 10, 0, false); |
| +добавлено | if (!s.bolt) return; |
| +добавлено | s.live = true; s.hit = false; s.t = 0; s.leader = 0.06 + 0.05 * fx.rand(); |
| +добавлено | s.left = 1 + ((fx.rand() * 3) | 0); s.nextR = 0; s.power = (0.75 + 0.5 * fx.rand()) * p.intensity; |
| +добавлено | s.dist = s.g.distanceTo(fx.camera.position); |
| +добавлено | s.light = fx.lights.acquire(); |
| +добавлено | if (s.light) { s.light.type = 0; s.light.pos.lerpVectors(s.g, s.a, 0.3); s.light.range = 38; s.light.intensity = 0; s.light.color.copy(core).lerp(glow, 0.25); s.light.shadow = true; for (let i = 0; i < MAX_STRIKES; i++) { const o = st.strikes[i]; if (o !== s && o.live && o.light?.shadow) s.light.shadow = false; } } |
| +добавлено | st.count++; |
| +добавлено | // the charge in the deck lights up just before it comes down |
| +добавлено | flashAdd(s.a.x, p.height + 1, s.a.z, 7 * s.power, 0.1); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function returnStroke(c, st, p, s) { |
| +добавлено | const g = s.g, x = g.x, z = g.z, first = st.count === 1, pw = s.power; |
| +добавлено | s.hit = true; |
| +добавлено | // at most 3 screen flashes a second, however many strokes land |
| +добавлено | if (st.age - st.lastFlash > 0.36) { fx.flash((first ? 0.5 : 0.32) * Math.min(pw, 1.4)); st.lastFlash = st.age; } |
| +добавлено | flashAdd(s.a.x, p.height + 0.4, s.a.z, 24 * pw, 0.11); |
| +добавлено | flashAdd(x, 3.2, z, 12 * pw, 0.09); |
| +добавлено | const y = Math.max(g.y, 0.05); |
| +добавлено | E.spark.pos.set(x, y, z); E.spark.vel.set(0, 1, 0); E.spark.burst(first ? 700 : 300); |
| +добавлено | if (g.y < 2) { // pillar hits burn the top, not the floor |
| +добавлено | E.smoke.pos.set(x, 0.2, z); E.smoke.burst(70); |
| +добавлено | const wet = sky.wet * p.wetness; |
| +добавлено | if (wet > 0.05) { |
| +добавлено | E.spray.pos.set(x, 0.25, z); E.spray.vel.set(0, 3.2, 0); E.spray.size = 1.1; E.spray.burst(36 * Math.min(1, wet + 0.3)); |
| +добавлено | E.steam.pos.set(x, 0.15, z); E.steam.vel.set(0, 0.6, 0); E.steam.size = 1.2; E.steam.burst(14); |
| +добавлено | E.drop.pos.set(x, 0.1, z); E.drop.vel.set(0, 3.4, 0); E.drop.burst(240 * Math.min(1, wet + 0.3)); |
| +добавлено | } |
| +добавлено | splat(x, z, 2.4, 'lichtenberg', c.seed + st.count * 7, 0, 0.75, 0.8, 0.8, 0); |
| +добавлено | fx.impacts.add('electric', _a.set(x, 0.3, z), 2.8, 1, 1.4, core); |
| +добавлено | fx.impacts.add('scorch', _a.set(x, 0.1, z), 1.6, 0.6, 8); |
| +добавлено | shock(x, z, 0.2, 4.2, 0.36, 0.5, 0.045); |
| +добавлено | // crawl arcs run across the wet stone |
| +добавлено | const nCrawl = Math.min(MAX_CRAWL, 1 + ((sky.wet * 3) | 0)), len0 = 1.6 + 3.2 * Math.min(1, sky.wet); |
| +добавлено | for (let i = 0; i < nCrawl; i++) { |
| +добавлено | const an = fx.rand() * TAU, len = len0 * (0.5 + 0.7 * fx.rand()); |
| +добавлено | _a.set(x, 0.05, z); _b.set(x + Math.cos(an) * len, 0.02, z + Math.sin(an) * len); |
| +добавлено | const h = bolt(_a, _b, 2, 2, 0.18, 0.012, 7, 1, true); |
| +добавлено | if (h) s.crawl[s.nc++] = h; |
| +добавлено | } |
| +добавлено | } else fx.impacts.add('electric', _a.copy(g), 2.2, 1, 1.2, core); |
| +добавлено | if (inPool(x, z, 1.2)) { fx.pool.flash(glow, 1.8); fx.pool.ripple(Math.min(Math.max(x, ARENA.pool.center.x - ARENA.pool.half.x), ARENA.pool.center.x + ARENA.pool.half.x), z, 0.1, 0.5); } |
| +добавлено | // everything standing in the wet is on the circuit |
| +добавлено | const reach = 2.6 + 3.4 * Math.min(1, sky.wet); |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i], dist = Math.hypot(dm.pos.x - x, dm.pos.z - z); |
| +добавлено | if (dist < reach) { |
| +добавлено | dm.status.shock = 1; dm.status.burn = Math.max(dm.status.burn, 0.35 * (1 - dist / reach)); |
| +добавлено | dm.hit(_b.set(dm.pos.x - x, 0, dm.pos.z - z).normalize(), (dist < 1 ? 0.9 : 0.5) * (1 - 0.6 * dist / reach)); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // light first, thunder a sound-speed later |
| +добавлено | fx.rig.shake(0.08 + 0.08 * pw, 22); |
| +добавлено | fx.rig.shake(0.12 + 0.1 * pw, 28, Math.min(s.dist / 343, 0.2)); |
| +добавлено | if (first && !fx.reduced) { fx.rig.punch(1.6, 0.06); fx.rig.hitstop(0.04); } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function updateStrike(c, st, p, s, dt) { |
| +добавлено | s.t += dt; |
| +добавлено | const m = s.bolt; |
| +добавлено | if (s.t < s.leader) { |
| +добавлено | // the stepped leader feels its way down |
| +добавлено | m.reveal = s.t / s.leader; m.intensity = 5 * s.power; |
| +добавлено | } else { |
| +добавлено | m.reveal = 1; |
| +добавлено | if (!s.hit) { returnStroke(c, st, p, s); m.intensity = 80 * s.power; s.nextR = 0.035 + 0.04 * fx.rand(); } |
| +добавлено | else { |
| +добавлено | s.nextR -= dt; |
| +добавлено | if (s.left > 0 && s.nextR <= 0) { |
| +добавлено | // restrike: same channel, new branches, dimmer each time |
| +добавлено | s.left--; s.nextR = 0.05 + 0.07 * fx.rand(); |
| +добавлено | m.branchSeed = seedInt(); |
| +добавлено | m.intensity = (34 + 26 * fx.rand()) * s.power; |
| +добавлено | flashAdd(s.a.x, p.height + 0.6, s.a.z, 16 * s.power, 0.08); |
| +добавлено | E.spark.pos.set(s.g.x, Math.max(s.g.y, 0.05), s.g.z); E.spark.burst(70); |
| +добавлено | } else { |
| +добавлено | // continuing current: the channel holds a dying glow, then goes out |
| +добавлено | m.intensity *= Math.exp(-dt * (s.left > 0 ? 26 : 13)); |
| +добавлено | if (s.left === 0 && m.intensity < 0.5) { endStrike(s); return; } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | if (s.light) s.light.intensity = m.intensity * 2.4; |
| +добавлено | const fade = Math.min(1, m.intensity / 40); |
| +добавлено | for (let i = 0; i < s.nc; i++) { |
| +добавлено | const h = s.crawl[i]; |
| +добавлено | h.intensity = fx.rand() < 0.55 ? 30 * s.power * fade : 0; |
| +добавлено | if (fx.rand() < 0.2) h.branchSeed = seedInt(); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // spider lightning: a slow discharge crawling along the base of the deck |
| +добавлено | function startSpider(st, p) { |
| +добавлено | const a0 = fx.rand() * TAU, r0 = 3 + 8 * fx.rand(), a1 = a0 + 1 + 1.8 * fx.rand(), r1 = 6 + 12 * fx.rand(), y = p.height - 0.4; |
| +добавлено | _a.set(st.g.x + Math.cos(a0) * r0, y, st.g.z + Math.sin(a0) * r0); |
| +добавлено | _b.set(st.g.x + Math.cos(a1) * r1, y + 0.8 * fx.rand(), st.g.z + Math.sin(a1) * r1); |
| +добавлено | st.spider = bolt(_a, _b, 8, 10, 0.1, 0.018, 8, 0, false); |
| +добавлено | st.spiderT = 0; st.spiderLen = 0.28 + 0.3 * fx.rand(); |
| +добавлено | flashAdd(_b.x, y + 1, _b.z, 15 * p.intensity, 0.2); |
| +добавлено | } |
| +добавлено | function updateSpider(st, p, dt) { |
| +добавлено | const h = st.spider; |
| +добавлено | if (!h) return; |
| +добавлено | st.spiderT += dt; |
| +добавлено | const k = st.spiderT / st.spiderLen; |
| +добавлено | if (k >= 1.5) { h.release(); st.spider = null; return; } |
| +добавлено | h.reveal = Math.min(1, k * 1.15); |
| +добавлено | h.intensity = 26 * p.intensity * (k < 1 ? 0.6 + 0.4 * fx.rand() : Math.pow(1.5 - k, 2) * 4); |
| +добавлено | if (st.glow && k < 1) st.glow.pos.copy(h.b); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // in-cloud flashes: pulse groups somewhere in the deck, most of them never reach the ground |
| +добавлено | function cloudFlicker(st, p, dt, rate, scale) { |
| +добавлено | st.icT -= dt; |
| +добавлено | if (st.icT > 0) return; |
| +добавлено | if (st.icLeft > 0) { |
| +добавлено | st.icLeft--; |
| +добавлено | flashAdd(st.icx, p.height + 1.4, st.icz, (9 + 16 * fx.rand()) * p.intensity * scale, 0.05 + 0.08 * fx.rand()); |
| +добавлено | st.icT = 0.03 + 0.09 * fx.rand(); |
| +добавлено | } else { |
| +добавлено | const a = fx.rand() * TAU, r = (p.radius * 1.7) * Math.sqrt(fx.rand()); |
| +добавлено | st.icx = st.g.x + Math.cos(a) * r; st.icz = st.g.z + Math.sin(a) * r; |
| +добавлено | st.icLeft = 2 + ((fx.rand() * 3) | 0); |
| +добавлено | st.icT = (0.35 + 1.0 * fx.rand()) / Math.max(rate, 0.05); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function worldRain(c, st, p, dt, T, rain) { |
| +добавлено | const g = st.g; |
| +добавлено | // soaking: patches first, then everything; the pool fills the rest of the frame with rings |
| +добавлено | st.wetT -= dt; |
| +добавлено | if (st.wetT <= 0 && p.wetness > 0 && rain > 0.1) { |
| +добавлено | st.wetT = 0.22; |
| +добавлено | const a = fx.rand() * TAU, r = 16 * Math.sqrt(fx.rand()); |
| +добавлено | splat(Math.cos(a) * r, Math.sin(a) * r, 4 + 3 * fx.rand(), 'noise', (c.seed + (T * 40) | 0), 0.3 * p.wetness * rain, 0, 0, 0, 0); |
| +добавлено | splat(g.x * 0.5, g.z * 0.5, 22, 'radial', c.seed + 1, 0.075 * p.wetness * rain, 0, 0, 0, 0); |
| +добавлено | } |
| +добавлено | if (fx.pool && rain > 0.05) { |
| +добавлено | const P0 = ARENA.pool; |
| +добавлено | for (let i = 0; i < 3; i++) fx.pool.ripple(P0.center.x + (fx.rand() - 0.5) * 2 * P0.half.x, P0.center.y + (fx.rand() - 0.5) * 2 * P0.half.y, 0.007 * rain * p.rain, 0.07 + 0.05 * fx.rand()); |
| +добавлено | const qx = Math.max(Math.abs(g.x - P0.center.x) - P0.half.x, 0), qz = Math.max(Math.abs(g.z - P0.center.y) - P0.half.y, 0), pd = Math.hypot(qx, qz); |
| +добавлено | if (pd < p.radius + 2) fx.pool.swirl(Math.min(Math.max(g.x, P0.center.x - P0.half.x), P0.center.x + P0.half.x), Math.min(Math.max(g.z, P0.center.y - P0.half.y), P0.center.y + P0.half.y), 1.6 * rain * (1 - pd / (p.radius + 2))); |
| +добавлено | } |
| +добавлено | // soaked and buffeted |
| +добавлено | st.buffetT -= dt; |
| +добавлено | const hit = st.buffetT <= 0; |
| +добавлено | if (hit) st.buffetT = 0.36; |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i]; |
| +добавлено | dm.status.wet = Math.max(dm.status.wet, Math.min(1, T * 0.33 * p.rain * p.wetness) * rain); |
| +добавлено | if (hit && p.wind > 0) { |
| +добавлено | const dx = dm.pos.x - g.x, dz = dm.pos.z - g.z, d = Math.hypot(dx, dz); |
| +добавлено | if (d < p.radius + 4) dm.hit(_b.set(dz * 0.12 + Math.cos(p.angle * Math.PI / 180), 0, -dx * 0.12 + Math.sin(p.angle * Math.PI / 180)).normalize(), 0.11 * p.wind * rain); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function updateGust(c, st, p, dt) { |
| +добавлено | if (!st.gustOn) return; |
| +добавлено | const g = st.g, v = 17 * Math.pow(Math.max(0.25, 1 - st.gustR / 28), 0.7), die = 1 - smooth(14, 22, st.gustR); |
| +добавлено | st.gustR += v * dt; |
| +добавлено | if (st.shock) { st.shock.pos.set(g.x, 0.5, g.z); st.shock.p[0] = st.gustR; st.shock.p[1] = 1.8; st.shock.p[2] = 26 * p.gust; } |
| +добавлено | // the roller along the front |
| +добавлено | E.scud.pos.set(g.x, 0.25, g.z); E.scud.radius = st.gustR; E.scud.radial = v * 0.6; E.scud.vel.set(0, 0.5, 0); E.scud.size = 1; E.scud.rate(190 * p.gust * die, dt); |
| +добавлено | E.gust.pos.set(g.x, 0.5, g.z); E.gust.radius = st.gustR; E.gust.radial = v; E.gust.vel.set(0, 0.2, 0); E.gust.rate(320 * p.gust * die, dt); |
| +добавлено | for (let i = 0; i < fx.dummies.length && i < 30; i++) { |
| +добавлено | const dm = fx.dummies[i], bit = 1 << i; |
| +добавлено | if (st.gustHit & bit) continue; |
| +добавлено | const dx = dm.pos.x - g.x, dz = dm.pos.z - g.z, d = Math.hypot(dx, dz); |
| +добавлено | if (d < st.gustR) { st.gustHit |= bit; dm.hit(_b.set(dx, 0.1, dz).normalize(), 0.65 * p.gust); } |
| +добавлено | } |
| +добавлено | // the front crossing the pool |
| +добавлено | if (fx.pool) { |
| +добавлено | for (let i = 0; i < 2; i++) { |
| +добавлено | const a = fx.rand() * TAU, x = g.x + Math.cos(a) * st.gustR, z = g.z + Math.sin(a) * st.gustR; |
| +добавлено | if (inPool(x, z)) fx.pool.ripple(x, z, 0.03 * p.gust, 0.35); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | if (st.gustR > 22) { st.gustOn = false; st.shock?.release(); st.shock = null; } |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam() { applyLook(); }, |
| +добавлено | |
| +добавлено | tick(dt) { |
| +добавлено | // the sky follows what the casts asked for this frame, and falls away with none alive |
| +добавлено | const k = 1 - Math.exp(-dt * 5); |
| +добавлено | sky.cover += (sky.wCover - sky.cover) * k; |
| +добавлено | sky.rain += (sky.wRain - sky.rain) * k; |
| +добавлено | sky.erode += (sky.wErode - sky.erode) * k; |
| +добавлено | sky.dim += (sky.wDim - sky.dim) * k; |
| +добавлено | sky.burst *= Math.exp(-dt * 0.9); |
| +добавлено | sky.wCover = 0; sky.wRain = 0; sky.wDim = 0; |
| +добавлено | const alive = sky.cover > 0.002 || sky.live > 0; |
| +добавлено | if (sky.live > 0) { if (sky.cover < 0.02) { sky.x = sky.wx; sky.z = sky.wz; } else { sky.x += (sky.wx - sky.x) * k; sky.z += (sky.wz - sky.z) * k; } } |
| +добавлено | sky.live = 0; sky.wErode = 1; |
| +добавлено | sky.wet = Math.min(1, sky.wet + dt * 0.3 * (sky.rain > 0.1 ? sky.rain : 0)); |
| +добавлено | if (!alive) sky.wet = Math.max(0, sky.wet - dt * 0.2); |
| +добавлено | storm.visible = rain.visible = film.visible = dome.visible = poolShade.visible = alive; |
| +добавлено | for (let i = 0; i < NF; i++) { const fl = flashes[i]; fl.I *= Math.exp(-dt / fl.tau); if (fl.I < 0.01) fl.I = 0; } |
| +добавлено | // the storm's shadow on the arena: dim from wherever the sun currently is, restore exactly |
| +добавлено | const sun = U.uSunColor.value, amb = U.uAmbient.value, pp = post.params; |
| +добавлено | if (!alive && sky.dim < 0.003) { |
| +добавлено | if (sky.dimmed) { sun.copy(sky.baseSun); amb.copy(sky.baseAmb); pp.keyValue = sky.baseKey; sky.dimmed = false; sky.dim = 0; } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (!sky.dimmed || !sun.equals(sky.lastSun) || !amb.equals(sky.lastAmb) || pp.keyValue !== sky.lastKey) { |
| +добавлено | sky.baseSun.copy(sun); sky.baseAmb.copy(amb); sky.baseKey = pp.keyValue; sky.dimmed = true; |
| +добавлено | } |
| +добавлено | const dm = Math.min(1, sky.dim); |
| +добавлено | // no direct sun under the deck, and what sky gets through is cold and grey |
| +добавлено | const lum = 0.3 * sky.baseAmb.x + 0.6 * sky.baseAmb.y + 0.1 * sky.baseAmb.z; |
| +добавлено | sun.copy(sky.baseSun).multiplyScalar(1 - 0.985 * dm); |
| +добавлено | amb.set(lum * 0.86, lum * 0.95, lum * 1.12).lerp(sky.baseAmb, 1 - dm).multiplyScalar(1 - 0.62 * dm); |
| +добавлено | pp.keyValue = sky.baseKey * (1 - 0.74 * dm); |
| +добавлено | sky.lastSun.copy(sun); sky.lastAmb.copy(amb); sky.lastKey = pp.keyValue; |
| +добавлено | if (!alive) return; |
| +добавлено | |
| +добавлено | const p = S.params; |
| +добавлено | sky.time += dt; |
| +добавлено | sky.spin += dt * 0.075 * p.spin * Math.min(1, sky.cover * 1.5); |
| +добавлено | const H = p.height, fall = 12.5, drift = 7 * p.wind * H / fall * (1 + 0.4 * Math.sin(sky.time * 0.9) * Math.sin(sky.time * 0.37 + 1)); |
| +добавлено | const an = p.angle * Math.PI / 180; |
| +добавлено | u.uCell.value.set(sky.x, sky.z, p.radius, sky.spin); |
| +добавлено | u.uDeck.value.set(H, H + 8.5, 3 + (RCYL - 12) * Math.pow(sky.cover, 1.6), sky.erode); |
| +добавлено | u.uRain.value.set(sky.rain, Math.cos(an) * drift, Math.sin(an) * drift, fall); |
| +добавлено | u.uAmb0.value.set(lum * 0.92, lum * 0.97, lum * 1.05); u.uSun0.value.copy(sky.baseSun); |
| +добавлено | // brightest flash first: it casts the deck's relief |
| +добавлено | let top = 0; |
| +добавлено | for (let i = 1; i < NF; i++) if (flashes[i].I > flashes[top].I) top = i; |
| +добавлено | for (let i = 0; i < NF; i++) { const fl = flashes[i === 0 ? top : i === top ? 0 : i]; uFlash[i].set(fl.x, fl.y, fl.z, fl.I); } |
| +добавлено | storm.position.set(sky.x, 0, sky.z); |
| +добавлено | dome.position.copy(fx.camera.position); |
| +добавлено | uDome.value.set(maxFlash() * 0.012, smooth(0.15, 0.85, sky.cover) * (1 - smooth(0.15, 0.8, sky.erode)), 0, 0); |
| +добавлено | { const a = Math.min(1, sky.cover * 1.2) * (1 - sky.erode * sky.erode); uShade.value.setRGB(1 - 0.86 * a, 1 - 0.76 * a, 1 - 0.52 * a); } // the dusk it mirrored goes neutral and dark |
| +добавлено | const Q = fx.Q, camFade = 1 - 0.88 * smooth(H + 6, H + 18, fx.camera.position.y); // from above it's a thin veil, or nothing shows |
| +добавлено | uMarch.value.set(Math.min(16, Math.max(6, Math.round(Q.volumeSteps * Q.auto.steps * 0.35))), Q.volumeSteps >= 24 ? 4 : 3, 26, camFade); |
| +добавлено | uFog.value.set(0.042 * p.sheets, 0.001, 0.035 * Math.max(sky.rain, 0.85 * smooth(0.5, 1, sky.cover) * (1 - sky.erode)) * Math.min(1.3, 0.25 + 0.75 * p.rain + 0.3 * p.sheets), 0.012).multiplyScalar(camFade); |
| +добавлено | uBurst.value.set(0.45 * p.spin, 0.02 + 0.14 * sky.burst * p.gust, Math.max(0.06, 0.55 - 0.4 * p.sheets), 0); |
| +добавлено | uFilm.value.set(smooth(0.35, 1, sky.cover) * (1 - sky.erode * sky.erode), 0.55 * p.rain, 1, 0); |
| +добавлено | rainGeo.instanceCount = sky.rain > 0.01 ? Math.min(RAIN_MAX, Math.round(RAIN_BASE * p.rain * Q.emission * Q.auto.emission)) : 0; |
| +добавлено | rain.visible = rainGeo.instanceCount > 0; |
| +добавлено | }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, st = states[c.index]; |
| +добавлено | if (name === 'charge') { |
| +добавлено | applyLook(); |
| +добавлено | releaseAll(st); |
| +добавлено | c.durations.linger = p.duration + FADE; |
| +добавлено | st.g.set(c.to.x, 0, c.to.z); |
| +добавлено | st.age = 0; st.next = 0; st.count = 0; st.lastTarget = c.target; st.reseed = 0; |
| +добавлено | st.icT = 0.3; st.icLeft = 0; st.gustR = 0; st.gustOn = false; st.gustHit = 0; st.wetT = 0; st.buffetT = 0; st.lastFlash = -1; |
| +добавлено | st.seedF = (c.seed % 997) / 997; |
| +добавлено | uLook.value.z = 0.13 + st.seedF * 0.5; |
| +добавлено | for (let i = 0; i < N_ARCS; i++) st.arcs[i] = bolt(c.from, c.from, 0, 0, 0.25, 0.012, 8, 1, false); |
| +добавлено | st.glow = fx.lights.acquire(); |
| +добавлено | if (st.glow) { st.glow.range = 34; st.glow.intensity = 0; st.glow.color.copy(u.uFlashCol.value); st.glow.pos.set(st.g.x, p.height - 1, st.g.z); } |
| +добавлено | st.vortex = fx.forces.acquire(); |
| +добавлено | if (st.vortex) { st.vortex.type = 1; st.vortex.pos.copy(st.g); st.vortex.dir.set(0, 1, 0); st.vortex.p[1] = 0; st.vortex.w = 6; } |
| +добавлено | st.wind = fx.forces.acquire(); |
| +добавлено | if (st.wind) { st.wind.type = 0; const an = p.angle * Math.PI / 180; st.wind.dir.set(Math.cos(an), 0, Math.sin(an)); st.wind.p[0] = 0; st.wind.p[1] = 0.9; st.wind.p[2] = 0.5; } |
| +добавлено | } else if (name === 'release') { |
| +добавлено | fx.rig.shake(0.12, 9); |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | for (let i = 0; i < N_ARCS; i++) { st.arcs[i]?.release(); st.arcs[i] = null; } |
| +добавлено | st.age = 0; |
| +добавлено | // the gust front and the first strike land together |
| +добавлено | const g = st.g; |
| +добавлено | st.gustOn = p.gust > 0; st.gustR = 1; |
| +добавлено | if (st.gustOn) { |
| +добавлено | sky.burst = 1; |
| +добавлено | st.shock = fx.forces.acquire(); |
| +добавлено | if (st.shock) st.shock.type = 4; |
| +добавлено | shock(g.x, g.z, 0.5, 15 * Math.max(p.gust, 0.4), 1, 1, 0.05 * p.gust); |
| +добавлено | E.leaf.pos.set(g.x, 0.25, g.z); E.leaf.radius = 2; E.leaf.vel.set(0, 2, 0); E.leaf.burst(26 * p.gust); |
| +добавлено | fx.rig.shake(0.22 * p.gust, 7); |
| +добавлено | } |
| +добавлено | startStrike(c, st, p); |
| +добавлено | st.next = 0.5 + 0.35 * fx.rand(); |
| +добавлено | } else if (name === 'done') releaseAll(st); |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, st = states[c.index]; |
| +добавлено | st.age += dt; |
| +добавлено | let cover, rainK, erode; |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = Math.min(1, c.t / c.durations.charge), f = c.from; |
| +добавлено | cover = smooth(0, 1, k); erode = 0; rainK = 0.1 * smooth(0.7, 1, k); |
| +добавлено | // the orb drinks in wind and charge |
| +добавлено | E.gather.pos.copy(f); E.gather.rate(420 * (0.3 + 0.7 * k), dt); |
| +добавлено | E.mote.pos.copy(f); E.mote.vel.set(0, 0, 0); E.mote.rate(1500 * k, dt); |
| +добавлено | st.reseed -= dt; |
| +добавлено | for (let i = 0; i < N_ARCS; i++) { |
| +добавлено | const h = st.arcs[i]; |
| +добавлено | if (!h) continue; |
| +добавлено | if (st.reseed <= 0) { |
| +добавлено | h.a.copy(f).add(_a.set(fx.rand() - 0.5, fx.rand() - 0.5, fx.rand() - 0.5).normalize().multiplyScalar(0.28)); |
| +добавлено | h.b.copy(f).add(_a.set(fx.rand() - 0.5, fx.rand() - 0.5, fx.rand() - 0.5).normalize().multiplyScalar(0.28 + 0.5 * k * fx.rand())); |
| +добавлено | h.seed = seedInt(); |
| +добавлено | } |
| +добавлено | h.intensity = 24 * p.intensity * (0.4 + 0.6 * k) * (fx.rand() < 0.75 ? 1 : 0); |
| +добавлено | } |
| +добавлено | if (st.reseed <= 0) st.reseed = 0.05; |
| +добавлено | // the air starts to circle under the gathering deck, and it starts to flicker |
| +добавлено | if (st.vortex) { st.vortex.p[0] = 0.4 * p.radius; st.vortex.p[1] = 16 * p.radius * p.spin * k; st.vortex.p[2] = 0.5 * k; st.vortex.p[3] = 1.2 * k; } |
| +добавлено | if (k > 0.35) cloudFlicker(st, p, dt, 1.6, 0.45 + 0.55 * k); |
| +добавлено | if (st.glow) { st.glow.intensity = maxFlash() * 3.2; } |
| +добавлено | } else { |
| +добавлено | const total = c.durations.impact + c.durations.linger, T = c.phase === 'linger' ? c.durations.impact + c.t : c.t; |
| +добавлено | const out = smooth(total - FADE, total - 0.1, T); |
| +добавлено | cover = 1; erode = out; rainK = smooth(0, 0.9, T) * (1 - smooth(total - FADE, total - FADE + 1.8, T)); |
| +добавлено | // a turning wind, gusting, gone when the rain goes |
| +добавлено | if (st.vortex) { st.vortex.p[0] = 0.4 * p.radius; st.vortex.p[1] = 30 * p.radius * p.spin * (1 - out); st.vortex.p[2] = 0.6 * (1 - out); st.vortex.p[3] = 2 * (1 - out); } |
| +добавлено | if (st.wind) { st.wind.p[0] = 8 * p.wind * (1 - out); st.wind.p[2] = 0.5 * (1 - out); } |
| +добавлено | updateGust(c, st, p, dt); |
| +добавлено | // staggered strikes: the storm builds to its middle and wears out |
| +добавлено | if (T < total - FADE - 0.3 && p.strikes > 0) { |
| +добавлено | st.next -= dt; |
| +добавлено | if (st.next <= 0) { |
| +добавлено | const build = 0.65 + 0.6 * Math.sin(Math.min(1, T / Math.max(total - FADE, 1)) * Math.PI); |
| +добавлено | startStrike(c, st, p); |
| +добавлено | if (fx.rand() < 0.22) startStrike(c, st, p); // sometimes two at once |
| +добавлено | st.next = Math.max(0.38, (0.5 + 1 * fx.rand()) / (p.strikes * build)); |
| +добавлено | } |
| +добавлено | if (!st.spider && fx.rand() < dt * 0.7 * p.strikes) startSpider(st, p); |
| +добавлено | } |
| +добавлено | updateSpider(st, p, dt); |
| +добавлено | for (let i = 0; i < MAX_STRIKES; i++) if (st.strikes[i].live) updateStrike(c, st, p, st.strikes[i], dt); |
| +добавлено | cloudFlicker(st, p, dt, 1.6 * p.strikes * (1 - 0.7 * out), 1 - 0.6 * out); |
| +добавлено | worldRain(c, st, p, dt, T, rainK * p.rain); |
| +добавлено | if (st.glow) { st.glow.intensity = maxFlash() * 2.6; if (!st.spider) { const fl = brightest(); st.glow.pos.set(fl.x, Math.min(fl.y, p.height - 1), fl.z); } } |
| +добавлено | } |
| +добавлено | // what this cast asks of the sky |
| +добавлено | sky.wCover = Math.max(sky.wCover, cover); |
| +добавлено | sky.wRain = Math.max(sky.wRain, rainK * Math.min(p.rain, 1.2)); |
| +добавлено | sky.wErode = Math.min(sky.wErode, erode); |
| +добавлено | sky.wDim = Math.max(sky.wDim, cover * (1 - erode) * Math.min(p.darkness, 1.2)); |
| +добавлено | sky.wx = st.g.x; sky.wz = st.g.z; sky.live++; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | function brightest() { |
| +добавлено | let top = flashes[0]; |
| +добавлено | for (let i = 1; i < NF; i++) if (flashes[i].I > top.I) top = flashes[i]; |
| +добавлено | return top; |
| +добавлено | } |
| +добавлено | function maxFlash() { return brightest().I; } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { U, COMMON_GLSL, NOISE_GLSL, LINES_BLENDING, arenaSDF } from '../vfx/common.js'; |
| +добавлено | import { blackbodyRGB } from '../vfx/noise.js'; |
| +добавлено | import { REFRACT_GLSL } from '../vfx/Refraction.js'; |
| +добавлено | |
| +добавлено | // Void (PLAN 3). Singularity, matched to Interstellar's Gargantua (James, von Tunzelmann, Franklin & |
| +добавлено | // Thorne 2015) and the EHT / NASA GSFC black hole visualisations: the charge drains the light around |
| +добавлено | // the focus (absorbing media, motes falling in) and a horizon forms inside it, its disk igniting. |
| +добавлено | // It drifts out at ~6 m/s, wobbling. The hole and its disk are traced per pixel by Lens.js (slot = |
| +добавлено | // void slot): Schwarzschild photon paths through a thin, banded, differentially rotating Kepler disk |
| +добавлено | // (blackbody T ~ r^-3/4, Doppler + gravitational shift, I ~ g^4), so the near side crosses in front |
| +добавлено | // of the shadow, the far side arches over and under it, a razor photon ring hugs the shadow, and the |
| +добавлено | // arena behind bends into the Einstein ring. Matter streams in on plunging Kepler orbits (real 1/r^2 |
| +добавлено | // gravity from the void's attractor, which every particle in the arena feels), drawn by this |
| +добавлено | // module's own Doppler streak pass. Six dark tendrils, corruption seeping along the floor's veins. |
| +добавлено | // Collapse: 0.3 s implosion (the disk plunges, spins up and heats, the shadow shrinks, the lens |
| +добавлено | // spikes), then the hole lets go: an expanding photographic-negative shell, the disk blown out as a |
| +добавлено | // white-hot ring, the attractor flipped into a shock shell, sparks, dark smoke, a heavy distortion ring. |
| +добавлено | // Rift, after the film tradition of spacetime wounds (Annihilation's shimmer refraction, Interstellar's |
| +добавлено | // wormhole interior): a jagged tear drawn in the refract pass. Around it the scene behind really is |
| +добавлено | // sheared, pushed aside and shattered into glass-like shards; its edge burns with a thin white line; |
| +добавлено | // inside, a funnel of burning wall narrows with depth to a throat of stars, every layer with its own |
| +добавлено | // parallax. Rocks and dust are torn off the floor and dragged in by the attractor, corruption spreads |
| +добавлено | // outward beneath it, and it snaps shut with a shock. |
| +добавлено | // |
| +добавлено | // The infall motes are a normal 'glow' kind (black in the shared draw) so they live in the shared |
| +добавлено | // pool and feel every force; one extra draw over the glow window shades them. |
| +добавлено | |
| +добавлено | const MAX_VOIDS = 2, TENDRILS = 8; |
| +добавлено | const UP = new THREE.Vector3(0, 1, 0), Z = new THREE.Vector3(0, 0, 1); |
| +добавлено | // hole scale: shadow radius (m) at size 1, b_c = 3 sqrt(3) / 2 r_s, Einstein ring / shadow, disk |
| +добавлено | // outer edge (r_s; the ISCO is 3), disk emission scale |
| +добавлено | const SHADOW = 0.34, B_CRIT = 2.598, EINSTEIN = 2.0, DISK_OUT = 13, DISK_I = 10; |
| +добавлено | |
| +добавлено | // floor-reflection stand-in for the traced disk (layer 1 only) |
| +добавлено | const PROXY_VERT = /* glsl */` |
| +добавлено | out vec2 vP; |
| +добавлено | void main() { vP = position.xy; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }`; |
| +добавлено | const PROXY_FRAG = /* glsl */` |
| +добавлено | uniform vec3 uCol; |
| +добавлено | uniform float uIn; |
| +добавлено | in vec2 vP; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | float r = length(vP); |
| +добавлено | if (r > 1.0) discard; |
| +добавлено | float a = smoothstep(uIn * 0.8, uIn * 1.3, r) * (1.0 - smoothstep(0.3, 1.0, r)) * pow(max(r / uIn, 1.0), -1.6); |
| +добавлено | fragColor = vec4(uCol * a * (0.65 + 0.35 * sin(r * 61.0) * sin(r * 23.0 + 1.3)), 0.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const DISK_VERT = (W) => /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | precision highp int; |
| +добавлено | ${COMMON_GLSL} |
| +добавлено | ${NOISE_GLSL} |
| +добавлено | uniform highp sampler2D uS0; |
| +добавлено | uniform highp sampler2D uS1; |
| +добавлено | uniform highp sampler2D uS2; |
| +добавлено | uniform int uStyleStart; |
| +добавлено | uniform int uStyleCount; |
| +добавлено | uniform int uFieldCount; |
| +добавлено | uniform int uRingBase; |
| +добавлено | uniform int uKind; |
| +добавлено | uniform vec4 uVoidA[${MAX_VOIDS}]; // centre, inner radius (0 = slot unused) |
| +добавлено | uniform vec4 uVoidB[${MAX_VOIDS}]; // centre velocity, - |
| +добавлено | uniform vec4 uDisk; // inner temperature K, intensity, beaming k, mote size (m) |
| +добавлено | uniform vec3 uTint; |
| +добавлено | out vec2 vUv; |
| +добавлено | out vec3 vColor; |
| +добавлено | out float vSoft; |
| +добавлено | const int PW = ${W}; |
| +добавлено | void main() { |
| +добавлено | gl_Position = vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | vColor = vec3(0.0); vUv = vec2(0.0); vSoft = 0.0; |
| +добавлено | int ring = uStyleCount - uFieldCount, id = gl_InstanceID; |
| +добавлено | int idx = uStyleStart + (id < uFieldCount ? id : uFieldCount + (uRingBase + id - uFieldCount) % max(ring, 1)); |
| +добавлено | ivec2 px = ivec2(idx % PW, idx / PW); |
| +добавлено | vec4 s2 = texelFetch(uS2, px, 0); |
| +добавлено | if (int(s2.x) != uKind) return; |
| +добавлено | vec4 s0 = texelFetch(uS0, px, 0), s1 = texelFetch(uS1, px, 0); |
| +добавлено | if (s0.w >= s1.w) return; |
| +добавлено | int vi = -1; float best = 1e9; |
| +добавлено | for (int i = 0; i < ${MAX_VOIDS}; i++) { |
| +добавлено | if (uVoidA[i].w <= 0.0) continue; |
| +добавлено | float d = distance(s0.xyz, uVoidA[i].xyz); |
| +добавлено | if (d < best) { best = d; vi = i; } |
| +добавлено | } |
| +добавлено | if (vi < 0) return; |
| +добавлено | vec4 A = uVoidA[vi]; |
| +добавлено | vec3 headV = (viewMatrix * vec4(s0.xyz, 1.0)).xyz; |
| +добавлено | float viewZ = max(-headV.z, 0.05); |
| +добавлено | float t = s0.w / s1.w; |
| +добавлено | float fade = smoothstep(0.0, 0.1, t) * (1.0 - smoothstep(0.7, 1.0, t)); |
| +добавлено | // Doppler beaming in the hole's frame: brightness D^3, approaching side bluer |
| +добавлено | vec3 rel = s1.xyz - uVoidB[vi].xyz; |
| +добавлено | float sp = length(rel); |
| +добавлено | float mu = sp > 1e-3 ? dot(rel / sp, normalize(cameraPosition - s0.xyz)) : 0.0; |
| +добавлено | float D = max(1.0 + uDisk.z * mu, 0.05); |
| +добавлено | // thin disk: T ~ r^-3/4, hot inner edge |
| +добавлено | float x = max(best / A.w, 0.7); |
| +добавлено | float T = uDisk.x * pow(x, -0.75) * mix(1.0, D, 0.45); |
| +добавлено | vec3 col = blackbody(T) * uTint * (uDisk.y * pow(x, -2.0) * D * D * D * fade); |
| +добавлено | vec3 tailV = (viewMatrix * vec4(s0.xyz - rel * 0.022, 1.0)).xyz; |
| +добавлено | float pxWorld = 2.0 * viewZ / (projectionMatrix[1][1] * uResolution.y); |
| +добавлено | float size = uDisk.w * (0.7 + 0.6 * hash11(s2.y * 3.1)), w = max(size, 1.5 * pxWorld); |
| +добавлено | vec2 axis = tailV.xy - headV.xy; |
| +добавлено | float len = length(axis); |
| +добавлено | vec2 dir = len > 1e-4 ? axis / len : vec2(1.0, 0.0); |
| +добавлено | vec2 perp = vec2(-dir.y, dir.x); |
| +добавлено | vec2 off = dir * (position.x < 0.0 ? -w : len + w) + perp * position.y * w; |
| +добавлено | gl_Position = projectionMatrix * vec4(headV + vec3(off, 0.0), 1.0); |
| +добавлено | vUv = vec2(position.x < 0.0 ? -1.0 : 1.0, position.y); |
| +добавлено | vSoft = len / (len + 2.0 * w); |
| +добавлено | vColor = col * (size / w) * smoothstep(0.3, 1.2, viewZ) * (2.0 * w / (len + 2.0 * w)); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const DISK_FRAG = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | in vec2 vUv; |
| +добавлено | in vec3 vColor; |
| +добавлено | in float vSoft; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | float x = vUv.x * 0.5 + 0.5, ends = 1.0 - vSoft; |
| +добавлено | float dx = x < ends * 0.5 ? (ends * 0.5 - x) / max(ends * 0.5, 1e-3) : x > 1.0 - ends * 0.5 ? (x - 1.0 + ends * 0.5) / max(ends * 0.5, 1e-3) : 0.0; |
| +добавлено | fragColor = vec4(vColor * exp(-(dx * dx + vUv.y * vUv.y) * 4.0) * mix(1.0, 0.35, x), 0.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // tendrils: p0 = base, seed p1 = dir, length p2 = width, amplitude, twist, extension |
| +добавлено | // p3 = wave speed, wave number, -, darkness |
| +добавлено | const TENDRIL_PATH = /* glsl */` |
| +добавлено | vec3 ribbonPath(float t, vec4 p0, vec4 p1, vec4 p2, vec4 p3, out float width) { |
| +добавлено | vec3 dir = normalize(p1.xyz); |
| +добавлено | mat3 B = basisFrom(dir); |
| +добавлено | float s = t * p2.w, L = p1.w, seed = p0.w; |
| +добавлено | float ph = uTime * p3.x - s * p3.y + seed * 6.2832; |
| +добавлено | vec2 wv = vec2(sin(ph), 0.8 * cos(ph * 0.71 + seed * 3.0)) + 0.35 * vec2(sin(ph * 2.3 + 1.7), sin(ph * 1.9 + seed)); |
| +добавлено | float tw = p2.z * s + seed, c = cos(tw), sn = sin(tw); |
| +добавлено | wv = mat2(c, -sn, sn, c) * wv; |
| +добавлено | width = p2.x * pow(1.0 - t, 0.75) * smoothstep(0.0, 0.06, t + 0.01) * step(0.02, p2.w); |
| +добавлено | return p0.xyz + dir * (s * L) + (B[0] * wv.x + B[1] * wv.y) * (p2.y * s * s * L); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const TENDRIL_SHADE = /* glsl */` |
| +добавлено | vec3 ribbonShade(RibbonFrag r, out vec3 media, out float alpha, out float distortion) { |
| +добавлено | float v = abs(r.uv.y), t = r.uv.x; |
| +добавлено | media = vec3(0.003, 0.0, 0.008); |
| +добавлено | alpha = (1.0 - v * v * v * v) * r.p3.w; |
| +добавлено | float edge = smoothstep(0.45, 0.85, v) * (1.0 - smoothstep(0.9, 1.0, v)); |
| +добавлено | float flick = 0.75 + 0.25 * sin(t * 11.0 - uTime * 9.0 + r.p0.w * 9.0) * sin(t * 4.3 + uTime * 3.1); |
| +добавлено | distortion = r.uv.y * exp(-3.0 * r.uv.y * r.uv.y) * 0.05; |
| +добавлено | return r.color * edge * flick * (0.4 + t); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // the rift, in the refract pass. Two draws of one program: the wound writes depth (what is behind it |
| +добавлено | // is gone, for particles too), the sheared halo around it doesn't. |
| +добавлено | const RIFT_H = 1.7, RIFT_W = 0.5, JAG_M = 12; // half height, half width, metres per jag texture repeat |
| +добавлено | const RIFT_VERT = /* glsl */` |
| +добавлено | uniform vec3 uC; |
| +добавлено | uniform vec3 uR; |
| +добавлено | uniform vec3 uUp; |
| +добавлено | uniform vec2 uHalf; |
| +добавлено | out vec2 vQ; |
| +добавлено | out vec3 vW; |
| +добавлено | void main() { |
| +добавлено | vQ = position.xy * uHalf; |
| +добавлено | vW = uC + uR * vQ.x + uUp * vQ.y; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(vW, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const RIFT_FRAG = (funnel, stars) => /* glsl */` |
| +добавлено | uniform sampler2D uJag; // r centre line, g b left and right edge, a edge burn: piecewise linear, so torn |
| +добавлено | uniform vec3 uR; |
| +добавлено | uniform vec3 uUp; |
| +добавлено | uniform vec3 uN; // towards the camera's side |
| +добавлено | uniform vec3 uRim; |
| +добавлено | uniform vec3 uHot; |
| +добавлено | uniform vec4 uRift; // half height, half width, seed, open |
| +добавлено | uniform vec4 uRift2; // reach, shear, flare, - |
| +добавлено | uniform float uMode; // 0 the wound, 1 the halo |
| +добавлено | in vec2 vQ; |
| +добавлено | in vec3 vW; |
| +добавлено | out vec4 fragColor; |
| +добавлено | |
| +добавлено | // signed distance across the torn outline at q (metres in the tear's plane), negative inside. |
| +добавлено | // grow narrows it: the walls of the wound, further in. |
| +добавлено | float tearD(vec2 q, float grow, out float cx, out float burn) { |
| +добавлено | float H = uRift.x * uRift2.x, y = q.y / max(H, 1e-3); |
| +добавлено | vec4 o = textureLod(uJag, vec2(q.y / ${JAG_M}.0 + uRift.z, 0.5), 0.0); |
| +добавлено | cx = (o.r - 0.5) * 0.62; |
| +добавлено | burn = o.a; |
| +добавлено | float env = pow(max(1.0 - y * y, 0.0), 0.6) * (1.0 + 0.3 * sin(y * 2.3 + uRift.z * 40.0)); |
| +добавлено | float w = uRift.y * uRift.w * grow * env; |
| +добавлено | float d = max(cx - w * (0.35 + 1.3 * o.g) - q.x, q.x - cx - w * (0.35 + 1.3 * o.b)); |
| +добавлено | return d + max(abs(y) - 1.0, 0.0) * H; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // inside: a funnel of burning wall closing on a throat, then stars at their own depths |
| +добавлено | vec3 wound(vec2 q, vec3 rd, float dStart) { |
| +добавлено | vec3 rp = vec3(dot(rd, uR), dot(rd, uUp), max(-dot(rd, uN), 0.3)); |
| +добавлено | vec2 sl = rp.xy / rp.z; |
| +добавлено | float zP = 0.0, dP = dStart, seed = uRift.z * 50.0, cx, b; |
| +добавлено | for (int i = 1; i <= ${funnel}; i++) { |
| +добавлено | float f = float(i) / ${funnel}.0, z = 2.4 * f * f; |
| +добавлено | vec2 p = q + sl * z; |
| +добавлено | float dd = tearD(p, mix(1.0, 0.3, 1.0 - exp(-z / 0.9)), cx, b); |
| +добавлено | if (dd > 0.0) { |
| +добавлено | float zh = mix(zP, z, clamp(-dP / (dd - dP), 0.0, 1.0)); |
| +добавлено | p = q + sl * zh; |
| +добавлено | // the torn wall, streaked along the depth it falls into |
| +добавлено | float n1 = noise3(vec3(p.y * 3.1, zh * 1.3 - uTime * 0.8, seed)), n2 = noise3(vec3(p.y * 11.0, zh * 2.6 - uTime * 1.4, seed + 3.0)); |
| +добавлено | float st = n1 * n1 * n1 * (0.2 + 2.4 * n2 * n2); |
| +добавлено | return uRim * ((exp(-zh * 4.5) * (0.02 + 1.3 * st) + 0.006 * exp(-zh * 0.8)) * uRift2.z) + uHot * (exp(-zh / 0.025) * 0.5 * uRift2.z); |
| +добавлено | } |
| +добавлено | zP = z; dP = dd; |
| +добавлено | } |
| +добавлено | vec3 sky = vec3(0.0); |
| +добавлено | for (int k = 0; k < 3; k++) { |
| +добавлено | float fk = float(k), D = 4.0 + 6.0 * fk; |
| +добавлено | vec2 uv = (q / D + sl) * (1.9 + fk * 0.7) + seed; |
| +добавлено | float n = noise3(vec3(uv, fk * 3.7 + uTime * 0.03)), m = n * (0.35 + 1.1 * noise3(vec3(uv * 2.7, fk * 5.1))); |
| +добавлено | sky += (k == 0 ? uRim * 0.4 : k == 1 ? vec3(0.07, 0.02, 0.2) : vec3(0.2, 0.015, 0.09)) * (m * m); |
| +добавлено | } |
| +добавлено | for (int j = 0; j < ${stars}; j++) { |
| +добавлено | float fj = float(j), D = 5.0 * pow(2.4, fj); |
| +добавлено | vec2 uv = (q / D + sl) * (60.0 + 45.0 * fj) + fj * 37.0 + seed, ic = floor(uv), fr = uv - ic - 0.5; |
| +добавлено | float h = hash12(ic + fj * 13.0); |
| +добавлено | if (h < 0.76) continue; |
| +добавлено | vec2 o = vec2(hash12(ic + 3.1), hash12(ic + 7.7)) - 0.5; |
| +добавлено | vec2 e = fr - o * 0.7; |
| +добавлено | float g = fract(h * 57.0); |
| +добавлено | sky += mix(vec3(0.72, 0.8, 1.0), vec3(1.0, 0.68, 0.6), fract(h * 19.0)) * ((0.5 + 11.0 * g * g * g * g) * exp(-dot(e, e) * 110.0)); |
| +добавлено | } |
| +добавлено | return sky; |
| +добавлено | } |
| +добавлено | |
| +добавлено | void main() { |
| +добавлено | vec2 q = vQ; |
| +добавлено | float cx, burn, d = tearD(q, 1.0, cx, burn); |
| +добавлено | float zs = refrViewZ(vW), px = zs * 2.0 / (projectionMatrix[1][1] * uResolution.y); |
| +добавлено | if (uMode < 0.5 ? d > px : (d < -px || d > 1.9)) discard; |
| +добавлено | float inside = smoothstep(px, -px, d), shear = uRift2.y, seed = uRift.z * 50.0; |
| +добавлено | vec3 col = vec3(0.0); |
| +добавлено | float dO = max(d, 0.0), near = exp(-dO / 0.2); |
| +добавлено | if (inside < 1.0) { |
| +добавлено | // space is pushed aside and slid past itself, opposite ways on the two sides of the tear ... |
| +добавлено | float side = q.x < cx ? -1.0 : 1.0, fall = exp(-dO / 0.5) * smoothstep(1.9, 1.0, dO); |
| +добавлено | vec2 off = vec2(-side * 0.3, side * 0.5 + 0.05 * sin(uTime * 1.7 + q.y * 2.0)) * (fall * shear); |
| +добавлено | // ... and close in it has shattered like glass: each shard looks somewhere else |
| +добавлено | vec2 g = q * 2.7 + seed, ig = floor(g), id = vec2(0.0), id2 = vec2(0.0); |
| +добавлено | float F1 = 9.0, F2 = 9.0; |
| +добавлено | for (int y = -1; y <= 1; y++) for (int x = -1; x <= 1; x++) { |
| +добавлено | vec2 c = ig + vec2(float(x), float(y)), h = vec2(hash12(c), hash12(c + 19.7)); |
| +добавлено | float l = length(g - c - h); |
| +добавлено | if (l < F1) { F2 = F1; F1 = l; id2 = id; id = h; } else if (l < F2) { F2 = l; id2 = h; } |
| +добавлено | } |
| +добавлено | float shard = exp(-dO / 0.38); |
| +добавлено | off += (id - 0.5) * (0.42 * shard * shear); |
| +добавлено | vec2 uv0 = gl_FragCoord.xy / uResolution, duv = refrUV(vW + uR * off.x + uUp * off.y) - uv0; |
| +добавлено | // a lookup landing on something in front of the tear falls back to straight through |
| +добавлено | duv *= smoothstep(zs - 0.3, zs + 0.3, linearDepth(textureLod(uSceneDepth, uv0 + duv, 0.0).r)); |
| +добавлено | float ca = 0.012 + 0.05 * near; |
| +добавлено | col = vec3(textureLod(uSceneColor, uv0 + duv * (1.0 + ca), 0.0).r, textureLod(uSceneColor, uv0 + duv, 0.0).g, textureLod(uSceneColor, uv0 + duv * (1.0 - ca), 0.0).b); |
| +добавлено | col *= 1.0 - min(0.6 * near * shear, 0.8); // the wound drinks the light around it |
| +добавлено | // the shards' broken edges catch the fire |
| +добавлено | float crack = (1.0 - smoothstep(0.0, max(0.035, 2.0 * px), F2 - F1)) * exp(-dO / 0.16) * step(0.3, id.x * id2.y + id2.x * id.y); |
| +добавлено | col += uRim * (crack * (0.08 + 0.8 * burn * burn) * uRift2.z * min(shear * 2.0, 1.0)); |
| +добавлено | } |
| +добавлено | if (inside > 0.0) col = mix(col, wound(q, normalize(vW - cameraPosition), min(d, -1e-3)), inside); |
| +добавлено | // the edge: a thin, broken line of white fire with a violet burn around it |
| +добавлено | float ad = abs(d), rw = max(0.0045, 0.9 * px); |
| +добавлено | float cr = smoothstep(0.42, 0.72, burn) * (0.15 + 2.2 * noise3(vec3(q.y * 4.3, uTime * 1.9, seed))); |
| +добавлено | col += (uHot * (exp(-ad / rw) * (0.1 + 2.6 * cr)) + uRim * (exp(-ad / (rw * 3.5)) * (0.03 + 0.9 * cr) + exp(-ad / 0.12) * 0.035)) * uRift2.z; |
| +добавлено | fragColor = vec4(col, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | function jagTexture() { |
| +добавлено | const n = 1024, a = new Uint8Array(n * 4); |
| +добавлено | let h = 0x2f6e2b1; |
| +добавлено | const rnd = () => { h = (Math.imul(h ^ (h >>> 15), 0x2c1b3c6d) + 0x6d2b79f5) | 0; return ((h ^ (h >>> 13)) >>> 0) / 4294967296; }; |
| +добавлено | const octave = (segs) => { const v = Array.from({ length: segs }, rnd); return (t) => { const x = t * segs, i = Math.floor(x), f = x - i; return v[i % segs] * (1 - f) + v[(i + 1) % segs] * f; }; }; |
| +добавлено | const channel = (...os) => { const fs = os.map(([segs, amp]) => [octave(segs), amp]); return (t) => fs.reduce((sum, [fn, amp]) => sum + fn(t) * amp, 0); }; |
| +добавлено | const ch = [channel([22, 0.62], [70, 0.27], [220, 0.11]), channel([30, 0.5], [90, 0.32], [270, 0.18]), channel([26, 0.5], [84, 0.32], [250, 0.18]), channel([40, 0.55], [170, 0.45])]; |
| +добавлено | for (let i = 0; i < n; i++) for (let k = 0; k < 4; k++) a[i * 4 + k] = 255 * ch[k](i / n); |
| +добавлено | const tex = new THREE.DataTexture(a, n, 1, THREE.RGBAFormat); |
| +добавлено | tex.wrapS = THREE.RepeatWrapping; tex.minFilter = tex.magFilter = THREE.LinearFilter; tex.needsUpdate = true; |
| +добавлено | return tex; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // module scratch (no allocations per frame) |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _r = new THREE.Vector3(), _t = new THREE.Vector3(); |
| +добавлено | const _col = new THREE.Color(), _col2 = new THREE.Color(); |
| +добавлено | const SPLAT = { x: 0, z: 0, radius: 1, corruption: 0, crack: 0, glow: 0, pattern: 'noise', seed: 0 }; |
| +добавлено | const SHOCK = { pos: new THREE.Vector3(), radius0: 0.2, radius1: 8, duration: 0.6, thickness: 1, distortion: 0.12, intensity: 0, ground: false, color: null }; |
| +добавлено | const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); }; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'void', name: 'Void', key: '7', hue: '#8a3dff', |
| +добавлено | shape: 'projectile', forms: ['Singularity', 'Rift'], speed: 6, radius: 0.35, |
| +добавлено | params: { size: 1, speed: 6, gravity: 1, lensing: 1, disk: 1, temperature: 4800, beaming: 0.35, tendrils: 1, corruption: 1, collapse: 1, hold: 2.6, tint: '#ffffff', edge: '#9a4dff' }, |
| +добавлено | ranges: { size: [0.4, 2, 0.01], speed: [2, 16, 0.1], gravity: [0.3, 3, 0.01], lensing: [0, 3, 0.01], disk: [0, 2, 0.01], temperature: [2500, 20000, 10], beaming: [0, 0.95, 0.01], tendrils: [0, 3, 0.01], corruption: [0, 3, 0.01], collapse: [0, 3, 0.01], hold: [0.8, 6, 0.1] }, |
| +добавлено | presets: { |
| +добавлено | gargantua: { temperature: 4600, beaming: 0.6, tendrils: 0.25, lensing: 1.6, size: 1.3, tint: '#ffd6a8' }, |
| +добавлено | 'violet disk': { temperature: 14000, tint: '#9b62ff', edge: '#c070ff' }, |
| +добавлено | abyssal: { temperature: 3400, tint: '#ff7088', edge: '#ff2a60', corruption: 2, tendrils: 1.8 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 1.0, impact: 0.55, linger: 3.2 }, |
| +добавлено | kinds: { |
| +добавлено | // shaded by the spell's own Doppler pass; black in the shared glow draw |
| +добавлено | disk: { style: 'glow', orbit: true, drag: 0, life: [0.6, 1.2], size: [0.012, 0.012], emissive: 0, color: ['#000000', '#000000'], alpha: [1, 1], forceResponse: 1 }, |
| +добавлено | mote: { style: 'glow', drag: 0.2, life: [0.7, 1.1], size: [0.016, 0.006], stretch: 0.03, emissive: 2.4, color: ['#a780ff', '#6a2dff'], alpha: [0.3, 1], fadeIn: 0.3, fadeOut: 0.1, forceResponse: 1 }, |
| +добавлено | spark: { style: 'glow', drag: 1.1, gravity: 1.5, life: [0.5, 1.5], size: [0.028, 0.008], stretch: 0.04, emissive: 7, color: ['#f4ecff', '#7b2dff'], alpha: [1, 0], forceResponse: 0.3 }, |
| +добавлено | drain: { style: 'media', absorbing: true, drag: 0.4, life: [0.8, 1.3], size: [1.1, 0.2], sizePow: 0.6, color: ['#020104', '#000000'], alpha: [0.75, 0.95], density: 3.5, erosion: 0.35, fadeIn: 0.35, fadeOut: 0.1, forceResponse: 1 }, |
| +добавлено | smoke: { style: 'media', absorbing: true, drag: 2.4, buoyancy: 0.2, curl: 0.9, curlScale: 0.25, life: [1.6, 3.2], size: [0.8, 2.8], sizePow: 0.6, color: ['#0d0718', '#07050c'], alpha: [0.85, 0], density: 2.2, erosion: 0.5, fadeIn: 0.04, forceResponse: 0.35 }, |
| +добавлено | // torn off the floor by the rift |
| +добавлено | debris: { style: 'debris', mesh: 'rock', gravity: 2.2, drag: 0.15, restitution: 0.2, friction: 0.5, life: [1.6, 3], size: [0.05, 0.05], spin: 7, color: ['#17131d', new THREE.Color(0.05, 0.015, 0.1)], alpha: [1, 1], fadeOut: 0.1, forceResponse: 1 }, |
| +добавлено | dust: { style: 'media', drag: 0.9, curl: 1.5, curlScale: 0.4, life: [1.1, 2.2], size: [0.26, 0.8], sizePow: 0.5, color: ['#4a4450', '#26222c'], alpha: [0.34, 0], fadeIn: 0.25, fadeOut: 0.2, density: 1.4, erosion: 0.9, lightResponse: 0.4, forceResponse: 1 }, |
| +добавлено | // Rift's seal: molten spatter thrown off the seam, falling |
| +добавлено | spatter: { style: 'glow', drag: 0.7, gravity: 7, restitution: 0.25, friction: 0.5, life: [0.35, 0.9], size: [0.022, 0.006], stretch: 0.045, emissive: 6, color: ['#f0d8ff', '#8a3dff'], alpha: [1, 0], forceResponse: 0.4 }, |
| +добавлено | ash: { style: 'media', absorbing: true, drag: 1.4, curl: 1.4, curlScale: 0.5, life: [1.0, 2.2], size: [0.09, 0.03], color: ['#000000', '#030106'], alpha: [0.95, 0], density: 25, erosion: 0.15, fadeIn: 0.1, forceResponse: 0.6 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const diskEm = P.emitter('void.disk', { shape: 'sphere', radius: 0.05, spread: 0.2 }); |
| +добавлено | const moteEm = P.emitter('void.mote', { shape: 'shell', radius: 2.4, spread: 0.3, radial: -1.5 }); |
| +добавлено | const drainEm = P.emitter('void.drain', { shape: 'shell', radius: 1.8, spread: 0.2, radial: -0.8 }); |
| +добавлено | const sparkEm = P.emitter('void.spark', { shape: 'shell', radius: 0.3, spread: 3, radial: 14 }); |
| +добавлено | const smokeEm = P.emitter('void.smoke', { shape: 'sphere', radius: 0.6, spread: 1, radial: 5 }); |
| +добавлено | const ashEm = P.emitter('void.ash', { shape: 'line', radius: 0.05, spread: 0.4, radial: 1.6 }); |
| +добавлено | const debrisEm = P.emitter('void.debris', { shape: 'sphere', radius: 0.12, spread: 0.7 }); |
| +добавлено | const dustEm = P.emitter('void.dust', { shape: 'sphere', radius: 0.25, spread: 0.3 }); |
| +добавлено | // Rift's seal: spatter off the two closing points as the seam zips shut, then a sheet of it |
| +добавлено | // sprayed off the whole seam (grinder sparks off a weld line, not a ball from one point) |
| +добавлено | const zipEm = P.emitter('void.spatter', { shape: 'sphere', radius: 0.05, spread: 1.2, radial: 2.6 }); |
| +добавлено | const seamEm = P.emitter('void.spatter', { shape: 'line', radius: 0.03, spread: 1.0, radial: 4.5 }); |
| +добавлено | const seamSmoke = P.emitter('void.smoke', { shape: 'line', radius: 0.12, spread: 0.5, radial: 1.4 }); |
| +добавлено | const SCAR = { a: new THREE.Vector3(), b: new THREE.Vector3(), branches: 2, subBranches: 2, jag: 0.06, width: 0.014, color: new THREE.Color(), glowColor: new THREE.Color(), intensity: 0, glowWidth: 7, reveal: 1, snapToArena: false, seed: 1, branchSeed: 1 }; |
| +добавлено | |
| +добавлено | // --- accretion disk motes: one draw over the shared glow window (only void.disk texels shade) |
| +добавлено | const quad = new THREE.InstancedBufferGeometry(); |
| +добавлено | quad.setAttribute('position', new THREE.Float32BufferAttribute([-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0], 3)); |
| +добавлено | quad.setIndex([0, 1, 2, 0, 2, 3]); |
| +добавлено | quad.instanceCount = 0; |
| +добавлено | const VA = [new THREE.Vector4(), new THREE.Vector4()], VB = [new THREE.Vector4(), new THREE.Vector4()]; |
| +добавлено | const tint = new THREE.Color(1, 1, 1), edge = new THREE.Color(), hue = new THREE.Color(this.hue); |
| +добавлено | const disk = new THREE.Mesh(quad, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: DISK_VERT(P.W), fragmentShader: DISK_FRAG, ...LINES_BLENDING, |
| +добавлено | uniforms: { |
| +добавлено | ...U, uS0: { value: null }, uS1: { value: null }, uS2: { value: null }, |
| +добавлено | uStyleStart: { value: 0 }, uStyleCount: { value: 0 }, uFieldCount: { value: 0 }, uRingBase: { value: 0 }, |
| +добавлено | uKind: { value: P.kind('void.disk') }, uVoidA: { value: VA }, uVoidB: { value: VB }, |
| +добавлено | uDisk: { value: new THREE.Vector4() }, uTint: { value: tint }, |
| +добавлено | }, |
| +добавлено | })); |
| +добавлено | disk.frustumCulled = false; |
| +добавлено | disk.layers.enable(1); // in the floor reflection too |
| +добавлено | disk.visible = false; |
| +добавлено | fx.scenes.lines.add(disk); |
| +добавлено | let linked = false; |
| +добавлено | // the pool's state textures, ring window and instance count exist after particles.build(): share them |
| +добавлено | function link() { |
| +добавлено | linked = true; |
| +добавлено | const g = P.glow.material.uniforms, u = disk.material.uniforms; |
| +добавлено | u.uS0 = P.stateU.uS0; u.uS1 = P.stateU.uS1; u.uS2 = P.stateU.uS2; |
| +добавлено | u.uStyleStart = g.uStyleStart; u.uStyleCount = g.uStyleCount; u.uFieldCount = g.uFieldCount; u.uRingBase = g.uRingBase; |
| +добавлено | disk.geometry = P.glow.geometry; |
| +добавлено | quad.dispose(); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- tendrils (dark ribbons with violet edges), both forms |
| +добавлено | const ribbons = new fx.AnalyticRibbons({ max: MAX_VOIDS * TENDRILS, segments: 40, target: 'fx', pathGLSL: TENDRIL_PATH, shadeGLSL: TENDRIL_SHADE }); |
| +добавлено | fx.scenes.fx.add(ribbons.mesh); |
| +добавлено | |
| +добавлено | // --- per-void resources: horizon sphere + disk stand-in for the floor reflection only (the main |
| +добавлено | // view's hole and disk are traced by Lens.js), rift tear, tendril state |
| +добавлено | const horizonGeo = new THREE.SphereGeometry(1, 40, 20); |
| +добавлено | const horizonMat = new THREE.MeshBasicMaterial({ color: 0x000000 }); |
| +добавлено | const tearGeo = new THREE.PlaneGeometry(2, 2); |
| +добавлено | const jag = jagTexture(); |
| +добавлено | const riftFrag = COMMON_GLSL + NOISE_GLSL + REFRACT_GLSL + RIFT_FRAG(fx.Q.mobile ? 5 : 10, fx.Q.mobile ? 2 : 3); |
| +добавлено | const slots = Array.from({ length: MAX_VOIDS }, (_, i) => { |
| +добавлено | const sphere = new THREE.Mesh(horizonGeo, horizonMat); |
| +добавлено | sphere.visible = false; |
| +добавлено | sphere.layers.set(1); |
| +добавлено | fx.scenes.opaque.add(sphere); |
| +добавлено | const proxyU = { uCol: { value: new THREE.Color() }, uIn: { value: 0.2 } }; |
| +добавлено | const proxy = new THREE.Mesh(tearGeo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: PROXY_VERT, fragmentShader: PROXY_FRAG, uniforms: proxyU, ...LINES_BLENDING, side: THREE.DoubleSide, |
| +добавлено | })); |
| +добавлено | proxy.visible = false; |
| +добавлено | proxy.layers.set(1); |
| +добавлено | fx.scenes.lines.add(proxy); |
| +добавлено | const riftU = { |
| +добавлено | ...U, uJag: { value: jag }, uC: { value: new THREE.Vector3() }, uR: { value: new THREE.Vector3(1, 0, 0) }, uUp: { value: new THREE.Vector3(0, 1, 0) }, uN: { value: new THREE.Vector3(0, 0, 1) }, |
| +добавлено | uHalf: { value: new THREE.Vector2(1, 1) }, uRim: { value: new THREE.Color() }, uHot: { value: new THREE.Color() }, |
| +добавлено | uRift: { value: new THREE.Vector4() }, uRift2: { value: new THREE.Vector4() }, |
| +добавлено | }; |
| +добавлено | const riftMesh = (mode) => { |
| +добавлено | const m = new THREE.Mesh(tearGeo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: RIFT_VERT, fragmentShader: riftFrag, uniforms: { ...riftU, uMode: { value: mode } }, side: THREE.DoubleSide, depthWrite: mode === 0, |
| +добавлено | })); |
| +добавлено | m.frustumCulled = false; |
| +добавлено | m.visible = false; |
| +добавлено | fx.scenes.refract.add(m); |
| +добавлено | return m; |
| +добавлено | }; |
| +добавлено | const riftIn = riftMesh(0), riftOut = riftMesh(1); |
| +добавлено | return { |
| +добавлено | i, busy: false, R: 0, E: 0, lens: 0, sphere, proxy, proxyU, riftIn, riftOut, riftU, held: false, wrote: new THREE.Vector3(), |
| +добавлено | rOn: false, open: 0, reach: 0, shear: 0, flare: 0, lean: 0, face: new THREE.Vector3(), |
| +добавлено | // traced disk: r_s (m), inner / outer (r_s), intensity, temperature scale, spin rate, phase, bending, shadow |
| +добавлено | dOn: false, mote: false, rs: 0.1, din: 3, dout: 13, dI: 0, dT: 1, phase: 0, bend: 1, shad: 1, twist: 0, |
| +добавлено | pos: new THREE.Vector3(), vel: new THREE.Vector3(), axis: new THREE.Vector3(), e1: new THREE.Vector3(), e2: new THREE.Vector3(), |
| +добавлено | tend: [], tdir: Array.from({ length: TENDRILS }, () => new THREE.Vector3()), tcyc: new Int32Array(TENDRILS), |
| +добавлено | trate: new Float32Array(TENDRILS), toff: new Float32Array(TENDRILS), tlen: new Float32Array(TENDRILS), tlash: new Uint8Array(TENDRILS), |
| +добавлено | }; |
| +добавлено | }); |
| +добавлено | const takeSlot = () => { for (const s of slots) if (!s.busy) { s.busy = true; return s; } return null; }; |
| +добавлено | // per cast record scratch (cast.js pools 16 records) |
| +добавлено | const recs = Array.from({ length: 16 }, () => ({ base: new THREE.Vector3(), prev: new THREE.Vector3(), side: new THREE.Vector3() })); |
| +добавлено | |
| +добавлено | let lensWas = false, invT = -1, invAmt = 1; |
| +добавлено | const shell = fx.lens.shell; |
| +добавлено | |
| +добавлено | const gm = (p) => 45 * p.size * p.gravity; |
| +добавлено | const seeded = (c, k) => (((c.seed * 2654435761) ^ (k * 40503)) >>> 0) % 1000; |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k, v) { if (k === 'tint') tint.set(v); if (k === 'edge') edge.set(v); }, |
| +добавлено | travel(c, dt) { |
| +добавлено | const p = S.params, d = c.data, r = recs[c.index]; |
| +добавлено | if (c.form === 'Rift') { c.pos.copy(d.s ? d.s.pos : c.to); c.vel.set(0, 0, 0); return c.t >= p.hold; } |
| +добавлено | d.dist += p.speed * dt; |
| +добавлено | r.base.copy(c.from).addScaledVector(c.dir, Math.min(d.dist, d.total)); |
| +добавлено | const w = 0.2 * p.size * Math.min(1, d.dist / 1.2); |
| +добавлено | r.prev.copy(c.pos); |
| +добавлено | c.pos.copy(r.base).addScaledVector(r.side, Math.sin(c.t * 3.7 + d.ph) * w).addScaledVector(UP, Math.sin(c.t * 2.6 + d.ph * 1.7) * w * 0.6); |
| +добавлено | if (dt > 0) c.vel.subVectors(c.pos, r.prev).divideScalar(dt); |
| +добавлено | c.travelled = d.dist; |
| +добавлено | return d.dist >= d.total || (d.dist > 1 && arenaSDF(c.pos) < 0.35 * p.size) || c.t > 10; |
| +добавлено | }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = c.data, r = recs[c.index]; |
| +добавлено | tint.set(p.tint); edge.set(p.edge); |
| +добавлено | if (name === 'charge') { |
| +добавлено | if (c.form === 'Rift') c.durations.charge = 0.6; |
| +добавлено | d.s = takeSlot(); d.light = fx.lights.acquire(); d.grav = fx.forces.acquire(); |
| +добавлено | d.banged = false; d.splatT = 0; d.dist = 0; d.shockR = 0; d.ph = fx.rand() * 6.28; d.scar = null; |
| +добавлено | d.total = c.from.distanceTo(c.to); |
| +добавлено | if (d.light) { d.light.range = 9; d.light.intensity = 0; d.light.shadow = true; } |
| +добавлено | if (d.grav) { d.grav.type = 2; d.grav.pos.copy(c.from); d.grav.p[0] = 0; d.grav.p[1] = 0.3; d.grav.p[2] = 0.15; d.grav.p[3] = 6; } |
| +добавлено | const s = d.s; |
| +добавлено | if (s) { |
| +добавлено | s.R = s.E = s.lens = s.dI = 0; s.dOn = s.mote = false; s.tend.length = 0; |
| +добавлено | s.pos.copy(c.from); s.vel.set(0, 0, 0); |
| +добавлено | if (c.form === 'Rift') { |
| +добавлено | // the tear hangs in front of the target (between it and the caster), its low tip just off the floor |
| +добавлено | _a.subVectors(c.to, c.from).setY(0).normalize(); |
| +добавлено | s.pos.copy(c.to).addScaledVector(_a, c.target >= 0 ? -1.15 : 0); |
| +добавлено | s.pos.y = RIFT_H * p.size + 0.15; |
| +добавлено | s.face.copy(_a).negate(); s.lean = (fx.rand() - 0.5) * 0.3; |
| +добавлено | s.riftU.uRift.value.set(RIFT_H * p.size, RIFT_W * p.size, seeded(c, 3) / 1000, 0); |
| +добавлено | s.riftU.uHalf.value.set((RIFT_W + 2) * p.size + 0.4, (RIFT_H + 2) * p.size + 0.2); |
| +добавлено | s.riftU.uRim.value.copy(edge).multiplyScalar(2.2); |
| +добавлено | s.riftU.uHot.value.copy(edge).lerp(_col.setRGB(1, 1, 1), 0.82).multiplyScalar(3.4); |
| +добавлено | s.rOn = true; s.open = 0; s.reach = 0.3; s.shear = 0; s.flare = 0.3; |
| +добавлено | if (d.grav) { d.grav.pos.copy(s.pos); d.grav.p[1] = 0.4; d.grav.p[2] = 0.5; d.grav.p[3] = 9; } |
| +добавлено | } else { |
| +добавлено | // disk tilted ~13 degrees towards the camera: Gargantua's near-edge-on view |
| +добавлено | _a.subVectors(fx.camera.position, c.from).setY(0).normalize(); |
| +добавлено | s.axis.copy(UP).addScaledVector(_a, 0.23).normalize(); |
| +добавлено | s.e1.crossVectors(s.axis, c.dir).normalize(); |
| +добавлено | if (s.e1.lengthSq() < 0.5) s.e1.set(1, 0, 0); |
| +добавлено | s.e2.crossVectors(s.axis, s.e1); |
| +добавлено | s.phase = fx.rand() * 30; s.bend = 1; s.shad = 1; s.dT = 1; s.din = 3; s.dout = DISK_OUT; |
| +добавлено | s.mote = true; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | r.side.crossVectors(c.dir, UP).normalize(); |
| +добавлено | } else if (name === 'release') { |
| +добавлено | fx.arena.setFocus(hue, 0.4); |
| +добавлено | fx.rig.shake(0.15, 8); |
| +добавлено | const s = d.s; |
| +добавлено | if (!s) return; |
| +добавлено | if (c.form === 'Singularity') { |
| +добавлено | for (let i = 0; i < 6; i++) { |
| +добавлено | const h = ribbons.acquire(); |
| +добавлено | if (!h) break; |
| +добавлено | s.tend.push(h); |
| +добавлено | s.toff[i] = fx.rand() * 6.28; |
| +добавлено | s.tlen[i] = (1.6 + 0.8 * fx.rand()) * p.size; |
| +добавлено | } |
| +добавлено | } else { |
| +добавлено | // thin dark whips, fewer than the wound has room for: the tear is the subject |
| +добавлено | for (let i = 0; i < 3; i++) { |
| +добавлено | const h = ribbons.acquire(); |
| +добавлено | if (!h) break; |
| +добавлено | s.tend.push(h); |
| +добавлено | s.trate[i] = 1 / (1.1 + 0.6 * fx.rand()); |
| +добавлено | s.toff[i] = -0.35 - 0.7 * fx.rand(); |
| +добавлено | s.tcyc[i] = -99; |
| +добавлено | } |
| +добавлено | corrupt(s.pos, 1.5 * p.size, 0.7 * p.corruption, c, 'radial'); |
| +добавлено | corrupt(s.pos, 3.2 * p.size, 0.22 * p.corruption, c, 'lichtenberg', 1.5); |
| +добавлено | fx.impacts.add('corruption', s.pos, 3 * p.size, 0.8, p.hold + 4, edge); |
| +добавлено | } |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | d.banged = false; |
| +добавлено | } else if (name === 'done') { |
| +добавлено | d.light?.release(); d.grav?.release(); d.scar?.release(); |
| +добавлено | d.light = d.grav = d.scar = null; |
| +добавлено | const s = d.s; |
| +добавлено | if (s) { |
| +добавлено | for (const h of s.tend) h.release(); |
| +добавлено | s.tend.length = 0; |
| +добавлено | s.busy = false; s.dOn = s.mote = false; s.lens = 0; s.R = 0; |
| +добавлено | s.sphere.visible = s.proxy.visible = false; s.rOn = false; |
| +добавлено | } |
| +добавлено | d.s = null; |
| +добавлено | fx.arena.setFocus(hue, 2.5); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = c.data, s = d.s, L = d.light, G = d.grav; |
| +добавлено | const R0 = SHADOW * p.size, rs0 = R0 / B_CRIT, I0 = DISK_I * p.disk; |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge; |
| +добавлено | fx.arena.setFocus(hue, 2.5 * (1 - k) * (1 - k) + 0.04); |
| +добавлено | if (c.form === 'Rift') { |
| +добавлено | // a hairline first, the air around it starting to slide |
| +добавлено | if (s) { s.open = 0.03 * k; s.reach = 0.3 + 0.55 * k * k; s.shear = 0.14 * k * k; s.flare = 0.3 + 0.8 * k; } |
| +добавлено | if (G) G.p[0] = 12 * k * p.gravity; |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | moteEm.pos.copy(c.from); moteEm.radius = 2.4; moteEm.rate(1800 * k, dt); |
| +добавлено | drainEm.pos.copy(c.from); drainEm.radius = 1.8; drainEm.rate(90, dt); |
| +добавлено | if (G) G.p[0] = 4 + 30 * k; |
| +добавлено | if (s && c.form === 'Singularity') { |
| +добавлено | // the horizon condenses, then the disk ignites from the inside out as the infall arrives |
| +добавлено | const g = smooth(0.4, 1, k), ig = smooth(0.55, 1, k); |
| +добавлено | s.R = R0 * g; s.E = EINSTEIN * s.R; s.lens = p.lensing * g; s.rs = Math.max(rs0 * g, 1e-3); s.shad = Math.min(1, g * 4); |
| +добавлено | s.din = 3; s.dout = 3 + 4 * ig; s.dI = I0 * ig * ig; s.dOn = p.disk > 0 && g > 0.01; s.twist = 0; |
| +добавлено | s.phase += dt * (1 + 2 * (1 - ig)); |
| +добавлено | hole(s, c.from); |
| +добавлено | if (L) { L.pos.copy(c.from); diskColor(p, L.color); L.intensity = 6 * p.disk * ig; } |
| +добавлено | } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (c.form === 'Rift') { rift(c, dt, p, d, s, L, G); return; } |
| +добавлено | |
| +добавлено | // --- Singularity |
| +добавлено | if (c.phase === 'travel') { |
| +добавлено | fx.arena.setFocus(hue, Math.min(2.5, 0.4 + c.t * 2)); |
| +добавлено | const GM = gm(p); |
| +добавлено | if (G) { G.pos.copy(c.pos); G.p[0] = GM; G.p[1] = 0.12; G.p[2] = rs0 * 1.6; G.p[3] = 30; } |
| +добавлено | if (s) { |
| +добавлено | s.pos.copy(c.pos); s.vel.copy(c.vel); |
| +добавлено | s.R = R0; s.E = EINSTEIN * R0; s.lens = p.lensing; s.rs = rs0; s.shad = 1; s.bend = 1; |
| +добавлено | s.din = 3; s.dout = 7 + (DISK_OUT - 7) * smooth(0, 0.45, c.t); s.dI = I0 * (1 + 0.06 * Math.sin(c.age * 5.3) * Math.sin(c.age * 3.1)); s.dT = 1; s.dOn = p.disk > 0; |
| +добавлено | s.twist = 0.25 * p.lensing; |
| +добавлено | s.phase += dt; |
| +добавлено | hole(s, c.pos); |
| +добавлено | if (p.disk > 0) infall(s, dt, GM, 1500 * p.disk); |
| +добавлено | tendrils(s, c, p, Math.min(1, c.t / 0.35)); |
| +добавлено | } |
| +добавлено | d.splatT -= dt; |
| +добавлено | if (d.splatT <= 0 && p.corruption > 0) { d.splatT = 0.2; corrupt(c.pos, 0.8 * p.size, 1.25 * p.corruption, c, 'noise'); } |
| +добавлено | if (L) { L.pos.copy(c.pos); diskColor(p, L.color); L.intensity = 16 * p.disk; } |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dist = Math.hypot(dm.pos.x - c.pos.x, dm.pos.z - c.pos.z); |
| +добавлено | if (dist < 2.5 * p.size) dm.status.corrupt = Math.max(dm.status.corrupt, 0.6 * (1 - dist / (2.5 * p.size))); |
| +добавлено | } |
| +добавлено | swirlPool(c.pos); |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | |
| +добавлено | if (c.phase === 'impact') { |
| +добавлено | const t = c.t; |
| +добавлено | if (t < 0.3) { |
| +добавлено | // implosion: the disk plunges, spins up and heats, the horizon shrinks, the lens spikes |
| +добавлено | const k = t / 0.3, GM = gm(p), cp = Math.min(p.collapse, 1.5); |
| +добавлено | if (G) { G.pos.copy(c.pos); G.p[0] = GM * (1 + 10 * k * p.collapse); G.p[2] = rs0 * 1.6; } |
| +добавлено | if (s) { |
| +добавлено | s.vel.set(0, 0, 0); |
| +добавлено | s.rs = rs0 * (1 - 0.55 * k * k * cp / 1.5); s.R = s.rs * B_CRIT; |
| +добавлено | s.E = EINSTEIN * R0; s.lens = p.lensing * (1 + 0.5 * k * k); s.twist = p.lensing * (0.25 + 1.2 * k * k * cp); |
| +добавлено | s.din = 3 - 1.3 * k; s.dout = DISK_OUT * (1 - 0.55 * k) * rs0 / s.rs; |
| +добавлено | s.dI = I0 * (1 + 3 * k * k * cp); s.dT = 1 + 0.7 * k * cp; |
| +добавлено | s.phase += dt * (1 + 6 * k * k); |
| +добавлено | hole(s, s.pos); |
| +добавлено | if (p.disk > 0) infall(s, dt, GM * (1 + 4 * k), 4000 * p.disk * (1 - k)); |
| +добавлено | tendrils(s, c, p, 1 - k); |
| +добавлено | } |
| +добавлено | if (L) { L.intensity = 16 * p.disk * (1 + 1.5 * k * k); diskColor(p, L.color, 1 + 0.7 * k); } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (!d.banged) bang(c, p, d, s, R0); |
| +добавлено | const k = (t - 0.3) / 0.12, kr = (t - 0.3) / 0.4; |
| +добавлено | if (s) { |
| +добавлено | s.lens = Math.max(0, p.lensing * 1.5 * (1 - k)); s.twist = p.lensing * 1.4 * Math.max(0, 1 - k); |
| +добавлено | // the disk, let go, flies out as a white-hot ring |
| +добавлено | if (kr < 1 && p.disk > 0) { |
| +добавлено | const rw = 0.5 + 6 * p.size * (1 - (1 - kr) ** 2.5); |
| +добавлено | s.din = 0.85 * rw / s.rs; s.dout = 1.25 * rw / s.rs; |
| +добавлено | s.dI = I0 * 0.9 * Math.max(p.collapse, 0.3) * (1 - kr) ** 3; |
| +добавлено | s.dT = 1.5 - 0.7 * kr; s.phase += dt * 3; |
| +добавлено | hole(s, s.pos); |
| +добавлено | } else { s.dOn = false; s.proxy.visible = false; } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // after the bang: the shock shell runs out and lets go |
| +добавлено | if (G && G.type === 4) { |
| +добавлено | d.shockR += dt * 16; |
| +добавлено | G.p[0] = d.shockR; G.p[2] = 170 * p.collapse * Math.max(0, 1 - d.shockR / 14); |
| +добавлено | if (d.shockR > 14) { G.release(); d.grav = null; } |
| +добавлено | } |
| +добавлено | if (L) L.intensity *= Math.exp(-dt * 5); |
| +добавлено | if (s && c.phase === 'linger') { s.lens = 0; s.dOn = false; s.proxy.visible = false; } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | tick(dt) { |
| +добавлено | if (!linked && P.built) link(); |
| +добавлено | const p = S.params, LN = fx.lens; |
| +добавлено | let lensOn = false, moteOn = false; |
| +добавлено | for (const s of slots) { |
| +добавлено | const on = s.busy && (s.lens > 0 || s.dOn); |
| +добавлено | if (on) { |
| +добавлено | LN.set(s.i, s.pos, Math.max(s.E, 1e-3), Math.max(s.R, 1e-4), Math.max(s.lens, 1e-3)); |
| +добавлено | if (s.dOn) { |
| +добавлено | const D = LN.lenses[s.i].disk; |
| +добавлено | D.on = true; D.axis.copy(s.axis); D.rs = s.rs; D.inner = s.din; D.outer = s.dout; |
| +добавлено | D.temperature = p.temperature * s.dT; D.beaming = 0.5 + 1.5 * p.beaming; D.intensity = s.dI; D.tint.copy(tint); |
| +добавлено | D.phase = s.phase; D.spin = 2.4; D.bend = s.bend; D.shadow = s.shad; D.twist = s.twist; |
| +добавлено | } |
| +добавлено | s.wrote.copy(s.pos); s.held = true; |
| +добавлено | } else if (s.held) { |
| +добавлено | // only clear the slot if it still holds what we wrote (Supernova may have taken it) |
| +добавлено | const l = LN.lenses[s.i]; |
| +добавлено | if (l.pos.equals(s.wrote)) { l.strength = 0; l.disk.on = false; } |
| +добавлено | s.held = false; |
| +добавлено | } |
| +добавлено | lensOn = lensOn || on; |
| +добавлено | const mk = s.busy && s.mote; |
| +добавлено | VA[s.i].set(s.pos.x, s.pos.y, s.pos.z, mk ? 3 * s.rs : 0); |
| +добавлено | VB[s.i].set(s.vel.x, s.vel.y, s.vel.z, s.R); |
| +добавлено | moteOn = moteOn || mk; |
| +добавлено | s.riftIn.visible = s.riftOut.visible = s.rOn; |
| +добавлено | if (s.rOn) riftPose(s); |
| +добавлено | } |
| +добавлено | // the collapse's photographic-negative shell, ~0.2 s |
| +добавлено | if (invT >= 0) { |
| +добавлено | invT += dt; |
| +добавлено | const k = invT / 0.2; |
| +добавлено | if (k >= 1) { shell.amount = 0; invT = -1; } |
| +добавлено | else { shell.radius = 0.25 + 6 * p.size * (1 - (1 - k) ** 2); shell.thickness = 0.08 + 0.2 * shell.radius; shell.amount = invAmt * (1 - k) ** 1.5; } |
| +добавлено | lensOn = true; |
| +добавлено | } |
| +добавлено | // other users (Supernova) may hold a slot: only switch the pass off when nobody does |
| +добавлено | if (lensOn) LN.active = true; |
| +добавлено | else if (lensWas && !LN.warming && !(LN.lenses[0].strength > 1e-4) && !(LN.lenses[1].strength > 1e-4)) LN.active = false; |
| +добавлено | lensWas = lensOn; |
| +добавлено | disk.material.uniforms.uDisk.value.set(p.temperature * 0.8, 1.2, p.beaming, 0.007 * Math.sqrt(p.size)); |
| +добавлено | disk.visible = moteOn; |
| +добавлено | ribbons.update(); |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | |
| +добавлено | function diskColor(p, out, k = 1) { blackbodyRGB(p.temperature * 0.85 * k, out); out.r *= tint.r; out.g *= tint.g; out.b *= tint.b; return out; } |
| +добавлено | |
| +добавлено | // the reflection-only horizon and disk stand-in follow the traced hole |
| +добавлено | function hole(s, pos) { |
| +добавлено | s.sphere.visible = s.shad > 0.01; s.sphere.position.copy(pos); s.sphere.scale.setScalar(Math.max(s.R, 1e-3)); |
| +добавлено | s.proxy.visible = s.dOn; |
| +добавлено | if (s.dOn) { |
| +добавлено | s.proxy.position.copy(pos); s.proxy.quaternion.setFromUnitVectors(Z, s.axis); s.proxy.scale.setScalar(s.dout * s.rs); |
| +добавлено | diskColor(S.params, s.proxyU.uCol.value, s.dT).multiplyScalar(s.dI * 1.6); |
| +добавлено | s.proxyU.uIn.value = s.din / s.dout; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the tear turns part way to the camera (faces it where the caster's line is edge-on), so orbiting |
| +добавлено | // still swings the funnel across the opening and it never goes edge-on |
| +добавлено | function riftPose(s) { |
| +добавлено | const u = s.riftU, ca = Math.cos(s.lean), sa = Math.sin(s.lean); |
| +добавлено | _a.subVectors(fx.camera.position, s.pos).setY(0).normalize(); |
| +добавлено | const k = Math.max(-1, Math.min(1, s.face.dot(_a) * 4)); |
| +добавлено | _b.copy(_a).addScaledVector(s.face, 0.7 * k).normalize(); |
| +добавлено | _r.crossVectors(UP, _b).normalize(); |
| +добавлено | u.uC.value.copy(s.pos); u.uN.value.copy(_b); |
| +добавлено | u.uR.value.copy(_r).multiplyScalar(ca).addScaledVector(UP, -sa); |
| +добавлено | u.uUp.value.copy(UP).multiplyScalar(ca).addScaledVector(_r, sa); |
| +добавлено | u.uRift.value.w = s.open; |
| +добавлено | u.uRift2.value.set(s.reach, s.shear, s.flare, 0); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function corrupt(pos, radius, amount, c, pattern, crack = 0) { |
| +добавлено | SPLAT.x = pos.x; SPLAT.z = pos.z; SPLAT.radius = radius; SPLAT.corruption = amount; SPLAT.crack = crack; SPLAT.pattern = pattern; |
| +добавлено | SPLAT.seed = (c.seed + c.age * 37) % 100; |
| +добавлено | fx.ground.splat(SPLAT); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function swirlPool(pos) { |
| +добавлено | const pool = fx.ARENA.pool; |
| +добавлено | if (pos.y < 3 && Math.abs(pos.x - pool.center.x) < pool.half.x + 1 && Math.abs(pos.z - pool.center.y) < pool.half.y + 1) fx.pool?.swirl?.(pos.x, pos.z, 1.5); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // infall: gas peeled in from beyond the disk's rim on plunging, sub-Keplerian orbits about the |
| +добавлено | // moving centre (Galilean: gravity doesn't care that the hole drifts), streaking in under real 1/r^2 |
| +добавлено | function infall(s, dt, GM, rate) { |
| +добавлено | const rim = s.dout * s.rs; |
| +добавлено | for (let i = 0; i < 4; i++) { |
| +добавлено | const rr = rim * (0.8 + 0.7 * fx.rand()), a = fx.rand() * Math.PI * 2, el = (fx.rand() - 0.5) * 0.7; |
| +добавлено | _r.copy(s.e1).multiplyScalar(Math.cos(a)).addScaledVector(s.e2, Math.sin(a)); |
| +добавлено | _t.crossVectors(s.axis, _r); |
| +добавлено | const v = Math.sqrt(GM / rr); |
| +добавлено | diskEm.pos.copy(s.pos).addScaledVector(_r, rr * Math.cos(el)).addScaledVector(s.axis, rr * Math.sin(el)); |
| +добавлено | diskEm.vel.copy(s.vel).addScaledVector(_t, v * (0.35 + 0.35 * fx.rand())).addScaledVector(_r, -0.3 * v).addScaledVector(s.axis, -v * Math.sin(el) * 0.8); |
| +добавлено | diskEm.radius = 0.05 * rr; diskEm.spread = 0.05 * v; |
| +добавлено | diskEm.rate(rate / 4, dt); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // singularity tendrils: six arms leaving the horizon above and below the disk, dragged round with it |
| +добавлено | function tendrils(s, c, p, ext) { |
| +добавлено | const n = s.tend.length; |
| +добавлено | for (let i = 0; i < n; i++) { |
| +добавлено | const h = s.tend[i], a = s.toff[i] + i * 1.047 + c.age * 1.1, el = (i % 2 ? 1 : -1) * (0.45 + 0.2 * Math.sin(c.age * 1.3 + i)); |
| +добавлено | _a.copy(s.e1).multiplyScalar(Math.cos(a)).addScaledVector(s.e2, Math.sin(a)).addScaledVector(s.axis, el).normalize(); |
| +добавлено | _b.copy(s.pos).addScaledVector(_a, s.R * 1.1); |
| +добавлено | h.p0.set(_b.x, _b.y, _b.z, s.toff[i]); |
| +добавлено | h.p1.set(_a.x, _a.y, _a.z, s.tlen[i]); |
| +добавлено | h.p2.set(0.08 * p.size, 0.12, -2.2, ext); |
| +добавлено | h.p3.set(5, 7, 0, 0.85); |
| +добавлено | h.color.copy(edge); |
| +добавлено | h.intensity = 1.2 * p.tendrils; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function bang(c, p, d, s, R0) { |
| +добавлено | d.banged = true; |
| +добавлено | const pos = c.pos, cp = p.collapse; |
| +добавлено | if (s) { |
| +добавлено | s.sphere.visible = false; s.R = 0; s.E = 2.6 * R0; s.shad = 0; s.bend = 0; |
| +добавлено | for (const h of s.tend) h.release(); |
| +добавлено | s.tend.length = 0; |
| +добавлено | } |
| +добавлено | if (d.grav) { d.grav.type = 4; d.grav.pos.copy(pos); d.grav.p[0] = 0.3; d.grav.p[1] = 1.4; d.grav.p[2] = 170 * cp; d.grav.p[3] = 0; d.shockR = 0.3; } |
| +добавлено | invT = 0; shell.pos.copy(pos); shell.radius = 0.25; shell.amount = 0; invAmt = fx.reduced ? 0.35 : Math.min(1, 0.6 + 0.4 * cp); |
| +добавлено | sparkEm.pos.copy(pos); sparkEm.radial = 14 * Math.sqrt(cp); sparkEm.burst(3000 * cp); |
| +добавлено | smokeEm.pos.copy(pos); smokeEm.size = p.size; smokeEm.burst(50 * Math.max(cp, 0.3)); |
| +добавлено | SHOCK.pos.copy(pos); SHOCK.radius0 = 0.2; SHOCK.radius1 = 9 * p.size; SHOCK.duration = 0.7; SHOCK.thickness = 1.4; SHOCK.distortion = 0.18 * cp; SHOCK.ground = false; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | SHOCK.pos.setY(0.05); SHOCK.radius1 = 7 * p.size; SHOCK.duration = 0.55; SHOCK.thickness = 0.7; SHOCK.distortion = 0.08 * cp; SHOCK.ground = true; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | corrupt(pos, 2.2 * p.size, 2.3 * p.corruption, c, 'radial'); |
| +добавлено | fx.impacts.add('corruption', pos, 4 * p.size, 1, 10, edge); |
| +добавлено | if (d.light) { _col.copy(edge).lerp(diskColor(p, _col2, 2), 0.5); d.light.color.copy(_col); d.light.intensity = 50 * cp; d.light.range = 14; d.light.pos.copy(pos); } |
| +добавлено | fx.flash(0.3 * cp); |
| +добавлено | fx.rig.shake(0.65 * cp, 13); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(3.5, 0.09); fx.rig.hitstop(0.07); } |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dist = Math.hypot(dm.pos.x - pos.x, dm.pos.z - pos.z), reach = 4 * p.size; |
| +добавлено | if (dist < reach) { |
| +добавлено | dm.status.corrupt = Math.max(dm.status.corrupt, 1 - dist / reach + 0.4); |
| +добавлено | dm.hit(_a.subVectors(dm.pos, pos).setY(0).normalize(), (1 - dist / reach) * cp); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | function rift(c, dt, p, d, s, L, G) { |
| +добавлено | const pos = s ? s.pos : c.to, sz = p.size, H = RIFT_H * sz; |
| +добавлено | if (c.phase === 'travel') { |
| +добавлено | fx.arena.setFocus(hue, Math.min(2.5, 0.4 + c.t * 2)); |
| +добавлено | // it tears open from the middle to the tips, then the wound breathes |
| +добавлено | const open = smooth(0, 0.5, c.t), reach = 0.85 + 0.15 * smooth(0, 0.25, c.t); |
| +добавлено | if (s) { |
| +добавлено | s.open = (0.03 + 0.97 * open) * (1 + 0.05 * Math.sin(c.age * 3.1) * Math.sin(c.age * 7.3)); |
| +добавлено | s.reach = reach; s.shear = p.lensing * (0.14 + 0.86 * smooth(0, 0.35, c.t)); s.flare = 1.1 + 1.2 * (1 - open); |
| +добавлено | riftTendrils(s, c, p, 1); |
| +добавлено | } |
| +добавлено | if (G) { G.pos.copy(pos); G.p[0] = 34 * sz * p.gravity; G.p[1] = 0.4; G.p[2] = 0.5; G.p[3] = 9; } |
| +добавлено | // the floor comes apart and is dragged in: rocks flung up off a ring, dust peeled along the ground |
| +добавлено | for (let i = 0; i < 2; i++) { |
| +добавлено | const a = fx.rand() * 6.283, r = (1.6 + 4.2 * fx.rand()) * sz; |
| +добавлено | debrisEm.pos.set(pos.x + Math.cos(a) * r, 0.1, pos.z + Math.sin(a) * r); |
| +добавлено | debrisEm.vel.set(0, 1.4 + 2.2 * fx.rand(), 0); |
| +добавлено | debrisEm.rate(26 * open, dt); |
| +добавлено | const a2 = fx.rand() * 6.283, r2 = (2 + 3.6 * fx.rand()) * sz; |
| +добавлено | dustEm.pos.set(pos.x + Math.cos(a2) * r2, 0.18, pos.z + Math.sin(a2) * r2); |
| +добавлено | dustEm.vel.set(0, 0.5, 0); |
| +добавлено | dustEm.rate(11 * open, dt); |
| +добавлено | } |
| +добавлено | // a few sparks and motes spit off the burning edge, a thin dark ash where it tears |
| +добавлено | _a.copy(pos).addScaledVector(UP, (fx.rand() - 0.5) * 1.7 * H); |
| +добавлено | sparkEm.pos.copy(_a); sparkEm.radial = 2.2; sparkEm.rate(18 * open, dt); |
| +добавлено | moteEm.pos.copy(pos); moteEm.radius = 2.6 * sz; moteEm.rate(45 * open, dt); |
| +добавлено | ashEm.pos.copy(pos).addScaledVector(UP, -0.9 * H); ashEm.p.copy(pos).addScaledVector(UP, 0.9 * H); ashEm.rate(50 * open, dt); |
| +добавлено | // corruption spreads out from under it, in patches |
| +добавлено | d.splatT -= dt; |
| +добавлено | if (d.splatT <= 0 && p.corruption > 0) { |
| +добавлено | d.splatT = 0.3; |
| +добавлено | const a = fx.rand() * 6.283, r = (0.8 + 2.8 * smooth(0, p.hold, c.t) * (0.5 + 0.5 * fx.rand())) * sz; |
| +добавлено | _a.set(pos.x + Math.cos(a) * r, 0, pos.z + Math.sin(a) * r); |
| +добавлено | corrupt(_a, (0.9 + 0.5 * fx.rand()) * sz, 0.42 * p.corruption, c, 'noise'); |
| +добавлено | } |
| +добавлено | if (L) { L.pos.copy(pos); L.color.copy(edge); L.intensity = 11 * open * (0.8 + 0.2 * Math.sin(c.age * 31) * Math.sin(c.age * 13)); } |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dist = Math.hypot(dm.pos.x - pos.x, dm.pos.z - pos.z); |
| +добавлено | if (dist < 3.2 * sz) dm.status.corrupt = Math.max(dm.status.corrupt, 1.2 - dist / (3.2 * sz)); |
| +добавлено | } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (c.phase === 'impact') { |
| +добавлено | // it snaps shut, the edge flaring, the shear whipping back |
| +добавлено | const k = Math.min(1, c.t / 0.16); |
| +добавлено | const up = s ? s.riftU.uUp.value : UP; |
| +добавлено | if (s && s.rOn) { |
| +добавлено | s.open = (1 - k) * (1 - k); s.shear = p.lensing * (1 + 0.8 * Math.sin(k * 3.14)) * (1 - k * k); s.flare = 2 + 7 * k; |
| +добавлено | riftTendrils(s, c, p, 1 - k); |
| +добавлено | // it zips shut from both tips towards the middle, spitting at the closing points |
| +добавлено | const yz = 0.92 * H * (1 - k); |
| +добавлено | zipEm.pos.copy(pos).addScaledVector(up, yz); zipEm.burst(16 * p.collapse); |
| +добавлено | zipEm.pos.copy(pos).addScaledVector(up, -yz); zipEm.burst(16 * p.collapse); |
| +добавлено | if (k >= 1) s.rOn = false; |
| +добавлено | } |
| +добавлено | if (k >= 1 && !d.banged) { |
| +добавлено | d.banged = true; |
| +добавлено | seamEm.pos.copy(pos).addScaledVector(up, -0.55 * H); seamEm.p.copy(pos).addScaledVector(up, 0.55 * H); |
| +добавлено | seamEm.vel.set(0, 0.8, 0); seamEm.burst(240 * p.collapse); |
| +добавлено | ashEm.pos.copy(seamEm.pos); ashEm.p.copy(seamEm.p); ashEm.burst(90); |
| +добавлено | // the scar: the closed seam still burning, a thin jagged line that cools in half a second |
| +добавлено | SCAR.a.copy(pos).addScaledVector(up, -0.85 * H); SCAR.b.copy(pos).addScaledVector(up, 0.85 * H); |
| +добавлено | SCAR.color.copy(edge).lerp(_col.setRGB(1, 1, 1), 0.6); SCAR.glowColor.copy(edge).multiplyScalar(0.12); |
| +добавлено | SCAR.intensity = 26 * p.collapse; SCAR.seed = (c.seed * 7 + 3) & 0xffffff; SCAR.branchSeed = (c.seed * 13 + 5) & 0xffffff; |
| +добавлено | d.scar = fx.bolts.acquire(SCAR); |
| +добавлено | // a thin sheet of smoke breathed out along the seam, not a ball at its middle |
| +добавлено | seamSmoke.pos.copy(SCAR.a); seamSmoke.p.copy(SCAR.b); seamSmoke.size = 0.4 * sz; seamSmoke.burst(22); |
| +добавлено | SHOCK.pos.copy(pos); SHOCK.radius0 = 0.1; SHOCK.radius1 = 5 * sz; SHOCK.duration = 0.5; SHOCK.thickness = 0.9; SHOCK.distortion = 0.12 * p.collapse; SHOCK.ground = false; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | if (G) { G.type = 4; G.pos.copy(pos); G.p[0] = 0.2; G.p[1] = 1; G.p[2] = 60 * p.collapse; d.shockR = 0.2; } |
| +добавлено | if (L) L.intensity = 12 * p.collapse; |
| +добавлено | fx.flash(0.12 * p.collapse); |
| +добавлено | fx.rig.shake(0.3 * p.collapse, 14); |
| +добавлено | if (s) { for (const h of s.tend) h.release(); s.tend.length = 0; } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | if (G && G.type === 4) { |
| +добавлено | d.shockR += dt * 12; |
| +добавлено | G.p[0] = d.shockR; G.p[2] = 60 * p.collapse * Math.max(0, 1 - d.shockR / 6); |
| +добавлено | if (d.shockR > 6) { G.release(); d.grav = null; } |
| +добавлено | } |
| +добавлено | if (d.scar) { d.scar.intensity *= Math.exp(-dt * 6); if (d.scar.intensity < 0.3) { d.scar.release(); d.scar = null; } } |
| +добавлено | if (L) L.intensity *= Math.exp(-dt * 6); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // rift tendrils: each lashes out (fast), thrashes, retracts, then picks a new direction |
| +добавлено | function riftTendrils(s, c, p, gate) { |
| +добавлено | const n = s.tend.length, tgt = c.target >= 0 ? fx.dummies[c.target].pos : null; |
| +добавлено | for (let i = 0; i < n; i++) { |
| +добавлено | const h = s.tend[i], ph = c.t * s.trate[i] + s.toff[i], cyc = Math.floor(ph), f = ph - cyc; |
| +добавлено | if (cyc !== s.tcyc[i]) { |
| +добавлено | s.tcyc[i] = cyc; s.tlash[i] = 0; |
| +добавлено | const dir = s.tdir[i]; |
| +добавлено | dir.set(fx.rand() - 0.5, (fx.rand() - 0.35) * 0.9, fx.rand() - 0.5).normalize(); |
| +добавлено | if (tgt && fx.rand() < 0.6) dir.addScaledVector(_a.copy(tgt).setY(0.5 + 1.2 * fx.rand()).sub(s.pos).normalize(), 1.4).normalize(); |
| +добавлено | s.tlen[i] = (2.4 + 1.1 * fx.rand()) * p.size; |
| +добавлено | } |
| +добавлено | const e = gate * (ph < 0 ? 0 : f < 0.2 ? 1 - (1 - f / 0.2) ** 3 : f < 0.7 ? 1 : 1 - smooth(0.7, 1, f)); |
| +добавлено | const dir = s.tdir[i], y = ((i + 0.5) / n - 0.5) * 2.2 * p.size; |
| +добавлено | _b.copy(s.pos).addScaledVector(UP, y); |
| +добавлено | h.p0.set(_b.x, _b.y, _b.z, i * 0.37 + 0.1); |
| +добавлено | h.p1.set(dir.x, dir.y, dir.z, s.tlen[i]); |
| +добавлено | h.p2.set(0.055 * p.size, 0.16, 1.2, e); |
| +добавлено | h.p3.set(10, 9, 0, 0.85); |
| +добавлено | h.color.copy(edge); |
| +добавлено | h.intensity = 0.55 * p.tendrils; |
| +добавлено | // the lash lands: knock the target, spit sparks at the tip |
| +добавлено | if (gate >= 1 && ph > 0 && f >= 0.2 && !s.tlash[i]) { |
| +добавлено | s.tlash[i] = 1; |
| +добавлено | _b.addScaledVector(dir, s.tlen[i]); |
| +добавлено | sparkEm.pos.copy(_b); sparkEm.radial = 4; sparkEm.burst(40); |
| +добавлено | if (tgt && _b.distanceTo(_a.copy(tgt).setY(1.1)) < 1.4) { const dm = fx.dummies[c.target]; dm.hit(_a.copy(dir).setY(0).normalize(), 0.25); dm.status.corrupt = 1; } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { U, ARENA, COMMON_GLSL, NOISE_GLSL, FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL, FX_BLENDING } from '../../vfx/common.js'; |
| +добавлено | |
| +добавлено | // Portal (void hero). Two linked portals: A opens between the caster and the target, facing the |
| +добавлено | // caster; B opens behind the target, on the camera's side, facing it. Each opens as a vertical slit |
| +добавлено | // of light that tears wide (the Rift's tear, grown up), then its membrane clears into a real view |
| +добавлено | // out of the other portal: right before a surface draws (onBeforeRender, as three's Reflector), the |
| +добавлено | // opaque and lines scenes are rendered from the camera carried through the portal pair, with an |
| +добавлено | // oblique near plane on the partner's face, scissored to the portal's screen box, into one shared |
| +добавлено | // half-res target. So through A you see the dummy from B's side, sparks and stream included. The |
| +добавлено | // surface writes depth, so whatever crosses its plane disappears into it. |
| +добавлено | // Release: a stream of motes pours from the orb into A and out of B into the target's back. The |
| +добавлено | // entry is a parallel sheet with life = time to the plane, so every streak dies exactly at A's |
| +добавлено | // surface; B re-emits the same flow from just behind its plane, delayed by the flight time, so the |
| +добавлено | // heads emerge from its surface (B hides the tails). Out of B the motes are real pool particles: |
| +добавлено | // they hit the dummy capsule, morph into splash sparks and paint corruption on the floor. |
| +добавлено | // World: an orbit force round each rim (sparks spin with it, loose particles spiral in and die at |
| +добавлено | // A's heart), an attractor into A, a push out of B, violet lights, floor void sigils, corruption. |
| +добавлено | // Collapse: the rims go unstable, each portal squeezes shut into a slit and pops. |
| +добавлено | |
| +добавлено | const MAX = 2; // concurrent casts, two portals each |
| +добавлено | const UP = new THREE.Vector3(0, 1, 0); |
| +добавлено | |
| +добавлено | const RAW_FRAG_HEAD = /* glsl */` |
| +добавлено | precision highp float; |
| +добавлено | precision highp int; |
| +добавлено | precision highp sampler2D; |
| +добавлено | uniform mat4 projectionMatrix; |
| +добавлено | uniform mat4 viewMatrix; |
| +добавлено | uniform vec3 cameraPosition; |
| +добавлено | `; |
| +добавлено | |
| +добавлено | // shared by the surface and the glow ring: the rim wobbles (radius units) |
| +добавлено | const EDGE_GLSL = /* glsl */` |
| +добавлено | float portalEdge(float a, float seed, float instab) { |
| +добавлено | float e = 1.0 + 0.022 * sin(a * 5.0 + uTime * 3.7 + seed) + 0.016 * sin(a * 11.0 - uTime * 6.1 + seed * 2.0) |
| +добавлено | + 0.05 * (noise3(vec3(cos(a) * 0.3, sin(a) * 0.3, uTime * 0.22 + seed * 0.013)) - 0.5); |
| +добавлено | return e + instab * (0.07 * sin(a * 3.0 + uTime * 23.0) + 0.05 * sin(a * 7.0 - uTime * 31.0)); |
| +добавлено | } |
| +добавлено | // approximate signed distance (radius units) to the rim of the slit-to-disc ellipse; a = angle |
| +добавлено | float portalDist(vec2 q, float ox, float seed, float instab, out float a, out float re) { |
| +добавлено | vec2 qe = vec2(q.x / ox, q.y); |
| +добавлено | re = length(qe); |
| +добавлено | a = atan(qe.y, qe.x); |
| +добавлено | float g = length(vec2(q.x / (ox * ox), q.y)) / max(re, 1e-4); |
| +добавлено | return (re - portalEdge(a, seed, instab)) / max(g, 1e-4); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const SURF_VERT = FX_VERT_HEAD + COMMON_GLSL + /* glsl */` |
| +добавлено | uniform vec4 uC; // centre, radius (0 = hidden) |
| +добавлено | uniform vec4 uN; // normal, opening (x scale: slit -> disc) |
| +добавлено | uniform vec4 uU; // in-plane right axis |
| +добавлено | out vec3 vW; |
| +добавлено | out vec2 vQ; |
| +добавлено | void main() { |
| +добавлено | vec3 n = uN.xyz, u = uU.xyz, v = cross(n, u); |
| +добавлено | vQ = position.xy * vec2(max(uN.w, 0.03) * 1.15, 1.15); |
| +добавлено | vW = uC.xyz + (u * vQ.x + v * vQ.y) * uC.w; |
| +добавлено | gl_Position = uC.w > 0.0 ? projectionMatrix * viewMatrix * vec4(vW, 1.0) : vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // raw (like the rest of the project): a ShaderMaterial's program key also holds the renderer's light |
| +добавлено | // and clipping state, which the nested view render can leave different from the warm-up's |
| +добавлено | const SURF_FRAG = RAW_FRAG_HEAD + COMMON_GLSL + NOISE_GLSL + EDGE_GLSL + /* glsl */` |
| +добавлено | uniform vec4 uC; uniform vec4 uN; uniform vec4 uU; |
| +добавлено | uniform vec4 uLook; // clarity, ripple, rim gain, seed |
| +добавлено | uniform vec4 uLook2; // swirl (distortion), view twist, -, instability |
| +добавлено | uniform vec3 uRim; |
| +добавлено | uniform sampler2D uView; // the scene seen out of the partner, rendered just before this draw |
| +добавлено | in vec3 vW; |
| +добавлено | in vec2 vQ; |
| +добавлено | out vec4 fragColor; |
| +добавлено | |
| +добавлено | void main() { |
| +добавлено | float ox = max(uN.w, 0.03), a, re; |
| +добавлено | float d = portalDist(vQ, ox, uLook.w, uLook2.w, a, re); |
| +добавлено | if (d > 0.0) discard; |
| +добавлено | float e = -d, E = re - d; |
| +добавлено | |
| +добавлено | // the membrane: a dark spiral nebula with a few stars, there while the portal opens and closes |
| +добавлено | float rn = clamp(re / max(E, 1e-3), 0.0, 1.0); |
| +добавлено | float sw = a + 2.2 / (rn + 0.3) - uTime * 1.1; |
| +добавлено | float neb = fbm3(vec3(cos(sw) * rn * 0.45, sin(sw) * rn * 0.45, uTime * 0.06 + uLook.w * 0.01)); |
| +добавлено | vec3 col = uRim * (0.05 * pow(neb, 2.2) + 0.12 * pow(neb, 6.0)) + vec3(0.002, 0.0, 0.004); |
| +добавлено | col += vec3(0.8, 0.85, 1.0) * step(0.993, hash12(floor(vQ * 70.0) + uLook.w)) * (0.4 + 0.4 * sin(uTime * 4.0 + vQ.x * 30.0)); |
| +добавлено | |
| +добавлено | // the view: clears from the centre outwards with clarity. Space twists towards the rim and rings |
| +добавлено | // ripple out from the stream's entry point: sample where the displaced surface point projects |
| +добавлено | float reveal = smoothstep(0.0, 0.35, uLook.x * 1.35 - rn); |
| +добавлено | if (reveal > 0.0) { |
| +добавлено | vec3 n = uN.xyz, u = uU.xyz, v = cross(n, u); |
| +добавлено | float tw = uLook2.y * rn * rn * rn, c = cos(tw), s = sin(tw); |
| +добавлено | vec2 q = mat2(c, s, -s, c) * vQ; |
| +добавлено | float rl = length(q); |
| +добавлено | q += q / max(rl, 1e-3) * (uLook.y * 0.022 * sin(rl * 24.0 - uTime * 13.0) * exp(-rl * 2.2)); |
| +добавлено | vec4 cl = projectionMatrix * viewMatrix * vec4(uC.xyz + (u * q.x + v * q.y) * uC.w, 1.0); |
| +добавлено | vec3 view = texture(uView, cl.xy / cl.w * 0.5 + 0.5).rgb; |
| +добавлено | col = mix(col, view, reveal); |
| +добавлено | } |
| +добавлено | // rim: darkens to violet-black, then the white-hot inner edge |
| +добавлено | float em = e * uC.w; // metres inside the rim |
| +добавлено | col = mix(uRim * 0.01, col, smoothstep(0.02, 0.16, em)); |
| +добавлено | float crackle = 0.55 + 0.9 * noise3(vec3(a * 0.5, uTime * 1.4, uLook.w * 0.02)); |
| +добавлено | col += (uRim * (exp(-em / 0.12) * 1.2) + mix(uRim, vec3(1.0), 0.6) * exp(-em / 0.022) * 5.0) * crackle * uLook.z; |
| +добавлено | fragColor = vec4(col, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // outer glow, plasma filaments and the swirl distortion, half-res FX pass |
| +добавлено | const RING_VERT = FX_VERT_HEAD + /* glsl */` |
| +добавлено | uniform vec4 uC; uniform vec4 uN; uniform vec4 uU; |
| +добавлено | out vec2 vQ; |
| +добавлено | out float vViewZ; |
| +добавлено | out vec2 vJu; |
| +добавлено | out vec2 vJv; |
| +добавлено | void main() { |
| +добавлено | vec3 n = uN.xyz, u = uU.xyz, v = cross(n, u); |
| +добавлено | float R = uC.w; |
| +добавлено | vQ = position.xy * vec2(max(uN.w, 0.03) * 1.1 + 0.6, 1.75); |
| +добавлено | vec3 w = uC.xyz + (u * vQ.x + v * vQ.y) * R; |
| +добавлено | w += normalize(cameraPosition - uC.xyz) * 0.05; // in front of the opaque surface |
| +добавлено | vec4 cv = viewMatrix * vec4(w, 1.0); |
| +добавлено | vViewZ = -cv.z; |
| +добавлено | gl_Position = R > 0.0 ? projectionMatrix * cv : vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | mat4 vp = projectionMatrix * viewMatrix; |
| +добавлено | vec4 c0 = vp * vec4(uC.xyz, 1.0), cu = vp * vec4(uC.xyz + u * 0.1, 1.0), cw = vp * vec4(uC.xyz + v * 0.1, 1.0); |
| +добавлено | vJu = (cu.xy / cu.w - c0.xy / c0.w) * 5.0; // screen uv per metre along u, v |
| +добавлено | vJv = (cw.xy / cw.w - c0.xy / c0.w) * 5.0; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const RING_FRAG = FX_FRAG_HEAD + COMMON_GLSL + NOISE_GLSL + FX_OUT_GLSL + EDGE_GLSL + /* glsl */` |
| +добавлено | uniform vec4 uC; uniform vec4 uN; |
| +добавлено | uniform vec4 uLook; uniform vec4 uLook2; |
| +добавлено | uniform vec3 uRim; |
| +добавлено | in vec2 vQ; |
| +добавлено | in float vViewZ; |
| +добавлено | in vec2 vJu; |
| +добавлено | in vec2 vJv; |
| +добавлено | void main() { |
| +добавлено | float ox = max(uN.w, 0.03), a, re; |
| +добавлено | float dm = portalDist(vQ, ox, uLook.w, uLook2.w, a, re) * uC.w; // metres, > 0 outside |
| +добавлено | float ad = abs(dm), out_ = step(0.0, dm); |
| +добавлено | float fil = noise3(vec3(cos(a) * 0.4 + uTime * 0.03, sin(a) * 0.4, dm * 0.5 - uTime * 0.5 + uLook.w * 0.01)); |
| +добавлено | fil = pow(clamp(fil * 1.7 - 0.45, 0.0, 1.0), 3.0); |
| +добавлено | vec3 e = uRim * uLook.z * (exp(-ad / 0.035) * 2.2 + out_ * (exp(-dm / 0.16) * 0.7 + fil * exp(-dm / 0.25) * 4.0)); |
| +добавлено | // space dragged round the rim |
| +добавлено | vec2 qe = vec2(vQ.x / ox, vQ.y), tg = vec2(-qe.y, qe.x) / max(re, 1e-3); |
| +добавлено | float amt = uLook2.x * exp(-ad / 0.35) * smoothstep(-0.25, 0.0, dm) * uC.w; |
| +добавлено | vec2 dist = (vJu * tg.x + vJv * tg.y) * amt; |
| +добавлено | float s = softFade(vViewZ, 0.15); |
| +добавлено | fxOut(vec3(0.0), 0.0, e * s, dist * s, vViewZ); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // module scratch (nothing allocated per frame) |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _h = new THREE.Vector3(), _sd = new THREE.Vector3(), _t = new THREE.Vector3(); |
| +добавлено | const _col = new THREE.Color(), _s2 = new THREE.Vector2(), _M = new THREE.Matrix4(), _F = new THREE.Matrix4(); |
| +добавлено | const _plane = new THREE.Plane(), _clip = new THREE.Vector4(), _q4 = new THREE.Vector4(); |
| +добавлено | const FLIP = new THREE.Matrix4().makeScale(-1, 1, -1); // half a turn about the portal's up axis |
| +добавлено | const SPLAT = { x: 0, z: 0, radius: 1, corruption: 0, glow: 0, pattern: 'noise', seed: 0, width: 0 }; |
| +добавлено | const SHOCK = { pos: new THREE.Vector3(), radius0: 0.2, radius1: 3, duration: 0.5, thickness: 0.6, distortion: 0.08, intensity: 0, ground: false, color: null }; |
| +добавлено | const SIGIL = { pos: new THREE.Vector3(), radius: 1, style: 'void', intensity: 0, reveal: 0, seed: 0, color: null }; |
| +добавлено | const clamp01 = (x) => Math.min(1, Math.max(0, x)); |
| +добавлено | const smooth = (a, b, x) => { const t = clamp01((x - a) / (b - a)); return t * t * (3 - 2 * t); }; |
| +добавлено | const win = (x, Ti) => smooth(0, 0.12, x) * (1 - smooth(Ti - 0.5, Ti - 0.3, x)); // the stream window |
| +добавлено | const backOut = (x) => { x = clamp01(x) - 1; return 1 + 2.4 * x * x * x + 1.4 * x * x; }; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'portal', name: 'Portal', element: 'void', order: 10, hue: '#8a3dff', |
| +добавлено | shape: 'area', radius: 0.35, |
| +добавлено | params: { size: 1, reach: 1, speed: 16, stream: 1, sparks: 1, clarity: 1, swirl: 1, ripple: 1, knockback: 1, corruption: 1, rim: '#9a4dff', core: '#efe4ff' }, |
| +добавлено | ranges: { size: [0.5, 1.6, 0.01], reach: [0.6, 1.8, 0.01], speed: [6, 30, 0.1], stream: [0, 3, 0.01], sparks: [0, 3, 0.01], clarity: [0, 1, 0.01], swirl: [0, 3, 0.01], ripple: [0, 3, 0.01], knockback: [0, 3, 0.01], corruption: [0, 3, 0.01] }, |
| +добавлено | presets: { |
| +добавлено | 'event horizon': { rim: '#5a8cff', core: '#e4f0ff', ripple: 2.2, swirl: 0.6, sparks: 0.4 }, |
| +добавлено | 'ember ring': { rim: '#ff7a1a', core: '#fff0d8', sparks: 2.2, swirl: 1.4, corruption: 0.3 }, |
| +добавлено | abyss: { rim: '#ff2a60', core: '#ffd6e0', clarity: 0.55, swirl: 2, corruption: 2, stream: 1.6 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 1.2, impact: 2.4, linger: 2.0 }, |
| +добавлено | kinds: { |
| +добавлено | // the entering sheet: no forces, no collisions, life = flight time to A's plane |
| +добавлено | stream: { style: 'glow', drag: 0, gravity: 0, forceResponse: 0, life: [1, 1], size: [0.026, 0.022], stretch: 0.03, emissive: 3.2, color: ['#f1e9ff', '#a670ff'], alpha: [1, 1], fadeIn: 0.03, fadeOut: 0.001 }, |
| +добавлено | // out of B: real particles, they splash on what they hit |
| +добавлено | jet: { style: 'glow', drag: 0.4, gravity: 2, forceResponse: 0, restitution: 0.25, friction: 0.5, life: [0.45, 0.8], size: [0.026, 0.01], stretch: 0.03, emissive: 5, color: ['#f1e9ff', '#7b2dff'], alpha: [1, 0], fadeIn: 0.001, fadeOut: 0.35, morph: 'portal.splash' }, |
| +добавлено | splash: { style: 'glow', drag: 1.3, gravity: 7, forceResponse: 0.5, restitution: 0.3, friction: 0.4, life: [0.25, 0.6], size: [0.03, 0.006], stretch: 0.03, emissive: 11, color: ['#ffffff', '#8a3dff'], alpha: [1, 0], paints: 'corruption', paintAmount: 0.05 }, |
| +добавлено | // rim sparks ride the portal's orbit force round the edge, then fall |
| +добавлено | spark: { style: 'glow', drag: 0.35, gravity: 2.4, forceResponse: 1, restitution: 0.3, friction: 0.3, life: [0.45, 1.0], size: [0.022, 0.006], stretch: 0.045, emissive: 8, color: ['#f4ecff', '#8a3dff'], alpha: [1, 0], fadeIn: 0.02 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const streamEm = P.emitter('portal.stream', { shape: 'line', radius: 0.05 }); |
| +добавлено | const jetEm = P.emitter('portal.jet', { shape: 'line', radius: 0.05, spread: 0.7 }); |
| +добавлено | const sparkEm = P.emitter('portal.spark', { shape: 'line', radius: 0.03, spread: 0.5 }); |
| +добавлено | const splashEm = P.emitter('portal.splash', { shape: 'sphere', radius: 0.15, spread: 3, radial: 3 }); |
| +добавлено | const moteIn = P.emitter('void.mote', { shape: 'shell', radius: 2.2, spread: 0.3, radial: -1.6 }); |
| +добавлено | const moteOut = P.emitter('void.mote', { shape: 'cone', radius: 0.15, spread: 0.4, radial: 2.6, cone: 0.9 }); |
| +добавлено | const popEm = P.emitter('void.spark', { shape: 'shell', radius: 0.25, spread: 2, radial: 7 }); |
| +добавлено | const smokeEm = P.emitter('void.smoke', { shape: 'sphere', radius: 0.4, spread: 0.6, radial: 1.5 }); |
| +добавлено | |
| +добавлено | const rim = new THREE.Color(), core = new THREE.Color(); |
| +добавлено | const quad = new THREE.PlaneGeometry(2, 2); |
| +добавлено | const R = fx.renderer, VIEW_SCALE = fx.Q?.mobile ? 0.35 : 0.5; |
| +добавлено | R.getDrawingBufferSize(_s2); |
| +добавлено | // one target for every portal: each surface draws right after its own view is rendered |
| +добавлено | const viewRT = new THREE.WebGLRenderTarget(Math.ceil(_s2.x * VIEW_SCALE), Math.ceil(_s2.y * VIEW_SCALE), { type: THREE.HalfFloatType, generateMipmaps: false }); |
| +добавлено | const uView = { value: viewRT.texture }; |
| +добавлено | const vcam = new THREE.PerspectiveCamera(); |
| +добавлено | vcam.matrixAutoUpdate = false; vcam.matrixWorldAutoUpdate = false; |
| +добавлено | |
| +добавлено | // four portals (two per cast); surface and ring share the portal's uniform objects |
| +добавлено | const portals = Array.from({ length: MAX * 2 }, (_, i) => ({ |
| +добавлено | i, c: new THREE.Vector3(), n: new THREE.Vector3(0, 0, 1), u: new THREE.Vector3(1, 0, 0), |
| +добавлено | R: 0, ox: 0.03, clarity: 0, ripple: 0, gain: 0, instab: 0, seed: 0, light: null, sigil: null, orbit: null, partner: null, surf: null, ring: null, vis: false, |
| +добавлено | su: { uC: { value: new THREE.Vector4() }, uN: { value: new THREE.Vector4() }, uU: { value: new THREE.Vector4() }, uLook: { value: new THREE.Vector4() }, uLook2: { value: new THREE.Vector4() }, uRim: { value: rim } }, |
| +добавлено | })); |
| +добавлено | for (const p of portals) { |
| +добавлено | p.partner = portals[p.i ^ 1]; |
| +добавлено | p.surf = new THREE.Mesh(quad, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: SURF_VERT, fragmentShader: SURF_FRAG, side: THREE.DoubleSide, |
| +добавлено | uniforms: { ...U, ...p.su, uView }, |
| +добавлено | })); |
| +добавлено | p.surf.onBeforeRender = (r, scene, camera) => renderView(p, camera); |
| +добавлено | p.ring = new THREE.Mesh(quad, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: RING_VERT, fragmentShader: RING_FRAG, ...FX_BLENDING, side: THREE.DoubleSide, |
| +добавлено | uniforms: { ...U, ...p.su }, |
| +добавлено | })); |
| +добавлено | for (const m of [p.surf, p.ring]) { m.frustumCulled = false; m.visible = false; } |
| +добавлено | fx.scenes.opaque.add(p.surf); |
| +добавлено | fx.scenes.fx.add(p.ring); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // world matrix of a portal's frame (u, v, n, centre) |
| +добавлено | function frame(q, out) { |
| +добавлено | _b.crossVectors(q.n, q.u); |
| +добавлено | return out.set(q.u.x, _b.x, q.n.x, q.c.x, q.u.y, _b.y, q.n.y, q.c.y, q.u.z, _b.z, q.n.z, q.c.z, 0, 0, 0, 1); |
| +добавлено | } |
| +добавлено | let inView = false; |
| +добавлено | // the scene out of X's partner Y, as seen through X: the camera carried by Y * flip * X^-1, an |
| +добавлено | // oblique near plane on Y's face (Lengyel), scissored to X's box on screen |
| +добавлено | function renderView(X, camera) { |
| +добавлено | if (inView || camera !== fx.camera || X.R <= 0.002 || X.clarity <= 0.001) return; |
| +добавлено | const Y = X.partner; |
| +добавлено | R.getDrawingBufferSize(_s2); |
| +добавлено | const w = Math.ceil(_s2.x * VIEW_SCALE), h = Math.ceil(_s2.y * VIEW_SCALE); |
| +добавлено | if (viewRT.width !== w || viewRT.height !== h) viewRT.setSize(w, h); |
| +добавлено | _M.copy(frame(Y, _F)).multiply(FLIP).multiply(frame(X, _F).invert()); |
| +добавлено | vcam.matrixWorld.multiplyMatrices(_M, camera.matrixWorld); |
| +добавлено | vcam.matrixWorldInverse.copy(vcam.matrixWorld).invert(); |
| +добавлено | vcam.projectionMatrix.copy(camera.projectionMatrix); |
| +добавлено | _a.setFromMatrixPosition(vcam.matrixWorld).sub(Y.c); |
| +добавлено | _plane.setFromNormalAndCoplanarPoint(_b.copy(Y.n).multiplyScalar(_a.dot(Y.n) < 0 ? 1 : -1), Y.c).applyMatrix4(vcam.matrixWorldInverse); |
| +добавлено | const pe = vcam.projectionMatrix.elements; |
| +добавлено | _clip.set(_plane.normal.x, _plane.normal.y, _plane.normal.z, _plane.constant); |
| +добавлено | _q4.set((Math.sign(_clip.x) + pe[8]) / pe[0], (Math.sign(_clip.y) + pe[9]) / pe[5], -1, (1 + pe[10]) / pe[14]); |
| +добавлено | _clip.multiplyScalar(2 / _clip.dot(_q4)); |
| +добавлено | pe[2] = _clip.x; pe[6] = _clip.y; pe[10] = _clip.z + 1; pe[14] = _clip.w; |
| +добавлено | vcam.projectionMatrixInverse.copy(vcam.projectionMatrix).invert(); |
| +добавлено | // X's screen box (with room for the twist and ripple) |
| +добавлено | let x0 = 1, y0 = 1, x1 = -1, y1 = -1; |
| +добавлено | _b.crossVectors(X.n, X.u); |
| +добавлено | for (let k = 0; k < 4; k++) { |
| +добавлено | _a.copy(X.c).addScaledVector(X.u, (k & 1 ? 1.25 : -1.25) * X.R).addScaledVector(_b, (k & 2 ? 1.25 : -1.25) * X.R).project(camera); |
| +добавлено | if (_a.z > 1) { x0 = y0 = -1; x1 = y1 = 1; break; } |
| +добавлено | x0 = Math.min(x0, _a.x); x1 = Math.max(x1, _a.x); y0 = Math.min(y0, _a.y); y1 = Math.max(y1, _a.y); |
| +добавлено | } |
| +добавлено | const px0 = Math.max(0, Math.floor((x0 * 0.5 + 0.5) * w)), py0 = Math.max(0, Math.floor((y0 * 0.5 + 0.5) * h)); |
| +добавлено | const px1 = Math.min(w, Math.ceil((x1 * 0.5 + 0.5) * w)), py1 = Math.min(h, Math.ceil((y1 * 0.5 + 0.5) * h)); |
| +добавлено | if (px1 <= px0 || py1 <= py0) return; |
| +добавлено | viewRT.scissor.set(px0, py0, px1 - px0, py1 - py0); |
| +добавлено | viewRT.scissorTest = true; |
| +добавлено | inView = true; |
| +добавлено | for (const q of portals) { q.vis = q.surf.visible; q.surf.visible = false; } |
| +добавлено | const prev = R.getRenderTarget(), auto = R.autoClear, ca = R.getClearAlpha(); |
| +добавлено | R.getClearColor(_col); |
| +добавлено | R.autoClear = false; |
| +добавлено | R.setRenderTarget(viewRT); |
| +добавлено | R.setClearColor(0x000000, 1); |
| +добавлено | R.clear(true, true, false); |
| +добавлено | R.render(fx.scenes.opaque, vcam); |
| +добавлено | R.render(fx.scenes.lines, vcam); |
| +добавлено | R.setRenderTarget(prev); |
| +добавлено | R.setClearColor(_col, ca); |
| +добавлено | R.autoClear = auto; |
| +добавлено | for (const q of portals) q.surf.visible = q.vis; |
| +добавлено | inView = false; |
| +добавлено | } |
| +добавлено | const slots = Array.from({ length: MAX }, (_, k) => ({ busy: false, A: portals[k * 2], B: portals[k * 2 + 1], AB: [portals[k * 2], portals[k * 2 + 1]] })); |
| +добавлено | const takeSlot = () => { for (const s of slots) if (!s.busy) { s.busy = true; return s; } return null; }; |
| +добавлено | |
| +добавлено | // kind colours follow the rim / core params (texels, no recompile) |
| +добавлено | const KC = { stream: { color: [new THREE.Color(), new THREE.Color()] }, jet: { color: [new THREE.Color(), new THREE.Color()] }, splash: { color: [new THREE.Color(), new THREE.Color()] }, spark: { color: [new THREE.Color(), new THREE.Color()] } }; |
| +добавлено | let lastRim = '', lastCore = ''; |
| +добавлено | function retint() { |
| +добавлено | const p = S.params; |
| +добавлено | if (p.rim === lastRim && p.core === lastCore) return; |
| +добавлено | lastRim = p.rim; lastCore = p.core; |
| +добавлено | rim.set(p.rim); core.set(p.core); |
| +добавлено | KC.stream.color[0].copy(core); KC.stream.color[1].copy(core).lerp(rim, 0.8); |
| +добавлено | KC.jet.color[0].copy(core); KC.jet.color[1].copy(rim); |
| +добавлено | KC.splash.color[0].copy(core).lerp(_col.setRGB(1, 1, 1), 0.5); KC.splash.color[1].copy(rim); |
| +добавлено | KC.spark.color[0].copy(core); KC.spark.color[1].copy(rim); |
| +добавлено | for (const k in KC) P.setKind(`portal.${k}`, KC[k]); |
| +добавлено | } |
| +добавлено | retint(); |
| +добавлено | |
| +добавлено | function splat(x, z, radius, amount, pattern, seed, width = 0) { |
| +добавлено | SPLAT.x = x; SPLAT.z = z; SPLAT.radius = radius; SPLAT.corruption = amount; SPLAT.glow = 0; SPLAT.pattern = pattern; SPLAT.seed = seed % 100; SPLAT.width = width; |
| +добавлено | fx.ground.splat(SPLAT); |
| +добавлено | } |
| +добавлено | function shock(pos, r1, dist) { |
| +добавлено | SHOCK.pos.copy(pos); SHOCK.radius0 = 0.15; SHOCK.radius1 = r1; SHOCK.duration = 0.45; SHOCK.thickness = 0.5; SHOCK.distortion = dist; SHOCK.ground = false; |
| +добавлено | fx.shocks?.spawn(SHOCK); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // A between the caster and the target facing the caster; B behind the target on the camera's |
| +добавлено | // side, facing the target's torso |
| +добавлено | function place(s, c, p) { |
| +добавлено | const A = s.A, B = s.B, R0 = p.size; |
| +добавлено | _h.subVectors(c.to, c.from).setY(0); |
| +добавлено | if (_h.lengthSq() < 1e-4) _h.set(0, 0, -1); |
| +добавлено | _h.normalize(); |
| +добавлено | _sd.crossVectors(_h, UP); |
| +добавлено | const dist = Math.hypot(c.to.x - c.from.x, c.to.z - c.from.z); |
| +добавлено | A.c.copy(c.from).addScaledVector(_h, Math.min(2.8, dist * 0.35)).setY(Math.max(c.from.y, R0 + 0.45)); |
| +добавлено | A.n.copy(_h).negate(); |
| +добавлено | const sg = Math.sign(_t.subVectors(fx.camera.position, c.to).dot(_sd)) || 1; |
| +добавлено | B.c.copy(c.to).addScaledVector(_h, 2.8 * p.reach).addScaledVector(_sd, sg * 1.5 * p.reach).setY(R0 + 0.55); |
| +добавлено | const rr = Math.hypot(B.c.x, B.c.z), lim = ARENA.radius - 1.5 - R0; |
| +добавлено | if (rr > lim) { B.c.x *= lim / rr; B.c.z *= lim / rr; } |
| +добавлено | _t.copy(c.to).setY(THREE.MathUtils.clamp(c.to.y, 0.9, 1.4)); |
| +добавлено | B.n.subVectors(_t, B.c).normalize(); |
| +добавлено | for (const q of s.AB) { |
| +добавлено | q.u.crossVectors(UP, q.n).normalize(); |
| +добавлено | q.seed = (c.seed % 997) * 0.37 + q.i * 13.1; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // per-portal look over the cast's global clock T (lag: B trails A a little) |
| +добавлено | function animate(q, c, p, lag) { |
| +добавлено | const T = c.age - lag, Tc = c.durations.charge, Te = Tc + c.durations.impact; |
| +добавлено | const slit = smooth(0.15, 0.38, T), open = backOut((T - 0.4) / 0.55); |
| +добавлено | const R0 = p.size; |
| +добавлено | q.R = R0 * slit; |
| +добавлено | q.ox = 0.03 + 0.97 * Math.max(0, open); |
| +добавлено | q.clarity = p.clarity * smooth(0.75, 1.25, T); |
| +добавлено | q.gain = slit * (1.9 - 0.9 * smooth(0.4, 0.9, T)) * (0.9 + 0.1 * Math.sin(c.age * 37 + q.i)); |
| +добавлено | q.ripple = 0; q.instab = 0; |
| +добавлено | const rel = c.age - Tc - lag; // the stream window |
| +добавлено | if (rel > 0 && T < Te + 0.1) q.ripple = p.ripple * smooth(0, 0.2, rel) * (1 - smooth(c.durations.impact - 0.45, c.durations.impact, rel)); |
| +добавлено | const tl = T - Te; // collapse |
| +добавлено | if (tl > 0) { |
| +добавлено | q.instab = smooth(0, 0.5, tl); |
| +добавлено | q.clarity *= 1 - smooth(0.2, 0.55, tl); |
| +добавлено | const close = smooth(0.5, 0.88, tl); |
| +добавлено | q.ox = Math.max(0.03, q.ox * (1 - close)); |
| +добавлено | q.R *= 1 - 0.25 * close; |
| +добавлено | q.gain *= 1 + 1.6 * q.instab + 2 * close; |
| +добавлено | if (tl >= 0.9) q.R = 0; |
| +добавлено | } |
| +добавлено | return tl; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // a line of emission across portal q's face through centre o at angle f |
| +добавлено | function comb(q, o, f, half, em) { |
| +добавлено | _b.crossVectors(q.n, q.u); |
| +добавлено | _a.copy(q.u).multiplyScalar(Math.cos(f) * half).addScaledVector(_b, Math.sin(f) * half); |
| +добавлено | em.pos.copy(o).add(_a); em.p.copy(o).sub(_a); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function pop(q, c, p) { |
| +добавлено | popEm.pos.copy(q.c); popEm.radial = 7 * p.size; popEm.burst(500 * p.sparks + 150); |
| +добавлено | smokeEm.pos.copy(q.c); smokeEm.size = 0.6 * p.size; smokeEm.burst(14); |
| +добавлено | shock(q.c, 3.2 * p.size, 0.07); |
| +добавлено | splat(q.c.x, q.c.z, 1.4 * p.size, 1.6 * p.corruption, 'radial', c.seed + q.i); |
| +добавлено | if (q.light) { q.light.intensity = 40; q.light.range = 9; } |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'rim' || k === 'core') retint(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = c.data; |
| +добавлено | retint(); |
| +добавлено | if (name === 'charge') { |
| +добавлено | d.s = takeSlot(); d.hit = fx.lights.acquire(); d.attr = fx.forces.acquire(); d.push = fx.forces.acquire(); |
| +добавлено | d.hitT = 0; d.splatT = 0; d.popped = 0; d.released = false; |
| +добавлено | if (d.hit) { d.hit.range = 6; d.hit.intensity = 0; d.hit.color.copy(rim); } |
| +добавлено | if (d.attr) { d.attr.type = 2; d.attr.p[0] = 0; d.attr.p[1] = 0.4; d.attr.p[2] = 0.2; d.attr.p[3] = 5; } |
| +добавлено | if (d.push) { d.push.type = 5; d.push.p[0] = 0; d.push.p[1] = 1; d.push.p[2] = 4; d.push.p[3] = 0; } |
| +добавлено | const s = d.s; |
| +добавлено | if (!s) return; |
| +добавлено | place(s, c, p); |
| +добавлено | for (const q of s.AB) { |
| +добавлено | q.R = 0; |
| +добавлено | q.light = fx.lights.acquire(); |
| +добавлено | if (q.light) { q.light.range = 7.5; q.light.intensity = 0; q.light.shadow = true; q.light.color.copy(rim); } |
| +добавлено | SIGIL.pos.set(q.c.x, 0.03, q.c.z); SIGIL.radius = 1.3 * p.size; SIGIL.seed = q.seed | 0; SIGIL.color = rim; SIGIL.intensity = 0; SIGIL.reveal = 0; |
| +добавлено | q.sigil = fx.sigils.acquire(SIGIL); |
| +добавлено | q.orbit = fx.forces.acquire(); |
| +добавлено | if (q.orbit) { q.orbit.type = 3; q.orbit.dir.copy(q.n).multiplyScalar(q === s.A ? 1 : -1); q.orbit.p[0] = 0; q.orbit.p[1] = 0.25; q.orbit.p[2] = 1.9 * p.size; q.orbit.p[3] = 0; } |
| +добавлено | } |
| +добавлено | } else if (name === 'release') { |
| +добавлено | fx.arena.setFocus(rim, 0.3); |
| +добавлено | fx.rig.shake(0.18, 9); |
| +добавлено | fx.flash(0.08); |
| +добавлено | const s = d.s; |
| +добавлено | if (!s) return; |
| +добавлено | for (const q of s.AB) { |
| +добавлено | shock(q.c, 3 * p.size, 0.06); |
| +добавлено | splat(q.c.x, q.c.z, 1.5 * p.size, 1.2 * p.corruption, 'ring', c.seed + q.i, 0.35); |
| +добавлено | fx.impacts.add('corruption', _a.copy(q.c).setY(0.3), 1.8 * p.size, 0.7, 6, rim); |
| +добавлено | popEm.pos.copy(q.c); popEm.radial = 4; popEm.burst(120 * p.sparks); |
| +добавлено | } |
| +добавлено | } else if (name === 'done') { |
| +добавлено | d.hit?.release(); d.attr?.release(); d.push?.release(); |
| +добавлено | d.hit = d.attr = d.push = null; |
| +добавлено | const s = d.s; |
| +добавлено | if (s) { |
| +добавлено | for (const q of s.AB) { |
| +добавлено | q.light?.release(); q.sigil?.release(); q.orbit?.release(); |
| +добавлено | q.light = q.sigil = q.orbit = null; q.R = 0; |
| +добавлено | } |
| +добавлено | s.busy = false; |
| +добавлено | } |
| +добавлено | d.s = null; |
| +добавлено | fx.arena.setFocus(_col.set(S.hue), 2.5); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = c.data, s = d.s; |
| +добавлено | if (!s) return; |
| +добавлено | const A = s.A, B = s.B, Tc = c.durations.charge, Ti = c.durations.impact; |
| +добавлено | const tlA = animate(A, c, p, 0), tlB = animate(B, c, p, 0.1); |
| +добавлено | const v = p.speed, start = 2.8 * Math.min(1, A.c.distanceTo(c.from) / 2.8); // entry plane distance from A |
| +добавлено | const tau = start / v, rel = c.age - Tc; |
| +добавлено | |
| +добавлено | // orb: flares, then dims while the stream drains it |
| +добавлено | if (c.phase === 'charge') fx.arena.setFocus(rim, 0.5 + 2.5 * smooth(0, 0.6, c.t)); |
| +добавлено | |
| +добавлено | for (const q of s.AB) { |
| +добавлено | const open = q.R > 0 ? smooth(0.3, 1, q.ox) * q.R / p.size : 0; |
| +добавлено | if (q.light) { q.light.pos.copy(q.c).addScaledVector(q.n, 0.45); q.light.color.copy(rim); q.light.intensity = Math.max(q.light.intensity * Math.exp(-dt * 6), 10 * q.gain * (q.R > 0 ? 1 : 0)); } |
| +добавлено | if (q.sigil) { |
| +добавлено | q.sigil.reveal = smooth(0, 0.95, c.age); |
| +добавлено | q.sigil.intensity = 2.4 * (1 - smooth(0.6, 1.8, c.age - Tc - Ti)); |
| +добавлено | q.sigil.rotation += dt * (q === A ? 0.4 : -0.4); |
| +добавлено | if (q.sigil.intensity <= 0) { q.sigil.release(); q.sigil = null; } |
| +добавлено | } |
| +добавлено | if (q.orbit) { q.orbit.pos.copy(q.c); q.orbit.p[0] = 22 * q.R * open; q.orbit.p[3] = 1.4 * open; } |
| +добавлено | // rim sparks: a chord of the rim per frame, flung along the spin |
| +добавлено | if (open > 0.2 && p.sparks > 0) { |
| +добавлено | const ang = fx.rand() * 6.2832, a2 = ang + 0.4, sp = (q === A ? 1 : -1); |
| +добавлено | const rr = q.R * 1.02; |
| +добавлено | sparkEm.pos.copy(q.c).addScaledVector(q.u, Math.cos(ang) * rr * q.ox).addScaledVector(_b.crossVectors(q.n, q.u), Math.sin(ang) * rr); |
| +добавлено | sparkEm.p.copy(q.c).addScaledVector(q.u, Math.cos(a2) * rr * q.ox).addScaledVector(_b, Math.sin(a2) * rr); |
| +добавлено | _t.subVectors(sparkEm.p, sparkEm.pos).normalize(); |
| +добавлено | sparkEm.vel.copy(_t).multiplyScalar(4.5 * sp).addScaledVector(_a.subVectors(sparkEm.pos, q.c).normalize(), 0.8); |
| +добавлено | sparkEm.rate(700 * p.sparks * (0.4 + q.gain * 0.5 + q.instab * 1.5), dt); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | // A inhales, B exhales: motes stream into A's heart, out of B's face |
| +добавлено | const k = smooth(0.35, 1, c.t / Tc); |
| +добавлено | moteIn.pos.copy(A.c); moteIn.radius = 2.2 * p.size; moteIn.rate(900 * k, dt); |
| +добавлено | moteOut.pos.copy(B.c).addScaledVector(B.n, -0.1); moteOut.p.copy(B.n); moteOut.vel.copy(B.n).multiplyScalar(1.2); moteOut.rate(500 * k, dt); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the stream: in through A (dies on its plane), out of B one flight time later |
| +добавлено | const inRate = 4800 * p.stream * win(rel, Ti), outRate = 4800 * p.stream * win(rel - tau, Ti); |
| +добавлено | // a twisting cross of two combs: B re-emits the comb that reached A, its angle mirrored |
| +добавлено | // (x flips through the portal), so the helix runs on unbroken out of B |
| +добавлено | if (inRate > 0 && A.R > 0) { |
| +добавлено | _t.copy(A.c).addScaledVector(A.n, start); |
| +добавлено | for (let j = 0; j < 2; j++) { |
| +добавлено | comb(A, _t, 18 * rel + j * 1.5708, 0.24 * p.size, streamEm); |
| +добавлено | streamEm.vel.copy(A.n).multiplyScalar(-v); |
| +добавлено | streamEm.life = (start + 0.1) / v; |
| +добавлено | streamEm.rate(inRate * 0.5, dt); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | if (outRate > 0 && B.R > 0) { |
| +добавлено | _t.copy(B.c).addScaledVector(B.n, -0.08); |
| +добавлено | for (let j = 0; j < 2; j++) { |
| +добавлено | comb(B, _t, Math.PI - 18 * (rel - tau) - j * 1.5708, 0.24 * p.size, jetEm); |
| +добавлено | jetEm.vel.copy(B.n).multiplyScalar(v); |
| +добавлено | jetEm.rate(outRate * 0.5, dt); |
| +добавлено | } |
| +добавлено | // the hit: knock the target from behind, light it, corrupt it and the floor under it |
| +добавлено | const k = outRate / (4800 * Math.max(p.stream, 1e-3)); |
| +добавлено | if (d.hit) { d.hit.pos.copy(c.to).addScaledVector(B.n, -0.5); d.hit.color.copy(rim); d.hit.intensity = 22 * k * (0.8 + 0.2 * Math.sin(c.age * 47)); } |
| +добавлено | d.hitT -= dt; |
| +добавлено | if (d.hitT <= 0) { |
| +добавлено | d.hitT = 0.2; |
| +добавлено | if (c.target >= 0) { |
| +добавлено | const dm = fx.dummies[c.target]; |
| +добавлено | if (dm.pos.distanceTo(_a.copy(B.c).setY(0)) < 5) { |
| +добавлено | dm.hit(_a.copy(B.n).setY(0).normalize(), 0.16 * p.knockback * k); |
| +добавлено | dm.status.corrupt = Math.max(dm.status.corrupt, 0.9); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | splat(c.to.x, c.to.z, 0.9 * p.size, 0.9 * p.corruption * k, 'noise', c.seed + c.age * 31); |
| +добавлено | splashEm.pos.copy(c.to).addScaledVector(B.n, -0.3); splashEm.vel.copy(B.n).multiplyScalar(-2); splashEm.burst(40 * p.stream * k); |
| +добавлено | } |
| +добавлено | } else if (d.hit) d.hit.intensity *= Math.exp(-dt * 8); |
| +добавлено | if (!d.released && rel > Ti - 0.3 + tau && c.target >= 0) { |
| +добавлено | // the last of the stream lands: one heavy shove |
| +добавлено | d.released = true; |
| +добавлено | const dm = fx.dummies[c.target]; |
| +добавлено | if (dm.pos.distanceTo(_a.copy(B.c).setY(0)) < 5) dm.hit(_a.copy(B.n).setY(0).normalize(), 0.7 * p.knockback); |
| +добавлено | fx.impacts.add('corruption', _a.copy(c.to), 1.6 * p.size, 1, 8, rim); |
| +добавлено | fx.rig.shake(0.22, 12); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // forces on loose particles: pulled into A, blown out of B, while open |
| +добавлено | const oA = A.R > 0 ? smooth(0.3, 1, A.ox) : 0, oB = B.R > 0 ? smooth(0.3, 1, B.ox) : 0; |
| +добавлено | if (d.attr) { d.attr.pos.copy(A.c); d.attr.p[0] = 9 * oA; } |
| +добавлено | if (d.push) { d.push.pos.copy(B.c); d.push.dir.copy(B.n); d.push.p[0] = 7 * oB; d.push.p[1] = B.R; d.push.p[3] = 2 * oB; } |
| +добавлено | |
| +добавлено | // collapse pops |
| +добавлено | if (tlA >= 0.9 && !(d.popped & 1)) { d.popped |= 1; pop(A, c, p); fx.flash(0.1); fx.rig.shake(0.3, 14); } |
| +добавлено | if (tlB >= 0.9 && !(d.popped & 2)) { d.popped |= 2; pop(B, c, p); fx.rig.shake(0.25, 14); if (!fx.reduced) fx.rig.punch(1.5, 0.08); } |
| +добавлено | if (c.phase === 'linger') fx.arena.setFocus(rim, 0.3 + 2.2 * smooth(0.8, 1.8, c.t)); |
| +добавлено | }, |
| +добавлено | |
| +добавлено | tick() { |
| +добавлено | const p = S.params; |
| +добавлено | for (const q of portals) { |
| +добавлено | const on = q.R > 0.002; |
| +добавлено | q.surf.visible = q.ring.visible = on; |
| +добавлено | if (!on) { q.su.uC.value.w = 0; continue; } |
| +добавлено | q.su.uC.value.set(q.c.x, q.c.y, q.c.z, q.R); |
| +добавлено | q.su.uN.value.set(q.n.x, q.n.y, q.n.z, q.ox); |
| +добавлено | q.su.uU.value.set(q.u.x, q.u.y, q.u.z, 0); |
| +добавлено | q.su.uLook.value.set(q.clarity, q.ripple, q.gain, q.seed); |
| +добавлено | q.su.uLook2.value.set(0.05 * p.swirl * (0.6 + q.instab), 0.35 * p.swirl, 0, q.instab); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { GROUND_GLSL, COMMON_GLSL, NOISE_GLSL, ARENA, arenaSDF, arenaRaycast } from '../vfx/common.js'; |
| +добавлено | import { REFRACT_GLSL } from '../vfx/Refraction.js'; |
| +добавлено | import { ParticleFluid, foamTexture } from '../vfx/ParticleFluid.js'; |
| +добавлено | import { BEAD_PLACE_GLSL, BEAD_FRAG } from '../vfx/ParticleFluid.glsl.js'; |
| +добавлено | import { SHADE } from './water/tsunami.glsl.js'; |
| +добавлено | |
| +добавлено | // Water (PLAN 3). What the footage of a fire hose, a water cannon, Old Faithful and a blowhole shows |
| +добавлено | // is three different things, so each gets the tool that can actually draw it: |
| +добавлено | // - the coherent stream is a ballistic tube mesh in the refract pass (all tiers, one draw, crisp |
| +добавлено | // MSAA edges): every ring is the water after tau seconds of free flight, so the arc, the hose |
| +добавлено | // whip running down it, the geyser flaring as it slows (continuity) and the tail flying on after |
| +добавлено | // letting go are all the same function. Glass rod at the nozzle (a real cylinder lens: inverted |
| +добавлено | // background, Beer-Lambert over the chord, Ultra Water's Fresnel sky and GGX sun); downstream the |
| +добавлено | // surface roughens, bubbles cloud the core, and it tears into holes along the flow. FLIP cannot do |
| +добавлено | // this part: a 32 m/s stream crosses 6 cells a step, so the grid only scatters it. |
| +добавлено | // - what water does when it hits something is simulated: the shared GPU FLIP fluid (fx.water) takes |
| +добавлено | // over at the target (medium and up): sheets round the dummy, the flood on the floor, the chunks |
| +добавлено | // falling back round the geyser. A small domain, so all its resolution is where the water lands. |
| +добавлено | // - droplets and ligaments are real little lenses (beads, refract pass): stateless ones torn off |
| +добавлено | // the stream and thrown from the hit, plus the fluid's own lone particles. Ballistic with drag. |
| +добавлено | // Charge: the focus orb's plasma dims and is replaced by a wobbling ball of water (a ball lens), |
| +добавлено | // droplets arcing into it out of the pool. Low tier (phones): no FLIP, no extra passes: stream, |
| +добавлено | // beads and rings all draw in the refract pass that already exists, the shared particles do the rest. |
| +добавлено | // Around it: mist and spray only at the torn edges, Cauchy-Poisson ripple rings on the wet floor, |
| +добавлено | // pool ripples, dummy wet status and hits, lens droplets, steam off hot puddles. |
| +добавлено | |
| +добавлено | const G = 9.81; |
| +добавлено | const MAX_RINGS = 8; |
| +добавлено | const MAX_STREAMS = 12; // instances: a live cast draws a ball and a stream (a geyser: a bundle of them) |
| +добавлено | const BEAD_STREAMS = 4; // streams shedding beads at once |
| +добавлено | const POOL = ARENA.pool; |
| +добавлено | const POOL_SRC = new THREE.Vector3(8.4, POOL.water, 3.0); // where the charge lifts water out of the pool |
| +добавлено | const UP = new THREE.Vector3(0, 1, 0); |
| +добавлено | const DOWNWIND = new THREE.Vector3(1, 0, 0.3).normalize(); // main.js's ambient wind direction |
| +добавлено | |
| +добавлено | // --- the stream: everything is the water's own free flight ----------------------------------- |
| +добавлено | // tau = seconds since this water left the nozzle; clock = seconds since release; so this water left |
| +добавлено | // at te = clock - tau, and anything frozen in the water is a function of te alone (it advects). |
| +добавлено | const STREAM_FUNCS = /* glsl */` |
| +добавлено | vec4 gA; // nozzle xyz (ball: centre), seed |
| +добавлено | vec4 gB; // launch velocity xyz (m/s), nozzle radius (m) (ball: radius) |
| +добавлено | vec4 gC; // tail flow time, head flow time, clock (s), whip |
| +добавлено | vec4 gD; // mode (0 stream, 1 ball, -1 geyser column), breakup flow time (s), last flow time: where it lands (s), aeration |
| +добавлено | vec4 gE; // dissolve, neck flare, turbulent spreading, turbulence at the nozzle |
| +добавлено | vec3 gS, gU; |
| +добавлено | float gSp; |
| +добавлено | void streamSetup() { |
| +добавлено | gSp = max(length(gB.xyz), 1e-3); |
| +добавлено | vec3 t0 = gB.xyz / gSp; |
| +добавлено | gS = normalize(cross(t0, abs(t0.y) > 0.9 ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0))); |
| +добавлено | gU = cross(gS, t0); |
| +добавлено | } |
| +добавлено | vec3 streamVel(float tau) { return gB.xyz - vec3(0.0, ${G.toFixed(3)} * tau, 0.0); } |
| +добавлено | // the hose's aim when this water left is carried down the stream: the whip wave runs to the tip |
| +добавлено | vec3 streamAt(float tau, float clock) { |
| +добавлено | float te = max(clock - tau, 0.0); |
| +добавлено | float amp = gC.w * (0.004 + 0.03 * exp(-te / 0.25)); |
| +добавлено | vec3 sway = gS * amp * sin(te * 9.0 + gA.w * 6.28) + gU * 0.5 * amp * cos(te * 6.3 + gA.w * 6.28); |
| +добавлено | return gA.xyz + (gB.xyz + sway * gSp) * tau - vec3(0.0, ${(G / 2).toFixed(3)} * tau * tau, 0.0); |
| +добавлено | } |
| +добавлено | // continuity: slower water is fatter; turbulent streams entrain air and spread with travel |
| +добавлено | float streamR(float tau) { |
| +добавлено | float k = gSp / max(length(streamVel(tau)), 0.1 * gSp); |
| +добавлено | return gB.w * sqrt(k) * (1.0 + gE.z * tau / max(gD.y, 1e-3)); |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | const STREAM_VERT = /* glsl */` |
| +добавлено | ${NOISE_GLSL} |
| +добавлено | in vec4 iA; |
| +добавлено | in vec4 iB; |
| +добавлено | in vec4 iC; |
| +добавлено | in vec4 iD; |
| +добавлено | in vec4 iE; |
| +добавлено | ${STREAM_FUNCS} |
| +добавлено | out vec3 vW; |
| +добавлено | out vec3 vN; |
| +добавлено | out vec3 vL; // local: across the section (m), and along the water (m), frozen in it |
| +добавлено | out vec3 vS; |
| +добавлено | out vec3 vT; |
| +добавлено | out vec4 vInfo; // breakup 0..1, radius (m), seed, aeration |
| +добавлено | out float vBall; |
| +добавлено | out float vCol; |
| +добавлено | vec3 surf(float u, float v, out vec4 info, out vec3 L, out vec3 Sf, out vec3 Tf) { |
| +добавлено | float a = u * 6.2831853, ca = cos(a), sa = sin(a), seed = gA.w; |
| +добавлено | if (gD.x > 0.5) { |
| +добавлено | // a held ball of water: low modes wobbling, as a floating drop does, and it swirls |
| +добавлено | float ph = v * 3.14159265, t = gC.z; |
| +добавлено | vec3 dir = vec3(sin(ph) * ca, -cos(ph), sin(ph) * sa); |
| +добавлено | float w = 1.0 + 0.07 * sin(dot(dir, vec3(2.3, 3.1, 1.7)) * 2.0 + t * 6.3) + 0.04 * sin(dot(dir, vec3(-3.7, 1.3, 2.9)) * 3.0 - t * 9.1) |
| +добавлено | + 0.05 * (noise3(dir * 0.05 + vec3(seed, t * 0.04, 0.0)) - 0.5); |
| +добавлено | float c = cos(t * 2.2), s = sin(t * 2.2); |
| +добавлено | L = vec3(dir.x * c - dir.z * s, dir.y, dir.x * s + dir.z * c) * gB.w * vec3(1.0, 1.0, 6.0); |
| +добавлено | Sf = vec3(1.0, 0.0, 0.0); Tf = vec3(0.0, 0.0, 1.0); |
| +добавлено | info = vec4(gD.w, gB.w, seed, gD.w); |
| +добавлено | return gA.xyz + dir * gB.w * w; |
| +добавлено | } |
| +добавлено | float tail = gC.x, head = gC.y, clock = gC.z; |
| +добавлено | float tau = mix(tail, head, v); |
| +добавлено | vec3 c = streamAt(tau, clock), vel = streamVel(tau); |
| +добавлено | float sp = max(length(vel), 0.5); |
| +добавлено | vec3 T = normalize(vel + vec3(0.0, 1e-3, 0.0)), S = normalize(cross(T, gU)), U = cross(S, T); |
| +добавлено | float b = clamp(tau / gD.y, 0.0, 1.0), r = streamR(tau); |
| +добавлено | // a round nose until it lands, a round tail once the source stops, a neck at the source |
| +добавлено | float cap = 1.0, dh = (head - tau) * sp, dt = (tau - tail) * sp; |
| +добавлено | if (head < gD.z - 1e-3) cap *= sqrt(clamp(dh / (2.0 * r), 0.0, 1.0)); |
| +добавлено | if (tail > 1e-3) cap *= sqrt(clamp(dt / (2.0 * r), 0.0, 1.0)); |
| +добавлено | else cap *= 1.0 + gE.y * exp(-dt / (1.6 * r)); |
| +добавлено | // instabilities frozen in the water, growing with travel: lumps, Rayleigh-Plateau, the core wandering |
| +добавлено | float z = (clock - tau) * gSp; |
| +добавлено | float zc = z * 0.06 + seed * 3.1, zf = z * 0.21 + seed * 1.7; |
| +добавлено | vec3 q = vec3(ca, sa, 0.0); |
| +добавлено | // a geyser column is not round: lobes round it, and fingers drawn out along the flow (Strokkur, |
| +добавлено | // Old Faithful at 240 fps) that stand out further the higher and slower the water gets |
| +добавлено | float col = step(gD.x, -0.5); |
| +добавлено | float n1 = noise3(q * mix(0.14, 1.1, col) + vec3(seed, 0.0, zc)) - 0.5; |
| +добавлено | float n2 = noise3(q * mix(0.36, 2.2, col) + vec3(0.0, seed, zf)) - 0.5; |
| +добавлено | float grow = gE.w + b * b; |
| +добавлено | float varic = sin(z * 6.2831853 / max(10.0 * gB.w, 0.1) + seed * 9.0); |
| +добавлено | float R = r * cap * (1.0 + grow * (1.5 * n1 + 1.2 * n2) + 0.12 * b * varic); |
| +добавлено | // a hose jet's last metres fray the same way before they reach what they hit |
| +добавлено | float fw = max(col, smoothstep(0.5, 1.0, b)); |
| +добавлено | if (fw > 0.0) { |
| +добавлено | float f = clamp((noise3(vec3(q.xy * 3.4, z * 0.13) + vec3(seed * 2.3, 0.4, 0.0)) - 0.5) * 3.2 + 0.5, 0.0, 1.0); |
| +добавлено | R *= 1.0 + fw * (0.12 + 1.5 * b) * (f * f - 0.3); |
| +добавлено | } |
| +добавлено | float w1 = noise3(vec3(seed * 0.7, 0.21, zc * 0.8)) - 0.5, w2 = noise3(vec3(0.63, seed * 0.7, zc * 0.8)) - 0.5; |
| +добавлено | c += (S * w1 + U * w2) * r * 2.4 * b; |
| +добавлено | R = max(R, 0.002); |
| +добавлено | L = vec3(ca * R, sa * R, z); |
| +добавлено | Sf = S; Tf = T; |
| +добавлено | info = vec4(max(b, gE.x), max(r * cap, 0.01), seed, gD.w); |
| +добавлено | return c + (S * ca + U * sa) * R; |
| +добавлено | } |
| +добавлено | void main() { |
| +добавлено | gA = iA; gB = iB; gC = iC; gD = iD; gE = iE; |
| +добавлено | streamSetup(); |
| +добавлено | vBall = max(gD.x, 0.0); |
| +добавлено | vCol = step(gD.x, -0.5); |
| +добавлено | float u = position.x, v = position.y; |
| +добавлено | vec4 i1, i2; vec3 l1, s1, t1; |
| +добавлено | vec3 p = surf(u, v, vInfo, vL, vS, vT); |
| +добавлено | float dv = v < 0.995 ? 0.005 : -0.005; |
| +добавлено | vec3 pu = surf(u + 0.01, v, i1, l1, s1, t1) - p; |
| +добавлено | vec3 pv = (surf(u, v + dv, i2, l1, s1, t1) - p) * sign(dv); |
| +добавлено | vec3 n = cross(pv, pu); |
| +добавлено | vN = dot(n, n) > 1e-14 ? normalize(n) : normalize(p - gA.xyz); |
| +добавлено | vW = p; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(p, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const STREAM_FRAG = /* glsl */` |
| +добавлено | ${SHADE} |
| +добавлено | uniform float uAbsK; // the eye reads a stream by its darkening: thicker than its centimetres |
| +добавлено | in vec3 vW; |
| +добавлено | in vec3 vN; |
| +добавлено | in vec3 vL; |
| +добавлено | in vec3 vS; |
| +добавлено | in vec3 vT; |
| +добавлено | in vec4 vInfo; |
| +добавлено | in float vBall; |
| +добавлено | in float vCol; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | gLightNoShadow = true; |
| +добавлено | float colm = vCol; |
| +добавлено | float b = vInfo.x, r = vInfo.y, seed = vInfo.z, aer = vInfo.w; |
| +добавлено | vec3 vd = normalize(cameraPosition - vW); |
| +добавлено | vec3 n0 = normalize(vN); |
| +добавлено | float NoV0 = clamp(dot(n0, vd), 1e-3, 1.0); |
| +добавлено | |
| +добавлено | // --- tears: holes along the flow, opening with travel, silhouette first --------------------- |
| +добавлено | vec3 so = seed * vec3(0.13, 0.29, 0.37); |
| +добавлено | float rr = max(r, 0.02); |
| +добавлено | // tears in the section's own scale: fingers round it, streaks along it |
| +добавлено | vec2 tx = vL.xy / rr * 1.4; |
| +добавлено | vec2 tz = colm > 0.5 ? vec2(0.09, 0.3) : vec2(0.045, 0.17); |
| +добавлено | float tn = noise3(vec3(tx * 0.7, vL.z * tz.x) + so) * 0.6 + noise3(vec3(tx * 2.6, vL.z * tz.y) + so.zxy) * 0.4; |
| +добавлено | float tear = smoothstep(mix(0.45, 0.3, colm), 1.0, b); |
| +добавлено | float rim = 1.0 - NoV0; |
| +добавлено | float m = (tn - (-0.15 + 0.78 * tear + mix(0.32 * rim * rim * b + 0.5 * rim * smoothstep(0.45, 0.95, b), 0.62 * rim * smoothstep(0.03, 0.3, b), colm))) * 4.0; |
| +добавлено | float cov = clamp(m / max(fwidth(m), 1e-4) + 0.5, 0.0, 1.0); |
| +добавлено | if (cov < 0.02) discard; |
| +добавлено | |
| +добавлено | // --- surface: what high-speed footage of a hose jet shows is striation, not chop: the nozzle's |
| +добавлено | // turbulence stretched into fine grooves along the flow, roughening into lumps with travel ------- |
| +добавлено | float h = noise3(vec3(vL.xy * 5.0, vL.z * 0.035) + so) * mix(0.75, 0.35, b) |
| +добавлено | + noise3(vec3(vL.xy * 12.0, vL.z * 0.3) + so.yzx) * mix(0.25, 0.65, b); |
| +добавлено | // the column's aeration: clear green-blue where it leaves the vent, white churn a metre up |
| +добавлено | // (a hose jet stays a clear core in a frayed white sheath to the end: never a solid white pillow) |
| +добавлено | float aerB = colm > 0.5 ? smoothstep(0.015, 0.13, b) : clamp(aer + b * b * 0.75, 0.0, 1.0); |
| +добавлено | vec3 n = bump(vW, n0, h * mix(0.006, 0.035, b) * (1.0 - 0.8 * vBall) * (1.0 - 0.5 * aer) * (1.0 + 1.5 * colm * aerB)); // a white column glints less than clear glass |
| +добавлено | |
| +добавлено | // --- a real rod lens: in at the surface, out the far side, on to what is behind -------------- |
| +добавлено | vec3 rd = refract(-vd, n, 1.0 / 1.33); |
| +добавлено | float chord = max(-2.0 * r * dot(rd, n0), 0.01); |
| +добавлено | vec3 p2 = vW + rd * chord; |
| +добавлено | vec3 n2 = normalize(n0 + rd * chord / r); |
| +добавлено | vec3 od = refract(rd, -n2, 1.33); |
| +добавлено | if (dot(od, od) < 1e-4) od = reflect(rd, -n2); // total internal reflection |
| +добавлено | vec2 px = 1.0 / uResolution, uv0 = gl_FragCoord.xy * px; |
| +добавлено | float zs = refrViewZ(vW), z0 = linearDepth(textureLod(uSceneDepth, uv0, 0.0).r); |
| +добавлено | vec2 uv = clamp(refrUV(p2 + od * clamp(z0 - zs, 0.1, 4.0)), px, 1.0 - px); |
| +добавлено | float keep = smoothstep(zs - 0.3, zs + 0.3, linearDepth(textureLod(uSceneDepth, uv, 0.0).r)); |
| +добавлено | vec3 refr = textureLod(uSceneColor, mix(uv0, uv, keep), 0.0).rgb; |
| +добавлено | vec3 col = waterSurface(vW, n, vd, refr, chord * uAbsK, 2.0 * r, 1.0 - smoothstep(0.1, 0.5, chord), mix(mix(0.02, 0.12, b), mix(0.04, 0.45, aerB), colm)); |
| +добавлено | |
| +добавлено | // --- the aerated core: white where the ray crosses it, a clear sheath at the silhouette (a geyser |
| +добавлено | // column, a hose jet a few metres out). Three taps through it; parallax makes the bubbles sit inside |
| +добавлено | float th = mix(0.78, 0.3, aerB), d = 0.0; |
| +добавлено | vec3 rl = vec3(dot(rd, vS), dot(rd, cross(vS, vT)), dot(rd, vT)); |
| +добавлено | for (int i = 0; i < 3; i++) { |
| +добавлено | vec3 q = vL + rl * chord * (0.2 + 0.3 * float(i)); |
| +добавлено | d += smoothstep(th, th + 0.2, noise3(vec3(q.xy / rr * 0.35, q.z * 0.12) + so.yxz)); |
| +добавлено | } |
| +добавлено | float core = smoothstep(0.15, 0.6, NoV0); |
| +добавлено | float milk = (1.0 - exp(-d * (0.35 + 2.4 * aerB))) * mix(0.3 + 0.5 * aerB, 1.0, core) * (1.0 - 0.6 * vBall); |
| +добавлено | float wrap = clamp(dot(n0, uSunDir) * 0.55 + 0.45, 0.0, 1.0); |
| +добавлено | float into = pow(clamp(dot(uSunDir, -vd), 0.0, 1.0), 4.0); |
| +добавлено | vec3 wl = uFoamColor * 0.85 * (uSunColor * (0.15 + 0.85 * wrap + 0.6 * into) + skyUp() * 1.3) + spellLightDiffuse(vW, n0) * 0.6; |
| +добавлено | col = mix(col, wl, milk * 0.95); |
| +добавлено | |
| +добавлено | // --- aeration on the skin: foam lace drawn out along the flow -------------------------------- |
| +добавлено | float pat, fh, cover; |
| +добавлено | foamPattern(vec2((vL.x + 0.6 * vL.y) / rr * 1.1 + seed * 5.0, vL.z * 2.0), vec2(0.0), pat, fh); // coarse enough not to glitter |
| +добавлено | // lace over the milk, not a skin hiding it (the milk carries the backlit glow of the churned core) |
| +добавлено | // (a geyser's core is dense froth: billowed and folded, matte, never a glossy tube) |
| +добавлено | col = foamSurface(col, vW, n, vd, mix(aerB * aerB * 0.55 + 0.45 * tear, 1.4 * aerB, colm), pat, fh, cover); |
| +добавлено | fragColor = vec4(col, cov); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- droplets and ligaments torn off the stream, and thrown from where it lands: stateless ----- |
| +добавлено | const BEAD_N_HIGH = 3600; |
| +добавлено | const beadVert = (N) => /* glsl */` |
| +добавлено | ${COMMON_GLSL} |
| +добавлено | uniform vec4 uBA[${BEAD_STREAMS}]; |
| +добавлено | uniform vec4 uBB[${BEAD_STREAMS}]; |
| +добавлено | uniform vec4 uBC[${BEAD_STREAMS}]; |
| +добавлено | uniform vec4 uBD[${BEAD_STREAMS}]; |
| +добавлено | uniform vec4 uBE[${BEAD_STREAMS}]; // keep min, keep max (of the stream's speed), radial max (m/s), size max (m) |
| +добавлено | uniform vec4 uBF[${BEAD_STREAMS}]; // fan axis xyz, fan fraction |
| +добавлено | uniform vec2 uBG[${BEAD_STREAMS}]; // fan speed max (m/s), target plane on (1) or off (0) |
| +добавлено | ${STREAM_FUNCS} |
| +добавлено | ${BEAD_PLACE_GLSL} |
| +добавлено | void main() { |
| +добавлено | gl_Position = vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | vAlpha = 0.0; |
| +добавлено | int w = gl_InstanceID / ${N}; |
| +добавлено | float k = float(gl_InstanceID - w * ${N}); |
| +добавлено | gA = uBA[w]; gB = uBB[w]; gC = uBC[w]; gD = uBD[w]; gE = vec4(0.0); |
| +добавлено | if (gB.w <= 0.0) return; |
| +добавлено | streamSetup(); |
| +добавлено | vec4 K = uBE[w], Fan = uBF[w]; |
| +добавлено | vec4 h = hash41(k * 1.37 + 0.11); |
| +добавлено | bool fan = h.w < Fan.w; |
| +добавлено | float life = fan ? mix(0.3, 1.1, h.y) : mix(0.25, 0.9, h.y); |
| +добавлено | float cyc = gC.z / life + h.x, age = fract(cyc) * life, born = gC.z - age; |
| +добавлено | vec4 r = hash41(k * 0.73 + floor(cyc) * 3.17); |
| +добавлено | float stopped = gC.z - gC.x; // the clock when the source stopped |
| +добавлено | float tEnd = gD.z, tb = fan ? tEnd : tEnd * mix(0.12, 1.0, pow(r.x, 0.55)); |
| +добавлено | float te = born - tb; |
| +добавлено | if (te < 0.0 || te > stopped || gC.y < tb) return; |
| +добавлено | vec3 P, V; |
| +добавлено | float KD; |
| +добавлено | vec3 rnd = randomOnSphere(r.yz); |
| +добавлено | float ligK = 9.0, ligT = 0.07; |
| +добавлено | if (fan && uBG[w].y > 0.5) { |
| +добавлено | // the splash crown (fire-hose slow motion): the jet spreads into a sheet round the hit, thrown |
| +добавлено | // back towards the nozzle as a cone, and the sheet's rim tears into fingers that neck into |
| +добавлено | // droplets. Each 1/12 s of jet makes its own crown, so the fingers flicker and never repeat |
| +добавлено | P = streamAt(tEnd, born); |
| +добавлено | float pulse = floor(born * 12.0); |
| +добавлено | vec3 N = normalize(Fan.xyz), e1 = normalize(cross(N, abs(N.y) > 0.9 ? vec3(1.0, 0.0, 0.0) : vec3(0.0, 1.0, 0.0))), e2 = cross(N, e1); |
| +добавлено | float fi = floor(r.y * 13.0); |
| +добавлено | vec4 fh = hash41(fi * 7.31 + pulse * 1.93 + gA.w * 17.0); |
| +добавлено | bool sheet = r.z < 0.3; // the unbroken sheet near the hit, between the fingers |
| +добавлено | float az = sheet ? r.y * 6.2831853 : (fi + 0.5 + (r.z - 0.65) * 0.18) / 13.0 * 6.2831853 + (fh.x - 0.5) * 0.3 + gA.w * 6.28; |
| +добавлено | float el = mix(0.12, 0.85, fh.y) + (r.x - 0.5) * 0.12; // radians off the sheet, back towards the source |
| +добавлено | vec3 dirC = (e1 * cos(az) + e2 * sin(az)) * cos(el) + N * sin(el); |
| +добавлено | P += dirC * 0.06; |
| +добавлено | // along a finger the beads string out by speed: the tip fastest, the neck still near the sheet |
| +добавлено | float reach = sheet ? mix(0.12, 0.35, r.w) : mix(0.3, 1.0, sqrt(r.w)) * mix(0.55, 1.0, fh.z); |
| +добавлено | V = (dirC + rnd * 0.06) * reach * uBG[w].x; |
| +добавлено | KD = sheet ? 2.5 : 1.0; |
| +добавлено | ligK = sheet ? 5.0 : 22.0; ligT = sheet ? 0.05 : 0.14; |
| +добавлено | } else if (fan) { |
| +добавлено | P = streamAt(tEnd, born); |
| +добавлено | V = normalize(Fan.xyz + rnd * 0.95) * mix(0.15, 1.0, r.w * r.w) * uBG[w].x; |
| +добавлено | KD = 1.0; |
| +добавлено | } else { |
| +добавлено | vec3 T = normalize(streamVel(tb) + vec3(0.0, 1e-3, 0.0)); |
| +добавлено | vec3 side = normalize(rnd - T * dot(rnd, T) + vec3(1e-3, 0.0, 0.0)); |
| +добавлено | P = streamAt(tb, born) + side * streamR(tb) * 0.9; |
| +добавлено | V = streamVel(tb) * mix(K.x, K.y, r.w) + side * K.z * mix(0.15, 1.0, r.x); |
| +добавлено | KD = 0.9 + 2.2 * r.w; |
| +добавлено | } |
| +добавлено | float e = exp(-KD * age), ex = (1.0 - e) / KD; |
| +добавлено | vec3 p = P + V * ex; |
| +добавлено | p.y -= (${G.toFixed(3)} / KD) * (age - ex); |
| +добавлено | if (p.y < 0.0) return; |
| +добавлено | if (uBG[w].y > 0.5) { |
| +добавлено | // nothing flies on through what the stream is hitting |
| +добавлено | vec3 end = streamAt(tEnd, gC.z), Te = normalize(streamVel(tEnd)); |
| +добавлено | if (dot(p - end, Te) > 0.2) return; |
| +добавлено | } |
| +добавлено | vec3 vel = V * e - vec3(0.0, ${G.toFixed(3)} * ex, 0.0); |
| +добавлено | float size = mix(0.0022, K.w, pow(h.z, 3.0)); |
| +добавлено | // just torn off it is a ligament; surface tension pulls it round within a tenth of a second |
| +добавлено | float lig = size * ligK * exp(-age / ligT); |
| +добавлено | placeBead(p, vel, size, smoothstep(0.0, 0.015, age) * (1.0 - smoothstep(0.85, 1.0, age / life)), lig); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- Cauchy-Poisson rings on a puddle: a floor decal that tilts what the floor already shows ---- |
| +добавлено | // Stationary phase of the impulse response with deep-water dispersion: phase = g t^2 / (4 r), so |
| +добавлено | // the rings run outward accelerating, tight inside and wide at the front (Lamb 1932, art. 238). |
| +добавлено | const RING_VERT = /* glsl */` |
| +добавлено | in vec4 iRing; // x, z, age (s; negative = still to come), amplitude (slope) |
| +добавлено | out vec3 vW; |
| +добавлено | out vec4 vRing; |
| +добавлено | void main() { |
| +добавлено | float t = max(iRing.z, 0.0) * 0.62; |
| +добавлено | float R = clamp(${G.toFixed(3)} * t * t / 6.2831853 + 0.3, 0.3, 4.5); |
| +добавлено | vW = vec3(iRing.x + position.x * R, 0.006, iRing.y + position.y * R); |
| +добавлено | vRing = iRing; |
| +добавлено | gl_Position = (iRing.z < 0.0 || iRing.z > 3.6) ? vec4(2.0, 2.0, 2.0, 1.0) : projectionMatrix * viewMatrix * vec4(vW, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const RING_FRAG = /* glsl */` |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | ${COMMON_GLSL} |
| +добавлено | ${GROUND_GLSL} |
| +добавлено | ${REFRACT_GLSL} |
| +добавлено | in vec3 vW; |
| +добавлено | in vec4 vRing; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | vec2 pq = abs(vW.xz - vec2(${POOL.center.x.toFixed(3)}, ${POOL.center.y.toFixed(3)})); |
| +добавлено | if (pq.x < ${(POOL.half.x + 0.05).toFixed(3)} && pq.y < ${(POOL.half.y + 0.05).toFixed(3)}) discard; // the pool has its own wave sim |
| +добавлено | float mask = smoothstep(0.22, 0.6, groundA(vW.xz).z); |
| +добавлено | if (mask <= 0.0) discard; |
| +добавлено | vec2 d = vW.xz - vRing.xy; |
| +добавлено | float r = max(length(d), 0.03); |
| +добавлено | // ponytail: one time scale stands in for mm-deep capillary-gravity dispersion (slower waves) |
| +добавлено | float t = vRing.z * 0.62; |
| +добавлено | float ph = ${G.toFixed(3)} * t * t / (4.0 * r); |
| +добавлено | float k = ph / r; // local wavenumber d(phase)/dr |
| +добавлено | float viewZ = -(viewMatrix * vec4(vW, 1.0)).z; |
| +добавлено | float px = 2.0 * viewZ / (projectionMatrix[1][1] * uResolution.y); |
| +добавлено | float lam = 6.2831853 / max(k, 1e-3); |
| +добавлено | // drop the short inner waves (alias and damp first) and everything ahead of the front |
| +добавлено | float keep = smoothstep(3.0 * px, 8.0 * px, lam) * smoothstep(0.015, 0.05, lam) * smoothstep(0.4, 2.2, ph); |
| +добавлено | float slope = vRing.w * exp(-vRing.z / 1.1) * keep * sin(ph); |
| +добавлено | if (abs(slope) * mask < 0.003) discard; |
| +добавлено | vec2 rd = d / r; |
| +добавлено | vec3 n = normalize(vec3(-rd.x * slope, 1.0, -rd.y * slope)); |
| +добавлено | vec3 v = normalize(cameraPosition - vW); |
| +добавлено | // the tilted film moves what the floor shows (its reflections, the wet sheen), depth-validated |
| +добавлено | vec2 uv0 = gl_FragCoord.xy / uResolution, uv = refrUV(vW + vec3(rd.x, 0.0, rd.y) * slope * 0.35); |
| +добавлено | float z1 = linearDepth(textureLod(uSceneDepth, uv, 0.0).r); |
| +добавлено | uv = mix(uv0, uv, smoothstep(viewZ - 0.3, viewZ + 0.3, z1)); |
| +добавлено | vec3 col = textureLod(uSceneColor, uv, 0.0).rgb; |
| +добавлено | // crests catch the sky and the sun |
| +добавлено | float F = fresnel(max(dot(n, v), 0.0), 0.02); |
| +добавлено | col += max(envReflect(reflect(-v, n), 0.05) - envReflect(reflect(-v, vec3(0.0, 1.0, 0.0)), 0.05), vec3(0.0)) * F * 2.0; |
| +добавлено | float nh = max(dot(n, normalize(uSunDir + v)), 0.0); |
| +добавлено | col += uSunColor * pow(nh, 600.0) * 4.0 * step(0.0, uSunDir.y); |
| +добавлено | fragColor = vec4(col, mask * clamp(abs(slope) * 40.0, 0.0, 1.0)); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- module scratch (no allocation after create) ----------------------------------------------- |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _p = new THREE.Vector3(), _n = new THREE.Vector3(), _v = new THREE.Vector3(); |
| +добавлено | const SPL = { x: 0, z: 0, radius: 1, wet: 0, pattern: 'noise', seed: 0 }; // reused ground splat |
| +добавлено | const clamp01 = (x) => (x < 0 ? 0 : x > 1 ? 1 : x); |
| +добавлено | const sstep = (a, b, x) => { const t = clamp01((x - a) / (b - a)); return t * t * (3 - 2 * t); }; |
| +добавлено | const floorY = (x, z) => (Math.abs(x - POOL.center.x) < POOL.half.x && Math.abs(z - POOL.center.y) < POOL.half.y ? POOL.water : 0); |
| +добавлено | const nearPool = (x, z, m) => Math.abs(x - POOL.center.x) < POOL.half.x + m && Math.abs(z - POOL.center.y) < POOL.half.y + m; |
| +добавлено | const absorb = (x) => 0.048 + 0.104 * -Math.log(Math.max(x, 0.03)); // tint -> 1/m, as Tsunami: the default gives a clear blue |
| +добавлено | // launch velocity that lands in T seconds under gravity and linear drag DRAG (the drop kind's |
| +добавлено | // 0.1 plus the ambient wind's 0.25 coupling): x(T) = v0 (1 - e^-kT) / k - (g / k) (T - (1 - e^-kT) / k) |
| +добавлено | const DRAG = 0.35; |
| +добавлено | function launch(out, from, to, T) { |
| +добавлено | const e = (1 - Math.exp(-DRAG * T)) / DRAG; |
| +добавлено | out.subVectors(to, from).divideScalar(e); |
| +добавлено | out.y += (G / DRAG) * (T - e) / e; |
| +добавлено | return out; |
| +добавлено | } |
| +добавлено | // the same for the stream (gravity only) |
| +добавлено | const lob = (out, from, to, T) => out.subVectors(to, from).divideScalar(T).addScaledVector(UP, 0.5 * G * T); |
| +добавлено | |
| +добавлено | function normalAt(p, out) { |
| +добавлено | const e = 0.02; |
| +добавлено | out.set( |
| +добавлено | arenaSDF(_a.copy(p).setX(p.x + e)) - arenaSDF(_b.copy(p).setX(p.x - e)), |
| +добавлено | arenaSDF(_a.copy(p).setY(p.y + e)) - arenaSDF(_b.copy(p).setY(p.y - e)), |
| +добавлено | arenaSDF(_a.copy(p).setZ(p.z + e)) - arenaSDF(_b.copy(p).setZ(p.z - e)), |
| +добавлено | ); |
| +добавлено | if (out.lengthSq() < 1e-10) out.set(0, 1, 0); |
| +добавлено | return out.normalize(); |
| +добавлено | } |
| +добавлено | function tubeGeometry(nu, nv) { |
| +добавлено | const g = new THREE.InstancedBufferGeometry(), pos = [], idx = []; |
| +добавлено | for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) pos.push(i / nu, j / nv, 0); |
| +добавлено | for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) { |
| +добавлено | const a = j * (nu + 1) + i, c = a + nu + 1; |
| +добавлено | idx.push(a, c + 1, a + 1, a, c, c + 1); |
| +добавлено | } |
| +добавлено | g.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3)); |
| +добавлено | g.setIndex(idx); |
| +добавлено | g.instanceCount = 0; |
| +добавлено | return g; |
| +добавлено | } |
| +добавлено | function instAttr(g, name, n) { |
| +добавлено | const a = new THREE.InstancedBufferAttribute(new Float32Array(n * 4), 4); |
| +добавлено | a.setUsage(THREE.DynamicDrawUsage); |
| +добавлено | g.setAttribute(name, a); |
| +добавлено | return a; |
| +добавлено | } |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'water', name: 'Water', key: '3', hue: '#2fa8ff', |
| +добавлено | shape: 'beam', forms: ['Tidal Lance', 'Geyser'], |
| +добавлено | params: { size: 1, speed: 32, whip: 1, height: 4.5, droplets: 1, mist: 1, splash: 1, wetness: 1, clarity: 1, tint: '#2fa8ff' }, |
| +добавлено | ranges: { size: [0.5, 2, 0.01], speed: [12, 60, 0.5], whip: [0, 2, 0.01], height: [1.5, 8, 0.1], droplets: [0, 3, 0.01], mist: [0, 3, 0.01], splash: [0.3, 2, 0.01], wetness: [0, 2, 0.01], clarity: [0.2, 3, 0.01] }, |
| +добавлено | presets: { |
| +добавлено | tropical: { tint: '#1fe0c8', clarity: 1.6 }, |
| +добавлено | abyssal: { tint: '#1a4dff', clarity: 0.45, mist: 1.4 }, |
| +добавлено | 'violet tide': { tint: '#9a6bff', clarity: 0.9 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 0.6, impact: 0.35, linger: 3, sustain: 1.4 }, |
| +добавлено | // 4 kinds only: the shared kind table is 64 for every element. Beads and motes are drops, steam is |
| +добавлено | // mist. Other water spells (Tsunami) emit these too. |
| +добавлено | kinds: { |
| +добавлено | drop: { style: 'glow', drag: 0.1, gravity: 9.8, restitution: 0.3, friction: 0.5, life: [0.9, 1.6], size: [0.009, 0.006], stretch: 0.005, emissive: 0.45, color: ['#e4f4ff', '#8fc6f0'], alpha: [1, 0.8], fadeIn: 0.02, fadeOut: 0.08, morph: 'water.splash', paints: 'wet', paintAmount: 0.05 }, |
| +добавлено | splash: { style: 'glow', drag: 0.6, gravity: 9.8, restitution: 0.2, friction: 0.6, killOnCollide: true, life: [0.12, 0.32], size: [0.008, 0.004], stretch: 0.004, emissive: 0.7, color: ['#ffffff', '#bfe6ff'], alpha: [0.9, 0], paints: 'wet', paintAmount: 0.04 }, |
| +добавлено | mist: { style: 'media', drag: 2.2, buoyancy: 0.2, curl: 0.5, curlScale: 0.25, life: [1.2, 2.4], size: [0.35, 1.5], sizePow: 0.6, color: ['#e2f2ff', '#cfe4f5'], alpha: [0.075, 0], density: 1.4, erosion: 0.5, fadeIn: 0.12, lightResponse: 2 }, |
| +добавлено | spray: { style: 'media', drag: 1.4, gravity: 5, curl: 0.3, life: [0.45, 0.9], size: [0.14, 0.5], sizePow: 0.7, color: ['#f2faff', '#dcecf8'], alpha: [0.1125, 0], density: 4, erosion: 0.5, fadeIn: 0.05, lightResponse: 2 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles, Q = fx.Q, low = Q.tier === 'low'; |
| +добавлено | // the shared fluid: whoever needs it first builds it (Tsunami may too); a phone pays ~20 passes |
| +добавлено | // for it, so Low draws the stream, beads and particles alone |
| +добавлено | let W = null; |
| +добавлено | if (!low) { |
| +добавлено | fx.water ??= new ParticleFluid(fx); |
| +добавлено | W = fx.water; |
| +добавлено | if (!fx.systems.includes(W)) fx.systems.push(W); |
| +добавлено | } |
| +добавлено | |
| +добавлено | const E = (k, o) => P.emitter(`water.${k}`, o); |
| +добавлено | const drip = E('drop', { shape: 'line', radius: 0.36, spread: 0.15 }); |
| +добавлено | const bead = E('drop', { shape: 'disk', radius: 1.0, spread: 0.1 }); |
| +добавлено | const dropCone = E('drop', { shape: 'cone', radius: 0.12, spread: 0.8 }); |
| +добавлено | const mist = E('mist', { shape: 'sphere', radius: 0.4, spread: 0.5 }); |
| +добавлено | const mistLine = E('mist', { shape: 'line', radius: 0.25, spread: 0.4 }); |
| +добавлено | const spray = E('spray', { shape: 'sphere', radius: 0.2, spread: 1.2 }); |
| +добавлено | const sprayRing = E('spray', { shape: 'ring', radius: 0.4, spread: 0.5 }); |
| +добавлено | const sprayCone = E('spray', { shape: 'cone', radius: 0.15, spread: 0.6, cone: 1.1 }); |
| +добавлено | const sprayLine = E('spray', { shape: 'line', radius: 0.2, spread: 0.8 }); |
| +добавлено | // steam: hot wet ground (heat + wet > 1.25, i.e. heat on a puddle) breathes mist upward |
| +добавлено | P.fieldEmitter('water.mist', { slots: 512, a: [1, 0, 1, 0], threshold: 1.25, rate: 3, up: 1.6, sizeScale: 0.8 }); |
| +добавлено | |
| +добавлено | const tint = new THREE.Color(), lightCol = new THREE.Color(), absorption = new THREE.Color(), scatter = new THREE.Color(); |
| +добавлено | const foamCol = new THREE.Color('#f2f6f7'), foamTint = new THREE.Color(); |
| +добавлено | const WHITE = new THREE.Color(1, 1, 1); |
| +добавлено | const lookU = new THREE.Vector4(1, 0.02, 0.0, 1.0); |
| +добавлено | function optics() { |
| +добавлено | const p = S.params; |
| +добавлено | tint.set(p.tint); |
| +добавлено | lightCol.copy(tint).lerp(WHITE, 0.55); |
| +добавлено | absorption.setRGB(absorb(tint.r), absorb(tint.g), absorb(tint.b)).multiplyScalar(1 / p.clarity); |
| +добавлено | scatter.copy(tint).lerp(WHITE, 0.15).multiplyScalar(0.06); |
| +добавлено | foamTint.copy(tint).multiplyScalar(0.25).addScalar(0.58); |
| +добавлено | } |
| +добавлено | optics(); |
| +добавлено | function look(h) { |
| +добавлено | const p = S.params; |
| +добавлено | h.tint.set(p.tint); h.clarity = p.clarity; h.tintWater(); |
| +добавлено | h.foam = p.droplets; |
| +добавлено | h.paintA.set(0, 0, 0.03 * p.wetness, 0); |
| +добавлено | h.sss = 0.5; // clear water: backlit sheets glint and refract the sun, they don't glow peach |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- stream + ball: one instanced draw in the refract pass --------------------------------- |
| +добавлено | const streamGeo = low ? tubeGeometry(12, 72) : tubeGeometry(30, 200); |
| +добавлено | const iA = instAttr(streamGeo, 'iA', MAX_STREAMS), iB = instAttr(streamGeo, 'iB', MAX_STREAMS), iC = instAttr(streamGeo, 'iC', MAX_STREAMS); |
| +добавлено | const iD = instAttr(streamGeo, 'iD', MAX_STREAMS), iE = instAttr(streamGeo, 'iE', MAX_STREAMS); |
| +добавлено | const streamMesh = new THREE.Mesh(streamGeo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: STREAM_VERT, fragmentShader: STREAM_FRAG, |
| +добавлено | uniforms: { ...fx.U, uAbsorption: { value: absorption }, uScatter: { value: scatter }, uFoamColor: { value: foamCol }, uFoamTint: { value: foamTint }, uLook: { value: lookU }, uAbsK: { value: 3.5 }, uFoamTex: { value: foamTexture(fx.renderer) } }, |
| +добавлено | side: THREE.FrontSide, alphaToCoverage: true, |
| +добавлено | })); |
| +добавлено | streamMesh.frustumCulled = false; |
| +добавлено | streamMesh.visible = false; |
| +добавлено | fx.scenes.refract.add(streamMesh); |
| +добавлено | |
| +добавлено | // --- beads: one instanced draw, per stream a block of instances ---------------------------- |
| +добавлено | const BEAD_N = low ? 800 : BEAD_N_HIGH; |
| +добавлено | const beadGeo = new THREE.InstancedBufferGeometry(); |
| +добавлено | beadGeo.setAttribute('position', new THREE.Float32BufferAttribute([-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0], 3)); |
| +добавлено | beadGeo.setIndex([0, 1, 2, 0, 2, 3]); |
| +добавлено | beadGeo.instanceCount = 0; |
| +добавлено | const v4s = () => Array.from({ length: BEAD_STREAMS }, () => new THREE.Vector4()); |
| +добавлено | const bu = { uBA: { value: v4s() }, uBB: { value: v4s() }, uBC: { value: v4s() }, uBD: { value: v4s() }, uBE: { value: v4s() }, uBF: { value: v4s() }, uBG: { value: Array.from({ length: BEAD_STREAMS }, () => new THREE.Vector2()) } }; |
| +добавлено | const beadMesh = new THREE.Mesh(beadGeo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: beadVert(BEAD_N), fragmentShader: BEAD_FRAG, uniforms: { ...fx.U, ...bu }, |
| +добавлено | transparent: true, depthWrite: false, |
| +добавлено | blending: THREE.CustomBlending, blendEquation: THREE.AddEquation, blendSrc: THREE.SrcAlphaFactor, blendDst: THREE.OneMinusSrcAlphaFactor, |
| +добавлено | blendEquationAlpha: THREE.AddEquation, blendSrcAlpha: THREE.ZeroFactor, blendDstAlpha: THREE.OneFactor, |
| +добавлено | })); |
| +добавлено | beadMesh.frustumCulled = false; |
| +добавлено | beadMesh.visible = false; |
| +добавлено | beadMesh.renderOrder = 3; |
| +добавлено | fx.scenes.refract.add(beadMesh); |
| +добавлено | |
| +добавлено | // puddle rings: one instanced draw, blended over the floor before the other translucents |
| +добавлено | const ringGeo = new THREE.InstancedBufferGeometry(); |
| +добавлено | ringGeo.setAttribute('position', new THREE.Float32BufferAttribute([-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0], 3)); |
| +добавлено | ringGeo.setIndex([0, 2, 1, 0, 3, 2]); |
| +добавлено | ringGeo.instanceCount = 0; |
| +добавлено | const rA = new THREE.InstancedBufferAttribute(new Float32Array(MAX_RINGS * 4), 4); |
| +добавлено | rA.setUsage(THREE.DynamicDrawUsage); |
| +добавлено | ringGeo.setAttribute('iRing', rA); |
| +добавлено | const ringMesh = new THREE.Mesh(ringGeo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: RING_VERT, fragmentShader: RING_FRAG, uniforms: { ...fx.U }, |
| +добавлено | transparent: true, depthWrite: false, side: THREE.DoubleSide, |
| +добавлено | blending: THREE.CustomBlending, blendEquation: THREE.AddEquation, blendSrc: THREE.SrcAlphaFactor, blendDst: THREE.OneMinusSrcAlphaFactor, |
| +добавлено | blendEquationAlpha: THREE.AddEquation, blendSrcAlpha: THREE.ZeroFactor, blendDstAlpha: THREE.OneFactor, |
| +добавлено | })); |
| +добавлено | ringMesh.frustumCulled = false; |
| +добавлено | ringMesh.visible = false; |
| +добавлено | ringMesh.renderOrder = -1; |
| +добавлено | fx.scenes.refract.add(ringMesh); |
| +добавлено | const rings = Array.from({ length: MAX_RINGS }, () => ({ live: false, x: 0, z: 0, age: 0, amp: 0 })); |
| +добавлено | |
| +добавлено | let lensWet = 0, dimmed = false; |
| +добавлено | |
| +добавлено | // per-cast state, indexed by the cast record (cast.js keeps 16) |
| +добавлено | const ST = Array.from({ length: 16 }, () => ({ |
| +добавлено | live: false, h: null, light: null, attract: null, breeze: null, A: new THREE.Vector3(), dir: new THREE.Vector3(), side: new THREE.Vector3(), up: new THREE.Vector3(), |
| +добавлено | hit: new THREE.Vector3(), hitN: new THREE.Vector3(), foot: new THREE.Vector3(), axis: new THREE.Vector3(), Gp: new THREE.Vector3(), |
| +добавлено | V0: new THREE.Vector3(), L: 1, R: 0.24, rj: 0.14, rc: 0.3, floorHit: true, phase: 0, seedF: 0, |
| +добавлено | t0: 0, tImp: 0, T: 0, contact: false, landed: false, erupt: -1, tTop: 1, H: 0, Hmax: 0, wetDummy: -1, speed: 1, |
| +добавлено | splat: 0, rip: 0, poolTick: 0, ball: 0, glow: 1, dome: 0, burst: false, |
| +добавлено | on: false, tail: 0, head: 0, clock: 0, tEnd: 1, tb: 1, aer: 0, r: 0, dissolve: 0, neck: 0, spreadK: 0, turb: 0, whip: 0, |
| +добавлено | keep0: 0.4, keep1: 0.85, radial: 3, bead: 0.012, fan: 0.3, fanSp: 7, plane: 1, |
| +добавлено | })); |
| +добавлено | |
| +добавлено | // --- helpers --------------------------------------------------------------------------- |
| +добавлено | function spawnRing(x, z, amp, delay) { |
| +добавлено | if (nearPool(x, z, 0.1)) return; |
| +добавлено | let k = null; |
| +добавлено | for (const r of rings) if (!r.live) { k = r; break; } |
| +добавлено | if (!k) { k = rings[0]; for (const r of rings) if (r.age > k.age) k = r; } |
| +добавлено | k.live = true; k.x = x; k.z = z; k.age = -delay; k.amp = amp; |
| +добавлено | } |
| +добавлено | function wetSplat(x, z, radius, wet, seed) { |
| +добавлено | SPL.x = x; SPL.z = z; SPL.radius = radius; SPL.wet = wet; SPL.seed = seed % 1000; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | function soak(pos, radius, strength, from) { |
| +добавлено | let best = -1; |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i], dist = Math.hypot(dm.pos.x - pos.x, dm.pos.z - pos.z); |
| +добавлено | if (dist > radius) continue; |
| +добавлено | const w = 1 - dist / radius; |
| +добавлено | dm.status.wet = Math.max(dm.status.wet, Math.min(1, w + 0.5)); |
| +добавлено | if (strength > 0) dm.hit(_n.subVectors(dm.pos, from).setY(0), strength * (0.4 + 0.6 * w)); |
| +добавлено | best = i; |
| +добавлено | } |
| +добавлено | return best; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // where the water lands: the first surface on the line from the focus (dummy, pillar, floor) |
| +добавлено | function aim(c, d) { |
| +добавлено | d.A.copy(c.from); |
| +добавлено | d.dir.subVectors(c.to, c.from); |
| +добавлено | const len = d.dir.length(); |
| +добавлено | d.dir.divideScalar(len); |
| +добавлено | const t = arenaRaycast(c.from, d.dir, len + 2); |
| +добавлено | d.hit.copy(t > 0 && t < len + 1.5 ? _p.copy(c.from).addScaledVector(d.dir, t) : c.to); |
| +добавлено | normalAt(d.hit, d.hitN); |
| +добавлено | d.floorHit = d.hitN.y > 0.7; |
| +добавлено | if (d.floorHit) { d.hitN.set(0, 1, 0); d.hit.y = floorY(d.hit.x, d.hit.z); } |
| +добавлено | // the spray leaves along the surface normal, thrown forward by the jet's momentum |
| +добавлено | _b.copy(d.dir).setY(0); |
| +добавлено | if (_b.lengthSq() > 1e-6) _b.normalize(); |
| +добавлено | d.axis.copy(d.floorHit ? UP : d.hitN).addScaledVector(d.floorHit ? _b : UP, d.floorHit ? 0.55 : 0.35).normalize(); |
| +добавлено | d.foot.copy(d.hit).addScaledVector(d.hitN, d.floorHit ? 0 : 0.35); |
| +добавлено | d.foot.y = floorY(d.foot.x, d.foot.z); |
| +добавлено | } |
| +добавлено | function frame(d, B) { |
| +добавлено | d.L = Math.max(d.A.distanceTo(B), 0.5); |
| +добавлено | d.dir.subVectors(B, d.A).divideScalar(d.L); |
| +добавлено | d.side.crossVectors(d.dir, UP); |
| +добавлено | if (d.side.lengthSq() < 1e-6) d.side.set(1, 0, 0); |
| +добавлено | d.side.normalize(); |
| +добавлено | d.up.crossVectors(d.side, d.dir).normalize(); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the cast's fluid domain stands where the water lands |
| +добавлено | function acquire(c, d) { |
| +добавлено | if (!W) return null; |
| +добавлено | const sz = S.params.size; |
| +добавлено | const w = (c.form === 'Geyser' ? 6 : 5.4) * Math.sqrt(sz); |
| +добавлено | const top = c.form === 'Geyser' ? d.Hmax + 1.4 : Math.max(d.hit.y, 0) + 1.8; |
| +добавлено | _b.subVectors(d.hit, c.from).setY(0); |
| +добавлено | const h = W.acquire({ size: [w, top, w], position: _p.copy(c.form === 'Geyser' ? d.Gp : d.foot).setY(0), yaw: Math.atan2(_b.z, _b.x), margin: 1.5, life: 4.2 }); |
| +добавлено | if (!h) return null; |
| +добавлено | look(h); |
| +добавлено | h.drain = 0.22; |
| +добавлено | // the flood is clear water with lace on it, not a white pillow (the white is the crown's spray) |
| +добавлено | h.aeration = c.form === 'Geyser' ? 0.8 : 0.4; |
| +добавлено | return h; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // what a hit does round the fluid: mist and spray clouds, wet floor, rings, the dummy soaked |
| +добавлено | // and shoved, the camera; s = 1 is the full slug |
| +добавлено | function hitFx(c, d, at, s) { |
| +добавлено | const p = S.params, sz = p.size; |
| +добавлено | spray.pos.copy(at); spray.vel.copy(d.axis).multiplyScalar(2.5); spray.size = sz; spray.burst(14 * s * p.mist); |
| +добавлено | mist.pos.copy(at); mist.vel.copy(d.axis).multiplyScalar(0.8); mist.size = sz; mist.burst(10 * s * p.mist); |
| +добавлено | dropCone.pos.copy(at); dropCone.p.copy(d.axis); dropCone.cone = 1.1; dropCone.radial = 5 * p.splash; dropCone.vel.set(0, 0.5, 0); dropCone.spread = 1; dropCone.burst(180 * s * p.droplets); |
| +добавлено | wetSplat(d.foot.x, d.foot.z, (1.5 + s) * sz, 0.6 * p.wetness, c.seed + c.age * 13); |
| +добавлено | fx.impacts.add('wet', at, 1.6 * sz, 1, 8); |
| +добавлено | spawnRing(d.foot.x, d.foot.z, 0.22 * (0.5 + 0.5 * s), d.floorHit ? 0.05 : 0.35); |
| +добавлено | if (nearPool(d.foot.x, d.foot.z, 0.6)) fx.pool.ripple(d.foot.x, d.foot.z, 0.05 * s * sz, 0.45 * sz); |
| +добавлено | const k = soak(at, 1.6 * sz, 0.7 * s, d.A); |
| +добавлено | if (k >= 0) d.wetDummy = k; |
| +добавлено | if (fx.camera.position.distanceTo(at) < 3 + sz) lensWet = 1; |
| +добавлено | fx.rig.shake(0.12 + 0.2 * s, 12); |
| +добавлено | if (s > 0.9 && !fx.reduced) fx.rig.punch(1.2, 0.08); |
| +добавлено | if (d.light) { d.light.pos.copy(d.foot).setY(d.foot.y + 1.3); d.light.intensity = Math.max(d.light.intensity, 22 * s); d.light.range = 7 * sz; } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- Tidal Lance ------------------------------------------------------------------------- |
| +добавлено | // the fluid takes the water over just before it lands, with the momentum it has left |
| +добавлено | function feed(d, dt) { |
| +добавлено | if (!d.h) return; |
| +добавлено | const p = S.params, v = _v.copy(d.V0).addScaledVector(UP, -G * d.T), sp = v.length(); |
| +добавлено | v.divideScalar(sp); |
| +добавлено | _p.copy(d.hit).addScaledVector(v, -0.45); |
| +добавлено | // a jet on a wall spends its momentum spreading out round the hit, not running on along the floor |
| +добавлено | d.h.jet(_p, v, 0.4 * sp, 1.15 * d.rj, dt, 0.05, 0, 1.3 * p.splash, 3.4 * p.splash); |
| +добавлено | } |
| +добавлено | function updateLance(c, d, dt) { |
| +добавлено | const p = S.params, L = d.light, tw = c.age - d.t0; |
| +добавлено | d.clock = tw; |
| +добавлено | if (c.phase === 'travel') { |
| +добавлено | d.on = true; d.tail = 0; d.head = Math.min(tw, d.T); |
| +добавлено | d.ball = d.R * Math.cbrt(Math.max(1 - 0.8 * tw / S.timing.sustain, 0.05)); |
| +добавлено | if (!d.contact && tw >= d.T) { d.contact = true; hitFx(c, d, d.hit, 0.55); } |
| +добавлено | if (d.contact) { feed(d, dt); sustain(c, d, dt); } |
| +добавлено | // spray only where the stream has started to tear |
| +добавлено | _p.copy(d.A).lerp(d.hit, 0.4); |
| +добавлено | sprayLine.pos.copy(_p); sprayLine.p.copy(d.hit); sprayLine.vel.copy(d.dir).multiplyScalar(6); sprayLine.radial = 0; sprayLine.size = 0.8 * p.size; |
| +добавлено | sprayLine.rate(32 * p.mist * Math.min(tw / d.T, 1), dt); |
| +добавлено | if (L) { L.pos.copy(d.foot).setY(d.foot.y + 1.6); L.intensity = d.contact ? Math.max(L.intensity * Math.exp(-dt / 0.3), 6) : 0; L.range = 6 * p.size; L.cookie[2] = c.age * 0.5; } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | // the source stops: the tail flies the same arc, and lands |
| +добавлено | const td = c.age - d.tImp; |
| +добавлено | d.tail = td; d.head = Math.min(tw, d.T); d.on = td < d.T; |
| +добавлено | d.ball = d.R * Math.cbrt(0.2) * (1 - sstep(0, 0.16, td)); |
| +добавлено | if (d.on) feed(d, dt); |
| +добавлено | if (!d.landed && td >= d.T) { d.landed = true; hitFx(c, d, d.hit, 1); } |
| +добавлено | if (c.phase === 'impact') decayLight(c, d, dt); else linger(c, d, dt); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the stream pounding the target while held: the fluid does the water; this is the air round it |
| +добавлено | function sustain(c, d, dt) { |
| +добавлено | const p = S.params, sz = p.size, hit = d.hit; |
| +добавлено | sprayCone.pos.copy(hit); sprayCone.p.copy(d.axis); sprayCone.radial = 3; sprayCone.size = sz; sprayCone.rate(14 * p.mist, dt); |
| +добавлено | mist.pos.copy(hit); mist.vel.copy(d.axis); mist.size = sz; mist.rate(6 * p.mist, dt); |
| +добавлено | dropCone.pos.copy(hit); dropCone.p.copy(d.axis); dropCone.cone = 1.1; dropCone.radial = 6 * p.splash; dropCone.vel.set(0, 0.8, 0); dropCone.spread = 1; |
| +добавлено | dropCone.rate((low ? 500 : 160) * p.droplets, dt); |
| +добавлено | d.splat -= dt; |
| +добавлено | if (d.splat <= 0) { |
| +добавлено | d.splat = 0.25; |
| +добавлено | wetSplat(d.foot.x, d.foot.z, 1.3 * sz, 0.35 * p.wetness, c.seed + c.age * 101); |
| +добавлено | soak(hit, 1.3 * sz, 0.15, d.A); |
| +добавлено | } |
| +добавлено | d.rip -= dt; |
| +добавлено | if (d.rip <= 0) { d.rip = 0.55; spawnRing(d.foot.x, d.foot.z, 0.12, d.floorHit ? 0 : 0.3); } |
| +добавлено | d.poolTick -= dt; |
| +добавлено | if (d.poolTick <= 0 && nearPool(d.foot.x, d.foot.z, 0.6)) { d.poolTick = 0.08; fx.pool.ripple(d.foot.x + (fx.rand() - 0.5) * 0.3, d.foot.z + (fx.rand() - 0.5) * 0.3, 0.015 * sz, 0.25 * sz); } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- Geyser ------------------------------------------------------------------------------ |
| +добавлено | // Strokkur: the dive heaves the water up out of the ground as a smooth blue dome for a quarter |
| +добавлено | // second, then the column bursts up through it |
| +добавлено | const DOME = 0.26; |
| +добавлено | function eruptStart(c, d) { |
| +добавлено | const p = S.params, sz = p.size, g = d.Gp; |
| +добавлено | wetSplat(g.x, g.z, 1.6 * sz, 0.5 * p.wetness, c.seed + 13); |
| +добавлено | spawnRing(g.x, g.z, 0.18, 0); |
| +добавлено | if (nearPool(g.x, g.z, 0.6)) fx.pool.ripple(g.x, g.z, 0.04 * sz, 0.6 * sz); |
| +добавлено | fx.rig.shake(0.15, 5); |
| +добавлено | if (d.light) d.light.intensity = 10; |
| +добавлено | } |
| +добавлено | function burstStart(c, d) { |
| +добавлено | const p = S.params, sz = p.size, g = d.Gp; |
| +добавлено | // the dome tears open: churned water heaves out round the column's foot |
| +добавлено | d.h?.burst(_a.copy(g).setY(g.y + 0.25 * sz), 0.3 * sz, _v.set(0, 5.5 * Math.sqrt(sz), 0), 0.1 * sz ** 3 * p.splash, 1.2, 2.6, 0.5); |
| +добавлено | spray.pos.copy(g).setY(g.y + 0.3); spray.vel.set(0, 4, 0); spray.size = 1.2 * sz; spray.burst(22 * p.mist); |
| +добавлено | dropCone.pos.copy(g).setY(g.y + 0.1); dropCone.p.set(0, 1, 0); dropCone.cone = 0.9; dropCone.radial = 7 * p.splash; dropCone.vel.set(0, 1, 0); dropCone.spread = 1.5; dropCone.burst(260 * p.droplets); |
| +добавлено | wetSplat(g.x, g.z, 2.5 * sz, 0.7 * p.wetness, c.seed + 17); |
| +добавлено | fx.impacts.add('wet', _a.copy(g).setY(g.y + 1), 2 * sz, 1, 8); |
| +добавлено | if (nearPool(g.x, g.z, 0.6)) fx.pool.ripple(g.x, g.z, 0.08 * sz, 0.6 * sz); |
| +добавлено | spawnRing(g.x, g.z, 0.25, 0); |
| +добавлено | const k = soak(g, 1.4 * sz, 0.9, _b.set(g.x + 0.3, 0, g.z + 0.2)); |
| +добавлено | if (k >= 0) d.wetDummy = k; |
| +добавлено | if (fx.camera.position.distanceTo(g) < 3 + sz) lensWet = 1; |
| +добавлено | fx.rig.shake(0.45, 8); |
| +добавлено | if (!fx.reduced) fx.rig.punch(1.5, 0.1); |
| +добавлено | if (d.light) d.light.intensity = 24; |
| +добавлено | // the breeze over the top carries its mist and spray off downwind |
| +добавлено | d.breeze = fx.forces.acquire(); |
| +добавлено | if (d.breeze) { const f = d.breeze; f.type = 5; f.p[0] = 2.6; f.p[1] = 1.9 * sz; f.p[2] = 8; f.p[3] = 0.9; f.dir.copy(DOWNWIND); f.pos.copy(g).setY(g.y + d.Hmax); } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the column itself: straight up with a small wander, the source surging |
| +добавлено | function columnState(d, te, stop) { |
| +добавлено | const p = S.params, v0 = Math.sqrt(2 * G * d.Hmax); |
| +добавлено | const surge = 1 + 0.04 * Math.sin(te * 7.3 + d.phase) + 0.018 * Math.sin(te * 17 + 2 * d.phase); |
| +добавлено | d.V0.set(0.022 * v0 * Math.sin(te * 1.9 + d.phase), v0 * surge * (1 - 0.35 * Math.exp(-te * 9)), 0.022 * v0 * Math.cos(te * 1.6 + d.phase)); |
| +добавлено | d.tTop = 0.9 * v0 / G; |
| +добавлено | d.tEnd = d.tTop; d.tb = d.tTop * 1.02; |
| +добавлено | d.r = d.rc * (1 + 0.5 * Math.exp(-te * 4)); d.clock = te; d.head = Math.min(te, d.tTop); |
| +добавлено | d.tail = stop; d.on = stop < d.tTop; |
| +добавлено | d.dissolve = sstep(0, 0.75, stop); |
| +добавлено | d.aer = 0.7; d.neck = 0.8; d.spreadK = 0.15; d.turb = 0.22; d.whip = 0; |
| +добавлено | d.keep0 = 0.75; d.keep1 = 1.05; d.radial = 3.6 * p.splash * Math.sqrt(p.size); d.bead = 0.026; d.fan = 0.45; d.fanSp = 4.2 * p.splash; d.plane = 0; |
| +добавлено | d.axis.set(0, 1, 0); |
| +добавлено | d.H = (v0 * d.head - 0.5 * G * d.head * d.head); |
| +добавлено | } |
| +добавлено | // the head breaks into chunks that arc out and fall back round the column, and what falls and |
| +добавлено | // what spills pools at the foot |
| +добавлено | function feedColumn(d, dt) { |
| +добавлено | if (!d.h) return; |
| +добавлено | const p = S.params, sz = p.size, g = d.Gp, fr = 60 * dt; |
| +добавлено | if (d.head > 0.25) { |
| +добавлено | const tau = d.head * (0.72 + 0.28 * fx.rand()), a = fx.rand() * 6.2832, R = 0.8 * d.rc; |
| +добавлено | _p.copy(g).setY(g.y + d.V0.y * tau - 0.5 * G * tau * tau); |
| +добавлено | _p.x += R * Math.cos(a) + d.V0.x * tau; _p.z += R * Math.sin(a) + d.V0.z * tau; |
| +добавлено | _v.set(Math.cos(a), 0, Math.sin(a)).multiplyScalar(0.9 + 1.5 * fx.rand()); |
| +добавлено | _v.y = d.V0.y - G * tau; |
| +добавлено | d.h.burst(_p, 0.1 * sz * Math.sqrt(1 + 2 * tau), _v, 0.016 * fr * sz ** 2, 0.4, 1.0, 0.25); |
| +добавлено | } |
| +добавлено | d.h.burst(_p.copy(g).setY(g.y + 0.1), 0.2 * sz, _v.set(0, 1.8, 0), 0.006 * fr * sz ** 2, 0.5, 3.0, 0.3); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function updateGeyser(c, d, dt) { |
| +добавлено | const p = S.params, sz = p.size, L = d.light, g = d.Gp, tw = c.age - d.t0; |
| +добавлено | if (d.erupt < 0) { |
| +добавлено | // the ball dives into the floor at the target: a short slug on an arc |
| +добавлено | d.on = true; d.clock = tw; d.head = Math.min(tw, d.T); d.tail = Math.max(tw - 0.14, 0); |
| +добавлено | d.ball = d.R * (1 - sstep(0, 0.14, tw)); |
| +добавлено | if (L) { L.pos.lerpVectors(d.A, g, Math.min(tw / d.T, 1)).setY(Math.max(L.pos.y, 0.6)); L.intensity = 6; } |
| +добавлено | if (tw < d.T) return; |
| +добавлено | d.erupt = tw; |
| +добавлено | eruptStart(c, d); |
| +добавлено | } |
| +добавлено | const te = tw - d.erupt - DOME; |
| +добавлено | d.dome = te < 0 ? sstep(0, DOME, te + DOME) : 0; |
| +добавлено | if (te < 0) { |
| +добавлено | d.on = false; d.clock = tw; |
| +добавлено | if (L) { L.pos.copy(g).setY(g.y + 0.8); L.intensity = Math.max(L.intensity * Math.exp(-dt / 0.3), 8); } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (!d.burst) { d.burst = true; burstStart(c, d); } |
| +добавлено | // the source outlives the 'beam' phase a little: a geyser runs on after the cast is let go |
| +добавлено | const stop = c.phase === 'travel' ? 0 : Math.max(c.age - d.tImp - 0.55, 0); |
| +добавлено | columnState(d, te, stop); |
| +добавлено | if (stop === 0) feedColumn(d, dt); |
| +добавлено | if (d.on) { |
| +добавлено | sprayCone.pos.copy(g).setY(g.y + d.H); sprayCone.p.set(0, 1, 0); sprayCone.radial = 3; sprayCone.size = 1.3 * sz; sprayCone.rate(45 * p.mist, dt); |
| +добавлено | } |
| +добавлено | if (stop === 0) { |
| +добавлено | sprayRing.pos.copy(g); sprayRing.radius = 0.5 * sz; sprayRing.vel.set(0, 1.2, 0); sprayRing.radial = 2.2; sprayRing.size = 0.9 * sz; sprayRing.rate(16 * p.mist, dt); |
| +добавлено | // spray shed off the sides, arcing out and falling back; mist off the top, carried downwind |
| +добавлено | sprayLine.pos.copy(g).setY(g.y + 0.3 * d.H); sprayLine.p.copy(g).setY(g.y + 0.95 * d.H); sprayLine.vel.set(0, 1.2, 0); sprayLine.radial = 2.8 * Math.sqrt(sz); sprayLine.size = 0.8 * sz; sprayLine.rate(28 * p.mist, dt); |
| +добавлено | mist.pos.copy(g).setY(g.y + 0.92 * d.H); mist.vel.copy(DOWNWIND).multiplyScalar(1.6).setY(0.5); mist.size = 1.4 * sz; mist.rate(13 * p.mist, dt); |
| +добавлено | if (low) { dropCone.pos.copy(g).setY(g.y + 0.1); dropCone.p.set(0, 1, 0); dropCone.cone = 0.9; dropCone.radial = 4.5 * p.splash; dropCone.vel.set(0, 1, 0); dropCone.spread = 1.2; dropCone.rate(400 * p.droplets, dt); } |
| +добавлено | d.splat -= dt; |
| +добавлено | if (d.splat <= 0) { d.splat = 0.3; wetSplat(g.x, g.z, 2.2 * sz, 0.4 * p.wetness, c.seed + c.age * 101); soak(g, 1.4 * sz, 0.25, _b.set(g.x + 0.3, 0, g.z)); } |
| +добавлено | d.rip -= dt; |
| +добавлено | if (d.rip <= 0) { d.rip = 0.5; spawnRing(g.x, g.z, 0.15, 0); } |
| +добавлено | d.poolTick -= dt; |
| +добавлено | if (d.poolTick <= 0 && nearPool(g.x, g.z, 0.6)) { d.poolTick = 0.1; fx.pool.ripple(g.x, g.z, 0.03 * sz, 0.5 * sz); } |
| +добавлено | } |
| +добавлено | if (d.breeze) { |
| +добавлено | const f = d.breeze; |
| +добавлено | f.pos.copy(g).addScaledVector(DOWNWIND, -0.6).setY(g.y + Math.max(d.H, 1.5) + 0.3); |
| +добавлено | } |
| +добавлено | if (c.phase === 'travel') { |
| +добавлено | if (L) { L.pos.copy(g).setY(g.y + d.H * 0.5); L.intensity = Math.max(L.intensity * Math.exp(-dt / 0.3), 10); L.range = 9 * sz; L.cookie[2] = c.age * 0.5; } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | // the column's top lands back round its foot |
| +добавлено | if (!d.landed && c.age - d.tImp >= d.T + 0.55) { |
| +добавлено | d.landed = true; |
| +добавлено | spray.pos.copy(g).setY(g.y + 0.4); spray.vel.set(0, 2, 0); spray.size = 1.4 * sz; spray.burst(26 * p.mist); |
| +добавлено | wetSplat(g.x, g.z, 3 * sz, 0.6 * p.wetness, c.seed + 23); |
| +добавлено | spawnRing(g.x, g.z, 0.25, 0); |
| +добавлено | if (nearPool(g.x, g.z, 0.6)) fx.pool.ripple(g.x, g.z, 0.08 * sz, 0.7 * sz); |
| +добавлено | fx.rig.shake(0.25, 9); |
| +добавлено | if (fx.camera.position.distanceTo(g) < 3 + sz) lensWet = 1; |
| +добавлено | } |
| +добавлено | if (c.phase === 'impact') decayLight(c, d, dt); else linger(c, d, dt); |
| +добавлено | } |
| +добавлено | |
| +добавлено | function decayLight(c, d, dt) { |
| +добавлено | if (d.light) d.light.intensity *= Math.exp(-dt / 0.4); |
| +добавлено | } |
| +добавлено | function linger(c, d, dt) { |
| +добавлено | const p = S.params, k = 1 - c.t / c.durations.linger, L = d.light; |
| +добавлено | if (L) { L.intensity = Math.max(L.intensity * Math.exp(-dt / 0.4), 2.5 * k); L.cookie[2] = c.age * 0.5; } |
| +добавлено | // the flood soaks away (each particle at its own pace: the sheet thins, it doesn't pop) |
| +добавлено | if (d.h) d.h.drain = 0.22 + 2.6 * sstep(0.35, 0.9, 1 - k); |
| +добавлено | d.glow = sstep(0.35, 1, 1 - k); |
| +добавлено | // a soaked dummy drips; the last of the mist settles |
| +добавлено | if (d.wetDummy >= 0) { |
| +добавлено | const dm = fx.dummies[d.wetDummy].pos; |
| +добавлено | drip.pos.set(dm.x, 1.55, dm.z); drip.p.set(dm.x, 0.5, dm.z); drip.vel.set(0, -0.3, 0); drip.life = 0.8; |
| +добавлено | drip.rate(40 * k * p.droplets, dt); |
| +добавлено | } |
| +добавлено | mist.pos.copy(d.foot).setY(d.foot.y + 0.3); mist.vel.set(0, 0.2, 0); mist.rate(6 * k * p.mist, dt); |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { |
| +добавлено | if (k !== 'tint' && k !== 'clarity' && k !== 'droplets' && k !== 'wetness') return; |
| +добавлено | optics(); |
| +добавлено | for (const d of ST) if (d.h?.active) look(d.h); |
| +добавлено | }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, d = ST[c.index], sz = p.size; |
| +добавлено | if (name === 'charge') { |
| +добавлено | optics(); |
| +добавлено | d.live = true; d.on = false; d.ball = 0; d.glow = 1; |
| +добавлено | d.phase = (c.seed % 628) / 100; |
| +добавлено | d.seedF = (c.seed % 997) / 997; |
| +добавлено | d.contact = d.landed = d.burst = false; d.erupt = -1; d.dome = 0; d.H = 0; d.wetDummy = -1; d.clock = 0; |
| +добавлено | d.splat = d.rip = d.poolTick = 0; |
| +добавлено | d.dissolve = 0; |
| +добавлено | aim(c, d); |
| +добавлено | if (c.form === 'Geyser') { |
| +добавлено | d.Gp.set(c.to.x, floorY(c.to.x, c.to.z), c.to.z); |
| +добавлено | d.foot.copy(d.Gp); d.hit.copy(d.Gp).setY(d.Gp.y + 0.5); d.hitN.set(0, 1, 0); d.axis.set(0, 1, 0); d.floorHit = true; |
| +добавлено | frame(d, d.Gp); |
| +добавлено | } else frame(d, d.hit); |
| +добавлено | d.Hmax = p.height * Math.sqrt(sz); |
| +добавлено | // fast enough that the head always lands within the minimum sustain |
| +добавлено | d.speed = Math.max(p.speed, d.L / (S.timing.sustain * 0.7)); |
| +добавлено | d.R = 0.24 * sz; |
| +добавлено | d.rc = 0.34 * sz; |
| +добавлено | d.rj = 0.13 * sz; |
| +добавлено | d.h = acquire(c, d); |
| +добавлено | d.light = fx.lights.acquire(); |
| +добавлено | if (d.light) { |
| +добавлено | const L = d.light; |
| +добавлено | L.type = 2; L.cookie[0] = 1; L.cookie[1] = 3.2; L.cookie[2] = 0; |
| +добавлено | L.range = 6; L.intensity = 0; L.color.copy(lightCol); L.pos.copy(c.from); |
| +добавлено | } |
| +добавлено | // the pool's droplets die on arrival in the orb |
| +добавлено | d.attract = fx.forces.acquire(); |
| +добавлено | if (d.attract) { const f = d.attract; f.type = 2; f.p[0] = 60; f.p[1] = 0.5; f.p[2] = 0.45; f.p[3] = 3.2; f.pos.copy(c.from); } |
| +добавлено | } else if (name === 'release') { |
| +добавлено | d.attract?.release(); d.attract = null; |
| +добавлено | d.t0 = c.age; |
| +добавлено | d.glow = 0; |
| +добавлено | mist.pos.copy(c.from); mist.vel.copy(d.dir).multiplyScalar(2); mist.size = 0.7 * sz; mist.burst(6 * p.mist); |
| +добавлено | fx.rig.shake(0.1, 10); |
| +добавлено | if (c.form === 'Geyser') { |
| +добавлено | // the orb dives into the floor at the target |
| +добавлено | d.T = Math.min(Math.max(d.L / 22, 0.25), 0.45); |
| +добавлено | lob(d.V0, c.from, _a.copy(d.Gp).setY(d.Gp.y - 0.1), d.T); |
| +добавлено | d.tEnd = d.T; d.tb = 1.6 * d.T; d.aer = 0.02; d.r = d.R * 0.72; d.neck = 0; d.spreadK = 0; d.turb = 0.12; d.whip = 0; |
| +добавлено | d.keep0 = 0.5; d.keep1 = 0.9; d.radial = 2.5; d.bead = 0.012; d.fan = 0; d.fanSp = 0; d.plane = 0; |
| +добавлено | } else { |
| +добавлено | d.T = d.A.distanceTo(d.hit) / d.speed; |
| +добавлено | lob(d.V0, d.A, d.hit, d.T); |
| +добавлено | d.tEnd = d.T; d.tb = 1.04 * d.T; d.aer = 0.1; d.r = d.rj; d.neck = 1.1; d.spreadK = 0.55; d.turb = 0.05; d.whip = p.whip; |
| +добавлено | d.keep0 = 0.35; d.keep1 = 0.85; d.radial = 3.6 * p.splash; d.bead = 0.014; d.fan = 0.5; d.fanSp = 7.5 * p.splash; d.plane = 1; |
| +добавлено | } |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | d.tImp = c.age; |
| +добавлено | d.landed = false; |
| +добавлено | if (c.form === 'Geyser' && d.erupt < 0) { d.erupt = c.age - d.t0 - DOME; d.dome = 0; d.burst = true; eruptStart(c, d); burstStart(c, d); } |
| +добавлено | } else if (name === 'done') { |
| +добавлено | d.live = false; d.on = false; d.ball = 0; d.glow = 1; |
| +добавлено | d.h?.release(); d.h = null; |
| +добавлено | d.light?.release(); d.light = null; |
| +добавлено | d.attract?.release(); d.attract = null; |
| +добавлено | d.breeze?.release(); d.breeze = null; |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, d = ST[c.index], L = d.light; |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge, sz = p.size; |
| +добавлено | // the orb's plasma dies down as water condenses round it |
| +добавлено | d.glow = 1 - sstep(0, 0.55, k); |
| +добавлено | d.ball = d.R * Math.max(sstep(0, 0.9, k), 0.02); |
| +добавлено | d.clock = c.age; |
| +добавлено | // droplets lifted out of the pool fly ballistic arcs that all land in the orb at release |
| +добавлено | if (k < 0.72) { |
| +добавлено | const T = Math.max(c.durations.charge - c.t + 0.04, 0.2); |
| +добавлено | launch(bead.vel, POOL_SRC, c.from, T); |
| +добавлено | bead.pos.copy(POOL_SRC); bead.life = T / 0.9; bead.rate(240 * p.droplets, dt); // the attractor kills them on arrival |
| +добавлено | fx.pool.swirl(POOL_SRC.x, POOL_SRC.z, 0.8 + k); |
| +добавлено | d.poolTick -= dt; |
| +добавлено | if (d.poolTick <= 0) { d.poolTick = 0.06; fx.pool.ripple(POOL_SRC.x + (fx.rand() - 0.5) * 1.6, POOL_SRC.z + (fx.rand() - 0.5) * 1.6, 0.008, 0.18); } |
| +добавлено | } |
| +добавлено | if (L) { L.pos.copy(c.from).setY(c.from.y + 0.5 * sz); L.intensity = 5 * k; L.cookie[2] = c.age * 0.5; } |
| +добавлено | if (c.form === 'Geyser' && k > 0.45) { |
| +добавлено | // the ground at the target starts to bubble |
| +добавлено | const g = d.Gp; |
| +добавлено | d.h?.burst(_p.copy(g).setY(g.y + 0.06), 0.14 * sz, _v.set(0, 1.4, 0), 0.0012 * sz ** 3, 0.5, 0.9, 0.5); |
| +добавлено | sprayRing.pos.copy(g); sprayRing.radius = 0.45 * sz; sprayRing.vel.set(0, 1.4, 0); sprayRing.radial = 0.6; sprayRing.size = 0.7; sprayRing.rate(14 * p.mist, dt); |
| +добавлено | d.splat -= dt; |
| +добавлено | if (d.splat <= 0) { d.splat = 0.2; wetSplat(g.x, g.z, 1.2 * sz, 0.3 * p.wetness, c.seed + c.age * 7); if (nearPool(g.x, g.z, 0.6)) fx.pool.ripple(g.x, g.z, 0.01, 0.3); } |
| +добавлено | } |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (c.form === 'Geyser') updateGeyser(c, d, dt); else updateLance(c, d, dt); |
| +добавлено | }, |
| +добавлено | |
| +добавлено | // once per frame: streams and balls upload their instance data, rings age; lens droplets dry |
| +добавлено | tick(dt) { |
| +добавлено | let n = 0, nb = 0, glow = 1; |
| +добавлено | const A = iA.array, B = iB.array, C = iC.array, D = iD.array, EE = iE.array; |
| +добавлено | for (const d of ST) { |
| +добавлено | if (!d.live) continue; |
| +добавлено | glow = Math.min(glow, d.glow); |
| +добавлено | if (d.ball > 0.004 && n < MAX_STREAMS) { |
| +добавлено | const o = n++ * 4; |
| +добавлено | A[o] = d.A.x; A[o + 1] = d.A.y; A[o + 2] = d.A.z; A[o + 3] = d.seedF; |
| +добавлено | B[o] = 0; B[o + 1] = 1; B[o + 2] = 0; B[o + 3] = d.ball; |
| +добавлено | C[o] = 0; C[o + 1] = 0; C[o + 2] = d.clock; C[o + 3] = 0; |
| +добавлено | D[o] = 1; D[o + 1] = 1; D[o + 2] = 1; D[o + 3] = 0; |
| +добавлено | EE[o] = 0; EE[o + 1] = 0; EE[o + 2] = 0; EE[o + 3] = 0; |
| +добавлено | } |
| +добавлено | if (d.dome > 0.01 && n < MAX_STREAMS) { |
| +добавлено | // the dome: a ball of clear water mostly under the floor, its cap swelling up |
| +добавлено | const o = n++ * 4, sz = S.params.size, R = 1.1 * sz * Math.cbrt(d.dome); |
| +добавлено | A[o] = d.Gp.x; A[o + 1] = d.Gp.y - R * (0.78 - 0.38 * d.dome); A[o + 2] = d.Gp.z; A[o + 3] = d.seedF; |
| +добавлено | B[o] = 0; B[o + 1] = 1; B[o + 2] = 0; B[o + 3] = R; |
| +добавлено | C[o] = 0; C[o + 1] = 0; C[o + 2] = d.clock; C[o + 3] = 0; |
| +добавлено | D[o] = 1; D[o + 1] = 1; D[o + 2] = 1; D[o + 3] = 0.03; |
| +добавлено | EE[o] = 0; EE[o + 1] = 0; EE[o + 2] = 0; EE[o + 3] = 0; |
| +добавлено | } |
| +добавлено | if (d.on && d.head > d.tail && n < MAX_STREAMS) { |
| +добавлено | // a geyser is a bundle of jets that separate as they slow, not one tube |
| +добавлено | const col = d.erupt >= 0, nt = col ? (low ? 3 : 5) : 1; |
| +добавлено | for (let i = 0; i < nt && n < MAX_STREAMS; i++) { |
| +добавлено | const o = n++ * 4; |
| +добавлено | const a = i * 2.399 + d.phase, off = col && i ? 0.55 * d.rc * (0.7 + 0.3 * Math.sin(i * 1.7)) : 0; |
| +добавлено | const sc = col ? (i ? 1 - 0.05 * (i % 3) : 1.02) : 1, tilt = col && i ? 0.055 * d.V0.y : 0; |
| +добавлено | A[o] = (col ? d.Gp.x : d.A.x) + off * Math.cos(a); A[o + 1] = col ? d.Gp.y : d.A.y; A[o + 2] = (col ? d.Gp.z : d.A.z) + off * Math.sin(a); A[o + 3] = (d.seedF + i * 0.137) % 1; |
| +добавлено | B[o] = d.V0.x * sc + tilt * Math.cos(a); B[o + 1] = d.V0.y * sc; B[o + 2] = d.V0.z * sc + tilt * Math.sin(a); B[o + 3] = col ? d.r * (i ? 0.46 + 0.12 * Math.sin(i * 2.3) : 0.72) : d.r; |
| +добавлено | C[o] = d.tail; C[o + 1] = d.head * (col ? 0.92 + 0.08 * Math.sin(i * 3.1 + d.phase) : 1); C[o + 2] = d.clock + i * 0.31; C[o + 3] = d.whip; |
| +добавлено | D[o] = col ? -1 : 0; D[o + 1] = d.tb; D[o + 2] = d.tEnd; D[o + 3] = d.aer; |
| +добавлено | EE[o] = d.dissolve; EE[o + 1] = d.neck; EE[o + 2] = d.spreadK; EE[o + 3] = d.turb; |
| +добавлено | if (i === 0 && nb < BEAD_STREAMS) { |
| +добавлено | const j = nb++; |
| +добавлено | bu.uBA.value[j].set(A[o], A[o + 1], A[o + 2], A[o + 3]); |
| +добавлено | bu.uBB.value[j].set(B[o], B[o + 1], B[o + 2], d.r); |
| +добавлено | bu.uBC.value[j].set(C[o], d.head, d.clock, C[o + 3]); |
| +добавлено | bu.uBD.value[j].set(D[o], D[o + 1], D[o + 2], D[o + 3]); |
| +добавлено | bu.uBE.value[j].set(d.keep0, d.keep1, d.radial, d.bead * Math.sqrt(S.params.size)); |
| +добавлено | bu.uBF.value[j].set(d.axis.x, d.axis.y, d.axis.z, d.fan); |
| +добавлено | bu.uBG.value[j].set(d.fanSp, d.plane); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | streamGeo.instanceCount = n; |
| +добавлено | streamMesh.visible = n > 0; |
| +добавлено | if (n) iA.needsUpdate = iB.needsUpdate = iC.needsUpdate = iD.needsUpdate = iE.needsUpdate = true; |
| +добавлено | const dens = Math.min(S.params.droplets, 1); |
| +добавлено | beadGeo.instanceCount = nb ? Math.round(BEAD_N * (nb - 1 + dens)) : 0; |
| +добавлено | beadMesh.visible = nb > 0 && dens > 0; |
| +добавлено | // the plasma orb gives way to the water |
| +добавлено | if (glow < 1 || dimmed) { |
| +добавлено | fx.arena.setFocus(fx.arena.focus.color, 2.5 * glow); |
| +добавлено | fx.arena.orb.visible = glow > 0.03; |
| +добавлено | dimmed = glow < 1; |
| +добавлено | } |
| +добавлено | let m = 0; |
| +добавлено | const R = rA.array; |
| +добавлено | for (const r of rings) { |
| +добавлено | if (!r.live) continue; |
| +добавлено | r.age += dt; |
| +добавлено | if (r.age > 3.6) { r.live = false; continue; } |
| +добавлено | const o = m * 4; |
| +добавлено | R[o] = r.x; R[o + 1] = r.z; R[o + 2] = r.age; R[o + 3] = r.amp; |
| +добавлено | m++; |
| +добавлено | } |
| +добавлено | ringGeo.instanceCount = m; |
| +добавлено | ringMesh.visible = m > 0; |
| +добавлено | if (m) rA.needsUpdate = true; |
| +добавлено | if (lensWet > 0) { lensWet = Math.max(0, lensWet - dt / 5); fx.post.params.droplets = lensWet; } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import { COMMON_GLSL, NOISE_GLSL, WORLD_GLSL, LIGHTS_GLSL, FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL } from '../../vfx/common.js'; |
| +добавлено | import { REFRACT_GLSL } from '../../vfx/Refraction.js'; |
| +добавлено | |
| +добавлено | // Tsunami meshes (refract pass), one instanced draw each per live wave. |
| +добавлено | // |
| +добавлено | // Wave sheet: u runs along the crest (0..1 across the width), v along the profile from the rear |
| +добавлено | // base, over the crest and the lip, round the lip tip, back under it and down the face to the |
| +добавлено | // toe. The profile is a uniform Catmull-Rom spline through NP control points; every control point |
| +добавлено | // is itself a Catmull-Rom blend across NK key poses (swell, wall, pitching lip, barrel, crash, |
| +добавлено | // bore, spent sheet), so the phase s morphs the shape smoothly. The break peels from the u = 1 |
| +добавлено | // end, the crest line bows back at the ends and drops off there, and low-frequency noise roughens |
| +добавлено | // the silhouette. The sheet is single-sided: its front faces always look at air, so the far side |
| +добавлено | // of the water body never costs a fragment. |
| +добавлено | // |
| +добавлено | // Splash curtain: where the lip lands, a sheet of ballistic parcels is thrown up and forward (the |
| +добавлено | // founding water's crown, unrolled along the crash line). Its columns launch one after another as |
| +добавлено | // the break peels, the rim tears into jets by alpha-clip and the sheet breaks into holes. |
| +добавлено | |
| +добавлено | export const NP = 12, NK = 7; |
| +добавлено | export const NU = 96, NV = 132; |
| +добавлено | export const NUC = 72, NVC = 24; // (10 rows creased the flying sheet into foil-like facets) |
| +добавлено | |
| +добавлено | // per control point: water thickness behind the surface (x H), and base whitewater |
| +добавлено | const TH = [0.02, 0.1, 0.25, 0.4, 0.45, 0.2, 0.03, 0.12, 0.25, 0.45, 0.35, 0.05]; |
| +добавлено | const FO = [0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.1, 0.0, 0.0, 0.1, 0.7]; |
| +добавлено | const arr = (a) => `float[${a.length}](${a.map((x) => x.toFixed(3)).join(', ')})`; |
| +добавлено | |
| +добавлено | const LATERAL = /* glsl */` |
| +добавлено | // crest-line height: a plateau that drops off towards the ends |
| +добавлено | float taper(float u) { float e = abs(2.0 * u - 1.0); return pow(max(1.0 - e * e * e, 0.0), 0.6); } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | const WAVE_FUNCS = /* glsl */` |
| +добавлено | uniform vec2 uPose[${NP * NK}]; |
| +добавлено | uniform vec4 uShape; // peel, bow (x H), lateral spread in the bore, crash time scale |
| +добавлено | ${NOISE_GLSL} |
| +добавлено | ${LATERAL} |
| +добавлено | in vec4 iA; // origin on the floor xyz, seed 0..1 |
| +добавлено | in vec4 iB; // forward x, forward z, height H (m), width W (m) |
| +добавлено | in vec4 iC; // phase s at the centre, travel d (m), dissolve 0..1, age (s) |
| +добавлено | const float TH[${NP}] = ${arr(TH)}; |
| +добавлено | const float FO[${NP}] = ${arr(FO)}; |
| +добавлено | |
| +добавлено | vec2 cr2(vec2 a, vec2 b, vec2 c, vec2 d, float g) { |
| +добавлено | return 0.5 * (2.0 * b + (c - a) * g + (2.0 * a - 5.0 * b + 4.0 * c - d) * g * g + (3.0 * b - a - 3.0 * c + d) * g * g * g); |
| +добавлено | } |
| +добавлено | int gK0, gK1, gK2, gK3; |
| +добавлено | float gF; |
| +добавлено | void poseWeights(float s) { |
| +добавлено | float sc = clamp(s, 0.0, ${(NK - 1).toFixed(1)}); |
| +добавлено | gK1 = min(int(sc), ${NK - 2}); |
| +добавлено | gF = sc - float(gK1); |
| +добавлено | gK0 = max(gK1 - 1, 0); gK2 = gK1 + 1; gK3 = min(gK1 + 2, ${NK - 1}); |
| +добавлено | } |
| +добавлено | vec2 pose(int i) { |
| +добавлено | return cr2(uPose[gK0 * ${NP} + i], uPose[gK1 * ${NP} + i], uPose[gK2 * ${NP} + i], uPose[gK3 * ${NP} + i], gF); |
| +добавлено | } |
| +добавлено | float phaseAt(float u, float s, float seed) { |
| +добавлено | float brk = smoothstep(0.8, 2.0, s) * (1.0 - smoothstep(4.5, 5.5, s)); |
| +добавлено | // three incommensurate waves: irregular along the crest, not a row of equal peaks (tsunami.js mirrors it) |
| +добавлено | float jit = (0.07 * sin(u * 7.3 + seed * 6.2831853) + 0.05 * sin(u * 17.9 + seed * 11.0) + 0.03 * sin(u * 31.1 + seed * 4.0)) * brk; |
| +добавлено | return max(s + uShape.x * (u - 0.5) + jit, 0.0); // the break peels from the u = 1 end |
| +добавлено | } |
| +добавлено | vec3 wavePos(float u, float v, out float th, out float fo, out float su, out float Hu, out float W) { |
| +добавлено | float e = 2.0 * u - 1.0; |
| +добавлено | su = phaseAt(u, iC.x, iA.w); |
| +добавлено | Hu = iB.z * taper(u) * (1.0 + 0.07 * sin(u * 7.0 + iA.w * 6.2831853)); |
| +добавлено | poseWeights(su); |
| +добавлено | float t = clamp(v, 0.0, 1.0) * ${(NP - 1).toFixed(1)}; |
| +добавлено | int i = min(int(t), ${NP - 2}); |
| +добавлено | float g = t - float(i); |
| +добавлено | vec2 xy = cr2(pose(max(i - 1, 0)), pose(i), pose(i + 1), pose(min(i + 2, ${NP - 1})), g); |
| +добавлено | th = mix(TH[i], TH[i + 1], g); |
| +добавлено | fo = mix(FO[i], FO[i + 1], g); |
| +добавлено | W = iB.w * (1.0 + uShape.z * smoothstep(3.0, 6.0, iC.x)); |
| +добавлено | // lumps riding the surface (profile plane, x H): the crest and lip are never a clean line |
| +добавлено | vec3 nq = vec3(u * W * 0.11, t * 0.09 - iC.w * 0.12, iA.w); |
| +добавлено | // (but the face below the barrel is drawn smooth while it stands: lumps there are vertical folds that |
| +добавлено | // each catch the sunset like a draped curtain) |
| +добавлено | vec2 lump = (vec2(noise3(nq), noise3(nq + vec3(0.37, 0.0, 0.53))) - 0.5) * 0.09 * smoothstep(0.3, 1.0, su) * step(xy.y, 2.0) |
| +добавлено | * (1.0 - 0.7 * smoothstep(7.8, 9.3, t) * (1.0 - smoothstep(3.2, 3.8, su))); |
| +добавлено | // once the lip has landed the mass churns: lumpy whitewater, not a smooth hump |
| +добавлено | // (broad and low: metre-scale lumps a metre high read as a range of glass peaks; the churn's |
| +добавлено | // detail is the foam's billows, in the shading) |
| +добавлено | vec3 nc = vec3(u * W * 0.16, t * 0.15 - iC.w * 0.3, iA.w + 0.5); |
| +добавлено | float churnK = smoothstep(3.3, 4.3, su) * (1.0 - smoothstep(5.0, 6.0, su)); |
| +добавлено | // (smooth gradient noise: the Perlin-Worley's cell creases folded the churn into crumpled ridges) |
| +добавлено | lump += curl3(nc).xy * 0.05 * churnK; |
| +добавлено | // and it heaves: metre-scale lumps rising and collapsing at about 1 Hz (time runs along the noise's |
| +добавлено | // third axis, so they boil in place instead of sliding; the curl field's gradient noise is smooth, |
| +добавлено | // where the Perlin-Worley's cell creases fold the mesh into crumpled ridges). (ponytail: ~1.5 m is |
| +добавлено | // the finest the 96 x 132 sheet carries without spiking the silhouette; finer heave needs more rows) |
| +добавлено | lump.y += curl3(vec3(u * W * 0.15, t * 0.2, iA.w * 3.0 + iC.w * 0.3)).x * 0.05 * churnK; |
| +добавлено | xy += lump * smoothstep(-0.02, 0.06, xy.y); |
| +добавлено | vec3 F = vec3(iB.x, 0.0, iB.y), S = vec3(-iB.y, 0.0, iB.x); |
| +добавлено | return iA.xyz + F * (iC.y - uShape.y * iB.z * e * e + xy.x * Hu) + S * (e * 0.5 * W) + vec3(0.0, xy.y * Hu, 0.0); |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | export const WAVE_VERT = WAVE_FUNCS + /* glsl */` |
| +добавлено | out vec3 vW; |
| +добавлено | out vec3 vN; |
| +добавлено | out vec2 vUv; // u, profile parameter t (0..NP-1) |
| +добавлено | out vec4 vInfo; // thickness (m), local phase, local height (m), seed |
| +добавлено | out vec4 vMisc; // base foam, dissolve, width (m), age |
| +добавлено | void main() { |
| +добавлено | float u = position.x, v = position.y; |
| +добавлено | float th, fo, su, Hu, W, a1, a2, a3, a4, a5; |
| +добавлено | vec3 p = wavePos(u, v, th, fo, su, Hu, W); |
| +добавлено | float du = ${(1 / NU).toFixed(6)}, dv = ${(1 / NV).toFixed(6)}; |
| +добавлено | float us = u < 1.0 - 0.5 * du ? du : -du, vs = v < 1.0 - 0.5 * dv ? dv : -dv; |
| +добавлено | vec3 pu = (wavePos(u + us, v, a1, a2, a3, a4, a5) - p) * sign(us); |
| +добавлено | vec3 pv = (wavePos(u, v + vs, a1, a2, a3, a4, a5) - p) * sign(vs); |
| +добавлено | vec3 n = cross(pu, pv); |
| +добавлено | vN = dot(n, n) > 1e-14 ? normalize(n) : vec3(0.0, 1.0, 0.0); |
| +добавлено | vW = p; |
| +добавлено | vUv = vec2(u, v * ${(NP - 1).toFixed(1)}); |
| +добавлено | vInfo = vec4(th * Hu * mix(1.0, 0.35, smoothstep(4.0, 5.5, su)), su, max(Hu, 0.05), iA.w); |
| +добавлено | vMisc = vec4(fo, iC.z, W, iC.w); |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(p, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // Shared water look: Ultra Water's ocean surface model (OceanSurface.js) on a breaking wall. |
| +добавлено | // Beer-Lambert body over the refracted arena, column in-scatter, Atlas-style crest SSS, roughness- |
| +добавлено | // aware Fresnel over the sky cube, GGX sun with geometric specular AA, and whitewater as a density |
| +добавлено | // eroded through a lace pattern into coverage (dense foam solid, thin foam a tinted bubble film). |
| +добавлено | export const SHADE = /* glsl */` |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | uniform vec3 uAbsorption; |
| +добавлено | uniform vec3 uScatter; |
| +добавлено | uniform vec3 uFoamColor; // dense whitewater albedo |
| +добавлено | uniform vec3 uFoamTint; // thin foam: the water showing through the bubbles |
| +добавлено | uniform vec4 uLook; // foam amount, chop (m), turbidity (1/m: bubbles and sand), crest SSS |
| +добавлено | uniform sampler2D uFoamTex; // Ultra Water's baked foam (ParticleFluid.foamTexture) |
| +добавлено | ${COMMON_GLSL} |
| +добавлено | ${NOISE_GLSL} |
| +добавлено | ${WORLD_GLSL} |
| +добавлено | ${LIGHTS_GLSL} |
| +добавлено | ${REFRACT_GLSL} |
| +добавлено | vec3 skyUp() { return envReflect(vec3(0.0, 1.0, 0.0), 1.0); } |
| +добавлено | // bump from a height field h (m) by screen derivatives (Mikkelsen 2010, surface gradient) |
| +добавлено | vec3 bump(vec3 pw, vec3 n, float h) { |
| +добавлено | vec3 dpx = dFdx(pw), dpy = dFdy(pw); |
| +добавлено | vec3 r1 = cross(dpy, n), r2 = cross(n, dpx); |
| +добавлено | float det = dot(dpx, r1); |
| +добавлено | vec3 grad = sign(det) * (dFdx(h) * r1 + dFdy(h) * r2); |
| +добавлено | return normalize(abs(det) * n - grad); |
| +добавлено | } |
| +добавлено | // refr: the arena behind; dep: metres of water on the view ray; hS: crest height driving the SSS |
| +добавлено | // (m); lip: thin breaking lip glow 0..1; rough: base roughness |
| +добавлено | vec3 waterSurface(vec3 pw, vec3 N, vec3 V, vec3 refr, float dep, float hS, float lip, float rough) { |
| +добавлено | float NoV = max(dot(N, V), 1e-4), NoL = dot(N, uSunDir), nl = max(NoL, 0.0); |
| +добавлено | // geometric specular AA (Kaplanyan): normal change across the pixel widens the lobe |
| +добавлено | vec3 dndx = dFdx(N), dndy = dFdy(N); |
| +добавлено | float geoVar = min(0.25 * (dot(dndx, dndx) + dot(dndy, dndy)), 0.18); |
| +добавлено | float alpha = sqrt(rough * rough + geoVar); |
| +добавлено | vec3 T = exp(-(uAbsorption + uLook.z) * dep); |
| +добавлено | vec3 sun = uSunColor; |
| +добавлено | float back = clamp(dot(uSunDir, -V), 0.0, 1.0); |
| +добавлено | // the water column is lit by the whole sky above it (UW: the sky cube's zenith), the sun only as |
| +добавлено | // far as it is up: a deep face stays blue under a low orange sun |
| +добавлено | vec3 inscatter = uScatter * (skyUp() + sun * max(uSunDir.y, 0.0)); |
| +добавлено | float k1 = uLook.w * hS * pow(back, 4.0) * pow(0.5 - 0.5 * NoL, 3.0); |
| +добавлено | float k2 = 0.06 * NoV * NoV; |
| +добавлено | // (light that has scattered through a metre of water is mostly sky: whitened, so a low red sun |
| +добавлено | // gives turquoise, not green or olive) |
| +добавлено | vec3 sunW = mix(sun, vec3(dot(sun, vec3(0.2126, 0.7152, 0.0722))), 0.8); |
| +добавлено | vec3 sss = (k1 + k2) * uScatter * sunW + 0.05 * nl * uScatter * sun; |
| +добавлено | // a thin breaking lip: light through about a metre of water glows turquoise |
| +добавлено | sss += lip * sunW * exp(-uAbsorption * 2.0) * 0.06 * (0.3 + 2.5 * pow(back, 3.0)); |
| +добавлено | vec3 body = refr * T + (inscatter + sss) * (1.0 - T); |
| +добавлено | float F90 = max(1.0 - 0.7 * sqrt(alpha), 0.02); |
| +добавлено | float F = 0.02 + (F90 - 0.02) * pow(1.0 - NoV, 5.0); |
| +добавлено | vec3 refl = envReflect(reflect(-V, N), sqrt(alpha)); |
| +добавлено | // sun: GGX, alpha floored so the sun acts as a small area light |
| +добавлено | float aS = max(alpha, 0.015), aS2 = aS * aS; |
| +добавлено | vec3 H = normalize(uSunDir + V); |
| +добавлено | float NoH = max(dot(N, H), 0.0); |
| +добавлено | float dd = NoH * NoH * (aS2 - 1.0) + 1.0; |
| +добавлено | float D = aS2 / (PI * dd * dd); |
| +добавлено | float vis = 0.5 / (nl * sqrt(NoV * NoV * (1.0 - aS2) + aS2) + NoV * sqrt(nl * nl * (1.0 - aS2) + aS2) + 1e-5); |
| +добавлено | float Fs = 0.02 + 0.98 * pow(1.0 - max(dot(V, H), 0.0), 5.0); |
| +добавлено | vec3 dS, sS; |
| +добавлено | spellLightPBR(pw, N, V, sqrt(alpha), vec3(0.02), vec3(0.0), dS, sS); |
| +добавлено | return mix(body, refl, F) + sun * (PI * D * vis * Fs * nl) + sS; |
| +добавлено | } |
| +добавлено | // whitewater over the water colour: dens 0..1+, pat the lace pattern (high = bubbles gather), |
| +добавлено | // fh billow height 0..1. Returns the colour; cover = how much of the pixel is foam. |
| +добавлено | vec3 foamSurface(vec3 col, vec3 pw, vec3 N, vec3 V, float dens, float pat, float fh, out float cover) { |
| +добавлено | // Up close the texture's Voronoi cells grow to metres on screen and tile dense churn with hard |
| +добавлено | // outlines: there it becomes froth instead, soft billows at three scales (the noise's perlin-worley |
| +добавлено | // fbm, boiling), grey in its folds. Faded in by the pixel footprint, and for dense churn by |
| +добавлено | // distance (resolution-free: a crash mound within 15 m is froth on a phone as on 4K, its cells |
| +добавлено | // being metres across in the frame either way), so a far wave and thin foam keep their lace. |
| +добавлено | float churn = smoothstep(1.0, 1.4, dens) * (1.0 - smoothstep(10.0, 18.0, refrViewZ(pw))); |
| +добавлено | float nearK = max(1.0 - smoothstep(0.004, 0.014, length(fwidth(pw))), churn) * smoothstep(0.6, 1.0, dens); |
| +добавлено | if (nearK > 0.0) { // (coherent: whole near regions take it, far foam skips the three taps; |
| +добавлено | // explicit LOD, so ANGLE's HLSL keeps this a real branch: the noise has no mips anyway) |
| +добавлено | vec3 q = pw + vec3(0.0, -0.4 * uTime, 0.0); |
| +добавлено | float froth = textureLod(uNoise3D, q * 0.33, 0.0).r * 0.3 + textureLod(uNoise3D, q * 1.1 + 0.37, 0.0).r * 0.4 + textureLod(uNoise3D, q * 3.7 + 0.71, 0.0).r * 0.3; |
| +добавлено | froth = clamp((froth - 0.5) * 1.8 + 0.5, 0.0, 1.0); // noise3 sits low-contrast round 0.5 |
| +добавлено | fh = mix(fh, froth, nearK); |
| +добавлено | pat = mix(pat, 0.4 + 0.5 * froth, nearK); |
| +добавлено | } |
| +добавлено | cover = smoothstep(1.05 - dens, 1.3 - dens, pat) * sqrt(clamp(dens, 0.0, 1.0)); |
| +добавлено | if (cover <= 0.0) return col; |
| +добавлено | // billows: whitewater is lumpy, lace lies flat (deeper, the cell borders crease into a quilt; and |
| +добавлено | // no bump from the trilinear froth, whose slope steps at every texel) |
| +добавлено | vec3 fN = bump(pw, N, fh * (0.03 + 0.05 * min(dens, 1.0)) * (1.0 - nearK)); |
| +добавлено | float fl = dot(fN, uSunDir); |
| +добавлено | float ao = mix(mix(0.45, 1.0, smoothstep(0.15, 0.75, fh)), mix(0.55, 1.0, smoothstep(0.15, 0.85, fh)), nearK); |
| +добавлено | float solid = smoothstep(0.2, 0.9, dens * (0.55 + 0.7 * fh)); |
| +добавлено | vec3 alb = mix(uFoamTint, uFoamColor, max(solid, smoothstep(0.3, 0.9, pat) * 0.6)); |
| +добавлено | float back = clamp(dot(uSunDir, -V), 0.0, 1.0); |
| +добавлено | // the side away from a low sun is sky-lit (blue-grey); only thin foam lets the sun through |
| +добавлено | // (dense foam scatters many times: its light follows the surface's form softly, wrapped) |
| +добавлено | float sl = clamp(fl * mix(0.75, 0.6, solid) + mix(0.25, 0.3, solid), 0.0, 1.0); |
| +добавлено | // sky light from the half of the sky the foam faces (blue away from a sunset, gold towards it), |
| +добавлено | // half of it from the whole sky so neighbouring lumps don't read as tilted plates |
| +добавлено | // (a fold hides the low sun more than the sky: it goes blue-grey, not brown) |
| +добавлено | vec3 lit = uSunColor * (0.08 + 0.92 * sl * sl) * ao * ao + mix(envReflect(fN, 1.0), skyUp(), 0.5) * 1.6 * mix(ao, 1.0, 0.4); |
| +добавлено | // the sun through thin foam, and round the silhouette of thick foam (forward scatter: a dense mass |
| +добавлено | // is opaque, so backlit it is the sky's grey-white with a bright rim, not lit through and beige) |
| +добавлено | lit += uSunColor * pow(back, 3.0) * (0.4 * (1.0 - solid) + 0.35 * solid * pow(1.0 - max(dot(N, V), 0.0), 2.0)); |
| +добавлено | // the folds: light that has wandered through the froth into them is grey-white, not the sunset's |
| +добавлено | lit = mix(vec3(dot(lit, vec3(0.2126, 0.7152, 0.0722))), lit, mix(0.4, 1.0, ao)); |
| +добавлено | lit += spellLightDiffuse(pw, fN) * ao; |
| +добавлено | // no specular: froth is a diffuse scatterer (its glints are sub-pixel bubbles) |
| +добавлено | return mix(col, 0.85 * alb * lit, cover * mix(0.8, 1.0, max(solid, nearK))); |
| +добавлено | } |
| +добавлено | // lace (high where bubbles gather on the cell borders) and billow height: Ultra Water's shore foam, |
| +добавлено | // its baked foam texture riding a flow map (p in metres on the surface, vel its drift in m/s) with |
| +добавлено | // two phases crossfaded every half period, which also makes it churn |
| +добавлено | void foamPattern(vec2 p, vec2 vel, out float pat, out float fh) { |
| +добавлено | const float FT = 2.5; |
| +добавлено | float f0 = fract(uTime / FT), f1 = fract(uTime / FT + 0.5), wb = abs(2.0 * f0 - 1.0); |
| +добавлено | vec4 b0 = texture(uFoamTex, (p - vel * (f0 * FT)) * 0.15), b1 = texture(uFoamTex, (p - vel * (f1 * FT)) * 0.15 + 0.43); |
| +добавлено | // a finer octave riding the same flow (lace only: as a bump it shows the 2x2 derivative quads); |
| +добавлено | // it rides phase 0, so it fades out where that phase wraps |
| +добавлено | vec4 b2 = texture(uFoamTex, (p - vel * (f0 * FT)) * 0.55 + 0.21); |
| +добавлено | fh = mix(b0.g, b1.g, wb); |
| +добавлено | pat = mix(b0.r, b1.r, wb) * 0.3 + (b2.r + b2.g - 1.0) * 0.2 * (1.0 - wb) + fh * 0.45 + texture(uFoamTex, p * 0.023 + 0.5).r * 0.25; |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | |
| +добавлено | export const WAVE_FRAG = /* glsl */` |
| +добавлено | ${SHADE} |
| +добавлено | in vec3 vW; |
| +добавлено | in vec3 vN; |
| +добавлено | in vec2 vUv; |
| +добавлено | in vec4 vInfo; |
| +добавлено | in vec4 vMisc; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | gLightNoShadow = true; |
| +добавлено | float u = vUv.x, t = vUv.y, thick = vInfo.x, s = vInfo.y, Hm = vInfo.z, seed = vInfo.w; |
| +добавлено | float fo0 = vMisc.x, diss = vMisc.y, Wm = vMisc.z, age = vMisc.w; |
| +добавлено | // surface coordinates (m): across the crest, and roughly along the profile |
| +добавлено | vec2 sc = vec2((u - 0.5) * Wm, t * Hm * 0.3); |
| +добавлено | |
| +добавлено | // --- coverage: the lip tears into jets as it throws; late on, holes open until it's gone --- |
| +добавлено | float lip = smoothstep(1.2, 2.0, s) * (1.0 - smoothstep(4.0, 4.8, s)); |
| +добавлено | float dTip = abs(t - 6.0); |
| +добавлено | float x = sc.x / 0.3 + 1.6 * noise3(vec3(sc.x * 0.3, seed, 0.5)); // irregular spacing |
| +добавлено | float cx = fract(x) - 0.5, hl = hash12(vec2(floor(x), seed * 31.0)); |
| +добавлено | float len = (0.03 + 0.16 * hl * hl) * lip * (1.0 - smoothstep(3.0, 3.5, s)); // jets while it throws, not once it lands |
| +добавлено | float q = clamp(dTip / max(len, 1e-3), 0.0, 1.0); |
| +добавлено | float fing = dTip < len ? (mix(0.12, 0.5, sqrt(q)) * (0.6 + 0.4 * hl) - abs(cx)) * 3.0 : 1.0; |
| +добавлено | float nb = noise3(vec3(sc * 0.07, seed * 0.37 + age * 0.012)); |
| +добавлено | float thr = mix(-0.2, 1.05, max(smoothstep(4.9, 6.0, s) * 0.55, diss)); |
| +добавлено | float m = min(fing, (nb - thr) * 3.0); |
| +добавлено | float cov = clamp(m / max(fwidth(m), 1e-4) + 0.5, 0.0, 1.0) * smoothstep(0.0, 0.03, min(u, 1.0 - u)); |
| +добавлено | if (cov < 0.02) discard; |
| +добавлено | |
| +добавлено | vec3 vd = normalize(cameraPosition - vW); |
| +добавлено | vec3 n = normalize(vN); |
| +добавлено | if (!gl_FrontFacing) n = -n; |
| +добавлено | // chop: water drawn up the face and over the lip, streaked along the profile. The face below the |
| +добавлено | // barrel (Nazare / Jaws from the side) is dark, smooth glass: there the chop is low and round, not |
| +добавлено | // stretched (stretched ridges each catch the sunset as a vertical ribbon and read as icicles) |
| +добавлено | float faceK = smoothstep(7.0, 8.5, t) * (1.0 - smoothstep(3.2, 3.8, s)); |
| +добавлено | float h = noise3(vec3(sc.x * 0.1, sc.y * mix(0.05, 0.1, faceK) - age * 0.08, seed * 0.21)) * 0.6 |
| +добавлено | + noise3(vec3(sc.x * 0.3, sc.y * mix(0.12, 0.3, faceK) - age * 0.25, seed * 0.21 + 0.5)) * 0.3 |
| +добавлено | + noise3(vec3(sc.x * 1.1, sc.y * mix(0.35, 1.1, faceK) - age * 0.6, seed * 0.21 + 0.25)) * 0.12; |
| +добавлено | // (the noise is trilinear: its slope steps at every texel, which shows as facets up close and under |
| +добавлено | // the lit whitewater of the bore, whose lumps are the foam's billows instead) |
| +добавлено | n = bump(vW, n, h * uLook.y * (1.0 - 0.85 * smoothstep(3.0, 4.0, s)) * (1.0 - 0.7 * faceK) * smoothstep(1.5, 6.0, refrViewZ(vW))); |
| +добавлено | |
| +добавлено | float behind; |
| +добавлено | vec3 refr = sceneRefract(vW, n, vd, 1.33, 0.25 + thick * 0.4, behind); |
| +добавлено | float contact = 1.0 - smoothstep(0.0, 0.35, behind); |
| +добавлено | cov *= smoothstep(0.0, 0.07, behind); |
| +добавлено | // the camera caught in the wash: no metre-wide sheets across the lens (spray and lens drops carry it) |
| +добавлено | cov *= smoothstep(0.4, 1.5, refrViewZ(vW)); |
| +добавлено | if (cov < 0.02) discard; |
| +добавлено | |
| +добавлено | // A wave face is the front of a deep body of water: behind it is more ocean, not the arena, so |
| +добавлено | // wherever the ray would go on into the body the path is long and the colour is the column's own |
| +добавлено | // in-scatter (Nazaré's dark slate-blue face, Ultra Water's deep colour). Only the toe, the rear |
| +добавлено | // edge, the thin backlit lip and the spent sheet show the floor through them. |
| +добавлено | float tipThin = smoothstep(0.35, 0.0, dTip) * lip; |
| +добавлено | float bodyPath = (1.0 - smoothstep(4.3, 5.4, s)) * smoothstep(0.2, 1.4, t) * (1.0 - smoothstep(9.6, 10.8, t)) * (1.0 - tipThin); |
| +добавлено | // (only the thin lip, until it lands; and only as thin as it is: a metre of lip already glows less) |
| +добавлено | float lipGlow = smoothstep(0.8, 0.0, abs(t - 6.0)) * lip * (1.0 - smoothstep(3.3, 3.8, s)) * exp(-1.5 * thick); |
| +добавлено | // light through the thin lip is mostly the sky scattered in it, not the low sun's orange (which the |
| +добавлено | // cyan transmission would turn green) |
| +добавлено | refr = mix(refr, vec3(dot(refr, vec3(0.2126, 0.7152, 0.0722))), 0.6 * tipThin); |
| +добавлено | // no direct light through the body: tens of metres of bubbly water scatter the beam away, so the |
| +добавлено | // sun's disc and glow (HDR, bright enough to survive any Beer-Lambert path) must not shine teal |
| +добавлено | // through the barrel. Backlit, only the thin lip is turquoise; the barrel is the column's dark blue. |
| +добавлено | // (and the sun's disc, seen through the thin lip, is spread into a glow by its rough surface) |
| +добавлено | // Close-range soup (the landed churn within ~15 m): through the tears in it shows dark, |
| +добавлено | // bubble-clouded water, not the floor. |
| +добавлено | float soup = smoothstep(3.4, 4.0, s) * (1.0 - smoothstep(5.2, 6.0, s)) * (1.0 - smoothstep(10.0, 18.0, refrViewZ(vW))); |
| +добавлено | refr *= (1.0 - bodyPath) * (1.0 - 0.85 * soup) * min(1.0, 4.0 / max(dot(refr, vec3(0.2126, 0.7152, 0.0722)), 1e-3)); |
| +добавлено | vec3 col = waterSurface(vW, n, vd, refr, thick * 2.0 + bodyPath * Hm * 3.0 + soup * 3.0, max(vW.y, 0.0) * 0.25 * (1.0 - 0.95 * bodyPath), lipGlow, 0.03 + 0.2 * soup); // (the soup's water is all chop and bubbles: no mirror, no foil) |
| +добавлено | |
| +добавлено | // --- whitewater density --- |
| +добавлено | float brk = smoothstep(0.9, 2.6, s); |
| +добавлено | float bore = smoothstep(3.3, 4.5, s); |
| +добавлено | float crestD = fo0 * mix(0.25, 1.0, brk); |
| +добавлено | // the face: a few faint scuffs of old foam drawn up it (meandering ridges of stretched noise), in |
| +добавлено | // sparse patches and thin enough to stay a grey lace over the dark water: on a Nazare face the |
| +добавлено | // white is the lip, the crest spray and the foot, never the face itself |
| +добавлено | float sx = sc.x + 1.6 * noise3(vec3(sc.x * 0.05, sc.y * 0.02, seed * 0.3)); |
| +добавлено | float l1 = 1.0 - abs(2.0 * noise3(vec3(sx * 0.2, sc.y * 0.012 - age * 0.01, seed * 0.5 + 0.25)) - 1.0); |
| +добавлено | float scuff = smoothstep(0.52, 0.66, noise3(vec3(sc.x * 0.05, sc.y * 0.04, seed * 0.7 + 0.4))); |
| +добавлено | // (the face's, until it falls; and not up close, where the noise texels would show in the thin lines) |
| +добавлено | float face = smoothstep(6.6, 8.2, t) * (1.0 - smoothstep(10.6, 11.0, t)) * (1.0 - smoothstep(2.7, 3.4, s)) * smoothstep(2.0, 6.0, refrViewZ(vW)); |
| +добавлено | float streakD = smoothstep(0.86, 0.97, l1) * scuff * face * 0.45 * mix(0.5, 1.0, brk); |
| +добавлено | // the plunge churns everything ahead of the crest into a white bore, lace behind it |
| +добавлено | float boreD = bore * mix(0.8, 1.7, smoothstep(2.0, 5.0, t)) * mix(1.0, 0.45, smoothstep(4.8, 6.0, s)); |
| +добавлено | // the jets torn off the lip; once it has pitched, the falling lip itself churns white |
| +добавлено | float tipD = max(smoothstep(0.3, 0.0, dTip) * 1.2, smoothstep(0.8, 0.0, dTip) * smoothstep(2.6, 3.3, s) * 1.3) * lip; |
| +добавлено | float dens = (max(max(crestD, boreD), max(tipD, contact * 0.9)) + streakD) * uLook.x; |
| +добавлено | float pat, fh, cover; |
| +добавлено | foamPattern(sc, vec2(0.0, -1.0 - 2.5 * bore), pat, fh); |
| +добавлено | // (under the soup the water is a bubble cloud: dim grey-blue, not the amber of the sky it mirrors) |
| +добавлено | vec3 water = mix(col, 0.25 * uFoamTint * dot(skyUp() + uSunColor * max(uSunDir.y, 0.0), vec3(0.2126, 0.7152, 0.0722)), 0.8 * soup); |
| +добавлено | col = foamSurface(col, vW, n, vd, dens, pat, fh, cover); |
| +добавлено | if (soup > 0.0) { |
| +добавлено | // The soup is never one white mound (GoPro footage from inside Nazare / Teahupo'o whitewater): a |
| +добавлено | // flow-advected coverage mask (the Perlin-Worley noise at two scales, stretched along the run and |
| +добавлено | // drifting with it, the fine one boiling faster) tears it. Dark water shows through the tears, |
| +добавлено | // thin foam is a grey-blue veil over it, only the fresh, thick tops are white; older soup tears |
| +добавлено | // open more. (Coverage and albedo, no bump: the trilinear noise facets as a bump.) |
| +добавлено | vec3 a = vec3(sc.x * 0.3, (sc.y + age * (1.0 + 2.5 * bore)) * 0.09, seed * 0.7 + age * 0.04); |
| +добавлено | float m2 = textureLod(uNoise3D, a * vec3(3.1, 3.1, 2.0) + vec3(0.31, 0.17, age * 0.1), 0.0).r; |
| +добавлено | float m = clamp((textureLod(uNoise3D, a, 0.0).r * 0.6 + m2 * 0.4 - 0.6) * 2.5 + 0.5, 0.0, 1.0); // (the dilated noise sits high and the two octaves average flat) |
| +добавлено | float th0 = mix(0.25, 0.5, smoothstep(3.9, 5.2, s)); |
| +добавлено | // (in the tears, streaks of thin foam still drift: the fine octave) |
| +добавлено | float c = min(0.1 + 0.3 * smoothstep(0.62, 0.8, m2) + 0.25 * smoothstep(th0, th0 + 0.04, m) + 0.45 * smoothstep(th0 + 0.06, th0 + 0.22, m), 1.0); |
| +добавлено | col = mix(water, col, mix(1.0, c, soup)); |
| +добавлено | } |
| +добавлено | fragColor = vec4(col, cov); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const CURTAIN_VERT = /* glsl */` |
| +добавлено | uniform vec4 uShape; // peel, bow (x H), lateral spread, crash time scale (s per phase unit) |
| +добавлено | uniform vec4 uCurt; // lip tip reach at the crash (x H), life (s), -, - |
| +добавлено | ${NOISE_GLSL} |
| +добавлено | ${LATERAL} |
| +добавлено | in vec4 iA; // origin xyz, seed |
| +добавлено | in vec4 iB; // forward x, forward z, height H (m), width W (m) |
| +добавлено | in vec4 iC; // age at the centre (s; negative = still to land), travel at the crash (m), launch speed (m/s), front speed (m/s) |
| +добавлено | out vec3 vW; |
| +добавлено | out vec3 vN; |
| +добавлено | out vec2 vUv; |
| +добавлено | out vec4 vInfo; // age / life (negative = not landed yet), seed, width (m), local height (m) |
| +добавлено | vec3 curtainAt(float u, float v) { |
| +добавлено | float e = 2.0 * u - 1.0; |
| +добавлено | float lag = (0.5 - u) * uShape.x * uShape.w; // the u = 1 end landed first |
| +добавлено | float age = clamp(iC.x - lag, 0.0, uCurt.y); |
| +добавлено | float tau = age * (1.0 - v), a = age - tau; // ejected at tau, flying for a |
| +добавлено | float Hu = iB.z * taper(u); |
| +добавлено | // irregular along the crest (no regular sine: that reads as a row of glass peaks) |
| +добавлено | float wob = 1.0 + 0.05 * sin(u * iB.w * 0.45 + iA.w * 6.2831853) + 0.4 * (noise3(vec3(u * iB.w * 0.12, iA.w * 3.0, 0.4)) - 0.5) |
| +добавлено | + 0.2 * (noise3(vec3(u * iB.w * 0.45, iA.w * 3.0, 0.8)) - 0.5); |
| +добавлено | float sp = 0.85 * iC.z * sqrt(Hu / max(iB.z, 0.1)) * wob * mix(0.45, 1.0, v) * inversesqrt(1.0 + 4.0 * tau); |
| +добавлено | vec3 F = vec3(iB.x, 0.0, iB.y), S = vec3(-iB.y, 0.0, iB.x); |
| +добавлено | vec3 base = iA.xyz + F * (iC.y + iC.w * (lag + tau) - uShape.y * iB.z * e * e + uCurt.x * Hu) + S * (e * 0.5 * iB.w); |
| +добавлено | vec3 dir = normalize(vec3(0.0, 1.0, 0.0) + F * mix(0.75, 0.3, v)); // the rim stands up |
| +добавлено | vec3 p = base + dir * sp * a; |
| +добавлено | p.y -= 4.905 * a * a; |
| +добавлено | // churned, not a clean sheet: lumps that grow with flight (a smooth sheet reads as glass or satin) |
| +добавлено | vec3 ln = vec3(u * iB.w * 0.6, v * 2.0, iA.w * 5.0 + age * 0.4); |
| +добавлено | p += (vec3(noise3(ln), noise3(ln + vec3(0.31, 0.17, 0.0)), noise3(ln + vec3(0.0, 0.53, 0.29))) - 0.5) * (0.15 + 0.6 * a); |
| +добавлено | return p; |
| +добавлено | } |
| +добавлено | void main() { |
| +добавлено | float u = position.x, v = position.y; |
| +добавлено | float age = iC.x - (0.5 - u) * uShape.x * uShape.w; |
| +добавлено | vec3 p = curtainAt(u, v); |
| +добавлено | float dv = v < 0.95 ? 0.05 : -0.05; |
| +добавлено | vec3 n = cross(curtainAt(min(u + ${(1 / NUC).toFixed(5)}, 1.0), v) - p, (curtainAt(u, v + dv) - p) * sign(dv)); |
| +добавлено | vN = dot(n, n) > 1e-14 ? normalize(n) : vec3(0.0, 1.0, 0.0); |
| +добавлено | vW = p; |
| +добавлено | vUv = vec2(u, v); |
| +добавлено | vInfo = vec4(age / uCurt.y, iA.w, iB.w, iB.z * taper(u)); |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * vec4(p, 1.0); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export const CURTAIN_FRAG = /* glsl */` |
| +добавлено | ${SHADE} |
| +добавлено | in vec3 vW; |
| +добавлено | in vec3 vN; |
| +добавлено | in vec2 vUv; |
| +добавлено | in vec4 vInfo; |
| +добавлено | out vec4 fragColor; |
| +добавлено | void main() { |
| +добавлено | gLightNoShadow = true; |
| +добавлено | float u = vUv.x, v = vUv.y, life = vInfo.x, seed = vInfo.y, Wm = vInfo.z, Hu = vInfo.w; |
| +добавлено | if (life <= 0.0 || life >= 1.0) discard; |
| +добавлено | float xm = u * Wm; |
| +добавлено | // the rim rolls up into a ragged lip of short, blunt jets (no spikes: surface tension rounds a |
| +добавлено | // torn sheet within centimetres); holes open through the thinning sheet until it is spray |
| +добавлено | float x = xm / 0.6 + 1.8 * noise3(vec3(xm * 0.2, seed, 0.2)); |
| +добавлено | float cx = fract(x) - 0.5, hl = hash12(vec2(floor(x), seed * 13.0)); |
| +добавлено | float len = (0.03 + 0.1 * hl) * smoothstep(0.0, 0.3, life); |
| +добавлено | float qq = clamp((v - (1.0 - len)) / max(len, 1e-3), 0.0, 1.0); |
| +добавлено | float fing = v < 1.0 - len ? 1.0 : (mix(0.5, 0.25, qq * qq) - abs(cx)) * 2.0; |
| +добавлено | float nh = noise3(vec3(xm * 0.12, v * 0.5 + life * 0.2, seed * 0.17)) * 0.75 + noise3(vec3(xm * 0.5, v * 2.0 - life * 0.5, seed * 0.29)) * 0.25; |
| +добавлено | float thr = mix(0.2, 1.0, pow(life, 0.5)) - 0.25 * (1.0 - v) * (1.0 - life); // torn to spray within a few tenths of a second |
| +добавлено | float m = min(fing, (nh - thr) * 3.0); |
| +добавлено | // (the low crest ends throw spray, not sheet; and so does the sheet within a few metres of the lens: |
| +добавлено | // seen from inside the splash its torn rim reads as flying glass shards, the plume and drops carry it) |
| +добавлено | float cov = clamp(m / max(fwidth(m), 1e-4) + 0.5, 0.0, 1.0) * smoothstep(0.05, 0.25, min(u, 1.0 - u)) * smoothstep(2.5, 7.5, refrViewZ(vW)); |
| +добавлено | if (cov < 0.02) discard; |
| +добавлено | vec3 vd = normalize(cameraPosition - vW); |
| +добавлено | vec3 n = normalize(vN); |
| +добавлено | if (dot(n, vd) < 0.0) n = -n; |
| +добавлено | n = bump(vW, n, noise3(vec3(xm * 0.4, v * 1.5 - life, seed)) * uLook.y * 0.8); |
| +добавлено | float behind; |
| +добавлено | vec3 refr = sceneRefract(vW, n, vd, 1.33, 0.3, behind); |
| +добавлено | vec3 col = waterSurface(vW, n, vd, refr, 0.5, Hu * 0.4 * v, 1.0 - life, 0.05); |
| +добавлено | // churned water: dense white at the torn rim and jets, thinning to lace as the sheet breaks up |
| +добавлено | float edge = 1.0 - smoothstep(0.0, 0.15, nh - thr); |
| +добавлено | float dens = (1.25 + 0.35 * edge + 0.3 * qq + 0.2 * v - 0.3 * life) * min(uLook.x, 1.3); // churned white through, never a glass sheet |
| +добавлено | float pat, fh, cover; |
| +добавлено | foamPattern(vec2(xm, v * 4.0 + seed * 7.0), vec2(0.0, 3.0), pat, fh); |
| +добавлено | col = foamSurface(col, vW, n, vd, dens, pat, fh, cover); |
| +добавлено | fragColor = vec4(col, cov * smoothstep(0.0, 0.05, behind)); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- splash plume (fx pass): stateless spray puffs, as Ultra Water's Spray.js --------------------- |
| +добавлено | // Each instance is a puff thrown from the crash line (or later from the running bore front) and |
| +добавлено | // flown ballistically with air drag, all from the crash uniforms: no CPU work or simulation state, |
| +добавлено | // so this part can move onto a particle-fluid system without touching the rest. Lit by the sun with |
| +добавлено | // a forward-scattering Henyey-Greenstein lobe plus the sky. |
| +добавлено | export const PLUME_N = 900; // puffs per crash |
| +добавлено | const PUFF_GLSL = /* glsl */` |
| +добавлено | out vec2 vUv; |
| +добавлено | out float vAlpha; |
| +добавлено | out vec3 vLight; |
| +добавлено | out float vSeed; |
| +добавлено | out float vViewZ; |
| +добавлено | // camera-facing quad of the given size at p; droplets: strong forward scattering of the sun + sky |
| +добавлено | void puff(vec3 p, float size, float seed) { |
| +добавлено | vec3 right = vec3(viewMatrix[0][0], viewMatrix[1][0], viewMatrix[2][0]); |
| +добавлено | vec3 upv = vec3(viewMatrix[0][1], viewMatrix[1][1], viewMatrix[2][1]); |
| +добавлено | vec4 view = viewMatrix * vec4(p + (right * position.x + upv * position.y) * size, 1.0); |
| +добавлено | gl_Position = projectionMatrix * view; |
| +добавлено | vViewZ = -view.z; |
| +добавлено | vUv = position.xy; |
| +добавлено | vSeed = seed; |
| +добавлено | float mu = dot(normalize(p - cameraPosition), uSunDir); |
| +добавлено | const float g = 0.6; |
| +добавлено | float phase = (1.0 - g * g) / (4.0 * 3.14159265 * pow(1.0 + g * g - 2.0 * g * mu, 1.5)); |
| +добавлено | // a dense droplet cloud scatters many times: bright as a cumulus on its lit side, sky-blue-grey in |
| +добавлено | // its shade, glowing when the sun is behind it (not a dim film of single scattering) |
| +добавлено | vLight = uSunColor * (0.45 + 3.0 * phase) + uAmbient * 1.5; |
| +добавлено | } |
| +добавлено | `; |
| +добавлено | export const PLUME_VERT = FX_VERT_HEAD + /* glsl */` |
| +добавлено | uniform vec4 uShape; // peel, bow (x H), lateral spread, crash time scale (s per phase unit) |
| +добавлено | uniform vec4 uCurt; // lip tip reach (x H), - |
| +добавлено | uniform vec4 uPA[4]; // per crash (as the curtain's instance data): origin xyz, seed |
| +добавлено | uniform vec4 uPB[4]; // forward x, z, height H, width W |
| +добавлено | uniform vec4 uPC[4]; // age at the centre (s), travel at the crash (m), launch speed, front speed |
| +добавлено | uniform vec4 uPlume; // amount, size, -, - |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | ${LATERAL} |
| +добавлено | ${PUFF_GLSL} |
| +добавлено | float h1(float x) { return fract(sin(x * 12.9898 + 4.1414) * 43758.5453); } |
| +добавлено | void main() { |
| +добавлено | vAlpha = 0.0; |
| +добавлено | gl_Position = vec4(2.0, 2.0, 2.0, 1.0); // dead puffs fall outside the clip volume |
| +добавлено | int w = gl_InstanceID / ${PLUME_N}; |
| +добавлено | float k = float(gl_InstanceID - w * ${PLUME_N}); |
| +добавлено | vec4 A = uPA[w], B = uPB[w], C = uPC[w]; |
| +добавлено | float r1 = h1(k * 0.731 + 0.17), r2 = h1(k * 1.173 + 0.53), r3 = h1(k * 0.419 + 0.91), r4 = h1(k * 1.637 + 0.29); |
| +добавлено | bool bore = r4 > 0.62; // the rest: the plunge plume |
| +добавлено | float u = 0.03 + 0.94 * r1, e = 2.0 * u - 1.0; |
| +добавлено | float lag = (0.5 - u) * uShape.x * uShape.w; |
| +добавлено | float te = bore ? 0.15 + 1.1 * r2 : 0.45 * r2 * r2; |
| +добавлено | float age = C.x - lag - te; |
| +добавлено | float life = bore ? 0.7 + 0.8 * r3 : 1.0 + 1.4 * r3; |
| +добавлено | if (age <= 0.0 || age >= life || B.z <= 0.0) return; |
| +добавлено | float Hu = B.z * taper(u), q = sqrt(Hu / B.z); |
| +добавлено | vec3 F = vec3(B.x, 0.0, B.y), S = vec3(-B.y, 0.0, B.x); |
| +добавлено | vec3 base = A.xyz + F * (C.y + C.w * (lag + te) - uShape.y * B.z * e * e + uCurt.x * Hu) + S * (e * 0.5 * B.w); |
| +добавлено | float up = C.z * q * (bore ? 0.15 + 0.3 * r3 : 0.3 + 0.8 * fract(r3 * 7.13)); |
| +добавлено | float fw = (bore ? 1.2 : 0.5) * C.w + up * 0.35 * fract(r2 * 5.3); |
| +добавлено | float sd = (fract(r1 * 31.7) - 0.5) * 2.4; |
| +добавлено | const float KD = 1.2, G = 9.81; |
| +добавлено | float ex = (1.0 - exp(-KD * age)) / KD; |
| +добавлено | vec3 p = base + (F * fw + S * sd) * ex; |
| +добавлено | float size = (bore ? 0.25 + 0.9 * age : 0.25 + 1.3 * age) * (0.5 + 0.9 * fract(r2 * 9.7)) * q * uPlume.y; |
| +добавлено | p.y = max(A.y + (up + G / KD) * ex - G * age / KD, size * 0.4); |
| +добавлено | puff(p, size, r1 + r2); |
| +добавлено | vAlpha = uPlume.x * (bore ? 1.6 : 2.6) * smoothstep(0.0, 0.06, age) * pow(1.0 - age / life, 2.2) |
| +добавлено | * smoothstep(1.5, 6.0, vViewZ); // no giant blobs on the lens |
| +добавлено | }`; |
| +добавлено | export const PLUME_FRAG = FX_FRAG_HEAD + COMMON_GLSL + NOISE_GLSL + FX_OUT_GLSL + /* glsl */` |
| +добавлено | uniform vec3 uFoamColor; |
| +добавлено | in vec2 vUv; |
| +добавлено | in float vAlpha; |
| +добавлено | in vec3 vLight; |
| +добавлено | in float vSeed; |
| +добавлено | in float vViewZ; |
| +добавлено | void main() { |
| +добавлено | float r = length(vUv); |
| +добавлено | if (vAlpha <= 0.0 || r > 1.0) discard; |
| +добавлено | // soft gaussian puff, broken up at low frequency so it reads as spray mist |
| +добавлено | // droplet clusters: two noise scales eat the puff from the edge in, so it never reads as a disc |
| +добавлено | float n = noise3(vec3(vUv * 0.22 + vSeed * 7.0, vSeed * 0.37)) * 0.65 + noise3(vec3(vUv * 0.6 + vSeed * 3.0, vSeed * 0.71)) * 0.35; |
| +добавлено | float a = vAlpha * smoothstep(0.0, 0.35, n - r * r * 0.55) * exp(-1.5 * r * r) * softFade(vViewZ, 0.8); |
| +добавлено | if (a < 0.002) discard; |
| +добавлено | fxOut(vLight * uFoamColor, a, vec3(0.0), vec2(0.0), vViewZ); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // --- crest spray (fx pass): the mane blown back off the crest while it stands up and pitches ------ |
| +добавлено | // One quad per puff (aPuff: u along the crest, emission phase, two randoms), drawn once per live wave |
| +добавлено | // with the wave's own instance data, so each puff rides the same crest the wave mesh draws. Puffs |
| +добавлено | // are emitted cyclically and fly back and up from the crest, slowed by drag. |
| +добавлено | export const MANE_N = 700; // puffs per wave |
| +добавлено | export const MANE_VERT = FX_VERT_HEAD + WAVE_FUNCS + /* glsl */` |
| +добавлено | uniform vec4 uMane; // amount, size, -, - |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | in vec4 aPuff; |
| +добавлено | ${PUFF_GLSL} |
| +добавлено | void main() { |
| +добавлено | vAlpha = 0.0; |
| +добавлено | gl_Position = vec4(2.0, 2.0, 2.0, 1.0); |
| +добавлено | float u = aPuff.x; |
| +добавлено | float s0 = phaseAt(u, iC.x, iA.w); |
| +добавлено | float k = smoothstep(0.35, 1.2, s0) * (1.0 - smoothstep(2.9, 3.6, s0)) * iB.z / max(iB.z, 1e-3); |
| +добавлено | if (k <= 0.0 || iB.z < 0.5) return; |
| +добавлено | float life = 0.8 + 0.9 * aPuff.z; |
| +добавлено | float age = fract(iC.w / life + aPuff.y) * life; |
| +добавлено | float th, fo, su, Hu, W; |
| +добавлено | vec3 c = wavePos(u, ${(4.6 / (NP - 1)).toFixed(4)} + 0.04 * aPuff.w, th, fo, su, Hu, W); |
| +добавлено | vec3 F = vec3(iB.x, 0.0, iB.y), S = vec3(-iB.y, 0.0, iB.x); |
| +добавлено | const float KD = 1.6; |
| +добавлено | float ex = (1.0 - exp(-KD * age)) / KD; |
| +добавлено | vec3 p = c + (-F * (1.5 + 2.5 * aPuff.w) + S * (aPuff.z - 0.5) * 1.2 + vec3(0.0, 1.2 + 1.6 * aPuff.z, 0.0)) * ex; |
| +добавлено | p.y -= 1.5 * age * age; |
| +добавлено | float q = sqrt(max(Hu, 0.0) / iB.z); |
| +добавлено | float size = (0.25 + 1.4 * age) * (0.6 + 0.8 * aPuff.w) * q * uMane.y; |
| +добавлено | puff(p, size, aPuff.y * 7.0 + aPuff.z); |
| +добавлено | vAlpha = uMane.x * 1.4 * k * q * smoothstep(0.0, 0.12, age) * pow(1.0 - age / life, 2.0) * smoothstep(2.5, 8.0, vViewZ); |
| +добавлено | }`; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { ARENA, FX_BLENDING } from '../../vfx/common.js'; |
| +добавлено | import { foamTexture } from '../../vfx/ParticleFluid.js'; |
| +добавлено | import { WAVE_VERT, WAVE_FRAG, CURTAIN_VERT, CURTAIN_FRAG, PLUME_VERT, PLUME_FRAG, PLUME_N, MANE_VERT, MANE_N, NP, NK, NU, NV, NUC, NVC } from './tsunami.glsl.js'; |
| +добавлено | |
| +добавлено | // Tsunami (SPELLBOOK, Water hero). The orb drains the pool: the water surface swirls and streams |
| +добавлено | // of droplets arc across the arena into a swell welling up behind the target, which drags loose |
| +добавлено | // particles towards it. On release the swell rears into a wall of water, spindrift tearing off its |
| +добавлено | // crest; the lip pitches forward (torn into jets by alpha-clip, shedding droplets), curls into a |
| +добавлено | // barrel over the target and plunges: a splash-up curtain of spray, drops, foam and mist peels |
| +добавлено | // outward along the crest as each section lands, the camera kicks and the dummies are thrown and |
| +добавлено | // soaked. The wave collapses into a foaming bore that runs on, painting a wet swath, pushing every |
| +добавлено | // loose particle ahead of it, then thins into a laced sheet that dissolves, leaving puddles, drips |
| +добавлено | // and low mist. The sheet is its own refract-pass mesh (tsunami.glsl.js), lit like the founding |
| +добавлено | // water (Fresnel sky, refracted and absorbed scene, tint-derived absorption and scatter), with a |
| +добавлено | // caustic cookie light riding under the barrel. |
| +добавлено | |
| +добавлено | const G = 9.81; |
| +добавлено | const MAX_WAVES = 4; |
| +добавлено | const POOL = ARENA.pool; |
| +добавлено | const UP = new THREE.Vector3(0, 1, 0); |
| +добавлено | |
| +добавлено | // key poses (x forward, y up, in units of the wave height); per pose: rear base, back slope (3), |
| +добавлено | // crest top, outer lip, lip tip, inner lip, barrel roof, face (2), toe |
| +добавлено | const POSES = [ |
| +добавлено | // 0 swell |
| +добавлено | [[-1.4, 0], [-1.0, 0.02], [-0.65, 0.065], [-0.3, 0.115], [0, 0.14], [0.18, 0.137], [0.34, 0.128], [0.48, 0.115], [0.62, 0.095], [0.8, 0.065], [1.05, 0.03], [1.4, 0]], |
| +добавлено | // 1 rising wall: the face stands up and starts to overhang |
| +добавлено | [[-1.15, 0], [-0.85, 0.13], [-0.55, 0.42], [-0.26, 0.76], [-0.04, 0.97], [0.12, 1.0], [0.24, 0.95], [0.27, 0.86], [0.26, 0.7], [0.22, 0.45], [0.3, 0.16], [0.62, 0]], |
| +добавлено | // 2 pitching lip |
| +добавлено | [[-1.15, 0], [-0.85, 0.14], [-0.55, 0.45], [-0.24, 0.82], [0.04, 1.04], [0.4, 1.06], [0.66, 0.86], [0.52, 0.9], [0.3, 0.84], [0.16, 0.55], [0.22, 0.18], [0.55, 0]], |
| +добавлено | // 3 barrel: the lip claws down in front of the face |
| +добавлено | [[-1.1, 0], [-0.8, 0.14], [-0.5, 0.45], [-0.2, 0.8], [0.1, 1.0], [0.58, 0.92], [0.86, 0.36], [0.74, 0.5], [0.5, 0.8], [0.2, 0.6], [0.22, 0.2], [0.5, 0]], |
| +добавлено | // 4 crash: the lip lands, the barrel's air pocket squashes |
| +добавлено | [[-1.15, 0], [-0.85, 0.08], [-0.5, 0.22], [-0.15, 0.38], [0.25, 0.47], [0.8, 0.38], [1.12, 0.03], [1.0, 0.08], [0.75, 0.18], [0.5, 0.18], [0.45, 0.06], [0.6, -0.05]], |
| +добавлено | // 5 bore (the inner surfaces sink under the floor) |
| +добавлено | [[-1.8, 0], [-1.3, 0.025], [-0.8, 0.055], [-0.3, 0.1], [0.3, 0.17], [0.9, 0.2], [1.3, 0.04], [1.18, 0.0], [1.0, -0.04], [0.85, -0.06], [0.75, -0.08], [0.95, -0.1]], |
| +добавлено | // 6 spent sheet |
| +добавлено | [[-2.2, 0], [-1.7, 0.012], [-1.1, 0.022], [-0.5, 0.03], [0.2, 0.04], [0.9, 0.045], [1.45, 0.005], [1.3, -0.02], [1.1, -0.05], [0.9, -0.06], [0.75, -0.08], [0.95, -0.1]], |
| +добавлено | ]; |
| +добавлено | // time since release (s, at speed 1) -> phase, and travel of the anchor (x H) |
| +добавлено | const TK = [-1.0, 0.0, 0.6, 1.1, 1.45, 1.7, 2.35, 6.0]; |
| +добавлено | const SK = [0, 0.3, 1, 2, 3, 4, 5, 6]; |
| +добавлено | const DK = [0, 0.04, 0.3, 0.6, 0.85, 1.0, 1.25, 1.35]; |
| +добавлено | const T_IMPACT = 2.8, T_LINGER = 3.2; |
| +добавлено | const BACK = 1.35; // the wave rises this far (x H) behind the target: the target sits in the barrel |
| +добавлено | const NSEG = 4; // emission walks the crest in segments, one per frame |
| +добавлено | const S_CRASH = 3.55; // the centre of the lip lands at this phase |
| +добавлено | const CURT_LIFE = 1.4; // splash-up curtain life (s) |
| +добавлено | const SPLASH_LIFE = 2.6; // the last plume puff is down by then (s after the centre lands) |
| +добавлено | |
| +добавлено | // --- module scratch (no allocation after create) ----------------------------------------------- |
| +добавлено | const PS = new Float32Array(NK * NP * 2); // poses with the curl applied |
| +добавлено | const PT = { x: 0, y: 0 }; |
| +добавлено | const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _c = new THREE.Vector3(), _v = new THREE.Vector3(); |
| +добавлено | const SPL = { x: 0, z: 0, x2: 0, z2: 0, radius: 1, width: 1, wet: 0, pattern: 'noise', seed: 0 }; |
| +добавлено | const clamp01 = (x) => (x < 0 ? 0 : x > 1 ? 1 : x); |
| +добавлено | const sstep = (a, b, x) => { const t = clamp01((x - a) / (b - a)); return t * t * (3 - 2 * t); }; |
| +добавлено | const cr = (a, b, c, d, g) => 0.5 * (2 * b + (c - a) * g + (2 * a - 5 * b + 4 * c - d) * g * g + (3 * b - a - 3 * c + d) * g * g * g); |
| +добавлено | // absorption (1/m) per channel from the tint: the default tint gives Ultra Water's ocean (0.42, 0.075, 0.048) |
| +добавлено | const absorb = (x) => 0.048 + 0.104 * -Math.log(Math.max(x, 0.03)); |
| +добавлено | const nearPool = (x, z, m) => Math.abs(x - POOL.center.x) < POOL.half.x + m && Math.abs(z - POOL.center.y) < POOL.half.y + m; |
| +добавлено | function lerpKeys(keys, vals, t) { |
| +добавлено | if (t <= keys[0]) return vals[0]; |
| +добавлено | for (let i = 1; i < keys.length; i++) if (t < keys[i]) return vals[i - 1] + (vals[i] - vals[i - 1]) * (t - keys[i - 1]) / (keys[i] - keys[i - 1]); |
| +добавлено | return vals[vals.length - 1]; |
| +добавлено | } |
| +добавлено | // control point i at phase s (the JS mirror of the vertex shader's pose()) |
| +добавлено | function pt(i, s) { |
| +добавлено | const sc = Math.min(Math.max(s, 0), NK - 1); |
| +добавлено | const k = Math.min(Math.floor(sc), NK - 2), f = sc - k; |
| +добавлено | const a = (Math.max(k - 1, 0) * NP + i) * 2, b = (k * NP + i) * 2, c = ((k + 1) * NP + i) * 2, d = (Math.min(k + 2, NK - 1) * NP + i) * 2; |
| +добавлено | PT.x = cr(PS[a], PS[b], PS[c], PS[d], f); |
| +добавлено | PT.y = cr(PS[a + 1], PS[b + 1], PS[c + 1], PS[d + 1], f); |
| +добавлено | return PT; |
| +добавлено | } |
| +добавлено | // crest-line height: a plateau dropping off towards the ends (mirrors the shaders' taper()) |
| +добавлено | const taper = (u) => { const e = Math.abs(2 * u - 1); return Math.pow(Math.max(1 - e * e * e, 0), 0.6); }; |
| +добавлено | // launch velocity landing in T seconds under gravity and the drop kind's drag plus the ambient wind |
| +добавлено | const DRAG = 0.35; |
| +добавлено | function launch(out, from, to, T) { |
| +добавлено | const e = (1 - Math.exp(-DRAG * T)) / DRAG; |
| +добавлено | out.subVectors(to, from).divideScalar(e); |
| +добавлено | out.y += (G / DRAG) * (T - e) / e; |
| +добавлено | return out; |
| +добавлено | } |
| +добавлено | function gridGeometry(nu, nv) { |
| +добавлено | const g = new THREE.InstancedBufferGeometry(), pos = [], idx = []; |
| +добавлено | for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) pos.push(i / nu, j / nv, 0); |
| +добавлено | for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) { |
| +добавлено | const a = j * (nu + 1) + i, c = a + nu + 1; |
| +добавлено | idx.push(a, a + 1, c + 1, a, c + 1, c); |
| +добавлено | } |
| +добавлено | g.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3)); |
| +добавлено | g.setIndex(idx); |
| +добавлено | g.instanceCount = 0; |
| +добавлено | return g; |
| +добавлено | } |
| +добавлено | function instAttr(g, name, n) { |
| +добавлено | const a = new THREE.InstancedBufferAttribute(new Float32Array(n * 4), 4); |
| +добавлено | a.setUsage(THREE.DynamicDrawUsage); |
| +добавлено | g.setAttribute(name, a); |
| +добавлено | return a; |
| +добавлено | } |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'tsunami', name: 'Tsunami', element: 'water', order: 10, hue: '#2fa8ff', |
| +добавлено | shape: 'area', |
| +добавлено | params: { height: 6, width: 13, speed: 1, curl: 1, peel: 1.0, angle: -60, foam: 1, spray: 1, mist: 1, wetness: 1, clarity: 1, tint: '#2fe4ff', foamColor: '#f2f6f7' }, |
| +добавлено | ranges: { height: [2, 8, 0.1], width: [6, 20, 0.1], speed: [0.5, 1.8, 0.01], curl: [0.4, 1.5, 0.01], peel: [0, 2.5, 0.01], angle: [-90, 90, 1], foam: [0, 2, 0.01], spray: [0, 3, 0.01], mist: [0, 3, 0.01], wetness: [0, 2, 0.01], clarity: [0.2, 3, 0.01] }, |
| +добавлено | presets: { |
| +добавлено | 'rogue wave': { height: 7.5, width: 17, speed: 0.85, clarity: 0.7, spray: 1.4, mist: 1.4 }, |
| +добавлено | tropical: { tint: '#1fe0c8', clarity: 1.7, foam: 1.2, height: 4 }, |
| +добавлено | abyssal: { tint: '#1a4dff', clarity: 0.6, foam: 0.7, mist: 1.6, foamColor: '#cfe0f0' }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 1.0, impact: T_IMPACT, linger: T_LINGER }, |
| +добавлено | // reuses the founding water's drop / spray / mist; whitewater, spray and mist are its own meshes |
| +добавлено | kinds: {}, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const drops = P.emitter('water.drop', { shape: 'line', radius: 0.15, spread: 1.2 }); |
| +добавлено | const spray = P.emitter('water.spray', { shape: 'line', radius: 0.3, spread: 0.8 }); |
| +добавлено | const mist = P.emitter('water.mist', { shape: 'line', radius: 0.4, spread: 0.5 }); |
| +добавлено | const beads = P.emitter('water.drop', { shape: 'disk', radius: 1.0, spread: 0.12 }); |
| +добавлено | const welling = P.emitter('water.spray', { shape: 'ring', radius: 1.5, spread: 0.4 }); |
| +добавлено | const drip = P.emitter('water.drop', { shape: 'line', radius: 0.3, spread: 0.15 }); |
| +добавлено | |
| +добавлено | // water colour, as the founding water derives it from the tint |
| +добавлено | const tint = new THREE.Color(), absorption = new THREE.Color(), scatter = new THREE.Color(), lightCol = new THREE.Color(), foamCol = new THREE.Color(), foamTint = new THREE.Color(); |
| +добавлено | const WHITE = new THREE.Color(1, 1, 1); |
| +добавлено | const look = new THREE.Vector4(1, 0.2, 0.3, 1.6), shape = new THREE.Vector4(0.9, 0.35, 0.25, 0.25), curt = new THREE.Vector4(1, CURT_LIFE, 0, 0); |
| +добавлено | const poseU = Array.from({ length: NK * NP }, () => new THREE.Vector2()); |
| +добавлено | function optics() { |
| +добавлено | const p = S.params; |
| +добавлено | tint.set(p.tint); |
| +добавлено | absorption.setRGB(absorb(tint.r), absorb(tint.g), absorb(tint.b)).multiplyScalar(1 / p.clarity); |
| +добавлено | // Ultra Water's deep in-scatter (0, 0.05, 0.08) for the default tint: weighted to the blue, so |
| +добавлено | // a deep face reads slate-blue, not green |
| +добавлено | scatter.setRGB(tint.r * 0.02, tint.g * 0.056, tint.b * 0.085); |
| +добавлено | lightCol.copy(tint).lerp(WHITE, 0.55); |
| +добавлено | foamCol.set(p.foamColor); |
| +добавлено | foamTint.copy(tint).multiplyScalar(0.25).addScalar(0.58); // a bubble film over the water colour |
| +добавлено | look.x = p.foam; |
| +добавлено | look.z = 0.3 / p.clarity; // suspended bubbles and sand: a breaking wave is never glass-clear |
| +добавлено | shape.x = p.peel; |
| +добавлено | shape.w = 0.25 / p.speed; // s per phase unit around the crash |
| +добавлено | } |
| +добавлено | // the curl stretches the lip (points past the crest) away from the crest |
| +добавлено | function poses() { |
| +добавлено | const k = S.params.curl; |
| +добавлено | for (let j = 0; j < NK; j++) { |
| +добавлено | const cx = POSES[j][4][0]; |
| +добавлено | for (let i = 0; i < NP; i++) { |
| +добавлено | const q = POSES[j][i], o = (j * NP + i) * 2; |
| +добавлено | const lip = i >= 5 && i <= 9 && j >= 1 && j <= 4; |
| +добавлено | PS[o] = lip ? cx + (q[0] - cx) * k : q[0]; |
| +добавлено | PS[o + 1] = q[1]; |
| +добавлено | poseU[j * NP + i].set(PS[o], PS[o + 1]); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | curt.x = PS[(4 * NP + 6) * 2]; // where the lip tip lands |
| +добавлено | } |
| +добавлено | optics(); |
| +добавлено | poses(); |
| +добавлено | |
| +добавлено | const foamTex = foamTexture(fx.renderer); |
| +добавлено | const geo = gridGeometry(NU, NV); |
| +добавлено | const aA = instAttr(geo, 'iA', MAX_WAVES), aB = instAttr(geo, 'iB', MAX_WAVES), aC = instAttr(geo, 'iC', MAX_WAVES); |
| +добавлено | const mesh = new THREE.Mesh(geo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: WAVE_VERT, fragmentShader: WAVE_FRAG, |
| +добавлено | uniforms: { ...fx.U, uPose: { value: poseU }, uShape: { value: shape }, uAbsorption: { value: absorption }, uScatter: { value: scatter }, uFoamColor: { value: foamCol }, uFoamTint: { value: foamTint }, uLook: { value: look }, uFoamTex: { value: foamTex } }, |
| +добавлено | side: THREE.FrontSide, alphaToCoverage: true, |
| +добавлено | })); |
| +добавлено | mesh.frustumCulled = false; |
| +добавлено | mesh.visible = false; |
| +добавлено | fx.scenes.refract.add(mesh); |
| +добавлено | // crest spray: MANE_N quads, drawn once per live wave with the wave's instance data |
| +добавлено | const mgeo = new THREE.InstancedBufferGeometry(), mpos = new Float32Array(MANE_N * 12), mpuff = new Float32Array(MANE_N * 16), midx = []; |
| +добавлено | const CORNER = [-1, -1, 1, -1, 1, 1, -1, 1]; |
| +добавлено | for (let i = 0; i < MANE_N; i++) { |
| +добавлено | const r = [(i + 0.5) / MANE_N, Math.abs(Math.sin(i * 12.9898) * 43758.5453) % 1, Math.abs(Math.sin(i * 78.233) * 12543.123) % 1, Math.abs(Math.sin(i * 39.425) * 24634.634) % 1]; |
| +добавлено | for (let c = 0; c < 4; c++) { |
| +добавлено | mpos.set([CORNER[c * 2], CORNER[c * 2 + 1], 0], (i * 4 + c) * 3); |
| +добавлено | mpuff.set(r, (i * 4 + c) * 4); |
| +добавлено | } |
| +добавлено | midx.push(i * 4, i * 4 + 1, i * 4 + 2, i * 4, i * 4 + 2, i * 4 + 3); |
| +добавлено | } |
| +добавлено | mgeo.setAttribute('position', new THREE.Float32BufferAttribute(mpos, 3)); |
| +добавлено | mgeo.setAttribute('aPuff', new THREE.Float32BufferAttribute(mpuff, 4)); |
| +добавлено | mgeo.setIndex(midx); |
| +добавлено | mgeo.setAttribute('iA', aA); mgeo.setAttribute('iB', aB); mgeo.setAttribute('iC', aC); |
| +добавлено | mgeo.instanceCount = 0; |
| +добавлено | const maneU = new THREE.Vector4(1, 1, 0, 0); |
| +добавлено | const mane = new THREE.Mesh(mgeo, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: MANE_VERT, fragmentShader: PLUME_FRAG, ...FX_BLENDING, |
| +добавлено | uniforms: { ...fx.U, uPose: { value: poseU }, uShape: { value: shape }, uMane: { value: maneU }, uFoamColor: { value: foamCol } }, |
| +добавлено | })); |
| +добавлено | mane.frustumCulled = false; |
| +добавлено | mane.visible = false; |
| +добавлено | fx.scenes.fx.add(mane); |
| +добавлено | // --- splash (curtain + plume) ------------------------------------------------------------------- |
| +добавлено | // Everything thrown up where the lip lands, driven only by per-crash data (origin, frame, crash |
| +добавлено | // age, travel, launch and front speed): the refract-pass curtain sheet and the fx-pass spray |
| +добавлено | // plume. Kept apart from the wave so it can move onto a particle-fluid system as one piece. |
| +добавлено | const splash = (() => { |
| +добавлено | const cgeo = gridGeometry(NUC, NVC); |
| +добавлено | const cA = instAttr(cgeo, 'iA', MAX_WAVES), cB = instAttr(cgeo, 'iB', MAX_WAVES), cC = instAttr(cgeo, 'iC', MAX_WAVES); |
| +добавлено | const curtain = new THREE.Mesh(cgeo, new THREE.ShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: CURTAIN_VERT, fragmentShader: CURTAIN_FRAG, |
| +добавлено | uniforms: { ...fx.U, uShape: { value: shape }, uCurt: { value: curt }, uAbsorption: { value: absorption }, uScatter: { value: scatter }, uFoamColor: { value: foamCol }, uFoamTint: { value: foamTint }, uLook: { value: look }, uFoamTex: { value: foamTex } }, |
| +добавлено | side: THREE.DoubleSide, alphaToCoverage: true, |
| +добавлено | })); |
| +добавлено | curtain.frustumCulled = false; |
| +добавлено | curtain.visible = false; |
| +добавлено | fx.scenes.refract.add(curtain); |
| +добавлено | const pgeo = new THREE.InstancedBufferGeometry(); |
| +добавлено | pgeo.setAttribute('position', new THREE.Float32BufferAttribute([-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0], 3)); |
| +добавлено | pgeo.setIndex([0, 1, 2, 0, 2, 3]); |
| +добавлено | pgeo.instanceCount = 0; |
| +добавлено | const PA = Array.from({ length: MAX_WAVES }, () => new THREE.Vector4()); |
| +добавлено | const PB = Array.from({ length: MAX_WAVES }, () => new THREE.Vector4()); |
| +добавлено | const PC = Array.from({ length: MAX_WAVES }, () => new THREE.Vector4()); |
| +добавлено | const plumeU = new THREE.Vector4(1, 1, 0, 0); |
| +добавлено | const plume = new THREE.Mesh(pgeo, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, vertexShader: PLUME_VERT, fragmentShader: PLUME_FRAG, ...FX_BLENDING, |
| +добавлено | uniforms: { ...fx.U, uShape: { value: shape }, uCurt: { value: curt }, uPA: { value: PA }, uPB: { value: PB }, uPC: { value: PC }, uPlume: { value: plumeU }, uFoamColor: { value: foamCol } }, |
| +добавлено | })); |
| +добавлено | plume.frustumCulled = false; |
| +добавлено | plume.visible = false; |
| +добавлено | fx.scenes.fx.add(plume); |
| +добавлено | let m = 0; |
| +добавлено | return { |
| +добавлено | begin() { m = 0; }, |
| +добавлено | // one crash: ac = age at the centre (s), d = anchor travel at the crash (m) |
| +добавлено | add(O, F, H, W, seedF, ac, d, launch, front) { |
| +добавлено | if (m >= MAX_WAVES) return; |
| +добавлено | const o = m * 4, A = cA.array, B = cB.array, C = cC.array; |
| +добавлено | A[o] = O.x; A[o + 1] = O.y; A[o + 2] = O.z; A[o + 3] = seedF; |
| +добавлено | B[o] = F.x; B[o + 1] = F.z; B[o + 2] = H; B[o + 3] = W; |
| +добавлено | C[o] = ac; C[o + 1] = d; C[o + 2] = launch; C[o + 3] = front; |
| +добавлено | PA[m].set(O.x, O.y, O.z, seedF); PB[m].set(F.x, F.z, H, W); PC[m].set(ac, d, launch, front); |
| +добавлено | m++; |
| +добавлено | }, |
| +добавлено | end(amount) { |
| +добавлено | cgeo.instanceCount = m; |
| +добавлено | curtain.visible = m > 0; |
| +добавлено | pgeo.instanceCount = m * PLUME_N; |
| +добавлено | plume.visible = m > 0 && amount > 0; |
| +добавлено | plumeU.x = amount; |
| +добавлено | if (m) cA.needsUpdate = cB.needsUpdate = cC.needsUpdate = true; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | })(); |
| +добавлено | |
| +добавлено | // per-cast state, indexed by the cast record (cast.js keeps 16) |
| +добавлено | const ST = Array.from({ length: 16 }, () => ({ |
| +добавлено | live: false, O: new THREE.Vector3(), F: new THREE.Vector3(), S: new THREE.Vector3(), src: new THREE.Vector3(), |
| +добавлено | H: 1, W: 1, s: 0, d: 0, grow: 0, fade: 0, age: 0, seedF: 0, tau: 0, crashT: 0, crashD: 0, |
| +добавлено | light: null, force: null, mask: 0, crashed: false, seg: 0, splat: 0, rip: 0, wetDummy: -1, released: false, |
| +добавлено | })); |
| +добавлено | let lensWet = 0; |
| +добавлено | |
| +добавлено | // world position of control point i at lateral u (the JS mirror of wavePos at integer t) |
| +добавлено | function waveAt(st, u, i, out) { |
| +добавлено | const e = 2 * u - 1, p = S.params; |
| +добавлено | const brk = sstep(0.8, 2, st.s) * (1 - sstep(4.5, 5.5, st.s)); |
| +добавлено | const jit = (0.07 * Math.sin(u * 7.3 + st.seedF * 6.2831853) + 0.05 * Math.sin(u * 17.9 + st.seedF * 11) + 0.03 * Math.sin(u * 31.1 + st.seedF * 4)) * brk; |
| +добавлено | const su = Math.max(st.s + p.peel * (u - 0.5) + jit, 0); |
| +добавлено | const Hu = st.H * st.grow * taper(u) * (1 + 0.07 * Math.sin(u * 7 + st.seedF * 6.2831853)); |
| +добавлено | pt(i, su); |
| +добавлено | const W = st.W * (1 + 0.25 * sstep(3, 6, st.s)); |
| +добавлено | out.copy(st.O).addScaledVector(st.F, st.d - 0.35 * st.H * e * e + PT.x * Hu).addScaledVector(st.S, e * 0.5 * W); |
| +добавлено | out.y = st.O.y + PT.y * Hu; |
| +добавлено | return su; |
| +добавлено | } |
| +добавлено | function wetLine(a, b, width, wet, seed) { |
| +добавлено | SPL.pattern = 'line'; SPL.x = a.x; SPL.z = a.z; SPL.x2 = b.x; SPL.z2 = b.z; SPL.width = width; SPL.radius = width; SPL.wet = wet; SPL.seed = seed % 1000; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | function wetSpot(x, z, radius, wet, seed) { |
| +добавлено | SPL.pattern = 'noise'; SPL.x = SPL.x2 = x; SPL.z = SPL.z2 = z; SPL.radius = radius; SPL.width = 0; SPL.wet = wet; SPL.seed = seed % 1000; |
| +добавлено | fx.ground.splat(SPL); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the swell, the wall, the curl, the crash, the bore: everything keyed off the phase |
| +добавлено | function step(c, st, dt) { |
| +добавлено | const p = S.params, H = st.H, tau = st.tau; |
| +добавлено | const sp = p.speed; |
| +добавлено | st.s = lerpKeys(TK, SK, tau * sp); |
| +добавлено | st.d = lerpKeys(TK, DK, tau * sp) * H; |
| +добавлено | st.grow = tau < 0 ? sstep(-1, -0.15, tau * sp) : 1; |
| +добавлено | st.fade = sstep(4.6, 6.0, tau * sp); |
| +добавлено | const s = st.s, L = st.light; |
| +добавлено | |
| +добавлено | // emission walks the crest one segment per frame (one record per emitter) |
| +добавлено | const j = st.seg, u0 = 0.06 + 0.88 * j / NSEG, u1 = 0.06 + 0.88 * (j + 1) / NSEG, um = 0.5 * (u0 + u1); |
| +добавлено | st.seg = (st.seg + 1) % NSEG; |
| +добавлено | const sdt = dt * NSEG, wgt = taper(um); |
| +добавлено | const su = waveAt(st, um, 6, _c); // this segment's phase (and its lip tip) |
| +добавлено | if (tau >= 0) { |
| +добавлено | // the lip sheds droplets and spray as it throws |
| +добавлено | if (su > 1.3 && su < 3.4) { |
| +добавлено | waveAt(st, u0, 6, drops.pos); waveAt(st, u1, 6, drops.p); |
| +добавлено | drops.vel.copy(st.F).multiplyScalar(3 + 2 * sp).addScaledVector(UP, su < 2.5 ? 1.5 : -1); drops.radial = 1.2; drops.size = 2.2; drops.spread = 1.6; |
| +добавлено | drops.rate(900 * p.spray * wgt, sdt); |
| +добавлено | spray.pos.copy(drops.pos); spray.p.copy(drops.p); |
| +добавлено | spray.vel.copy(st.F).multiplyScalar(2.5).addScaledVector(UP, 0.6); spray.size = 1.6; spray.radial = 0.8; |
| +добавлено | spray.rate(45 * p.spray * wgt, sdt); |
| +добавлено | } |
| +добавлено | // the plunge: a splash-up curtain where this section of the lip lands |
| +добавлено | if (su > 3.3 && su < 4.4) { |
| +добавлено | const k = clamp01(1 - Math.abs(su - 3.75) / 0.6); |
| +добавлено | waveAt(st, u0, 6, drops.pos); waveAt(st, u1, 6, drops.p); |
| +добавлено | drops.pos.y = drops.p.y = 0.15; |
| +добавлено | drops.vel.copy(st.F).multiplyScalar(3).addScaledVector(UP, 6.5); drops.radial = 2.5; drops.size = 2.4; drops.spread = 2.4; |
| +добавлено | drops.rate(3200 * p.spray * wgt * k, sdt); |
| +добавлено | } |
| +добавлено | // the bore: droplets kicked off the running front |
| +добавлено | if (su > 4.2 && s < 5.6) { |
| +добавлено | const k = 1 - sstep(4.8, 5.6, su); |
| +добавлено | waveAt(st, u0, 5, drops.pos); waveAt(st, u1, 5, drops.p); |
| +добавлено | drops.vel.copy(st.F).multiplyScalar(4.5 * sp).addScaledVector(UP, 2.5); drops.radial = 1.5; drops.size = 1.8; drops.spread = 1.5; |
| +добавлено | drops.rate(500 * p.spray * wgt * k, sdt); |
| +добавлено | } |
| +добавлено | // the wind tears spray and mist off the churn's tops, back over it |
| +добавлено | if (su > 3.6 && su < 5.2) { |
| +добавлено | waveAt(st, u0, 4, spray.pos); waveAt(st, u1, 4, spray.p); |
| +добавлено | spray.vel.copy(st.F).multiplyScalar(-1.2).addScaledVector(UP, 1.4); spray.size = 2.0; spray.radial = 0.7; |
| +добавлено | spray.rate(30 * p.spray * wgt * (1 - sstep(4.6, 5.2, su)), sdt); |
| +добавлено | } |
| +добавлено | // low mist hanging over the spent sheet |
| +добавлено | if (su > 4.8) { |
| +добавлено | waveAt(st, u0, 3, mist.pos); waveAt(st, u1, 3, mist.p); |
| +добавлено | mist.pos.y = mist.p.y = 0.25; |
| +добавлено | mist.vel.copy(st.F).multiplyScalar(0.8).addScaledVector(UP, 0.15); mist.size = 1.6; |
| +добавлено | mist.rate(14 * p.mist * wgt * (1 - st.fade), sdt); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the centre's plunge: the big hit |
| +добавлено | if (!st.crashed && s >= S_CRASH) { |
| +добавлено | st.crashed = true; |
| +добавлено | waveAt(st, 0.5, 6, _a); |
| +добавлено | _a.y = 0; |
| +добавлено | wetSpot(_a.x, _a.z, 0.8 * H, 1.0 * p.wetness, c.seed + 7); |
| +добавлено | fx.impacts.add('wet', _a, 0.9 * H, 1, 8, lightCol); |
| +добавлено | fx.rig.shake(0.7, 9); |
| +добавлено | if (!fx.reduced) { fx.rig.punch(2.2, 0.12); fx.rig.hitstop(0.05); } |
| +добавлено | if (fx.camera.position.distanceTo(_a) < 7 + H) lensWet = 1; |
| +добавлено | if (nearPool(_a.x, _a.z, 1.5)) fx.pool.ripple(Math.min(Math.max(_a.x, POOL.center.x - POOL.half.x), POOL.center.x + POOL.half.x), _a.z, 0.12, 0.9); |
| +добавлено | if (L) L.intensity = Math.max(L.intensity, 30); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // the running front paints a wet swath and drenches whatever it reaches |
| +добавлено | if (tau > 0.1 && s < 5.8) { |
| +добавлено | st.splat -= dt; |
| +добавлено | if (st.splat <= 0) { |
| +добавлено | st.splat = 0.1; |
| +добавлено | waveAt(st, 0.1, s < 3.5 ? 11 : 6, _a); waveAt(st, 0.9, s < 3.5 ? 11 : 6, _b); |
| +добавлено | wetLine(_a, _b, 0.12 * H, 0.45 * p.wetness, c.seed + tau * 97); |
| +добавлено | } |
| +добавлено | const fr = s < 3.5 ? 11 : 6; |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i]; |
| +добавлено | _v.subVectors(dm.pos, st.O); |
| +добавлено | const lat = _v.dot(st.S) / (0.5 * st.W); |
| +добавлено | if (Math.abs(lat) > 0.9) continue; |
| +добавлено | waveAt(st, 0.5 + 0.5 * lat, fr, _a); |
| +добавлено | const ahead = _v.dot(st.F) - _b.subVectors(_a, st.O).dot(st.F); |
| +добавлено | if (ahead > 0.3) continue; |
| +добавлено | dm.status.wet = 1; |
| +добавлено | if (!(st.mask & (1 << i)) && s > 0.6) { |
| +добавлено | st.mask |= 1 << i; |
| +добавлено | dm.hit(st.F, (s > 3 ? 1.6 : 1.0) * (H / 4.5) * taper(0.5 + 0.5 * lat)); |
| +добавлено | st.wetDummy = i; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // a bore reaching the pool shoves its surface |
| +добавлено | st.rip -= dt; |
| +добавлено | if (st.rip <= 0) { |
| +добавлено | st.rip = 0.06; |
| +добавлено | waveAt(st, 0.2 + 0.6 * fx.rand(), fr, _a); |
| +добавлено | if (nearPool(_a.x, _a.z, 0.3)) fx.pool.ripple(_a.x, _a.z, 0.04, 0.5); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // forces: the swell drags loose particles in, the wave front shoves them on |
| +добавлено | const fo = st.force; |
| +добавлено | if (fo) { |
| +добавлено | if (tau < 0) { |
| +добавлено | fo.type = 2; fo.pos.copy(st.O).setY(0.6); fo.p[0] = 35 * st.grow; fo.p[1] = 1.2; fo.p[2] = 0.35; fo.p[3] = 1.6 * H; |
| +добавлено | } else { |
| +добавлено | waveAt(st, 0.5, s < 3.5 ? 10 : 5, fo.pos); |
| +добавлено | fo.pos.addScaledVector(st.F, -1.5); fo.pos.y = 0.4; |
| +добавлено | fo.type = 5; fo.dir.copy(st.F); |
| +добавлено | fo.p[0] = 7 * sp * (1 - st.fade); fo.p[1] = 0.45 * st.W; fo.p[2] = 3.5; fo.p[3] = 2.5; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // a caustic cookie light rides under the barrel |
| +добавлено | if (L) { |
| +добавлено | waveAt(st, 0.5, 8, L.pos); |
| +добавлено | L.pos.y = Math.max(L.pos.y, 0.6 * H * st.grow) + 0.4; |
| +добавлено | // (gone as the lip lands: the bore is opaque churn that focuses no light, and the cookie's |
| +добавлено | // caustic web drawn over its foam reads as wire netting; back faintly for the clear spent sheet) |
| +добавлено | const target = tau < 0 ? 6 * st.grow : s < 3.3 ? 6 + 10 * sstep(0.3, 2.8, s) : 16 * (1 - sstep(3.3, 3.8, s)) + 3 * (1 - st.fade) * sstep(4.8, 5.5, s); |
| +добавлено | L.intensity += (target - L.intensity) * (1 - Math.exp(-dt * 6)); |
| +добавлено | L.range = 2.4 * H; |
| +добавлено | L.cookie[1] = 1.6 / Math.sqrt(H / 4.5); |
| +добавлено | L.cookie[2] = st.age * 0.35; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'curl') poses(); else optics(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, st = ST[c.index]; |
| +добавлено | if (name === 'charge') { |
| +добавлено | optics(); |
| +добавлено | const sp = p.speed; |
| +добавлено | c.durations.charge = 1.0 / sp; |
| +добавлено | c.durations.impact = T_IMPACT / sp; |
| +добавлено | c.durations.linger = T_LINGER / sp; |
| +добавлено | st.live = true; st.age = 0; st.tau = -c.durations.charge; st.mask = 0; st.crashed = false; st.seg = 0; |
| +добавлено | st.splat = 0; st.rip = 0; st.wetDummy = -1; st.released = false; |
| +добавлено | st.seedF = (c.seed % 997) / 997; |
| +добавлено | st.H = p.height; st.W = p.width; |
| +добавлено | st.crashT = lerpKeys(SK, TK, S_CRASH) / sp; |
| +добавлено | st.crashD = lerpKeys(TK, DK, st.crashT * sp) * st.H; |
| +добавлено | // sweep: from behind the target towards the caster's side, turned by 'angle' |
| +добавлено | st.F.set(c.from.x - c.to.x, 0, c.from.z - c.to.z); |
| +добавлено | if (st.F.lengthSq() < 1e-6) st.F.set(0, 0, 1); |
| +добавлено | st.F.normalize().applyAxisAngle(UP, THREE.MathUtils.degToRad(p.angle)); |
| +добавлено | st.S.set(-st.F.z, 0, st.F.x); |
| +добавлено | // rise behind the target (pulled in if that would leave the arena) |
| +добавлено | let back = BACK * st.H; |
| +добавлено | st.O.set(c.to.x, 0, c.to.z).addScaledVector(st.F, -back); |
| +добавлено | while (back > 0.6 * st.H && Math.hypot(st.O.x, st.O.z) > ARENA.radius - 1.1 * st.H) { |
| +добавлено | back -= 0.25; |
| +добавлено | st.O.set(c.to.x, 0, c.to.z).addScaledVector(st.F, -back); |
| +добавлено | } |
| +добавлено | // the pool water nearest the swell feeds it |
| +добавлено | st.src.set( |
| +добавлено | Math.min(Math.max(st.O.x, POOL.center.x - POOL.half.x + 0.6), POOL.center.x + POOL.half.x - 0.6), |
| +добавлено | POOL.water, |
| +добавлено | Math.min(Math.max(st.O.z, POOL.center.y - POOL.half.y + 0.6), POOL.center.y + POOL.half.y - 0.6)); |
| +добавлено | st.light = fx.lights.acquire(); |
| +добавлено | if (st.light) { |
| +добавлено | const L = st.light; |
| +добавлено | L.type = 2; L.cookie[0] = 1; L.cookie[1] = 1.6; L.cookie[2] = 0; |
| +добавлено | L.range = 2.4 * st.H; L.intensity = 0; L.color.copy(lightCol); L.pos.copy(st.O); |
| +добавлено | } |
| +добавлено | st.force = fx.forces.acquire(); |
| +добавлено | wetSpot(st.O.x, st.O.z, 0.7 * st.H, 0.35 * p.wetness, c.seed + 3); |
| +добавлено | fx.rig.shake(0.18, 4); |
| +добавлено | } else if (name === 'release') { |
| +добавлено | st.released = true; |
| +добавлено | // the swell bursts upward into the wall |
| +добавлено | waveAt(st, 0.2, 4, drops.pos); waveAt(st, 0.8, 4, drops.p); |
| +добавлено | drops.vel.set(0, 4, 0).addScaledVector(st.F, 1); drops.radial = 2; drops.size = 2; drops.spread = 2; |
| +добавлено | drops.burst(900 * p.spray); |
| +добавлено | spray.pos.copy(drops.pos); spray.p.copy(drops.p); spray.vel.set(0, 3, 0); spray.size = 2; spray.radial = 1; |
| +добавлено | spray.burst(50 * p.spray); |
| +добавлено | fx.rig.shake(0.3, 6); |
| +добавлено | } else if (name === 'done') { |
| +добавлено | st.live = false; |
| +добавлено | st.light?.release(); st.light = null; |
| +добавлено | st.force?.release(); st.force = null; |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, st = ST[c.index]; |
| +добавлено | if (!st.live) return; |
| +добавлено | st.age += dt; |
| +добавлено | st.tau = c.phase === 'charge' ? c.t - c.durations.charge : c.phase === 'impact' ? c.t : c.durations.impact + c.t; |
| +добавлено | step(c, st, dt); |
| +добавлено | if (c.phase === 'charge') { |
| +добавлено | const k = c.t / c.durations.charge; |
| +добавлено | // drawback: the pool swirls and streams arc out of it into the swell |
| +добавлено | if (k < 0.8) { |
| +добавлено | const T = Math.max(c.durations.charge - c.t + 0.15, 0.25); |
| +добавлено | launch(beads.vel, st.src, _a.copy(st.O).setY(0.3 * st.H * st.grow + 0.2), T); |
| +добавлено | beads.pos.copy(st.src); beads.radius = 1.0; beads.size = 1.6; beads.life = T / 0.9; |
| +добавлено | beads.rate(700 * p.spray, dt); |
| +добавлено | fx.pool.swirl(st.src.x, st.src.z, 1 + 1.5 * k); |
| +добавлено | st.rip -= dt; |
| +добавлено | if (st.rip <= 0) { st.rip = 0.05; fx.pool.ripple(st.src.x + (fx.rand() - 0.5) * 2, st.src.z + (fx.rand() - 0.5) * 2, -0.012, 0.3); } |
| +добавлено | } |
| +добавлено | // the ground behind the target wells up |
| +добавлено | welling.pos.copy(st.O).setY(0.1); welling.radius = 0.25 * st.H * (0.4 + k); welling.vel.set(0, 1.6, 0); welling.radial = 0.8; welling.size = 1.4; |
| +добавлено | welling.rate(40 * p.mist * k, dt); |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (c.phase === 'linger' && st.wetDummy >= 0) { |
| +добавлено | // a soaked dummy drips |
| +добавлено | const dm = fx.dummies[st.wetDummy].pos, k = 1 - c.t / c.durations.linger; |
| +добавлено | drip.pos.set(dm.x, 1.55, dm.z); drip.p.set(dm.x, 0.5, dm.z); drip.vel.set(0, -0.3, 0); drip.life = 0.8; |
| +добавлено | drip.rate(60 * k, dt); |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | |
| +добавлено | // once per frame: live waves upload their instance data; lens droplets dry |
| +добавлено | tick(dt) { |
| +добавлено | let n = 0; |
| +добавлено | const A = aA.array, B = aB.array, C = aC.array; |
| +добавлено | for (const st of ST) { |
| +добавлено | if (!st.live) continue; |
| +добавлено | const o = n * 4; |
| +добавлено | A[o] = st.O.x; A[o + 1] = st.O.y; A[o + 2] = st.O.z; A[o + 3] = st.seedF; |
| +добавлено | B[o] = st.F.x; B[o + 1] = st.F.z; B[o + 2] = st.H * st.grow; B[o + 3] = st.W; |
| +добавлено | C[o] = st.s; C[o + 1] = st.d; C[o + 2] = st.fade; C[o + 3] = st.age; |
| +добавлено | if (++n >= MAX_WAVES) break; |
| +добавлено | } |
| +добавлено | geo.instanceCount = mgeo.instanceCount = n; |
| +добавлено | mesh.visible = n > 0; |
| +добавлено | maneU.x = S.params.mist; |
| +добавлено | mane.visible = n > 0 && maneU.x > 0; |
| +добавлено | if (n) aA.needsUpdate = aB.needsUpdate = aC.needsUpdate = true; |
| +добавлено | // splash: from the first section landing until the last spray has fallen |
| +добавлено | splash.begin(); |
| +добавлено | const sp = S.params.speed, spread = 0.5 * Math.abs(S.params.peel) * shape.w + 0.05; |
| +добавлено | for (const st of ST) { |
| +добавлено | if (!st.live) continue; |
| +добавлено | const ac = st.tau - st.crashT; |
| +добавлено | if (ac < -spread || ac > SPLASH_LIFE + spread) continue; |
| +добавлено | splash.add(st.O, st.F, st.H, st.W, st.seedF, ac, st.crashD, 4.6 * Math.sqrt(st.H), 0.45 * st.H * sp); |
| +добавлено | } |
| +добавлено | splash.end(S.params.spray); |
| +добавлено | if (lensWet > 0) { lensWet = Math.max(0, lensWet - dt / 5); fx.post.params.droplets = lensWet; } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | import * as THREE from 'three'; |
| +добавлено | import { COMMON_GLSL, NOISE_GLSL, ARENA_GLSL, LIGHTS_GLSL, FX_VERT_HEAD, FX_FRAG_HEAD, FX_OUT_GLSL, FX_BLENDING } from '../vfx/common.js'; |
| +добавлено | |
| +добавлено | // Wind (PLAN 3). Air is invisible: it is shown by what it carries and by how it bends the light. |
| +добавлено | // Reference: tornado / dust-devil footage (a coherent column whose walls are sheets of dust |
| +добавлено | // spiralling up, thick and dark at the foot, thinning aloft; a debris cloud skirting the ground). |
| +добавлено | // Cyclone: the caster's focus gathers air while dust starts to whirl on the floor where the funnel |
| +добавлено | // will touch down; then a raymarched dust funnel (its own marcher: helical dust sheets advected round |
| +добавлено | // and up the wall, a churning cauliflower debris cloud at the foot, sun-lit with a self-shadowed |
| +добавлено | // side) spins up from the ground and walks to the target. A Rankine vortex joins the shared force |
| +добавлено | // field (every loose particle of every spell is dragged in), gravel, rocks and leaves are lifted |
| +добавлено | // and flung out of it with weight, lit dust rolls out along the floor and is sucked back in, the |
| +добавлено | // floor is scoured in cycloidal streaks, refraction sheets (pure distortion helix ribbons) and |
| +добавлено | // shimmer sprites ride the wall, dummies are buffeted and the pool swirls. It ropes out at the |
| +добавлено | // end: narrows, sways, thins and drops its load. |
| +добавлено | // Wind Slash: a pressure blade thrown low. Mostly invisible: a refraction edge with a faint vapour |
| +добавлено | // sheet, a shear line of floor dust lifted and dragged behind it, a rooster tail of dust and chips |
| +добавлено | // where its lower tip scores a cut into the ground field, shimmer left in its wake. |
| +добавлено | |
| +добавлено | const NH = 10; // refraction helix ribbons per cyclone (fx: distortion only) |
| +добавлено | const NS = 3; // charge swirl arms |
| +добавлено | const NB = 3; // blade layers (main + 2 echoes) |
| +добавлено | const NV = 2; // funnel marchers (cyclones alive at once with a funnel) |
| +добавлено | const TAU = Math.PI * 2; |
| +добавлено | // LOD-0 lookups only inside the march (Volumes.js: gradients in a data-dependent loop make ANGLE's FXC unroll it) |
| +добавлено | const NO_GRADIENTS = '#define texture(s, p) textureLod(s, p, 0.0)\n'; |
| +добавлено | |
| +добавлено | // One path for every wind ribbon (one draw per pass). p0.w picks the shape: |
| +добавлено | // 0 helix up the funnel: p0 foot, p1 (height, radius foot, radius top, turns), p2 (angle, lean x, lean z, width), p3 (seed, reveal, distortion m, body) |
| +добавлено | // 1 crescent: p0 middle (leading point), p1 (forward, radius), p2 (transverse, width), p3 (seed, half arc, distortion m, body) |
| +добавлено | // 2 swirl into the focus: p0 centre, p1 (outer r, inner r, turns, rise), p2 (angle, -, -, width), p3 (seed, reveal, distortion m, body) |
| +добавлено | const PATH_GLSL = /* glsl */` |
| +добавлено | vec3 ribbonPath(float t, vec4 p0, vec4 p1, vec4 p2, vec4 p3, out float width) { |
| +добавлено | int mode = int(p0.w + 0.5); |
| +добавлено | if (mode == 1) { |
| +добавлено | // the middle leads and the tips trail; width tapers to points |
| +добавлено | float A = p3.y, th = (t * 2.0 - 1.0) * A; |
| +добавлено | width = p2.w * pow(max(cos(th / A * 1.5707963), 0.0), 0.7); |
| +добавлено | return p0.xyz + p1.xyz * (p1.w * (cos(th) - 1.0)) + p2.xyz * (p1.w * sin(th)); |
| +добавлено | } |
| +добавлено | if (mode == 2) { |
| +добавлено | float th = p2.x - t * p1.z * 6.2831853; |
| +добавлено | float r = mix(p1.x, p1.y, t); |
| +добавлено | width = p2.w * (1.0 - 0.6 * t); |
| +добавлено | return p0.xyz + vec3(cos(th) * r, (t - 0.5) * p1.w, sin(th) * r); |
| +добавлено | } |
| +добавлено | float r = mix(p1.y, p1.z, pow(t, 1.6)) * (1.0 + 0.07 * sin(t * 6.0 + p3.x * 6.2831853 + uTime * 2.3)); |
| +добавлено | float th = p2.x + t * p1.w * 6.2831853; |
| +добавлено | width = p2.w * mix(0.6, 1.6, t); |
| +добавлено | return p0.xyz + vec3(p2.y, 0.0, p2.z) * (t * t) + vec3(cos(th) * r, t * p1.x, sin(th) * r); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const SHADE_GLSL = /* glsl */` |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | vec3 ribbonShade(RibbonFrag r, out vec3 media, out float alpha, out float distortion) { |
| +добавлено | int mode = int(r.p0.w + 0.5); |
| +добавлено | float u = r.uv.x, v = r.uv.y; |
| +добавлено | float core = max(exp(-4.0 * v * v) - 0.0183, 0.0); |
| +добавлено | float along = 1.0, body = 1.0; |
| +добавлено | if (mode == 1) { |
| +добавлено | // turbulent shear along the blade |
| +добавлено | along = 0.7 + 0.3 * sin(u * 37.0 - uTime * 23.0 + r.p3.x * 6.2831853) * sin(u * 11.0 + uTime * 9.0); |
| +добавлено | } else { |
| +добавлено | // gusts of moving air running along the ribbon: up the funnel, into the focus |
| +добавлено | // a bright head and a fading tail, ~40% duty, so it reads as gusts rather than stripes |
| +добавлено | float s = fract(u * (mode == 0 ? 3.0 : 2.0) - uTime * (mode == 0 ? 0.5 : 1.4) + r.p3.x); |
| +добавлено | float ends = smoothstep(0.0, 0.1, u) * (1.0 - smoothstep(0.8, 1.0, u)) * (1.0 - smoothstep(r.p3.y - 0.15, r.p3.y, u)); |
| +добавлено | along = smoothstep(0.5, 0.95, s) * (1.0 - smoothstep(0.95, 1.0, s)) * ends; |
| +добавлено | body = (0.35 + 0.65 * along / max(ends, 1e-3)) * ends; |
| +добавлено | } |
| +добавлено | // derivative-of-gaussian profile: the centreline acts as a cylindrical lens |
| +добавлено | distortion = r.p3.z * v * exp(-3.0 * v * v) * 2.33 * body; |
| +добавлено | media = uSunColor * 0.12 + uAmbient * 1.6; |
| +добавлено | alpha = r.p3.w * core * along; |
| +добавлено | return r.color * core * along; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | // The funnel: its own raymarcher in the fx pass (a box proxy, back faces, depth test off, like |
| +добавлено | // Volumes.js), because a generic box march spends most of its samples in the empty air round a |
| +добавлено | // column. Samples are spread with a Cauchy warp about the ray's closest approach to the axis, so |
| +добавлено | // ~half of them land within one funnel radius of it (~0.15 m apart through the wall, coarser in |
| +добавлено | // the empty air). Integration is trapezoid slabs over the warped samples; lighting (sun with one |
| +добавлено | // shadow tap + a multiple-scattering lobe, sky dimmed by the same tap and by the floor, spell |
| +добавлено | // lights with one tap towards the brightest) is refreshed on every 2nd occupied sample. |
| +добавлено | // The medium is in cylindrical coordinates about the swaying axis. The dust pattern is advected |
| +добавлено | // with the flow (round with the wall, up with the updraft): noise sampled in (angle + spin, |
| +добавлено | // height - rise, radius) with an integer number of tiles per turn (the noise texture tiles) and a |
| +добавлено | // slope that follows the streamlines, which stretches it into helical sheets. |
| +добавлено | const FUNNEL_GLSL = /* glsl */` |
| +добавлено | uniform vec4 uTw0; // foot x, z, height H (m), spin (turns) |
| +добавлено | uniform vec4 uTw1; // lean x, z at the top (m), size, rise (m) |
| +добавлено | uniform vec4 uTw2; // seed, density 1/m, sway (m), time (s) |
| +добавлено | uniform vec4 uTw3; // debris cloud 0..1, column reveal (fraction of H), detail, rope-out 0..1 |
| +добавлено | uniform vec3 uDust; // albedo |
| +добавлено | uniform vec3 uBoxMin; |
| +добавлено | uniform vec3 uBoxMax; |
| +добавлено | uniform vec4 uMarch; // samples, sun tap (m), HG g, axis scale (m) |
| +добавлено | uniform vec3 uSunDir; |
| +добавлено | uniform vec3 uSunColor; |
| +добавлено | uniform vec3 uAmbient; |
| +добавлено | in vec3 vWorld; |
| +добавлено | float gDt = 0.1; // this sample's spacing: detail finer than it is faded out |
| +добавлено | float sq(float x) { return x * x; } |
| +добавлено | // coarse: the shadow taps, which only need the big shapes (no sheets, no billows) |
| +добавлено | float funnel(vec3 pw, bool coarse, out vec3 albedo) { |
| +добавлено | albedo = vec3(0.0); |
| +добавлено | float H = uTw0.z, sz = uTw1.z, y = pw.y, hn = y / H; |
| +добавлено | if (y < 0.0 || hn > 1.15) return 0.0; |
| +добавлено | float t = uTw2.w, seed = uTw2.x, rope = uTw3.w, hc = min(hn, 1.0); |
| +добавлено | // the axis lags its motion (lean) and sways like a rope, more as it ropes out |
| +добавлено | vec2 sway = vec2(sin(hn * 4.7 - t * 1.9 + seed * 6.0), cos(hn * 3.9 - t * 1.6 + seed * 4.0)); |
| +добавлено | vec2 q = pw.xz - uTw0.xy - uTw1.xy * (hn * hn) - sway * (uTw2.z * hn * (1.0 + 2.5 * rope)); |
| +добавлено | float r = length(q); |
| +добавлено | float R = sz * (0.4 + 1.5 * pow(hc, 1.8)) * (1.0 - 0.6 * rope); // wall radius |
| +добавлено | float cloudR = sz * 2.0 * uTw3.x; // debris cloud reach |
| +добавлено | float reach = max(R * 1.5 + 0.2 * sz, cloudR * 1.4 * (1.0 - smoothstep(1.0 * sz, 2.2 * sz, y))); |
| +добавлено | if (r > reach) return 0.0; |
| +добавлено | float a = atan(q.y, q.x) * 0.15915494; |
| +добавлено | float spin = uTw0.w * (1.0 - 0.4 * hc); |
| +добавлено | // big lumps riding round with the wall and rising; they also roughen every edge |
| +добавлено | float n2 = noise3(vec3((a + spin) * 2.0, (y - 0.5 * uTw1.w) * 0.08, r * 0.1 + seed)); |
| +добавлено | // helical sheets: across the streamlines, stretched along them |
| +добавлено | float n1 = coarse ? 0.5 : noise3(vec3((a + spin) * 4.0 + hn * 6.5, (y - uTw1.w) * 0.07, r * 0.12 + seed * 0.7)); |
| +добавлено | float lump = n2 - 0.5; |
| +добавлено | // column: a wall of dust round a thinner core, broken into sheets, thick at the foot |
| +добавлено | float rr = r + lump * R * 0.7; |
| +добавлено | float W = R * (0.2 + 0.1 * hc) + 0.1 * sz; |
| +добавлено | float wall = exp(-sq((rr - 0.72 * R) / W)) + 0.25 * (1.0 - smoothstep(0.1 * R, 0.8 * R, rr)); |
| +добавлено | float sheets = smoothstep(0.32, 0.7, n1 + lump * 0.35); |
| +добавлено | float reveal = 1.0 - smoothstep(uTw3.y * H - 0.8, uTw3.y * H + 0.3, y + lump * 2.0); |
| +добавлено | float top = 1.0 - smoothstep(0.7, 1.08, hn + lump * 0.3); |
| +добавлено | float column = wall * mix(0.1, 1.4, sheets) * mix(1.6, 0.45, hc) * top * reveal; |
| +добавлено | // debris cloud: a squat, churning sheath round the foot, cauliflower edged |
| +добавлено | float hb = y / (1.6 * sz); |
| +добавлено | float cr = cloudR * (0.7 + 0.45 * hb) + lump * 1.5 * sz; |
| +добавлено | float cloud = smoothstep(cr, cr * 0.35, r) * (1.0 - smoothstep(0.3, 1.0, hb + lump * 0.8)); |
| +добавлено | float d = column + 2.2 * cloud; |
| +добавлено | if (d < 0.01) return 0.0; |
| +добавлено | // fine billows, faded where the march can't resolve them |
| +добавлено | float detail = uTw3.z * (1.0 - smoothstep(0.1, 0.3, gDt)); |
| +добавлено | if (detail > 0.01 && !coarse) { |
| +добавлено | float n3 = noise3(vec3((a + spin * 1.15) * 6.0, (y - 0.4 * uTw1.w) * 0.2, r * 0.3 + seed * 1.3)); |
| +добавлено | d *= mix(1.0, 0.35 + 1.2 * n3, detail); |
| +добавлено | } |
| +добавлено | // dirtier and darker low down, paler where the wall thins aloft |
| +добавлено | albedo = uDust * (0.8 + 0.4 * n1) * mix(0.45, 1.05, smoothstep(0.0, 0.6, hn)); |
| +добавлено | return uTw2.y * d * (1.0 - 0.6 * rope); |
| +добавлено | } |
| +добавлено | void main() { |
| +добавлено | gLightNoShadow = true; |
| +добавлено | vec3 ro = cameraPosition, rd = normalize(vWorld - ro); |
| +добавлено | vec3 inv = 1.0 / mix(rd, vec3(1e-6), lessThan(abs(rd), vec3(1e-6))); |
| +добавлено | vec3 ta = (uBoxMin - ro) * inv, tb = (uBoxMax - ro) * inv, tn = min(ta, tb), tf = max(ta, tb); |
| +добавлено | float cosV = dot(rd, -vec3(viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2])); |
| +добавлено | float t0 = max(max(max(tn.x, tn.y), tn.z), 0.0), t1 = min(min(tf.x, tf.y), tf.z); |
| +добавлено | float tEnd = min(t1, sceneViewZ() / cosV); |
| +добавлено | if (tEnd <= t0) discard; |
| +добавлено | // Cauchy warp about the closest approach to the axis (in plan), over the whole box chord so the |
| +добавлено | // samples don't slide with the floor depth (that turns into contour bands); cut at the scene |
| +добавлено | float l2 = dot(rd.xz, rd.xz); |
| +добавлено | float tc = l2 > 1e-6 ? dot(uTw0.xy + 0.5 * uTw1.xy - ro.xz, rd.xz) / l2 : 0.5 * (t0 + t1); |
| +добавлено | float s = min(uMarch.w / sqrt(max(l2, 1e-6)), t1 - t0); |
| +добавлено | float a0 = atan((t0 - tc) / s), a1 = atan((t1 - tc) / s); |
| +добавлено | int N = int(uMarch.x); |
| +добавлено | float da = (a1 - a0) / float(N); |
| +добавлено | float tap = uMarch.y, g = uMarch.z; |
| +добавлено | vec3 sun = uSunColor * hgPhase(dot(uSunDir, rd), g) * 4.0 * PI; |
| +добавлено | bool hasLight = uWorldCounts.x > 0.5; |
| +добавлено | vec3 scat = vec3(0.0), Lin = vec3(0.0), sP = vec3(0.0), alb, a2; |
| +добавлено | float T = 1.0, zSum = 0.0, wSum = 0.0, sigP = 0.0, tP = t0, odSun = 0.0, odL = 0.0; |
| +добавлено | int lit = 0; |
| +добавлено | for (int i = 0; i <= 48; i++) { |
| +добавлено | if (i > N || T < 0.01 || tP >= tEnd) break; |
| +добавлено | float t = min(tc + s * tan(a0 + da * float(i)), tEnd); |
| +добавлено | gDt = s * da * (1.0 + sq((t - tc) / s)); // dt/di of the warp |
| +добавлено | vec3 pw = ro + rd * t; |
| +добавлено | float sigma = funnel(pw, false, alb); |
| +добавлено | if (sigma > 1e-4) { |
| +добавлено | // sun shadow every occupied sample (the lit side vs the shadowed side is the look); |
| +добавлено | // the spell-light tap every 2nd |
| +добавлено | odSun = (sigma + funnel(pw + uSunDir * tap, true, a2)) * tap; |
| +добавлено | { |
| +добавлено | if (hasLight && (lit & 1) == 0) { |
| +добавлено | SpellLight L0 = spellLight(0); |
| +добавлено | vec3 lp = L0.type == 1 ? closestOnSegment(L0.pos, L0.end, pw) : L0.pos, ld = lp - pw; |
| +добавлено | float ll = length(ld), dl = min(tap, ll * 0.5); |
| +добавлено | odL = (sigma + funnel(pw + ld * (dl / max(ll, 1e-4)), true, a2)) * dl; |
| +добавлено | } |
| +добавлено | // the sky is half hidden near the floor |
| +добавлено | float sky = mix(0.35, 1.0, exp(-0.5 * odSun)) * mix(0.55, 1.0, smoothstep(0.0, 2.5, pw.y)); |
| +добавлено | Lin = sun * (exp(-odSun) + 0.25 * exp(-0.25 * odSun)) + uAmbient * sky |
| +добавлено | + spellLightScatter(pw, -rd, g) * (exp(-odL) + 0.25 * exp(-0.25 * odL)); |
| +добавлено | } |
| +добавлено | lit++; |
| +добавлено | } |
| +добавлено | vec3 S = alb * Lin; |
| +добавлено | float sg = 0.5 * (sigma + sigP), len = t - tP; |
| +добавлено | if (i > 0 && sg > 1e-4) { |
| +добавлено | float Tr = exp(-sg * len), w = T * (1.0 - Tr); |
| +добавлено | scat += w * 0.5 * (S + sP); |
| +добавлено | zSum += w * (t - 0.5 * len); wSum += w; |
| +добавлено | T *= Tr; |
| +добавлено | } |
| +добавлено | sigP = sigma; sP = S; tP = t; |
| +добавлено | } |
| +добавлено | float alpha = 1.0 - T; |
| +добавлено | if (alpha < 0.002) discard; |
| +добавлено | fxOut(scat / max(alpha, 1e-4), alpha, vec3(0.0), vec2(0.0), (wSum > 0.0 ? zSum / wSum : t0) * cosV); |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | const FUNNEL_VERT = /* glsl */` |
| +добавлено | out vec3 vWorld; |
| +добавлено | void main() { |
| +добавлено | vec4 w = modelMatrix * vec4(position, 1.0); |
| +добавлено | vWorld = w.xyz; |
| +добавлено | gl_Position = projectionMatrix * viewMatrix * w; |
| +добавлено | }`; |
| +добавлено | |
| +добавлено | export default { |
| +добавлено | id: 'wind', name: 'Wind', key: '6', hue: '#c8f5e4', |
| +добавлено | shape: 'projectile', forms: ['Cyclone', 'Wind Slash'], radius: 0.3, |
| +добавлено | params: { |
| +добавлено | strength: 1, size: 1, drift: 3.5, duration: 3.5, dust: 1, debris: 1, streaks: 1, refraction: 1, |
| +добавлено | slashSpeed: 24, slashSize: 1, tilt: 50, scratch: 1, air: '#dff4ff', dustColor: '#7d6a55', |
| +добавлено | }, |
| +добавлено | ranges: { |
| +добавлено | strength: [0.2, 2.5, 0.01], size: [0.4, 2, 0.01], drift: [0, 10, 0.1], duration: [1, 8, 0.1], |
| +добавлено | dust: [0, 3, 0.01], debris: [0, 3, 0.01], streaks: [0, 3, 0.01], refraction: [0, 3, 0.01], |
| +добавлено | slashSpeed: [6, 50, 0.1], slashSize: [0.4, 2.5, 0.01], tilt: [0, 90, 1], scratch: [0, 2, 0.01], |
| +добавлено | }, |
| +добавлено | presets: { |
| +добавлено | 'dust devil': { size: 0.7, strength: 1.3, dust: 1.8, debris: 0.6, dustColor: '#b4875a', air: '#ffe9cf' }, |
| +добавлено | gale: { size: 1.4, strength: 1.8, streaks: 2, refraction: 1.6, debris: 2.2, slashSize: 1.5, scratch: 1.6 }, |
| +добавлено | 'spirit wind': { air: '#8dffd6', dustColor: '#5f8f84', streaks: 2.4, dust: 0.5, refraction: 1.4 }, |
| +добавлено | }, |
| +добавлено | timing: { charge: 0.9, impact: 0.3, linger: 3.5, sustain: 8 }, |
| +добавлено | kinds: { |
| +добавлено | streak: { style: 'glow', drag: 0.4, curl: 1.2, curlScale: 0.3, forceResponse: 2, life: [0.35, 0.7], size: [0.016, 0.006], stretch: 0.08, emissive: 1.8, color: ['#dff4ff', '#a7c6d4'], alpha: [0.9, 0], fadeIn: 0.2, fadeOut: 0.4 }, |
| +добавлено | mote: { style: 'glow', drag: 1.0, buoyancy: 0.9, curl: 0.9, curlScale: 0.5, forceResponse: 1.2, life: [1.4, 2.4], size: [0.014, 0.007], stretch: 0.012, emissive: 1.1, color: ['#fff0d6', '#d6c6a6'], alpha: [1, 0], fadeIn: 0.25, fadeOut: 0.35 }, |
| +добавлено | dust: { style: 'media', drag: 0.25, buoyancy: 0.2, curl: 0.5, curlScale: 0.25, forceResponse: 2.5, life: [1.1, 1.9], size: [0.35, 1.2], sizePow: 0.6, color: ['#7d6a55', '#8c7760'], alpha: [0.2, 0], density: 2.6, erosion: 0.55, fadeIn: 0.2, fadeOut: 0.4 }, |
| +добавлено | skirt: { style: 'media', drag: 1.2, buoyancy: 0.3, curl: 0.6, curlScale: 0.2, forceResponse: 1.3, life: [1.0, 1.8], size: [0.4, 1.2], color: ['#7d6a55', '#8c7760'], alpha: [0.1625, 0], density: 1.7, erosion: 0.6, fadeIn: 0.15 }, |
| +добавлено | kick: { style: 'media', drag: 2.4, buoyancy: 0.35, curl: 0.6, curlScale: 0.3, life: [0.6, 1.3], size: [0.2, 0.95], color: ['#7d6a55', '#8c7760'], alpha: [0.1125, 0], density: 1.6, erosion: 0.6, fadeIn: 0.05 }, |
| +добавлено | haze: { style: 'media', drag: 0.3, forceResponse: 2.5, life: [0.9, 1.5], size: [0.9, 1.8], alpha: [1, 0], density: 0, distortion: 4.5, fadeIn: 0.25, fadeOut: 0.4 }, |
| +добавлено | leaf: { style: 'debris', mesh: 'leaf', drag: 1.2, gravity: 1.4, curl: 2.5, curlScale: 0.4, forceResponse: 2.4, restitution: 0.1, friction: 0.8, life: [2.5, 4], size: [0.09, 0.09], spin: 7, color: ['#5e5a2c', '#000000'], alpha: [1, 0.75], fadeOut: 0.15 }, |
| +добавлено | pebble: { style: 'debris', mesh: 'rock', drag: 0.15, gravity: 9.8, forceResponse: 2.2, restitution: 0.3, friction: 0.5, life: [1.8, 3], size: [0.07, 0.07], spin: 9, color: ['#7a736b', '#000000'], alpha: [1, 0.9], fadeOut: 0.1 }, |
| +добавлено | // heavy rubble: lifted slowly, swung wide and thrown out of the funnel, lands and tumbles |
| +добавлено | chunk: { style: 'debris', mesh: 'rock', drag: 0.06, gravity: 9.8, forceResponse: 0.75, restitution: 0.22, friction: 0.65, life: [2.6, 3.6], size: [0.15, 0.15], spin: 5, color: ['#6e655b', '#000000'], alpha: [1, 0.92], fadeOut: 0.08 }, |
| +добавлено | // dust dragged off the floor by a passing blade: hangs, spreads and settles |
| +добавлено | sheet: { style: 'media', drag: 3.2, buoyancy: 0.25, curl: 0.9, curlScale: 0.35, forceResponse: 1, life: [0.9, 1.7], size: [0.25, 1.1], sizePow: 0.5, color: ['#7d6a55', '#8c7760'], alpha: [0.16, 0], density: 2.2, erosion: 0.55, fadeIn: 0.04, fadeOut: 0.5 }, |
| +добавлено | }, |
| +добавлено | |
| +добавлено | create(fx) { |
| +добавлено | const S = this, P = fx.particles; |
| +добавлено | const E = { |
| +добавлено | gather: P.emitter('wind.streak', { shape: 'shell', radius: 1.8, spread: 0.4, radial: -3.5 }), |
| +добавлено | motes: P.emitter('wind.mote', { shape: 'disk', radius: 2.2, spread: 0.5 }), |
| +добавлено | focusHaze: P.emitter('wind.haze', { shape: 'sphere', radius: 0.4, spread: 0.5 }), |
| +добавлено | stir: P.emitter('wind.skirt', { shape: 'ring', radius: 1.6, spread: 0.3, radial: -0.8 }), |
| +добавлено | dust: P.emitter('wind.dust', { shape: 'ring', radius: 1, spread: 0.5, radial: 0.4 }), |
| +добавлено | inflow: P.emitter('wind.dust', { shape: 'ring', radius: 3.5, spread: 0.4, radial: -2.2 }), |
| +добавлено | haze: P.emitter('wind.haze', { shape: 'ring', radius: 1, spread: 0.4 }), |
| +добавлено | skirt: P.emitter('wind.skirt', { shape: 'ring', radius: 1.8, spread: 0.5, radial: 1.2 }), |
| +добавлено | leaf: P.emitter('wind.leaf', { shape: 'ring', radius: 1.5, spread: 1.2, radial: -0.5 }), |
| +добавлено | pebble: P.emitter('wind.pebble', { shape: 'ring', radius: 1.5, spread: 1.0 }), |
| +добавлено | grit: P.emitter('wind.pebble', { shape: 'ring', radius: 1.2, spread: 1.2, radial: 0.5 }), |
| +добавлено | chunk: P.emitter('wind.chunk', { shape: 'ring', radius: 1.1, spread: 1.4, radial: 2.5 }), |
| +добавлено | burst: P.emitter('wind.streak', { shape: 'sphere', radius: 0.3, spread: 2, radial: 6 }), |
| +добавлено | sheet: P.emitter('wind.sheet', { shape: 'line', radius: 0.12, spread: 0.5 }), |
| +добавлено | wake: P.emitter('wind.haze', { shape: 'line', radius: 0.1, spread: 0.3 }), |
| +добавлено | kick: P.emitter('wind.kick', { shape: 'sphere', radius: 0.15, spread: 1.2 }), |
| +добавлено | tail: P.emitter('wind.sheet', { shape: 'sphere', radius: 0.12, spread: 0.6 }), |
| +добавлено | chip: P.emitter('wind.pebble', { shape: 'sphere', radius: 0.1, spread: 1.5 }), |
| +добавлено | }; |
| +добавлено | |
| +добавлено | // one ribbon material per pass: refraction sheets / vapour at half res, the blade's crisp edge at full res |
| +добавлено | const ribs = new fx.AnalyticRibbons({ max: 48, segments: 64, pathGLSL: PATH_GLSL, shadeGLSL: SHADE_GLSL, target: 'fx' }); |
| +добавлено | const cores = new fx.AnalyticRibbons({ max: 24, segments: 64, pathGLSL: PATH_GLSL, shadeGLSL: SHADE_GLSL, target: 'lines' }); |
| +добавлено | fx.scenes.fx.add(ribs.mesh); |
| +добавлено | fx.scenes.lines.add(cores.mesh); |
| +добавлено | |
| +добавлено | // funnel marchers, owned by a cast while it lives |
| +добавлено | const dustLin = new THREE.Color(), boxGeo = new THREE.BoxGeometry(2, 2, 2); |
| +добавлено | const vols = Array.from({ length: NV }, () => { |
| +добавлено | const u = { |
| +добавлено | ...fx.U, |
| +добавлено | uTw0: { value: new THREE.Vector4() }, uTw1: { value: new THREE.Vector4() }, uTw2: { value: new THREE.Vector4() }, |
| +добавлено | uTw3: { value: new THREE.Vector4() }, uDust: { value: dustLin }, uBoxMin: { value: new THREE.Vector3() }, |
| +добавлено | uBoxMax: { value: new THREE.Vector3() }, uMarch: { value: new THREE.Vector4() }, |
| +добавлено | }; |
| +добавлено | const m = new THREE.Mesh(boxGeo, new THREE.RawShaderMaterial({ |
| +добавлено | glslVersion: THREE.GLSL3, uniforms: u, ...FX_BLENDING, depthTest: false, side: THREE.BackSide, |
| +добавлено | vertexShader: FX_VERT_HEAD + FUNNEL_VERT, |
| +добавлено | fragmentShader: FX_FRAG_HEAD + NO_GRADIENTS + COMMON_GLSL + NOISE_GLSL + ARENA_GLSL + LIGHTS_GLSL + FX_OUT_GLSL + FUNNEL_GLSL, |
| +добавлено | })); |
| +добавлено | m.visible = false; |
| +добавлено | m.userData.owner = null; |
| +добавлено | fx.scenes.fx.add(m); |
| +добавлено | return m; |
| +добавлено | }); |
| +добавлено | |
| +добавлено | // per-cast state, indexed by the cast record (16), so casting allocates nothing |
| +добавлено | const state = Array.from({ length: 16 }, () => ({ |
| +добавлено | helix: new Array(NH).fill(null), swirl: new Array(NS).fill(null), |
| +добавлено | blades: new Array(NB).fill(null), blade: null, vortex: null, gather: null, light: null, vol: null, |
| +добавлено | hp: new Float32Array(NH * 5), sx: new Float32Array(2), sz: new Float32Array(2), |
| +добавлено | start: new THREE.Vector3(), end: new THREE.Vector3(), base: new THREE.Vector3(), prev: new THREE.Vector3(), |
| +добавлено | vel: new THREE.Vector3(), lean: new THREE.Vector3(), F: new THREE.Vector3(), Sd: new THREE.Vector3(), T: new THREE.Vector3(), |
| +добавлено | tip: new THREE.Vector3(), top: new THREE.Vector3(), lastTip: new THREE.Vector3(), |
| +добавлено | s: 0, dist: 0, env: 0, age: 0, hitT: 0, mask: 0, range: 0, travelled: 0, cy: 0, sign: 1, scratching: false, seedF: 0, tImpact: 0, |
| +добавлено | spin: 0, rise: 0, scourT: 0, scourOn: false, stainT: 0, flingT: 0, |
| +добавлено | })); |
| +добавлено | |
| +добавлено | const air = new THREE.Color(), _a = new THREE.Vector3(), _b = new THREE.Vector3(), _up = new THREE.Vector3(0, 1, 0); |
| +добавлено | const col = { air: '', dust: '' }, tmp = new THREE.Color(); |
| +добавлено | function applyColors(p) { |
| +добавлено | air.set(p.air); |
| +добавлено | if (col.air !== p.air) { |
| +добавлено | col.air = p.air; |
| +добавлено | P.setKind('wind.streak', { color: [p.air, `#${tmp.set(p.air).multiplyScalar(0.72).getHexString()}`] }); |
| +добавлено | } |
| +добавлено | if (col.dust !== p.dustColor) { |
| +добавлено | col.dust = p.dustColor; |
| +добавлено | const dark = `#${tmp.set(p.dustColor).multiplyScalar(1.12).getHexString()}`; |
| +добавлено | for (const k of ['wind.dust', 'wind.skirt', 'wind.kick', 'wind.sheet']) P.setKind(k, { color: [p.dustColor, dark] }); |
| +добавлено | // the volume's albedo: the dust colour, a little greyer (sunset light already tints it) |
| +добавлено | dustLin.set(p.dustColor); dustLin.lerp(tmp.setScalar((dustLin.r + dustLin.g + dustLin.b) / 3), 0.3).multiplyScalar(1.6); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // funnel radius (before size) at normalised height: narrow foot, flared top (FUNNEL_GLSL's wall) |
| +добавлено | const rAt = (hn) => 0.4 + 1.5 * Math.pow(hn, 1.8); |
| +добавлено | const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); }; |
| +добавлено | |
| +добавлено | function releaseAll(st) { |
| +добавлено | for (let i = 0; i < NH; i++) { st.helix[i]?.release(); st.helix[i] = null; } |
| +добавлено | for (let i = 0; i < NS; i++) { st.swirl[i]?.release(); st.swirl[i] = null; } |
| +добавлено | for (let i = 0; i < NB; i++) { st.blades[i]?.release(); st.blades[i] = null; } |
| +добавлено | st.blade?.release(); st.blade = null; |
| +добавлено | st.vortex?.release(); st.vortex = null; |
| +добавлено | st.gather?.release(); st.gather = null; |
| +добавлено | st.light?.release(); st.light = null; |
| +добавлено | if (st.vol) { st.vol.visible = false; st.vol.userData.owner = null; st.vol = null; } |
| +добавлено | } |
| +добавлено | function acquireVol(st) { |
| +добавлено | for (const v of vols) if (!v.userData.owner) { v.userData.owner = st; return v; } |
| +добавлено | return null; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- funnel: frame and look from the cast state --- |
| +добавлено | // cloud 0..1 (debris cloud), reveal (column height fraction), rope 0..1 (dying), dens (density scale) |
| +добавлено | function placeVolume(st, p, cloud, reveal, rope, dens) { |
| +добавлено | const v = st.vol; |
| +добавлено | if (!v) return; |
| +добавлено | const sz = p.size, H = 7 * sz, b = st.base, L = st.lean, u = v.material.uniforms; |
| +добавлено | const sway = 0.35 * sz; |
| +добавлено | u.uTw0.value.set(b.x, b.z, H, st.spin / TAU); |
| +добавлено | u.uTw1.value.set(L.x, L.z, sz, st.rise); |
| +добавлено | u.uTw2.value.set(st.seedF * 7, 3.6 * dens * p.dust, sway, st.age); |
| +добавлено | u.uTw3.value.set(cloud, reveal, 1, rope); |
| +добавлено | // box round the leaning column, its crown and its foot |
| +добавлено | const half = Math.max(rAt(1) * 1.5 + 0.2, 2.0 * 1.4) * sz + sway * 2; |
| +добавлено | const hx = half + Math.abs(L.x) * 0.5, hz = half + Math.abs(L.z) * 0.5, cx = b.x + L.x * 0.5, cz = b.z + L.z * 0.5, top = H * 1.15; |
| +добавлено | u.uBoxMin.value.set(cx - hx, 0, cz - hz); u.uBoxMax.value.set(cx + hx, top, cz + hz); |
| +добавлено | v.position.set(cx, top * 0.5, cz); v.scale.set(hx, top * 0.5, hz); |
| +добавлено | // samples from its size on screen (fx pixels), tier-scaled; ~half fall within one wall radius |
| +добавлено | const dist = Math.max(_a.set(cx, top * 0.4, cz).distanceTo(fx.camera.position), 1); |
| +добавлено | const px = 1.2 * sz * 0.5 * fx.U.uFxResolution.value.y * fx.camera.projectionMatrix.elements[5] / dist; |
| +добавлено | const cap = Math.max(8, Math.min(48, Math.round(32 * fx.Q.volumeSteps / 24 * fx.Q.auto.steps))); |
| +добавлено | u.uMarch.value.set(Math.min(cap, Math.max(12, Math.ceil(px / 3))), 0.8 * sz, 0.35, 1.3 * sz); |
| +добавлено | v.visible = dens > 1e-3; |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- charge: air gathers into the focus; dust starts to whirl where the funnel will touch down --- |
| +добавлено | function charge(c, st, p, dt) { |
| +добавлено | const k = Math.min(1, c.t / c.durations.charge), f = c.from, slash = c.form !== 'Cyclone'; |
| +добавлено | E.gather.pos.copy(f); E.gather.radius = slash ? 1.4 : 1.9; |
| +добавлено | E.gather.rate((slash ? 700 : 260) * p.streaks * (0.3 + 0.7 * k), dt); |
| +добавлено | if (!slash) { E.motes.pos.set(f.x, 0.1, f.z); E.motes.vel.set(0, 1.7, 0); E.motes.rate(250 / c.durations.charge, dt); } |
| +добавлено | E.focusHaze.pos.copy(f); E.focusHaze.size = 0.55; E.focusHaze.rate(12 * p.refraction * k, dt); |
| +добавлено | const G = st.gather; |
| +добавлено | if (G) { |
| +добавлено | if (slash) { G.pos.set(f.x, 0, f.z); G.p[0] = 0.6; G.p[1] = 4 + 8 * k; G.p[2] = 2.5; G.p[3] = 1; G.w = 3.5; } |
| +добавлено | else { G.pos.copy(st.start); G.p[0] = 0.8 * p.size; G.p[1] = (6 + 16 * k) * p.size; G.p[2] = 1.5 * k; G.p[3] = 1.2 * k; G.w = 2; } |
| +добавлено | } |
| +добавлено | for (let i = 0; i < NS; i++) { |
| +добавлено | const r = st.swirl[i]; |
| +добавлено | if (!r) continue; |
| +добавлено | r.p0.set(f.x, f.y, f.z, 2); |
| +добавлено | r.p1.set(1.1 - 0.3 * k, 0.12, 1.25, 0.35); |
| +добавлено | r.p2.set(i * TAU / NS - st.age * 5, 0, 0, 0.14); |
| +добавлено | r.p3.set(i * 0.37 + st.seedF, 0.25 + 1.0 * k, 0.05 * k * p.refraction, 0.06); |
| +добавлено | r.color.copy(air); r.intensity = 0.5 * k * p.streaks; |
| +добавлено | } |
| +добавлено | if (st.light) { st.light.pos.copy(f); st.light.intensity = 3 * k; } |
| +добавлено | if (slash) { blades(c, st, p, f, 0.25 + 0.75 * k, k); return; } |
| +добавлено | // the touchdown point: a ground whirl spinning up (dust drawn in and round, a low swirl of the volume) |
| +добавлено | E.stir.pos.set(st.start.x, 0.12, st.start.z); E.stir.radius = 1.8 * p.size; E.stir.size = 0.8 * p.size; |
| +добавлено | E.stir.rate(70 * p.dust * k, dt); |
| +добавлено | st.spin += dt * 3.4 * p.strength * k; st.rise += dt * 1.5 * k; |
| +добавлено | st.base.copy(st.start); st.lean.set(0, 0, 0); |
| +добавлено | placeVolume(st, p, 0.35 * k * k, 0.06 * k, 0.5 * (1 - k), 0.6 * k); |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- cyclone --- |
| +добавлено | function placeCyclone(c, st, p, dt) { |
| +добавлено | const b = st.base; |
| +добавлено | st.prev.copy(b); |
| +добавлено | b.lerpVectors(st.start, st.end, st.s); |
| +добавлено | const w = Math.sin(Math.PI * st.s) * 0.7 * p.size * Math.sin(st.age * 1.6 + st.seedF * 6); |
| +добавлено | b.addScaledVector(st.Sd, w); |
| +добавлено | if (c.phase !== 'travel') { // parked over the target: a slow wobble |
| +добавлено | const m = smooth(0, 1, c.phase === 'linger' ? c.t + c.durations.impact : c.t); |
| +добавлено | b.x += Math.sin(st.age * 0.8 + st.seedF * 5) * 0.4 * p.size * m; |
| +добавлено | b.z += Math.cos(st.age * 0.65 + st.seedF * 3) * 0.4 * p.size * m; |
| +добавлено | } |
| +добавлено | b.y = 0; |
| +добавлено | if (dt > 0) st.vel.lerp(_a.subVectors(b, st.prev).divideScalar(dt), 1 - Math.exp(-dt * 4)); |
| +добавлено | c.pos.copy(b); |
| +добавлено | } |
| +добавлено | |
| +добавлено | const scour = { x: 0, z: 0, x2: 0, z2: 0, radius: 0.1, width: 0.05, char: 0.2, pattern: 'line', seed: 0 }; |
| +добавлено | const stain = { x: 0, z: 0, radius: 1.5, char: 0.05, pattern: 'noise', seed: 0 }; |
| +добавлено | function cyclone(c, st, p, dt) { |
| +добавлено | const sz = p.size, e = st.env, b = st.base, H = 7 * sz; |
| +добавлено | const V = st.vortex; |
| +добавлено | if (V) { |
| +добавлено | V.pos.copy(b); V.dir.copy(_up); |
| +добавлено | V.p[0] = 1.1 * sz; V.p[1] = 32 * p.strength * sz * e; V.p[2] = 6.5 * p.strength * e; V.p[3] = 0.9 * p.strength * e; |
| +добавлено | V.w = H * 1.1; |
| +добавлено | } |
| +добавлено | // the wall turns and the dust climbs; spun up at touchdown, wound down as it dies |
| +добавлено | st.spin += dt * 3.4 * p.strength * (0.3 + 0.7 * e); |
| +добавлено | st.rise += dt * 3.6 * p.strength * (0.3 + 0.7 * e); |
| +добавлено | // the top lags its motion and sways |
| +добавлено | st.lean.set(-st.vel.x * 0.35 + Math.sin(st.age * 0.9 + st.seedF * 4) * 0.5 * sz, 0, -st.vel.z * 0.35 + Math.cos(st.age * 0.7 + st.seedF * 2) * 0.5 * sz); |
| +добавлено | const grow = smooth(0, 0.7, st.age), dying = c.phase === 'linger' ? smooth(c.durations.linger - 2.2, c.durations.linger, c.t) : 0; |
| +добавлено | placeVolume(st, p, (0.35 + 0.65 * smooth(0, 0.45, st.age)) * (1 - dying * dying), 0.06 + 1.1 * grow, dying, Math.max(0.6, grow) * (1 - dying * 0.85)); |
| +добавлено | // refraction sheets: the helix ribbons, distortion only |
| +добавлено | const reveal = 1.15 * smooth(0, 0.9, st.age), unwind = 1 + (1 - e) * 0.9, omega = 3 * p.strength; |
| +добавлено | for (let i = 0; i < NH; i++) { |
| +добавлено | const o = i * 5, hp = st.hp, h = st.helix[i]; |
| +добавлено | if (!h) continue; |
| +добавлено | h.p0.set(b.x, 0.02, b.z, 0); |
| +добавлено | h.p1.set(hp[o + 3] * sz * (0.7 + 0.3 * e), hp[o + 1] * sz, hp[o + 2] * sz * unwind, -hp[o]); |
| +добавлено | h.p2.set(hp[o + 4] - st.age * omega * (0.8 + 0.25 * hp[o]), st.lean.x, st.lean.z, 0.45 * sz); |
| +добавлено | h.p3.set(hp[o + 4] / TAU, reveal, 0.14 * p.refraction * e, 0); |
| +добавлено | h.color.copy(air); h.intensity = 1e-4; |
| +добавлено | } |
| +добавлено | // what the wind carries |
| +добавлено | const lean = st.lean, k = e * grow; |
| +добавлено | for (let j = 0; j < 2; j++) { |
| +добавлено | const hn = j === 0 ? fx.rand() * 0.25 : fx.rand() * 0.8; |
| +добавлено | E.dust.pos.set(b.x + lean.x * hn * hn, hn * H, b.z + lean.z * hn * hn); |
| +добавлено | E.dust.radius = rAt(hn) * sz * (0.8 + 0.4 * fx.rand()); |
| +добавлено | E.dust.vel.set(0, 1.2, 0); E.dust.size = sz * (0.8 + 0.6 * hn); |
| +добавлено | E.dust.rate(150 * p.dust * k, dt); |
| +добавлено | } |
| +добавлено | // floor dust drawn in along the ground from all round |
| +добавлено | E.inflow.pos.set(b.x, 0.1, b.z); E.inflow.radius = 3.6 * sz; E.inflow.size = 0.9 * sz; |
| +добавлено | E.inflow.rate(90 * p.dust * k, dt); |
| +добавлено | let hn = 0.1 + 0.8 * fx.rand(); |
| +добавлено | E.haze.pos.set(b.x + lean.x * hn * hn, hn * H, b.z + lean.z * hn * hn); |
| +добавлено | E.haze.radius = rAt(hn) * sz * 1.05; E.haze.size = sz; E.haze.vel.set(0, 1, 0); |
| +добавлено | E.haze.rate(45 * p.refraction * e, dt); |
| +добавлено | E.skirt.pos.set(b.x, 0.15, b.z); E.skirt.radius = 2 * sz; E.skirt.size = sz; |
| +добавлено | E.skirt.rate(150 * p.dust * k, dt); // fill-bound: count x area is most of the particles' fx cost |
| +добавлено | // the load: leaves, gravel and rubble lifted round the foot, flung out higher up |
| +добавлено | E.leaf.pos.set(b.x, 0.3, b.z); E.leaf.radius = 1.6 * sz; E.leaf.vel.set(0, 3.5, 0); |
| +добавлено | E.leaf.rate(40 * p.debris * k, dt); |
| +добавлено | E.pebble.pos.set(b.x, 0.2, b.z); E.pebble.radius = 0.9 * sz; E.pebble.vel.set(0, 5, 0); E.pebble.size = 0.7 + 0.8 * fx.rand(); |
| +добавлено | E.pebble.rate(40 * p.debris * k, dt); |
| +добавлено | E.grit.pos.set(b.x, 0.1, b.z); E.grit.radius = 1.3 * sz; E.grit.vel.set(0, 3, 0); E.grit.size = 0.3; |
| +добавлено | E.grit.rate(110 * p.debris * k, dt); |
| +добавлено | st.flingT -= dt; |
| +добавлено | if (st.flingT <= 0 && k > 0.2) { |
| +добавлено | // rubble ripped off the floor in clumps: varied sizes, thrown out of the debris cloud |
| +добавлено | st.flingT = 0.12 + 0.2 * fx.rand(); |
| +добавлено | E.chunk.pos.set(b.x, 0.25, b.z); E.chunk.radius = 1.2 * sz; |
| +добавлено | E.chunk.vel.set(0, 4 + 3 * fx.rand(), 0); |
| +добавлено | E.chunk.size = 0.6 + 1.8 * fx.rand() * fx.rand(); |
| +добавлено | E.chunk.burst(Math.round((2 + 3 * fx.rand()) * p.debris * k)); |
| +добавлено | } |
| +добавлено | // the floor is scoured: two points on the debris ring trace cycloids as the funnel walks |
| +добавлено | st.scourT -= dt; |
| +добавлено | if (st.scourT <= 0) { |
| +добавлено | st.scourT = 0.05; |
| +добавлено | const on = k > 0.3 && p.dust > 0; |
| +добавлено | for (let i = 0; i < 2 && on; i++) { |
| +добавлено | const ang = -st.spin + i * Math.PI + st.seedF * TAU, rr = (1.0 + 0.25 * i) * sz; |
| +добавлено | const x = b.x + Math.cos(ang) * rr, z = b.z + Math.sin(ang) * rr; |
| +добавлено | if (st.scourOn) { |
| +добавлено | scour.x = st.sx[i]; scour.z = st.sz[i]; scour.x2 = x; scour.z2 = z; |
| +добавлено | scour.width = (0.05 + 0.04 * i) * sz; scour.char = 0.16 * Math.min(p.dust, 1.5) * k; scour.seed = (c.seed + st.age * 37) % 100; |
| +добавлено | fx.ground.splat(scour); |
| +добавлено | } |
| +добавлено | st.sx[i] = x; st.sz[i] = z; |
| +добавлено | } |
| +добавлено | st.scourOn = on; |
| +добавлено | } |
| +добавлено | st.stainT -= dt; |
| +добавлено | if (st.stainT <= 0 && k > 0.3) { |
| +добавлено | st.stainT = 0.35; |
| +добавлено | stain.x = b.x; stain.z = b.z; stain.radius = 1.7 * sz; stain.char = 0.035 * Math.min(p.dust, 1.5); stain.seed = (c.seed + st.age * 11) % 100; |
| +добавлено | fx.ground.splat(stain); |
| +добавлено | } |
| +добавлено | // dummies in reach get buffeted round the funnel |
| +добавлено | st.hitT -= dt; |
| +добавлено | if (st.hitT <= 0) { |
| +добавлено | st.hitT = 0.25; |
| +добавлено | for (const dm of fx.dummies) { |
| +добавлено | const dx = dm.pos.x - b.x, dz = dm.pos.z - b.z, d = Math.hypot(dx, dz); |
| +добавлено | if (d < 2.5 * sz) dm.hit(_b.set(dz, 0, -dx), 0.35 * e * p.strength * (1 - d / (2.5 * sz))); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | // the pool's ripple sim swirls when the funnel is over or near it |
| +добавлено | const pool = fx.ARENA.pool; |
| +добавлено | const qx = Math.max(Math.abs(b.x - pool.center.x) - pool.half.x, 0), qz = Math.max(Math.abs(b.z - pool.center.y) - pool.half.y, 0); |
| +добавлено | const pd = Math.hypot(qx, qz), reach = 3 * sz; |
| +добавлено | if (pd < reach && fx.pool) { |
| +добавлено | const cx = Math.min(Math.max(b.x, pool.center.x - pool.half.x), pool.center.x + pool.half.x); |
| +добавлено | const cz = Math.min(Math.max(b.z, pool.center.y - pool.half.y), pool.center.y + pool.half.y); |
| +добавлено | fx.pool.swirl(cx, cz, 3 * e * p.strength * (1 - pd / reach)); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | // --- wind slash --- |
| +добавлено | // crescent frame at centre m with scale k (0..1 while forming); tip = lower tip, top = upper tip |
| +добавлено | function frame(st, p, m, k) { |
| +добавлено | const R = 1.2 * p.slashSize * k, A = 1.2; |
| +добавлено | st.tip.copy(m).addScaledVector(st.F, R * (Math.cos(A) - 1)); |
| +добавлено | st.top.copy(st.tip).addScaledVector(st.T, R * Math.sin(A)); |
| +добавлено | st.tip.addScaledVector(st.T, -R * Math.sin(A)); |
| +добавлено | return R; |
| +добавлено | } |
| +добавлено | // the blade itself is air: a lens edge with a faint vapour sheet, and a thin glint at full res |
| +добавлено | function blades(c, st, p, m, k, glow) { |
| +добавлено | const R = frame(st, p, m, k), sp = p.slashSpeed / 24; |
| +добавлено | for (let i = 0; i < NB; i++) { |
| +добавлено | const r = st.blades[i]; |
| +добавлено | if (!r) continue; |
| +добавлено | const back = c.phase === 'travel' ? i * 0.3 * sp : i * 0.12; |
| +добавлено | r.p0.set(m.x - st.F.x * back, m.y - st.F.y * back, m.z - st.F.z * back, 1); |
| +добавлено | r.p1.set(st.F.x, st.F.y, st.F.z, R * (1 + 0.08 * i)); |
| +добавлено | r.p2.set(st.T.x, st.T.y, st.T.z, 0.5 * p.slashSize * (1 + 0.6 * i) * k); |
| +добавлено | r.p3.set(st.seedF + i * 0.3, 1.2, 0.3 * p.refraction * (1 - 0.25 * i) * glow, 0.07 * Math.pow(0.5, i) * glow); |
| +добавлено | r.color.copy(air); r.intensity = 0.1 * glow * Math.pow(0.45, i) * p.streaks; |
| +добавлено | } |
| +добавлено | const q = st.blade; |
| +добавлено | if (q) { |
| +добавлено | q.p0.set(m.x, m.y, m.z, 1); |
| +добавлено | q.p1.set(st.F.x, st.F.y, st.F.z, R); |
| +добавлено | q.p2.set(st.T.x, st.T.y, st.T.z, 0.03 * p.slashSize * k); |
| +добавлено | q.p3.set(st.seedF, 1.2, 0, 0); |
| +добавлено | q.color.copy(air); q.intensity = 0.7 * glow * p.streaks; |
| +добавлено | } |
| +добавлено | } |
| +добавлено | const scratch = { x: 0, z: 0, x2: 0, z2: 0, radius: 0.1, width: 0.1, crack: 1, char: 0, pattern: 'line', seed: 0 }; |
| +добавлено | const scuff = { x: 0, z: 0, x2: 0, z2: 0, radius: 0.1, width: 0.4, char: 0.12, pattern: 'line', seed: 0 }; |
| +добавлено | function scratchTo(st, p, c) { |
| +добавлено | scratch.x = scuff.x = st.lastTip.x; scratch.z = scuff.z = st.lastTip.z; scratch.x2 = scuff.x2 = st.tip.x; scratch.z2 = scuff.z2 = st.tip.z; |
| +добавлено | scratch.width = 0.07 * p.slashSize; scratch.crack = 1.6 * p.scratch; scratch.char = 0.65 * p.scratch; |
| +добавлено | scratch.seed = scuff.seed = (c.seed + st.travelled * 13) % 100; |
| +добавлено | fx.ground.splat(scratch); |
| +добавлено | // the dust it ploughed aside, either side of the cut |
| +добавлено | scuff.width = 0.4 * p.slashSize; scuff.char = 0.1 * p.scratch; |
| +добавлено | fx.ground.splat(scuff); |
| +добавлено | st.lastTip.copy(st.tip); |
| +добавлено | } |
| +добавлено | function slashFly(c, st, p, dt) { |
| +добавлено | const sp = p.slashSpeed; |
| +добавлено | frame(st, p, c.pos, 1); |
| +добавлено | // the shear line: floor dust dragged up the blade's lower half and left hanging in its wake |
| +добавлено | E.sheet.vel.copy(st.F).multiplyScalar(sp * 0.35).setY(0.6); |
| +добавлено | E.sheet.pos.copy(st.tip); E.sheet.p.lerpVectors(st.tip, c.pos, 0.8); E.sheet.size = p.slashSize; |
| +добавлено | E.sheet.rate(260 * p.dust, dt); |
| +добавлено | E.sheet.pos.copy(c.pos); E.sheet.p.copy(st.top); E.sheet.size = 0.6 * p.slashSize; |
| +добавлено | E.sheet.rate(50 * p.dust, dt); |
| +добавлено | // shimmer left along the whole edge |
| +добавлено | E.wake.vel.copy(st.F).multiplyScalar(sp * 0.15); |
| +добавлено | E.wake.pos.copy(st.tip); E.wake.p.copy(st.top); E.wake.size = 0.45 * p.slashSize; |
| +добавлено | E.wake.rate(70 * p.refraction, dt); |
| +добавлено | if (st.tip.y < 0.2) { |
| +добавлено | // the lower tip scores the floor: a rooster tail of dust and chips, a cut in the crack channel |
| +добавлено | E.tail.pos.copy(st.tip).setY(0.06); |
| +добавлено | E.tail.vel.copy(st.F).multiplyScalar(sp * 0.12).addScaledVector(st.Sd, -1.6 * st.sign).setY(2.2); |
| +добавлено | E.tail.size = 0.9 * p.slashSize; E.tail.rate(220 * p.dust, dt); |
| +добавлено | E.kick.pos.copy(st.tip).setY(0.08); |
| +добавлено | E.kick.vel.copy(st.F).multiplyScalar(sp * 0.05).addScaledVector(st.Sd, 1.4 * st.sign).setY(0.8); |
| +добавлено | E.kick.size = p.slashSize; E.kick.rate(140 * p.dust, dt); |
| +добавлено | E.chip.pos.copy(E.tail.pos); E.chip.vel.copy(st.F).multiplyScalar(sp * 0.2).setY(3.6); |
| +добавлено | E.chip.size = 0.5 + fx.rand(); E.chip.rate(70 * p.debris, dt); |
| +добавлено | if (!st.scratching) { st.scratching = true; st.lastTip.copy(st.tip); } |
| +добавлено | else if (st.lastTip.distanceToSquared(st.tip) > 0.49) scratchTo(st, p, c); |
| +добавлено | } |
| +добавлено | // shear through dummies |
| +добавлено | for (let i = 0; i < fx.dummies.length; i++) { |
| +добавлено | const dm = fx.dummies[i]; |
| +добавлено | if (st.mask & (1 << i)) continue; |
| +добавлено | if (Math.hypot(dm.pos.x - c.pos.x, dm.pos.z - c.pos.z) < 0.7) { |
| +добавлено | st.mask |= 1 << i; |
| +добавлено | dm.hit(st.F, 1.2 * p.slashSize); |
| +добавлено | E.tail.pos.copy(dm.pos).setY(0.7); E.tail.vel.copy(st.F).multiplyScalar(4); E.tail.size = p.slashSize; E.tail.burst(50 * p.dust); |
| +добавлено | E.chip.pos.copy(dm.pos).setY(0.8); E.chip.vel.copy(st.F).multiplyScalar(6).setY(2); E.chip.size = 0.8; E.chip.burst(18 * p.debris); |
| +добавлено | E.burst.pos.copy(dm.pos).setY(c.pos.y); E.burst.vel.copy(st.F).multiplyScalar(5); E.burst.burst(30 * p.streaks); |
| +добавлено | fx.shocks?.spawn({ pos: _a.copy(dm.pos).setY(0.9), radius0: 0.1, radius1: 1.6 * p.slashSize, duration: 0.3, thickness: 0.4, distortion: 0.06 * p.refraction, intensity: 0 }); |
| +добавлено | fx.rig.shake(0.2, 14); |
| +добавлено | if (!fx.reduced) fx.rig.hitstop(0.03); |
| +добавлено | } |
| +добавлено | } |
| +добавлено | } |
| +добавлено | |
| +добавлено | return { |
| +добавлено | onParam(k) { if (k === 'air' || k === 'dustColor') applyColors(S.params); }, |
| +добавлено | tick() { ribs.update(); cores.update(); }, |
| +добавлено | |
| +добавлено | phase(c, name) { |
| +добавлено | const p = S.params, st = state[c.index]; |
| +добавлено | applyColors(p); |
| +добавлено | if (name === 'charge') { |
| +добавлено | releaseAll(st); |
| +добавлено | const slash = c.form !== 'Cyclone'; |
| +добавлено | c.durations.charge = slash ? 0.35 : 0.9; |
| +добавлено | c.durations.impact = slash ? 0.15 : 0.3; |
| +добавлено | c.durations.linger = slash ? 1.0 : p.duration; |
| +добавлено | c.durations.travel = 8; |
| +добавлено | st.age = 0; st.env = 0; st.s = 0; st.mask = 0; st.hitT = 0; st.travelled = 0; st.scratching = false; |
| +добавлено | st.spin = 0; st.rise = 0; st.scourT = 0; st.scourOn = false; st.stainT = 0; st.flingT = 0; |
| +добавлено | st.seedF = (c.seed % 997) / 997; st.sign = c.seed & 1 ? 1 : -1; |
| +добавлено | st.F.set(c.to.x - c.from.x, 0, c.to.z - c.from.z); |
| +добавлено | st.dist = st.F.length(); |
| +добавлено | if (st.dist < 1e-3) st.F.set(0, 0, -1); else st.F.divideScalar(st.dist); |
| +добавлено | st.Sd.crossVectors(st.F, _up).normalize(); |
| +добавлено | const tl = THREE.MathUtils.degToRad(p.tilt); |
| +добавлено | st.T.copy(_up).multiplyScalar(Math.cos(tl)).addScaledVector(st.Sd, Math.sin(tl) * st.sign); |
| +добавлено | st.gather = fx.forces.acquire(); |
| +добавлено | if (st.gather) { st.gather.type = 1; st.gather.dir.copy(_up); } |
| +добавлено | for (let i = 0; i < NS; i++) st.swirl[i] = ribs.acquire(); |
| +добавлено | if (slash) { for (let i = 0; i < NB; i++) st.blades[i] = ribs.acquire(); st.blade = cores.acquire(); } |
| +добавлено | else { |
| +добавлено | // where the funnel will touch down: a little in front of the caster |
| +добавлено | const lead = Math.min(2, st.dist * 0.3); |
| +добавлено | st.start.set(c.from.x + st.F.x * lead, 0, c.from.z + st.F.z * lead); |
| +добавлено | st.end.set(c.to.x, 0, c.to.z); |
| +добавлено | st.base.copy(st.start); |
| +добавлено | st.vol = acquireVol(st); |
| +добавлено | } |
| +добавлено | st.light = fx.lights.acquire(); |
| +добавлено | if (st.light) { st.light.range = slash ? 6 : 5; st.light.intensity = 0; st.light.color.copy(air); } |
| +добавлено | } else if (name === 'release') { |
| +добавлено | st.gather?.release(); st.gather = null; |
| +добавлено | for (let i = 0; i < NS; i++) { st.swirl[i]?.release(); st.swirl[i] = null; } |
| +добавлено | st.age = 0; |
| +добавлено | fx.rig.shake(0.12, 9); |
| +добавлено | if (c.form === 'Cyclone') { |
| +добавлено | st.light?.release(); st.light = null; |
| +добавлено | st.dist = st.start.distanceTo(st.end); |
| +добавлено | st.base.copy(st.start); st.prev.copy(st.start); st.vel.set(0, 0, 0); |
| +добавлено | c.pos.copy(st.start); |
| +добавлено | st.vortex = fx.forces.acquire(); |
| +добавлено | if (st.vortex) st.vortex.type = 1; |
| +добавлено | for (let i = 0; i < NH; i++) { |
| +добавлено | const o = i * 5; |
| +добавлено | st.hp[o] = 2.6 + 1.0 * fx.rand(); // turns |
| +добавлено | st.hp[o + 1] = 0.35 + 0.25 * fx.rand(); // foot radius |
| +добавлено | st.hp[o + 2] = 1.9 + 0.9 * fx.rand(); // top radius |
| +добавлено | st.hp[o + 3] = 5.5 + 2.0 * fx.rand(); // height |
| +добавлено | st.hp[o + 4] = (i + 0.3 * fx.rand()) * TAU / NH; // angle |
| +добавлено | st.helix[i] = ribs.acquire(); |
| +добавлено | } |
| +добавлено | // touchdown: the debris cloud erupts off the floor |
| +добавлено | E.skirt.pos.set(st.start.x, 0.15, st.start.z); E.skirt.radius = 1.4 * p.size; E.skirt.size = p.size; E.skirt.burst(140 * p.dust); |
| +добавлено | E.chunk.pos.set(st.start.x, 0.2, st.start.z); E.chunk.radius = 0.8 * p.size; E.chunk.vel.set(0, 5, 0); E.chunk.size = 1; E.chunk.burst(10 * p.debris); |
| +добавлено | E.grit.pos.set(st.start.x, 0.1, st.start.z); E.grit.vel.set(0, 4, 0); E.grit.size = 0.35; E.grit.burst(60 * p.debris); |
| +добавлено | fx.shocks?.spawn({ pos: _a.set(st.start.x, 0.05, st.start.z), radius0: 0.3, radius1: 4.5 * p.size, duration: 0.6, thickness: 0.6, distortion: 0.05 * p.refraction, ground: true, intensity: 0 }); |
| +добавлено | } else { |
| +добавлено | c.pos.copy(c.from); |
| +добавлено | st.cy = 1.2 * p.slashSize * Math.sin(1.2) * Math.cos(THREE.MathUtils.degToRad(p.tilt)) - 0.06; |
| +добавлено | st.range = st.dist + 6; |
| +добавлено | // the throw: a pressure pulse off the caster, floor dust blown ahead |
| +добавлено | E.kick.pos.set(c.from.x, 0.1, c.from.z); E.kick.vel.copy(st.F).multiplyScalar(6).setY(0.6); E.kick.size = 1.2; E.kick.burst(50 * p.dust); |
| +добавлено | E.burst.pos.copy(c.from); E.burst.vel.copy(st.F).multiplyScalar(9); E.burst.burst(30 * p.streaks); |
| +добавлено | fx.shocks?.spawn({ pos: c.from, radius0: 0.2, radius1: 2.2, duration: 0.3, thickness: 0.5, distortion: 0.05 * p.refraction, intensity: 0 }); |
| +добавлено | if (!fx.reduced) fx.rig.punch(1.5, 0.08); |
| +добавлено | } |
| +добавлено | } else if (name === 'impact') { |
| +добавлено | st.tImpact = 0; |
| +добавлено | if (c.form === 'Cyclone') { |
| +добавлено | const b = st.base; |
| +добавлено | E.skirt.pos.set(b.x, 0.15, b.z); E.skirt.radius = 1.8 * p.size; E.skirt.size = p.size; E.skirt.burst(160 * p.dust); |
| +добавлено | E.pebble.pos.set(b.x, 0.05, b.z); E.pebble.size = 1; E.pebble.burst(24 * p.debris); |
| +добавлено | fx.shocks?.spawn({ pos: _a.set(b.x, 0.05, b.z), radius0: 0.5, radius1: 5.5 * p.size, duration: 0.7, thickness: 0.7, distortion: 0.05 * p.refraction, ground: true, intensity: 0 }); |
| +добавлено | fx.rig.shake(0.25, 7); |
| +добавлено | } else { |
| +добавлено | frame(st, p, c.pos, 1); |
| +добавлено | if (st.scratching) scratchTo(st, p, c); |
| +добавлено | // the blade bursts: the air it carried slams on, dust blooms |
| +добавлено | E.tail.pos.copy(c.pos); E.tail.vel.copy(st.F).multiplyScalar(5); E.tail.size = 1.3 * p.slashSize; E.tail.burst(90 * p.dust); |
| +добавлено | E.kick.pos.copy(st.tip).setY(0.2); E.kick.vel.set(0, 1, 0); E.kick.burst(50 * p.dust); |
| +добавлено | E.burst.pos.copy(c.pos); E.burst.vel.copy(st.F).multiplyScalar(4); E.burst.burst(50 * p.streaks); |
| +добавлено | fx.shocks?.spawn({ pos: c.pos, radius0: 0.2, radius1: 2.6 * p.slashSize, duration: 0.35, thickness: 0.5, distortion: 0.08 * p.refraction, intensity: 0 }); |
| +добавлено | fx.rig.shake(0.18, 12); |
| +добавлено | } |
| +добавлено | } else if (name === 'done') releaseAll(st); |
| +добавлено | }, |
| +добавлено | |
| +добавлено | // own integration for both forms: the funnel walks, the crescent skims the floor |
| +добавлено | travel(c, dt) { |
| +добавлено | const p = S.params, st = state[c.index]; |
| +добавлено | st.age += dt; |
| +добавлено | if (c.form === 'Cyclone') { |
| +добавлено | st.s = Math.min(1, st.s + p.drift * dt / Math.max(st.dist, 0.1)); |
| +добавлено | placeCyclone(c, st, p, dt); |
| +добавлено | return st.s >= 1 || c.t > c.durations.travel; |
| +добавлено | } |
| +добавлено | const step = p.slashSpeed * dt; |
| +добавлено | c.vel.copy(st.F).multiplyScalar(p.slashSpeed); |
| +добавлено | c.pos.addScaledVector(st.F, step); |
| +добавлено | c.pos.y += (st.cy - c.pos.y) * (1 - Math.exp(-dt * 14)); |
| +добавлено | st.travelled += step; |
| +добавлено | const R = Math.hypot(c.pos.x, c.pos.z); |
| +добавлено | let hit = R > fx.ARENA.radius - 0.8; |
| +добавлено | for (const pl of fx.ARENA.pillars) if (Math.hypot(pl.x - c.pos.x, pl.z - c.pos.z) < fx.ARENA.pillarRadius + 0.5) hit = true; |
| +добавлено | return hit || st.travelled >= st.range || c.t > c.durations.travel; |
| +добавлено | }, |
| +добавлено | |
| +добавлено | update(c, dt) { |
| +добавлено | const p = S.params, st = state[c.index]; |
| +добавлено | if (c.phase === 'charge') { st.age += dt; charge(c, st, p, dt); return; } |
| +добавлено | if (c.form === 'Cyclone') { |
| +добавлено | if (c.phase !== 'travel') { st.age += dt; placeCyclone(c, st, p, dt); } |
| +добавлено | st.env = c.phase === 'linger' ? 1 - smooth(c.durations.linger - 1.3, c.durations.linger, c.t) : smooth(0, 0.8, st.age); |
| +добавлено | cyclone(c, st, p, dt); |
| +добавлено | return; |
| +добавлено | } |
| +добавлено | if (c.phase === 'travel') { |
| +добавлено | blades(c, st, p, c.pos, 1, 1); |
| +добавлено | slashFly(c, st, p, dt); |
| +добавлено | if (st.light) { st.light.pos.copy(c.pos); st.light.intensity = 0.8; } |
| +добавлено | } else { |
| +добавлено | // the blade overshoots, spreads and thins out |
| +добавлено | st.tImpact += dt; |
| +добавлено | const t = st.tImpact; |
| +добавлено | c.pos.addScaledVector(st.F, p.slashSpeed * Math.exp(-t * 7) * dt); |
| +добавлено | blades(c, st, p, c.pos, 1 + 1.4 * t, Math.exp(-t * 5)); |
| +добавлено | if (st.light) { st.light.pos.copy(c.pos); st.light.intensity = 0.8 * Math.exp(-t * 6); } |
| +добавлено | } |
| +добавлено | }, |
| +добавлено | }; |
| +добавлено | }, |
| +добавлено | }; |