Depth fog

Find the density where the far columns disappear but the nearest row still reads.
Interactive demo#LightAndGlowABZÛINSIDE3D lightingPS2 era
Distant surfaces take on more of the fog colour than nearby ones.
These three rows of projected columns sit at different depths. For each visible surface, exponential fog uses its distance and a density setting to blend stone into the colour of the air. The nearby character stays dark. Move the camera sideways to separate the rows by parallax as well as tone.
Code
namespace depth_fog_lesson;
import stage;
import std;
import math;
var pixels: u8[64000] = [0;64000];
export "depth-fog" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
load_texture("hall");
let density = number_control("density", 0.21);
let drift = if hover { (px - 80) / 6 } else { 12 * sin(time * 0.4) };
for y in 100 {
for x in 160 {
// Project sets of columns at three depths. The nearest covering layer wins.
var depth = 15.0;
var stone = 0.20;
for layer in 3 {
let z = [8.0, 4.0, 2.0][layer];
let col = (x + 190 + drift / z + layer * 12) % (110 / z);
let base = 54 + 42 / z;
let foot = y > base - 10 / z;
let arch_y = 37 - 15 / z;
let span = min(col, 110 / z - col) * z;
let arch = abs(hypot(span - 27, (y - arch_y) * z) - 23) < 3.5 && y < arch_y;
let post = col < (if foot { 20.0 } else { 12.0 }) / z;
if post || arch || y >= base {
depth = z;
stone = if y >= base { 0.34 + 0.06 * cos((y - base) * 0.65) }
else if arch { 0.24 } else { 0.10 + 0.21 * max(0.0, 1 - col * z / 8) };
}
}
let iron = texel(x, y);
stone += iron[1] / 270.0;
let air = 1.0 - exp(-depth * density);
put_rgb(pixels[..], x, y, mix_value(94 * stone, 105 + iron[0] * 0.24, air),
mix_value(120 * stone, 136 + iron[1] * 0.22, air), mix_value(132 * stone, 150 + iron[2] * 0.22, air));
}
}
pixel_buffer(pixels[..], 160, 100);
actor(65 + drift / 2, 78, walk(time), 1.65, "base", "runner");
}

Comments
No comments yet.