From 8a4884a37784bfeb34e1ee497b5bb3a5439a979e Mon Sep 17 00:00:00 2001 From: Richard Zink Date: Wed, 14 Oct 2026 07:48:52 +0200 Subject: [PATCH] fix: align sun altitude with sunrise sunset --- background.html | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/background.html b/background.html index 353ac02..875f696 100644 --- a/background.html +++ b/background.html @@ -684,6 +684,11 @@ window.addEventListener('resize', scheduleFitInfoPanel); var SITE_LAT = (window.CONFIG && window.CONFIG.location && window.CONFIG.location.lat) || 51.5074; var SITE_LON = (window.CONFIG && window.CONFIG.location && window.CONFIG.location.lon) || -0.1278; + // SunCalc.getPosition returns the geometric centre altitude. The + // scene's sunrise/sunset times use the standard apparent upper-limb + // altitude (-0.833 deg: refraction + solar semidiameter), so the + // lighting altitude is shifted by the same amount. + var SUN_HORIZON_CORR_RAD = 0.833 * Math.PI / 180; var realDayProgress = 0; @@ -923,14 +928,15 @@ window.sunPalettes = [ function getColor(t) { var now = sceneNow(); var sunPos = SunCalc.getPosition(now, SITE_LAT, SITE_LON); + var sunAlt = sunPos.altitude + SUN_HORIZON_CORR_RAD; var moonIllum = SunCalc.getMoonIllumination(now); // The day palette covers sunAlt down to sunPalettes[0].alt // (-pi/2, deep below horizon). Only below that do we hand off // to the moon-based night palette. This makes twilight a // smooth transition through the day palette entries. - if (sunPos.altitude > window.sunPalettes[0].alt) { - return getDayColor(sunPos.altitude); + if (sunAlt > window.sunPalettes[0].alt) { + return getDayColor(sunAlt); } else { return getNightColor(moonIllum); } @@ -1986,7 +1992,8 @@ window.weatherThemes = { // Sun altitude in radians. Negative = below horizon (night). var _sunNow = sceneNow(); var _sunPos = SunCalc.getPosition(_sunNow, SITE_LAT, SITE_LON); - window.__currentSunAlt = _sunPos.altitude; + var _sunAlt = _sunPos.altitude + SUN_HORIZON_CORR_RAD; + window.__currentSunAlt = _sunAlt; window.__currentSceneDate = _sunNow; window.__flightDow = _sunNow.getDay(); // Unit vector towards the sun in world space. SunCalc azimuth @@ -1995,9 +2002,9 @@ window.weatherThemes = { // World frame: +X = west, +Z = north, +Y = up -> east is -X. var _sunBearingR = _sunPos.azimuth + Math.PI; window.__currentSunDir = [ - -Math.cos(_sunPos.altitude) * Math.sin(_sunBearingR), - Math.sin(_sunPos.altitude), - Math.cos(_sunPos.altitude) * Math.cos(_sunBearingR) + -Math.cos(_sunAlt) * Math.sin(_sunBearingR), + Math.sin(_sunAlt), + Math.cos(_sunAlt) * Math.cos(_sunBearingR) ]; // ---- Local sidereal time ------------------------------------- // Same formula SunCalc uses internally (Meeus): GMST in degrees