use std::f64::consts::PI;
use glam::{DMat3, DVec3};
use mln_gfx::color_mode::ColorMode;
use mln_gfx::cull_face_mode::CullFaceMode;
use mln_gfx::gpu::{Gpu, ShaderKey};
use mln_gfx::projection_variant::ProjectionVariant;
use mln_gfx::{AttributeDataType, DepthMaskType, DrawMode};
use mln_style::color::Color;
use mln_style::geo::WrapMode;
use mln_style::types::LightAnchorType;
use crate::camera::View;
use crate::draw::context::Context;
use crate::draw::draws::{Depth, Draw, SingleDraw};
use crate::draw::indexes::DrawIndexes;
use crate::draw::objects::{DrawGeometry, Mesh, Textures, raw_vertices};
use crate::draw::range::DrawRange;
use crate::draw::vertex_attribute::Uniforms;
use crate::render::light::EvaluatedLight;
use crate::render::sky::EvaluatedSky;
use crate::render::static_data::RenderStaticData;
use crate::shaders::programs;
use crate::shaders::ubo::{AtmospherePropsUBO, SkyPropsUBO};
fn fullscreen_draw(
context: &mut Context,
shader: ShaderKey,
vertex_attribute: &'static str,
depth: Depth,
) -> SingleDraw {
let corners: [[i16; 2]; 4] = [[-1, -1], [1, -1], [-1, 1], [1, 1]];
let vertices = bytemuck::cast_slice(&corners).to_vec();
let geometry = DrawGeometry {
vertices: raw_vertices(vertex_attribute, vertices, corners.len(), AttributeDataType::Short2),
vertex_count: corners.len(),
mode: DrawMode::Triangles(),
indexes: DrawIndexes::new(RenderStaticData::quad_triangle_indices()),
ranges: vec![DrawRange::indexes(6)],
instances: None,
};
let mesh =
Mesh::new(context, shader, ColorMode::alpha_blended(), CullFaceMode::disabled(), geometry, Textures::default());
SingleDraw::new(mesh, Draw { depth, ..Draw::DEFAULT })
}
fn sun_position(light: &EvaluatedLight, state: &View, blend: f32) -> [f32; 4] {
let position = light.light_position.uniform().get_cartesian();
let mut sun = -DVec3::new(f64::from(position[0]), f64::from(position[1]), f64::from(position[2]));
if *light.light_anchor.uniform() == LightAnchorType::Viewport {
let center = state.center(WrapMode::Unwrapped);
let view_to_globe = DMat3::from_rotation_y(center.longitude() * PI / 180.0)
* DMat3::from_rotation_x(-center.latitude() * PI / 180.0)
* DMat3::from_rotation_z(state.angle())
* DMat3::from_rotation_x(state.pitch())
* DMat3::from_rotation_z(-state.roll())
* DMat3::from_rotation_z(state.north_orientation_angle());
sun = view_to_globe * sun;
}
sun.as_vec3().extend(blend).to_array()
}
const ATMOSPHERE_HEIGHT: f64 = 100_000.0 / 6_371_000.0;
fn atmosphere_altitude_blend(altitude: f64) -> f64 {
let x = ((altitude - ATMOSPHERE_HEIGHT) / (9.0 * ATMOSPHERE_HEIGHT)).clamp(0.0, 1.0);
x * x * (3.0 - 2.0 * x)
}
fn color_components(color: &Color) -> [f32; 4] {
[color.r, color.g, color.b, color.a]
}
#[derive(Default)]
pub(crate) struct SkyPass {
sky: Option<SingleDraw>,
atmosphere: Option<SingleDraw>,
}
impl SkyPass {
#[expect(
clippy::too_many_arguments,
reason = "the context and camera, the viewport, and the evaluated sky and light that the pass draws"
)]
pub(crate) fn update(
&mut self,
gpu: &mut dyn Gpu,
context: &mut Context,
state: &View,
viewport_size: mln_gfx::Size,
pixel_ratio: f32,
evaluated: Option<&EvaluatedSky>,
light: &EvaluatedLight,
) {
let Some(evaluated) = evaluated.filter(|_| !viewport_size.is_empty()) else {
self.sky = None;
self.atmosphere = None;
return;
};
let projection_transition = state.projection_transition().clamp(0.0, 1.0);
if projection_transition < 1.0 {
let sky = self.sky.get_or_insert_with(|| {
let shader = context.shader_key(&programs::SKY, Uniforms::default(), ProjectionVariant::Mercator);
let depth = Depth::Layer { sub_layer: 0, mask: DepthMaskType::ReadWrite };
fullscreen_draw(context, shader, "pos", depth)
});
let horizon_distance = state.mercator_horizon() * f64::from(pixel_ratio);
let roll = state.roll();
let normal = [(-roll.sin()) as f32, roll.cos() as f32];
let horizon = [
(f64::from(viewport_size.width) * 0.5 + f64::from(normal[0]) * horizon_distance) as f32,
(f64::from(viewport_size.height) * 0.5 + f64::from(normal[1]) * horizon_distance) as f32,
];
let props = SkyPropsUBO {
sky_color: color_components(evaluated.sky_color.uniform()),
horizon_color: color_components(evaluated.sky_horizon_color.uniform()),
horizon,
horizon_normal: normal,
viewport_size: [viewport_size.width as f32, viewport_size.height as f32],
sky_horizon_blend: *evaluated.sky_horizon_blend.uniform() * viewport_size.height as f32 * 0.5,
sky_blend: projection_transition as f32,
};
sky.block(&props).write(gpu, context, ());
} else {
self.sky = None;
}
if !state.is_globe() {
self.atmosphere = None;
return;
}
let camera = state.globe_camera_position();
let atmosphere_blend = *evaluated.sky_atmosphere_blend.uniform()
* (projection_transition * atmosphere_altitude_blend(camera.length() - 1.0)) as f32;
if atmosphere_blend <= 0.0 {
self.atmosphere = None;
return;
}
let atmosphere = self.atmosphere.get_or_insert_with(|| {
let shader = context.shader_key(&programs::ATMOSPHERE, Uniforms::default(), ProjectionVariant::Globe);
fullscreen_draw(context, shader, "pos", Depth::Volume { mask: DepthMaskType::ReadOnly })
});
let props = AtmospherePropsUBO {
inv_view_projection: state.inverse_globe_view_projection_matrix().as_mat4().to_cols_array(),
camera_position: camera.as_vec3().extend(0.0).to_array(),
sun_position: sun_position(light, state, atmosphere_blend),
};
atmosphere.block(&props).write(gpu, context, ());
}
#[must_use]
pub(crate) fn sky(&mut self) -> Option<&mut SingleDraw> {
self.sky.as_mut()
}
#[must_use]
pub(crate) fn atmosphere(&mut self) -> Option<&mut SingleDraw> {
self.atmosphere.as_mut()
}
#[must_use]
pub(crate) fn has_atmosphere(&self) -> bool {
self.atmosphere.is_some()
}
}
#[cfg(test)]
mod tests {
use approx::assert_abs_diff_eq;
use glam::Vec3;
use mln_style::geo::LatLng;
use mln_style::property::evaluated::Evaluated;
use mln_style::types::{Position, ProjectionDefinition};
use super::*;
use crate::camera::{NorthOrientation, TransformState};
fn light(anchor: LightAnchorType, position: [f32; 3]) -> EvaluatedLight {
EvaluatedLight {
light_anchor: Evaluated::Constant(anchor),
light_position: Evaluated::Constant(Position::new(position)),
..EvaluatedLight::default()
}
}
fn camera(latitude: f64, longitude: f64, bearing: f64) -> TransformState {
let mut state = TransformState::default();
state.set_size(mln_style::size::Size::new(512, 512));
state.set_lat_lng_zoom(&LatLng::new(latitude, longitude).unwrap(), 1.0);
state.set_angle(bearing);
state
}
#[test]
fn map_light_stays_over_one_place() {
let light = light(LightAnchorType::Map, [1.5, 90.0, 90.0]);
for state in [camera(0.0, 0.0, 0.0), camera(0.0, 160.0, 0.0), camera(30.0, 160.0, 0.6)] {
assert_abs_diff_eq!(
Vec3::new(1.5, 0.0, 0.0),
Vec3::from_slice(&sun_position(&light, state.view(), 1.0)),
epsilon = 1e-5
);
}
}
#[test]
fn viewport_light_stays_on_screen() {
let light = light(LightAnchorType::Viewport, [1.5, 90.0, 90.0]);
assert_abs_diff_eq!(
Vec3::new(1.5, 0.0, 0.0),
Vec3::from_slice(&sun_position(&light, camera(0.0, 0.0, 0.0).view(), 1.0)),
epsilon = 1e-5
);
assert_abs_diff_eq!(
Vec3::new(0.0, 0.0, -1.5),
Vec3::from_slice(&sun_position(&light, camera(0.0, 90.0, 0.0).view(), 1.0)),
epsilon = 1e-5
);
}
#[test]
fn the_atmosphere_fades_out_as_the_camera_enters_it() {
let blend = |heights: f64| atmosphere_altitude_blend(heights * ATMOSPHERE_HEIGHT);
assert_eq!(0.0, blend(0.5));
assert_eq!(0.0, blend(1.0));
assert!((blend(5.5) - 0.5).abs() < 1e-12, "{}", blend(5.5));
assert_eq!(1.0, blend(10.0));
assert_eq!(1.0, blend(100.0));
}
#[test]
fn the_atmosphere_fades_with_the_zoom() {
let altitude = |zoom: f64| {
let mut state = camera(0.0, 160.0, 0.0);
state.set_projection_definition(&ProjectionDefinition::from_name("vertical-perspective"));
state.set_lat_lng_zoom(&LatLng::new(0.0, 160.0).unwrap(), zoom);
state.view().globe_camera_position().length() - 1.0
};
assert_eq!(1.0, atmosphere_altitude_blend(altitude(1.0)));
assert!(atmosphere_altitude_blend(altitude(8.0)) < 0.1, "{}", atmosphere_altitude_blend(altitude(8.0)));
}
#[test]
fn viewport_light_turns_with_the_north_orientation() {
let light = light(LightAnchorType::Viewport, [1.5, 90.0, 90.0]);
let mut state = camera(0.0, 0.0, 0.0);
state.set_north_orientation(NorthOrientation::Downwards);
assert_abs_diff_eq!(
Vec3::new(-1.5, 0.0, 0.0),
Vec3::from_slice(&sun_position(&light, state.view(), 1.0)),
epsilon = 1e-5
);
}
}