Move horizontally to steer.

0:01 / 0:30
Playback speed

Mode 7 racing floor

Raise the horizon setting. How does the amount of road you can see change?

Interactive demo#TilesAndWorldsF-ZEROSNES era

Sample a flat course at a different scale on each row to make it appear to recede toward the horizon.

F-ZERO used the SNES Mode 7 background for its course. Here each scanline maps to a distance on a flat track. Rows near the horizon span more of the map per pixel. Rotate the sample coordinates to steer, and add a forward offset to drive along the surface.

Code

src/demos/mode_seven.goose
namespace mode_seven_lesson;
import stage;
import std;
import math;

var pixels: u8[64000] = [0;64000];

export "mode-seven" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
    load_texture("ridge");
    let steer = if hover { (px - 80) / 45 } else { sin(time * 0.45) * 0.6 };
    let horizon = number_control("horizon", 32) as i64;
    let cs = cos(steer);
    let sn = sin(steer);
    for y in 100 {
        for x in 160 {
            if y <= horizon {
                let sky = texel(x, y * 80 / horizon);
                put_rgb(pixels[..], x, y, sky[0], sky[1], sky[2]);
                continue;
            }
            let forward = 900.0 / (y - horizon);
            let side = (x - 80) * forward / 86;
            let u = side * cs + forward * sn;
            let v = forward * cs - side * sn + time * 32;
            let lane = abs(u - 9 * sin(v / 38));
            let stripe = (floor(v / 8) as i64) % 2 == 0;
            let gravel = abs(floor(u * 2) + floor(v * 3)) % 4.0;
            var rgb = [0.0;3];
            if lane < 0.45 && stripe { rgb = [247.0, 222.0, 158.0]; }
            else if lane < 16.4 {
                let tar = 48 + gravel * 1.8 + 3 * sin(u * 0.7);
                rgb = [tar, tar + 9, tar + 15];
            } else if lane < 17 { rgb = [238.0, 217.0, 172.0]; }
            else if lane < 20 { rgb = if stripe { [185.0, 82.0, 57.0] } else { [234.0, 205.0, 156.0] }; }
            else if lane < 22 { rgb = [150.0 + gravel * 3, 125.0, 84.0]; }
            else {
                let field = (abs(floor(u / 17) + floor(v / 24)) as i64) % 2;
                rgb = [68.0 + field * 9 + gravel, 117.0 + field * 12, 95.0 + field * 3];
            }
            let haze = min(0.62, forward / 330.0);
            put_rgb(pixels[..], x, y, mix_value(rgb[0], 150, haze), mix_value(rgb[1], 156, haze), mix_value(rgb[2], 155, haze));
        }
    }
    pixel_buffer(pixels[..], 160, 100);
}

Stage drawing helpers · All demos on GitHub

Comments

No comments yet.