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