From 44c49c262792beec49abc0fea3c7c2f20f1a41f0 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 5 Dec 2026 11:43:54 +0000 Subject: [PATCH] Add the headlight to the golden_line example After dusk the engine's headlight (train/index.js's SpotLight) throws its pool on the line ahead: terrain, track, trees, grass and the train take it with plain physical shading, as the original's lamps keep it. Co-Authored-By: Claude Opus 5.5 --- core/golden_line_train.ts | 13 +++++++++++++ examples/golden_line.ts | 21 +++++++++++++++++---- shaders/golden_line.hlsl | 11 +++++++++++ shaders/golden_line_common.hlsli | 22 ++++++++++++++++++++++ shaders/golden_line_grass.hlsl | 10 ++++++++++ shaders/golden_line_structures.hlsl | 10 ++++++++++ shaders/golden_line_train.hlsl | 11 +++++++++++ shaders/golden_line_trees.hlsl | 18 ++++++++++++++++++ 8 files changed, 112 insertions(+), 4 deletions(-) diff --git a/core/golden_line_train.ts b/core/golden_line_train.ts index c38c469..a935f13 100644 --- a/core/golden_line_train.ts +++ b/core/golden_line_train.ts @@ -949,6 +949,19 @@ export class TrainRenderer { out[o + 14] = v.horizon; } + /** + * The engine's headlight for a train placed by place() with its front buffer at sFront (train/index.js's + * lights): [lamp x, y, z, aim x, y, z (true world), intensity in candela]. It shines on a point of the line + * 15 m ahead, a little below the rail, and comes up with the engine's lamps. + */ + headlight(sFront: number): number[] { + const p = this.vehiclePoint(0, 0.0, ANCHOR_HEADLAMP[1], ANCHOR_HEADLAMP[2] + 0.12); + const f = new TrackFrame(); + trackFrameAt(this.world.value.track, sFront + 15.0, f); + const lit = trnSmooth(0.08, 0.75, this.vehicles[0].lamps); + return [p[0], p[1], p[2], f.x, f.y - 0.35, f.z, 185.0 * lit]; + } + /** A new view (train/index.js's onCut): the next update settles at once instead of easing. */ cut() { this.snap = true; diff --git a/examples/golden_line.ts b/examples/golden_line.ts index fde764a..72a46f4 100644 --- a/examples/golden_line.ts +++ b/examples/golden_line.ts @@ -53,9 +53,10 @@ import { Birds } from "../core/golden_line_birds"; // of the original's capture presets; and the director (cameraman.js: -director, the D key or the panel), // which cuts from take to take, timing a cut to a tunnel exit or a viaduct coming up and sending the train on // down the line when the next take needs somewhere else, once that place has streamed in beside the train's -// own; by hand the keys 1-5 and C choose a camera, and dragging with the left button looks about. -// -clock holds the ride's clock still, as the original's captures are taken. The headlight's beam on -// the line after dusk, the throttle (W/S), photo mode and the sound aren't ported yet. +// own; by hand the keys 1-5 and C choose a camera, and dragging with the left button looks about. After +// dusk the engine's headlight throws its pool on the line ahead (train/index.js's SpotLight). +// -clock holds the ride's clock still, as the original's captures are taken. The throttle (W/S), +// photo mode 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; @@ -155,7 +156,9 @@ namespace GoldenLine { const G_RIDGE_SHIFT = 104; const G_TONE = 112; const G_MIRROR = 116; - const G_FLOATS = 120; + const G_HEAD_POS = 120; + const G_HEAD_DIR = 124; + const G_FLOATS = 128; // --- Time of day (src/render/timeofday.js) --------------------------------------------------- // golden hour -> sunset -> dusk -> blue hour, every value that depends on the hour keyed here. @@ -1568,6 +1571,16 @@ namespace GoldenLine { 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); + // the engine's headlight (train/index.js's SpotLight): the lamp (origin-relative) and its intensity, + // and the beam's axis + const head = this.train.headlight(this.trainFront()); + let hx = head[3] - head[0], hy = head[4] - head[1], hz = head[5] - head[2]; + const hl = Math.sqrt(hx * hx + hy * hy + hz * hz); + hx /= hl; + hy /= hl; + hz /= hl; + this.set4(G_HEAD_POS, head[0], head[1], head[2] - originZ, head[6]); + this.set4(G_HEAD_DIR, hx, hy, hz, 0.0); this.water.update(this.time, 1.0 / tanX, 1.0 / tanY, width, height); } diff --git a/shaders/golden_line.hlsl b/shaders/golden_line.hlsl index 1246900..885a457 100644 --- a/shaders/golden_line.hlsl +++ b/shaders/golden_line.hlsl @@ -353,6 +353,17 @@ float4 terrain_ps(TerrainVaryings i) : SV_Target // SunlitGroundModel: blades and stones that stand up catch the sun on their sides. directDiffuse += lightColor * albedo * max(lift - dotNL, 0) / PI; directDiffuse += lightColor * glowTerm; + // The engine's headlight after dusk: plain physical shading (the sunlit ground's lift and glow are the + // sun's alone), its specular share kept down as the sun's is. + if (g.headPos.w > 0) + { + float3 Lh; + float3 headColor = headlightAt(i.world, Lh); + float3 headIrradiance = saturate(dot(n, Lh)) * headColor; + float3 Fh = F_Schlick(f0, 1.0, saturate(dot(V, normalize(Lh + V)))); + directDiffuse += headIrradiance * albedo / PI * (1 - Fh); + directSpecular += headIrradiance * BRDF_GGX(Lh, V, n, f0, roughness) * multiScatteringCompensation * specularShare; + } // The hemisphere light and the environment. float3 hemi = lerp(g.hemiGround.rgb, g.hemiSky.rgb, n.y * 0.5 + 0.5); diff --git a/shaders/golden_line_common.hlsli b/shaders/golden_line_common.hlsli index 948f819..2787849 100644 --- a/shaders/golden_line_common.hlsli +++ b/shaders/golden_line_common.hlsli @@ -36,6 +36,8 @@ struct Globals float4 ridgeShift23; // layers 2 and 3 float4 tone; // exposure, width, height float4 mirror; // the water's level, 1 while drawing its mirror image (else 0); z: lamps (0 by day, 1 lit) + float4 headPos; // the engine's headlight (origin-relative); w: its intensity, 0 by day + float4 headDir; // the beam's axis }; cbuffer c_Globals : register(b0) @@ -360,6 +362,26 @@ float3 envRadiance(float3 n) #endif // three's BRDF_GGX (D_GGX, V_GGX_SmithCorrelated, F_Schlick). +// The engine's headlight (train/index.js): three's SpotLight(0xffd29a, intensity, 150 m, 0.45 rad, penumbra 1, +// decay 1.75), casting no shadow. The light arriving at P, and in L the direction toward the lamp. +static const float3 HEAD_COLOR = float3(1.0, 0.64447968196343608, 0.32314320910222849); // 0xffd29a, linear +static const float HEAD_CONE_COS = 0.90044710235267689; // cos(0.45) +static const float HEAD_REACH = 150.0; +static const float HEAD_DECAY = 1.75; + +float3 headlightAt(float3 P, out float3 L) +{ + float3 v = g.headPos.xyz - P; + float d = length(v); + L = v / max(d, 1e-6); + // (the penumbra's cosine is cos(0.45 * (1 - 1)) = 1: the beam feathered all the way in) + float spot = smoothstep(HEAD_CONE_COS, 1.0, dot(-L, g.headDir.xyz)); + float q = d / HEAD_REACH; + float cut = saturate(1 - q * q * q * q); + float falloff = 1.0 / max(pow(d, HEAD_DECAY), 0.01) * cut * cut; + return HEAD_COLOR * (g.headPos.w * spot * falloff); +} + float3 F_Schlick(float3 f0, float f90, float dotVH) { float fresnel = exp2((-5.55473 * dotVH - 6.98316) * dotVH); diff --git a/shaders/golden_line_grass.hlsl b/shaders/golden_line_grass.hlsl index a25fcf7..09b0ac6 100644 --- a/shaders/golden_line_grass.hlsl +++ b/shaders/golden_line_grass.hlsl @@ -348,6 +348,16 @@ float3 shadeBlade(Blade b, float3 world, float horizon, float shadow) float through = pow(saturate(dot(V, -L)), 3.0); float back = saturate(dot(n, L) * -0.7 + 0.3); direct += lightColor * b.transmit * (through + back * b.wrap); + // The engine's headlight after dusk, as deep into the crop as the sun gets (no rim: that is sunlight). + if (g.headPos.w > 0) + { + float3 Lh; + float3 headColor = headlightAt(world, Lh) * b.depth; + direct += headColor * saturate(dot(n, Lh)) * b.albedo / PI; + float throughH = pow(saturate(dot(V, -Lh)), 3.0); + float backH = saturate(dot(n, Lh) * -0.7 + 0.3); + direct += headColor * b.transmit * (throughH + backH * b.wrap); + } float3 hemi = lerp(g.hemiGround.rgb, g.hemiSky.rgb, n.y * 0.5 + 0.5); float3 indirect = (hemi / PI * b.fill + b.sky) * b.albedo * b.ao; return applyFog(max(direct + indirect + b.emissive, 0), world); diff --git a/shaders/golden_line_structures.hlsl b/shaders/golden_line_structures.hlsl index 841284d..0d174bb 100644 --- a/shaders/golden_line_structures.hlsl +++ b/shaders/golden_line_structures.hlsl @@ -295,6 +295,16 @@ float3 shade(Surface s, float3 world) // SunlitGroundModel: stones and blades that stand up catch the sun on their sides directDiffuse += lightColor * diffuseColor * max(s.lift - dotNL, 0) / PI; directDiffuse += lightColor * s.glow; + // the engine's headlight after dusk: plain physical shading, no shadow (the lift and glow are the sun's) + if (g.headPos.w > 0) + { + float3 Lh; + float3 headColor = headlightAt(world, Lh); + float3 headIrradiance = saturate(dot(n, Lh)) * headColor; + float3 Fh = F_Schlick(specularColor, 1.0, saturate(dot(V, normalize(Lh + V)))); + directDiffuse += headIrradiance * diffuseColor / PI * (1 - Fh); + directSpecular += headIrradiance * BRDF_GGX(Lh, V, n, specularBlended, r) * msComp * s.specular; + } float3 hemi = lerp(g.hemiGround.rgb, g.hemiSky.rgb, n.y * 0.5 + 0.5); float3 indirectDiffuse = hemi * diffuseColor / PI * (1 - (singleD + multiD)); diff --git a/shaders/golden_line_train.hlsl b/shaders/golden_line_train.hlsl index c732441..7f4dae7 100644 --- a/shaders/golden_line_train.hlsl +++ b/shaders/golden_line_train.hlsl @@ -263,6 +263,17 @@ float4 paint_ps(PaintVaryings i) : SV_Target float3 directDiffuse = irradiance * diffuseContribution / PI * (1 - F); float3 directSpecular = irradiance * BRDF_GGX_F90(L, V, n, specularBlended, specularF90, r) * msComp; float3 ccDirect = saturate(dot(n, L)) * lightColor * BRDF_GGX_F90(L, V, n, 0.04, 1.0, ccR); + // the engine's own headlight after dusk (no shadow, nor the tunnel's darkness: it is a lamp) + if (g.headPos.w > 0) + { + float3 Lh; + float3 headColor = headlightAt(i.world, Lh); + float3 headIrradiance = saturate(dot(n, Lh)) * headColor; + float3 Fh = F_Schlick(specularColor, specularF90, saturate(dot(V, normalize(Lh + V)))); + directDiffuse += headIrradiance * diffuseContribution / PI * (1 - Fh); + directSpecular += headIrradiance * BRDF_GGX_F90(Lh, V, n, specularBlended, specularF90, r) * msComp; + ccDirect += headIrradiance * BRDF_GGX_F90(Lh, V, n, 0.04, 1.0, ccR); + } // the hemisphere light and the environment float3 hemi = lerp(g.hemiGround.rgb, g.hemiSky.rgb, n.y * 0.5 + 0.5); diff --git a/shaders/golden_line_trees.hlsl b/shaders/golden_line_trees.hlsl index b41c0d2..f160992 100644 --- a/shaders/golden_line_trees.hlsl +++ b/shaders/golden_line_trees.hlsl @@ -483,6 +483,24 @@ float3 shade(Surface s, float3 world, float horizon) // Rough leaves have no mirror-like sky sheen at grazing angles. sheen = 0.18; } + // The engine's headlight after dusk, through the same model (the light through a leaf too), no shadow. + if (g.headPos.w > 0) + { + float3 Lh; + float3 headColor = headlightAt(world, Lh); + float3 headIrradiance = saturate(dot(n, Lh)) * headColor; + float3 Fh = F_Schlick(f0, 1.0, saturate(dot(V, normalize(Lh + V)))); + directDiffuse += headIrradiance * albedo / PI * (1 - Fh); + float3 headSpecular = headIrradiance * BRDF_GGX(Lh, V, n, f0, roughness) * multiScatteringCompensation; + if (s.thin) + { + headSpecular *= 0.12; + float through = pow(saturate(dot(V, -Lh)), 3.0); + float back = saturate(dot(n, Lh) * -0.7 + 0.3); + directDiffuse += headColor * s.transmit * (through + back * s.wrap); + } + directSpecular += headSpecular; + } // The hemisphere light and the environment. float3 hemi = lerp(g.hemiGround.rgb, g.hemiSky.rgb, n.y * 0.5 + 0.5);