diff --git a/CMakeLists.txt b/CMakeLists.txt index da238f4..8924361 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3733,7 +3733,7 @@ add_tslang_example(golden_line core/golden_line_world.ts core/golden_line_trees. core/golden_line_tunnel.ts core/golden_line_portal.ts core/golden_line_houses.ts core/golden_line_station.ts core/golden_line_signs.ts core/golden_line_shadowfit.ts core/golden_line_shadows.ts core/golden_line_puffs.ts - core/golden_line_plume.ts core/golden_line_steam.ts core/golden_line_birds.ts core/golden_line_motes.ts core/golden_line_bloom.ts) + core/golden_line_plume.ts core/golden_line_steam.ts core/golden_line_birds.ts core/golden_line_motes.ts core/golden_line_bloom.ts core/golden_line_shots.ts) add_tslang_example_test(golden_line) add_tslang_example_test(golden_line SUFFIX cab ARGS -shot cab) add_tslang_example_test(golden_line SUFFIX seat ARGS -shot seat) diff --git a/core/golden_line_plume.ts b/core/golden_line_plume.ts index 63e1f50..5eea5fa 100644 --- a/core/golden_line_plume.ts +++ b/core/golden_line_plume.ts @@ -10,8 +10,8 @@ // ground shadow reads). Computes in doubles as the JavaScript does (the Float32Array maps rounded where it // stores them), so the same frames give the same puffs (checked against the original's dumps). // -// The original's drone and inspect shots (frame.shotId) are not ported: frame.aerialShot stands for "the shot -// is the drone or the inspect camera", which our app never sets, so `aerial` stays 0. +// frame.aerialShot: the shot is the drone chase or a still (-eye) camera (the original's shotId 'drone' or +// 'inspect'), from which the swollen tail of the plume is seen. // // Imported with `import { Plume, PlumeFrame } from "../core/golden_line_plume";` (from examples/). @@ -422,7 +422,7 @@ export class PlumeFrame { forwardY = 0.0; forwardZ = 1.0; cut: int = 0; // counts the camera's cuts - aerialShot = false; // the drone or inspect shot (never, in our app) + aerialShot = false; // the drone chase or a still camera interior = false; // the camera rides inside the train tunnelFactor = 0.0; // how deep the camera is in a bore originZ = 0.0; // the floating origin diff --git a/core/golden_line_shots.ts b/core/golden_line_shots.ts new file mode 100644 index 0000000..7557c16 --- /dev/null +++ b/core/golden_line_shots.ts @@ -0,0 +1,1216 @@ +// The Golden Line's camera shots: a port of golden-line's src/camera/shots.js and the manual half of +// src/camera/cameraman.js (github.com/StarKnightt/golden-line, MIT License, Copyright (c) 2026 Prasenjit +// (StarKnightt)). +// +// Six rigs: the drone chase (and the composed opening take), the camera beside the rails by a telegraph +// pole, the viaduct seen small in its valley, the way out of a tunnel, the window seat and the driver's +// cab. Each one knows when the world suits it (fits), where to send the train if it doesn't (place), and +// how to move while it is on screen (update, writing a ShotPose). All positions are true world +// coordinates; the window seat and the cab answer in their vehicle's own frame (ShotPose.vehicle), which +// the app turns into the world with the train. The Cameraman holds the running rig and composes shots as +// the original's capture tools do (compose: the same random draws, then the shot played `at` seconds in, +// frame by frame at 60 Hz, as the original's composeNow steps it). The director (cutting between shots on +// its own, sending the train down the line) is not ported yet. +// +// Imported with `import { Cameraman, ShotOptions, ShotPose, SHOT_DRONE, ... } from "../core/golden_line_shots";`. + +import { + World, Track, Terrain, Rng, Biome, Feature, hashFloat, hypot2, hypot3, lerp, smoothstep, saturate, + SEG_TUNNEL, SEG_VIADUCT, LINE_GROUND, LINE_TUNNEL, NO_WATER, +} from "./golden_line_world"; +import { skSin, skCos, skTan, skAtan, skAtan2, skExp } from "./golden_line_structkit"; +import { CONSIST, VEHICLE_SPECS, TRAIN_GAP, COACH_WINDOW_CENTRES } from "./golden_line_wheels"; +import { LS_MILE_QUARTER, LS_POLE_SPACING } from "./golden_line_lineside"; + +export const SHOT_DRONE: int = 0; +export const SHOT_TRACKSIDE: int = 1; +export const SHOT_BRIDGE: int = 2; +export const SHOT_INTERIOR: int = 3; +export const SHOT_TUNNEL: int = 4; +export const SHOT_CAB: int = 5; +export const SHOT_NAMES: string[] = ["drone", "trackside", "bridge", "interior", "tunnel", "cab"]; +// [minTime, maxTime]: how long a shot runs (the director's cut; drawn on every shot all the same) +const SH_TIMES: number[] = [7.0, 10.0, 7.2, 9.0, 7.5, 10.0, 6.5, 9.5, 8.2, 9.6, 8.0, 10.0]; + +const SH_PI = 3.141592653589793; +const SH_CAMERA_COACH: int = 3; // consist.js's CAMERA_COACH +const SH_RAIL_JOINT = 18.3; // consist.js's RAIL_JOINT +const SH_COACH_FLOOR_Y = 1.28; // consist.js's COACH.floorY +const SH_CHASE_ANCHOR = 5.0; // metres behind the front buffer: the point the chase holds on the third +const SH_POLE_AHEAD = 17.0; // the trackside camera sets up this far beyond a telegraph pole +const SH_CAB_EYE: number[] = [1.74, 3.18, -4.92]; +const SH_CAB_AIM: number[] = [0.9, 2.05, 6.0]; + +/** The train's length (consist.js's TRAIN_LENGTH). */ +export function shTrainLength(): number { + let sum = -TRAIN_GAP; + for (let i = 0; i < CONSIST.length; i++) { + sum += VEHICLE_SPECS[CONSIST[i]][0] + TRAIN_GAP; + } + return sum; +} + +function shClamp(v: number, a: number, b: number): number { + return v < a ? a : v > b ? b : v; +} + +function smootherstep(e0: number, e1: number, v: number): number { + const t = saturate((v - e0) / (e1 - e0)); + return t * t * t * (t * (t * 6.0 - 15.0) + 10.0); +} + +// JS's Math.round (halves toward +infinity) +function shRound(v: number): number { + return Math.floor(v + 0.5); +} + +function shSet(v: number): boolean { + return v == v; +} + +// log(1 + x) (only for the tunnel take's soft floor) +function shLog1p(x: number): number { + return Math.log(1.0 + x); +} + +/** What a rig is asked for (the original's opts); NaN or an empty list: not given, so drawn or defaulted. */ +@final +export class ShotOptions { + seed = NaN; + side = NaN; + variant = NaN; + bay = NaN; + back = NaN; + lat0 = NaN; + lat1 = NaN; + up0 = NaN; + up1 = NaN; + along0 = NaN; + along1 = NaN; + yaw0 = NaN; + yaw1 = NaN; + dist0 = NaN; + dist1 = NaN; + rise = NaN; + markX = NaN; + markY = NaN; + fov = NaN; + clear = NaN; + crop = NaN; + sight = NaN; + place: number[] = []; // the composed take: [engine x, horizon y] at the start and end + clearPoles: number[] = []; // a stretch of the pole line [z0, z1] to leave out + clearPolesSet = false; // clearPoles given (an empty list given means none) +} + +/** Where the camera is this frame. */ +@final +export class ShotPose { + // eye and target: true world, or (vehicle >= 0) in that vehicle's own frame + px = 0.0; + py = 0.0; + pz = 0.0; + tx = 0.0; + ty = 0.0; + tz = 1.0; + vehicle: int = -1; // -1: world; else the up is that vehicle's + fov = 40.0; + near = 0.5; + roll = 0.0; // radians about the line of sight (three's rotateZ after lookAt) +} + +/** The ride as the rigs see it: the engine's front buffer (arc length) and its speed. */ +@final +export class ShotContext { + sFront = 0.0; + speed = 18.0; +} + +// --- Shared queries (shots.js's helpers) -------------------------------------------------------------- + +/** A point on the line (track.js's frameAt; as golden_line_train.ts's, kept here so the rigs need no GPU module). */ +@final +class ShFrame { + x = 0.0; y = 0.0; z = 0.0; + tx = 0.0; ty = 0.0; tz = 1.0; + sx = 1.0; sz = 0.0; +} + +function shFrameAt(track: Track, s: number, out: ShFrame): void { + const z = track.zOfS(s); + const dx = track.slopeX(z), dy = track.slopeY(z); + const len = hypot3(dx, dy, 1.0); + out.x = track.X(z); out.y = track.Y(z); out.z = z; + out.tx = dx / len; out.ty = dy / len; out.tz = 1.0 / len; + const hl = hypot2(dx, 1.0); + out.sx = 1.0 / hl; out.sz = -dx / hl; +} + +/** A point beside the line: `back` metres behind arc length s, `lateral` toward +x, `height` above the rail (consist.js's pointAlong). */ +function shPointAlong(track: Track, f: ShFrame, s: number, back: number, lateral: number, height: number, out: number[]) { + shFrameAt(track, s - back, f); + out[0] = f.x + f.sx * lateral; + out[1] = f.y + height; + out[2] = f.z + f.sz * lateral; +} + +function shInTunnelBetween(terrain: Terrain, s0: number, s1: number): boolean { + const track = terrain.track; + for (let s = s0; s <= s1; s += 24.0) { + if (terrain.lineStateAt(track.zOfS(s)) == LINE_TUNNEL) { + return true; + } + } + return terrain.lineStateAt(track.zOfS(s1)) == LINE_TUNNEL; +} + +/** True when no ground rises into the sight line. */ +function shLineClear(terrain: Terrain, fx: number, fy: number, fz: number, tx: number, ty: number, tz: number, margin: number, samples: int): boolean { + for (let i = 1; i < samples; i++) { + const t = i / samples; + const x = lerp(fx, tx, t), y = lerp(fy, ty, t), z = lerp(fz, tz, t); + if (terrain.height(x, z, false) > y - margin * (1.0 - t)) { + return false; + } + } + return true; +} + +/** Extra height needed to clear a forest canopy at z. */ +function shCanopyAt(terrain: Terrain, z: number): number { + const b = terrain.biomeAt(z); + return Math.max(b.pine, b.tunnel * 0.8) * 13.0; +} + +/** The first segment of `type` past z whose [span start or end] passes the test; -1 if none in six. */ +function shNextSegment(terrain: Terrain, type: int, z: number, exitEnd: boolean, beyond: number, lead: number): int { + const layout = terrain.track.layout; + const track = terrain.track; + let from = z; + for (let i = 0; i < 6; i++) { + const index = layout.nextOfType(type, from); + const seg = layout.segments[index]; + let z0 = 0.0, z1 = 0.0; + if (type == SEG_TUNNEL) { + const f = terrain.tunnelSpan(seg); + z0 = f.spanA; + z1 = f.spanB; + } else { + const f = terrain.bridgeSpan(seg); + z0 = f.spanA; + z1 = f.spanB; + } + const s = track.sOfZ(exitEnd ? z1 : z0) + lead; + if (s > beyond) { + return index; + } + from = seg.zc + 1.0; + } + return -1; +} + +// --- The drone chase ----------------------------------------------------------------------------------- + +function shDroneFits(terrain: Terrain, c: ShotContext): boolean { + return !shInTunnelBetween(terrain, c.sFront - 90.0, c.sFront + c.speed * 10.0 + 45.0); +} + +/** Two ways of flying. A composed take (`place` given: the opening) rides behind the engine and holds it + * and the horizon on chosen marks. Every other take chases from the front quarter, 30 to 45 degrees off + * square and drifting about that angle, so the line runs diagonally across the frame with the engine + * nearest, on a third, and the coaches running back into the country behind it. */ +@final +class DroneRig { + side = -1.0; + place: number[] = []; + back = 13.0; + lat0 = 19.0; + lat1 = 17.0; + up0 = 4.5; + up1 = 4.0; + along0 = -15.5; + along1 = -18.0; + swing0 = 0.0; + swing1 = 0.0; + reach0 = 0.0; + reach1 = 0.0; + rise = 0.0; + markX = 0.0; + markY = 0.0; + fov = 0.0; + seed = 0.0; + clear = 5.5; + crop = 0.0; + sight = 3.2; + lift = NaN; + clearPoles: number[] = []; + private f = new ShFrame(); + private p: number[] = [0.0, 0.0, 0.0]; + + constructor(terrain: Terrain, c: ShotContext, rng: Rng, o: ShotOptions) { + this.side = shSet(o.side) ? o.side : rng.chance(0.7) ? -1.0 : 1.0; + for (let i = 0; i < o.place.length; i++) { + this.place.push(o.place[i]); + } + this.back = shSet(o.back) ? o.back : 13.0; + this.lat0 = shSet(o.lat0) ? o.lat0 : 19.0; + this.lat1 = shSet(o.lat1) ? o.lat1 : 17.0; + this.up0 = shSet(o.up0) ? o.up0 : 4.5; + this.up1 = shSet(o.up1) ? o.up1 : 4.0; + this.along0 = shSet(o.along0) ? o.along0 : -15.5; + this.along1 = shSet(o.along1) ? o.along1 : -18.0; + this.swing0 = shSet(o.yaw0) ? o.yaw0 : rng.range(33.0, 42.0); + this.swing1 = shSet(o.yaw1) ? o.yaw1 : shClamp(this.swing0 + rng.range(-6.0, 6.0), 32.0, 43.0); + this.reach0 = shSet(o.dist0) ? o.dist0 : rng.range(37.0, 47.0); + this.reach1 = shSet(o.dist1) ? o.dist1 : this.reach0 * rng.range(0.9, 1.07); + this.rise = shSet(o.rise) ? o.rise : rng.range(0.215, 0.285); + this.markX = shSet(o.markX) ? o.markX : rng.range(0.63, 0.68); + this.markY = shSet(o.markY) ? o.markY : rng.range(0.62, 0.67); + this.fov = shSet(o.fov) ? o.fov : rng.range(32.0, 37.0); + this.seed = rng.range(0.0, 100.0); + this.clear = shSet(o.clear) ? o.clear : 5.5; + this.crop = shSet(o.crop) ? o.crop : 0.0; + this.sight = shSet(o.sight) ? o.sight : 3.2; + // A pole whose wires are too fine to see stood by the engine like a stake, so a chase leaves the + // telegraph line out over the ground it covers. It is whole again as soon as the view cuts away. + if (o.clearPolesSet) { + for (let i = 0; i < o.clearPoles.length; i++) { + this.clearPoles.push(o.clearPoles[i]); + } + } else if (this.place.length == 0) { + const z0 = terrain.track.zOfS(c.sFront); + this.clearPoles.push(z0 - 320.0); + this.clearPoles.push(z0 + c.speed * 12.0 + 160.0); + } + } + + update(terrain: Terrain, c: ShotContext, dt: number, t: number, out: ShotPose) { + const track = terrain.track; + const noise = track.noise; + const f = this.f; + const p = this.p; + const composed = this.place.length == 4; + const u = 0.5 - 0.5 * skCos(SH_PI * shClamp(t / 11.0, 0.0, 1.0)); + const drift = Math.max(0.0, t - 11.0); + // A composed take is only known to be safe where it was composed. Left running (manual mode), it + // climbs, swings out and drops back to an ordinary chase, clear of platforms, roofs and trees, + // with the whole engine in the frame. + const retire = composed ? smoothstep(0.0, 14.0, drift) : 0.0; + if (composed) { + const a = lerp(this.along0, this.along1, u) + 14.0 * skSin(drift * 0.07) - 13.0 * retire; + const l = lerp(this.lat0, this.lat1, u) + 3.0 * skSin(drift * 0.053) + 9.0 * retire; + const h = lerp(this.up0, this.up1, u) + 1.1 * skSin(drift * 0.041) + 6.5 * retire; + shPointAlong(track, f, c.sFront, this.back - a, this.side * l, h, p); + out.px = p[0]; + out.py = p[1]; + out.pz = p[2]; + shPointAlong(track, f, c.sFront, 6.3, 0.0, 2.2, p); + out.tx = p[0]; + out.ty = p[1]; + out.tz = p[2]; + } else { + // Flown in the engine's own frame, so the angle holds however the line bends. + const wave = 3.5 * skSin(t * 0.19 + this.seed) * smoothstep(0.0, 3.0, t); + const off = (shClamp(lerp(this.swing0, this.swing1, u) + wave, 31.0, 45.0) * SH_PI) / 180.0; + const dist = lerp(this.reach0, this.reach1, u) + 5.0 * skSin(drift * 0.057); + shFrameAt(track, c.sFront - SH_CHASE_ANCHOR, f); + let flat = hypot2(f.tx, f.tz); + if (flat == 0.0) { + flat = 1.0; + } + const ahead = dist * skSin(off), abeam = this.side * dist * skCos(off); + out.tx = f.x; + out.ty = f.y + 2.6; + out.tz = f.z; + out.px = f.x + (f.tx / flat) * ahead + f.sx * abeam; + out.py = f.y + 2.6 + dist * this.rise + 1.4 * skSin(drift * 0.043); + out.pz = f.z + (f.tz / flat) * ahead + f.sz * abeam; + } + const sway = composed ? 0.4 : 1.0; + out.px += 0.28 * sway * noise.noise1(t * 0.31, this.seed); + out.py += 0.2 * sway * noise.noise1(t * 0.27, this.seed + 1.0); + + // Stay above ground and canopy, and rise until the train is actually visible. + const floor = terrain.height(out.px, out.pz, false) + this.clear + shCanopyAt(terrain, out.pz); + let need = Math.max(0.0, floor - out.py); + if (this.crop > 0.0 && retire < 1.0) { + // Where the ground rises toward the line, standing wheat would swallow the wheels: look over it. + // Only ever a small lift, and only while the composed move lasts (later the engine may be deep + // in a tunnel, where this line of sight runs through a whole hill). + shPointAlong(track, f, c.sFront, 8.0, 0.0, 0.35, p); + let over = 0.0; + for (let i = 3; i <= 14; i++) { + const s = i / 18.0; + const top = terrain.height(lerp(out.px, p[0], s), lerp(out.pz, p[2], s), false) + this.crop; + over = Math.max(over, (top - p[1] * s) / (1.0 - s) - out.py); + } + need = Math.max(need, Math.min(over, 2.5) * (1.0 - retire)); + } + let py = out.py + need; + for (let i = 0; i < 7 && !shLineClear(terrain, out.px, py, out.pz, out.tx, out.ty, out.tz, this.sight, 18); i++) { + py += 5.0; + need += 5.0; + } + if (!shSet(this.lift)) { + this.lift = need; + } else { + this.lift = this.lift + (need - this.lift) * (1.0 - skExp(-dt * (need > this.lift ? 2.4 : 0.7))); + } + out.py += this.lift; + + // Turn the lens so the engine lands on its mark. + const tanV = skTan((this.fov * SH_PI) / 360.0), tanH = tanV * (16.0 / 9.0); + const ex = composed ? lerp(this.place[0], this.place[2], u) : this.side < 0.0 ? this.markX : 1.0 - this.markX; + const ax = out.tx - out.px, ay = out.ty - out.py, az = out.tz - out.pz; + const yaw = skAtan2(ax, az) + skAtan((ex * 2.0 - 1.0) * tanH); // +yaw turns the lens left + const level = skAtan2(ay, hypot2(ax, az)); + let pitch = 0.0; + if (composed) { + // The composed take holds the horizon where it was asked for; as it retires it holds the + // engine instead, however high the ground or the trees have made it climb. + const hz = lerp(this.place[1], this.place[3], u); + pitch = lerp(skAtan((hz * 2.0 - 1.0) * tanV), level + skAtan((2.0 * 0.63 - 1.0) * tanV), retire); + } else { + pitch = level + skAtan((2.0 * this.markY - 1.0) * tanV); + } + const cp = skCos(pitch); + out.tx = skSin(yaw) * cp * 40.0 + out.px; + out.ty = skSin(pitch) * 40.0 + out.py; + out.tz = skCos(yaw) * cp * 40.0 + out.pz; + out.fov = this.fov; + out.near = 0.6; + } +} + +// --- Beside the rails ---------------------------------------------------------------------------------- + +/** One side of the line a trackside camera can stand on. */ +@final +class TrackSpot { + constructor(public side: number, public ground: number) { + } +} + +/** The sides of the line at arc length s a camera can stand on (none: an empty list). */ +function shTracksideSpot(terrain: Terrain, f: ShFrame, s: number): TrackSpot[] { + const track = terrain.track; + const options: TrackSpot[] = []; + const z = track.zOfS(s); + for (let dz = -26.0; dz <= 26.0; dz += 13.0) { + if (terrain.lineStateAt(z + dz) != LINE_GROUND) { + return options; + } + } + // A post right under the lens would loom over the whole frame. + if (Math.abs(s - shRound(s / LS_MILE_QUARTER) * LS_MILE_QUARTER) < 7.0) { + return options; + } + shFrameAt(track, s, f); + const fx = f.x, fy = f.y, fz = f.z, fsx = f.sx, fsz = f.sz; + const to: number[] = [0.0, 0.0, 0.0]; + for (let side = -1.0; side <= 1.0; side += 2.0) { + const x = fx + fsx * side * 4.3, zz = fz + fsz * side * 4.3; + const ground = terrain.height(x, zz, true); + const level = terrain.waterLevelAt(x, zz); + if (ground > fy + 0.15 || ground < fy - 2.6 || (level != NO_WATER && ground < level + 0.2)) { + continue; + } + // Not inside a station platform, and not standing on the level crossing. + if (terrain.townInfluence(x, zz)) { + const d = x - track.X(zz); + const plan = terrain.townPlan; + if (plan) { + const sign = d > 0.0 ? 1 : d < 0.0 ? -1 : 0; + if (sign == plan.side && Math.abs(zz - plan.zc) < 92.0 / 2.0 + 8.0) { + continue; + } + if (Math.abs(zz - (plan.zc + 118.0)) < 6.0) { + continue; + } + } + } + // The approach has to be visible too. + shPointAlong(track, f, s - 45.0, 0.0, 0.0, 0.0, to); + if (!shLineClear(terrain, x, Math.max(ground + 0.7, fy - 0.1), zz, to[0], to[1] + 2.4, to[2], 0.5, 20)) { + continue; + } + options.push(new TrackSpot(side, ground)); + } + return options; +} + +// The camera sets up a little way beyond a telegraph pole, so that one stands near in the frame, clear +// of the engine, with its insulators and the wires swinging away from it. +function shBesidePole(terrain: Terrain, f: ShFrame, s: number): number { + const track = terrain.track; + const z = track.zOfS(s); + const nearest: int = shRound((z - SH_POLE_AHEAD) / LS_POLE_SPACING); + const ks: int[] = [nearest, nearest + 1, nearest - 1]; + for (let i = 0; i < 3; i++) { + const k = ks[i]; + const spot = track.sOfZ(k * LS_POLE_SPACING + (hashFloat(k, 301, 0) - 0.5) * 4.0 + SH_POLE_AHEAD); + // never so much closer than asked that the engine is upon the camera as soon as the shot opens + if (spot - s > -30.0 && spot - s < LS_POLE_SPACING && shTracksideSpot(terrain, f, spot).length > 0) { + return spot; + } + } + return s; +} + +function shTracksideLead(speed: number): number { + return Math.max(9.0, speed) * 3.0 + 8.0; +} + +/** Where the trackside camera will stand for a train whose front is at sFront now. */ +export function shTracksideSpotFor(terrain: Terrain, sFront: number, speed: number): number { + const f = new ShFrame(); + return shBesidePole(terrain, f, sFront + shTracksideLead(speed)); +} + +function shTracksideFits(terrain: Terrain, c: ShotContext): boolean { + const f = new ShFrame(); + return c.speed > 3.0 && shTracksideSpot(terrain, f, c.sFront + shTracksideLead(c.speed)).length > 0; +} + +@final +class TracksideRig { + spot = 0.0; + side = -1.0; + ground = 0.0; + height = 0.0; + lateral = 0.0; + seed = 0.0; + primed = false; + private target: number[] = [0.0, 0.0, 0.0]; + private aim: number[] = [0.0, 0.0, 0.0]; + private p: number[] = [0.0, 0.0, 0.0]; + private f = new ShFrame(); + + constructor(terrain: Terrain, c: ShotContext, rng: Rng) { + const spot = shBesidePole(terrain, this.f, c.sFront + shTracksideLead(c.speed)); + this.seed = rng.range(0.0, 100.0); + this.arm(terrain, rng, spot); + } + + arm(terrain: Terrain, rng: Rng, s: number) { + const track = terrain.track; + this.spot = s; + let options = shTracksideSpot(terrain, this.f, s); + if (options.length == 0) { + options = [new TrackSpot(-1.0, track.Y(track.zOfS(s)) - 0.8)]; + } + // (options.find(o => o.side === -1 && rng.chance(0.72)) || rng.pick(options)) + let chosen = -1; + for (let i = 0; i < options.length && chosen < 0; i++) { + if (options[i].side == -1.0 && rng.chance(0.72)) { + chosen = i; + } + } + if (chosen < 0) { + chosen = rng.pick(options.length); + } + this.side = options[chosen].side; + this.ground = options[chosen].ground; + shFrameAt(track, s, this.f); + this.height = Math.max(this.ground - this.f.y + 0.55, -0.22) + rng.range(0.0, 0.55); + this.lateral = rng.range(3.7, 4.5); + this.primed = false; + } + + /** Manual mode: once the train is well past, set up again further down the line. */ + rearm(terrain: Terrain, rng: Rng, c: ShotContext): boolean { + if (c.sFront - shTrainLength() > this.spot + 55.0) { + const s = shBesidePole(terrain, this.f, c.sFront + shTracksideLead(c.speed)); + if (shTracksideSpot(terrain, this.f, s).length > 0) { + this.arm(terrain, rng, s); + return true; + } + } + return false; + } + + update(terrain: Terrain, c: ShotContext, dt: number, t: number, out: ShotPose) { + const track = terrain.track; + const noise = track.noise; + const f = this.f; + const p = this.p; + const aim = this.aim; + const target = this.target; + const length = shTrainLength(); + shPointAlong(track, f, this.spot, 0.0, this.side * this.lateral, this.height, p); + out.px = p[0]; + out.py = p[1]; + out.pz = p[2]; + const gap = this.spot - c.sFront; // metres until the engine draws level + const w = smoothstep(17.0, -9.0, gap); + shPointAlong(track, f, c.sFront, 1.2, 0.0, 2.35, aim); + const back = shClamp(c.sFront - this.spot + 8.5, 2.0, length - 1.5); + shPointAlong(track, f, c.sFront, back, 0.0, 2.15, p); + for (let k = 0; k < 3; k++) { + aim[k] += (p[k] - aim[k]) * w; + } + if (!this.primed) { + for (let k = 0; k < 3; k++) { + target[k] = aim[k]; + } + this.primed = true; + } + const k9 = 1.0 - skExp(-dt * 9.0); + for (let k = 0; k < 3; k++) { + target[k] += (aim[k] - target[k]) * k9; + } + out.tx = target[0]; + out.ty = target[1]; + out.tz = target[2]; + + // The ground shakes as the engine arrives and keeps drumming while the coaches pass. + const abreast = smoothstep(26.0, 4.0, gap) * smoothstep(length + 22.0, length - 6.0, -gap); + const amp = abreast * Math.min(1.0, c.speed / 18.0) * (0.55 + 0.45 * smoothstep(14.0, 0.0, Math.abs(gap))); + const seed = this.seed; + out.px += 0.013 * amp * noise.noise1(t * 23.0, seed); + out.py += 0.017 * amp * noise.noise1(t * 27.0, seed + 1.0); + out.tx += 0.045 * amp * noise.noise1(t * 19.0, seed + 2.0); + out.ty += 0.05 * amp * noise.noise1(t * 21.0, seed + 3.0); + out.roll = 0.006 * amp * noise.noise1(t * 17.0, seed + 4.0); + out.fov = lerp(37.0, 55.0, w); + out.near = 0.15; + } +} + +// --- The viaduct, seen small in a big valley ----------------------------------------------------------- + +/** The viaduct about arc length s ([segment index, deck start z, deck end z]), or an empty list. */ +function shViaductNear(terrain: Terrain, s: number): number[] { + const track = terrain.track; + const layout = track.layout; + const z = track.zOfS(s); + const list = layout.inRange(z - 700.0, z + 700.0); + for (let k = 0; k < list.length; k++) { + const seg = layout.segments[list[k]]; + if (seg.type != SEG_VIADUCT) { + continue; + } + const span = terrain.bridgeSpan(seg); + const a = span.spanA, b = span.spanB; + const s0 = track.sOfZ(a), s1 = track.sOfZ(b); + if (s > s0 + 45.0 && s < s1 + 35.0) { + return [list[k], a, b]; + } + } + return []; +} + +function shBridgeFits(terrain: Terrain, c: ShotContext): boolean { + return shViaductNear(terrain, c.sFront).length > 0; +} + +@final +class BridgeRig { + private pos: number[] = [0.0, 0.0, 0.0]; + private target: number[] = [0.0, 0.0, 0.0]; + private toTarget: number[] = [0.0, 0.0, 0.0]; + private right: number[] = [0.0, 0.0, 0.0]; + private fov = 40.0; + private drift = 1.0; + + constructor(terrain: Terrain, c: ShotContext, rng: Rng) { + const track = terrain.track; + const layout = track.layout; + let hit = shViaductNear(terrain, c.sFront); + if (hit.length == 0) { + const next = layout.segments[layout.nextOfType(SEG_VIADUCT, track.zOfS(c.sFront))]; + hit = shViaductNear(terrain, track.sOfZ(terrain.bridgeSpan(next).spanA) + 120.0); + } + const seg = layout.segments[hit[0]]; + const v = terrain.features(seg); + const floorY = v.floorY, depth = v.depth, deckY = v.deckY; + const a = hit[1], b = hit[2]; + const mid = (a + b) / 2.0; + const deck: number[] = []; + const shares: number[] = [0.12, 0.32, 0.5, 0.68, 0.88]; + for (let i = 0; i < 5; i++) { + const z = lerp(a, b, shares[i]); + deck.push(track.X(z)); + deck.push(track.Y(z) - 3.0); + deck.push(z); + } + // Scout the valley for a viewpoint that sees the whole span, and from which the train on the deck + // stands clear against the sky and the haze rather than against a dark wooded hillside. + let bestScore = 0.0; + let found = false; + const sides: number[] = [-1.0, 1.0]; + const dzs: number[] = [-190.0, -125.0, -70.0, 70.0, 125.0, 190.0]; + const lats: number[] = [255.0, 320.0, 395.0]; + for (let si = 0; si < 2; si++) { + const side = sides[si]; + for (let di = 0; di < 6; di++) { + for (let li = 0; li < 3; li++) { + const lat = lats[li]; + const x = track.X(mid) + side * lat, z = mid + dzs[di]; + const ground = terrain.height(x, z, false); + const y = Math.max(floorY + 24.0 + 18.0 * rng.next(), ground + 9.0); + let seen = 0, open = 0; + for (let d = 0; d < 5; d++) { + const dx = deck[d * 3], dy = deck[d * 3 + 1], dz = deck[d * 3 + 2]; + if (!shLineClear(terrain, x, y, z, dx, dy, dz, 4.0, 30)) { + continue; + } + seen++; + if (shOpenBehind(terrain, x, y, z, dx, dy, dz)) { + open++; + } + } + const score = seen * 3.0 + open * 1.1 - Math.max(0.0, y - deckY - 8.0) * 0.08 + (side == -1.0 ? 1.4 : 0.4) + + rng.next() * 1.6 - Math.abs(lat - 320.0) * 0.004; + if (!found || score > bestScore) { + found = true; + bestScore = score; + this.pos[0] = x; + this.pos[1] = y; + this.pos[2] = z; + } + } + } + } + const target = this.target; + target[0] = track.X(mid); + target[1] = floorY + depth * 0.68; + target[2] = mid; + const pos = this.pos; + const ddx = target[0] - pos[0], ddy = target[1] - pos[1], ddz = target[2] - pos[2]; + const dist = Math.sqrt(ddx * ddx + ddy * ddy + ddz * ddz); + const hfov = 2.0 * skAtan(((b - a) * 0.68) / dist); + this.fov = shClamp((2.0 * skAtan(skTan(hfov / 2.0) / (16.0 / 9.0)) * 180.0) / SH_PI, 25.0, 44.0); + const tt = this.toTarget; + let len = Math.sqrt(ddx * ddx + ddy * ddy + ddz * ddz); + if (len == 0.0) { + len = 1.0; + } + tt[0] = ddx / len; + tt[1] = ddy / len; + tt[2] = ddz / len; + // right = toTarget x (0, 1, 0), normalised + const rx = -tt[2], rz = tt[0]; + let rl = Math.sqrt(rx * rx + rz * rz); + if (rl == 0.0) { + rl = 1.0; + } + this.right[0] = rx / rl; + this.right[1] = 0.0; + this.right[2] = rz / rl; + this.drift = rng.sign(); + } + + update(terrain: Terrain, c: ShotContext, dt: number, t: number, out: ShotPose) { + const u = smootherstep(0.0, 1.0, t / 13.0); + const pos = this.pos, tt = this.toTarget, r = this.right; + const a = 26.0 * u, d = this.drift * 16.0 * u; + out.px = pos[0] + tt[0] * a + r[0] * d; + out.py = pos[1] + tt[1] * a + r[1] * d + 2.5 * u; + out.pz = pos[2] + tt[2] * a + r[2] * d; + out.tx = this.target[0]; + out.ty = this.target[1]; + out.tz = this.target[2]; + out.fov = this.fov - 2.5 * u; + out.near = 1.5; + } +} + +/** Does the line from (fx, fy, fz) past the deck point d run on into open sky? */ +function shOpenBehind(terrain: Terrain, fx: number, fy: number, fz: number, dx: number, dy: number, dz: number): boolean { + const rx = dx - fx, ry = dy - fy + 6.0, rz = dz - fz; // (the coaches, a little above the parapet) + const run = hypot2(rx, rz); + for (let t = 120.0; t < 2000.0; t += 130.0) { + const k = 1.0 + t / run; + if (terrain.natural(fx + rx * k, fz + rz * k, false) > fy + ry * k - 4.0) { + return false; + } + } + return true; +} + +// --- Out of the tunnel --------------------------------------------------------------------------------- + +// The exit shot, keyed to `past`, the metres the engine's front has gone beyond the way out (negative while +// it is still inside). The camera stands low in the cutting on the sun side, so the low sun comes from over +// its shoulder, and looks back at the portal: first the black mouth and the headlamp coming; then the engine +// breaks out of it into the sun, while the camera backs away just ahead of it and swings wide. +// [past, camera metres beyond the portal, camera lateral (+ = sun side), camera height above rail, +// aim metres beyond the portal, aim lateral, aim height, lens] +const SH_EXIT: number[][] = [ + [-100.0, 45.5, 2.6, 2.05, 0.0, -0.1, 3.5, 23.5], + [-40.0, 43.5, 2.65, 1.95, 0.0, -0.1, 3.45, 23.5], + [-12.0, 41.0, 2.85, 1.8, 0.0, -0.05, 3.4, 23.0], + [4.0, 40.0, 3.2, 1.7, 2.5, 0.1, 3.3, 23.0], + [18.0, 41.5, 3.9, 1.8, 10.0, 0.15, 3.2, 25.0], + [30.0, 47.0, 4.5, 2.05, 21.5, 0.2, 3.1, 26.5], + [46.0, 60.0, 5.3, 2.25, 37.5, 0.3, 3.05, 29.0], + [66.0, 78.0, 6.8, 2.5, 58.0, 0.35, 3.0, 32.0], + [96.0, 106.5, 9.2, 2.85, 89.0, 0.2, 2.95, 35.0], + [140.0, 149.5, 11.5, 3.3, 134.0, 0.0, 2.95, 38.0], + // (held level and at the engine's own pace into the end, so the run-on past it joins without a bump) + [190.0, 199.5, 11.5, 3.3, 184.0, 0.0, 2.95, 38.0], +]; +// The eye as the engine comes out: [past, exposure, glare]. Adapted to the dark of the mouth, then dazzled +// as the sunlit engine and its steam burst out into view, then settling as the eye closes down. +const SH_EYE: number[][] = [ + [-100.0, 1.2, 0.0], [-14.0, 1.24, 0.05], [6.0, 1.65, 0.35], [22.0, 2.85, 1.0], [36.0, 2.5, 0.92], + [56.0, 1.76, 0.55], [84.0, 1.36, 0.35], [150.0, 1.1, 0.2], +]; + +/** Catmull-Rom through uneven keys, so the camera never jerks as it passes one. Writes columns 1.. to out. */ +function shKeyed(keys: number[][], x: number, out: number[]) { + const last = keys.length - 1; + const t = shClamp(x, keys[0][0], keys[last][0]); + let i = 0; + while (i < last - 1 && t > keys[i + 1][0]) { + i++; + } + const p1 = keys[i], p2 = keys[i + 1], p0 = keys[Math.max(0, i - 1)], p3 = keys[Math.min(last, i + 2)]; + const span = p2[0] - p1[0], u = (t - p1[0]) / span, u2 = u * u, u3 = u2 * u; + for (let c = 1; c < p1.length; c++) { + const m1 = i > 0 ? ((p2[c] - p0[c]) / (p2[0] - p0[0])) * span : 0.0; + const m2 = i < last - 1 ? ((p3[c] - p1[c]) / (p3[0] - p1[0])) * span : p2[c] - p1[c]; + out[c - 1] = (2.0 * u3 - 3.0 * u2 + 1.0) * p1[c] + (u3 - 2.0 * u2 + u) * m1 + (-2.0 * u3 + 3.0 * u2) * p2[c] + (u3 - u2) * m2; + } +} + +/** The bore the engine is in about arc length s ([way in, way out] as arc lengths), or an empty list. */ +function shTunnelExitNear(terrain: Terrain, s: number): number[] { + const track = terrain.track; + const layout = track.layout; + const z = track.zOfS(s); + const list = layout.inRange(z - 500.0, z + 500.0); + for (let k = 0; k < list.length; k++) { + const seg = layout.segments[list[k]]; + if (seg.type != SEG_TUNNEL) { + continue; + } + const span = terrain.tunnelSpan(seg); + const s0 = track.sOfZ(span.spanA), s1 = track.sOfZ(span.spanB); + if (s > s0 + 20.0 && s < s1) { + return [s0, s1]; + } + } + return []; +} + +// The shot opens with the engine still this far back in the dark: about two seconds to the mouth. +function shExitLead(speed: number): number { + return shClamp(speed, 10.0, 30.0) * 2.0; +} + +function shTunnelFits(terrain: Terrain, c: ShotContext): boolean { + const hit = shTunnelExitNear(terrain, c.sFront); + if (hit.length == 0 || c.speed < 3.0) { + return false; + } + const gap = hit[1] - c.sFront; + return gap > shExitLead(c.speed) * 0.7 && gap < shExitLead(c.speed) * 1.25; +} + +/** Where to send the train for the tunnel take; NaN if nowhere. */ +function shTunnelPlace(terrain: Terrain, c: ShotContext): number { + const track = terrain.track; + const lead = shExitLead(c.speed); + const index = shNextSegment(terrain, SEG_TUNNEL, track.zOfS(c.sFront) - 200.0, true, c.sFront + 60.0, -lead); + if (index < 0) { + return NaN; + } + return track.sOfZ(terrain.tunnelSpan(track.layout.segments[index]).spanB) - lead; +} + +@final +class TunnelRig { + exitS = 0.0; + seed = 0.0; + clearPoles: number[] = []; + private cleared = NaN; + // the eye for this move (main.js): an exposure multiplier, and 0..1 of extra glare off bright things + exposure = 1.0; + glare = 0.0; + private spans: number[] = []; // the bores about `centre`: way in, way out, by pairs, in order + private centre = -1.0e9; + private pose: number[] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]; + private eye: number[] = [1.0, 0.0]; + private p: number[] = [0.0, 0.0, 0.0]; + private f = new ShFrame(); + + constructor(terrain: Terrain, c: ShotContext, rng: Rng) { + const speed = Math.max(8.0, c.speed); + let hit = shTunnelExitNear(terrain, c.sFront); + if (hit.length == 0) { + const placed = shTunnelPlace(terrain, c); + hit = shTunnelExitNear(terrain, shSet(placed) ? placed : c.sFront); + } + this.exitS = hit.length > 0 ? hit[1] : c.sFront + shExitLead(speed); + this.seed = rng.range(0.0, 100.0); + this.clearAt(terrain.track, this.exitS); + } + + // The telegraph poles stand on the camera's side here: none from the mouth to well past the take. + // (A new list for each way out: the lineside notices a change by identity.) + private clearAt(track: Track, s: number) { + this.cleared = s; + this.clearPoles = [track.zOfS(s) - 8.0, track.zOfS(s + 440.0)]; + } + + // The bores around arc length s. Refreshed only after real travel. + private bores(terrain: Terrain, s: number) { + if (Math.abs(s - this.centre) > 300.0) { + const track = terrain.track; + const layout = track.layout; + this.centre = s; + const found: number[] = []; + const z = track.zOfS(s); + const list = layout.inRange(z - 900.0, z + 900.0); + for (let k = 0; k < list.length; k++) { + const seg = layout.segments[list[k]]; + if (seg.type != SEG_TUNNEL) { + continue; + } + const span = terrain.tunnelSpan(seg); + found.push(track.sOfZ(span.spanA)); + found.push(track.sOfZ(span.spanB)); + } + // (sorted by the way in: a few pairs, by insertion) + for (let i = 2; i < found.length; i += 2) { + for (let j = i; j > 0 && found[j - 2] > found[j]; j -= 2) { + const a = found[j - 2], b = found[j - 1]; + found[j - 2] = found[j]; + found[j - 1] = found[j + 1]; + found[j] = a; + found[j + 1] = b; + } + } + this.spans = found; + } + } + + update(terrain: Terrain, c: ShotContext, dt: number, t: number, out: ShotPose) { + const track = terrain.track; + const noise = track.noise; + // Manual mode only: once this way out is long past and the engine nears the next tunnel, the take + // starts again outside that tunnel's way out. + this.bores(terrain, c.sFront); + const spans = this.spans; + for (let i = 0; i < spans.length; i += 2) { + const a = spans[i], b = spans[i + 1]; + if (b > this.exitS + 1.0 && c.sFront > a - 30.0 && c.sFront < b - 30.0) { + this.exitS = b; + break; + } + } + if (this.exitS != this.cleared) { + this.clearAt(track, this.exitS); + } + const past = c.sFront - this.exitS; + const pose = this.pose; + shKeyed(SH_EXIT, past, pose); + let ahead = pose[0], side = pose[1], high = pose[2], aimAhead = pose[3]; + const aimSide = pose[4], aimHigh = pose[5], lens = pose[6]; + // Waiting long for the engine (manual mode), it creeps in toward the mouth rather than standing dead. + ahead += shClamp(SH_EXIT[0][0] - past, 0.0, 260.0) * 0.03; + // Past the last key (manual mode) the camera runs on beside the engine, then draws in to ride just + // inside the lineside, high enough to clear parapets, platforms and fences. + const run = Math.max(0.0, past - SH_EXIT[SH_EXIT.length - 1][0]); + if (run > 0.0) { + const settle = smootherstep(30.0, 260.0, run); + ahead += run; + aimAhead += run; + side = lerp(side, -5.2, settle); + high = lerp(high, 4.6, settle); + } + const p = this.p; + shPointAlong(track, this.f, this.exitS + ahead, 0.0, side, high, p); + out.px = p[0]; + out.py = p[1]; + out.pz = p[2]; + shPointAlong(track, this.f, this.exitS + aimAhead, 0.0, aimSide, aimHigh, p); + out.tx = p[0]; + out.ty = p[1]; + out.tz = p[2]; + + // a camera held low beside the line: it breathes, and shudders as the engine bears down on it + const seed = this.seed; + const near = smoothstep(34.0, 9.0, ahead - past) * smoothstep(-6.0, 14.0, past) * Math.min(1.0, c.speed / 18.0); + out.px += 0.01 * (0.35 + near) * noise.noise1(t * 6.1, seed) + 0.009 * near * noise.noise1(t * 17.0, seed + 4.0); + out.py += 0.012 * (0.35 + near) * noise.noise1(t * 7.7, seed + 1.0) + 0.01 * near * noise.noise1(t * 19.0, seed + 5.0); + out.ty += 0.025 * (0.3 + near) * noise.noise1(t * 5.3, seed + 2.0) + 0.026 * near * noise.noise1(t * 15.0, seed + 6.0); + out.roll = 0.0022 * (0.3 + near) * noise.noise1(t * 4.1, seed + 3.0); + + // never under the turf + if (terrain.lineStateAt(out.pz) != LINE_TUNNEL) { + const floor = terrain.height(out.px, out.pz, false) + 0.7; + const over = out.py - floor; // a soft floor: eased into, never struck + if (over < 1.5) { + out.py = floor + shLog1p(skExp(over * 6.0)) / 6.0; + } + } + shKeyed(SH_EYE, past, this.eye); + this.exposure = this.eye[0]; + this.glare = Math.max(0.0, this.eye[1]); + out.fov = lens; + out.near = 0.3; + } +} + +// --- The window seat ----------------------------------------------------------------------------------- + +// The sun is low ahead on the +x side and its light travels back down the car, so both views look back +// along the train: that way the lit faces of the seats and the window are in frame together. +@final +class InteriorRig { + side = 1.0; + variant: int = 0; + zk = 0.0; + + constructor(rng: Rng, o: ShotOptions) { + this.side = shSet(o.side) ? o.side : 1.0; + this.variant = shSet(o.variant) ? o.variant : rng.chance(0.6) ? 0 : 1; + const bay: int = shSet(o.bay) ? o.bay : rng.int(2, 4); + this.zk = COACH_WINDOW_CENTRES[bay]; + } + + update(terrain: Terrain, c: ShotContext, dt: number, t: number, out: ShotPose) { + const u = smootherstep(0.0, 1.0, t / 11.0); + const breathe = skSin(t * 0.55) * 0.012; + const side = this.side, zk = this.zk; + out.vehicle = SH_CAMERA_COACH; + if (this.variant == 0) { + // In the window seat: over the table to the sunlit chair facing, the country sliding by beside it. + out.px = side * (0.6 - 0.03 * u); + out.py = SH_COACH_FLOOR_Y + 1.25 + breathe; + out.pz = zk + 0.9 - 0.12 * u; + out.tx = side * (1.08 - 0.03 * u); + out.ty = 2.14 + breathe; + out.tz = zk - 0.98 + 0.16 * u; + } else { + // Across the aisle: one table, its lamp and the sunlit armchair against the window. The lens sits + // at a seated eye and looks nearly level, so the country and its sky fill the upper frame. + out.px = -side * (0.4 - 0.08 * u); + out.py = SH_COACH_FLOOR_Y + 1.31 + breathe; + out.pz = zk + 0.8 - 0.12 * u; + out.tx = side * 1.0; + out.ty = 2.295 + breathe * 0.5; + out.tz = zk - 0.62 + 0.12 * u; + } + out.fov = this.variant == 0 ? 60.0 : 52.0; + out.near = 0.04; + } +} + +// --- The driver's view --------------------------------------------------------------------------------- + +// The cab's front windows are two small dark ports, so the road is watched as drivers watched it: from the +// left side of the cab, head out past the side sheet, looking along the boiler. +@final +class CabRig { + seed = 0.0; + + constructor(rng: Rng) { + this.seed = rng.range(0.0, 100.0); + } + + update(terrain: Terrain, c: ShotContext, dt: number, t: number, out: ShotPose) { + const noise = terrain.track.noise; + const seed = this.seed; + // metres from the front buffer to the driving axle under the cab: the one whose rail joints are felt + const axle = VEHICLE_SPECS[0][0] / 2.0 + 3.15; + const pace = Math.min(1.0, c.speed / 18.0); + // a knock from each joint, in step with the clack: the same arc positions the sound uses + const past = (((c.sFront - axle) % SH_RAIL_JOINT) + SH_RAIL_JOINT) % SH_RAIL_JOINT; + const since = past / Math.max(3.0, c.speed); + const knock = pace * skExp(-since / 0.13) * skSin(since * SH_PI * 12.0); + // the slow roll of the engine on its springs, and a fine tremor over it + const x = pace * (0.014 * noise.noise1(t * 0.85, seed) + 0.0025 * noise.noise1(t * 7.1, seed + 1.0)); + const y = pace * (0.008 * noise.noise1(t * 1.35, seed + 2.0) + 0.0025 * noise.noise1(t * 10.7, seed + 3.0)) + 0.007 * knock + + 0.0015 * skSin(t * 1.7); + out.vehicle = 0; + out.px = SH_CAB_EYE[0] + x; + out.py = SH_CAB_EYE[1] + y; + out.pz = SH_CAB_EYE[2]; + // the head steadies itself: the aim follows only part of what the body does + out.tx = SH_CAB_AIM[0] + x * 0.4; + out.ty = SH_CAB_AIM[1] + y * 0.4; + out.tz = SH_CAB_AIM[2]; + out.roll = pace * (0.0045 * noise.noise1(t * 0.7, seed + 4.0)) + 0.0012 * knock; + out.fov = 58.0; + out.near = 0.06; + } +} + +// --- The cameraman (manual) ---------------------------------------------------------------------------- + +/** Holds the running rig: composes a shot and plays it, frame by frame. */ +@final +export class Cameraman { + private world: Shared; + private rng = new Rng(11); + private seeded: Rng | null = null; // a composed shot's own stream (opts.seed), else the cameraman's + shot: int = SHOT_DRONE; + elapsed = 0.0; + duration = 9.0; + cuts: int = 0; + readonly pose = new ShotPose(); + private drone: DroneRig | null = null; + private trackside: TracksideRig | null = null; + private bridge: BridgeRig | null = null; + private interiorRig: InteriorRig | null = null; + private tunnel: TunnelRig | null = null; + private cab: CabRig | null = null; + + constructor(world: Shared) { + this.world = world; + } + + /** Does the world at c suit the shot? */ + fits(shot: int, c: ShotContext): boolean { + const terrain = this.world.value.terrain; + if (shot == SHOT_DRONE) return shDroneFits(terrain, c); + if (shot == SHOT_TRACKSIDE) return shTracksideFits(terrain, c); + if (shot == SHOT_BRIDGE) return shBridgeFits(terrain, c); + if (shot == SHOT_TUNNEL) return shTunnelFits(terrain, c); + if (shot == SHOT_INTERIOR) { + return !shInTunnelBetween(terrain, c.sFront - 110.0, c.sFront + c.speed * 8.0 - 20.0); + } + return true; + } + + /** Where to send the train for the shot (an arc length), NaN if nowhere. */ + place(shot: int, c: ShotContext): number { + const terrain = this.world.value.terrain; + const probe = new ShotContext(); + probe.speed = c.speed; + if (shot == SHOT_DRONE || shot == SHOT_INTERIOR) { + for (let s = c.sFront + 260.0; s < c.sFront + 6000.0; s += 70.0) { + probe.sFront = s; + if (this.fits(shot, probe)) return s; + } + return NaN; + } + if (shot == SHOT_TRACKSIDE) { + for (let s = c.sFront + 300.0; s < c.sFront + 6000.0; s += 45.0) { + probe.sFront = s; + if (shTracksideFits(terrain, probe)) return s; + } + return NaN; + } + if (shot == SHOT_BRIDGE) { + const track = terrain.track; + const index = shNextSegment(terrain, SEG_VIADUCT, track.zOfS(c.sFront), false, c.sFront + 60.0, 120.0); + if (index < 0) return NaN; + return track.sOfZ(terrain.bridgeSpan(track.layout.segments[index]).spanA) + 120.0; + } + if (shot == SHOT_TUNNEL) return shTunnelPlace(terrain, c); + return c.sFront; + } + + /** The trackside camera's spot for a train whose front is at sFront. */ + tracksideSpotFor(sFront: number, speed: number): number { + return shTracksideSpotFor(this.world.value.terrain, sFront, speed); + } + + /** Starts a shot (cameraman.js's begin): its rig from the seeded stream or the cameraman's, its run. */ + begin(shot: int, c: ShotContext, o: ShotOptions) { + this.shot = shot; + this.seeded = shSet(o.seed) ? new Rng(o.seed) : null; + const seeded = this.seeded; + if (seeded) { + this.create(shot, c, seeded, o); + } else { + this.create(shot, c, this.rng, o); + } + this.elapsed = 0.0; + this.duration = this.rng.range(SH_TIMES[shot * 2], SH_TIMES[shot * 2 + 1]); + this.cuts++; + } + + private create(shot: int, c: ShotContext, rng: Rng, o: ShotOptions) { + const terrain = this.world.value.terrain; + this.drone = null; + this.trackside = null; + this.bridge = null; + this.interiorRig = null; + this.tunnel = null; + this.cab = null; + if (shot == SHOT_DRONE) { + this.drone = new DroneRig(terrain, c, rng, o); + } else if (shot == SHOT_TRACKSIDE) { + this.trackside = new TracksideRig(terrain, c, rng); + } else if (shot == SHOT_BRIDGE) { + this.bridge = new BridgeRig(terrain, c, rng); + } else if (shot == SHOT_INTERIOR) { + this.interiorRig = new InteriorRig(rng, o); + } else if (shot == SHOT_TUNNEL) { + this.tunnel = new TunnelRig(terrain, c, rng); + } else { + this.cab = new CabRig(rng); + } + } + + /** Advances the shot's clock (cameraman.js's update, manual: a trackside camera sets up again once passed). */ + update(c: ShotContext, dt: number) { + this.elapsed += dt; + const trackside = this.trackside; + if (trackside) { + const seeded = this.seeded; + const terrain = this.world.value.terrain; + const rearmed = seeded ? trackside.rearm(terrain, seeded, c) : trackside.rearm(terrain, this.rng, c); + if (rearmed) { + this.elapsed = 0.0; + } + } + } + + /** The camera for this frame, once the train has been placed (cameraman.js's frame). */ + frame(c: ShotContext, dt: number) { + const out = this.pose; + const terrain = this.world.value.terrain; + const t = this.elapsed; + out.vehicle = -1; + out.roll = 0.0; + const drone = this.drone; + const trackside = this.trackside; + const bridge = this.bridge; + const interiorRig = this.interiorRig; + const tunnel = this.tunnel; + const cab = this.cab; + if (drone) drone.update(terrain, c, dt, t, out); + else if (trackside) trackside.update(terrain, c, dt, t, out); + else if (bridge) bridge.update(terrain, c, dt, t, out); + else if (interiorRig) interiorRig.update(terrain, c, dt, t, out); + else if (tunnel) tunnel.update(terrain, c, dt, t, out); + else if (cab) cab.update(terrain, c, dt, t, out); + } + + /** The window seat: the eye settles on the lamp-lit saloon. */ + get interior(): boolean { + return this.shot == SHOT_INTERIOR; + } + + /** The shot's own exposure multiplier (the tunnel take's eye), 1 otherwise. */ + get exposure(): number { + const tunnel = this.tunnel; + return tunnel ? tunnel.exposure : 1.0; + } + + /** The shot's own glare 0..1 (the tunnel take), 0 otherwise. */ + get glare(): number { + const tunnel = this.tunnel; + return tunnel ? tunnel.glare : 0.0; + } + + /** A stretch of the pole line [z0, z1] the shot leaves out, or an empty list. */ + clearPoles(): number[] { + const drone = this.drone; + const tunnel = this.tunnel; + const out: number[] = []; + if (drone) { + for (let i = 0; i < drone.clearPoles.length; i++) out.push(drone.clearPoles[i]); + } else if (tunnel) { + for (let i = 0; i < tunnel.clearPoles.length; i++) out.push(tunnel.clearPoles[i]); + } + return out; + } +} diff --git a/core/golden_line_steam.ts b/core/golden_line_steam.ts index 41daa85..7308226 100644 --- a/core/golden_line_steam.ts +++ b/core/golden_line_steam.ts @@ -8,8 +8,8 @@ // line, splatted on the CPU every frame; and before them the plume's soft shadow on the land, a box // multiplying the finished colour, looked up in the plume's map toward the sun. // -// Not ported: the original's dazzle (the extra exposure of the shot breaking out of a bore, which our -// camera never takes) and the drone's swollen plume (golden_line_plume.ts leaves `aerial` at 0). +// As the engine breaks out of a bore the tunnel take opens the eye wide (SteamLight.dazzle, its extra +// exposure): the steam then takes back about half of it, so that it glows and still shows its billows. // // Imported with `import { Steam, SteamLight } from "../core/golden_line_steam";` (from examples/). @@ -71,6 +71,8 @@ export class SteamLight { lightX = 0.0; lightY = 1.0; lightZ = 0.0; + // the tunnel take's extra exposure (main.js's frame.dazzle), 0 elsewhere + dazzle = 0.0; } function smWrap(v: number): number { @@ -278,6 +280,16 @@ export class Steam { this.set4(SC_DRIFT, smWrap(-t * 0.045), smWrap(-t * 0.062), smWrap(t * 0.036), this.depthA); this.set4(SC_DRIFT_BROAD, smWrap(-t * 0.0255), smWrap(-t * 0.034), smWrap(t * 0.021), this.depthB); this.lightSmoke(light); + // Steam is the brightest thing in a dazzled frame and would burn out to a flat white shape. + if (light.dazzle != 0.0) { + const held = 1.0 / (1.0 + light.dazzle * 0.55); + const slots: int[] = [SC_SUN, SC_SKY, SC_SIDE, SC_GROUND, SC_RIM]; + for (const slot of slots) { + for (let k = 0; k < 3; k++) { + c[slot + k] = (c[slot + k] * held); + } + } + } this.placeSun(light, width, height); const mouth = frame.tunnelFactor * (1.0 - 0.8 * light.lamps); this.set4(SC_PORTAL, 1.0 * 0.6 * mouth, 0.82 * 0.6 * mouth, 0.58 * 0.6 * mouth, 0.0); diff --git a/core/golden_line_structures.ts b/core/golden_line_structures.ts index cd1a717..7e94961 100644 --- a/core/golden_line_structures.ts +++ b/core/golden_line_structures.ts @@ -335,6 +335,12 @@ export class StructuresRenderer { private packX = 1e9; private packZ = 1e9; private dirty = true; + // a stretch of the pole line a camera take leaves out (setClearPoles) + private clearOn = false; + // the tunnel take's extra exposure (main.js's frame.dazzle): the dark of a mouth stays dark under it + dazzle = 0.0; + private clearZ0 = 0.0; + private clearZ1 = 0.0; private anchorZ = 0.0; private wireCount: int = 0; private wireAnchor = 0.0; @@ -1203,7 +1209,7 @@ export class StructuresRenderer { gl[1] = (this.tunnelOut - originZ); gl[2] = this.inside; // seen from daylight the dark inside a mouth goes down with the day outside - gl[3] = (1.2 * (1.0 - 0.86 * this.lamps)); + gl[3] = (1.2 * (1.0 - 0.86 * this.lamps) / (1.0 + this.dazzle)); gl[4] = this.cabX; gl[5] = this.cabY; gl[6] = (this.cabZ - originZ); @@ -1329,6 +1335,25 @@ export class StructuresRenderer { } } + /** + * A composed camera take can ask for a stretch of the pole line [z0, z1] to be left out of its frame + * (an empty list: none). A change re-packs the pools on the spot, where the cut hides it. + */ + setClearPoles(range: number[]): void { + const on = range.length == 2; + const z0 = on ? range[0] : 0.0, z1 = on ? range[1] : 0.0; + if (on != this.clearOn || z0 != this.clearZ0 || z1 != this.clearZ1) { + this.clearOn = on; + this.clearZ0 = z0; + this.clearZ1 = z1; + this.dirty = true; + } + } + + private cleared(z: number): boolean { + return this.clearOn && z > this.clearZ0 && z < this.clearZ1; + } + /** lineside.js's packRing: the items within reach, nearest first, into the ring's near and far pools. */ private packRing(commandList: CommandList, r: StRing): void { const cx = this.cameraX, cz = this.cameraZ; @@ -1344,6 +1369,9 @@ export class StructuresRenderer { } const list = this.chunks[c].items.list(r.kind); for (let i = 0; i < list.length; i++) { + if (r.kind == LS_POLES && this.cleared(list[i].z)) { + continue; + } const dx = list[i].x - cx, ddz = list[i].z - cz; const dist = dx * dx + ddz * ddz; if (dist < reach * reach) { @@ -1409,6 +1437,9 @@ export class StructuresRenderer { for (let c = 0; c < this.chunks.length; c++) { const wires = this.chunks[c].items.wires; for (let i = 0; i < wires.length; i++) { + if (this.cleared(wires[i].az) || this.cleared(wires[i].bz)) { + continue; + } const dx = wires[i].ax - cx, dz = wires[i].az - cz; const dist = dx * dx + dz * dz; if (dist < 420.0 * 420.0) { diff --git a/core/golden_line_vegetation.ts b/core/golden_line_vegetation.ts index 9838246..ff57463 100644 --- a/core/golden_line_vegetation.ts +++ b/core/golden_line_vegetation.ts @@ -7,7 +7,7 @@ // Imported with `import { TreeRenderer } from "../core/golden_line_vegetation";` (from examples/). import { App, BindingLayoutDesc, BindingSet, BindingSetDesc, CommandList, Frame, GraphicsPipelineDesc, InputLayoutDesc } from "./donut"; -import { World, TerrainMesh, terrainColumns, deg, SEG_VIADUCT, SEG_RIVER, SEG_TUNNEL, SUN_DIR_X, SUN_DIR_Z } from "./golden_line_world"; +import { World, TerrainMesh, terrainColumns, deg, hypot2, SEG_VIADUCT, SEG_RIVER, SEG_TUNNEL, SUN_DIR_X, SUN_DIR_Z } from "./golden_line_world"; import { buildTrees, TREE_TYPES, TREE_STRIDE, TREE_BUSH } from "./golden_line_trees"; import { LsFarmSite, lsSiteFarm } from "./golden_line_lineside"; import { Geo, Prototype, buildPrototype, PROTO_COUNT } from "./golden_line_treegen"; @@ -88,7 +88,12 @@ const VEG_SLICES: number[] = [0.0, 19.0, 15.0, 68.0, 56.0, 218.0, 180.0, 660.0]; const VEG_VERTEX_FLOATS: int = 15; // struct TreeGlobals: six planes, the camera and level-of-detail scale, the wind, the cascades' slices, the sun. -const VEG_GLOBAL_FLOATS: int = 52; +// The viewpoints trees are kept out of (trees.js's CLEARANCE): none, the viaduct, beside the rails, the drone. +export const VEG_CLEAR_NONE: int = 0; +export const VEG_CLEAR_BRIDGE: int = 1; +export const VEG_CLEAR_TRACKSIDE: int = 2; +export const VEG_CLEAR_DRONE: int = 3; +const VEG_GLOBAL_FLOATS: int = 60; // TreeGlobals (shaders/golden_line_trees.hlsl) const MODE_BARK: int = 0; const MODE_NEAR: int = 1; @@ -354,6 +359,14 @@ export class TreeRenderer { private app: App; private world: Shared; + // keepViewOpen's latch: where the rule was set up, and for which cut + private latchCut: int = -1; + private latchX = 0.0; + private latchY = 0.0; + private latchZ = 0.0; + private latchSlope = 0.0; + private latchOffset = 1.0; + private latchHeight = 0.0; // the last chunk's farm, sited on its trees before any were cleared (for the lineside planner) private farm = new LsFarmSite(); private protos: Prototype[] = []; @@ -429,6 +442,7 @@ export class TreeRenderer { for (let i = 0; i < VEG_GLOBAL_FLOATS; i++) { this.globals.push(0.0); } + this.globals[58] = 1.0; // clearB: no rule (0, 0, 1, 0) this.quad = new MeshRange(0, 0); } @@ -1032,6 +1046,66 @@ export class TreeRenderer { } } + /** + * The rule that keeps a viewpoint open (trees.js's CLEARANCE and keepViewOpen): a tree that would wall + * off the view grows only as high as the sight line allows. rule: VEG_CLEAR_NONE, _BRIDGE, _TRACKSIDE + * or _DRONE; the camera in true world, which way it looks (its forward's y), its vertical field of + * view (degrees), its render-space x and z; cut: the shots so far, jumped: the train was sent on. + */ + keepViewOpen(rule: int, cut: int, jumped: boolean, eyeX: number, eyeY: number, eyeZ: number, forwardY: number, + fovDeg: number, originZ: number, camX: number, camZ: number) { + const g = this.globals; + if (rule == VEG_CLEAR_NONE) { + g[56] = 0.0; + g[57] = 0.0; + g[58] = 1.0; + g[59] = 0.0; + this.latchCut = -1; + return; + } + const track = this.world.value.track; + const radial = rule != VEG_CLEAR_DRONE; + const reach = rule == VEG_CLEAR_BRIDGE ? 190.0 : rule == VEG_CLEAR_TRACKSIDE ? 8.5 : 520.0; + const below = rule == VEG_CLEAR_BRIDGE ? 0.22 : -0.1; + const lift = rule == VEG_CLEAR_TRACKSIDE ? 0.6 : 0.0; + const rail = track.Y(eyeZ); + // A new shot, or a trackside camera that has set itself up again further down the line. + const moved = radial && hypot2(eyeX - this.latchX, eyeZ - this.latchZ) > 60.0; + if (this.latchCut != cut || jumped || moved) { + this.latchCut = cut; + this.latchX = eyeX; + this.latchZ = eyeZ; + this.latchY = eyeY + lift; + if (radial) { + const pitch = Math.asin(Math.max(-1.0, Math.min(1.0, forwardY))); + this.latchSlope = Math.tan(pitch - deg(fovDeg) * below); + } + const offset = eyeX - track.X(eyeZ); + this.latchOffset = Math.abs(offset) < 8.0 ? (offset < 0.0 ? -8.0 : 8.0) : offset; + this.latchHeight = eyeY - rail; + } + if (radial) { + g[52] = this.latchX; + g[53] = (this.latchZ - originZ); + g[54] = this.latchSlope; + g[55] = this.latchY; + g[56] = 1.0; + g[57] = reach; + g[58] = 1.0; + g[59] = 0.0; + } else { + // Distance is measured from the flying camera, but only far out, where it cannot be seen to matter. + g[52] = camX; + g[53] = camZ; + g[54] = 0.0; + g[55] = (rail + this.latchHeight); + g[56] = 2.0; + g[57] = reach; + g[58] = this.latchOffset; + g[59] = rail; + } + } + // Sorts the trees into the frame's draw lists. worldToClip is golden_line.hlsl's (by rows, render // space); camera in render space; fovY in radians; time in seconds. cull(commandList: CommandList, originZ: number, worldToClip: f32[], cx: number, cy: number, cz: number, fovY: number, time: number) { diff --git a/core/golden_line_world.ts b/core/golden_line_world.ts index 2f2ca19..9fec94f 100644 --- a/core/golden_line_world.ts +++ b/core/golden_line_world.ts @@ -548,6 +548,21 @@ export class Layout { return this.segments[this.indexAt(z)]; } + // The index of the first segment of `type` whose centre lies beyond z (layout.js's nextOfType). + nextOfType(type: int, z: number): int { + let i = this.indexAt(z); + for (;;) { + while (i >= this.segments.length) { + this.generateNext(); + } + const seg = this.segments[i]; + if (seg.type == type && seg.zc > z) { + return i; + } + i++; + } + } + // The indices in segments of all segments overlapping [a, b]. inRange(a: number, b: number): int[] { this.ensure(b + 1.0); diff --git a/examples/golden_line.ts b/examples/golden_line.ts index 97aabe8..c857452 100644 --- a/examples/golden_line.ts +++ b/examples/golden_line.ts @@ -7,7 +7,7 @@ import { World, NoiseTextures, NOISE_SIZE, SEED, CHUNK_LEN, ORIGIN_STEP, START_Z, SUN_AZIMUTH, linearFromHex, lerp, smoothstep, deg, } from "../core/golden_line_world"; -import { TreeRenderer } from "../core/golden_line_vegetation"; +import { TreeRenderer, VEG_CLEAR_NONE, VEG_CLEAR_BRIDGE, VEG_CLEAR_TRACKSIDE, VEG_CLEAR_DRONE } from "../core/golden_line_vegetation"; import { WaterRenderer } from "../core/golden_line_water"; import { GrassRenderer } from "../core/golden_line_grass"; import { TrainRenderer, TRN_CAMERA_COACH } from "../core/golden_line_train"; @@ -16,6 +16,7 @@ import { SunShadows, SH_CASCADES } from "../core/golden_line_shadows"; import { Steam, SteamLight } from "../core/golden_line_steam"; import { Motes } from "../core/golden_line_motes"; import { Bloom } from "../core/golden_line_bloom"; +import { Cameraman, ShotContext, ShotOptions, ShotPose, SHOT_NAMES, SHOT_DRONE, SHOT_TRACKSIDE, SHOT_BRIDGE, SHOT_INTERIOR, SHOT_TUNNEL, SHOT_CAB } from "../core/golden_line_shots"; import { Birds } from "../core/golden_line_birds"; // The Golden Line's ground and sky: an endless line through European-style countryside at golden @@ -31,8 +32,7 @@ import { Birds } from "../core/golden_line_birds"; // five coaches running on the line (core/golden_line_train.ts, golden_line_loco.ts, golden_line_tender.ts, // golden_line_coach.ts, shaders/golden_line_train.hlsl; -trainz puts its front buffer at z), with the // footplate and its crew (golden_line_cab.ts) and the furnished saloon of the camera coach -// (golden_line_saloon.ts, shaders/golden_line_interior.hlsl), and the original's two cameras inside it: -// -shot cab (the driver's view) and -shot seat (the window seat); and beside the line its permanent way and +// (golden_line_saloon.ts, shaders/golden_line_interior.hlsl); and beside the line its permanent way and // lineside (core/golden_line_structures.ts, golden_line_trackway.ts, golden_line_lineside.ts, // shaders/golden_line_structures.hlsl): ballast, rails and sleepers, telegraph poles and wires, fences, // quarter-mile posts, bales, dry-stone walls and the outlying farms (golden_line_houses.ts); the viaducts @@ -46,15 +46,21 @@ import { Birds } from "../core/golden_line_birds"; // in its sky (golden_line_birds.ts, shaders/golden_line_birds.hlsl); and the dust hanging in the sunbeams // of the camera coach (golden_line_motes.ts, shaders/golden_line_motes.hlsl); and the bloom on real light, // three's BloomNode widened while the eye is open in a bore (golden_line_bloom.ts, -// shaders/golden_line_bloom.hlsl). -clock holds the ride's clock still, as the original's captures -// are taken. The camera's shots and the sound aren't ported yet. +// shaders/golden_line_bloom.hlsl); and the cameraman's six takes (golden_line_shots.ts): the drone chase, +// beside the rails, the viaduct, the window seat, out of the tunnel and the driver's cab (-shot , with +// -seed, -side, -at and the seat's -variant and -bay as the original's shot options), the view kept open +// through the trees and poles they look through, and the tunnel take's dazzle; -preset composes one +// of the original's capture presets. -clock holds the ride's clock +// still, as the original's captures are taken. The director (the cuts from take to take), the headlight's +// beam on the line after dusk and the sound aren't ported yet. namespace GoldenLine { // the exposure multiplier of an eye used to the dark (main.js) const EYE_OPEN = 3.8; // Test mode: the frames rendered. const TEST_FRAMES = 30; - // -test: checks the last frame: the sky over the land, blue overhead, the ground warm below. From the + // -test: checks the last frame of the opening take: the low morning sky bright over the land, clear of the + // steam at the right, the ground warm below. From the // driver's cab: the bright sky beyond the boiler, the frame lit; from the window seat: the crimson // velvet of the armchairs, the room lit. function checkFrame(app: App, view: int) { @@ -79,16 +85,16 @@ namespace GoldenLine { } return check.share(total - dark, total, 90.0, 100.0, "Lit"); } - const sky = check.rgb(check.width / 2, 2); - if (((sky >> 16) & 0xFF) <= (sky & 0xFF)) { - return "the sky overhead isn't blue"; + const sky = check.rgb(check.width * 7 / 8, 2); + if (((sky >> 16) & 0xFF) + ((sky >> 8) & 0xFF) + (sky & 0xFF) < 540) { + return "the sky over the land isn't bright"; } const ground = check.rgb(check.width / 2, check.height - 3); if ((ground & 0xFF) <= ((ground >> 16) & 0xFF)) { return "the ground isn't warm"; } - // (the train's underframe in its own shade, and the near telegraph pole, its wires and the sleepers - // against the low sun, are near black: about 3.5% of the opening view) + // (the train's underframe in its own shade, the wires and the sleepers against the low sun are near + // black) return check.share(total - check.countInRange(0, 8, 0, 8, 0, 8, 0, 0, check.width, check.height), total, 94.0, 100.0, "Lit"); } @@ -102,14 +108,9 @@ namespace GoldenLine { const VIEW_OUTSIDE = 0; const VIEW_CAB = 1; const VIEW_SEAT = 2; - // The driver's view: from the left side of the cab, head out past the side sheet, looking along the - // boiler (engine frame: +x is the left, y above the rail head, +z forward). - const CAB_EYE: number[] = [1.74, 3.18, -4.92]; - const CAB_AIM: number[] = [0.9, 2.05, 6.0]; - // The window seat: in the camera coach (the third), whose windows are centred on these z. - const SEAT_COACH = 3; - const SEAT_FLOOR = 1.28; - const SEAT_BAYS: number[] = [-6.3, -4.2, -2.1, 0.0, 2.1, 4.2, 6.3]; + // The panel's camera buttons, in the original's key order (1-5, C). + const GL_SHOT_LABELS: string[] = ["Drone chase", "Beside the rails", "Viaduct", "Window seat", "Out of the tunnel", "Driver's cab"]; + const GL_SHOT_ORDER: int[] = [SHOT_DRONE, SHOT_TRACKSIDE, SHOT_BRIDGE, SHOT_INTERIOR, SHOT_TUNNEL, SHOT_CAB]; // How far ahead of and behind the camera the ground is kept (the original's streamer). const BEHIND = 1100.0; @@ -281,6 +282,27 @@ namespace GoldenLine { // --- The program ------------------------------------------------------------------------------ + // The original's opening take (main.js's OPENING): riding behind the engine, the engine and the + // horizon on chosen marks, no telegraph pole crossing the engine while the take plays. + function glOpening(o: ShotOptions) { + o.seed = 5.0; + o.side = -1.0; + o.back = 13.0; + o.lat0 = 19.0; + o.lat1 = 17.2; + o.up0 = 3.5; + o.up1 = 3.15; + o.along0 = -15.5; + o.along1 = -18.0; + o.fov = 33.0; + o.place = [0.65, 0.44, 0.675, 0.455]; + o.clear = 2.2; + o.sight = 0.5; + o.crop = 1.7; + o.clearPoles = [1640.0, 2160.0]; + o.clearPolesSet = true; + } + class GoldenLineArgs extends Args { // -time <0..1>: the hour to start at, 0 golden hour to 1 blue hour (0.1 by default). time = 0.1; @@ -302,14 +324,25 @@ namespace GoldenLine { // -trainz : where the engine's front buffer stands (with -eye: at -base, as the original's // composed shots put it; on the ride: 30 m ahead of the camera). It holds the train still. trainZ = -1.0; - // -shot : the camera: beside the line (by default), the driver's cab, or the - // window seat; -variant <0|1> (the seat: over the table, or across the aisle), -side <1|-1> (the - // seat's side of the coach), -bay <0..6> (which window), -at (how far into the shot). + // -shot : the camera (shots.js's rigs): the drone chase, beside + // the rails, the viaduct, out of a tunnel, the window seat or the driver's cab; by default the + // original's opening take. The train is sent to where the shot works if it doesn't here (from -base). + // -variant <0|1> (the seat: over the table, or across the aisle), -side <1|-1> (the seat's side of + // the coach; the drone's side of the line), -bay <0..6> (which window), -seed (the shot's own + // random stream), -at (how far into the shot, played at 60 Hz as the original's + // composeNow; pass: the trackside camera just before the engine draws level). + shot: int = -1; view = VIEW_OUTSIDE; + opts = new ShotOptions(); + atPass = false; + // -preset : one of the original's composed camera presets (main.js's PRESETS): hero, drone, + // trackside, bridge, interior, tunnel, sunset, dusk, dusk-interior, cab, dusk-cab, motion. + preset = ""; // -noshadows: no sun shadow cascades (the baked hill shadows stay). noShadows = false; variant = 0; side = 1.0; + sideSet = false; bay = 3; at = 0.0; // -clock : the ride's clock, held still (the steam, the drift of the air and the water stop). @@ -349,13 +382,20 @@ namespace GoldenLine { i++; } else if (arg == "-shot" && i + 1 < this.argc) { const shot = this[i + 1]; - this.view = shot == "cab" ? VIEW_CAB : shot == "seat" ? VIEW_SEAT : VIEW_OUTSIDE; + this.shot = shot == "seat" ? SHOT_INTERIOR : SHOT_NAMES.indexOf(shot); + i++; + } else if (arg == "-preset" && i + 1 < this.argc) { + this.preset = this[i + 1]; + i++; + } else if (arg == "-seed" && i + 1 < this.argc) { + this.opts.seed = parseFloat(this[i + 1]); i++; } else if (arg == "-variant" && i + 1 < this.argc) { this.variant = parseInt(this[i + 1]); i++; } else if (arg == "-side" && i + 1 < this.argc) { this.side = parseFloat(this[i + 1]) < 0.0 ? -1.0 : 1.0; + this.sideSet = true; i++; } else if (arg == "-bay" && i + 1 < this.argc) { this.bay = Math.max(0, Math.min(6, parseInt(this[i + 1]))); @@ -364,10 +404,89 @@ namespace GoldenLine { this.clock = parseFloat(this[i + 1]); i++; } else if (arg == "-at" && i + 1 < this.argc) { - this.at = parseFloat(this[i + 1]); + if (this[i + 1] == "pass") { + this.atPass = true; + } else { + this.at = parseFloat(this[i + 1]); + } i++; } } + this.applyPreset(); + const o = this.opts; + if (this.shot == SHOT_INTERIOR) { + o.variant = this.variant; + o.side = this.side; + o.bay = this.bay; + } else if (this.shot == SHOT_DRONE && this.sideSet) { + o.side = this.side; + } + if (this.shot < 0) { + // the original's opening (main.js: cameraman.start(ctx, 'drone', OPENING)) + this.shot = SHOT_DRONE; + glOpening(o); + } + this.view = this.eye.length == 3 ? VIEW_OUTSIDE : this.shot == SHOT_INTERIOR ? VIEW_SEAT : this.shot == SHOT_CAB ? VIEW_CAB : VIEW_OUTSIDE; + } + + // main.js's PRESETS: the shot, the hour, how far in, its seed and options, where along the line. + private applyPreset() { + const name = this.preset; + if (name == "") { + return; + } + const o = this.opts; + if (name == "hero" || name == "sunset") { + this.shot = SHOT_DRONE; + glOpening(o); + this.time = name == "hero" ? 0.1 : 0.36; + this.at = name == "hero" ? 1.2 : 3.0; + if (name == "sunset") { + o.lat0 = 19.5; + o.lat1 = 18.0; + o.up0 = 3.5; + o.up1 = 3.2; + o.fov = 34.0; + o.place = [0.665, 0.5, 0.66, 0.51]; + this.base = START_Z; + } + } else if (name == "drone") { + this.shot = SHOT_DRONE; + this.time = 0.12; + this.at = 3.0; + o.seed = 21.0; + o.side = -1.0; + this.base = 4350.0; + } else if (name == "trackside" || name == "motion" || name == "dusk") { + this.shot = SHOT_TRACKSIDE; + this.time = name == "dusk" ? 0.7 : 0.12; + this.atPass = true; + o.seed = name == "dusk" ? 12.0 : 4.0; + this.base = name == "dusk" ? 4300.0 : 1500.0; + } else if (name == "bridge") { + this.shot = SHOT_BRIDGE; + this.time = 0.17; + this.at = 3.5; + o.seed = 3.0; + } else if (name == "interior" || name == "dusk-interior") { + this.shot = SHOT_INTERIOR; + this.time = name == "interior" ? 0.14 : 0.74; + this.at = 2.5; + o.seed = 3.0; + this.variant = name == "interior" ? 0 : 1; + this.side = 1.0; + this.bay = 3; + } else if (name == "tunnel") { + this.shot = SHOT_TUNNEL; + this.time = 0.12; + this.at = 3.8; + o.seed = 2.0; + } else if (name == "cab" || name == "dusk-cab") { + this.shot = SHOT_CAB; + this.time = name == "cab" ? 0.12 : 0.72; + this.at = 2.0; + o.seed = 3.0; + } } } @@ -389,9 +508,14 @@ namespace GoldenLine { // The ride: distance along the line, and whether it moves. private s: number; riding = true; - // Which camera, and how long it has been on. + // Which camera: the running shot (shots.js's rigs), or a still camera from -eye. view = VIEW_OUTSIDE; - private shotTime = 0.0; + private cameraman: Cameraman; + private shotContext = new ShotContext(); + private inspect = false; + // the train was sent somewhere else for a shot: stream the place in at once + private jumped = false; + private treesJumped = false; private time = 0.0; private cloudShiftX = 0.0; private cloudShiftY = 0.0; @@ -451,6 +575,9 @@ namespace GoldenLine { private lastDt = 0.0; // the eye's adaptation to the dark of a bore (main.js's exposureAdapt), snapped on the first frame private exposureAdapt = 1.0; + // the shot's own extra exposure and glare (main.js's dazzle and rig.glare) + private dazzle = 0.0; + private shotGlare = 0.0; private snapExposure = true; private constants: f32[] = []; private pushConstants: f32[] = [0.0, 0.0, 0.0, 0.0]; @@ -475,9 +602,13 @@ namespace GoldenLine { this.structures = new StructuresRenderer(app, this.world); this.shadows = new SunShadows(app); this.s = this.world.value.track.sOfZ(args.base >= 0.0 && args.eye.length != 3 ? args.base : START_Z); - this.riding = args.eye.length != 3 || args.target.length != 3; + this.inspect = args.eye.length == 3 && args.target.length == 3; + this.riding = !this.inspect; this.view = args.view; - this.shotTime = args.at; + this.cameraman = new Cameraman(this.world); + if (!this.inspect) { + this.compose(args.shot, args.opts, args.at, args.atPass); + } for (let i = 0; i < G_FLOATS; i++) { this.constants.push(0.0); this.mirrorConstants.push(0.0); @@ -627,12 +758,10 @@ namespace GoldenLine { this.time += elapsedSeconds; this.lastDt = elapsedSeconds; this.tod.update(elapsedSeconds); - if (this.riding) { - this.s += CRUISE * elapsedSeconds; - } - // (a train held still by -trainz holds its shot too) - if (this.args.trainZ < 0.0) { - this.shotTime += elapsedSeconds; + // (cameraman.update before the train moves on, as main.js's step; a train held still by -trainz + // holds its shot too) + if (this.riding && this.args.trainZ < 0.0) { + this.stepShot(elapsedSeconds); } this.cloudShiftX += elapsedSeconds * 0.00042; this.cloudShiftY += elapsedSeconds * 0.00017; @@ -749,99 +878,132 @@ namespace GoldenLine { private fov = 40.0; private near = Z_NEAR; - // Shows the camera's view: beside the line, or one of the shots inside the train. - setView(view: int) { - if (view != this.view) { - this.view = view; - this.shotTime = 0.0; - this.steamCut = true; + // Composes a shot as the original's capture tools do (main.js's composeNow): the train sent to where + // the shot works if it doesn't at -base, the rig made, then played `at` seconds in at 60 Hz. + compose(shot: int, opts: ShotOptions, at0: number, atPass: boolean) { + const c = this.shotContext; + const track = this.world.value.track; + c.speed = this.args.trainZ >= 0.0 ? 0.0 : CRUISE; + c.sFront = this.args.trainZ >= 0.0 ? track.sOfZ(this.args.trainZ) : this.s; + if (this.args.trainZ < 0.0 && !this.cameraman.fits(shot, c)) { + const placed = this.cameraman.place(shot, c); + if (placed == placed) { + c.sFront = placed; + } } + let at = at0; + // (at: 'pass': the trackside camera just before the engine draws level) + if (shot == SHOT_TRACKSIDE && atPass && c.speed > 0.0) { + at = (this.cameraman.tracksideSpotFor(c.sFront, c.speed) - c.sFront - 13.0) / c.speed; + } + this.cameraman.begin(shot, c, opts); + if (at < 0.0) { + // a long lead-in starts before the composed position: the train goes back along the line + c.sFront += at * c.speed; + at = 0.0; + } + this.s = c.sFront; + this.view = shot == SHOT_INTERIOR ? VIEW_SEAT : shot == SHOT_CAB ? VIEW_CAB : VIEW_OUTSIDE; + this.stepShot(0.0); + for (let t = 0.0; t < at - 1e-6; t += 1.0 / 60.0) { + this.stepShot(1.0 / 60.0); + } + this.steamCut = true; + this.snapExposure = true; + } + + // One step of the ride (main.js's step, the camera's part): the shot's clock, the train on, the rig. + stepShot(dt: number) { + const c = this.shotContext; + this.cameraman.update(c, dt); + this.s += c.speed * dt; + c.sFront = this.s; + this.cameraman.frame(c, dt); + } + + // Cuts to a shot from the panel (cameraman.js's select): sending the train on if it must. + setShot(shot: int) { + const before = this.s; + const opts = new ShotOptions(); + if (shot == SHOT_INTERIOR) { + opts.variant = this.args.variant; + opts.side = this.args.side; + opts.bay = this.args.bay; + } + this.riding = true; + this.inspect = false; + this.compose(shot, opts, 0.0, false); + this.jumped = Math.abs(this.s - before) > 1.0; + this.treesJumped = this.jumped; + this.birdsCut = this.jumped; + } + + get shot(): int { + return this.cameraman.shot; } - // Riding: from the sun side of the line, a little above the rails, looking down the line. // The arc length of the engine's front buffer. trainFront(): number { const track = this.world.value.track; if (this.args.trainZ >= 0.0) { return track.sOfZ(this.args.trainZ); } - if (this.view != VIEW_OUTSIDE) { - return this.s; - } - if (this.riding) { - return this.s + 30.0; + if (this.inspect) { + return track.sOfZ(this.args.base >= 0.0 ? this.args.base : START_Z); } - return track.sOfZ(this.args.base >= 0.0 ? this.args.base : START_Z); + return this.s; } placeCamera() { - const track = this.world.value.track; this.cameraUp = [0.0, 1.0, 0.0]; this.fov = this.args.fov; this.near = Z_NEAR; - if (this.view == VIEW_CAB) { - // A slow roll of the engine on its springs and a fine tremor over it (only while it runs); - // the head steadies itself: the aim follows only part of what the body does. - const t = this.shotTime; - const pace = this.args.trainZ < 0.0 ? 1.0 : 0.0; - const x = pace * (0.014 * Math.sin(t * 0.85 * 2.1) * Math.sin(t * 0.37 + 1.3) + 0.0025 * Math.sin(t * 7.1 * 2.3)); - const y = pace * (0.008 * Math.sin(t * 1.35 * 2.2 + 0.4) + 0.0025 * Math.sin(t * 10.7 * 2.1)) + 0.0015 * Math.sin(t * 1.7); - this.cameraEye = this.train.vehiclePoint(0, CAB_EYE[0] + x, CAB_EYE[1] + y, CAB_EYE[2]); - this.cameraTarget = this.train.vehiclePoint(0, CAB_AIM[0] + x * 0.4, CAB_AIM[1] + y * 0.4, CAB_AIM[2]); - this.cameraUp = this.train.vehicleUp(0); - this.fov = 58.0; - this.near = 0.06; - return; - } - if (this.view == VIEW_SEAT) { - // The sun is low ahead on the +x side and its light travels back down the car, so both - // views look back along the train: the lit seats and the window are in frame together. - const t = this.shotTime; - const x = Math.max(0.0, Math.min(1.0, t / 11.0)); - const u = x * x * x * (x * (x * 6.0 - 15.0) + 10.0); - const breathe = Math.sin(t * 0.55) * 0.012; - const side = this.args.side; - const zk = SEAT_BAYS[this.args.bay]; - if (this.args.variant == 0) { - // in the window seat: over the table to the sunlit chair facing, the country beside it - this.cameraEye = this.train.vehiclePoint(SEAT_COACH, side * (0.6 - 0.03 * u), SEAT_FLOOR + 1.25 + breathe, zk + 0.9 - 0.12 * u); - this.cameraTarget = this.train.vehiclePoint(SEAT_COACH, side * (1.08 - 0.03 * u), 2.14 + breathe, zk - 0.98 + 0.16 * u); - this.fov = 60.0; - } else { - // across the aisle: one table, its lamp and the sunlit armchair against the window - this.cameraEye = this.train.vehiclePoint(SEAT_COACH, -side * (0.4 - 0.08 * u), SEAT_FLOOR + 1.31 + breathe, zk + 0.8 - 0.12 * u); - this.cameraTarget = this.train.vehiclePoint(SEAT_COACH, side * 1.0, 2.295 + breathe * 0.5, zk - 0.62 + 0.12 * u); - this.fov = 52.0; + if (this.inspect) { + if (this.args.near > 0.0) { + this.near = this.args.near; + } + if (this.args.vehicle >= 0) { + const e = this.args.eye, t = this.args.target; + this.cameraEye = this.train.vehiclePoint(this.args.vehicle, e[0], e[1], e[2]); + this.cameraTarget = this.train.vehiclePoint(this.args.vehicle, t[0], t[1], t[2]); + this.cameraUp = this.train.vehicleUp(this.args.vehicle); + return; } - this.cameraUp = this.train.vehicleUp(SEAT_COACH); - this.near = 0.04; - return; - } - if (this.args.near > 0.0) { - this.near = this.args.near; - } - if (!this.riding && this.args.vehicle >= 0) { - const e = this.args.eye, t = this.args.target; - this.cameraEye = this.train.vehiclePoint(this.args.vehicle, e[0], e[1], e[2]); - this.cameraTarget = this.train.vehiclePoint(this.args.vehicle, t[0], t[1], t[2]); - this.cameraUp = this.train.vehicleUp(this.args.vehicle); - return; - } - if (!this.riding) { for (let k = 0; k < 3; k++) { this.cameraEye[k] = this.args.eye[k]; this.cameraTarget[k] = this.args.target[k]; } return; } - const z = track.zOfS(this.s); - const ahead = track.zOfS(this.s + 140.0); - this.cameraEye[0] = track.X(z) + 9.0; - this.cameraEye[1] = track.Y(z) + 5.5; - this.cameraEye[2] = z; - this.cameraTarget[0] = track.X(ahead) + 4.0; - this.cameraTarget[1] = track.Y(ahead) + 1.5; - this.cameraTarget[2] = ahead; + // The running shot, its vehicle-space shots (the cab, the window seat) placed with the train. + const pose = this.cameraman.pose; + if (pose.vehicle >= 0) { + this.cameraEye = this.train.vehiclePoint(pose.vehicle, pose.px, pose.py, pose.pz); + this.cameraTarget = this.train.vehiclePoint(pose.vehicle, pose.tx, pose.ty, pose.tz); + this.cameraUp = this.train.vehicleUp(pose.vehicle); + } else { + this.cameraEye = [pose.px, pose.py, pose.pz]; + this.cameraTarget = [pose.tx, pose.ty, pose.tz]; + } + this.fov = pose.fov; + this.near = pose.near; + if (pose.roll != 0.0) { + // three's camera.rotateZ(roll) after lookAt: the up turned about the line of sight + const e = this.cameraEye, t = this.cameraTarget, u = this.cameraUp; + let bx = e[0] - t[0], by = e[1] - t[1], bz = e[2] - t[2]; + const bl = Math.sqrt(bx * bx + by * by + bz * bz); + bx /= bl; + by /= bl; + bz /= bl; + let rx = u[1] * bz - u[2] * by, ry = u[2] * bx - u[0] * bz, rz = u[0] * by - u[1] * bx; + const rl = Math.sqrt(rx * rx + ry * ry + rz * rz); + rx /= rl; + ry /= rl; + rz /= rl; + const ux = by * rz - bz * ry, uy = bz * rx - bx * rz, uz = bx * ry - by * rx; + const cr = Math.cos(pose.roll), sr = Math.sin(pose.roll); + this.cameraUp = [ux * cr - rx * sr, uy * cr - ry * sr, uz * cr - rz * sr]; + } } // Keeps the chunks about the camera on the GPU: all of them at once at the start, then at @@ -989,6 +1151,7 @@ namespace GoldenLine { f.sFront = this.trainFront(); f.speed = CRUISE; f.interior = this.view == VIEW_SEAT; + f.aerialShot = this.inspect || this.cameraman.shot == SHOT_DRONE; f.tunnelFactor = this.structures.tunnelFactor(eye[0], eye[1], eye[2]); f.originZ = originZ; // the engine: where it is and which way a tunnel mouth ahead of it lies @@ -1005,6 +1168,7 @@ namespace GoldenLine { this.steamCut = false; } this.fillSteamLight(); + this.steamLight.dazzle = this.dazzle; steam.update(commandList, this.steamLight, width, height); } @@ -1182,7 +1346,7 @@ namespace GoldenLine { this.setColor(G_FOG_SUN, C_FOG_SUN, 1.0, p[FOG_DENSITY]); this.setColor(G_FOG_AWAY, C_FOG_AWAY, 1.0, p[VALLEY_DENSITY]); // The valley mist lies about 33 m below the line at the ride's position. - const rideZ = this.view != VIEW_OUTSIDE ? eye[2] : this.riding ? this.world.value.track.zOfS(this.s) : (this.args.base >= 0.0 ? this.args.base : eye[2]); + const rideZ = this.view != VIEW_OUTSIDE ? eye[2] : !this.inspect ? this.world.value.track.zOfS(this.s) : (this.args.base >= 0.0 ? this.args.base : eye[2]); this.set4(G_VALLEY, this.world.value.track.Y(rideZ) - 33.0, 1.0 / 19.0, 1.0 - inside * 0.9, p[CLOUD_AMOUNT]); this.setColor(G_CLOUD_LIT, C_CLOUD_LIT, 1.0, p[STARS]); this.setColor(G_CLOUD_SHADE, C_CLOUD_SHADE, 1.0, p[GROUND_FILL]); @@ -1211,7 +1375,12 @@ namespace GoldenLine { const tau = eyeTarget > this.exposureAdapt ? 1.3 : 0.62; this.exposureAdapt += (eyeTarget - this.exposureAdapt) * (1.0 - Math.exp(-this.lastDt / tau)); } - this.set4(G_TONE, p[EXPOSURE] * this.exposureAdapt, width, height, 0.0); + // A shot may also work the eye itself (the tunnel exit: dazzled as the engine breaks out into the + // sun, with more glare off bright things). After dusk there is nothing to be dazzled by. + const daylight = 1.0 - 0.8 * p[LAMPS]; + this.dazzle = this.inspect ? 0.0 : (this.cameraman.exposure - 1.0) * daylight; + this.shotGlare = this.inspect ? 0.0 : this.cameraman.glare * daylight; + this.set4(G_TONE, p[EXPOSURE] * this.exposureAdapt * (1.0 + this.dazzle), width, height, 0.0); this.set4(G_MIRROR, 0.0, 0.0, p[LAMPS], 0.0); this.water.update(this.time, 1.0 / tanX, 1.0 / tanY, width, height); } @@ -1321,6 +1490,9 @@ namespace GoldenLine { const commandList = frame.getCommandList(); const firstFrame = !this.hdr; + // (everything streamed in at once: the first frame, a test still, or the train sent elsewhere) + const urgent = firstFrame || this.args.test || this.jumped; + this.jumped = false; if (!this.hdrFramebuffer) { this.createTargets(width, height); } @@ -1336,14 +1508,14 @@ namespace GoldenLine { } // The train first: the camera may ride in it. - this.train.place(this.trainFront(), this.time, CRUISE, this.lastDt, this.tod.numbers[LAMPS], firstFrame || this.args.test); + this.train.place(this.trainFront(), this.time, CRUISE, this.lastDt, this.tod.numbers[LAMPS], urgent); this.placeCamera(); // The floating origin follows the ride in 1024 m steps. const focusZ = this.view != VIEW_OUTSIDE || this.args.vehicle >= 0 ? this.cameraEye[2] - : this.riding ? this.world.value.track.zOfS(this.s) : (this.args.base >= 0.0 ? this.args.base : this.cameraEye[2]); + : !this.inspect ? this.world.value.track.zOfS(this.s) : (this.args.base >= 0.0 ? this.args.base : this.cameraEye[2]); const originZ = Math.floor(focusZ / ORIGIN_STEP) * ORIGIN_STEP; - this.streamChunks(commandList, this.cameraEye[2], firstFrame || this.args.test); - this.water.stream(commandList, this.cameraEye[2], firstFrame || this.args.test); + this.streamChunks(commandList, this.cameraEye[2], urgent); + this.water.stream(commandList, this.cameraEye[2], urgent); // The reeds grow along the shallows of the water bodies streamed in. for (const src of this.water.takeFresh()) { this.grass.addWater(src); @@ -1360,7 +1532,16 @@ namespace GoldenLine { // The sky's light, for the ground's environment term. commandList.dispatch(this.envPipeline, this.envSet, 1, 1, 1); - // The trees in view, sorted into their draw lists. + // The trees in view, sorted into their draw lists; kept out of the shot's viewpoint. + const shot = this.cameraman.shot; + const rule = this.inspect ? VEG_CLEAR_NONE : shot == SHOT_BRIDGE ? VEG_CLEAR_BRIDGE : shot == SHOT_TRACKSIDE ? VEG_CLEAR_TRACKSIDE + : shot == SHOT_DRONE ? VEG_CLEAR_DRONE : VEG_CLEAR_NONE; + const fl = Math.sqrt((this.cameraTarget[0] - this.cameraEye[0]) * (this.cameraTarget[0] - this.cameraEye[0]) + + (this.cameraTarget[1] - this.cameraEye[1]) * (this.cameraTarget[1] - this.cameraEye[1]) + + (this.cameraTarget[2] - this.cameraEye[2]) * (this.cameraTarget[2] - this.cameraEye[2])); + this.trees.keepViewOpen(rule, this.cameraman.cuts, this.treesJumped, this.cameraEye[0], this.cameraEye[1], this.cameraEye[2], + (this.cameraTarget[1] - this.cameraEye[1]) / fl, this.fov, originZ, this.cameraEye[0], this.cameraEye[2] - originZ); + this.treesJumped = false; this.trees.cull(commandList, originZ, this.constants, this.cameraEye[0], this.cameraEye[1], this.cameraEye[2] - originZ, deg(this.fov), this.time); // The grass, wheat and flowers in view, placed into their draw lists. @@ -1369,14 +1550,16 @@ namespace GoldenLine { this.cameraEye[0], this.cameraEye[1], this.cameraEye[2] - originZ, this.view == VIEW_SEAT); this.updateMotes(height, originZ); this.grass.update(commandList, originZ, this.constants, this.cameraEye[0], this.cameraEye[1], this.cameraEye[2] - originZ, - this.time, firstFrame || this.args.test); + this.time, urgent); // The permanent way and everything beside the line: the tunnel state, the stone window and the // lineside pools re-packed about the camera. // (its engine lights the bore it runs through: index.js's LOCO_ANCHORS) this.structures.setEngine(this.train.vehiclePoint(0, 0.0, 2.45, -4.9), this.train.vehiclePoint(0, 0.0, 1.95, 6.85), this.train.vehiclePoint(0, 0.0, 0.0, 0.0)[2], this.tod.numbers[LAMPS]); + this.structures.dazzle = this.dazzle; + this.structures.setClearPoles(this.inspect ? [] : this.cameraman.clearPoles()); this.structures.update(commandList, originZ, this.cameraEye[0], this.cameraEye[1], this.cameraEye[2], - this.cameraTarget[2] - this.cameraEye[2], this.view != VIEW_OUTSIDE, firstFrame || this.args.test); + this.cameraTarget[2] - this.cameraEye[2], this.view != VIEW_OUTSIDE, urgent); // The sun's shadow cascades, each drawn from its own camera (render/lighting.js). if (this.shadows.on) { @@ -1413,9 +1596,9 @@ namespace GoldenLine { // The dust in the sunbeams of the camera coach. this.motes.draw(commandList, frame, hdrFramebuffer); // The glow of whatever is brighter than daylight, wider while the eye is open in a bore (main.js's - // setGlare; the shots' own dazzle isn't ported). + // setGlare, with the shot's own glare). const open = this.view == VIEW_SEAT ? 0.0 : Math.min(1.0, Math.max(0.0, (this.exposureAdapt - 1.0) / (EYE_OPEN - 1.0))); - this.bloom.setGlare(open, 0.0); + this.bloom.setGlare(open, this.shotGlare); this.bloom.draw(frame); frame.beginDrawToFramebuffer(tonemapPipeline, ldrFramebuffer); @@ -1489,16 +1672,15 @@ namespace GoldenLine { } tod.speed = ImGui.sliderFloat("Day speed", tod.speed, 0.0, 20.0); this.pass.riding = ImGui.checkbox("Ride", this.pass.riding); - if (ImGui.radioButton("Outside", this.pass.view == VIEW_OUTSIDE)) { - this.pass.setView(VIEW_OUTSIDE); - } - ImGui.sameLine(); - if (ImGui.radioButton("Driver's cab", this.pass.view == VIEW_CAB)) { - this.pass.setView(VIEW_CAB); - } - ImGui.sameLine(); - if (ImGui.radioButton("Window seat", this.pass.view == VIEW_SEAT)) { - this.pass.setView(VIEW_SEAT); + // the original's camera keys (shots.js's labels) + const shot = this.pass.shot; + for (let k = 0; k < GL_SHOT_LABELS.length; k++) { + if (k > 0 && k != 3) { + ImGui.sameLine(); + } + if (ImGui.radioButton(GL_SHOT_LABELS[k], shot == GL_SHOT_ORDER[k])) { + this.pass.setShot(GL_SHOT_ORDER[k]); + } } ImGui.end(); } diff --git a/shaders/golden_line_trees.hlsl b/shaders/golden_line_trees.hlsl index 4757934..b41c0d2 100644 --- a/shaders/golden_line_trees.hlsl +++ b/shaders/golden_line_trees.hlsl @@ -78,8 +78,15 @@ struct TreeGlobals float4 wind; // xy: direction; z: time (seconds, wrapped); w: strength float4 cascades[4]; // the view-depth slice each shadow cascade covers, as a sphere (centre, radius) float4 sun; // xyz: away from the sun; w: the slope of its rays (rise over run, at least 0.035) + // The rule that keeps a viewpoint open (trees.js's clearA, clearB). A: camera x, render z, sight slope, + // eye height. B: mode (0 none, 1 a still view, 2 the drone), reach in metres, the camera's offset from + // the line, rail height. + float4 clearA; + float4 clearB; }; +static const float LATERAL_BIAS = 2600; + cbuffer c_Trees : register(b2) { TreeGlobals tg; @@ -230,6 +237,12 @@ struct Instance float horizon; }; +// 1 for trees in the foreground of a wide static view (the viaduct shot, which holds reach 190). +float wideView(float3 base) +{ + return step(0.5, tg.clearB.x) * step(tg.clearB.x, 1.5) * step(100, tg.clearB.y) * sstep(230, 170, length(base.xz - tg.clearA.xy)); +} + Instance readInstance(uint instance) { uint idx = t_visible[draw.offset + instance]; @@ -244,7 +257,29 @@ Instance readInstance(uint instance) // b.x packs the offset from the line (half metres) with the yaw. float packed = floor(b.x / 8); inst.yaw = b.x - packed * 8; - inst.scale = a.w; + float lateral = packed * 0.5 - LATERAL_BIAS; + // A tree that would wall off the view grows only as high as the sight line allows, so the ground in + // front of a viewpoint stays wooded with young growth. The shadow casters follow the same rule. + float4 cA = tg.clearA, cB = tg.clearB; + float H = protoDims(inst.proto).x * a.w; + float level = length(inst.base.xz - cA.xy); + float radialTop = cA.w + level * cA.z; + float across = lateral / cB.z; // 0 on the line, 1 level with the camera + // (kept well under the lens's line to the wheels, so no crown ever stands across the running gear) + float planeTop = cB.w + 1.2 + (cA.w - cB.w - 1.5) * saturate(across) * 0.72; + float inBand = sstep(-0.25, 0.0, across) * (1 - sstep(abs(cB.z) + 10, abs(cB.z) + 16, abs(lateral))); + bool chase = cB.x > 1.5; + // The camera looks down, so a crown just under the drone's own flight line still fills the bottom of + // the frame. In that strip (again by distance from the track only) tops stay well below the lens. + float underLens = sstep(abs(cB.z) - 26, abs(cB.z) - 17, abs(lateral)); + float chaseTop = lerp(planeTop, min(planeTop, cB.w + max((cA.w - cB.w) * 0.34, 2.8)), underLens); + float allowed = (chase ? chaseTop : radialTop) - inst.base.y; + float weight = saturate((cB.y - level) / (chase ? 90.0 : 24.0)) * (chase ? inBand : 1.0) * step(0.5, cB.x); + float shrink = lerp(1, clamp(allowed / max(H, 0.1), 0.16, 1), weight); + // The trees right under a long viewpoint are seen as a row of crowns: give them very unlike heights. + float uneven = frac(inst.rnd * 23.17) * 0.58 + 0.64; + inst.scale = a.w * shrink * lerp(1, uneven, wideView(inst.base) * (weight > 0.01 ? 0.45 : 1.0)); + inst.widthJitter *= (1 - shrink) * 1.3 + 1; inst.tint = b.y; inst.horizon = b.w; return inst; @@ -285,17 +320,23 @@ float handOver(Instance inst) void instanceColours(Instance inst, out float3 colA, out float3 colB) { uint p = inst.proto; - float t1 = sstep(0.0, 0.55, inst.tint); - float t2 = sstep(0.55, 1.0, inst.tint); + // (the same goes for the yellowest tints of birch and poplar in the foreground of a wide view) + float wide = wideView(inst.base); + float tone = inst.tint * (1 - wide * 0.62); + float t1 = sstep(0.0, 0.55, tone); + float t2 = sstep(0.55, 1.0, tone); float3 A = lerp(lerp(palette(p, 0), palette(p, 2), t1), palette(p, 4), t2); float3 B = lerp(lerp(palette(p, 1), palette(p, 3), t1), palette(p, 5), t2); - bool autumn = frac(inst.rnd * 13.71) < protoDims(p).w; + // No blazing autumn crown in the foreground of a wide view: at the edge of the frame it reads as a card. + bool autumn = frac(inst.rnd * 13.71) < protoDims(p).w * (1 - wide); float4 drift = t_noise.SampleLevel(s_wrap, inst.base.xz / 256, 0); float4 drift2 = t_noise.SampleLevel(s_wrap, inst.base.xz / 64 + float2(0.31, 0.12), 0); float bright = (frac(inst.rnd * 29.37) * 0.34 + 0.82) * ((drift.g - 0.5) * 0.7 + (drift2.r - 0.5) * 0.45 + 1); float3 warm = 1 + float3(0.3, 0.1, -0.12) * (drift.r - 0.5); colA = (autumn ? palette(p, 6) : A) * bright * warm; - colB = (autumn ? palette(p, 7) : B) * bright * warm; + // Under the wide view the leaves are turned toward olive: a yellow crown cut by the frame reads as a card. + float3 lighter = lerp(B, float3(B.y * 0.55, B.y * 0.7, B.z), wide); + colB = (autumn ? palette(p, 7) : lighter) * bright * warm; } struct TreeVertex