veloren_voxygen/scene/
mod.rs

1pub mod camera;
2pub mod debug;
3pub mod figure;
4pub mod lod;
5pub mod math;
6pub mod particle;
7pub mod simple;
8pub mod smoke_cycle;
9pub mod terrain;
10pub mod tether;
11pub mod trail;
12
13use std::collections::HashSet;
14
15pub use self::{
16    camera::{Camera, CameraMode},
17    debug::{Debug, DebugShape, DebugShapeId},
18    figure::FigureMgr,
19    lod::Lod,
20    particle::ParticleMgr,
21    terrain::{SpriteRenderContextLazy, Terrain},
22    tether::TetherMgr,
23    trail::TrailMgr,
24};
25use crate::{
26    audio::{
27        AudioFrontend,
28        ambience::{self, AmbienceMgr},
29        music::MusicMgr,
30        sfx::SfxMgr,
31    },
32    render::{
33        CloudsLocals, Consts, CullingMode, Drawer, GlobalModel, Globals, GlobalsBindGroup, Light,
34        Model, PointLightMatrix, PostProcessLocals, RainOcclusionLocals, Renderer, Shadow,
35        ShadowLocals, SkyboxVertex, create_skybox_mesh,
36    },
37    session::PlayerDebugLines,
38    settings::Settings,
39    window::{AnalogGameInput, Event},
40};
41use client::Client;
42use common::{
43    calendar::Calendar,
44    comp::{
45        self, item::ItemDesc, ship::figuredata::VOXEL_COLLIDER_MANIFEST, slot::EquipSlot,
46        tool::ToolKind,
47    },
48    outcome::Outcome,
49    resources::{DeltaTime, TimeOfDay, TimeScale},
50    terrain::{BlockKind, TerrainChunk, TerrainGrid},
51    vol::ReadVol,
52    weather::WeatherGrid,
53};
54use common_base::{prof_span, span};
55use common_state::State;
56use comp::item::Reagent;
57use hashbrown::HashMap;
58use num::traits::{Float, FloatConst};
59use specs::{Entity as EcsEntity, Join, LendJoin, WorldExt};
60use vek::*;
61
62const ZOOM_CAP_PLAYER: f32 = 1000.0;
63const ZOOM_CAP_ADMIN: f32 = 100000.0;
64
65// TODO: Don't hard-code this.
66const CURSOR_PAN_SCALE: f32 = 0.005;
67
68pub(crate) const MAX_LIGHT_COUNT: usize = 20; // 31 (total shadow_mats is limited to 128 with default
69// max_uniform_buffer_binding_size)
70pub(crate) const MAX_SHADOW_COUNT: usize = 24;
71pub(crate) const MAX_POINT_LIGHT_MATRICES_COUNT: usize = MAX_LIGHT_COUNT * 6 + 6;
72const NUM_DIRECTED_LIGHTS: usize = 1;
73const LIGHT_DIST_RADIUS: f32 = 64.0; // The distance beyond which lights may not emit light from their origin
74const SHADOW_DIST_RADIUS: f32 = 8.0;
75const SHADOW_MAX_DIST: f32 = 96.0; // The distance beyond which shadows may not be visible
76/// The minimum sin γ we will use before switching to uniform mapping.
77const EPSILON_UPSILON: f64 = -1.0;
78
79const SHADOW_NEAR: f32 = 0.25; // Near plane for shadow map point light rendering.
80const SHADOW_FAR: f32 = 128.0; // Far plane for shadow map point light rendering.
81
82/// Above this speed is considered running
83/// Used for first person camera effects
84const RUNNING_THRESHOLD: f32 = 0.7;
85
86/// The threashold for starting calculations with rain.
87const RAIN_THRESHOLD: f32 = 0.0;
88
89/// is_daylight, array of active lights.
90pub type LightData<'a> = (bool, &'a [Light]);
91
92struct EventLight {
93    light: Light,
94    timeout: f32,
95    fadeout: fn(f32) -> f32,
96}
97
98struct Skybox {
99    model: Model<SkyboxVertex>,
100}
101
102pub struct Scene {
103    data: GlobalModel,
104    globals_bind_group: GlobalsBindGroup,
105    camera: Camera,
106    camera_input_state: Vec2<f32>,
107    event_lights: Vec<EventLight>,
108
109    skybox: Skybox,
110    terrain: Terrain<TerrainChunk>,
111    pub debug: Debug,
112    pub lod: Lod,
113    loaded_distance: f32,
114    /// x coordinate is sea level (minimum height for any land chunk), and y
115    /// coordinate is the maximum height above the mnimimum for any land
116    /// chunk.
117    map_bounds: Vec2<f32>,
118    select_pos: Option<Vec3<i32>>,
119    light_data: Vec<Light>,
120
121    particle_mgr: ParticleMgr,
122    trail_mgr: TrailMgr,
123    figure_mgr: FigureMgr,
124    tether_mgr: TetherMgr,
125    pub sfx_mgr: SfxMgr,
126    pub music_mgr: MusicMgr,
127    ambience_mgr: AmbienceMgr,
128
129    integrated_rain_vel: f32,
130    wind_vel: Vec2<f32>,
131    pub interpolated_time_of_day: Option<f64>,
132    last_lightning: Option<(Vec3<f32>, f64)>,
133    local_time: f64,
134
135    pub debug_vectors_enabled: bool,
136}
137
138pub struct SceneData<'a> {
139    pub client: &'a Client,
140    pub state: &'a State,
141    pub viewpoint_entity: specs::Entity,
142    pub mutable_viewpoint: bool,
143    pub target_entities: &'a HashSet<specs::Entity>,
144    pub loaded_distance: f32,
145    pub terrain_view_distance: u32, // not used currently
146    pub entity_view_distance: u32,
147    pub tick: u64,
148    pub gamma: f32,
149    pub exposure: f32,
150    pub ambiance: f32,
151    pub mouse_smoothing: bool,
152    pub sprite_render_distance: f32,
153    pub particles_enabled: bool,
154    pub weapon_trails_enabled: bool,
155    pub flashing_lights_enabled: bool,
156    pub figure_lod_render_distance: f32,
157    pub is_aiming: bool,
158    pub interpolated_time_of_day: Option<f64>,
159}
160
161impl SceneData<'_> {
162    pub fn get_sun_dir(&self) -> Vec3<f32> {
163        TimeOfDay::new(self.interpolated_time_of_day.unwrap_or(0.0)).get_sun_dir()
164    }
165
166    pub fn get_moon_dir(&self) -> Vec3<f32> {
167        TimeOfDay::new(self.interpolated_time_of_day.unwrap_or(0.0)).get_moon_dir()
168    }
169}
170
171/// Approximate a scalar field of view angle using the parameterization from
172/// section 4.3 of Lloyd's thesis:
173///
174/// W_e = 2 n_e tan θ
175///
176/// where
177///
178/// W_e = 2 is the width of the image plane (for our projections, since they go
179/// from -1 to 1) n_e = near_plane is the near plane for the view frustum
180/// θ = (fov / 2) is the half-angle of the FOV (the one passed to
181/// Mat4::projection_rh_zo).
182///
183/// Although the widths for the x and y image planes are the same, they are
184/// different in this framework due to the introduction of an aspect ratio:
185///
186/// y'(p) = 1.0 / tan(fov / 2) * p.y / -p.z
187/// x'(p) = 1.0 / (aspect * tan(fov / 2)) * p.x / -p.z
188///
189/// i.e.
190///
191/// y'(x, y, -near, w) = 1 / tan(fov / 2) p.y / near
192/// x'(x, y, -near, w) = 1 / (aspect * tan(fov / 2)) p.x / near
193///
194/// W_e,y = 2 * near_plane * tan(fov / 2)
195/// W_e,x = 2 * near_plane * aspect * W_e,y
196///
197/// Θ_x = atan(W_e_y / 2 / near_plane) = atanfov / t()
198///
199/// i.e. we have an "effective" W_e_x of
200///
201/// 2 = 2 * near_plane * tan Θ
202///
203/// atan(1 / near_plane) = θ
204///
205/// y'
206/// x(-near)
207/// W_e = 2 * near_plane *
208///
209/// W_e_y / n_e = tan (fov / 2)
210/// W_e_x = 2 n
211fn compute_scalar_fov<F: Float>(_near_plane: F, fov: F, aspect: F) -> F {
212    let two = F::one() + F::one();
213    let theta_y = fov / two;
214    let theta_x = (aspect * theta_y.tan()).atan();
215    theta_x.min(theta_y)
216}
217
218/// Compute a near-optimal warping parameter that helps minimize error in a
219/// shadow map.
220///
221/// See section 5.2 of Brandon Lloyd's thesis:
222///
223/// [http://gamma.cs.unc.edu/papers/documents/dissertations/lloyd07.pdf](Logarithmic Perspective Shadow Maps).
224///
225/// η =
226///     0                                                         γ < γ_a
227///     -1 + (η_b + 1)(1 + cos(90 (γ - γ_a)/(γ_b - γ_a)))   γ_a ≤ γ < γ_b
228///     η_b + (η_c - η_b)  sin(90 (γ - γ_b)/(γ_c - γ_b))    γ_b ≤ γ < γ_c
229///     η_c                                                 γ_c ≤ γ
230///
231/// NOTE: Equation's described behavior is *wrong!*  I have pieced together a
232/// slightly different function that seems to more closely satisfy the author's
233/// intent:
234///
235/// η =
236///     -1                                                        γ < γ_a
237///     -1 + (η_b + 1)            (γ - γ_a)/(γ_b - γ_a)     γ_a ≤ γ < γ_b
238///     η_b + (η_c - η_b)  sin(90 (γ - γ_b)/(γ_c - γ_b))    γ_b ≤ γ < γ_c
239///     η_c                                                 γ_c ≤ γ
240///
241/// There are other alternatives that may have more desirable properties, such
242/// as:
243///
244/// η =
245///     -1                                                        γ < γ_a
246///     -1 + (η_b + 1)(1 - cos(90 (γ - γ_a)/(γ_b - γ_a)))   γ_a ≤ γ < γ_b
247///     η_b + (η_c - η_b)  sin(90 (γ - γ_b)/(γ_c - γ_b))    γ_b ≤ γ < γ_c
248///     η_c                                                 γ_c ≤ γ
249fn compute_warping_parameter<F: Float + FloatConst>(
250    gamma: F,
251    (gamma_a, gamma_b, gamma_c): (F, F, F),
252    (eta_b, eta_c): (F, F),
253) -> F {
254    if gamma < gamma_a {
255        -F::one()
256        /* F::zero() */
257    } else if gamma_a <= gamma && gamma < gamma_b {
258        /* -F::one() + (eta_b + F::one()) * (F::one() + (F::FRAC_PI_2() * (gamma - gamma_a) / (gamma_b - gamma_a)).cos()) */
259        -F::one() + (eta_b + F::one()) * (F::one() - (F::FRAC_PI_2() * (gamma - gamma_a) / (gamma_b - gamma_a)).cos())
260        // -F::one() + (eta_b + F::one()) * ((gamma - gamma_a) / (gamma_b - gamma_a))
261    } else if gamma_b <= gamma && gamma < gamma_c {
262        eta_b + (eta_c - eta_b) * (F::FRAC_PI_2() * (gamma - gamma_b) / (gamma_c - gamma_b)).sin()
263    } else {
264        eta_c
265    }
266    // NOTE: Just in case we go out of range due to floating point imprecision.
267    .max(-F::one()).min(F::one())
268}
269
270/// Compute a near-optimal warping parameter that falls off quickly enough
271/// when the warp angle goes past the minimum field of view angle, for
272/// perspective projections.
273///
274/// For F_p (perspective warping) and view fov angle θ,the parameters are:
275///
276/// γ_a = θ / 3
277/// γ_b = θ
278/// γ_c = θ + 0.3(90 - θ)
279///
280/// η_b = -0.2
281/// η_c = 0
282///
283/// See compute_warping_parameter.
284fn compute_warping_parameter_perspective<F: Float + FloatConst>(
285    gamma: F,
286    near_plane: F,
287    fov: F,
288    aspect: F,
289) -> F {
290    let theta = compute_scalar_fov(near_plane, fov, aspect);
291    let two = F::one() + F::one();
292    let three = two + F::one();
293    let ten = three + three + three + F::one();
294    compute_warping_parameter(
295        gamma,
296        (
297            theta / three,
298            theta,
299            theta + (three / ten) * (F::FRAC_PI_2() - theta),
300        ),
301        (-two / ten, F::zero()),
302    )
303}
304
305impl Scene {
306    /// Create a new `Scene` with default parameters.
307    pub fn new(
308        renderer: &mut Renderer,
309        lazy_init: &mut SpriteRenderContextLazy,
310        client: &Client,
311        settings: &Settings,
312    ) -> Self {
313        let resolution = renderer.resolution().map(|e| e as f32);
314        let sprite_render_context = lazy_init(renderer);
315
316        let data = GlobalModel {
317            globals: renderer.create_consts(&[Globals::default()]),
318            lights: renderer.create_consts(&[Light::default(); MAX_LIGHT_COUNT]),
319            shadows: renderer.create_consts(&[Shadow::default(); MAX_SHADOW_COUNT]),
320            shadow_mats: renderer.create_shadow_bound_locals(&[ShadowLocals::default()]),
321            rain_occlusion_mats: renderer
322                .create_rain_occlusion_bound_locals(&[RainOcclusionLocals::default()]),
323            point_light_matrices: Box::new(
324                [PointLightMatrix::default(); MAX_POINT_LIGHT_MATRICES_COUNT],
325            ),
326        };
327
328        let lod = Lod::new(renderer, client, settings);
329
330        let globals_bind_group = renderer.bind_globals(&data, lod.get_data());
331
332        let terrain = Terrain::new(renderer, &data, lod.get_data(), sprite_render_context);
333
334        let camera_mode = match client.presence() {
335            Some(comp::PresenceKind::Spectator) => CameraMode::Freefly,
336            _ => CameraMode::ThirdPerson,
337        };
338
339        let calendar = client.state().ecs().read_resource::<Calendar>();
340
341        Self {
342            data,
343            globals_bind_group,
344            camera: Camera::new(resolution.x / resolution.y, camera_mode),
345            camera_input_state: Vec2::zero(),
346            event_lights: Vec::new(),
347
348            skybox: Skybox {
349                model: renderer.create_model(&create_skybox_mesh()).unwrap(),
350            },
351            terrain,
352            debug: Debug::new(),
353            lod,
354            loaded_distance: 0.0,
355            map_bounds: Vec2::new(
356                client.world_data().min_chunk_alt(),
357                client.world_data().max_chunk_alt(),
358            ),
359            select_pos: None,
360            light_data: Vec::new(),
361            particle_mgr: ParticleMgr::new(renderer),
362            trail_mgr: TrailMgr::default(),
363            figure_mgr: FigureMgr::new(renderer),
364            tether_mgr: TetherMgr::new(renderer),
365            sfx_mgr: SfxMgr::default(),
366            music_mgr: MusicMgr::new(&calendar),
367            ambience_mgr: AmbienceMgr {
368                ambience: ambience::load_ambience_items(),
369            },
370            integrated_rain_vel: 0.0,
371            wind_vel: Vec2::zero(),
372            interpolated_time_of_day: None,
373            last_lightning: None,
374            local_time: 0.0,
375            debug_vectors_enabled: false,
376        }
377    }
378
379    /// Get a reference to the scene's globals.
380    pub fn globals(&self) -> &Consts<Globals> { &self.data.globals }
381
382    /// Get a reference to the scene's camera.
383    pub fn camera(&self) -> &Camera { &self.camera }
384
385    /// Get a reference to the scene's terrain.
386    pub fn terrain(&self) -> &Terrain<TerrainChunk> { &self.terrain }
387
388    /// Get a reference to the scene's lights.
389    pub fn lights(&self) -> &Vec<Light> { &self.light_data }
390
391    /// Get a reference to the scene's particle manager.
392    pub fn particle_mgr(&self) -> &ParticleMgr { &self.particle_mgr }
393
394    /// Get a reference to the scene's trail manager.
395    pub fn trail_mgr(&self) -> &TrailMgr { &self.trail_mgr }
396
397    /// Get a reference to the scene's figure manager.
398    pub fn figure_mgr(&self) -> &FigureMgr { &self.figure_mgr }
399
400    pub fn music_mgr(&self) -> &MusicMgr { &self.music_mgr }
401
402    /// Get a mutable reference to the scene's camera.
403    pub fn camera_mut(&mut self) -> &mut Camera { &mut self.camera }
404
405    /// Set the block position that the player is interacting with
406    pub fn set_select_pos(&mut self, pos: Option<Vec3<i32>>) { self.select_pos = pos; }
407
408    pub fn select_pos(&self) -> Option<Vec3<i32>> { self.select_pos }
409
410    /// Handle an incoming user input event (e.g.: cursor moved, key pressed,
411    /// window closed).
412    ///
413    /// If the event is handled, return true.
414    pub fn handle_input_event(&mut self, event: Event, client: &Client) -> bool {
415        match event {
416            // When the window is resized, change the camera's aspect ratio
417            Event::Resize(dims) => {
418                self.camera.set_aspect_ratio(dims.x as f32 / dims.y as f32);
419                true
420            },
421            // Panning the cursor makes the camera rotate
422            Event::CursorPan(delta) => {
423                self.camera.rotate_by(Vec3::from(delta) * CURSOR_PAN_SCALE);
424                true
425            },
426            // Zoom the camera when a zoom event occurs
427            Event::Zoom(delta) => {
428                let cap = if client.is_moderator() {
429                    ZOOM_CAP_ADMIN
430                } else {
431                    ZOOM_CAP_PLAYER
432                };
433                // when zooming in the distance the camera travelles should be based on the
434                // final distance. This is to make sure the camera travelles the
435                // same distance when zooming in and out
436                let player_scale = client
437                    .state()
438                    .read_component_copied::<comp::Scale>(client.entity())
439                    .map_or(1.0, |s| s.0);
440                if delta < 0.0 {
441                    self.camera.zoom_switch(
442                        // Thank you Imbris for doing the math
443                        delta * (0.05 + self.camera.get_distance() * 0.01) / (1.0 - delta * 0.01),
444                        cap,
445                        player_scale,
446                    );
447                } else {
448                    self.camera.zoom_switch(
449                        delta * (0.05 + self.camera.get_distance() * 0.01),
450                        cap,
451                        player_scale,
452                    );
453                }
454                true
455            },
456            Event::AnalogGameInput(input) => match input {
457                AnalogGameInput::CameraX(d) => {
458                    self.camera_input_state.x = d;
459                    true
460                },
461                AnalogGameInput::CameraY(d) => {
462                    self.camera_input_state.y = d;
463                    true
464                },
465                _ => false,
466            },
467            // All other events are unhandled
468            _ => false,
469        }
470    }
471
472    pub fn handle_outcome(
473        &mut self,
474        outcome: &Outcome,
475        scene_data: &SceneData,
476        audio: &mut AudioFrontend,
477    ) {
478        span!(_guard, "handle_outcome", "Scene::handle_outcome");
479        self.particle_mgr
480            .handle_outcome(outcome, scene_data, &self.figure_mgr);
481        self.sfx_mgr
482            .handle_outcome(outcome, audio, scene_data.client);
483
484        match outcome {
485            Outcome::Lightning { pos } => {
486                self.last_lightning = Some((*pos, scene_data.state.get_time()));
487            },
488            Outcome::Explosion {
489                pos,
490                power,
491                is_attack,
492                reagent,
493                ..
494            } => self.event_lights.push(EventLight {
495                light: Light::new(
496                    *pos,
497                    match reagent {
498                        Some(Reagent::Blue) => Rgb::new(0.15, 0.4, 1.0),
499                        Some(Reagent::Green) => Rgb::new(0.0, 1.0, 0.0),
500                        Some(Reagent::Purple) => Rgb::new(0.7, 0.0, 1.0),
501                        Some(Reagent::Red) => {
502                            if *is_attack {
503                                Rgb::new(1.0, 0.5, 0.0)
504                            } else {
505                                Rgb::new(1.0, 0.0, 0.0)
506                            }
507                        },
508                        Some(Reagent::Phoenix) => Rgb::new(1.0, 0.8, 0.3),
509                        Some(Reagent::White) => Rgb::new(1.0, 1.0, 1.0),
510                        Some(Reagent::Yellow) => Rgb::new(1.0, 1.0, 0.0),
511                        None => Rgb::new(1.0, 0.5, 0.0),
512                    },
513                    power
514                        * if *is_attack || reagent.is_none() {
515                            2.5
516                        } else {
517                            5.0
518                        },
519                ),
520                timeout: match reagent {
521                    Some(_) => 1.0,
522                    None => 0.5,
523                },
524                fadeout: |timeout| timeout * 2.0,
525            }),
526            Outcome::ProjectileShot { .. } => {},
527            _ => {},
528        }
529    }
530
531    /// Maintain data such as GPU constant buffers, models, etc. To be called
532    /// once per tick.
533    pub fn maintain(
534        &mut self,
535        renderer: &mut Renderer,
536        audio: &mut AudioFrontend,
537        scene_data: &SceneData,
538        client: &Client,
539        settings: &Settings,
540    ) {
541        span!(_guard, "maintain", "Scene::maintain");
542        // Get player position.
543        let ecs = scene_data.state.ecs();
544
545        let dt = ecs.fetch::<DeltaTime>().0;
546
547        self.local_time += dt as f64 * ecs.fetch::<TimeScale>().0;
548
549        let positions = ecs.read_storage::<comp::Pos>();
550
551        let viewpoint_pos = if let Some(viewpoint_pos) =
552            positions.get(scene_data.viewpoint_entity).map(|pos| pos.0)
553        {
554            let viewpoint_ori = ecs
555                .read_storage::<comp::Ori>()
556                .get(scene_data.viewpoint_entity)
557                .map_or(Quaternion::identity(), |ori| ori.to_quat());
558
559            let viewpoint_look_ori = ecs
560                .read_storage::<comp::CharacterActivity>()
561                .get(scene_data.viewpoint_entity)
562                .and_then(|activity| activity.look_dir)
563                .map(|dir| {
564                    let d = dir.to_vec();
565
566                    let pitch = (-d.z).asin();
567                    let yaw = d.x.atan2(d.y);
568
569                    Vec3::new(yaw, pitch, 0.0)
570                })
571                .unwrap_or_else(|| {
572                    let q = viewpoint_ori;
573                    let sinr_cosp = 2.0 * (q.w * q.x + q.y * q.z);
574                    let cosr_cosp = 1.0 - 2.0 * (q.x * q.x + q.y * q.y);
575                    let pitch = sinr_cosp.atan2(cosr_cosp);
576
577                    let siny_cosp = 2.0 * (q.w * q.z + q.x * q.y);
578                    let cosy_cosp = 1.0 - 2.0 * (q.y * q.y + q.z * q.z);
579                    let yaw = siny_cosp.atan2(cosy_cosp);
580
581                    Vec3::new(-yaw, -pitch, 0.0)
582                });
583
584            let viewpoint_scale = ecs
585                .read_storage::<comp::Scale>()
586                .get(scene_data.viewpoint_entity)
587                .map_or(1.0, |scale| scale.0);
588
589            let (is_humanoid, viewpoint_height, viewpoint_eye_height) = ecs
590                .read_storage::<comp::Body>()
591                .get(scene_data.viewpoint_entity)
592                .map_or((false, 1.0, 0.0), |b| {
593                    (
594                        matches!(b, comp::Body::Humanoid(_)),
595                        b.height(),
596                        b.eye_height(1.0), // Scale is applied later
597                    )
598                });
599
600            if scene_data.mutable_viewpoint || matches!(self.camera.get_mode(), CameraMode::Freefly)
601            {
602                // Add the analog input to camera if it's a mutable viewpoint
603                self.camera
604                    .rotate_by(Vec3::from([self.camera_input_state.x, 0.0, 0.0]));
605                self.camera
606                    .rotate_by(Vec3::from([0.0, self.camera_input_state.y, 0.0]));
607            } else {
608                // Otherwise set the cameras rotation to the viewpoints
609                self.camera.set_orientation(viewpoint_look_ori);
610            }
611
612            let viewpoint_offset = if is_humanoid {
613                let viewpoint_rolling = ecs
614                    .read_storage::<comp::CharacterState>()
615                    .get(scene_data.viewpoint_entity)
616                    .is_some_and(|cs| cs.is_dodge());
617
618                let is_running = ecs
619                    .read_storage::<comp::Vel>()
620                    .get(scene_data.viewpoint_entity)
621                    .zip(
622                        ecs.read_storage::<comp::PhysicsState>()
623                            .get(scene_data.viewpoint_entity),
624                    )
625                    .map(|(v, ps)| {
626                        (v.0 - ps.ground_vel).magnitude_squared() > RUNNING_THRESHOLD.powi(2)
627                    })
628                    .unwrap_or(false);
629
630                let on_ground = ecs
631                    .read_storage::<comp::PhysicsState>()
632                    .get(scene_data.viewpoint_entity)
633                    .map(|p| p.on_ground.is_some());
634
635                let player_entity = client.entity();
636                let holding_ranged = client
637                    .inventories()
638                    .get(player_entity)
639                    .and_then(|inv| inv.equipped(EquipSlot::ActiveMainhand))
640                    .and_then(|item| item.tool_info())
641                    .is_some_and(|tool_kind| {
642                        matches!(
643                            tool_kind,
644                            ToolKind::Bow | ToolKind::Staff | ToolKind::Sceptre
645                        )
646                    });
647
648                let up = match self.camera.get_mode() {
649                    CameraMode::FirstPerson => {
650                        if viewpoint_rolling {
651                            viewpoint_height * 0.42
652                        } else if is_running && on_ground.unwrap_or(false) {
653                            viewpoint_eye_height
654                                + (scene_data.state.get_time() as f32 * 17.0).sin() * 0.05
655                        } else {
656                            viewpoint_eye_height
657                        }
658                    },
659                    CameraMode::ThirdPerson if scene_data.is_aiming && holding_ranged => {
660                        viewpoint_height * 1.16 + settings.gameplay.aim_offset_y
661                    },
662                    CameraMode::ThirdPerson if scene_data.is_aiming => viewpoint_height * 1.16,
663                    CameraMode::ThirdPerson => viewpoint_eye_height,
664                    CameraMode::Freefly => 0.0,
665                };
666
667                let right = match self.camera.get_mode() {
668                    CameraMode::FirstPerson => 0.0,
669                    CameraMode::ThirdPerson if scene_data.is_aiming && holding_ranged => {
670                        settings.gameplay.aim_offset_x
671                    },
672                    CameraMode::ThirdPerson => 0.0,
673                    CameraMode::Freefly => 0.0,
674                };
675
676                // Alter camera position to match player.
677                let tilt = self.camera.get_orientation().y;
678                let dist = self.camera.get_distance();
679
680                Vec3::unit_z() * (up * viewpoint_scale - tilt.min(0.0).sin() * dist * 0.6)
681                    + self.camera.right() * (right * viewpoint_scale)
682            } else {
683                self.figure_mgr
684                    .viewpoint_offset(scene_data, scene_data.viewpoint_entity)
685            };
686
687            match self.camera.get_mode() {
688                CameraMode::FirstPerson | CameraMode::ThirdPerson => {
689                    self.camera.set_focus_pos(viewpoint_pos + viewpoint_offset);
690                },
691                CameraMode::Freefly => {},
692            };
693
694            // Tick camera for interpolation.
695            self.camera
696                .update(scene_data.state.get_time(), dt, scene_data.mouse_smoothing);
697            viewpoint_pos
698        } else {
699            Vec3::zero()
700        };
701
702        // Compute camera matrices.
703        self.camera.compute_dependents(&scene_data.state.terrain());
704        let camera::Dependents {
705            view_mat,
706            view_mat_inv,
707            proj_mat,
708            proj_mat_inv,
709            cam_pos,
710            ..
711        } = self.camera.dependents();
712
713        // Update chunk loaded distance smoothly for nice shader fog
714        let loaded_distance =
715            (0.98 * self.loaded_distance + 0.02 * scene_data.loaded_distance).max(0.01);
716
717        // Reset lights ready for the next tick
718        let lights = &mut self.light_data;
719        lights.clear();
720
721        // Maintain the particles.
722        self.particle_mgr.maintain(
723            renderer,
724            scene_data,
725            &self.terrain,
726            &self.figure_mgr,
727            lights,
728        );
729
730        // Maintain the trails.
731        self.trail_mgr.maintain(renderer, scene_data);
732
733        // Update light constants
734        let max_light_dist = loaded_distance.powi(2) + LIGHT_DIST_RADIUS;
735        lights.extend(
736            (
737                &scene_data.state.ecs().read_storage::<comp::Pos>(),
738                scene_data
739                    .state
740                    .ecs()
741                    .read_storage::<crate::ecs::comp::Interpolated>()
742                    .maybe(),
743                &scene_data
744                    .state
745                    .ecs()
746                    .read_storage::<comp::LightAnimation>(),
747                scene_data
748                    .state
749                    .ecs()
750                    .read_storage::<comp::Health>()
751                    .maybe(),
752            )
753                .join()
754                .filter(|(pos, _, light_anim, h)| {
755                    light_anim.col != Rgb::zero()
756                        && light_anim.strength > 0.0
757                        && pos.0.distance_squared(viewpoint_pos) < max_light_dist
758                        && h.is_none_or(|h| !h.is_dead)
759                })
760                .map(|(pos, interpolated, light_anim, _)| {
761                    // Use interpolated values if they are available
762                    let pos = interpolated.map_or(pos.0, |i| i.pos);
763                    Light::new(pos + light_anim.offset, light_anim.col, light_anim.strength)
764                })
765                .chain(
766                    self.event_lights
767                        .iter()
768                        .map(|el| el.light.with_strength((el.fadeout)(el.timeout))),
769                ),
770        );
771        let voxel_colliders_manifest = VOXEL_COLLIDER_MANIFEST.read();
772        let figure_mgr = &self.figure_mgr;
773        lights.extend(
774            (
775                &scene_data.state.ecs().entities(),
776                &scene_data
777                    .state
778                    .read_storage::<crate::ecs::comp::Interpolated>(),
779                &scene_data.state.read_storage::<comp::Body>(),
780                &scene_data.state.read_storage::<comp::Collider>(),
781            )
782                .join()
783                .filter_map(|(entity, interpolated, body, collider)| {
784                    let vol = collider.get_vol(&voxel_colliders_manifest)?;
785                    let (blocks_of_interest, offset) =
786                        figure_mgr.get_blocks_of_interest(entity, body, Some(collider))?;
787
788                    let mat = Mat4::from(interpolated.ori.to_quat())
789                        .translated_3d(interpolated.pos)
790                        * Mat4::translation_3d(offset);
791
792                    let p = mat.inverted().mul_point(viewpoint_pos);
793                    let aabb = Aabb {
794                        min: Vec3::zero(),
795                        max: vol.volume().sz.as_(),
796                    };
797                    if aabb.contains_point(p) || aabb.distance_to_point(p) < max_light_dist {
798                        Some(
799                            blocks_of_interest
800                                .lights
801                                .iter()
802                                .map(move |(block_offset, level)| {
803                                    let wpos = mat.mul_point(block_offset.as_() + 0.5);
804                                    (wpos, level)
805                                })
806                                .filter(move |(wpos, _)| {
807                                    wpos.distance_squared(viewpoint_pos) < max_light_dist
808                                })
809                                .map(|(wpos, level)| {
810                                    Light::new(wpos, Rgb::white(), *level as f32 / 7.0)
811                                }),
812                        )
813                    } else {
814                        None
815                    }
816                })
817                .flatten(),
818        );
819        lights.sort_by_key(|light| light.get_pos().distance_squared(viewpoint_pos) as i32);
820        lights.truncate(MAX_LIGHT_COUNT);
821        renderer.update_consts(&mut self.data.lights, lights);
822
823        // Update event lights
824        self.event_lights.retain_mut(|el| {
825            el.timeout -= dt;
826            el.timeout > 0.0
827        });
828
829        // Update shadow constants
830        let mut shadows = (
831            &scene_data.state.ecs().read_storage::<comp::Pos>(),
832            scene_data
833                .state
834                .ecs()
835                .read_storage::<crate::ecs::comp::Interpolated>()
836                .maybe(),
837            scene_data.state.ecs().read_storage::<comp::Scale>().maybe(),
838            &scene_data.state.ecs().read_storage::<comp::Body>(),
839            &scene_data.state.ecs().read_storage::<comp::Health>(),
840        )
841            .join()
842            .filter(|(_, _, _, _, health)| !health.is_dead)
843            .filter(|(pos, _, _, _, _)| {
844                pos.0.distance_squared(viewpoint_pos)
845                    < (loaded_distance.min(SHADOW_MAX_DIST) + SHADOW_DIST_RADIUS).powi(2)
846            })
847            .map(|(pos, interpolated, scale, _, _)| {
848                Shadow::new(
849                    // Use interpolated values pos if it is available
850                    interpolated.map_or(pos.0, |i| i.pos),
851                    scale.map_or(1.0, |s| s.0),
852                )
853            })
854            .collect::<Vec<_>>();
855        shadows.sort_by_key(|shadow| shadow.get_pos().distance_squared(viewpoint_pos) as i32);
856        shadows.truncate(MAX_SHADOW_COUNT);
857        renderer.update_consts(&mut self.data.shadows, &shadows);
858
859        // Remember to put the new loaded distance back in the scene.
860        self.loaded_distance = loaded_distance;
861
862        // Update light projection matrices for the shadow map.
863
864        // When the target time of day and time of day have a large discrepancy
865        // (i.e two days), the linear interpolation causes brght flashing effects
866        // in the sky. This will snap the time of day to the target time of day
867        // for the client to avoid the flashing effect if flashing lights is
868        // disabled.
869        const DAY: f64 = 60.0 * 60.0 * 24.0;
870        let time_of_day = scene_data.state.get_time_of_day();
871        let max_lerp_period = if scene_data.flashing_lights_enabled {
872            DAY * 2.0
873        } else {
874            DAY * 0.25
875        };
876        self.interpolated_time_of_day =
877            Some(self.interpolated_time_of_day.map_or(time_of_day, |tod| {
878                if (tod - time_of_day).abs() > max_lerp_period {
879                    time_of_day
880                } else {
881                    Lerp::lerp(tod, time_of_day, dt as f64)
882                }
883            }));
884        let time_of_day = self.interpolated_time_of_day.unwrap_or(time_of_day);
885        let focus_pos = self.camera.get_focus_pos();
886        let focus_off = focus_pos.map(|e| e.trunc());
887
888        // Update global constants.
889        renderer.update_consts(&mut self.data.globals, &[Globals::new(
890            view_mat,
891            proj_mat,
892            cam_pos,
893            focus_pos,
894            self.loaded_distance,
895            self.lod.get_data().tgt_detail as f32,
896            self.map_bounds,
897            time_of_day,
898            scene_data.state.get_time(),
899            self.local_time,
900            renderer.resolution().as_(),
901            Vec2::new(SHADOW_NEAR, SHADOW_FAR),
902            lights.len(),
903            shadows.len(),
904            NUM_DIRECTED_LIGHTS,
905            scene_data
906                .state
907                .terrain()
908                .get((cam_pos + focus_off).map(|e| e.floor() as i32))
909                .ok()
910                // Don't block the camera's view in solid blocks if the player is a moderator
911                .filter(|b| !(b.is_filled() && client.is_moderator()))
912                .map(|b| b.kind())
913                .unwrap_or(BlockKind::Air),
914            self.select_pos.map(|e| e - focus_off.map(|e| e as i32)),
915            scene_data.gamma,
916            scene_data.exposure,
917            self.last_lightning.unwrap_or((Vec3::zero(), -1000.0)),
918            self.wind_vel,
919            scene_data.ambiance,
920            self.camera.get_mode(),
921            scene_data.sprite_render_distance - 20.0,
922        )]);
923        renderer.update_clouds_locals(CloudsLocals::new(proj_mat_inv, view_mat_inv));
924        renderer.update_postprocess_locals(PostProcessLocals::new(proj_mat_inv, view_mat_inv));
925
926        // Maintain LoD.
927        self.lod.maintain(renderer, client, focus_pos, &self.camera);
928
929        // Maintain tethers.
930        self.tether_mgr.maintain(renderer, client, focus_pos);
931
932        // Maintain debug shapes
933        self.debug.maintain(renderer);
934
935        // Maintain the terrain.
936        let (
937            _visible_bounds,
938            visible_light_volume,
939            visible_psr_bounds,
940            visible_occlusion_volume,
941            visible_por_bounds,
942        ) = self.terrain.maintain(
943            renderer,
944            scene_data,
945            focus_pos,
946            self.loaded_distance,
947            &self.camera,
948        );
949
950        // Maintain the figures.
951        let _figure_bounds = self.figure_mgr.maintain(
952            renderer,
953            &mut self.trail_mgr,
954            scene_data,
955            visible_psr_bounds,
956            visible_por_bounds,
957            &self.camera,
958            Some(&self.terrain),
959        );
960
961        let fov = self.camera.get_effective_fov();
962        let aspect_ratio = self.camera.get_aspect_ratio();
963        let view_dir = ((focus_pos.map(f32::fract)) - cam_pos).normalized();
964
965        // We need to compute these offset matrices to transform world space coordinates
966        // to the translated ones we use when multiplying by the light space
967        // matrix; this helps avoid precision loss during the
968        // multiplication.
969        let look_at = cam_pos;
970        let new_dir = view_dir;
971        let new_dir = new_dir.normalized();
972        let up: math::Vec3<f32> = math::Vec3::unit_y();
973
974        // Optimal warping for directed lights:
975        //
976        // n_opt = 1 / sin y (z_n + √(z_n + (f - n) sin y))
977        //
978        // where n is near plane, f is far plane, y is the tilt angle between view and
979        // light direction, and n_opt is the optimal near plane.
980        // We also want a way to transform and scale this matrix (* 0.5 + 0.5) in order
981        // to transform it correctly into texture coordinates, as well as
982        // OpenGL coordinates.  Note that the matrix for directional light
983        // is *already* linear in the depth buffer.
984        //
985        // Also, observe that we flip the texture sampling matrix in order to account
986        // for the fact that DirectX renders top-down.
987        let texture_mat = Mat4::<f32>::scaling_3d::<Vec3<f32>>(Vec3::new(0.5, -0.5, 1.0))
988            * Mat4::translation_3d(Vec3::new(1.0, -1.0, 0.0));
989
990        let directed_mats = |d_view_mat: math::Mat4<f32>,
991                             d_dir: math::Vec3<f32>,
992                             volume: &Vec<math::Vec3<f32>>|
993         -> (Mat4<f32>, Mat4<f32>) {
994            // NOTE: Light view space, right-handed.
995            let v_p_orig = math::Vec3::from(d_view_mat * math::Vec4::from_direction(new_dir));
996            let mut v_p = v_p_orig.normalized();
997            let cos_gamma = new_dir.map(f64::from).dot(d_dir.map(f64::from));
998            let sin_gamma = (1.0 - cos_gamma * cos_gamma).sqrt();
999            let gamma = sin_gamma.asin();
1000            let view_mat = math::Mat4::from_col_array(view_mat.into_col_array());
1001            // coordinates are transformed from world space (right-handed) to view space
1002            // (right-handed).
1003            let bounds1 = math::fit_psr(
1004                view_mat.map_cols(math::Vec4::from),
1005                volume.iter().copied(),
1006                math::Vec4::homogenized,
1007            );
1008            let n_e = f64::from(-bounds1.max.z);
1009            let factor = compute_warping_parameter_perspective(
1010                gamma,
1011                n_e,
1012                f64::from(fov),
1013                f64::from(aspect_ratio),
1014            );
1015
1016            v_p.z = 0.0;
1017            v_p.normalize();
1018            let l_r: math::Mat4<f32> = if factor > EPSILON_UPSILON {
1019                // NOTE: Our coordinates are now in left-handed space, but v_p isn't; however,
1020                // v_p has no z component, so we don't have to adjust it for left-handed
1021                // spaces.
1022                math::Mat4::look_at_lh(math::Vec3::zero(), math::Vec3::unit_z(), v_p)
1023            } else {
1024                math::Mat4::identity()
1025            };
1026            // Convert from right-handed to left-handed coordinates.
1027            let directed_proj_mat = math::Mat4::new(
1028                1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
1029            );
1030
1031            let light_all_mat = l_r * directed_proj_mat * d_view_mat;
1032            // coordinates are transformed from world space (right-handed) to rotated light
1033            // space (left-handed).
1034            let bounds0 = math::fit_psr(
1035                light_all_mat,
1036                volume.iter().copied(),
1037                math::Vec4::homogenized,
1038            );
1039            // Vague idea: project z_n from the camera view to the light view (where it's
1040            // tilted by γ).
1041            //
1042            // NOTE: To transform a normal by M, we multiply by the transpose of the inverse
1043            // of M. For the cases below, we are transforming by an
1044            // already-inverted matrix, so the transpose of its inverse is
1045            // just the transpose of the original matrix.
1046            let (z_0, z_1) = {
1047                let f_e = f64::from(-bounds1.min.z).max(n_e);
1048                // view space, right-handed coordinates.
1049                let p_z = bounds1.max.z;
1050                // rotated light space, left-handed coordinates.
1051                let p_y = bounds0.min.y;
1052                let p_x = bounds0.center().x;
1053                // moves from view-space (right-handed) to world space (right-handed)
1054                let view_inv = view_mat.inverted();
1055                // moves from rotated light space (left-handed) to world space (right-handed).
1056                let light_all_inv = light_all_mat.inverted();
1057
1058                // moves from view-space (right-handed) to world-space (right-handed).
1059                let view_point = view_inv
1060                    * math::Vec4::from_point(
1061                        -math::Vec3::unit_z() * p_z, /* + math::Vec4::unit_w() */
1062                    );
1063                let view_plane = view_mat.transposed() * -math::Vec4::unit_z();
1064
1065                // moves from rotated light space (left-handed) to world space (right-handed).
1066                let light_point = light_all_inv
1067                    * math::Vec4::from_point(
1068                        math::Vec3::unit_y() * p_y, /* + math::Vec4::unit_w() */
1069                    );
1070                let light_plane = light_all_mat.transposed() * math::Vec4::unit_y();
1071
1072                // moves from rotated light space (left-handed) to world space (right-handed).
1073                let shadow_point = light_all_inv
1074                    * math::Vec4::from_point(
1075                        math::Vec3::unit_x() * p_x, /* + math::Vec4::unit_w() */
1076                    );
1077                let shadow_plane = light_all_mat.transposed() * math::Vec4::unit_x();
1078
1079                // Find the point at the intersection of the three planes; note that since the
1080                // equations are already in right-handed world space, we don't need to negate
1081                // the z coordinates.
1082                let solve_p0 = math::Mat4::new(
1083                    view_plane.x,
1084                    view_plane.y,
1085                    view_plane.z,
1086                    0.0,
1087                    light_plane.x,
1088                    light_plane.y,
1089                    light_plane.z,
1090                    0.0,
1091                    shadow_plane.x,
1092                    shadow_plane.y,
1093                    shadow_plane.z,
1094                    0.0,
1095                    0.0,
1096                    0.0,
1097                    0.0,
1098                    1.0,
1099                );
1100
1101                // in world-space (right-handed).
1102                let plane_dist = math::Vec4::new(
1103                    view_plane.dot(view_point),
1104                    light_plane.dot(light_point),
1105                    shadow_plane.dot(shadow_point),
1106                    1.0,
1107                );
1108                let p0_world = solve_p0.inverted() * plane_dist;
1109                // in rotated light-space (left-handed).
1110                let p0 = light_all_mat * p0_world;
1111                let mut p1 = p0;
1112                // in rotated light-space (left-handed).
1113                p1.y = bounds0.max.y;
1114
1115                // transforms from rotated light-space (left-handed) to view space
1116                // (right-handed).
1117                let view_from_light_mat = view_mat * light_all_inv;
1118                // z0 and z1 are in view space (right-handed).
1119                let z0 = view_from_light_mat * p0;
1120                let z1 = view_from_light_mat * p1;
1121
1122                // Extract the homogenized forward component (right-handed).
1123                //
1124                // NOTE: I don't think the w component should be anything but 1 here, but
1125                // better safe than sorry.
1126                (
1127                    f64::from(z0.homogenized().dot(-math::Vec4::unit_z())).clamp(n_e, f_e),
1128                    f64::from(z1.homogenized().dot(-math::Vec4::unit_z())).clamp(n_e, f_e),
1129                )
1130            };
1131
1132            // all of this is in rotated light-space (left-handed).
1133            let mut light_focus_pos: math::Vec3<f32> = math::Vec3::zero();
1134            light_focus_pos.x = bounds0.center().x;
1135            light_focus_pos.y = bounds0.min.y;
1136            light_focus_pos.z = bounds0.center().z;
1137
1138            let d = f64::from(bounds0.max.y - bounds0.min.y).abs();
1139
1140            let w_l_y = d;
1141
1142            // NOTE: See section 5.1.2.2 of Lloyd's thesis.
1143            // NOTE: Since z_1 and z_0 are in the same coordinate space, we don't have to
1144            // worry about the handedness of their ratio.
1145            let alpha = z_1 / z_0;
1146            let alpha_sqrt = alpha.sqrt();
1147            let directed_near_normal = if factor < 0.0 {
1148                // Standard shadow map to LiSPSM
1149                (1.0 + alpha_sqrt - factor * (alpha - 1.0)) / ((alpha - 1.0) * (factor + 1.0))
1150            } else {
1151                // LiSPSM to PSM
1152                ((alpha_sqrt - 1.0) * (factor * alpha_sqrt + 1.0)).recip()
1153            };
1154
1155            // Equation 5.14 - 5.16
1156            let y_ = |v: f64| w_l_y * (v + directed_near_normal).abs();
1157            let directed_near = y_(0.0) as f32;
1158            let directed_far = y_(1.0) as f32;
1159            light_focus_pos.y = if factor > EPSILON_UPSILON {
1160                light_focus_pos.y - directed_near
1161            } else {
1162                light_focus_pos.y
1163            };
1164            // Left-handed translation.
1165            let w_v: math::Mat4<f32> = math::Mat4::translation_3d(-math::Vec3::new(
1166                light_focus_pos.x,
1167                light_focus_pos.y,
1168                light_focus_pos.z,
1169            ));
1170            let shadow_view_mat: math::Mat4<f32> = w_v * light_all_mat;
1171            let w_p: math::Mat4<f32> = {
1172                if factor > EPSILON_UPSILON {
1173                    // Projection for y
1174                    let near = directed_near;
1175                    let far = directed_far;
1176                    let left = -1.0;
1177                    let right = 1.0;
1178                    let bottom = -1.0;
1179                    let top = 1.0;
1180                    let s_x = 2.0 * near / (right - left);
1181                    let o_x = (right + left) / (right - left);
1182                    let s_z = 2.0 * near / (top - bottom);
1183                    let o_z = (top + bottom) / (top - bottom);
1184
1185                    let s_y = (far + near) / (far - near);
1186                    let o_y = -2.0 * far * near / (far - near);
1187
1188                    math::Mat4::new(
1189                        s_x, o_x, 0.0, 0.0, 0.0, s_y, 0.0, o_y, 0.0, o_z, s_z, 0.0, 0.0, 1.0, 0.0,
1190                        0.0,
1191                    )
1192                } else {
1193                    math::Mat4::identity()
1194                }
1195            };
1196
1197            let shadow_all_mat: math::Mat4<f32> = w_p * shadow_view_mat;
1198            // coordinates are transformed from world space (right-handed)
1199            // to post-warp light space (left-handed), then homogenized.
1200            let math::Aabb::<f32> {
1201                min:
1202                    math::Vec3 {
1203                        x: xmin,
1204                        y: ymin,
1205                        z: zmin,
1206                    },
1207                max:
1208                    math::Vec3 {
1209                        x: xmax,
1210                        y: ymax,
1211                        z: zmax,
1212                    },
1213            } = math::fit_psr(
1214                shadow_all_mat,
1215                volume.iter().copied(),
1216                math::Vec4::homogenized,
1217            );
1218            let s_x = 2.0 / (xmax - xmin);
1219            let s_y = 2.0 / (ymax - ymin);
1220            let s_z = 1.0 / (zmax - zmin);
1221            let o_x = -(xmax + xmin) / (xmax - xmin);
1222            let o_y = -(ymax + ymin) / (ymax - ymin);
1223            let o_z = -zmin / (zmax - zmin);
1224            let directed_proj_mat = Mat4::new(
1225                s_x, 0.0, 0.0, o_x, 0.0, s_y, 0.0, o_y, 0.0, 0.0, s_z, o_z, 0.0, 0.0, 0.0, 1.0,
1226            );
1227
1228            let shadow_all_mat: Mat4<f32> = Mat4::from_col_arrays(shadow_all_mat.into_col_arrays());
1229
1230            let directed_texture_proj_mat = texture_mat * directed_proj_mat;
1231            (
1232                directed_proj_mat * shadow_all_mat,
1233                directed_texture_proj_mat * shadow_all_mat,
1234            )
1235        };
1236
1237        let weather = client
1238            .state()
1239            .max_weather_near(focus_off.xy() + cam_pos.xy());
1240        self.wind_vel = weather.wind_vel();
1241        if weather.rain > RAIN_THRESHOLD {
1242            let weather = client.weather_at_player();
1243            let rain_vel = weather.rain_vel();
1244            let rain_view_mat = math::Mat4::look_at_rh(look_at, look_at + rain_vel, up);
1245
1246            self.integrated_rain_vel += rain_vel.magnitude() * dt;
1247            let rain_dir_mat = Mat4::rotation_from_to_3d(-Vec3::unit_z(), rain_vel);
1248
1249            let (shadow_mat, texture_mat) =
1250                directed_mats(rain_view_mat, rain_vel, &visible_occlusion_volume);
1251
1252            let rain_occlusion_locals = RainOcclusionLocals::new(
1253                shadow_mat,
1254                texture_mat,
1255                rain_dir_mat,
1256                weather.rain,
1257                self.integrated_rain_vel,
1258            );
1259
1260            renderer.update_consts(&mut self.data.rain_occlusion_mats, &[rain_occlusion_locals]);
1261        } else if self.integrated_rain_vel > 0.0 {
1262            self.integrated_rain_vel = 0.0;
1263            // Need to set rain to zero
1264            let rain_occlusion_locals = RainOcclusionLocals::default();
1265            renderer.update_consts(&mut self.data.rain_occlusion_mats, &[rain_occlusion_locals]);
1266        }
1267
1268        let sun_dir = scene_data.get_sun_dir();
1269        let is_daylight = sun_dir.z < 0.0;
1270        if renderer.pipeline_modes().shadow.is_map() && (is_daylight || !lights.is_empty()) {
1271            let (point_shadow_res, _directed_shadow_res) = renderer.get_shadow_resolution();
1272            // NOTE: The aspect ratio is currently always 1 for our cube maps, since they
1273            // are equal on all sides.
1274            let point_shadow_aspect = point_shadow_res.x as f32 / point_shadow_res.y as f32;
1275            // Construct matrices to transform from world space to light space for the sun
1276            // and moon.
1277            let directed_light_dir = sun_dir;
1278
1279            // We upload view matrices as well, to assist in linearizing vertex positions.
1280            // (only for directional lights, so far).
1281            let mut directed_shadow_mats = Vec::with_capacity(6);
1282
1283            let light_view_mat = math::Mat4::look_at_rh(look_at, look_at + directed_light_dir, up);
1284            let (shadow_mat, texture_mat) =
1285                directed_mats(light_view_mat, directed_light_dir, &visible_light_volume);
1286
1287            let shadow_locals = ShadowLocals::new(shadow_mat, texture_mat);
1288
1289            renderer.update_consts(&mut self.data.shadow_mats, &[shadow_locals]);
1290
1291            directed_shadow_mats.push(light_view_mat);
1292            // This leaves us with five dummy slots, which we push as defaults.
1293            directed_shadow_mats
1294                .extend_from_slice(&[math::Mat4::default(); 6 - NUM_DIRECTED_LIGHTS] as _);
1295            // Now, construct the full projection matrices in the first two directed light
1296            // slots.
1297            let mut shadow_mats = Vec::with_capacity(6 * (lights.len() + 1));
1298            shadow_mats.resize_with(6, PointLightMatrix::default);
1299            // Now, we tackle point lights.
1300            // First, create a perspective projection matrix at 90 degrees (to cover a whole
1301            // face of the cube map we're using); we use a negative near plane to exactly
1302            // match OpenGL's behavior if we use a left-handed coordinate system everywhere
1303            // else.
1304            let shadow_proj = camera::perspective_rh_zo_general(
1305                90.0f32.to_radians(),
1306                point_shadow_aspect,
1307                1.0 / SHADOW_NEAR,
1308                1.0 / SHADOW_FAR,
1309            );
1310            // NOTE: We negate here to emulate a right-handed projection with a negative
1311            // near plane, which produces the correct transformation to exactly match
1312            // OpenGL's rendering behavior if we use a left-handed coordinate
1313            // system everywhere else.
1314            let shadow_proj = shadow_proj * Mat4::scaling_3d(-1.0);
1315
1316            // Next, construct the 6 orientations we'll use for the six faces, in terms of
1317            // their (forward, up) vectors.
1318            let orientations = [
1319                (Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, -1.0, 0.0)),
1320                (Vec3::new(-1.0, 0.0, 0.0), Vec3::new(0.0, -1.0, 0.0)),
1321                (Vec3::new(0.0, 1.0, 0.0), Vec3::new(0.0, 0.0, 1.0)),
1322                (Vec3::new(0.0, -1.0, 0.0), Vec3::new(0.0, 0.0, -1.0)),
1323                (Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, -1.0, 0.0)),
1324                (Vec3::new(0.0, 0.0, -1.0), Vec3::new(0.0, -1.0, 0.0)),
1325            ];
1326
1327            // NOTE: We could create the shadow map collection at the same time as the
1328            // lights, but then we'd have to sort them both, which wastes time.  Plus, we
1329            // want to prepend our directed lights.
1330            shadow_mats.extend(lights.iter().flat_map(|light| {
1331                // Now, construct the full projection matrix by making the light look at each
1332                // cube face.
1333                let eye = Vec3::new(light.pos[0], light.pos[1], light.pos[2]) - focus_off;
1334                orientations.iter().map(move |&(forward, up)| {
1335                    // NOTE: We don't currently try to linearize point lights or need a separate
1336                    // transform for them.
1337                    PointLightMatrix::new(shadow_proj * Mat4::look_at_lh(eye, eye + forward, up))
1338                })
1339            }));
1340
1341            for (i, val) in shadow_mats.into_iter().enumerate() {
1342                self.data.point_light_matrices[i] = val
1343            }
1344        }
1345
1346        // Remove unused figures.
1347        self.figure_mgr.clean(scene_data.tick);
1348
1349        // Maintain audio
1350        self.sfx_mgr.maintain(
1351            audio,
1352            scene_data.state,
1353            scene_data.viewpoint_entity,
1354            &self.camera,
1355            &self.terrain,
1356            client,
1357        );
1358
1359        self.ambience_mgr
1360            .maintain(audio, scene_data.state, client, &self.camera);
1361
1362        self.music_mgr.maintain(audio, scene_data.state, client);
1363    }
1364
1365    pub fn global_bind_group(&self) -> &GlobalsBindGroup { &self.globals_bind_group }
1366
1367    /// Render the scene using the provided `Drawer`.
1368    pub fn render(
1369        &self,
1370        drawer: &mut Drawer<'_>,
1371        state: &State,
1372        viewpoint_entity: EcsEntity,
1373        tick: u64,
1374        scene_data: &SceneData,
1375    ) {
1376        span!(_guard, "render", "Scene::render");
1377        let sun_dir = scene_data.get_sun_dir();
1378        let is_daylight = sun_dir.z < 0.0;
1379        let focus_pos = self.camera.get_focus_pos();
1380        let cam_pos = self.camera.dependents().cam_pos + focus_pos.map(|e| e.trunc());
1381        let is_rain = state.max_weather_near(cam_pos.xy()).rain > RAIN_THRESHOLD;
1382        let culling_mode = if scene_data
1383            .state
1384            .terrain()
1385            .get_key(scene_data.state.terrain().pos_key(cam_pos.as_()))
1386            .is_some_and(|c| cam_pos.z < c.meta().alt() - terrain::UNDERGROUND_ALT)
1387        {
1388            CullingMode::Underground
1389        } else {
1390            CullingMode::Surface
1391        };
1392
1393        let camera_data = (&self.camera, scene_data.figure_lod_render_distance);
1394
1395        // would instead have this as an extension.
1396        if drawer.pipeline_modes().shadow.is_map() && (is_daylight || !self.light_data.is_empty()) {
1397            if is_daylight {
1398                prof_span!("directed shadows");
1399                if let Some(mut shadow_pass) = drawer.shadow_pass() {
1400                    // Render terrain directed shadows.
1401                    self.terrain.render_shadows(
1402                        &mut shadow_pass.draw_terrain_shadows(),
1403                        focus_pos,
1404                        culling_mode,
1405                    );
1406
1407                    // Render figure directed shadows.
1408                    self.figure_mgr.render_shadows(
1409                        &mut shadow_pass.draw_figure_shadows(),
1410                        state,
1411                        tick,
1412                        camera_data,
1413                    );
1414                    self.debug
1415                        .render_shadows(&mut shadow_pass.draw_debug_shadows());
1416                }
1417            }
1418
1419            // Render terrain point light shadows.
1420            {
1421                prof_span!("point shadows");
1422                drawer.draw_point_shadows(
1423                    &self.data.point_light_matrices,
1424                    self.terrain.chunks_for_point_shadows(focus_pos),
1425                )
1426            }
1427        }
1428        // Render rain occlusion texture
1429        if is_rain {
1430            prof_span!("rain occlusion");
1431            if let Some(mut occlusion_pass) = drawer.rain_occlusion_pass() {
1432                self.terrain
1433                    .render_rain_occlusion(&mut occlusion_pass.draw_terrain_shadows(), cam_pos);
1434
1435                self.figure_mgr.render_rain_occlusion(
1436                    &mut occlusion_pass.draw_figure_shadows(),
1437                    state,
1438                    tick,
1439                    camera_data,
1440                );
1441            }
1442        }
1443
1444        prof_span!(guard, "main pass");
1445        if let Some(mut first_pass) = drawer.first_pass() {
1446            self.figure_mgr.render_viewpoint(
1447                &mut first_pass.draw_figures(),
1448                state,
1449                viewpoint_entity,
1450                tick,
1451                camera_data,
1452            );
1453
1454            self.terrain
1455                .render(&mut first_pass, focus_pos, culling_mode);
1456
1457            self.figure_mgr.render(
1458                &mut first_pass.draw_figures(),
1459                state,
1460                viewpoint_entity,
1461                tick,
1462                camera_data,
1463            );
1464
1465            self.lod.render(&mut first_pass, culling_mode);
1466
1467            // Render the skybox.
1468            first_pass.draw_skybox(&self.skybox.model);
1469
1470            // Draws sprites
1471            let mut sprite_drawer = first_pass.draw_sprites(
1472                &self.terrain.sprite_globals,
1473                &self.terrain.sprite_render_state.sprite_atlas_textures,
1474            );
1475            self.figure_mgr.render_sprites(
1476                &mut sprite_drawer,
1477                state,
1478                cam_pos,
1479                scene_data.sprite_render_distance,
1480            );
1481            self.terrain.render_sprites(
1482                &mut sprite_drawer,
1483                focus_pos,
1484                cam_pos,
1485                scene_data.sprite_render_distance,
1486                culling_mode,
1487            );
1488            drop(sprite_drawer);
1489
1490            // Render tethers.
1491            self.tether_mgr.render(&mut first_pass);
1492
1493            // Draws translucent
1494            self.terrain.render_translucent(&mut first_pass, focus_pos);
1495
1496            // Render particle effects.
1497            self.particle_mgr
1498                .render(&mut first_pass.draw_particles(), scene_data);
1499
1500            // Render debug shapes
1501            self.debug.render(&mut first_pass.draw_debug());
1502        }
1503        drop(guard);
1504    }
1505
1506    pub fn maintain_debug_hitboxes(
1507        &mut self,
1508        client: &Client,
1509        settings: &Settings,
1510        hitboxes: &mut HashMap<specs::Entity, DebugShapeId>,
1511        tracks: &mut HashMap<Vec2<i32>, Vec<DebugShapeId>>,
1512    ) {
1513        let ecs = client.state().ecs();
1514        {
1515            let mut current_chunks = hashbrown::HashSet::new();
1516            let terrain_grid = ecs.read_resource::<TerrainGrid>();
1517            for (key, chunk) in terrain_grid.iter() {
1518                current_chunks.insert(key);
1519                tracks.entry(key).or_insert_with(|| {
1520                    let mut ret = Vec::new();
1521                    for bezier in chunk.meta().tracks().iter() {
1522                        let shape_id = self.debug.add_shape(DebugShape::TrainTrack {
1523                            path: *bezier,
1524                            rail_width: 0.25,
1525                            rail_sep: 1.0,
1526                            plank_width: 0.5,
1527                            plank_height: 0.125,
1528                            plank_sep: 2.0,
1529                        });
1530                        ret.push(shape_id);
1531                        self.debug
1532                            .set_context(shape_id, [0.0; 4], [1.0; 4], [0.0, 0.0, 0.0, 1.0]);
1533                    }
1534                    for point in chunk.meta().debug_points().iter() {
1535                        let shape_id = self.debug.add_shape(DebugShape::Cylinder {
1536                            radius: 0.1,
1537                            height: 0.1,
1538                        });
1539                        ret.push(shape_id);
1540                        self.debug.set_context(
1541                            shape_id,
1542                            point.with_w(0.0).into_array(),
1543                            [1.0; 4],
1544                            [0.0, 0.0, 0.0, 1.0],
1545                        );
1546                    }
1547                    for line in chunk.meta().debug_lines().iter() {
1548                        let shape_id = self
1549                            .debug
1550                            .add_shape(DebugShape::Line([line.start, line.end], 0.1));
1551                        ret.push(shape_id);
1552                        self.debug
1553                            .set_context(shape_id, [0.0; 4], [1.0; 4], [0.0, 0.0, 0.0, 1.0]);
1554                    }
1555                    ret
1556                });
1557            }
1558            tracks.retain(|k, v| {
1559                let keep = current_chunks.contains(k);
1560                if !keep {
1561                    for shape in v.iter() {
1562                        self.debug.remove_shape(*shape);
1563                    }
1564                }
1565                keep
1566            });
1567        }
1568        let mut current_entities = hashbrown::HashSet::new();
1569        if settings.interface.toggle_hitboxes {
1570            let positions = ecs.read_component::<comp::Pos>();
1571            let colliders = ecs.read_component::<comp::Collider>();
1572            let orientations = ecs.read_component::<comp::Ori>();
1573            let scales = ecs.read_component::<comp::Scale>();
1574            let groups = ecs.read_component::<comp::Group>();
1575            for (entity, pos, collider, ori, scale, group) in (
1576                &ecs.entities(),
1577                &positions,
1578                &colliders,
1579                &orientations,
1580                scales.maybe(),
1581                groups.maybe(),
1582            )
1583                .join()
1584            {
1585                match collider {
1586                    comp::Collider::CapsulePrism {
1587                        p0,
1588                        p1,
1589                        radius,
1590                        z_min,
1591                        z_max,
1592                    } => {
1593                        let scale = scale.map_or(1.0, |s| s.0);
1594                        current_entities.insert(entity);
1595
1596                        let shape = DebugShape::CapsulePrism {
1597                            p0: *p0 * scale,
1598                            p1: *p1 * scale,
1599                            radius: *radius * scale,
1600                            height: (*z_max - *z_min) * scale,
1601                        };
1602
1603                        // If this shape no longer matches, remove the old one
1604                        if let Some(shape_id) = hitboxes.get(&entity) {
1605                            if self.debug.get_shape(*shape_id).is_some_and(|s| s != &shape) {
1606                                self.debug.remove_shape(*shape_id);
1607                                hitboxes.remove(&entity);
1608                            }
1609                        }
1610
1611                        let shape_id = hitboxes
1612                            .entry(entity)
1613                            .or_insert_with(|| self.debug.add_shape(shape));
1614                        let hb_pos = [pos.0.x, pos.0.y, pos.0.z + *z_min * scale, 0.0];
1615                        let color = if group == Some(&comp::group::ENEMY) {
1616                            [1.0, 0.0, 0.0, 0.5]
1617                        } else if group == Some(&comp::group::NPC) {
1618                            [0.0, 0.0, 1.0, 0.5]
1619                        } else {
1620                            [0.0, 1.0, 0.0, 0.5]
1621                        };
1622                        //let color = [1.0, 1.0, 1.0, 1.0];
1623                        let ori = ori.to_quat();
1624                        let hb_ori = [ori.x, ori.y, ori.z, ori.w];
1625                        self.debug.set_context(*shape_id, hb_pos, color, hb_ori);
1626                    },
1627                    comp::Collider::Voxel { .. }
1628                    | comp::Collider::Volume(_)
1629                    | comp::Collider::Point => {
1630                        // ignore terrain-like or point-hitboxes
1631                    },
1632                }
1633            }
1634        }
1635        hitboxes.retain(|k, v| {
1636            let keep = current_entities.contains(k);
1637            if !keep {
1638                self.debug.remove_shape(*v);
1639            }
1640            keep
1641        });
1642    }
1643
1644    pub fn maintain_debug_vectors(&mut self, client: &Client, lines: &mut PlayerDebugLines) {
1645        lines
1646            .chunk_normal
1647            .take()
1648            .map(|id| self.debug.remove_shape(id));
1649        lines.fluid_vel.take().map(|id| self.debug.remove_shape(id));
1650        lines.wind.take().map(|id| self.debug.remove_shape(id));
1651        lines.vel.take().map(|id| self.debug.remove_shape(id));
1652        if self.debug_vectors_enabled {
1653            let ecs = client.state().ecs();
1654
1655            let vels = &ecs.read_component::<comp::Vel>();
1656            let Some(vel) = vels.get(client.entity()) else {
1657                return;
1658            };
1659
1660            let phys_states = &ecs.read_component::<comp::PhysicsState>();
1661            let Some(phys) = phys_states.get(client.entity()) else {
1662                return;
1663            };
1664
1665            let positions = &ecs.read_component::<comp::Pos>();
1666            let Some(pos) = positions.get(client.entity()) else {
1667                return;
1668            };
1669
1670            let weather = ecs.read_resource::<WeatherGrid>();
1671            // take id and remove to delete the previous lines.
1672
1673            const LINE_WIDTH: f32 = 0.05;
1674            // Fluid Velocity
1675            {
1676                let Some(fluid) = phys.in_fluid else {
1677                    return;
1678                };
1679                let shape = DebugShape::Line([pos.0, pos.0 + fluid.flow_vel().0 / 2.], LINE_WIDTH);
1680                let id = self.debug.add_shape(shape);
1681                lines.fluid_vel = Some(id);
1682                self.debug
1683                    .set_context(id, [0.0; 4], [0.18, 0.72, 0.87, 0.8], [0.0, 0.0, 0.0, 1.0]);
1684            }
1685            // Chunk Terrain Normal Vector
1686            {
1687                let Some(chunk) = client.current_chunk() else {
1688                    return;
1689                };
1690                let shape = DebugShape::Line(
1691                    [
1692                        pos.0,
1693                        pos.0
1694                            + chunk
1695                                .meta()
1696                                .approx_chunk_terrain_normal()
1697                                .unwrap_or(Vec3::unit_z())
1698                                * 2.5,
1699                    ],
1700                    LINE_WIDTH,
1701                );
1702                let id = self.debug.add_shape(shape);
1703                lines.chunk_normal = Some(id);
1704                self.debug
1705                    .set_context(id, [0.0; 4], [0.22, 0.63, 0.1, 0.8], [0.0, 0.0, 0.0, 1.0]);
1706            }
1707            // Wind
1708            {
1709                let wind = weather.get_interpolated(pos.0.xy()).wind_vel();
1710                let shape = DebugShape::Line([pos.0, pos.0 + wind * 5.0], LINE_WIDTH);
1711                let id = self.debug.add_shape(shape);
1712                lines.wind = Some(id);
1713                self.debug
1714                    .set_context(id, [0.0; 4], [0.76, 0.76, 0.76, 0.8], [0.0, 0.0, 0.0, 1.0]);
1715            }
1716            // Player Vel
1717            {
1718                let shape = DebugShape::Line([pos.0, pos.0 + vel.0 / 2.0], LINE_WIDTH);
1719                let id = self.debug.add_shape(shape);
1720                lines.vel = Some(id);
1721                self.debug
1722                    .set_context(id, [0.0; 4], [0.98, 0.76, 0.01, 0.8], [0.0, 0.0, 0.0, 1.0]);
1723            }
1724        }
1725    }
1726}