Culling

Slide a single tree across the camera boundary. Keep an eye on the count as it enters and leaves.
Interactive demo#Camera#Camera
Not drawing what the camera can’t see.
A level may hold thousands of objects, but only a few dozen are on screen. The game checks each one against the camera rectangle and skips the rest. The squares count which trees are being drawn.
Code
namespace culling_lesson;
import stage;
import std;
import math;
export "culling" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
artwork("draft");
let zoom = W / 420;
let camera = if hover { clamp(px / zoom - 80, 0.0, 260.0) } else { 260 * pingpong(time * 0.22) };
let left = camera - 8;
let right = camera + W + 8;
save();
translate(0, 14);
scale(zoom, zoom);
artwork("valley", 0, 0, 420, 150);
rect(0, 0, camera, 150, "rgba(12,22,39,0.68)");
rect(camera + W, 0, 420 - camera - W, 150, "rgba(12,22,39,0.68)");
world(110, left, right);
actor(camera + 80, 110, walk(time), 2, "base", "director");
restore();
box(camera * zoom, 14, W * zoom, 150 * zoom, MAG);
for x, i in [30, 95, 150, 215, 262, 330, 395] {
rect(50 + i * 9, 84, 6, 6, if x > left && x < right { MOSS } else { LINE });
}
}

Comments
No comments yet.