Files
horizon-airdef/crates/horizon-client/src/screen_effects.rs
T
Ludovic Marie ef0c6604a5 Compare camera overlays and the sleep overlay with vanilla's Hud
The overlays cases run Hud.extractCameraOverlays and extractSleepOverlay:
the vignette with its brightness and world border warning, the spyglass
scope and its scopeScale easing (read back and compared), equipment
camera overlays, powder snow, the nether portal sprite and the nausea
swirl. The game feeds its full-screen camera overlays from the same
layout (the vignette option and first person now count), draws the
spyglass with vanilla's unclamped easing and the sleep fade at vanilla's
opacity under the sidebar, titles and chat.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 13:19:38 +02:00

260 lines
8.9 KiB
Rust

//! Full-screen camera effects drawn between the world and the HUD:
//! `ScreenEffectRenderer` (in-wall block texture, underwater overlay, fire)
//! and the full-screen `Hud` camera overlays laid out by
//! `hud_overlays::camera_overlays` (vignette, pumpkin blur, powder-snow
//! outline, nether portal swirl). Positions are clip-space quads; textures
//! come from the shared 2D atlas and may repeat.
use crate::{hud::Rect, hud_layout::GuiDraw};
use std::collections::HashMap;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Blend {
/// `GUI_TEXTURED` / screen effects: alpha blending.
Alpha,
/// `RenderPipelines.VIGNETTE`: `dst * (1 - src)`.
Vignette,
}
/// One clip-space vertex of a screen quad.
#[repr(C)]
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct ScreenVertex {
pub pos: [f32; 2],
/// Texture coordinates in sprite units (repeat outside 0..1).
pub uv: [f32; 2],
/// Atlas rectangle (u0, v0, u1, v1) of the sprite.
pub rect: [f32; 4],
pub color: [f32; 4],
}
#[derive(Clone, Debug)]
pub struct ScreenQuad {
pub vertices: [ScreenVertex; 4],
pub blend: Blend,
}
/// What the camera sees this frame.
pub struct ScreenInputs<'a> {
pub sprites: &'a HashMap<String, Rect>,
pub aspect: f32,
pub partial: f32,
/// Particle sprite of the view-blocking block around the eye.
pub block_overlay: Option<String>,
/// Eye in water: (lightmap brightness at the eye, yaw, pitch).
pub water: Option<(f32, f32, f32)>,
pub on_fire: bool,
/// `Hud.extractCameraOverlays` draws: the full-screen blits become
/// screen quads (the spyglass, its fills and the nausea swirl do not).
pub gui_overlays: &'a [GuiDraw],
}
/// Effect state advanced every tick.
#[derive(Clone, Copy, Debug)]
pub struct ScreenEffects {
/// `Hud.vignetteBrightness`.
pub vignette_brightness: f32,
pub portal: f32,
pub o_portal: f32,
}
impl Default for ScreenEffects {
fn default() -> Self {
Self {
vignette_brightness: 1.0,
portal: 0.0,
o_portal: 0.0,
}
}
}
/// `Lightmap.getBrightness`.
pub fn lightmap_brightness(ambient_light: f32, level: u8) -> f32 {
let v = f32::from(level) / 15.0;
let curved = v / (4.0 - 3.0 * v);
curved + (1.0 - curved) * ambient_light
}
/// Clip-space scale of the hand projection (fov 70) at view depth 0.5.
fn hand_scale(aspect: f32) -> [f32; 2] {
let half = 0.5 * (35.0f32).to_radians().tan();
[1.0 / (half * aspect), 1.0 / half]
}
impl ScreenEffects {
/// `Hud.updateVignetteBrightness` and `LocalPlayer.handlePortalTransitionEffect`.
pub fn tick(&mut self, eye_brightness: f32, inside_portal: bool) {
let target = (1.0 - eye_brightness).clamp(0.0, 1.0);
self.vignette_brightness += (target - self.vignette_brightness) * 0.01;
self.o_portal = self.portal;
let step = if inside_portal {
0.0125
} else if self.portal > 0.0 {
-0.05
} else {
0.0
};
self.portal = (self.portal + step).clamp(0.0, 1.0);
}
pub fn build(&self, input: &ScreenInputs) -> Vec<ScreenQuad> {
let mut out = Vec::new();
let sprite = |name: &str| input.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]);
let scale = hand_scale(input.aspect);
// Screen-effect quads live at view depth 0.5 in the hand projection.
let hand_quad = |corners: [[f32; 2]; 4],
uv: [[f32; 2]; 4],
rect: [f32; 4],
color: [f32; 4]| ScreenQuad {
vertices: std::array::from_fn(|i| ScreenVertex {
pos: [corners[i][0] * scale[0], corners[i][1] * scale[1]],
uv: uv[i],
rect,
color,
}),
blend: Blend::Alpha,
};
let full = [[-1.0, -1.0], [1.0, -1.0], [1.0, 1.0], [-1.0, 1.0]];
// ScreenEffectRenderer.submitBlockSprite: u1/v1 at (-1, -1), colour 0xFF191919.
if let Some(name) = &input.block_overlay
&& let Some(rect) = sprite(name)
{
let c = 0x19 as f32 / 255.0;
out.push(hand_quad(
full,
[[1.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0]],
rect,
[c, c, c, 1.0],
));
}
if let Some((brightness, yaw, pitch)) = input.water
&& let Some(rect) = sprite("misc/underwater")
{
let (u, v) = (-yaw / 64.0, pitch / 64.0);
let uv = [[u + 4.0, v + 4.0], [u, v + 4.0], [u, v], [u + 4.0, v]];
out.push(hand_quad(
full,
uv,
rect,
[brightness, brightness, brightness, 0.1],
));
}
if input.on_fire
&& let Some(rect) = sprite("block/fire_1")
{
for (dx, angle) in [(0.24f32, -10.0f32), (-0.24, 10.0)] {
let (s, c) = angle.to_radians().sin_cos();
// rotateY about the quad then translate; z = -0.5 feeds into x.
let corner = |x: f32, y: f32| [x * c + -0.5 * s + dx, y - 0.3];
let corners = [
corner(-0.5, -0.5),
corner(0.5, -0.5),
corner(0.5, 0.5),
corner(-0.5, 0.5),
];
let alpha = 0xE5 as f32 / 255.0;
out.push(hand_quad(
corners,
[[1.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0]],
rect,
[1.0, 1.0, 1.0, alpha],
));
}
}
// Hud camera overlays: GUI-space full-screen blits (v grows downwards).
let gui = |rect: [f32; 4], color: [f32; 4], blend: Blend| ScreenQuad {
vertices: std::array::from_fn(|i| {
let p = [[-1.0f32, 1.0], [1.0, 1.0], [1.0, -1.0], [-1.0, -1.0]][i];
ScreenVertex {
pos: p,
uv: [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]][i],
rect,
color,
}
}),
blend,
};
let rgba = |c: u32| {
let [a, r, g, b] = c.to_be_bytes();
[r, g, b, a].map(|v| f32::from(v) / 255.0)
};
for d in input.gui_overlays {
match d {
GuiDraw::Blit {
texture,
blend,
x: 0,
y: 0,
color,
..
} if *blend != crate::hud_layout::Blend::Nausea
&& texture != "misc/spyglass_scope" =>
{
let blend = if *blend == crate::hud_layout::Blend::Vignette {
Blend::Vignette
} else {
Blend::Alpha
};
if let Some(rect) = sprite(texture) {
out.push(gui(rect, rgba(*color), blend));
}
}
GuiDraw::AtlasSprite {
sprite: name,
color,
..
} => {
if let Some(rect) = sprite(name) {
out.push(gui(rect, rgba(*color), Blend::Alpha));
}
}
_ => {}
}
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lightmap_brightness_curve() {
assert_eq!(lightmap_brightness(0.0, 15), 1.0);
assert_eq!(lightmap_brightness(0.0, 0), 0.0);
assert!((lightmap_brightness(0.1, 0) - 0.1).abs() < 1e-6);
}
#[test]
fn portal_intensity_ramps() {
let mut e = ScreenEffects::default();
for _ in 0..80 {
e.tick(1.0, true);
}
assert!((e.portal - 1.0).abs() < 1e-5);
e.tick(1.0, false);
assert!((e.portal - 0.95).abs() < 1e-5);
// Vignette fades towards 1 - brightness at 1% per tick.
assert!(e.vignette_brightness < 1.0);
}
#[test]
fn overlays_need_their_sprites() {
let mut sprites = HashMap::new();
sprites.insert(
"misc/vignette".to_owned(),
Rect {
u0: 0.0,
v0: 0.0,
u1: 1.0,
v1: 1.0,
w: 256,
h: 256,
},
);
let e = ScreenEffects::default();
let input = ScreenInputs {
sprites: &sprites,
aspect: 1.6,
partial: 0.0,
block_overlay: None,
water: Some((1.0, 0.0, 0.0)),
on_fire: false,
gui_overlays: &[crate::hud_overlays::vignette(320, 240, 1.0, 0.0)],
};
let quads = e.build(&input);
assert_eq!(quads.len(), 1);
assert_eq!(quads[0].blend, Blend::Vignette);
}
}