Port FogRenderer and the vanilla sky disc
fog.rs implements the fog environments (atmospheric with rain fog, water with water vision, lava, powder snow, blindness), the camera fog type and computeFogColor (sunrise tint, sky mix, void darkness). The sky shader evaluates SkyRenderer's sky disc, sunrise/sunset fan and dark disc per pixel with sky fog; clouds fade by the cloud fog end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,8 @@ struct Globals {
|
||||
inverse_view: mat4x4<f32>,
|
||||
sunrise: vec4<f32>,
|
||||
sun: vec4<f32>,
|
||||
// x: sky fog end, y: cloud fog end
|
||||
fog_ends: vec4<f32>,
|
||||
}
|
||||
@group(0) @binding(0) var<uniform> globals: Globals;
|
||||
@group(0) @binding(1) var atlas: texture_2d<f32>;
|
||||
@@ -21,6 +23,7 @@ struct Out {
|
||||
@location(1) color: vec4<f32>,
|
||||
@location(2) spherical: f32,
|
||||
@location(3) cylindrical: f32,
|
||||
@location(4) @interpolate(flat) overlay: u32,
|
||||
}
|
||||
@vertex fn vs_entity(@location(0) position: vec3<f32>, @location(1) uv: vec2<f32>,
|
||||
@location(2) overlay: u32, @location(3) color: vec4<f32>, @location(4) light: vec4<u32>) -> Out {
|
||||
@@ -30,14 +33,16 @@ struct Out {
|
||||
let l = vec2(f32(light.y), f32(light.x));
|
||||
let lm = textureSampleLevel(lightmap, lightmap_sampler,
|
||||
clamp(l / 256.0 + 0.5 / 16.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)), 0.0);
|
||||
// overlay 2: celestial bodies use their vertex colour unlit.
|
||||
var c = select(color * lm, color, overlay == 2u);
|
||||
// overlay 2: celestial bodies, 4: clouds; both use their vertex colour unlit.
|
||||
var c = select(color * lm, color, overlay == 2u || overlay == 4u);
|
||||
// Hurt overlay (OverlayTexture red tint).
|
||||
if overlay == 1u { c = vec4(mix(c.rgb, vec3(1.0, 0.0, 0.0), 0.3), c.a); }
|
||||
out.color = c;
|
||||
// Sky geometry (clouds, celestial bodies) is exempt from render-distance fog.
|
||||
out.spherical = select(length(position), 0.0, overlay >= 2u);
|
||||
out.cylindrical = select(max(length(position.xz), abs(position.y)), 0.0, overlay >= 2u);
|
||||
out.overlay = overlay;
|
||||
if overlay == 4u { out.spherical = length(position); }
|
||||
return out;
|
||||
}
|
||||
fn linear_fog(d: f32, start: f32, end: f32) -> f32 {
|
||||
@@ -47,6 +52,10 @@ fn linear_fog(d: f32, start: f32, end: f32) -> f32 {
|
||||
}
|
||||
@fragment fn fs_entity(in: Out) -> @location(0) vec4<f32> {
|
||||
let color = textureSample(atlas, atlas_sampler, in.uv) * in.color;
|
||||
if in.overlay == 4u {
|
||||
// clouds.fsh: alpha fades out towards the cloud fog end.
|
||||
return vec4(color.rgb, color.a * (1.0 - linear_fog(in.spherical, 0.0, globals.fog_ends.y)));
|
||||
}
|
||||
if color.a < 0.1 { discard; }
|
||||
let f = max(linear_fog(in.spherical, globals.fog.x, globals.fog.y),
|
||||
linear_fog(in.cylindrical, globals.fog.z, globals.fog.w));
|
||||
|
||||
@@ -0,0 +1,399 @@
|
||||
//! `FogRenderer` and its fog environments (atmospheric with rain fog, water
|
||||
//! with water vision, lava, powder snow, blindness), the camera fog type
|
||||
//! (`Camera.getFluidInCamera`) and the fog colour (`computeFogColor`: sunrise
|
||||
//! tint, sky-colour mix, void darkness, water-vision brightening).
|
||||
use crate::{
|
||||
biome::Biomes,
|
||||
blocks::{BlockFacts, Fluid},
|
||||
environment::Sky,
|
||||
world::World,
|
||||
};
|
||||
use std::path::Path;
|
||||
|
||||
/// `FogType` of the camera position.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum FogType {
|
||||
Atmospheric,
|
||||
Water,
|
||||
Lava,
|
||||
PowderSnow,
|
||||
}
|
||||
|
||||
/// The fog uniform (`FogData`).
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct FogData {
|
||||
pub color: [f32; 4],
|
||||
pub environmental_start: f32,
|
||||
pub environmental_end: f32,
|
||||
pub render_distance_start: f32,
|
||||
pub render_distance_end: f32,
|
||||
pub sky_end: f32,
|
||||
pub cloud_end: f32,
|
||||
}
|
||||
|
||||
/// Per-biome water fog end multipliers (`visual/water_fog_end_distance`).
|
||||
pub struct FogTables {
|
||||
water_fog_end: Vec<f32>,
|
||||
}
|
||||
impl FogTables {
|
||||
pub fn load(root: &Path, biomes: &Biomes) -> Self {
|
||||
let mut water_fog_end = vec![1.0; biomes.list.len()];
|
||||
for (name, &id) in &biomes.index {
|
||||
let path = root.join(format!(
|
||||
"data/minecraft/worldgen/biome/{}.json",
|
||||
name.trim_start_matches("minecraft:")
|
||||
));
|
||||
let Ok(bytes) = std::fs::read(path) else {
|
||||
continue;
|
||||
};
|
||||
let Ok(json) = serde_json::from_slice::<serde_json::Value>(&bytes) else {
|
||||
continue;
|
||||
};
|
||||
let v = &json["attributes"]["minecraft:visual/water_fog_end_distance"];
|
||||
if v["modifier"].as_str() == Some("multiply")
|
||||
&& let (Some(f), Some(slot)) =
|
||||
(v["argument"].as_f64(), water_fog_end.get_mut(id as usize))
|
||||
{
|
||||
*slot = f as f32;
|
||||
}
|
||||
}
|
||||
Self { water_fog_end }
|
||||
}
|
||||
}
|
||||
|
||||
/// Fog state that evolves over time.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct FogState {
|
||||
/// `LocalPlayer.waterVisionTime` (0..600).
|
||||
pub water_vision_time: i32,
|
||||
/// `AtmosphericFogEnvironment.rainFogMultiplier`.
|
||||
pub rain_fog_multiplier: f32,
|
||||
}
|
||||
impl FogState {
|
||||
/// `LocalPlayer.aiStep`: water vision ramps up while the eye is in water.
|
||||
pub fn tick(&mut self, eye_in_water: bool) {
|
||||
self.water_vision_time = if eye_in_water {
|
||||
(self.water_vision_time + 1).min(600)
|
||||
} else {
|
||||
(self.water_vision_time - 10).max(0)
|
||||
};
|
||||
}
|
||||
/// `LocalPlayer.getWaterVision`.
|
||||
pub fn water_vision(&self, eye_in_water: bool) -> f32 {
|
||||
if !eye_in_water {
|
||||
return 0.0;
|
||||
}
|
||||
let t = self.water_vision_time as f32;
|
||||
if t >= 600.0 {
|
||||
return 1.0;
|
||||
}
|
||||
let a = (t / 100.0).clamp(0.0, 1.0);
|
||||
let b = if t < 100.0 {
|
||||
0.0
|
||||
} else {
|
||||
((t - 100.0) / 500.0).clamp(0.0, 1.0)
|
||||
};
|
||||
a * 0.6 + b * 0.399_999_98
|
||||
}
|
||||
}
|
||||
|
||||
/// The fog tables and evolving state kept by the game.
|
||||
pub struct FogRenderer {
|
||||
pub tables: FogTables,
|
||||
pub state: FogState,
|
||||
}
|
||||
impl FogRenderer {
|
||||
pub fn load(root: &Path, biomes: &Biomes) -> Self {
|
||||
Self {
|
||||
tables: FogTables::load(root, biomes),
|
||||
state: FogState::default(),
|
||||
}
|
||||
}
|
||||
pub fn setup(&self, input: &FogInputs) -> FogData {
|
||||
setup_fog(&self.tables, &self.state, input)
|
||||
}
|
||||
}
|
||||
/// `FluidState.getHeightForCamera`.
|
||||
fn camera_fluid_height(world: &World, facts: &BlockFacts, b: [i32; 3]) -> Option<(Fluid, f64)> {
|
||||
let s = facts.get(world.block(b[0], b[1], b[2]));
|
||||
if s.fluid == Fluid::None {
|
||||
return None;
|
||||
}
|
||||
let above = facts.get(world.block(b[0], b[1] + 1, b[2]));
|
||||
let source = s.fluid_level >= 8 && !s.fluid_falling;
|
||||
let height = if source && above.full_collision {
|
||||
1.0
|
||||
} else if above.fluid == s.fluid {
|
||||
1.0
|
||||
} else {
|
||||
f64::from(s.fluid_level) / 9.0
|
||||
};
|
||||
Some((s.fluid, height))
|
||||
}
|
||||
/// `Camera.getFluidInCamera` (near-plane points reduced to the camera and
|
||||
/// the point just in front of it).
|
||||
pub fn fog_type(world: &World, facts: &BlockFacts, camera: [f64; 3], forward: [f64; 3]) -> FogType {
|
||||
let block = |p: [f64; 3]| {
|
||||
[
|
||||
p[0].floor() as i32,
|
||||
p[1].floor() as i32,
|
||||
p[2].floor() as i32,
|
||||
]
|
||||
};
|
||||
let b = block(camera);
|
||||
if let Some((Fluid::Water, h)) = camera_fluid_height(world, facts, b)
|
||||
&& camera[1] < f64::from(b[1]) + h
|
||||
{
|
||||
return FogType::Water;
|
||||
}
|
||||
for p in [
|
||||
[
|
||||
camera[0] + forward[0] * 0.05,
|
||||
camera[1] + forward[1] * 0.05,
|
||||
camera[2] + forward[2] * 0.05,
|
||||
],
|
||||
camera,
|
||||
] {
|
||||
let b = block(p);
|
||||
match camera_fluid_height(world, facts, b) {
|
||||
Some((Fluid::Lava, h)) if p[1] <= f64::from(b[1]) + h => return FogType::Lava,
|
||||
Some((Fluid::Lava, _)) => {}
|
||||
_ => {
|
||||
if &*facts.get(world.block(b[0], b[1], b[2])).name == "minecraft:powder_snow" {
|
||||
return FogType::PowderSnow;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
FogType::Atmospheric
|
||||
}
|
||||
|
||||
/// Everything `FogRenderer.setupFog` reads for one frame.
|
||||
pub struct FogInputs<'a> {
|
||||
pub fog_type: FogType,
|
||||
pub sky: &'a Sky,
|
||||
/// Biome water fog colour (`visual/water_fog_color`).
|
||||
pub water_fog_color: [f32; 3],
|
||||
pub biome: u16,
|
||||
pub render_distance_chunks: i32,
|
||||
pub camera_y: f64,
|
||||
pub min_y: i32,
|
||||
/// Flat worlds use a 1-block void darkness range.
|
||||
pub flat: bool,
|
||||
pub forward: [f32; 3],
|
||||
pub rain: f32,
|
||||
pub thunder: f32,
|
||||
/// Dimensions with a sky disc (overworld): sunrise tint and sky mix apply.
|
||||
pub skybox: bool,
|
||||
/// Blindness: (remaining duration ticks, infinite).
|
||||
pub blindness: Option<(i32, bool)>,
|
||||
}
|
||||
|
||||
/// `FogRenderer.setupFog` + `computeFogColor`.
|
||||
pub fn setup_fog(tables: &FogTables, state: &FogState, input: &FogInputs) -> FogData {
|
||||
let rd = input.render_distance_chunks as f32 * 16.0;
|
||||
let sky = input.sky;
|
||||
let eye_in_water = input.fog_type == FogType::Water;
|
||||
let water_vision = state.water_vision(eye_in_water);
|
||||
// Base colour of the applicable environment.
|
||||
let mut color = match input.fog_type {
|
||||
FogType::Lava => rgb(0x99_1A00),
|
||||
FogType::PowderSnow => rgb(0x9F_BACC),
|
||||
FogType::Water => input.water_fog_color,
|
||||
FogType::Atmospheric => {
|
||||
let mut fog = sky.fog_color;
|
||||
if input.skybox && input.render_distance_chunks >= 4 {
|
||||
let sun_x = if sky.sun_angle.to_radians().sin() > 0.0 {
|
||||
-1.0
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
let looking = input.forward[0] * sun_x;
|
||||
let alpha = sky.sunrise_color[3];
|
||||
if looking > 0.0 && alpha > 0.0 {
|
||||
let t = looking * alpha;
|
||||
fog = std::array::from_fn(|i| fog[i] + (sky.sunrise_color[i] - fog[i]) * t);
|
||||
}
|
||||
}
|
||||
let mut sky_color = sky.sky_color;
|
||||
if input.rain > 0.0 {
|
||||
let m = 1.0 - input.rain * 0.5;
|
||||
let mb = 1.0 - input.rain * 0.4;
|
||||
sky_color = [sky_color[0] * m, sky_color[1] * m, sky_color[2] * mb];
|
||||
}
|
||||
if input.thunder > 0.0 {
|
||||
let m = 1.0 - input.thunder * 0.5;
|
||||
sky_color = sky_color.map(|c| c * m);
|
||||
}
|
||||
let sky_fog_end = (512.0f32 / 16.0).min(input.render_distance_chunks as f32);
|
||||
let mix = (sky_fog_end / 32.0).clamp(0.0, 1.0).mul_add(0.75, 0.25);
|
||||
let mix = 1.0 - mix.powf(0.25);
|
||||
std::array::from_fn(|i| fog[i] + (sky_color[i] - fog[i]) * mix)
|
||||
}
|
||||
};
|
||||
// Void darkness and blindness.
|
||||
let range = if input.flat { 1.0 } else { 32.0 };
|
||||
let mut darkness =
|
||||
((range + input.min_y as f32 - input.camera_y as f32) / range).clamp(0.0, 1.0);
|
||||
if let Some((duration, infinite)) = input.blindness {
|
||||
darkness = if !infinite && duration < 20 {
|
||||
darkness.max(duration as f32 / 20.0)
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
}
|
||||
if darkness > 0.0 && !matches!(input.fog_type, FogType::Lava | FogType::PowderSnow) {
|
||||
let brightness = (1.0 - darkness) * (1.0 - darkness);
|
||||
color = color.map(|c| c * brightness);
|
||||
}
|
||||
let brighten = if eye_in_water { water_vision } else { 0.0 };
|
||||
if color.iter().all(|&c| c != 0.0) {
|
||||
let scale = 1.0 / color[0].max(color[1]).max(color[2]);
|
||||
color = color.map(|c| c + (c * scale - c) * brighten);
|
||||
}
|
||||
let mut fog = FogData {
|
||||
color: [color[0], color[1], color[2], 1.0],
|
||||
environmental_start: 0.0,
|
||||
environmental_end: 1024.0,
|
||||
render_distance_start: 0.0,
|
||||
render_distance_end: rd,
|
||||
sky_end: rd.min(512.0),
|
||||
cloud_end: 2048.0,
|
||||
};
|
||||
match input.fog_type {
|
||||
FogType::Lava => {
|
||||
fog.environmental_start = 0.25;
|
||||
fog.environmental_end = 1.0;
|
||||
fog.sky_end = 1.0;
|
||||
fog.cloud_end = 1.0;
|
||||
}
|
||||
FogType::PowderSnow => {
|
||||
fog.environmental_start = 0.0;
|
||||
fog.environmental_end = 2.0;
|
||||
fog.sky_end = 2.0;
|
||||
fog.cloud_end = 2.0;
|
||||
}
|
||||
_ if input.blindness.is_some() => {
|
||||
let (duration, infinite) = input.blindness.unwrap_or((0, true));
|
||||
let distance = if infinite {
|
||||
5.0
|
||||
} else {
|
||||
rd + (5.0 - rd) * (duration as f32 / 20.0).min(1.0)
|
||||
};
|
||||
fog.environmental_start = distance * 0.25;
|
||||
fog.environmental_end = distance;
|
||||
fog.sky_end = distance * 0.8;
|
||||
fog.cloud_end = distance * 0.8;
|
||||
}
|
||||
FogType::Water => {
|
||||
fog.environmental_start = -8.0;
|
||||
let factor = tables
|
||||
.water_fog_end
|
||||
.get(input.biome as usize)
|
||||
.copied()
|
||||
.unwrap_or(1.0);
|
||||
fog.environmental_end = 96.0 * factor * water_vision.max(0.25);
|
||||
fog.sky_end = fog.environmental_end;
|
||||
fog.cloud_end = fog.environmental_end;
|
||||
}
|
||||
FogType::Atmospheric => {
|
||||
fog.environmental_start = sky.fog_start + -160.0 * state.rain_fog_multiplier;
|
||||
let min_end = sky.fog_end.min(96.0);
|
||||
fog.environmental_end = min_end.max(sky.fog_end + -256.0 * state.rain_fog_multiplier);
|
||||
fog.sky_end = rd.min(512.0);
|
||||
fog.cloud_end = 2048.0f32.min(128.0 * 16.0);
|
||||
}
|
||||
}
|
||||
let span = (rd / 10.0).clamp(4.0, 64.0);
|
||||
fog.render_distance_start = rd - span;
|
||||
fog.render_distance_end = rd;
|
||||
fog
|
||||
}
|
||||
/// `AtmosphericFogEnvironment.updateRainFogState`.
|
||||
pub fn update_rain_fog(
|
||||
state: &mut FogState,
|
||||
rain: f32,
|
||||
sky_light: u8,
|
||||
biome_rains: bool,
|
||||
delta_ticks: f32,
|
||||
) {
|
||||
let sky = ((f32::from(sky_light) - 8.0) / 7.0).clamp(0.0, 1.0);
|
||||
let target = rain * sky * if biome_rains { 1.0 } else { 0.5 };
|
||||
state.rain_fog_multiplier += (target - state.rain_fog_multiplier) * delta_ticks * 0.2;
|
||||
}
|
||||
fn rgb(c: u32) -> [f32; 3] {
|
||||
[
|
||||
(c >> 16 & 255) as f32 / 255.0,
|
||||
(c >> 8 & 255) as f32 / 255.0,
|
||||
(c & 255) as f32 / 255.0,
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
fn inputs(sky: &Sky) -> FogInputs<'_> {
|
||||
FogInputs {
|
||||
fog_type: FogType::Atmospheric,
|
||||
sky,
|
||||
water_fog_color: [0.0, 0.0, 0.2],
|
||||
biome: 0,
|
||||
render_distance_chunks: 12,
|
||||
camera_y: 70.0,
|
||||
min_y: -64,
|
||||
flat: false,
|
||||
forward: [0.0, 0.0, 1.0],
|
||||
rain: 0.0,
|
||||
thunder: 0.0,
|
||||
skybox: true,
|
||||
blindness: None,
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn render_distance_fog_span() {
|
||||
let sky = Sky {
|
||||
fog_end: 1024.0,
|
||||
fog_color: [0.5; 3],
|
||||
sky_color: [0.5; 3],
|
||||
..Sky::default()
|
||||
};
|
||||
let tables = FogTables {
|
||||
water_fog_end: vec![1.0],
|
||||
};
|
||||
let fog = setup_fog(&tables, &FogState::default(), &inputs(&sky));
|
||||
// 12 chunks = 192 blocks, span clamp(19.2, 4, 64).
|
||||
assert!((fog.render_distance_start - (192.0 - 19.2)).abs() < 1e-4);
|
||||
assert_eq!(fog.render_distance_end, 192.0);
|
||||
assert_eq!(fog.sky_end, 192.0);
|
||||
}
|
||||
#[test]
|
||||
fn water_vision_ramps() {
|
||||
let mut s = FogState::default();
|
||||
for _ in 0..100 {
|
||||
s.tick(true);
|
||||
}
|
||||
assert!((s.water_vision(true) - 0.6).abs() < 1e-6);
|
||||
for _ in 0..500 {
|
||||
s.tick(true);
|
||||
}
|
||||
assert_eq!(s.water_vision(true), 1.0);
|
||||
assert_eq!(s.water_vision(false), 0.0);
|
||||
}
|
||||
#[test]
|
||||
fn rain_fog_pulls_fog_in() {
|
||||
let sky = Sky {
|
||||
fog_end: 1024.0,
|
||||
..Sky::default()
|
||||
};
|
||||
let tables = FogTables {
|
||||
water_fog_end: vec![1.0],
|
||||
};
|
||||
let state = FogState {
|
||||
rain_fog_multiplier: 1.0,
|
||||
..FogState::default()
|
||||
};
|
||||
let fog = setup_fog(&tables, &state, &inputs(&sky));
|
||||
assert_eq!(fog.environmental_start, -160.0);
|
||||
assert_eq!(fog.environmental_end, 768.0);
|
||||
}
|
||||
}
|
||||
@@ -280,6 +280,7 @@ pub struct Resources {
|
||||
providers: Providers,
|
||||
block_entity_render: horizon_client::block_entity_render::BlockEntityRenderer,
|
||||
animate: horizon_client::animate_tick::AnimateTables,
|
||||
fog: horizon_client::fog::FogRenderer,
|
||||
nether_environment: Environment,
|
||||
behaviours: Behaviours,
|
||||
names: Names,
|
||||
@@ -2791,7 +2792,8 @@ impl Game<'_> {
|
||||
let (x1, z1, y0, y1) = (x0 + 12.0, z0 + 12.0, bottom, bottom + 4.0);
|
||||
let mut quad = |p: [[f32; 3]; 4], shade: f32| {
|
||||
for v in p {
|
||||
out.push(Vertex { pos: v, uv, layer: 2, color: color(shade), light });
|
||||
// Overlay 4: clouds (unlit, faded by the cloud fog end).
|
||||
out.push(Vertex { pos: v, uv, layer: 4, color: color(shade), light });
|
||||
}
|
||||
};
|
||||
quad([[x0, y1, z0], [x0, y1, z1], [x1, y1, z1], [x1, y1, z0]], 1.0);
|
||||
@@ -4161,6 +4163,8 @@ impl Game<'_> {
|
||||
// The camera inside an opaque block sees nothing through it: disable.
|
||||
cave_culling: !self.res.facts.get(self.world.block(block[0], block[1], block[2])).solid_render,
|
||||
water_fog,
|
||||
fog: Some(self.frame_fog(camera, &sky, biome)),
|
||||
dark_disc: self.dark_disc(camera),
|
||||
outline,
|
||||
hud: &hud,
|
||||
entities: &entities,
|
||||
@@ -4643,6 +4647,7 @@ pub fn run() -> Result<()> {
|
||||
let world = World::new(facts.air, 0);
|
||||
let creative = options.creative;
|
||||
let animate = horizon_client::animate_tick::AnimateTables::load(&root, registry_owner.registry, &biomes)?;
|
||||
let fog = horizon_client::fog::FogRenderer::load(&root, &biomes);
|
||||
let mut resources = Resources {
|
||||
assets,
|
||||
facts,
|
||||
@@ -4656,6 +4661,7 @@ pub fn run() -> Result<()> {
|
||||
providers: Providers::load(&root),
|
||||
block_entity_render: horizon_client::block_entity_render::BlockEntityRenderer::load(&root, ®istry_owner.registry)?,
|
||||
animate,
|
||||
fog,
|
||||
nether_environment: Environment::load_dimension(&root, "the_nether", false)?,
|
||||
behaviours: Behaviours::load(registry_owner.registry, &root)?,
|
||||
names: Names::load(&root),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Per-tick client visuals: the particle engine, `ClientLevel.animateTick`,
|
||||
//! campfire smoke and rain splashes (`ClientLevel.tickWeatherEffects`).
|
||||
use super::{Dim, Game};
|
||||
use horizon_client::fog::{FogData, FogInputs, FogType, fog_type, update_rain_fog};
|
||||
use horizon_client::{
|
||||
animate_tick::{AnimateContext, animate_tick, campfire_particles},
|
||||
biome::Biomes,
|
||||
@@ -41,8 +42,94 @@ fn rains_at(world: &World, biomes: &Biomes, x: i32, y: i32, z: i32) -> bool {
|
||||
}
|
||||
|
||||
impl Game<'_> {
|
||||
fn forward(&self) -> [f64; 3] {
|
||||
let (yaw, pitch) = (
|
||||
f64::from(self.yaw).to_radians(),
|
||||
f64::from(self.pitch).to_radians(),
|
||||
);
|
||||
[
|
||||
-yaw.sin() * pitch.cos(),
|
||||
-pitch.sin(),
|
||||
yaw.cos() * pitch.cos(),
|
||||
]
|
||||
}
|
||||
/// `FogRenderer.setupFog` for the frame's camera.
|
||||
pub(super) fn frame_fog(
|
||||
&self,
|
||||
camera: glam::DVec3,
|
||||
sky: &horizon_client::environment::Sky,
|
||||
biome: u16,
|
||||
) -> FogData {
|
||||
let forward = self.forward();
|
||||
let at = [camera.x, camera.y, camera.z];
|
||||
let fog_type = fog_type(&self.world, &self.res.facts, at, forward);
|
||||
let water = self.res.biomes.get(biome).water_fog;
|
||||
let (rain, thunder) = if self.dim == Dim::Overworld {
|
||||
self.world.weather.levels(self.clock.fraction() as f32)
|
||||
} else {
|
||||
(0.0, 0.0)
|
||||
};
|
||||
self.res.fog.setup(&FogInputs {
|
||||
fog_type,
|
||||
sky,
|
||||
water_fog_color: [
|
||||
(water >> 16 & 255) as f32 / 255.0,
|
||||
(water >> 8 & 255) as f32 / 255.0,
|
||||
(water & 255) as f32 / 255.0,
|
||||
],
|
||||
biome,
|
||||
render_distance_chunks: self.streaming.radius,
|
||||
camera_y: camera.y,
|
||||
min_y: MIN_Y,
|
||||
flat: false,
|
||||
forward: forward.map(|c| c as f32),
|
||||
rain,
|
||||
thunder,
|
||||
skybox: self.dim == Dim::Overworld,
|
||||
blindness: None,
|
||||
})
|
||||
}
|
||||
/// `SkyRenderer.shouldRenderDarkDisc`: eye below the horizon (y 63) and not under water.
|
||||
pub(super) fn dark_disc(&self, camera: glam::DVec3) -> bool {
|
||||
camera.y < 63.0
|
||||
&& fog_type(
|
||||
&self.world,
|
||||
&self.res.facts,
|
||||
[camera.x, camera.y, camera.z],
|
||||
self.forward(),
|
||||
) != FogType::Water
|
||||
}
|
||||
pub(super) fn tick_particles(&mut self) {
|
||||
let eye = self.eye_position();
|
||||
if let Some(e) = eye {
|
||||
// LocalPlayer water vision and AtmosphericFogEnvironment rain fog.
|
||||
let in_water =
|
||||
fog_type(&self.world, &self.res.facts, e, self.forward()) == FogType::Water;
|
||||
self.res.fog.state.tick(in_water);
|
||||
let b = [
|
||||
e[0].floor() as i32,
|
||||
e[1].floor() as i32,
|
||||
e[2].floor() as i32,
|
||||
];
|
||||
let rain = if self.dim == Dim::Overworld {
|
||||
self.world.weather.levels(1.0).0
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let biome = self
|
||||
.world
|
||||
.chunks
|
||||
.get(&(b[0] >> 4, b[2] >> 4))
|
||||
.map_or(0, |c| c.biome(b[0], b[1], b[2]));
|
||||
let rains = self.res.biomes.get(biome).precipitation;
|
||||
update_rain_fog(
|
||||
&mut self.res.fog.state,
|
||||
rain,
|
||||
self.world.light(b[0], b[1], b[2]) >> 4,
|
||||
rains,
|
||||
1.0,
|
||||
);
|
||||
}
|
||||
if let Some(e) = eye {
|
||||
self.particles.camera = e;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ struct Globals {
|
||||
inverse_view: [[f32; 4]; 4],
|
||||
sunrise: [f32; 4],
|
||||
sun: [f32; 4],
|
||||
/// x: sky fog end, y: cloud fog end, z: dark disc (camera below the horizon), w: unused.
|
||||
fog_ends: [f32; 4],
|
||||
}
|
||||
pub type SectionKey = (i32, i32, i32);
|
||||
struct SectionGpu {
|
||||
@@ -52,6 +54,10 @@ pub struct Frame<'a> {
|
||||
/// Environmental fog start/end (Nether 10..96), if any.
|
||||
pub environment_fog: Option<(f32, f32)>,
|
||||
pub water_fog: [f32; 3],
|
||||
/// `FogRenderer.setupFog` result; replaces the underwater/environment fields when set.
|
||||
pub fog: Option<crate::fog::FogData>,
|
||||
/// Camera eye below the horizon height and not under water (`shouldRenderDarkDisc`).
|
||||
pub dark_disc: bool,
|
||||
pub outline: Option<[f64; 6]>,
|
||||
pub hud: &'a HudBatch,
|
||||
/// Camera-relative entity geometry textured from the shared atlas.
|
||||
@@ -73,6 +79,7 @@ pub struct Renderer {
|
||||
solid: wgpu::RenderPipeline,
|
||||
translucent: wgpu::RenderPipeline,
|
||||
sky: wgpu::RenderPipeline,
|
||||
dark_disc: wgpu::RenderPipeline,
|
||||
lines: wgpu::RenderPipeline,
|
||||
entity_pipeline: wgpu::RenderPipeline,
|
||||
celestial_pipeline: wgpu::RenderPipeline,
|
||||
@@ -356,6 +363,7 @@ impl Renderer {
|
||||
Some(alpha_blend),
|
||||
);
|
||||
let sky = pipeline("Sky", "vs_sky", "fs_sky", &[], tri, None, false, false, None);
|
||||
let dark_disc = pipeline("Dark disc", "vs_sky", "fs_dark_disc", &[], tri, None, false, false, Some(alpha_blend));
|
||||
let lines = pipeline(
|
||||
"Block outline",
|
||||
"vs_line",
|
||||
@@ -668,6 +676,7 @@ impl Renderer {
|
||||
solid,
|
||||
translucent,
|
||||
sky,
|
||||
dark_disc,
|
||||
lines,
|
||||
entity_pipeline,
|
||||
celestial_pipeline,
|
||||
@@ -975,7 +984,9 @@ impl Renderer {
|
||||
let sun_angle = frame.sky.sun_angle.to_radians();
|
||||
// Celestial bodies rotate around the X axis; angle 0 = noon.
|
||||
let sun = Vec3::new(0.0, sun_angle.cos(), -sun_angle.sin());
|
||||
let (fog_color, fog) = if frame.underwater {
|
||||
let (fog_color, fog) = if let Some(f) = frame.fog {
|
||||
(f.color, [f.environmental_start, f.environmental_end, f.render_distance_start, f.render_distance_end])
|
||||
} else if frame.underwater {
|
||||
(
|
||||
[frame.water_fog[0], frame.water_fog[1], frame.water_fog[2], 1.0],
|
||||
[-8.0, 96.0, -8.0, 96.0],
|
||||
@@ -1005,6 +1016,12 @@ impl Renderer {
|
||||
inverse_view: view.inverse().to_cols_array_2d(),
|
||||
sunrise: frame.sky.sunrise_color,
|
||||
sun: [sun.x, sun.y, sun.z, frame.sky.star_brightness],
|
||||
fog_ends: [
|
||||
frame.fog.map_or(frame.distance.min(512.0), |f| f.sky_end),
|
||||
frame.fog.map_or(2048.0, |f| f.cloud_end),
|
||||
if frame.dark_disc { 1.0 } else { 0.0 },
|
||||
0.0,
|
||||
],
|
||||
};
|
||||
self.queue
|
||||
.write_buffer(&self.globals, 0, bytemuck::bytes_of(&globals));
|
||||
@@ -1175,7 +1192,12 @@ impl Renderer {
|
||||
..Default::default()
|
||||
});
|
||||
pass.set_bind_group(0, &self.group, &[]);
|
||||
if !frame.underwater && frame.skybox {
|
||||
// LevelRenderer: no sky inside lava or powder snow (sky fog ends within 2 blocks).
|
||||
let sky_visible = match frame.fog {
|
||||
Some(f) => f.sky_end > 2.0,
|
||||
None => !frame.underwater,
|
||||
};
|
||||
if sky_visible && frame.skybox {
|
||||
if !frame.title {
|
||||
pass.set_pipeline(&self.sky);
|
||||
pass.draw(0..3, 0..1);
|
||||
@@ -1188,6 +1210,10 @@ impl Renderer {
|
||||
pass.draw_indexed(0..(celestial_vertices as u32 / 4) * 6, 0, 0..1);
|
||||
pass.set_bind_group(0, &self.group, &[]);
|
||||
}
|
||||
if !frame.title && frame.dark_disc {
|
||||
pass.set_pipeline(&self.dark_disc);
|
||||
pass.draw(0..3, 0..1);
|
||||
}
|
||||
}
|
||||
if frame.title {
|
||||
visible.clear();
|
||||
|
||||
@@ -17,6 +17,7 @@ pub mod creative;
|
||||
pub mod collisions;
|
||||
pub mod entities;
|
||||
pub mod environment;
|
||||
pub mod fog;
|
||||
pub mod gfx;
|
||||
pub mod health;
|
||||
pub mod hud;
|
||||
|
||||
@@ -12,6 +12,8 @@ struct Globals {
|
||||
sunrise: vec4<f32>,
|
||||
// xyz: sun direction, w: star brightness
|
||||
sun: vec4<f32>,
|
||||
// x: sky fog end, y: cloud fog end, z: dark disc flag
|
||||
fog_ends: vec4<f32>,
|
||||
}
|
||||
@group(0) @binding(0) var<uniform> globals: Globals;
|
||||
@group(0) @binding(1) var sprites: texture_2d_array<f32>;
|
||||
@@ -67,8 +69,12 @@ fn apply_fog(color: vec4<f32>, spherical: f32, cylindrical: f32) -> vec4<f32> {
|
||||
return apply_fog(color, in.spherical, in.cylindrical);
|
||||
}
|
||||
|
||||
// Sky: vanilla draws a sky disc in the sky colour and blends towards the fog
|
||||
// colour at the horizon; sunrise/sunset tints the horizon facing the sun.
|
||||
// Sky (SkyRenderer): the 10-vertex sky disc fan at y = 16 (radius 512) in
|
||||
// the sky colour with sky fog (sky.fsh: fog 0..FogSkyEnd on the spherical
|
||||
// distance, a step at FogSkyEnd on the cylindrical one), evaluated per pixel
|
||||
// along the view ray with the fan's interpolated vertex distances; the
|
||||
// clear colour (fog colour) elsewhere; the sunrise/sunset fan blended over
|
||||
// it; the dark disc (y = -4) below the horizon when the camera is low.
|
||||
struct SkyOut {
|
||||
@builtin(position) clip: vec4<f32>,
|
||||
@location(0) ndc: vec2<f32>,
|
||||
@@ -80,22 +86,87 @@ struct SkyOut {
|
||||
out.ndc = p;
|
||||
return out;
|
||||
}
|
||||
fn view_ray(ndc: vec2<f32>) -> vec3<f32> {
|
||||
return normalize((globals.inverse_view * vec4(ndc.x * globals.misc.z * globals.misc.w,
|
||||
ndc.y * globals.misc.w, -1.0, 0.0)).xyz);
|
||||
}
|
||||
const PI: f32 = 3.14159265;
|
||||
// Interpolated (spherical, cylindrical) distance where the ray meets a disc
|
||||
// fan at height y, or negative when it misses.
|
||||
fn disc_distance(d: vec3<f32>, y: f32) -> vec2<f32> {
|
||||
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 = 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 sky_fog(color: vec3<f32>, dist: vec2<f32>) -> vec3<f32> {
|
||||
let end = globals.fog_ends.x;
|
||||
let f = max(linear_fog(dist.x, 0.0, end), linear_fog(dist.y, end, end));
|
||||
return mix(color, globals.fog_color.rgb, f * globals.fog_color.a);
|
||||
}
|
||||
fn rotate_x90(v: vec3<f32>) -> vec3<f32> { return vec3(v.x, -v.z, v.y); }
|
||||
// Weight of the fan centre where the ray crosses the sunrise fan (0 when missed).
|
||||
fn sunrise_weight(d: vec3<f32>) -> f32 {
|
||||
let alpha = globals.sunrise.a;
|
||||
if alpha <= 0.001 { return 0.0; }
|
||||
// sin(sunAngle) < 0 flips the fan to the other horizon; sun.z = -sin(sunAngle).
|
||||
let flip = select(0.0, 180.0, -globals.sun.z < 0.0);
|
||||
let theta = radians(flip + 90.0);
|
||||
let c = cos(theta);
|
||||
let s = sin(theta);
|
||||
var best = 0.0;
|
||||
let apex = rotate_x90(vec3(-100.0 * s, 100.0 * c, 0.0));
|
||||
for (var i = 0; i < 16; i++) {
|
||||
let a0 = f32(i) * 2.0 * PI / 16.0;
|
||||
let a1 = f32(i + 1) * 2.0 * 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 = rotate_x90(vec3(l0.x * c - l0.y * s, l0.x * s + l0.y * c, l0.z));
|
||||
let r1 = rotate_x90(vec3(l1.x * c - l1.y * s, l1.x * s + l1.y * c, l1.z));
|
||||
// Moller-Trumbore from the origin along d.
|
||||
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;
|
||||
}
|
||||
@fragment fn fs_sky(in: SkyOut) -> @location(0) vec4<f32> {
|
||||
let view = normalize((globals.inverse_view * vec4(in.ndc.x * globals.misc.z * globals.misc.w,
|
||||
in.ndc.y * globals.misc.w, -1.0, 0.0)).xyz);
|
||||
let up = clamp(view.y, -1.0, 1.0);
|
||||
// Fog band towards the horizon (vanilla sky end distance blending).
|
||||
let horizon = 1.0 - smoothstep(0.0, 0.35, up);
|
||||
var color = mix(globals.sky_color.rgb, globals.fog_color.rgb, horizon);
|
||||
let toward_sun = max(dot(normalize(vec3(view.x, 0.0, view.z) + vec3(0.0001)), normalize(vec3(globals.sun.x, 0.0, globals.sun.z) + vec3(0.0001))), 0.0);
|
||||
let band = (1.0 - smoothstep(-0.1, 0.4, abs(up))) * pow(toward_sun, 3.0);
|
||||
color = mix(color, globals.sunrise.rgb, band * globals.sunrise.a);
|
||||
if up < 0.0 {
|
||||
// Void below the horizon.
|
||||
color = mix(color, globals.sky_color.rgb * 0.2, smoothstep(0.0, -0.3, up) * (1.0 - globals.misc.x));
|
||||
let d = view_ray(in.ndc);
|
||||
var color = globals.fog_color.rgb;
|
||||
let dist = disc_distance(d, 16.0);
|
||||
if dist.x >= 0.0 {
|
||||
color = sky_fog(globals.sky_color.rgb, dist);
|
||||
}
|
||||
let w = sunrise_weight(d);
|
||||
if w > 0.0 {
|
||||
color = mix(color, globals.sunrise.rgb, w * globals.sunrise.a);
|
||||
}
|
||||
return vec4(color, 1.0);
|
||||
}
|
||||
@fragment fn fs_dark_disc(in: SkyOut) -> @location(0) vec4<f32> {
|
||||
let d = view_ray(in.ndc);
|
||||
let dist = disc_distance(d, -4.0);
|
||||
if dist.x < 0.0 { discard; }
|
||||
return vec4(sky_fog(vec3(0.0), dist), 1.0);
|
||||
}
|
||||
|
||||
struct LineOut { @builtin(position) clip: vec4<f32> }
|
||||
@vertex fn vs_line(@location(0) position: vec3<f32>) -> LineOut {
|
||||
|
||||
Reference in New Issue
Block a user