From e33daac636c252188b4b32641d159c2b9930de13 Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 18:38:20 +0300 Subject: [PATCH] HD sky, atmosphere and warmer soft sunlight; lighter rippled water (fork patch 21); doc updated Co-Authored-By: Claude Opus 5.5 --- docs/HD-RENDERER.md | 38 +- ...ork-world-models-client-particles-so.patch | 2 +- ...izon-mod-report-particles-and-sounds.patch | 2 +- ...-fork-full-screen-post-pass-for-mods.patch | 2 +- ...far-models-as-scaled-impostors-past-.patch | 2 +- ...er.set-views-draws-the-world-from-ot.patch | 2 +- ...er-tick-and-per-event-AirDef-plans-r.patch | 2 +- ...-up-to-1920x1080-full-screen-replays.patch | 2 +- ...ant-terrain-tiles-fog-measured-from-.patch | 2 +- ...id-so-mods-keep-state-per-world-API-.patch | 2 +- ...d-trial-runs-behind-feature-full-so-.patch | 2 +- ...ffer-pass-per-pixel-mod-point-lights.patch | 2 +- ...e-2-sun-and-moon-shadow-cascades-the.patch | 2 +- ...ached-until-a-resize-or-a-new-source.patch | 2 +- ...-per-16x16-tile-surface-and-halo-lis.patch | 2 +- ...pass-in-the-F3-timing-list-half-reso.patch | 2 +- ...only-sun-facing-quads-and-skip-burie.patch | 2 +- ...es-render.model-id-model-generation-.patch | 2 +- ...aling-HORIZON_HD_SCALE-world-G-buffe.patch | 2 +- ...-pass-colour-normal-depth-the-floor-.patch | 2 +- ...lla-texture-on-the-surface-the-biome.patch | 2 +- ...-a-physical-atmosphere-Rayleigh-Mie-.patch | 945 ++++++++++++++++++ 22 files changed, 996 insertions(+), 27 deletions(-) create mode 100644 horizon-patches/0021-HD-sky-and-light-a-physical-atmosphere-Rayleigh-Mie-.patch diff --git a/docs/HD-RENDERER.md b/docs/HD-RENDERER.md index f94c9f2..2112a51 100644 --- a/docs/HD-RENDERER.md +++ b/docs/HD-RENDERER.md @@ -8,8 +8,9 @@ HD — третий пресет графики мода (`/airdef shader off|on|hd`): отложенное освещение в стиле шейдерпаков (BSL/Complementary-класс), но написанное с нуля внутри нашего форка Horizon (Minecraft 26.3 на Rust/wgpu). -Код шейдерпаков (BSL, Complementary, Photon, Iris/OptiFine) **не копируем**: их лицензии запрещают перенос, а в -Horizon действует правило clean-room. Пишем по знанию техник (статьи, общеизвестные алгоритмы). +Код шейдерпаков (BSL, Complementary, Iris/OptiFine и прочих) **не копируем**: их лицензии запрещают перенос, а в +Horizon действует правило clean-room. Исключение — Photon (sixthsurge), коммит 26c2906ee02cc484359b835fba6993d443d45966: он под MIT, из него можно +переносить идеи и код **с указанием авторства** (комментарий у кода + `gfx/THIRD_PARTY_PHOTON.md`). Остальное пишем по знанию техник (статьи, общеизвестные алгоритмы). Пресеты должны оставаться: `off` (ваниль), `on` (пост-эффекты мода), `hd` (этот рендер), позже `rt` (аппаратная трассировка, отдельно). @@ -19,6 +20,7 @@ Horizon действует правило clean-room. Пишем по знани | Что | Где | |---|---| | Весь HD-конвейер: цели, проходы, WGSL-шейдеры (строками), тайлы света, AO, тени, bloom, tonemap, TAA | `horizon/crates/horizon-client/src/gfx/hd.rs` | +| Атмосфера: сборка текстуры неба (WGSL), чтение неба/дымки/тона света в шейдерах, цвет солнца (`sun_transmittance`) | `horizon/crates/horizon-client/src/gfx/hd_sky.rs` (+ `THIRD_PARTY_PHOTON.md`) | | Встраивание в кадр: G-буфер-проход, каскады теней, вызовы `hd.light` / `hd.finish`, джиттер, тайминги | `horizon/crates/horizon-client/src/gfx.rs` (`Renderer::render`, ищи `hd_on`, `AirDef fork:`) | | G-буфер террейна: `fs_gbuffer`, `fs_shadow` | `horizon/crates/horizon-client/src/terrain.wgsl` | | G-буфер сущностей и моделей мода: `fs_entity_gbuffer`, `fs_entity_shadow` | `horizon/crates/horizon-client/src/entity.wgsl` | @@ -43,6 +45,7 @@ cd .. && rm -f horizon-patches/*.patch && git -C horizon format-patch -q $(cat H (`Depth32Float`, 1 = близко, 0 = далеко). Позиции везде относительно камеры (camera-relative), мир — в `f64` на CPU. +0. **Небо** (`hd.sky`, проход `hd sky`, перед освещением): текстура 128×72 (`Rgba16Float`): 128 азимутов (от солнца, 0..π) × 64 высот (v = √(h/(π/2))), плюс строка 64 с «окружающим светом» (текселы 0 — зенит, 1 — среднее у горизонта). Каждый тексел — марш по лучу (32 шага) однократного рассеяния Рэлея/Ми + озон, плоский член многократного рассеяния (`MULTI_SCATTER`), солнце и луна (луна — напротив) как источники; пропускание до источника — аналитическое (Chapman, как у Photon). Зависит только от высоты солнца → перерисовывается, лишь когда она сдвинулась больше чем на 0.001. Читают её `fs_light` и композит воды (привязки 15 и 10). 1. **Каскады теней** (2 шт., `CASCADES = [40, 176]` блоков полуразмера, `SHADOW_SIZE = 2048`, массив глубины из 2 слоёв). Источник — солнце днём, луна ночью (`light_of`). Матрицы `cascades()` привязаны к текселям (тени не «ползут»). Во время прохода буфер глобалов мира подменяется на глобалы каскада (copy-трюк) и потом @@ -59,7 +62,7 @@ cd .. && rm -f horizon-patches/*.patch && git -C horizon format-patch -q $(cat H землёй решают потребители). Пайплайны `gbuffer_water` (секции) и `flat.water` (flat-путь). 3a. **GTAO** (полразрешения, `Rg16Float`: окклюзия, дистанция) + depth-aware блюр 5×5. 3 среза × 5 шагов, радиус 1.8 блока, шум IGN по кадру. Сила — `HdFrame.ambient.w` (`HORIZON_HD_AO=<число>`, 0 = выкл). -4. **Освещение** `fs_light` → `lit` (HDR): пиксель — вода, если `water_depth >= g` и перед водой в мировой глубине ничего нет +4. **Освещение** `fs_light` → `lit` (HDR): пиксель неба (глубина мира 0, Оверворлд, без env-тумана) — это небо атмосферы + то, что мир нарисовал поверх ванильного неба (`max(scene − vanilla_sky(view), 0)`: спрайты луны, звёзды; квадрат ванильного солнца вырезается вокруг направления солнца и заменяется круглым диском с гало); облака (глубина > 0, нет в G-буфере, y > 8, дальше 48 блоков) на закате/рассвете окрашиваются цветом солнца. Свет неба: ванильный `lightmap` × подкраска цветом неба (зенит сверху, горизонт сбоку; яркость 1) × форма по нормали (вверх 1.1, вниз 0.66), только там, где есть небесный свет. Тени: ближний каскад — PCSS-lite (поиск блокировщиков 8 выборок, ширина полутени от расстояния до блокировщика, 12 выборок спирали Фогеля, повёрнутых IGN; сглаживает TAA), дальний — 3×3. Туман: цвет — небо над горизонтом в направлении пикселя (`haze_colour`) + воздушная перспектива `1 − exp(−d/700)` × 0.5. Далее прежнее: пиксель — вода, если `water_depth >= g` и перед водой в мировой глубине ничего нет (`d - wd <= 0.2%`); тогда освещается **дно** по данным G-буфера (одна выборка тени, без AO), а в альфу `lit` пишется 0.5 (признак воды). `max(небо·ambient.x, блоки·ambient.y)·AO + солнце·N·L·тень + точечные источники` × albedo + эмиссия; затем ванильный туман заново, объёмные лучи (14 шагов, Henyey-Greenstein g=0.6), ореолы вокруг источников. Точечные источники — по тайлам 16×16 (`tile_lists`, буфер `tiles`: на тайл 4 слова — @@ -71,7 +74,7 @@ cd .. && rm -f horizon-patches/*.patch && git -C horizon format-patch -q $(cat H по глубине: не берутся банк/сущности перед водой); толщина = расстояние от поверхности до дна; поглощение `exp(-(1-цвет)·0.8·(t+0.3))` + рассеяние цветом воды при свете «объёма» (лайтмап + солнце с тенью). Отражение: SSR по глубине G-буфера (24 шага, растущие, джиттер IGN по кадру, 5 шагов бинарного уточнения, толщина `0.4+0.08·s`), при промахе — небо/туман (`sky_color`, `fog_color`, тусклее - под навесом). Френель Шлика F0=0.02 (SSR пропускается при F<0.04: взгляд вниз), блик солнца GGX (a=0.05) × тень, туман поверх. + под навесом). Волны: к 10 крупным добавлены 16 мелких (длины 0.35–3 блока, уклон до 0.05, гасятся по размеру пикселя) — густая мелкая рябь для искорок блика; блик GGX с a=0.07. Цвет воды светлее: биом × 0.85, поглощение 0.8, рассеяние 0.85 (ночью до 0.4). Небо отражения и туман — из атмосферы. Френель Шлика F0=0.02 (SSR пропускается при F<0.04: взгляд вниз), блик солнца GGX (a=0.05) × тень, туман поверх. Подводный вид и `HORIZON_HD_WATER=0` — композит и проход воды не работают, `lit` как раньше. 5. **Пост-проход мода** `lit` → `fx` (`effects.wgsl`: взрывы, дым, вспышки, свечение; при HD его собственная «подсветка огнём» выключена — свет даёт HD). @@ -105,12 +108,13 @@ Bind groups кэшируются и пересоздаются только пр | 12 | тайлы источников (storage) | | 13 | AO | | 14 | глубина воды (`water_depth`) | +| 15 | текстура неба (`sky_lut`) | `HdFrame` (Rust и WGSL держать в синхроне!): `inverse_view_proj`, `resolution` (w, h, 1/w, 1/h), `params` (число источников, экспозиция, bloom, счётчик кадров), `cascades[2]`, `sun` (направление, сила), `sun_color` (rgb, ночь), -`fog_color`, `fog`, `ambient` (тень неба, блоки, плотность воздуха, AO), `shadow` (вкл, R0, R1, тайлов в строке), `prev_view_proj`, `taa`, `taa_params`, `view_proj` (с джиттером, для лучей воды), `water` (x: эффект идёт, y: секунды волн mod 600, zw: камера x, z mod 256), `sky_color` (w: есть небо). +`fog_color`, `fog`, `ambient` (тень неба, блоки, плотность воздуха, AO), `shadow` (вкл, R0, R1, тайлов в строке), `prev_view_proj`, `taa`, `taa_params`, `view_proj` (с джиттером, для лучей воды), `water` (x: эффект идёт, y: секунды волн mod 600, zw: камера x, z mod 256), `sky_color` (w: есть небо), `sky_sun` (xyz: на само солнце, w: 1 — небо рисует атмосфера), `sky_extra` (x: конец ванильного тумана неба, z: пасмурность от дождя 0..0.85), `sunrise` (ванильная полоса восхода/заката — вычитается из мира). -Композит воды (`WATER`, group 0): 0 `HdFrame`, 1 `lit`, 2 глубина G-буфера, 3 цвет воды, 4 нормаль воды, 5 глубина воды, 6–7 тени, 8 лайтмап, 9 сэмплер. +Композит воды (`WATER`, group 0): 0 `HdFrame`, 1 `lit`, 2 глубина G-буфера, 3 цвет воды, 4 нормаль воды, 5 глубина воды, 6–7 тени, 8 лайтмап, 9 сэмплер, 10 текстура неба. Проход неба (`SKY_PASS`, group 0): только `HdFrame`. ## Как проверять @@ -166,6 +170,8 @@ TAAU (2560×1440, GPU avg кадр, сцена Polygon): scale 1.0 — 18.1 мс | TAAU: мир в `HORIZON_HD_SCALE` (0.25–1, по умолчанию 1 = как раньше), восстановление в TAA-проходе `fs_taau`; без LOD-bias текстур (не доступен) | готово | патч 18 | | Исправлено мерцание теней у игрока: ближний каскад терял секции в кадры, когда дальний держится; держимая матрица дальнего каскада сдвигается на шаг камеры; у растений (наклонные квады) нормаль «вверх», а не к камере; TAA держит 94% истории в покое | готово | патч 18 | | Вода: проход `fs_water`, преломление, SSR, Френель, блик (`HORIZON_HD_WATER=0` — выкл) | готово | патч 19 (+20: без ванильной текстуры на поверхности, цвет темнее и глубже) | +| Вода: светлее, густая мелкая рябь (26 волн), блик a=0.07 (мерцание на реке ≈1.4% пикселей >20/255) | готово | патч 21 | +| Небо и атмосфера (Рэлей/Ми/озон, текстура неба, воздушная перспектива, цвет солнца из пропускания, голубоватый ambient по нормали, PCSS-lite ближнего каскада, диск солнца, цвет облаков на закате; порт констант и Chapman из Photon, MIT) | готово | патч 22 | | API моделей по хэндлам (#30, уровень API 11: `model-id`, `model-generation`, `set-models-by-id`) | готово | патч 17 | ## План (по порядку) и ТЗ @@ -181,7 +187,7 @@ TAAU (2560×1440, GPU avg кадр, сцена Polygon): scale 1.0 — 18.1 мс 2. **Вода** — СДЕЛАНО (патч 19, см. «Вода» ниже). Исходное ТЗ: отдельный G-буфер-путь для воды (маска + нормаль с анимированными волнами), SSR (трассировка по глубине, откат на небо/туман), преломление (смещение по нормали), затухание по глубине воды (цвет биома), блики солнца (GGX-подобный), френель. Подводный вид не ломать (`frame.underwater`). -3. **Небо и атмосфера**: рассеяние Рэлея/Ми (предвычисленная LUT пропускания и небесного цвета в маленькой +3. **Небо и атмосфера** — СДЕЛАНО (патч 22, см. «Небо и свет» ниже). Исходное ТЗ: рассеяние Рэлея/Ми (предвычисленная LUT пропускания и небесного цвета в маленькой текстуре), солнце/луна дисками, звёзды из ванили; туман и лучи берут цвет из той же модели. Учесть дождь (`weather`), закат, Незер/Энд (там — ванильное небо). 4. **Объёмные облака** (рэймарч слоя с шумом) + их тени на земле (в карту/маску солнца). @@ -208,8 +214,18 @@ TAAU (2560×1440, GPU avg кадр, сцена Polygon): scale 1.0 — 18.1 мс пена у берега, подводная каустика не сделаны; ночью вода слегка ярче неба (нужно привязать к модели неба — п. 4 плана «Небо»); отражение солнечного диска — только блик GGX (без диска в SSR). Проверка мерцания на реке: разница двух скриншотов ≈1.6% пикселей >20/255 (норма ~2%). +## Небо и свет (патч 22): что знать преемнику + +- Тайминги (M5, 2560×1440, Hills, река в кадре): `hd sky` ≈ 2.4–4.7 мс по таймстемпам, но кадр не вырос (GPU avg 18.4 мс против 18.1) — на Metal метка крошечного прохода ловит ожидание соседних; с кэшем по высоте солнца в покое проход не выполняется. Освещение 4.0–5.1 мс (PCSS-lite и небо почти не добавили), композит воды 0.6–2.4, TAA 2.6–2.9. +- Яркость: небо — физическая радиация → сжатие по максимальному каналу (`SKY_EXPOSURE` 0.85) → гамма 1/1.9 → насыщенность 1.2 (+0.45 у низкого солнца). Весь конвейер в гамма-пространстве, поэтому цвета неба и подкраски света — «показанные» значения; подкраска света неба имеет яркость 1 и не меняет экспозицию (яркость задаёт ванильный лайтмап × `ambient.x` = 0.62 днём, 0.95 ночью). +- Цвет солнца (`light_of`): пропускание атмосферы вдоль солнца (`hd_sky::sun_transmittance`), нормировано на 1, степень 0.6, × (1, 0.97, 0.88). Луна — холодная, как раньше. +- Дождь: `sky_extra.z` смешивает небо/дымку с ванильным градиентом (серым от ванили); Нижний мир/Энд/подводное — `sky_sun.w = 0`, всё как раньше (ванильное небо и туман). +- Облака — по-прежнему ванильные блоки; объёмные облака — п. 4 плана. Окраска облаков цветом закатного солнца — эвристика по маске (глубина есть, G-буфера нет, высота и дальность), может задеть далёкие частицы. +- Идеи дальше: многократное рассеяние по Hillaire вместо плоского члена, тени облаков, диск луны с фазой, звёзды/галактика по Photon (`sky.glsl`), розовый оттенок заката (`get_sun_tint` у Photon). + ## Правила +- Код Photon (MIT) переносить можно только с атрибуцией (см. «Что это»); код других шейдерпаков — нельзя. - Ничего не ломать в `off`/`on`; HD выключен — код HD не работает вовсе (`hd = None`). - Без потери качества ради скорости без договорённости (шаги, разрешение, число эффектов). - Каждый новый WGSL — в naga-тест. Каждый новый проход — в тайминги (`PassTiming`). @@ -217,3 +233,11 @@ TAAU (2560×1440, GPU avg кадр, сцена Polygon): scale 1.0 — 18.1 мс - Коммиты: в форке, затем патчи; подпись `Co-Authored-By: Claude <модель> `. - Пуш в Gitea с машины без DNS: `ssh://git@100.92.6.40:2222/chexit/airdef.git` (ключ `~/Documents/claude-keys/deploy`). - Обнови этот документ (статус + что изменилось в конвейере) в том же коммите, что и код. + +## Тюнинг после проверки на полигоне (патч 22) + +Голубая заливка неба по умолчанию делала белый бетон «снегом»: оттенок неба в заливке ослаблен до 0.4 +(`sky_light_tint`, в `fs_light`), заливка днём `ambient.x` = 0.5 (+0.45 ночью), солнце 0.7 и теплее +(`light_of`: множитель (1, 0.93, 0.78)). Проверять баланс на `Polygon` в полдень (`time set 6000`) — белое должно +оставаться белым с тёплым оттенком. + diff --git a/horizon-patches/0001-Mod-API-AirDef-fork-world-models-client-particles-so.patch b/horizon-patches/0001-Mod-API-AirDef-fork-world-models-client-particles-so.patch index a2b4ce5..50c66ab 100644 --- a/horizon-patches/0001-Mod-API-AirDef-fork-world-models-client-particles-so.patch +++ b/horizon-patches/0001-Mod-API-AirDef-fork-world-models-client-particles-so.patch @@ -1,7 +1,7 @@ From 3de7a6875140a107e28563cc44489b55f5794517 Mon Sep 17 00:00:00 2001 From: chexit Date: Wed, 28 Oct 2026 12:37:45 +0300 -Subject: [PATCH 01/20] Mod API (AirDef fork): world models, client particles, +Subject: [PATCH 01/21] Mod API (AirDef fork): world models, client particles, sounds, ground heights render.set-models draws block models with an interpolated pose per id; diff --git a/horizon-patches/0002-cargo-horizon-mod-report-particles-and-sounds.patch b/horizon-patches/0002-cargo-horizon-mod-report-particles-and-sounds.patch index 011e65c..837f395 100644 --- a/horizon-patches/0002-cargo-horizon-mod-report-particles-and-sounds.patch +++ b/horizon-patches/0002-cargo-horizon-mod-report-particles-and-sounds.patch @@ -1,7 +1,7 @@ From a6d64e6415bf618539af464e521091e72b72d72f Mon Sep 17 00:00:00 2001 From: chexit Date: Wed, 28 Oct 2026 12:39:45 +0300 -Subject: [PATCH 02/20] cargo-horizon-mod: report particles and sounds +Subject: [PATCH 02/21] cargo-horizon-mod: report particles and sounds Co-Authored-By: Claude Opus 5.5 --- diff --git a/horizon-patches/0003-Mod-API-AirDef-fork-full-screen-post-pass-for-mods.patch b/horizon-patches/0003-Mod-API-AirDef-fork-full-screen-post-pass-for-mods.patch index fad722f..abee923 100644 --- a/horizon-patches/0003-Mod-API-AirDef-fork-full-screen-post-pass-for-mods.patch +++ b/horizon-patches/0003-Mod-API-AirDef-fork-full-screen-post-pass-for-mods.patch @@ -1,7 +1,7 @@ From ee79b3562feebe51959a01f1585a9e85bd53ccb5 Mon Sep 17 00:00:00 2001 From: chexit Date: Wed, 28 Oct 2026 13:33:10 +0300 -Subject: [PATCH 03/20] Mod API (AirDef fork): full-screen post pass for mods +Subject: [PATCH 03/21] Mod API (AirDef fork): full-screen post pass for mods render.set-post compiles a mod's WGSL after a prelude (camera matrices, time, 256 vec4 of params, the world's colour and depth) and runs it after diff --git a/horizon-patches/0004-Mod-models-draw-far-models-as-scaled-impostors-past-.patch b/horizon-patches/0004-Mod-models-draw-far-models-as-scaled-impostors-past-.patch index 34c2297..b429960 100644 --- a/horizon-patches/0004-Mod-models-draw-far-models-as-scaled-impostors-past-.patch +++ b/horizon-patches/0004-Mod-models-draw-far-models-as-scaled-impostors-past-.patch @@ -1,7 +1,7 @@ From 2b137dbbedd540daeebc7f5e387847aeb56a1355 Mon Sep 17 00:00:00 2001 From: chexit Date: Wed, 28 Oct 2026 22:09:30 +0300 -Subject: [PATCH 04/20] Mod models: draw far models as scaled impostors past +Subject: [PATCH 04/21] Mod models: draw far models as scaled impostors past the far plane, cull at 4096 --- diff --git a/horizon-patches/0005-Mod-cameras-render.set-views-draws-the-world-from-ot.patch b/horizon-patches/0005-Mod-cameras-render.set-views-draws-the-world-from-ot.patch index e6e2673..c03f337 100644 --- a/horizon-patches/0005-Mod-cameras-render.set-views-draws-the-world-from-ot.patch +++ b/horizon-patches/0005-Mod-cameras-render.set-views-draws-the-world-from-ot.patch @@ -1,7 +1,7 @@ From c8ebca6e0af68630e65211844b34d25c5b985c03 Mon Sep 17 00:00:00 2001 From: chexit Date: Wed, 28 Oct 2026 22:31:00 +0300 -Subject: [PATCH 05/20] Mod cameras: render.set-views draws the world from +Subject: [PATCH 05/21] Mod cameras: render.set-views draws the world from other points into pictures that HUD view shapes show (API level 9) Co-Authored-By: Claude Opus 5.5 diff --git a/horizon-patches/0006-Mods-more-fuel-per-tick-and-per-event-AirDef-plans-r.patch b/horizon-patches/0006-Mods-more-fuel-per-tick-and-per-event-AirDef-plans-r.patch index 74fafe4..20eef7f 100644 --- a/horizon-patches/0006-Mods-more-fuel-per-tick-and-per-event-AirDef-plans-r.patch +++ b/horizon-patches/0006-Mods-more-fuel-per-tick-and-per-event-AirDef-plans-r.patch @@ -1,7 +1,7 @@ From 8d2261fc4f8072c32e454139cfea7a8c0cd9d664 Mon Sep 17 00:00:00 2001 From: chexit Date: Wed, 28 Oct 2026 23:08:21 +0300 -Subject: [PATCH 06/20] Mods: more fuel per tick and per event (AirDef plans +Subject: [PATCH 06/21] Mods: more fuel per tick and per event (AirDef plans raids) Co-Authored-By: Claude Opus 5.5 diff --git a/horizon-patches/0007-Mod-cameras-up-to-1920x1080-full-screen-replays.patch b/horizon-patches/0007-Mod-cameras-up-to-1920x1080-full-screen-replays.patch index 3aa75d4..f26d3a4 100644 --- a/horizon-patches/0007-Mod-cameras-up-to-1920x1080-full-screen-replays.patch +++ b/horizon-patches/0007-Mod-cameras-up-to-1920x1080-full-screen-replays.patch @@ -1,7 +1,7 @@ From 6c548de34def8e4a624d734243418090798bd16a Mon Sep 17 00:00:00 2001 From: chexit Date: Wed, 28 Oct 2026 23:25:29 +0300 -Subject: [PATCH 07/20] Mod cameras up to 1920x1080 (full-screen replays) +Subject: [PATCH 07/21] Mod cameras up to 1920x1080 (full-screen replays) Co-Authored-By: Claude Opus 5.5 --- diff --git a/horizon-patches/0008-Mod-cameras-distant-terrain-tiles-fog-measured-from-.patch b/horizon-patches/0008-Mod-cameras-distant-terrain-tiles-fog-measured-from-.patch index 73046a1..db18744 100644 --- a/horizon-patches/0008-Mod-cameras-distant-terrain-tiles-fog-measured-from-.patch +++ b/horizon-patches/0008-Mod-cameras-distant-terrain-tiles-fog-measured-from-.patch @@ -1,7 +1,7 @@ From 9b9e93269d67401cc620567a884d5ce97f68d76e Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 11:15:11 +0300 -Subject: [PATCH 08/20] Mod cameras: distant terrain tiles, fog measured from +Subject: [PATCH 08/21] Mod cameras: distant terrain tiles, fog measured from the camera, no impostors while one is shown Co-Authored-By: Claude Opus 5.5 diff --git a/horizon-patches/0009-Mods-game.world-id-so-mods-keep-state-per-world-API-.patch b/horizon-patches/0009-Mods-game.world-id-so-mods-keep-state-per-world-API-.patch index 4ce42bf..91341dd 100644 --- a/horizon-patches/0009-Mods-game.world-id-so-mods-keep-state-per-world-API-.patch +++ b/horizon-patches/0009-Mods-game.world-id-so-mods-keep-state-per-world-API-.patch @@ -1,7 +1,7 @@ From 065d9fa02966238a5bd9ff460392a1a809db0b11 Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 11:25:45 +0300 -Subject: [PATCH 09/20] Mods: game.world-id, so mods keep state per world (API +Subject: [PATCH 09/21] Mods: game.world-id, so mods keep state per world (API level 9) Co-Authored-By: Claude Opus 5.5 diff --git a/horizon-patches/0010-cargo-horizon-mod-trial-runs-behind-feature-full-so-.patch b/horizon-patches/0010-cargo-horizon-mod-trial-runs-behind-feature-full-so-.patch index 4dfc458..5e11a08 100644 --- a/horizon-patches/0010-cargo-horizon-mod-trial-runs-behind-feature-full-so-.patch +++ b/horizon-patches/0010-cargo-horizon-mod-trial-runs-behind-feature-full-so-.patch @@ -1,7 +1,7 @@ From 1bf28135fd9a31f837323dc3a8d81ed73343d816 Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 12:01:08 +0300 -Subject: [PATCH 10/20] cargo-horizon-mod: trial runs behind feature full, so a +Subject: [PATCH 10/21] cargo-horizon-mod: trial runs behind feature full, so a packing-only tool skips wasmtime Co-Authored-By: Claude Opus 5.5 diff --git a/horizon-patches/0011-HD-lighting-G-buffer-pass-per-pixel-mod-point-lights.patch b/horizon-patches/0011-HD-lighting-G-buffer-pass-per-pixel-mod-point-lights.patch index 5d9e89d..3089329 100644 --- a/horizon-patches/0011-HD-lighting-G-buffer-pass-per-pixel-mod-point-lights.patch +++ b/horizon-patches/0011-HD-lighting-G-buffer-pass-per-pixel-mod-point-lights.patch @@ -1,7 +1,7 @@ From 0a56f5616df4699588cbcc529d4720661baa810f Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 13:18:43 +0300 -Subject: [PATCH 11/20] HD lighting: G-buffer pass, per-pixel mod point lights +Subject: [PATCH 11/21] HD lighting: G-buffer pass, per-pixel mod point lights in HDR, bloom and tone curve; render.set-lights and render.set-lighting (API level 10) diff --git a/horizon-patches/0012-HD-lighting-stage-2-sun-and-moon-shadow-cascades-the.patch b/horizon-patches/0012-HD-lighting-stage-2-sun-and-moon-shadow-cascades-the.patch index ae61b0d..62589ab 100644 --- a/horizon-patches/0012-HD-lighting-stage-2-sun-and-moon-shadow-cascades-the.patch +++ b/horizon-patches/0012-HD-lighting-stage-2-sun-and-moon-shadow-cascades-the.patch @@ -1,7 +1,7 @@ From 9ebf0031fe9ba6b7f02afb26e4fd3a1ba4ddb0d0 Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 13:59:43 +0300 -Subject: [PATCH 12/20] HD lighting stage 2: sun and moon shadow cascades, the +Subject: [PATCH 12/21] HD lighting stage 2: sun and moon shadow cascades, the world relit (sun by face angle and shadow, sky and block light through the lightmap), light shafts and halos, fog over it again, a light colour grade diff --git a/horizon-patches/0013-HD-bind-groups-cached-until-a-resize-or-a-new-source.patch b/horizon-patches/0013-HD-bind-groups-cached-until-a-resize-or-a-new-source.patch index 81b4797..f6a385d 100644 --- a/horizon-patches/0013-HD-bind-groups-cached-until-a-resize-or-a-new-source.patch +++ b/horizon-patches/0013-HD-bind-groups-cached-until-a-resize-or-a-new-source.patch @@ -1,7 +1,7 @@ From 7a5a38f68923aec62f10bfa0df50b436474ed612 Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 16:19:53 +0300 -Subject: [PATCH 13/20] HD: bind groups cached until a resize or a new source, +Subject: [PATCH 13/21] HD: bind groups cached until a resize or a new source, shadow passes draw only the entities that cast shadows Co-Authored-By: Claude Opus 5.5 diff --git a/horizon-patches/0014-HD-lights-culled-per-16x16-tile-surface-and-halo-lis.patch b/horizon-patches/0014-HD-lights-culled-per-16x16-tile-surface-and-halo-lis.patch index 4a9187c..91d936f 100644 --- a/horizon-patches/0014-HD-lights-culled-per-16x16-tile-surface-and-halo-lis.patch +++ b/horizon-patches/0014-HD-lights-culled-per-16x16-tile-surface-and-halo-lis.patch @@ -1,7 +1,7 @@ From 8020811e57d4b5a9ea1cd72d30ce859dc5095d35 Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 16:25:44 +0300 -Subject: [PATCH 14/20] HD: lights culled per 16x16 tile (surface and halo +Subject: [PATCH 14/21] HD: lights culled per 16x16 tile (surface and halo lists built on the CPU), a naga test for the lighting shaders Co-Authored-By: Claude Opus 5.5 diff --git a/horizon-patches/0015-HD-GPU-time-per-pass-in-the-F3-timing-list-half-reso.patch b/horizon-patches/0015-HD-GPU-time-per-pass-in-the-F3-timing-list-half-reso.patch index 2e10c14..eb7661b 100644 --- a/horizon-patches/0015-HD-GPU-time-per-pass-in-the-F3-timing-list-half-reso.patch +++ b/horizon-patches/0015-HD-GPU-time-per-pass-in-the-F3-timing-list-half-reso.patch @@ -1,7 +1,7 @@ From 72d3fddfea329e578162656151350f0aa2efd323 Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 16:43:36 +0300 -Subject: [PATCH 15/20] HD: GPU time per pass in the F3 timing list, +Subject: [PATCH 15/21] HD: GPU time per pass in the F3 timing list, half-resolution GTAO with a depth-aware blur Co-Authored-By: Claude Opus 5.5 diff --git a/horizon-patches/0016-HD-shadows-draw-only-sun-facing-quads-and-skip-burie.patch b/horizon-patches/0016-HD-shadows-draw-only-sun-facing-quads-and-skip-burie.patch index 2e86987..229f38b 100644 --- a/horizon-patches/0016-HD-shadows-draw-only-sun-facing-quads-and-skip-burie.patch +++ b/horizon-patches/0016-HD-shadows-draw-only-sun-facing-quads-and-skip-burie.patch @@ -1,7 +1,7 @@ From 1fd83a821bf270879d8ece5aa583f200ed920d89 Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 17:02:02 +0300 -Subject: [PATCH 16/20] HD: shadows draw only sun-facing quads and skip buried +Subject: [PATCH 16/21] HD: shadows draw only sun-facing quads and skip buried sections, the far cascade every second frame; TAA (Halton jitter, reprojection by depth, YCoCg variance clipping) before bloom diff --git a/horizon-patches/0017-Mods-model-handles-render.model-id-model-generation-.patch b/horizon-patches/0017-Mods-model-handles-render.model-id-model-generation-.patch index e4ce6be..e3e9a28 100644 --- a/horizon-patches/0017-Mods-model-handles-render.model-id-model-generation-.patch +++ b/horizon-patches/0017-Mods-model-handles-render.model-id-model-generation-.patch @@ -1,7 +1,7 @@ From 1502d359d599228fe0f44ce6b99a3fded2ac5ea0 Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 17:02:02 +0300 -Subject: [PATCH 17/20] Mods: model handles (render.model-id, model-generation, +Subject: [PATCH 17/21] Mods: model handles (render.model-id, model-generation, set-models-by-id; API level 11) so mods send no model names each tick; clear-post and set-lighting documented diff --git a/horizon-patches/0018-HD-temporal-upscaling-HORIZON_HD_SCALE-world-G-buffe.patch b/horizon-patches/0018-HD-temporal-upscaling-HORIZON_HD_SCALE-world-G-buffe.patch index b278845..261910e 100644 --- a/horizon-patches/0018-HD-temporal-upscaling-HORIZON_HD_SCALE-world-G-buffe.patch +++ b/horizon-patches/0018-HD-temporal-upscaling-HORIZON_HD_SCALE-world-G-buffe.patch @@ -1,7 +1,7 @@ From 0f9d99b8ad3e4b72e621d84c2396cf1d3d2b7a33 Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 17:19:57 +0300 -Subject: [PATCH 18/20] HD: temporal upscaling (HORIZON_HD_SCALE: world, +Subject: [PATCH 18/21] HD: temporal upscaling (HORIZON_HD_SCALE: world, G-buffer and lighting at a lower size, rebuilt to the window in the TAA pass); fix flickering shadows near the player (the near cascade lost its sections on frames the far one was held, the held far matrix ignored the diff --git a/horizon-patches/0019-HD-water-its-own-pass-colour-normal-depth-the-floor-.patch b/horizon-patches/0019-HD-water-its-own-pass-colour-normal-depth-the-floor-.patch index f42db4e..5416f47 100644 --- a/horizon-patches/0019-HD-water-its-own-pass-colour-normal-depth-the-floor-.patch +++ b/horizon-patches/0019-HD-water-its-own-pass-colour-normal-depth-the-floor-.patch @@ -1,7 +1,7 @@ From 38250c4392e27d5700842ca6ee47291dd1831b72 Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 17:47:52 +0300 -Subject: [PATCH 19/20] HD water: its own pass (colour, normal, depth), the +Subject: [PATCH 19/21] HD water: its own pass (colour, normal, depth), the floor under it lit, a composite with world-space waves, refraction with depth absorption in the biome colour, screen-space reflections, Fresnel and a shadowed sun glint (HORIZON_HD_WATER=0 to switch off) diff --git a/horizon-patches/0020-HD-water-no-vanilla-texture-on-the-surface-the-biome.patch b/horizon-patches/0020-HD-water-no-vanilla-texture-on-the-surface-the-biome.patch index 49b64e5..dd66f25 100644 --- a/horizon-patches/0020-HD-water-no-vanilla-texture-on-the-surface-the-biome.patch +++ b/horizon-patches/0020-HD-water-no-vanilla-texture-on-the-surface-the-biome.patch @@ -1,7 +1,7 @@ From dbc6f8a6e9c2eeabe1de15f1794013c90be7faa5 Mon Sep 17 00:00:00 2001 From: chexit Date: Thu, 29 Oct 2026 17:56:04 +0300 -Subject: [PATCH 20/20] HD water: no vanilla texture on the surface (the biome +Subject: [PATCH 20/21] HD water: no vanilla texture on the surface (the biome colour only), a darker, deeper colour Co-Authored-By: Claude Opus 5.5 diff --git a/horizon-patches/0021-HD-sky-and-light-a-physical-atmosphere-Rayleigh-Mie-.patch b/horizon-patches/0021-HD-sky-and-light-a-physical-atmosphere-Rayleigh-Mie-.patch new file mode 100644 index 0000000..2298907 --- /dev/null +++ b/horizon-patches/0021-HD-sky-and-light-a-physical-atmosphere-Rayleigh-Mie-.patch @@ -0,0 +1,945 @@ +From 13e74537dd6e63482e3613e0e416de1158944dfe Mon Sep 17 00:00:00 2001 +From: chexit +Date: Thu, 29 Oct 2026 18:38:19 +0300 +Subject: [PATCH 21/21] HD sky and light: a physical atmosphere (Rayleigh, Mie, + ozone; constants and transmittance after Photon, MIT) for the sky, haze, fog + and water reflections; warmer sunlight from the atmosphere's transmittance, a + gentle sky-coloured ambient, soft PCSS-lite shadows near the player, a haze + round the sun; lighter water with fine ripples + +Co-Authored-By: Claude Opus 5.5 +--- + crates/horizon-client/src/gfx.rs | 4 + + .../src/gfx/THIRD_PARTY_PHOTON.md | 34 ++ + crates/horizon-client/src/gfx/hd.rs | 259 +++++++++++++- + crates/horizon-client/src/gfx/hd_sky.rs | 324 ++++++++++++++++++ + crates/horizon-client/src/terrain.wgsl | 2 +- + 5 files changed, 604 insertions(+), 19 deletions(-) + create mode 100644 crates/horizon-client/src/gfx/THIRD_PARTY_PHOTON.md + create mode 100644 crates/horizon-client/src/gfx/hd_sky.rs + +diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs +index f9dab8abf..d01131433 100644 +--- a/crates/horizon-client/src/gfx.rs ++++ b/crates/horizon-client/src/gfx.rs +@@ -72,6 +72,7 @@ mod cull; + mod end_sky; + mod flat; + mod hd; ++mod hd_sky; + mod hdr; + pub mod lightning; + mod lod; +@@ -4275,6 +4276,8 @@ impl Renderer { + camera.z.rem_euclid(256.0) as f32, + ], + water: !frame.underwater, ++ sunrise: frame.sky.sunrise_color, ++ sky_fog_end: frame.fog.map_or(frame.distance.min(512.0), |f| f.sky_end), + }; + let (shadow_dir, shadow_strength, _) = hd::light_of(&hd_scene); + let mut hd_cascades = hd::cascades(camera, shadow_dir); +@@ -5817,6 +5820,7 @@ impl Renderer { + { + // AirDef fork: lit by the mods' lights into the HD target, the mod's post pass on + // that, then bloom and the tone curve onto the screen. ++ hd.sky(&mut encoder); + hd.light( + &self.device, + &mut encoder, +diff --git a/crates/horizon-client/src/gfx/THIRD_PARTY_PHOTON.md b/crates/horizon-client/src/gfx/THIRD_PARTY_PHOTON.md +new file mode 100644 +index 000000000..03d945001 +--- /dev/null ++++ b/crates/horizon-client/src/gfx/THIRD_PARTY_PHOTON.md +@@ -0,0 +1,34 @@ ++# Third-party code: Photon ++ ++Parts of the HD renderer's atmosphere (`hd_sky.rs`) are ported from, or follow closely, the ++Photon shader pack by sixthsurge, `shaders/include/sky/atmosphere.glsl` (and the idea of the water ++waves in `shaders/include/misc/water_normal.glsl`) at commit ++26c2906ee02cc484359b835fba6993d443d45966, https://github.com/sixthsurge/photon ++ ++What is taken: the air's scattering and absorption coefficients (Rayleigh, Mie, ozone), the scale ++heights, the ozone density profile, the Mie albedo and Henyey-Greenstein anisotropy, the sunlight ++colour outside the atmosphere, and the Chapman-function approximation of the sun's transmittance. ++The sky-view texture, its march, the sun's colour and everything else are our own. ++ ++Photon is distributed under the MIT License: ++ ++ The MIT License ++ ++ Copyright © 2025 Benjamin Stott "sixthsurge" ++ ++ Permission is hereby granted, free of charge, to any person obtaining a copy of this software ++ and associated documentation files (the "Software"), to deal in the Software without ++ restriction, including without limitation the rights to use, copy, modify, merge, publish, ++ distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the ++ Software is furnished to do so, subject to the following conditions: ++ ++ The above copyright notice and this permission notice shall be included in all copies or ++ substantial portions of the Software. ++ ++ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING ++ BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ++ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, ++ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ++ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++ ++No other shader pack's code (BSL, Complementary and so on) may be used: their licences forbid it. +diff --git a/crates/horizon-client/src/gfx/hd.rs b/crates/horizon-client/src/gfx/hd.rs +index 8070aab3c..031e9f2de 100644 +--- a/crates/horizon-client/src/gfx/hd.rs ++++ b/crates/horizon-client/src/gfx/hd.rs +@@ -5,6 +5,7 @@ + //! into a high-dynamic-range target; the mod's post pass runs on that; then + //! bright parts bloom and a tone curve brings it to the screen — vanilla's + //! range (up to about 0.8) left as it is, only what is brighter rolled off. ++use super::hd_sky; + use super::pack_timing::PassTiming; + use glam::Mat4; + use std::sync::atomic::{AtomicU64, Ordering}; +@@ -101,6 +102,12 @@ struct HdFrame { + water: [f32; 4], + /// rgb: the sky's colour; w: 1 when there is a sky. + sky_color: [f32; 4], ++ /// xyz: towards the sun itself (not the moon); w: 1 when the atmosphere draws the sky. ++ sky_sun: [f32; 4], ++ /// x: vanilla's sky fog end; z: how overcast the sky is (rain: 0 clear, 1 vanilla's colours). ++ sky_extra: [f32; 4], ++ /// Vanilla's sunrise colour fan (rgb, alpha), taken out of the sky before the atmosphere is put in. ++ sunrise: [f32; 4], + } + + /// What the world around looks like this frame (from the renderer's globals). +@@ -131,6 +138,9 @@ pub struct HdScene { + pub camera_mod: [f32; 2], + /// The water effect may run (not underwater, not disabled). + pub water: bool, ++ /// Vanilla's sunrise fan colour and its sky fog end (to subtract vanilla's sky from the world). ++ pub sunrise: [f32; 4], ++ pub sky_fog_end: f32, + } + + /// The cascades' half sizes (blocks) and shadow map size. +@@ -155,6 +165,9 @@ struct HdFrame { + view_proj: mat4x4, + water: vec4, + sky_color: vec4, ++ sky_sun: vec4, ++ sky_extra: vec4, ++ sunrise: vec4, + } + struct FullOut { + @builtin(position) position: vec4, +@@ -195,6 +208,9 @@ struct Light { + @group(0) @binding(13) var ao_tex: texture_2d; + // The water pass's depth: the nearest water surface (0 where there is none). + @group(0) @binding(14) var water_depth: texture_depth_2d; ++//SKY15 ++// The pixel being lit, for the shadow filter's noise. ++var frag_px: vec2; + + // The occlusion at a full-resolution pixel `dist` away: the four nearest half-resolution texels + // weighted by closeness and by how near their distance is, so it doesn't bleed across edges. +@@ -239,6 +255,42 @@ fn shadow(p: vec3, n: vec3) -> f32 { + return shadow_taps(p, n, 1); + } + ++// A point of a disc of radius 1 (Vogel's spiral), turned by `angle`. ++fn vogel(i: i32, count: i32, angle: f32) -> vec2 { ++ let r = sqrt((f32(i) + 0.5) / f32(count)); ++ let a = f32(i) * 2.39996323 + angle; ++ return vec2(cos(a), sin(a)) * r; ++} ++ ++// The near cascade's soft shadow: a blocker search sets the penumbra's width (wider the farther ++// the shadow's caster is above the receiver), then a ring of taps turned by a per-pixel noise ++// filters it; the temporal pass averages the noise. `s`: the point in the cascade (uv, depth), ++// `texel`: the cascade's texel in blocks. ++fn soft_shadow(s: vec3, texel: f32) -> f32 { ++ let angle = ign(frag_px) * 6.2831853; ++ var blocker = 0.0; ++ var found = 0.0; ++ for (var i = 0; i < 8; i++) { ++ let o = vogel(i, 8, angle) * (6.0 / 2048.0); ++ let q = clamp(vec2((s.xy + o) * 2048.0), vec2(0), vec2(2047)); ++ let z = textureLoad(shadow_map, q, 0, 0); ++ if z < s.z - 0.0006 { ++ blocker += z; ++ found += 1.0; ++ } ++ } ++ if found < 0.5 { return 1.0; } ++ // The caster's height above the receiver, in blocks (the cascade's depth spans 799 blocks). ++ let height = (s.z - blocker / found) * 799.0; ++ let radius = clamp(height * 0.18 / texel, 1.6, 8.0); ++ var lit = 0.0; ++ for (var i = 0; i < 12; i++) { ++ let o = vogel(i, 12, angle + 1.7) * (radius / 2048.0); ++ lit += textureSampleCompareLevel(shadow_map, shadow_sampler, s.xy + o, 0, s.z - 0.0005 - 0.00005 * radius); ++ } ++ return lit / 12.0; ++} ++ + // `shadow` with a (2 * r + 1) square of taps: the floor under water takes one (the water dims and + // blurs it anyway). + fn shadow_taps(p: vec3, n: vec3, r: i32) -> f32 { +@@ -249,13 +301,16 @@ fn shadow_taps(p: vec3, n: vec3, r: i32) -> f32 { + let s = cascade_at(q, c); + if any(s.xy < vec2(0.0)) || any(s.xy > vec2(1.0)) || s.z > 1.0 { return 1.0; } + var lit = 0.0; ++ let fade = clamp((frame.shadow.z - dist) / 16.0, 0.0, 1.0); ++ if r == 1 && c == 0 { ++ return mix(1.0, soft_shadow(s, texel), fade); ++ } + let step = 1.0 / 2048.0; + for (var y = -r; y <= r; y++) { + for (var x = -r; x <= r; x++) { + lit += textureSampleCompareLevel(shadow_map, shadow_sampler, s.xy + vec2(f32(x), f32(y)) * step, c, s.z - 0.0005); + } + } +- let fade = clamp((frame.shadow.z - dist) / 16.0, 0.0, 1.0); + return mix(1.0, lit / f32((2 * r + 1) * (2 * r + 1)), fade); + } + +@@ -300,7 +355,31 @@ fn fs_light(in: FullOut) -> @location(0) vec4 { + let halo_start = tiles[tile + 2u]; + let halo_all = tiles[tile + 3u] == 0xffffffffu; + let halo_count = select(tiles[tile + 3u], count, halo_all); ++ frag_px = in.position.xy; ++ let atmosphere = frame.sky_sun.w > 0.5; + var c = scene; ++ // The sky: the atmosphere's, with what the world drew over vanilla's plain sky (sun, moon, ++ // stars, clouds) kept on top. ++ if d <= 0.0 && atmosphere { ++ var kept = max(scene - vanilla_sky(view), vec3(0.0)); ++ var sky = atmosphere_sky(view); ++ // The sun: vanilla's square sprite is taken out and a round disc with a soft haze round ++ // it (warm, strongest low) is drawn in its place. ++ let toward = dot(view, frame.sky_sun.xyz); ++ kept *= 1.0 - smoothstep(0.965, 0.975, toward); ++ let clear = 1.0 - frame.sky_extra.z; ++ let shine = smoothstep(-0.04, 0.02, frame.sky_sun.y) * clear; ++ let glow = max(toward, 0.0); ++ let angle = acos(clamp(toward, -1.0, 1.0)); ++ sky += frame.sun_color.rgb * shine * (pow(glow, 24.0) * 0.07 + pow(glow, 400.0) * 0.4 + 6.0 * (1.0 - smoothstep(0.016, 0.022, angle))); ++ c = sky + kept; ++ } ++ // Clouds (vanilla's, drawn into the world with depth but not into the G-buffer): lit by the low ++ // sun's colour at dusk and dawn instead of vanilla's flat white. ++ if atmosphere && !water && d > 0.0 && g <= 0.0 && p.y > 8.0 && dot(p, p) > 2304.0 { ++ let low = 1.0 - smoothstep(0.05, 0.5, frame.sky_sun.y); ++ c = scene * mix(vec3(1.0), mix(frame.sun_color.rgb, vec3(1.0), 0.45) * 1.1, low * 0.7 * (1.0 - frame.sky_extra.z)); ++ } + let surface = water || (d > 0.0 && abs(d - g) <= max(d, g) * 0.002); + if surface { + let n = normalize(textureLoad(normal_tex, px, 0).xyz); +@@ -323,7 +402,17 @@ fn fs_light(in: FullOut) -> @location(0) vec4 { + if frame.ambient.w > 0.0 && !water { occlusion = ao_at(px, length(p)); } + let ao_ambient = mix(1.0, occlusion, frame.ambient.w); + let ao_lights = mix(1.0, occlusion, frame.ambient.w * 0.5); +- var light = max(base * frame.ambient.x, block * frame.ambient.y) * ao_ambient + direct; ++ // The sky's light is bluish from above and carries the horizon's colour from the sides; ++ // faces turned up take more of it than the ones turned down. ++ var sky_tint = vec3(1.0); ++ if atmosphere { ++ let reach = smoothstep(0.25, 0.9, lm.x); ++ let up = clamp(n.y * 0.5 + 0.5, 0.0, 1.0); ++ let shape = mix(0.66, 1.1, smoothstep(0.1, 1.0, up)); ++ let hue = mix(vec3(1.0), sky_light_tint(n), 0.4 * (1.0 - 0.5 * frame.sun_color.a)); ++ sky_tint = mix(vec3(1.0), hue * shape, reach); ++ } ++ var light = max(base * sky_tint * frame.ambient.x, block * frame.ambient.y) * ao_ambient + direct; + // The mods' lights. + for (var k = 0u; k < surface_count; k++) { + let l = lights[select(tiles[surface_start + k], k, surface_all)]; +@@ -340,7 +429,12 @@ fn fs_light(in: FullOut) -> @location(0) vec4 { + let sph = length(p); + let cyl = max(length(p.xz), abs(p.y)); + let f = max(linear_fog(sph, frame.fog.x, frame.fog.y), linear_fog(cyl, frame.fog.z, frame.fog.w)); +- c = mix(c, frame.fog_color.rgb, f * frame.fog_color.a); ++ let haze = haze_colour(view); ++ c = mix(c, haze, f * frame.fog_color.a); ++ // Aerial perspective: far things take the horizon's colour a little before the fog does. ++ if atmosphere && frame.fog.x > 1.0e6 { ++ c = mix(c, haze, (1.0 - exp(-sph / 700.0)) * 0.5); ++ } + } + // Light in the air: the sun's rays between the shadows, and halos round the mods' lights. + let far = select(min(length(p), frame.shadow.z), frame.shadow.z, d <= 0.0); +@@ -387,6 +481,7 @@ const REFLECT_RANGE: f32 = 96.0; + @group(0) @binding(7) var shadow_sampler: sampler_comparison; + @group(0) @binding(8) var lightmap: texture_2d; + @group(0) @binding(9) var linear: sampler; ++//SKY10 + + fn ign(px: vec2) -> f32 { + let p = px + vec2(5.588238 * frame.params.w); +@@ -417,7 +512,7 @@ fn texel(uv: vec2) -> vec2 { + // are whole cycles per 600 s, so the sum repeats every 256 blocks and 10 minutes and holds still + // in the world as the camera moves. x, y: the vector in units of 2 pi / 256; z: the slope the + // wave adds; w: its phase. +-const WAVES = array, 10>( ++const WAVES = array, 26>( + vec4(3.0, 1.0, 0.040, 0.3), + vec4(-2.0, 5.0, 0.045, 2.1), + vec4(7.0, 2.0, 0.040, 4.4), +@@ -428,17 +523,36 @@ const WAVES = array, 10>( + vec4(9.0, 23.0, 0.030, 2.7), + vec4(-27.0, 19.0, 0.025, 4.9), + vec4(31.0, 30.0, 0.025, 1.8), ++ // Fine ripples (wavelengths of 0.7 to 3 blocks) for the many small sun sparkles: slopes small, ++ // faded out by distance and pixel size like the rest. ++ vec4(83.0, 41.0, 0.042, 0.4), ++ vec4(-97.0, 59.0, 0.042, 3.1), ++ vec4(71.0, -113.0, 0.042, 5.0), ++ vec4(131.0, 89.0, 0.040, 1.5), ++ vec4(-157.0, -103.0, 0.040, 4.2), ++ vec4(191.0, -67.0, 0.038, 2.4), ++ vec4(-223.0, 149.0, 0.038, 0.8), ++ vec4(101.0, 241.0, 0.036, 3.7), ++ vec4(-263.0, -181.0, 0.034, 5.5), ++ vec4(311.0, 127.0, 0.034, 2.0), ++ // Finer still (0.35 to 0.6 blocks): the small sparkles. ++ vec4(397.0, -263.0, 0.050, 1.1), ++ vec4(-467.0, 331.0, 0.050, 4.6), ++ vec4(521.0, 449.0, 0.045, 3.0), ++ vec4(-613.0, -199.0, 0.045, 5.9), ++ vec4(293.0, 577.0, 0.045, 0.2), ++ vec4(-701.0, 263.0, 0.040, 2.8), + ); + + // The gradient of the water's height at world x, z: the waves that a pixel `footprint` blocks + // wide can still resolve (finer ones fade out, or they would shimmer). + fn wave_gradient(xz: vec2, footprint: f32) -> vec2 { + var grad = vec2(0.0); +- for (var i = 0; i < 10; i++) { ++ for (var i = 0; i < 26; i++) { + let w = WAVES[i]; + let k = w.xy * (2.0 * PI / 256.0); + let len = length(k); +- let fade = clamp((2.0 * PI / len) / max(footprint, 0.0001) * 0.25 - 0.5, 0.0, 1.0); ++ let fade = clamp((2.0 * PI / len) / max(footprint, 0.0001) * 0.2 - 0.6, 0.0, 1.0); + let cycles = round(sqrt(9.8 * len) * 0.5 * 600.0 / (2.0 * PI)); + let omega = cycles * 2.0 * PI / 600.0; + let phase = dot(k, xz) - omega * frame.water.y + w.w; +@@ -481,7 +595,9 @@ fn linear_fog(d: f32, start: f32, end: f32) -> f32 { + // the zenith's, darker where the water lies under cover (`vis` is the sky light there). + fn sky_along(r: vec3, vis: f32) -> vec3 { + var c = frame.fog_color.rgb; +- if frame.sky_color.w > 0.5 { ++ if frame.sky_sun.w > 0.5 { ++ c = atmosphere_sky(r); ++ } else if frame.sky_color.w > 0.5 { + c = mix(frame.fog_color.rgb, frame.sky_color.rgb, smoothstep(0.0, 0.5, r.y)); + } + return c * mix(0.12, 1.0, vis); +@@ -531,7 +647,7 @@ fn reflection(p: vec3, r: vec3, jitter: f32) -> vec4 { + // The sun's glint on the waves (GGX, a rough lobe so it glitters instead of aliasing). + fn glint(n: vec3, v: vec3, l: vec3) -> f32 { + let h = normalize(l + v); +- let a = 0.05; ++ let a = 0.07; + let nh = max(dot(n, h), 0.0); + let d = a * a / (PI * pow(nh * nh * (a * a - 1.0) + 1.0, 2.0)); + let f = 0.02 + 0.98 * pow(1.0 - max(dot(v, h), 0.0), 5.0); +@@ -565,7 +681,10 @@ fn fs_water(in: FullOut) -> @location(0) vec4 { + // Light on the water: vanilla's lightmap at the surface, and the sun where it reaches. + let levels = vec2(nt.a, tint.a); + let sky_reach = smoothstep(0.35, 0.95, levels.x); +- let ambient = textureSampleLevel(lightmap, linear, clamp(vec2(levels.y, levels.x) * (15.0 / 16.0) + 0.5 / 16.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)), 0.0).rgb; ++ var ambient = textureSampleLevel(lightmap, linear, clamp(vec2(levels.y, levels.x) * (15.0 / 16.0) + 0.5 / 16.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)), 0.0).rgb; ++ if frame.sky_sun.w > 0.5 { ++ ambient *= mix(vec3(1.0), sky_light_tint(vec3(0.0, 1.0, 0.0)), 0.7 * sky_reach); ++ } + let block = textureSampleLevel(lightmap, linear, vec2(levels.y * (15.0 / 16.0) + 0.5 / 16.0, 0.5 / 16.0), 0.0).rgb; + let sun = frame.sun.xyz; + let sun_on = frame.sun.w; +@@ -594,10 +713,10 @@ fn fs_water(in: FullOut) -> @location(0) vec4 { + // A deep, dark water: the biome's colour darkened and a little greyed, so it reads as + // depth rather than as bright paint. + let biome = clamp(tint.rgb, vec3(0.03), vec3(1.0)); +- let colour = mix(vec3(dot(biome, vec3(0.333))), biome, 0.8) * vec3(0.55, 0.68, 0.8); ++ let colour = mix(vec3(dot(biome, vec3(0.333))), biome, 0.85) * vec3(0.92, 1.0, 1.0); + // The colours the water takes up fastest are the ones it has the least of. +- let absorb = exp(-(vec3(1.0) - colour) * 1.1 * (thickness + 0.3)); +- let refracted = behind * absorb + colour * volume_light * 0.6 * (vec3(1.0) - absorb); ++ let absorb = exp(-(vec3(1.0) - colour) * 0.8 * (thickness + 0.3)); ++ let refracted = behind * absorb + colour * volume_light * mix(0.85, 0.4, frame.sun_color.a) * (vec3(1.0) - absorb); + + // Reflection: the screen where the ray stays on it, the sky where it leaves it or misses. + var r = reflect(-v, n); +@@ -617,7 +736,11 @@ fn fs_water(in: FullOut) -> @location(0) vec4 { + let sph = dist; + let cyl = max(length(p.xz), abs(p.y)); + let f = max(linear_fog(sph, frame.fog.x, frame.fog.y), linear_fog(cyl, frame.fog.z, frame.fog.w)); +- c = mix(c, frame.fog_color.rgb, f * frame.fog_color.a); ++ let haze = haze_colour(-v); ++ c = mix(c, haze, f * frame.fog_color.a); ++ if frame.sky_sun.w > 0.5 && frame.fog.x > 1.0e6 { ++ c = mix(c, haze, (1.0 - exp(-dist / 700.0)) * 0.5); ++ } + return vec4(max(c, vec3(0.0)), 1.0); + } + "#; +@@ -1200,6 +1323,14 @@ pub struct Hd { + /// The frame's translucent terrain exists (set by the renderer before `write`). + pub water_geometry: bool, + water_composited: bool, ++ /// The sky-view texture (see `hd_sky`), the pass that marches it and its bind group. ++ sky_lut: wgpu::TextureView, ++ sky_pipeline: wgpu::RenderPipeline, ++ sky_group: wgpu::BindGroup, ++ /// The sun's height the sky-view texture was marched for (it depends on nothing else), and ++ /// whether this frame's sun has moved enough to march it again. ++ sky_drawn_for: f32, ++ sky_stale: bool, + started: std::time::Instant, + /// TAA: the resolved frame, and the two histories (one read, one written, alternating). + taa: wgpu::TextureView, +@@ -1403,10 +1534,23 @@ fn ao_strength_default() -> f32 { + .unwrap_or(AO_STRENGTH) + } + ++/// The shader's source: the common part, then the body with the sky-reading functions where it ++/// has a marker (`//SKY15`: the sky-view texture is binding 15, `//SKY10`: binding 10). ++fn assemble(body: &str) -> String { ++ let mut body = body.to_owned(); ++ for binding in ["15", "10"] { ++ body = body.replace( ++ &format!("//SKY{binding}"), ++ &hd_sky::SKY_FUNCS.replace("SKYBIND", binding), ++ ); ++ } ++ format!("{COMMON}{body}") ++} ++ + fn module(device: &wgpu::Device, label: &str, body: &str) -> wgpu::ShaderModule { + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some(label), +- source: wgpu::ShaderSource::Wgsl(format!("{COMMON}{body}").into()), ++ source: wgpu::ShaderSource::Wgsl(assemble(body).into()), + }) + } + +@@ -1523,6 +1667,7 @@ impl Hd { + }, + UNFILTERED, + DEPTH, ++ FLOAT, + ]), + }); + let water_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { +@@ -1542,8 +1687,13 @@ impl Hd { + wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Comparison), + FLOAT, + SAMPLER, ++ FLOAT, + ]), + }); ++ let sky_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { ++ label: Some("HD sky"), ++ entries: &entries(&[UNIFORM]), ++ }); + let taa_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("HD TAA"), + entries: &entries(&[UNIFORM, UNFILTERED, DEPTH, FLOAT, SAMPLER]), +@@ -1605,6 +1755,16 @@ impl Hd { + let ao = module(device, "HD ambient occlusion", AO); + let ao_blur = module(device, "HD ambient occlusion blur", AO_BLUR); + let water = module(device, "HD water", WATER); ++ let sky_module = module(device, "HD sky", hd_sky::SKY_PASS); ++ let sky_pipeline = fullscreen( ++ device, ++ "HD sky", ++ &sky_layout, ++ &sky_module, ++ "fs_sky_lut", ++ hd_sky::SKY_LUT_FORMAT, ++ None, ++ ); + let water_pipeline = fullscreen( + device, + "HD water", +@@ -1691,6 +1851,21 @@ impl Hd { + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); ++ let sky_lut = target( ++ device, ++ "HD sky", ++ hd_sky::SKY_LUT_SIZE, ++ hd_sky::SKY_LUT_FORMAT, ++ ) ++ .view; ++ let sky_group = device.create_bind_group(&wgpu::BindGroupDescriptor { ++ label: Some("HD sky"), ++ layout: &sky_layout, ++ entries: &[wgpu::BindGroupEntry { ++ binding: 0, ++ resource: frame.as_entire_binding(), ++ }], ++ }); + let lights = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("HD lights"), + size: (MAX_LIGHTS * std::mem::size_of::()) as u64, +@@ -1788,6 +1963,11 @@ impl Hd { + water_active: false, + water_geometry: false, + water_composited: false, ++ sky_lut, ++ sky_pipeline, ++ sky_group, ++ sky_drawn_for: 2.0, ++ sky_stale: true, + started: std::time::Instant::now(), + taa: target(device, "HD TAA", [1, 1], HDR).view, + history: [ +@@ -1961,6 +2141,11 @@ impl Hd { + if !self.taa_active || scene.camera_delta.length() > 64.0 { + self.history_ready = false; + } ++ let sun_height = scene.sun.normalize_or(glam::Vec3::Y).y; ++ self.sky_stale = (sun_height - self.sky_drawn_for).abs() > 0.001; ++ if self.sky_stale { ++ self.sky_drawn_for = sun_height; ++ } + let frame = HdFrame { + prev_view_proj: scene.prev_view_proj.to_cols_array_2d(), + taa: [ +@@ -1982,6 +2167,17 @@ impl Hd { + scene.sky_color[2], + if scene.sky { 1.0 } else { 0.0 }, + ], ++ sky_sun: { ++ let sun = scene.sun.normalize_or(glam::Vec3::Y); ++ [sun.x, sun.y, sun.z, if scene.sky { 1.0 } else { 0.0 }] ++ }, ++ sky_extra: [ ++ scene.sky_fog_end, ++ 0.0, ++ (scene.rain / 0.7).clamp(0.0, 1.0) * 0.85, ++ 0.0, ++ ], ++ sunrise: scene.sunrise, + taa_params: [ + if self.upscaling() { + UPSCALE_HISTORY +@@ -2123,6 +2319,23 @@ impl Hd { + pass.draw(0..3, 0..1); + } + ++ /// Marches the sky-view texture for this frame's sun (before the lighting and the water); skipped ++ /// while the sun's height has moved less than a thousandth since the last march. ++ pub fn sky(&mut self, encoder: &mut wgpu::CommandEncoder) { ++ if !self.sky_stale { ++ return; ++ } ++ Self::draw( ++ encoder, ++ "HD sky", ++ &self.sky_lut, ++ false, ++ &self.sky_pipeline, ++ &self.sky_group, ++ stamp(&mut self.timing, "hd sky"), ++ ); ++ } ++ + /// Lights the world (its colour and depth) into `lit`. + pub fn light( + &mut self, +@@ -2203,6 +2416,10 @@ impl Hd { + binding: 14, + resource: wgpu::BindingResource::TextureView(&self.water_depth), + }, ++ wgpu::BindGroupEntry { ++ binding: 15, ++ resource: wgpu::BindingResource::TextureView(&self.sky_lut), ++ }, + ], + }); + self.light_group = Some((key, group)); +@@ -2512,6 +2729,7 @@ impl Hd { + wgpu::BindingResource::Sampler(&self.shadow_sampler), + wgpu::BindingResource::TextureView(lightmap.0), + wgpu::BindingResource::Sampler(&self.sampler), ++ wgpu::BindingResource::TextureView(&self.sky_lut), + ], + ); + self.water_group = Some((key, group)); +@@ -2608,9 +2826,13 @@ pub fn light_of(scene: &HdScene) -> (glam::Vec3, f32, glam::Vec3) { + let clear = 1.0 - scene.rain * 0.85; + if sun.y > -0.02 { + let up = smooth(-0.02, 0.18, sun.y); +- let warm = smooth(0.0, 0.45, sun.y); +- let color = glam::Vec3::new(1.0, 0.62, 0.38).lerp(glam::Vec3::new(1.0, 0.95, 0.86), warm); +- (sun, 0.6 * up * clear, color) ++ // The atmosphere's transmittance along the sun's ray (orange when low, a clean warm white ++ // high), kept at full brightness and tempered, with a slight warm tint on top. ++ let t = hd_sky::sun_transmittance(sun.y); ++ let hue = t / t.max_element().max(1e-4); ++ let color = glam::Vec3::new(hue.x.powf(0.6), hue.y.powf(0.6), hue.z.powf(0.6)) ++ * glam::Vec3::new(1.0, 0.93, 0.78); ++ (sun, 0.7 * up * clear, color) + } else { + let moon = -sun; + let up = smooth(-0.02, 0.18, moon.y); +@@ -2661,8 +2883,9 @@ mod tests { + ("ao", AO), + ("ao blur", AO_BLUR), + ("water", WATER), ++ ("sky", hd_sky::SKY_PASS), + ] { +- let source = format!("{COMMON}{body}"); ++ let source = assemble(body); + let module = naga::front::wgsl::parse_str(&source).unwrap_or_else(|e| { + panic!("{name}: {}", e.emit_to_string(&source)); + }); +diff --git a/crates/horizon-client/src/gfx/hd_sky.rs b/crates/horizon-client/src/gfx/hd_sky.rs +new file mode 100644 +index 000000000..b5c231a7b +--- /dev/null ++++ b/crates/horizon-client/src/gfx/hd_sky.rs +@@ -0,0 +1,324 @@ ++//! AirDef fork: the HD renderer's atmosphere. A small sky-view texture (azimuth from the sun by ++//! elevation, plus a row of ambient samples) is marched each frame from single scattering of ++//! Rayleigh and Mie particles and ozone absorption with a cheap multiple-scattering term; the ++//! lighting and water shaders read it for the sky, the aerial-perspective haze, the water's ++//! reflection fallback and the colour of the sky's light. The sunlight's colour is the same ++//! atmosphere's transmittance at the sun's elevation (`sun_transmittance`). ++//! ++//! The coefficients, the density profiles (scale heights, the ozone layer), the Henyey-Greenstein ++//! Mie parameters and the Chapman-function transmittance come from Photon by sixthsurge (MIT ++//! licence, see THIRD_PARTY_PHOTON.md), shaders/include/sky/atmosphere.glsl at commit 26c2906. ++ ++/// The sky-view texture: 128 azimuths by 64 elevations, then 8 rows for the ambient samples ++/// (row 64: texel 0 the zenith, texel 1 the horizon's average). ++pub const SKY_LUT_SIZE: [u32; 2] = [128, 72]; ++pub const SKY_LUT_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float; ++ ++/// The atmosphere's constants and the pass that marches the sky-view texture (after `COMMON`). ++pub const SKY_PASS: &str = r#" ++@group(0) @binding(0) var frame: HdFrame; ++ ++// Photon's atmosphere (sixthsurge, MIT): metres, Earth's size; the camera stands on the surface. ++const PI: f32 = 3.14159265; ++const PLANET_R: f32 = 6371000.0; ++const ATMOSPHERE_TOP: f32 = 6481000.0; ++const GROUND_R: f32 = 6370000.0; ++const SCALE_HEIGHTS: vec2 = vec2(8400.0, 1250.0); ++const RAYLEIGH: vec3 = vec3(8.059375432e-06, 1.671209429e-05, 4.080133294e-05); ++const MIE: vec3 = vec3(1.666442358e-06, 1.812685127e-06, 1.958927896e-06); ++const OZONE: vec3 = vec3(8.304280072e-07, 1.314911970e-06, 5.440679729e-08); ++const MIE_ALBEDO: f32 = 0.9; ++const MIE_G: f32 = 0.77; ++// Sunlight in space (Photon's AM0 colour) and how bright the sun and the moon shine here. ++const SUN_I: vec3 = vec3(1.051, 0.985, 0.940) * 24.0; ++const MOON_I: vec3 = vec3(0.62, 0.78, 1.0) * 0.07; ++// The share of the light scattered a second time, spread evenly (the haze's lift). ++const MULTI_SCATTER: f32 = 0.6; ++const STEPS: i32 = 32; ++ ++// Photon's density: Rayleigh, Mie (exponential) and the ozone layer, at an altitude in metres. ++fn density_at(altitude: f32) -> vec3 { ++ let rm = exp(-max(altitude, 0.0) / SCALE_HEIGHTS); ++ let km = altitude * 1e-3; ++ let o1 = 12.5 * exp(-km / 8.0); ++ let o2 = 30.0 * exp((18.0 - km) * (km - 18.0) / 80.0); ++ let o3 = 75.0 * exp((23.5 - km) * (km - 23.5) / 50.0); ++ let o4 = 50.0 * exp((30.0 - km) * (km - 30.0) / 150.0); ++ return vec3(rm, 7.428e-3 * (o1 + o2 + o3 + o4)); ++} ++ ++// Chapman function approximation (thetenthplanet.de/archives/4519, as Photon uses it). ++fn chapman(x: f32, cos_theta: f32) -> f32 { ++ let c = sqrt(1.5707963 * x); ++ if cos_theta >= 0.0 { ++ return c / ((c - 1.0) * cos_theta + 1.0); ++ } ++ let sin_theta = sqrt(clamp(1.0 - cos_theta * cos_theta, 0.0, 1.0)); ++ return c / ((c - 1.0) * cos_theta - 1.0) + 2.0 * c * exp(min(x - x * sin_theta, 80.0)) * sqrt(sin_theta); ++} ++ ++// The light reaching a point r from the planet's centre (altitude h, whose sky direction makes ++// cos mu with the up there) from the sun or the moon; 0 when the planet is in the way. ++fn light_transmittance(mu: f32, r: f32, h: f32) -> vec3 { ++ if mu < 0.0 && r * r * (1.0 - mu * mu) < PLANET_R * PLANET_R { return vec3(0.0); } ++ let dens = exp(-max(h, 0.0) / SCALE_HEIGHTS); ++ var air = SCALE_HEIGHTS * dens; ++ air.x *= chapman(r / SCALE_HEIGHTS.x, mu); ++ air.y *= chapman(r / SCALE_HEIGHTS.y, mu); ++ return clamp(exp(-(RAYLEIGH * air.x + MIE * air.y + OZONE * air.x)), vec3(0.0), vec3(1.0)); ++} ++ ++fn rayleigh_phase(nu: f32) -> f32 { ++ return 3.0 / (16.0 * PI) * (1.0 + nu * nu); ++} ++ ++fn mie_phase(nu: f32) -> f32 { ++ let g = MIE_G; ++ return (1.0 - g * g) / (4.0 * PI * pow(1.0 + g * g - 2.0 * g * nu, 1.5)); ++} ++ ++// The radiance the camera sees along `view` (y up) with the sun and the moon in the directions ++// given: single scattering plus a flat multiple-scattering term. ++fn march(view: vec3, sun: vec3, moon: vec3) -> vec3 { ++ let b = PLANET_R * view.y; ++ let t_max = -b + sqrt(b * b + ATMOSPHERE_TOP * ATMOSPHERE_TOP - PLANET_R * PLANET_R); ++ let nu_sun = dot(view, sun); ++ let nu_moon = dot(view, moon); ++ let phase_r_sun = rayleigh_phase(nu_sun); ++ let phase_m_sun = mie_phase(nu_sun); ++ let phase_r_moon = rayleigh_phase(nu_moon); ++ let phase_m_moon = mie_phase(nu_moon); ++ var od = vec3(0.0); ++ var sum = vec3(0.0); ++ var t0 = 0.0; ++ for (var i = 0; i < STEPS; i++) { ++ let u = (f32(i) + 1.0) / f32(STEPS); ++ let t1 = t_max * u * u; ++ let dt = t1 - t0; ++ let t = 0.5 * (t0 + t1); ++ t0 = t1; ++ let r2 = PLANET_R * PLANET_R + 2.0 * PLANET_R * t * view.y + t * t; ++ let r = sqrt(r2); ++ // The altitude without the cancellation of r - planet radius. ++ let altitude = (2.0 * PLANET_R * t * view.y + t * t) / (r + PLANET_R); ++ let dens = density_at(altitude); ++ let extinction = RAYLEIGH * dens.x + MIE * dens.y + OZONE * dens.z; ++ let view_t = exp(-(od + 0.5 * extinction * dt)); ++ od += extinction * dt; ++ let mu_sun = (PLANET_R * sun.y + t * nu_sun) / r; ++ let mu_moon = (PLANET_R * moon.y + t * nu_moon) / r; ++ let sun_t = light_transmittance(mu_sun, r, altitude); ++ let moon_t = light_transmittance(mu_moon, r, altitude); ++ let scatter_r = RAYLEIGH * dens.x; ++ let scatter_m = MIE * MIE_ALBEDO * dens.y; ++ let single = (scatter_r * phase_r_sun + scatter_m * phase_m_sun) * sun_t * SUN_I ++ + (scatter_r * phase_r_moon + scatter_m * phase_m_moon) * moon_t * MOON_I; ++ let multi = (scatter_r + scatter_m) * (1.0 / (4.0 * PI)) * MULTI_SCATTER ++ * (sun_t * SUN_I + moon_t * MOON_I) * 0.5; ++ sum += view_t * (single + multi) * dt; ++ } ++ return sum; ++} ++ ++@fragment ++fn fs_sky_lut(in: FullOut) -> @location(0) vec4 { ++ let px = vec2(in.position.xy); ++ // The texture's frame: the sun in the xy plane, the moon opposite. ++ let sin_e = clamp(frame.sky_sun.y, -1.0, 1.0); ++ let sun = vec3(sqrt(1.0 - sin_e * sin_e), sin_e, 0.0); ++ let moon = -sun; ++ // A faint night glow, so the night sky is not black. ++ let night = vec3(0.0006, 0.0011, 0.0026); ++ if px.y < 64 { ++ let phi = f32(px.x) / 127.0 * PI; ++ let v = f32(px.y) / 63.0; ++ let l = v * v * PI * 0.5; ++ let dir = vec3(cos(l) * cos(phi), sin(l), cos(l) * sin(phi)); ++ return vec4(march(dir, sun, moon) + night, 1.0); ++ } ++ if px.y == 64 && px.x == 0 { ++ return vec4(march(vec3(0.0, 1.0, 0.0), sun, moon) + night, 1.0); ++ } ++ if px.y == 64 && px.x == 1 { ++ var sum = vec3(0.0); ++ for (var k = 0; k < 6; k++) { ++ let phi = (f32(k) + 0.5) / 6.0 * PI; ++ let l = 0.3; ++ sum += march(vec3(cos(l) * cos(phi), sin(l), cos(l) * sin(phi)), sun, moon); ++ } ++ return vec4(sum / 6.0 + night, 1.0); ++ } ++ return vec4(0.0); ++} ++"#; ++ ++/// Reading the sky-view texture, in the lighting and the water shaders (`SKYBIND` is replaced by ++/// the binding of the texture; `frame` and the `linear` sampler come before it). ++pub const SKY_FUNCS: &str = r#" ++@group(0) @binding(SKYBIND) var sky_lut: texture_2d; ++const SKY_PI: f32 = 3.14159265; ++// How bright the sky is shown (the exposure of the physical radiance) and its gamma. ++const SKY_EXPOSURE: f32 = 0.85; ++ ++// Where the direction `dir` (world, y up) lies in the sky-view texture. ++fn sky_uv(dir: vec3) -> vec2 { ++ let s = frame.sky_sun.xyz; ++ let l = asin(clamp(dir.y, 0.0, 1.0)); ++ var cphi = 1.0; ++ let dl = length(dir.xz); ++ let sl = length(s.xz); ++ if dl > 1e-4 && sl > 1e-4 { cphi = dot(dir.xz / dl, s.xz / sl); } ++ let u = acos(clamp(cphi, -1.0, 1.0)) / SKY_PI; ++ let v = sqrt(l / (SKY_PI * 0.5)); ++ return vec2((u * 127.0 + 0.5) / 128.0, (v * 63.0 + 0.5) / 72.0); ++} ++ ++// The physical sky's colour as it is shown: a soft shoulder, then the display's gamma. ++fn sky_show(radiance: vec3) -> vec3 { ++ // Compressed by the brightest channel, so the hue stays as the atmosphere made it. ++ let peak = max(max(radiance.r, radiance.g), max(radiance.b, 1e-6)); ++ let c = pow(radiance * ((1.0 - exp(-peak * SKY_EXPOSURE)) / peak), vec3(1.0 / 1.9)); ++ // A little more colour (Photon boosts the atmosphere's saturation too). ++ // The low sun gets more, for orange and pink in place of pastel. ++ let boost = 1.2 + 0.45 * (1.0 - smoothstep(0.0, 0.35, frame.sky_sun.y)); ++ return max(mix(vec3(dot(c, vec3(0.2126, 0.7152, 0.0722))), c, boost), vec3(0.0)); ++} ++ ++// The vanilla sky's own gradient (the overcast and the rain keep it). ++fn vanilla_gradient(dir: vec3) -> vec3 { ++ return mix(frame.fog_color.rgb, frame.sky_color.rgb, smoothstep(0.0, 0.5, max(dir.y, 0.0))); ++} ++ ++// The sky along `dir`: the atmosphere, greyed towards vanilla's colours in rain. ++fn atmosphere_sky(dir: vec3) -> vec3 { ++ let lin = textureSampleLevel(sky_lut, linear, sky_uv(dir), 0.0).rgb; ++ var c = sky_show(lin); ++ let overcast = frame.sky_extra.z; ++ if overcast > 0.0 { c = mix(c, vanilla_gradient(dir), overcast); } ++ return c; ++} ++ ++// The colour distant things fade to: the sky just above the horizon in their direction (so the ++// haze leans towards the sun), or vanilla's fog colour where the environment sets its own fog. ++fn haze_colour(dir: vec3) -> vec3 { ++ if frame.sky_sun.w < 0.5 || frame.fog.x < 1.0e6 { return frame.fog_color.rgb; } ++ let up = max(dir.y, 0.0) * 0.5 + 0.035; ++ return atmosphere_sky(normalize(vec3(dir.x, 0.0, dir.z) + vec3(0.0, up, 0.0))); ++} ++ ++// The sky's light for a surface of normal n: the horizon's colour from the sides, the zenith's ++// from above, as a hue (luminance 1) to tint vanilla's sky light with. ++fn sky_light_tint(n: vec3) -> vec3 { ++ let zenith = sky_show(textureLoad(sky_lut, vec2(0, 64), 0).rgb); ++ let horizon = sky_show(textureLoad(sky_lut, vec2(1, 64), 0).rgb); ++ let up = clamp(n.y * 0.5 + 0.5, 0.0, 1.0); ++ let c = mix(horizon, zenith, smoothstep(0.35, 1.0, up)); ++ let luma = dot(c, vec3(0.2126, 0.7152, 0.0722)); ++ return c / max(luma, 0.003); ++} ++ ++// vanilla's sky (terrain.wgsl: fs_sky), to tell what the world drew on the sky pixel (the sun and ++// moon sprites, the stars, the clouds) from its plain gradient. ++fn vanilla_disc_distance(d: vec3, y: f32) -> vec2 { ++ if d.y * y <= 0.0 { return vec2(-1.0); } ++ let p = d * (y / d.y); ++ let h = length(p.xz); ++ let phi = atan2(p.z, p.x); ++ let sector = SKY_PI / 4.0; ++ let delta = phi - (floor(phi / sector) * sector + sector * 0.5); ++ let edge = 512.0 * cos(sector * 0.5) / cos(delta); ++ if h > edge { return vec2(-1.0); } ++ let s = h / edge; ++ let ay = abs(y); ++ let rim = sqrt(512.0 * 512.0 + y * y); ++ return vec2(ay + s * (rim - ay), ay + s * (512.0 - ay)); ++} ++fn vanilla_linear_fog(d: f32, start: f32, end: f32) -> f32 { ++ if d <= start { return 0.0; } ++ if d >= end { return 1.0; } ++ return (d - start) / (end - start); ++} ++fn vanilla_rotate_x90(v: vec3) -> vec3 { return vec3(v.x, -v.z, v.y); } ++fn vanilla_sunrise_weight(d: vec3) -> f32 { ++ let alpha = frame.sunrise.a; ++ if alpha <= 0.001 { return 0.0; } ++ let flip = select(0.0, 180.0, frame.sky_sun.x > 0.0); ++ let theta = radians(flip + 90.0); ++ let c = cos(theta); ++ let s = sin(theta); ++ var best = 0.0; ++ let apex = vanilla_rotate_x90(vec3(-100.0 * s, 100.0 * c, 0.0)); ++ for (var i = 0; i < 16; i++) { ++ let a0 = f32(i) * 2.0 * SKY_PI / 16.0; ++ let a1 = f32(i + 1) * 2.0 * SKY_PI / 16.0; ++ let l0 = vec3(sin(a0) * 120.0, cos(a0) * 120.0, -cos(a0) * 40.0 * alpha); ++ let l1 = vec3(sin(a1) * 120.0, cos(a1) * 120.0, -cos(a1) * 40.0 * alpha); ++ let r0 = vanilla_rotate_x90(vec3(l0.x * c - l0.y * s, l0.x * s + l0.y * c, l0.z)); ++ let r1 = vanilla_rotate_x90(vec3(l1.x * c - l1.y * s, l1.x * s + l1.y * c, l1.z)); ++ let e1 = r0 - apex; ++ let e2 = r1 - apex; ++ let pv = cross(d, e2); ++ let det = dot(e1, pv); ++ if abs(det) < 1e-6 { continue; } ++ let tv = -apex; ++ let u = dot(tv, pv) / det; ++ let qv = cross(tv, e1); ++ let v = dot(d, qv) / det; ++ let t = dot(e2, qv) / det; ++ if u >= 0.0 && v >= 0.0 && u + v <= 1.0 && t > 0.0 { ++ best = max(best, 1.0 - u - v); ++ } ++ } ++ return best; ++} ++fn vanilla_sky(d: vec3) -> vec3 { ++ var color = frame.fog_color.rgb; ++ let dist = vanilla_disc_distance(d, 16.0); ++ if dist.x >= 0.0 { ++ let end = frame.sky_extra.x; ++ let f = max(vanilla_linear_fog(dist.x, 0.0, end), vanilla_linear_fog(dist.y, end, end)); ++ color = mix(frame.sky_color.rgb, frame.fog_color.rgb, f * frame.fog_color.a); ++ } ++ let w = vanilla_sunrise_weight(d); ++ if w > 0.0 { ++ color = mix(color, frame.sunrise.rgb, w * frame.sunrise.a); ++ } ++ return color; ++} ++"#; ++ ++/// The sun's light at the ground: the atmosphere's transmittance along the sun's direction ++/// (`sin_elevation`, the y of the unit vector towards it), linear, as a fraction of the light ++/// outside the air. Same Chapman model as the sky pass. ++pub fn sun_transmittance(sin_elevation: f32) -> glam::Vec3 { ++ const PLANET: f32 = 6_371_000.0; ++ const SCALE: [f32; 2] = [8400.0, 1250.0]; ++ const RAYLEIGH: glam::Vec3 = glam::Vec3::new(8.059_375e-6, 1.671_209_4e-5, 4.080_133_4e-5); ++ const MIE: glam::Vec3 = glam::Vec3::new(1.666_442_4e-6, 1.812_685e-6, 1.958_927_9e-6); ++ const OZONE: glam::Vec3 = glam::Vec3::new(8.304_28e-7, 1.314_912e-6, 5.440_679_7e-8); ++ // A sun below the horizon keeps the horizon's colour (its strength is faded elsewhere). ++ let mu = sin_elevation.clamp(0.0, 1.0); ++ let chapman = |x: f32, c: f32| { ++ let k = (std::f32::consts::FRAC_PI_2 * x).sqrt(); ++ k / ((k - 1.0) * c + 1.0) ++ }; ++ let air = [ ++ SCALE[0] * chapman(PLANET / SCALE[0], mu), ++ SCALE[1] * chapman(PLANET / SCALE[1], mu), ++ ]; ++ let tau = RAYLEIGH * air[0] + MIE * air[1] + OZONE * air[0]; ++ glam::Vec3::new((-tau.x).exp(), (-tau.y).exp(), (-tau.z).exp()) ++} ++ ++#[cfg(test)] ++mod tests { ++ use super::*; ++ ++ #[test] ++ fn the_low_sun_is_redder_than_the_high_one() { ++ let noon = sun_transmittance(1.0); ++ let low = sun_transmittance(0.1); ++ assert!(noon.x > 0.85 && noon.z > 0.6, "{noon:?}"); ++ assert!(low.z / low.x < noon.z / noon.x * 0.6, "{low:?} vs {noon:?}"); ++ } ++} +diff --git a/crates/horizon-client/src/terrain.wgsl b/crates/horizon-client/src/terrain.wgsl +index e892d5a0f..0a2a6cdba 100644 +--- a/crates/horizon-client/src/terrain.wgsl ++++ b/crates/horizon-client/src/terrain.wgsl +@@ -197,7 +197,7 @@ struct WaterOut { + if layer != WATER_STILL && layer != WATER_FLOW { discard; } + // The biome's colour at the texture's mean brightness, not the texture: the HD water draws + // its own surface (waves, reflections), and vanilla's ripples on top would look painted on. +- let color = vec3(0.62) * in.tint.rgb; ++ let color = vec3(0.85) * in.tint.rgb; + var out: WaterOut; + out.tint = vec4(color, clamp(in.lm.y, 0.0, 1.0)); + out.normal = vec4(face_normal(in.pos) * 0.5 + 0.5, clamp(in.lm.x, 0.0, 1.0)); +-- +2.54.0 (Apple Git-157) +