Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-3d

Source of release 0.9.2

0.9.2 / shaders / fog.frag

// A fog volume's slab, ray-marched (src/renderers/fog_volume.lucb draws them).
//
// The volume is a dense box of texels, its z slices tiled side by side in one
// rgba16_float atlas (image 1): r the density, g the optical depth toward the key
// light, b the optical depth toward the sky (+Y), both integrated through the
// densities on the CPU when the volume was made, a the emission field. Image 2 is a
// strip of 256 texels: the emitted color along the look's emission range (an
// emission color ramp, or one color). The box is
// drawn as view-aligned slabs, back to front; each fragment of a slab marches its
// stretch of the eye ray, from the slab's plane to the next one's, front to back,
// and emits that stretch's color and coverage premultiplied, so the slabs composite
// "over" one another and over the scene. Slabs are depth tested without writing
// depth: a mesh in front hides them, and they fog what lies behind.
//
// The vertex color is the fragment's position on the slab's plane in the box's
// coordinates (0..1 across it); distances along the ray are in world units through
// `scale`, the world length of the box's three axes.
#version 450
layout(location = 0) in vec4 vertex_color;
layout(location = 0) out vec4 fragment_color;
layout(push_constant) uniform Params {
    vec4 eye;       // the eye in box coordinates; w the slab thickness in world units, along the view axis
    vec4 forward;   // the view axis as box coordinates' depth gradient (depth = dot(p - eye, forward)); w steps per slab
    vec4 scale;     // world length of the box's x, y and z; w the density scale
    vec4 key;       // the key light's color times intensity; w the ambient (sky) intensity
    vec4 smoke;     // the smoke's scattering color; w the shadow scale
    vec4 glow;      // x the emission scale; the strip's texel for emission e is at clamp((e - y) * z, 0, 1); w unused
    vec4 atlas;     // texels across the box: x, y, z; w atlas columns
    vec4 size;      // the atlas's width and height in texels; zw unused
} params;
layout(set = 0, binding = 1) uniform sampler2D volume;
layout(set = 0, binding = 2) uniform sampler2D strip;

const float pi = 3.14159265358979;

// The box's texels at `q` (0..1 across the box): bilinear within a z slice of the
// atlas (texel centers clamped inside the slice's tile, so tiles never bleed), and
// linear between the two nearest slices.
vec4 texels(vec3 q) {
    vec3 t = q * params.atlas.xyz - 0.5;
    vec2 xy = clamp(t.xy, vec2(0.0), params.atlas.xy - 1.0) + 0.5;
    float z = clamp(t.z, 0.0, params.atlas.z - 1.0);
    float z0 = floor(z);
    float z1 = min(z0 + 1.0, params.atlas.z - 1.0);
    float columns = params.atlas.w;
    vec2 tile0 = vec2(mod(z0, columns), floor(z0 / columns)) * params.atlas.xy;
    vec2 tile1 = vec2(mod(z1, columns), floor(z1 / columns)) * params.atlas.xy;
    vec4 a = textureLod(volume, (tile0 + xy) / params.size.xy, 0.0);
    vec4 b = textureLod(volume, (tile1 + xy) / params.size.xy, 0.0);
    return mix(a, b, z - z0);
}

// The light emitted per world unit at emission field value `e`: the emission scale
// times the value, in the strip's color for it (texel centers 0.5/256 .. 255.5/256).
vec3 emission(float e) {
    if (e <= 0.0 || params.glow.x <= 0.0) {
        return vec3(0.0);
    }
    float t = clamp((e - params.glow.y) * params.glow.z, 0.0, 1.0);
    vec3 color = textureLod(strip, vec2((t * 255.0 + 0.5) / 256.0, 0.5), 0.0).rgb;
    return color * (params.glow.x * e);
}

// Interleaved gradient noise (Jimenez 2014): a per-pixel offset in 0..1 that
// turns step banding into fine noise.
float jitter(vec2 pixel) {
    return fract(52.9829189 * fract(dot(pixel, vec2(0.06711056, 0.00583715))));
}

void main() {
    vec3 start = vertex_color.xyz;
    vec3 ray = start - params.eye.xyz;
    float depth = dot(ray, params.forward.xyz);
    if (depth <= 0.0) {
        discard;
    }
    // The ray is eye + ray * s; the slab's plane is s = 1, the next plane s = 1 + T / depth.
    float steps = params.forward.w;
    float ds = params.eye.w / depth / steps;
    float span = length(ray * params.scale.xyz);
    float step_length = span * ds;
    float density_scale = params.scale.w;
    float shadow_scale = params.smoke.w * density_scale;
    vec3 key = params.key.rgb;
    float ambient = params.key.w;
    vec3 radiance = vec3(0.0);
    float transmittance = 1.0;
    float offset = jitter(gl_FragCoord.xy);
    for (float i = 0.0; i < steps; i += 1.0) {
        vec3 q = params.eye.xyz + ray * (1.0 + (i + offset) * ds);
        if (any(lessThan(q, vec3(0.0))) || any(greaterThan(q, vec3(1.0)))) {
            continue;
        }
        vec4 s = texels(q);
        float sigma = s.r * density_scale;
        vec3 emitted = emission(s.a);
        if (sigma <= 0.0 && emitted == vec3(0.0)) {
            continue;
        }
        float alpha = 1.0 - exp(-sigma * step_length);
        vec3 lit = params.smoke.rgb * (key * exp(-s.g * shadow_scale) + ambient * exp(-s.b * density_scale));
        radiance += transmittance * (alpha * lit + emitted * step_length);
        transmittance *= 1.0 - alpha;
    }
    float coverage = 1.0 - transmittance;
    if (coverage <= 0.0 && radiance == vec3(0.0)) {
        discard;
    }
    fragment_color = vec4(radiance, coverage);
}