Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/golden_line_train.ts
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,19 @@ export class TrainRenderer {
out[o + 14] = <f32>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;
Expand Down
21 changes: 17 additions & 4 deletions examples/golden_line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <seconds> 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 <seconds> 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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

Expand Down
11 changes: 11 additions & 0 deletions shaders/golden_line.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions shaders/golden_line_common.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions shaders/golden_line_grass.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions shaders/golden_line_structures.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
11 changes: 11 additions & 0 deletions shaders/golden_line_train.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions shaders/golden_line_trees.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading