RustToDust/client/gpu_sparks_process.gdshader

154 lines
4.6 KiB
Text

shader_type particles;
render_mode disable_velocity;
global uniform sampler3D rtd_flow_field;
global uniform vec3 rtd_flow_origin;
global uniform vec3 rtd_flow_inv_extent;
float tri(float x) {
return abs(fract(x) - 0.5);
}
vec3 tri3(vec3 p) {
return vec3(
tri(p.z + tri(p.y)),
tri(p.z + tri(p.x)),
tri(p.y + tri(p.x))
);
}
vec3 safeNorm(vec3 v) {
float l2 = dot(v, v);
return l2 > 1e-8 ? v * inversesqrt(l2) : vec3(0.0, 1.0, 0.0);
}
vec3 aetheriaCurl(vec3 pos, float flowScale, float scrollSpeed, float t) {
vec3 field = pos / max(0.5, flowScale);
float s1 = t * scrollSpeed;
float s2 = t * scrollSpeed * 1.61803398875;
vec3 t1 = safeNorm(tri3(field - vec3(0.0, s1, 0.0)));
vec3 t2 = safeNorm(tri3(field * 1.61803398875 - vec3(0.0, s2, 0.0)));
return cross(t1, t2);
}
uint hashU32(uint x) {
x ^= x >> 16u;
x *= 0x7FEB352Du;
x ^= x >> 15u;
x *= 0x846CA68Bu;
x ^= x >> 16u;
return x;
}
float hash01(uint x) {
return float(hashU32(x) & 0x00FFFFFFu) / 16777216.0;
}
void start() {
ACTIVE = true;
float pType = floor(CUSTOM.w + 0.001);
if (pType > 2.5) {
// Type 3: MuzzleStarburst stays stationary at muzzle
VELOCITY = vec3(0.0);
TRANSFORM[0].xyz = vec3(1.0, 0.0, 0.0);
TRANSFORM[1].xyz = vec3(0.0, 1.0, 0.0);
TRANSFORM[2].xyz = vec3(0.0, 0.0, 1.0);
} else {
uint seed = INDEX ^ (NUMBER * 747796405u) ^ floatBitsToUint(TIME);
float r0 = hash01(seed + 1u);
float r1 = hash01(seed + 17u);
float r2 = hash01(seed + 53u);
float r3 = hash01(seed + 131u);
float r4 = hash01(seed + 257u);
float r5 = hash01(seed + 509u);
// Wide log-normal speed dispersion (0.25x..2.8x) + 3D directional chaos to shatter radial shell fans
float gauss = (r0 + r1 - 1.0) * 1.45;
float speedMult = clamp(exp(gauss * 0.72), 0.25, 2.80);
float baseSpeed = length(VELOCITY);
vec3 dirJitter = vec3(r2 * 2.0 - 1.0, r4 * 2.0 - 1.0, r5 * 2.0 - 1.0);
VELOCITY = VELOCITY * speedMult + dirJitter * (baseSpeed * 0.30);
// Sub-frame / burst-window birth offset (0..55ms) with immediate curl + gravity advection
float dt0 = r3 * 0.055;
vec3 curl0 = aetheriaCurl(TRANSFORM[3].xyz, 3.0, 0.90, TIME);
if (pType < 0.5) {
VELOCITY += curl0 * (18.0 * dt0);
VELOCITY.y -= 11.0 * dt0;
} else if (pType < 1.5) {
VELOCITY += curl0 * (14.0 * dt0);
VELOCITY.y += 2.6 * dt0;
} else {
VELOCITY += curl0 * (10.0 * dt0);
VELOCITY.y -= 1.2 * dt0;
}
TRANSFORM[3].xyz += VELOCITY * dt0;
CUSTOM.y = min(CUSTOM.z * 0.35, dt0 * 0.65);
TRANSFORM[0].xyz = VELOCITY;
TRANSFORM[1].xyz = vec3(0.0, 1.0, 0.0);
TRANSFORM[2].xyz = vec3(0.0, 0.0, 1.0);
}
}
void process() {
float age = CUSTOM.y + DELTA;
float maxLife = max(0.01, CUSTOM.z);
if (age >= maxLife) {
ACTIVE = false;
} else {
CUSTOM.y = age;
float pType = floor(CUSTOM.w + 0.001);
float drag = fract(CUSTOM.w) * 20.0;
if (pType > 2.5) {
// Type 3: MuzzleStarburst (stationary HDR flash)
VELOCITY = vec3(0.0);
TRANSFORM[0].xyz = vec3(1.0, 0.0, 0.0);
TRANSFORM[1].xyz = vec3(0.0, 1.0, 0.0);
TRANSFORM[2].xyz = vec3(0.0, 0.0, 1.0);
} else {
vec3 worldPos = TRANSFORM[3].xyz;
vec3 flowUv = (worldPos - rtd_flow_origin) * rtd_flow_inv_extent;
vec4 flowSample = vec4(0.0);
if (all(greaterThanEqual(flowUv, vec3(0.0))) && all(lessThanEqual(flowUv, vec3(1.0)))) {
flowSample = textureLod(rtd_flow_field, flowUv, 0.0);
}
vec3 flowVel = flowSample.rgb;
float shockP = flowSample.a;
vec3 curl = aetheriaCurl(worldPos, 3.2, 0.85, TIME);
if (pType < 0.5) {
// Type 0: VelocitySpark (ballistic + wispy Tri3D curl streams + flow drag + shock kick)
VELOCITY += (flowVel - VELOCITY) * min(1.0, drag * DELTA);
VELOCITY += curl * (22.0 * DELTA);
VELOCITY.y -= 11.0 * DELTA;
if (shockP > 0.20) {
VELOCITY += flowVel * (shockP * 0.55 * DELTA);
}
} else if (pType < 1.5) {
// Type 1: BuoyantEmber (upward buoyancy + swirling Tri3D thermal streams)
VELOCITY += (flowVel - VELOCITY) * min(1.0, drag * DELTA);
VELOCITY += curl * (15.5 * DELTA);
VELOCITY.y += 3.0 * DELTA;
if (shockP > 0.20) {
VELOCITY += flowVel * (shockP * 0.70 * DELTA);
}
} else {
// Type 2: DustMote (heavily flow-entrained ground grit + curl wisps)
float dustDrag = max(drag, 3.2);
VELOCITY += (flowVel - VELOCITY) * min(1.0, dustDrag * DELTA);
VELOCITY += curl * (11.0 * DELTA);
VELOCITY.y -= 1.2 * DELTA;
}
TRANSFORM[3].xyz += VELOCITY * DELTA;
// Pass world VELOCITY to gpu_sparks_draw.gdshader cleanly via MODEL_MATRIX[0]
TRANSFORM[0].xyz = VELOCITY;
TRANSFORM[1].xyz = vec3(0.0, 1.0, 0.0);
TRANSFORM[2].xyz = vec3(0.0, 0.0, 1.0);
}
}
}