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, CharacterState, item::ItemDesc, ship::figuredata::VOXEL_COLLIDER_MANIFEST,
46 slot::EquipSlot, 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
65const CURSOR_PAN_SCALE: f32 = 0.005;
67
68pub(crate) const MAX_LIGHT_COUNT: usize = 20; pub(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; const SHADOW_DIST_RADIUS: f32 = 8.0;
75const SHADOW_MAX_DIST: f32 = 96.0; const EPSILON_UPSILON: f64 = -1.0;
78
79const SHADOW_NEAR: f32 = 0.25; const SHADOW_FAR: f32 = 128.0; const RUNNING_THRESHOLD: f32 = 0.7;
85
86const RAIN_THRESHOLD: f32 = 0.0;
88
89pub 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 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, 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
171fn 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
218fn 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 } 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())
260 } 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 .max(-F::one()).min(F::one())
268}
269
270fn 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 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 pub fn globals(&self) -> &Consts<Globals> { &self.data.globals }
381
382 pub fn camera(&self) -> &Camera { &self.camera }
384
385 pub fn terrain(&self) -> &Terrain<TerrainChunk> { &self.terrain }
387
388 pub fn lights(&self) -> &Vec<Light> { &self.light_data }
390
391 pub fn particle_mgr(&self) -> &ParticleMgr { &self.particle_mgr }
393
394 pub fn trail_mgr(&self) -> &TrailMgr { &self.trail_mgr }
396
397 pub fn figure_mgr(&self) -> &FigureMgr { &self.figure_mgr }
399
400 pub fn music_mgr(&self) -> &MusicMgr { &self.music_mgr }
401
402 pub fn camera_mut(&mut self) -> &mut Camera { &mut self.camera }
404
405 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 pub fn handle_input_event(&mut self, event: Event, client: &Client) -> bool {
415 match event {
416 Event::Resize(dims) => {
418 self.camera.set_aspect_ratio(dims.x as f32 / dims.y as f32);
419 true
420 },
421 Event::CursorPan(delta) => {
423 self.camera.rotate_by(Vec3::from(delta) * CURSOR_PAN_SCALE);
424 true
425 },
426 Event::Zoom(delta) => {
428 let cap = if client.is_moderator() {
429 ZOOM_CAP_ADMIN
430 } else {
431 ZOOM_CAP_PLAYER
432 };
433 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 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 _ => 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 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 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), )
598 });
599
600 if scene_data.mutable_viewpoint || matches!(self.camera.get_mode(), CameraMode::Freefly)
601 {
602 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 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
645 | ToolKind::Staff
646 | ToolKind::Sceptre
647 | ToolKind::Throwable
648 )
649 })
650 || client
651 .current::<CharacterState>()
652 .is_some_and(|char_state| matches!(char_state, CharacterState::Throw(_)));
653
654 let up = match self.camera.get_mode() {
655 CameraMode::FirstPerson => {
656 if viewpoint_rolling {
657 viewpoint_height * 0.42
658 } else if is_running && on_ground.unwrap_or(false) {
659 viewpoint_eye_height
660 + (scene_data.state.get_time() as f32 * 17.0).sin() * 0.05
661 } else {
662 viewpoint_eye_height
663 }
664 },
665 CameraMode::ThirdPerson if scene_data.is_aiming && holding_ranged => {
666 viewpoint_height * 1.16 + settings.gameplay.aim_offset_y
667 },
668 CameraMode::ThirdPerson if scene_data.is_aiming => viewpoint_height * 1.16,
669 CameraMode::ThirdPerson => viewpoint_eye_height,
670 CameraMode::Freefly => 0.0,
671 };
672
673 let right = match self.camera.get_mode() {
674 CameraMode::FirstPerson => 0.0,
675 CameraMode::ThirdPerson if scene_data.is_aiming && holding_ranged => {
676 settings.gameplay.aim_offset_x
677 },
678 CameraMode::ThirdPerson => 0.0,
679 CameraMode::Freefly => 0.0,
680 };
681
682 let tilt = self.camera.get_orientation().y;
684 let dist = self.camera.get_distance();
685
686 Vec3::unit_z() * (up * viewpoint_scale - tilt.min(0.0).sin() * dist * 0.6)
687 + self.camera.right() * (right * viewpoint_scale)
688 } else {
689 self.figure_mgr
690 .viewpoint_offset(scene_data, scene_data.viewpoint_entity)
691 };
692
693 match self.camera.get_mode() {
694 CameraMode::FirstPerson | CameraMode::ThirdPerson => {
695 self.camera.set_focus_pos(viewpoint_pos + viewpoint_offset);
696 },
697 CameraMode::Freefly => {},
698 };
699
700 self.camera
702 .update(scene_data.state.get_time(), dt, scene_data.mouse_smoothing);
703 viewpoint_pos
704 } else {
705 Vec3::zero()
706 };
707
708 self.camera.compute_dependents(&scene_data.state.terrain());
710 let camera::Dependents {
711 view_mat,
712 view_mat_inv,
713 proj_mat,
714 proj_mat_inv,
715 cam_pos,
716 ..
717 } = self.camera.dependents();
718
719 let loaded_distance =
721 (0.98 * self.loaded_distance + 0.02 * scene_data.loaded_distance).max(0.01);
722
723 let lights = &mut self.light_data;
725 lights.clear();
726
727 self.particle_mgr.maintain(
729 renderer,
730 scene_data,
731 &self.terrain,
732 &self.figure_mgr,
733 lights,
734 );
735
736 self.trail_mgr.maintain(renderer, scene_data);
738
739 let max_light_dist = loaded_distance.powi(2) + LIGHT_DIST_RADIUS;
741 lights.extend(
742 (
743 &scene_data.state.ecs().read_storage::<comp::Pos>(),
744 scene_data
745 .state
746 .ecs()
747 .read_storage::<crate::ecs::comp::Interpolated>()
748 .maybe(),
749 &scene_data
750 .state
751 .ecs()
752 .read_storage::<comp::LightAnimation>(),
753 scene_data
754 .state
755 .ecs()
756 .read_storage::<comp::Health>()
757 .maybe(),
758 )
759 .join()
760 .filter(|(pos, _, light_anim, h)| {
761 light_anim.col != Rgb::zero()
762 && light_anim.strength > 0.0
763 && pos.0.distance_squared(viewpoint_pos) < max_light_dist
764 && h.is_none_or(|h| !h.is_dead)
765 })
766 .map(|(pos, interpolated, light_anim, _)| {
767 let pos = interpolated.map_or(pos.0, |i| i.pos);
769 Light::new(pos + light_anim.offset, light_anim.col, light_anim.strength)
770 })
771 .chain(
772 self.event_lights
773 .iter()
774 .map(|el| el.light.with_strength((el.fadeout)(el.timeout))),
775 ),
776 );
777 let voxel_colliders_manifest = VOXEL_COLLIDER_MANIFEST.read();
778 let figure_mgr = &self.figure_mgr;
779 lights.extend(
780 (
781 &scene_data.state.ecs().entities(),
782 &scene_data
783 .state
784 .read_storage::<crate::ecs::comp::Interpolated>(),
785 &scene_data.state.read_storage::<comp::Body>(),
786 &scene_data.state.read_storage::<comp::Collider>(),
787 )
788 .join()
789 .filter_map(|(entity, interpolated, body, collider)| {
790 let vol = collider.get_vol(&voxel_colliders_manifest)?;
791 let (blocks_of_interest, offset) =
792 figure_mgr.get_blocks_of_interest(entity, body, Some(collider))?;
793
794 let mat = Mat4::from(interpolated.ori.to_quat())
795 .translated_3d(interpolated.pos)
796 * Mat4::translation_3d(offset);
797
798 let p = mat.inverted().mul_point(viewpoint_pos);
799 let aabb = Aabb {
800 min: Vec3::zero(),
801 max: vol.volume().sz.as_(),
802 };
803 if aabb.contains_point(p) || aabb.distance_to_point(p) < max_light_dist {
804 Some(
805 blocks_of_interest
806 .lights
807 .iter()
808 .map(move |(block_offset, level)| {
809 let wpos = mat.mul_point(block_offset.as_() + 0.5);
810 (wpos, level)
811 })
812 .filter(move |(wpos, _)| {
813 wpos.distance_squared(viewpoint_pos) < max_light_dist
814 })
815 .map(|(wpos, level)| {
816 Light::new(wpos, Rgb::white(), *level as f32 / 7.0)
817 }),
818 )
819 } else {
820 None
821 }
822 })
823 .flatten(),
824 );
825 lights.sort_by_key(|light| light.get_pos().distance_squared(viewpoint_pos) as i32);
826 lights.truncate(MAX_LIGHT_COUNT);
827 renderer.update_consts(&mut self.data.lights, lights);
828
829 self.event_lights.retain_mut(|el| {
831 el.timeout -= dt;
832 el.timeout > 0.0
833 });
834
835 let mut shadows = (
837 &scene_data.state.ecs().read_storage::<comp::Pos>(),
838 scene_data
839 .state
840 .ecs()
841 .read_storage::<crate::ecs::comp::Interpolated>()
842 .maybe(),
843 scene_data.state.ecs().read_storage::<comp::Scale>().maybe(),
844 &scene_data.state.ecs().read_storage::<comp::Body>(),
845 &scene_data.state.ecs().read_storage::<comp::Health>(),
846 )
847 .join()
848 .filter(|(_, _, _, _, health)| !health.is_dead)
849 .filter(|(pos, _, _, _, _)| {
850 pos.0.distance_squared(viewpoint_pos)
851 < (loaded_distance.min(SHADOW_MAX_DIST) + SHADOW_DIST_RADIUS).powi(2)
852 })
853 .map(|(pos, interpolated, scale, _, _)| {
854 Shadow::new(
855 interpolated.map_or(pos.0, |i| i.pos),
857 scale.map_or(1.0, |s| s.0),
858 )
859 })
860 .collect::<Vec<_>>();
861 shadows.sort_by_key(|shadow| shadow.get_pos().distance_squared(viewpoint_pos) as i32);
862 shadows.truncate(MAX_SHADOW_COUNT);
863 renderer.update_consts(&mut self.data.shadows, &shadows);
864
865 self.loaded_distance = loaded_distance;
867
868 const DAY: f64 = 60.0 * 60.0 * 24.0;
876 let time_of_day = scene_data.state.get_time_of_day();
877 let max_lerp_period = if scene_data.flashing_lights_enabled {
878 DAY * 2.0
879 } else {
880 DAY * 0.25
881 };
882 self.interpolated_time_of_day =
883 Some(self.interpolated_time_of_day.map_or(time_of_day, |tod| {
884 if (tod - time_of_day).abs() > max_lerp_period {
885 time_of_day
886 } else {
887 Lerp::lerp(tod, time_of_day, dt as f64)
888 }
889 }));
890 let time_of_day = self.interpolated_time_of_day.unwrap_or(time_of_day);
891 let focus_pos = self.camera.get_focus_pos();
892 let focus_off = focus_pos.map(|e| e.trunc());
893
894 renderer.update_consts(&mut self.data.globals, &[Globals::new(
896 view_mat,
897 proj_mat,
898 cam_pos,
899 focus_pos,
900 self.loaded_distance,
901 self.lod.get_data().tgt_detail as f32,
902 self.map_bounds,
903 time_of_day,
904 scene_data.state.get_time(),
905 self.local_time,
906 renderer.resolution().as_(),
907 Vec2::new(SHADOW_NEAR, SHADOW_FAR),
908 lights.len(),
909 shadows.len(),
910 NUM_DIRECTED_LIGHTS,
911 scene_data
912 .state
913 .terrain()
914 .get((cam_pos + focus_off).map(|e| e.floor() as i32))
915 .ok()
916 .filter(|b| !(b.is_filled() && client.is_moderator()))
918 .map(|b| b.kind())
919 .unwrap_or(BlockKind::Air),
920 self.select_pos.map(|e| e - focus_off.map(|e| e as i32)),
921 scene_data.gamma,
922 scene_data.exposure,
923 self.last_lightning.unwrap_or((Vec3::zero(), -1000.0)),
924 self.wind_vel,
925 scene_data.ambiance,
926 self.camera.get_mode(),
927 scene_data.sprite_render_distance - 20.0,
928 )]);
929 renderer.update_clouds_locals(CloudsLocals::new(proj_mat_inv, view_mat_inv));
930 renderer.update_postprocess_locals(PostProcessLocals::new(proj_mat_inv, view_mat_inv));
931
932 self.lod.maintain(renderer, client, focus_pos, &self.camera);
934
935 self.tether_mgr.maintain(renderer, client, focus_pos);
937
938 self.debug.maintain(renderer);
940
941 let (
943 _visible_bounds,
944 visible_light_volume,
945 visible_psr_bounds,
946 visible_occlusion_volume,
947 visible_por_bounds,
948 ) = self.terrain.maintain(
949 renderer,
950 scene_data,
951 focus_pos,
952 self.loaded_distance,
953 &self.camera,
954 );
955
956 let _figure_bounds = self.figure_mgr.maintain(
958 renderer,
959 &mut self.trail_mgr,
960 scene_data,
961 visible_psr_bounds,
962 visible_por_bounds,
963 &self.camera,
964 Some(&self.terrain),
965 );
966
967 let fov = self.camera.get_effective_fov();
968 let aspect_ratio = self.camera.get_aspect_ratio();
969 let view_dir = ((focus_pos.map(f32::fract)) - cam_pos).normalized();
970
971 let look_at = cam_pos;
976 let new_dir = view_dir;
977 let new_dir = new_dir.normalized();
978 let up: math::Vec3<f32> = math::Vec3::unit_y();
979
980 let texture_mat = Mat4::<f32>::scaling_3d::<Vec3<f32>>(Vec3::new(0.5, -0.5, 1.0))
994 * Mat4::translation_3d(Vec3::new(1.0, -1.0, 0.0));
995
996 let directed_mats = |d_view_mat: math::Mat4<f32>,
997 d_dir: math::Vec3<f32>,
998 volume: &Vec<math::Vec3<f32>>|
999 -> (Mat4<f32>, Mat4<f32>) {
1000 let v_p_orig = math::Vec3::from(d_view_mat * math::Vec4::from_direction(new_dir));
1002 let mut v_p = v_p_orig.normalized();
1003 let cos_gamma = new_dir.map(f64::from).dot(d_dir.map(f64::from));
1004 let sin_gamma = (1.0 - cos_gamma * cos_gamma).sqrt();
1005 let gamma = sin_gamma.asin();
1006 let view_mat = math::Mat4::from_col_array(view_mat.into_col_array());
1007 let bounds1 = math::fit_psr(
1010 view_mat.map_cols(math::Vec4::from),
1011 volume.iter().copied(),
1012 math::Vec4::homogenized,
1013 );
1014 let n_e = f64::from(-bounds1.max.z);
1015 let factor = compute_warping_parameter_perspective(
1016 gamma,
1017 n_e,
1018 f64::from(fov),
1019 f64::from(aspect_ratio),
1020 );
1021
1022 v_p.z = 0.0;
1023 v_p.normalize();
1024 let l_r: math::Mat4<f32> = if factor > EPSILON_UPSILON {
1025 math::Mat4::look_at_lh(math::Vec3::zero(), math::Vec3::unit_z(), v_p)
1029 } else {
1030 math::Mat4::identity()
1031 };
1032 let directed_proj_mat = math::Mat4::new(
1034 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,
1035 );
1036
1037 let light_all_mat = l_r * directed_proj_mat * d_view_mat;
1038 let bounds0 = math::fit_psr(
1041 light_all_mat,
1042 volume.iter().copied(),
1043 math::Vec4::homogenized,
1044 );
1045 let (z_0, z_1) = {
1053 let f_e = f64::from(-bounds1.min.z).max(n_e);
1054 let p_z = bounds1.max.z;
1056 let p_y = bounds0.min.y;
1058 let p_x = bounds0.center().x;
1059 let view_inv = view_mat.inverted();
1061 let light_all_inv = light_all_mat.inverted();
1063
1064 let view_point = view_inv
1066 * math::Vec4::from_point(
1067 -math::Vec3::unit_z() * p_z, );
1069 let view_plane = view_mat.transposed() * -math::Vec4::unit_z();
1070
1071 let light_point = light_all_inv
1073 * math::Vec4::from_point(
1074 math::Vec3::unit_y() * p_y, );
1076 let light_plane = light_all_mat.transposed() * math::Vec4::unit_y();
1077
1078 let shadow_point = light_all_inv
1080 * math::Vec4::from_point(
1081 math::Vec3::unit_x() * p_x, );
1083 let shadow_plane = light_all_mat.transposed() * math::Vec4::unit_x();
1084
1085 let solve_p0 = math::Mat4::new(
1089 view_plane.x,
1090 view_plane.y,
1091 view_plane.z,
1092 0.0,
1093 light_plane.x,
1094 light_plane.y,
1095 light_plane.z,
1096 0.0,
1097 shadow_plane.x,
1098 shadow_plane.y,
1099 shadow_plane.z,
1100 0.0,
1101 0.0,
1102 0.0,
1103 0.0,
1104 1.0,
1105 );
1106
1107 let plane_dist = math::Vec4::new(
1109 view_plane.dot(view_point),
1110 light_plane.dot(light_point),
1111 shadow_plane.dot(shadow_point),
1112 1.0,
1113 );
1114 let p0_world = solve_p0.inverted() * plane_dist;
1115 let p0 = light_all_mat * p0_world;
1117 let mut p1 = p0;
1118 p1.y = bounds0.max.y;
1120
1121 let view_from_light_mat = view_mat * light_all_inv;
1124 let z0 = view_from_light_mat * p0;
1126 let z1 = view_from_light_mat * p1;
1127
1128 (
1133 f64::from(z0.homogenized().dot(-math::Vec4::unit_z())).clamp(n_e, f_e),
1134 f64::from(z1.homogenized().dot(-math::Vec4::unit_z())).clamp(n_e, f_e),
1135 )
1136 };
1137
1138 let mut light_focus_pos: math::Vec3<f32> = math::Vec3::zero();
1140 light_focus_pos.x = bounds0.center().x;
1141 light_focus_pos.y = bounds0.min.y;
1142 light_focus_pos.z = bounds0.center().z;
1143
1144 let d = f64::from(bounds0.max.y - bounds0.min.y).abs();
1145
1146 let w_l_y = d;
1147
1148 let alpha = z_1 / z_0;
1152 let alpha_sqrt = alpha.sqrt();
1153 let directed_near_normal = if factor < 0.0 {
1154 (1.0 + alpha_sqrt - factor * (alpha - 1.0)) / ((alpha - 1.0) * (factor + 1.0))
1156 } else {
1157 ((alpha_sqrt - 1.0) * (factor * alpha_sqrt + 1.0)).recip()
1159 };
1160
1161 let y_ = |v: f64| w_l_y * (v + directed_near_normal).abs();
1163 let directed_near = y_(0.0) as f32;
1164 let directed_far = y_(1.0) as f32;
1165 light_focus_pos.y = if factor > EPSILON_UPSILON {
1166 light_focus_pos.y - directed_near
1167 } else {
1168 light_focus_pos.y
1169 };
1170 let w_v: math::Mat4<f32> = math::Mat4::translation_3d(-math::Vec3::new(
1172 light_focus_pos.x,
1173 light_focus_pos.y,
1174 light_focus_pos.z,
1175 ));
1176 let shadow_view_mat: math::Mat4<f32> = w_v * light_all_mat;
1177 let w_p: math::Mat4<f32> = {
1178 if factor > EPSILON_UPSILON {
1179 let near = directed_near;
1181 let far = directed_far;
1182 let left = -1.0;
1183 let right = 1.0;
1184 let bottom = -1.0;
1185 let top = 1.0;
1186 let s_x = 2.0 * near / (right - left);
1187 let o_x = (right + left) / (right - left);
1188 let s_z = 2.0 * near / (top - bottom);
1189 let o_z = (top + bottom) / (top - bottom);
1190
1191 let s_y = (far + near) / (far - near);
1192 let o_y = -2.0 * far * near / (far - near);
1193
1194 math::Mat4::new(
1195 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,
1196 0.0,
1197 )
1198 } else {
1199 math::Mat4::identity()
1200 }
1201 };
1202
1203 let shadow_all_mat: math::Mat4<f32> = w_p * shadow_view_mat;
1204 let math::Aabb::<f32> {
1207 min:
1208 math::Vec3 {
1209 x: xmin,
1210 y: ymin,
1211 z: zmin,
1212 },
1213 max:
1214 math::Vec3 {
1215 x: xmax,
1216 y: ymax,
1217 z: zmax,
1218 },
1219 } = math::fit_psr(
1220 shadow_all_mat,
1221 volume.iter().copied(),
1222 math::Vec4::homogenized,
1223 );
1224 let s_x = 2.0 / (xmax - xmin);
1225 let s_y = 2.0 / (ymax - ymin);
1226 let s_z = 1.0 / (zmax - zmin);
1227 let o_x = -(xmax + xmin) / (xmax - xmin);
1228 let o_y = -(ymax + ymin) / (ymax - ymin);
1229 let o_z = -zmin / (zmax - zmin);
1230 let directed_proj_mat = Mat4::new(
1231 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,
1232 );
1233
1234 let shadow_all_mat: Mat4<f32> = Mat4::from_col_arrays(shadow_all_mat.into_col_arrays());
1235
1236 let directed_texture_proj_mat = texture_mat * directed_proj_mat;
1237 (
1238 directed_proj_mat * shadow_all_mat,
1239 directed_texture_proj_mat * shadow_all_mat,
1240 )
1241 };
1242
1243 let weather = client
1244 .state()
1245 .max_weather_near(focus_off.xy() + cam_pos.xy());
1246 self.wind_vel = weather.wind_vel();
1247 if weather.rain > RAIN_THRESHOLD {
1248 let weather = client.weather_at_player();
1249 let rain_vel = weather.rain_vel();
1250 let rain_view_mat = math::Mat4::look_at_rh(look_at, look_at + rain_vel, up);
1251
1252 self.integrated_rain_vel += rain_vel.magnitude() * dt;
1253 let rain_dir_mat = Mat4::rotation_from_to_3d(-Vec3::unit_z(), rain_vel);
1254
1255 let (shadow_mat, texture_mat) =
1256 directed_mats(rain_view_mat, rain_vel, &visible_occlusion_volume);
1257
1258 let rain_occlusion_locals = RainOcclusionLocals::new(
1259 shadow_mat,
1260 texture_mat,
1261 rain_dir_mat,
1262 weather.rain,
1263 self.integrated_rain_vel,
1264 );
1265
1266 renderer.update_consts(&mut self.data.rain_occlusion_mats, &[rain_occlusion_locals]);
1267 } else if self.integrated_rain_vel > 0.0 {
1268 self.integrated_rain_vel = 0.0;
1269 let rain_occlusion_locals = RainOcclusionLocals::default();
1271 renderer.update_consts(&mut self.data.rain_occlusion_mats, &[rain_occlusion_locals]);
1272 }
1273
1274 let sun_dir = scene_data.get_sun_dir();
1275 let is_daylight = sun_dir.z < 0.0;
1276 if renderer.pipeline_modes().shadow.is_map() && (is_daylight || !lights.is_empty()) {
1277 let (point_shadow_res, _directed_shadow_res) = renderer.get_shadow_resolution();
1278 let point_shadow_aspect = point_shadow_res.x as f32 / point_shadow_res.y as f32;
1281 let directed_light_dir = sun_dir;
1284
1285 let mut directed_shadow_mats = Vec::with_capacity(6);
1288
1289 let light_view_mat = math::Mat4::look_at_rh(look_at, look_at + directed_light_dir, up);
1290 let (shadow_mat, texture_mat) =
1291 directed_mats(light_view_mat, directed_light_dir, &visible_light_volume);
1292
1293 let shadow_locals = ShadowLocals::new(shadow_mat, texture_mat);
1294
1295 renderer.update_consts(&mut self.data.shadow_mats, &[shadow_locals]);
1296
1297 directed_shadow_mats.push(light_view_mat);
1298 directed_shadow_mats
1300 .extend_from_slice(&[math::Mat4::default(); 6 - NUM_DIRECTED_LIGHTS] as _);
1301 let mut shadow_mats = Vec::with_capacity(6 * (lights.len() + 1));
1304 shadow_mats.resize_with(6, PointLightMatrix::default);
1305 let shadow_proj = camera::perspective_rh_zo_general(
1311 90.0f32.to_radians(),
1312 point_shadow_aspect,
1313 1.0 / SHADOW_NEAR,
1314 1.0 / SHADOW_FAR,
1315 );
1316 let shadow_proj = shadow_proj * Mat4::scaling_3d(-1.0);
1321
1322 let orientations = [
1325 (Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, -1.0, 0.0)),
1326 (Vec3::new(-1.0, 0.0, 0.0), Vec3::new(0.0, -1.0, 0.0)),
1327 (Vec3::new(0.0, 1.0, 0.0), Vec3::new(0.0, 0.0, 1.0)),
1328 (Vec3::new(0.0, -1.0, 0.0), Vec3::new(0.0, 0.0, -1.0)),
1329 (Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, -1.0, 0.0)),
1330 (Vec3::new(0.0, 0.0, -1.0), Vec3::new(0.0, -1.0, 0.0)),
1331 ];
1332
1333 shadow_mats.extend(lights.iter().flat_map(|light| {
1337 let eye = Vec3::new(light.pos[0], light.pos[1], light.pos[2]) - focus_off;
1340 orientations.iter().map(move |&(forward, up)| {
1341 PointLightMatrix::new(shadow_proj * Mat4::look_at_lh(eye, eye + forward, up))
1344 })
1345 }));
1346
1347 for (i, val) in shadow_mats.into_iter().enumerate() {
1348 self.data.point_light_matrices[i] = val
1349 }
1350 }
1351
1352 self.figure_mgr.clean(scene_data.tick);
1354
1355 self.sfx_mgr.maintain(
1357 audio,
1358 scene_data.state,
1359 scene_data.viewpoint_entity,
1360 &self.camera,
1361 &self.terrain,
1362 client,
1363 );
1364
1365 self.ambience_mgr
1366 .maintain(audio, scene_data.state, client, &self.camera);
1367
1368 self.music_mgr.maintain(audio, scene_data.state, client);
1369 }
1370
1371 pub fn global_bind_group(&self) -> &GlobalsBindGroup { &self.globals_bind_group }
1372
1373 pub fn render(
1375 &self,
1376 drawer: &mut Drawer<'_>,
1377 state: &State,
1378 viewpoint_entity: EcsEntity,
1379 tick: u64,
1380 scene_data: &SceneData,
1381 ) {
1382 span!(_guard, "render", "Scene::render");
1383 let sun_dir = scene_data.get_sun_dir();
1384 let is_daylight = sun_dir.z < 0.0;
1385 let focus_pos = self.camera.get_focus_pos();
1386 let cam_pos = self.camera.dependents().cam_pos + focus_pos.map(|e| e.trunc());
1387 let is_rain = state.max_weather_near(cam_pos.xy()).rain > RAIN_THRESHOLD;
1388 let culling_mode = if scene_data
1389 .state
1390 .terrain()
1391 .get_key(scene_data.state.terrain().pos_key(cam_pos.as_()))
1392 .is_some_and(|c| cam_pos.z < c.meta().alt() - terrain::UNDERGROUND_ALT)
1393 {
1394 CullingMode::Underground
1395 } else {
1396 CullingMode::Surface
1397 };
1398
1399 let camera_data = (&self.camera, scene_data.figure_lod_render_distance);
1400
1401 if drawer.pipeline_modes().shadow.is_map() && (is_daylight || !self.light_data.is_empty()) {
1403 if is_daylight {
1404 prof_span!("directed shadows");
1405 if let Some(mut shadow_pass) = drawer.shadow_pass() {
1406 self.terrain.render_shadows(
1408 &mut shadow_pass.draw_terrain_shadows(),
1409 focus_pos,
1410 culling_mode,
1411 );
1412
1413 self.figure_mgr.render_shadows(
1415 &mut shadow_pass.draw_figure_shadows(),
1416 state,
1417 tick,
1418 camera_data,
1419 );
1420 self.debug
1421 .render_shadows(&mut shadow_pass.draw_debug_shadows());
1422 }
1423 }
1424
1425 {
1427 prof_span!("point shadows");
1428 drawer.draw_point_shadows(
1429 &self.data.point_light_matrices,
1430 self.terrain.chunks_for_point_shadows(focus_pos),
1431 )
1432 }
1433 }
1434 if is_rain {
1436 prof_span!("rain occlusion");
1437 if let Some(mut occlusion_pass) = drawer.rain_occlusion_pass() {
1438 self.terrain
1439 .render_rain_occlusion(&mut occlusion_pass.draw_terrain_shadows(), cam_pos);
1440
1441 self.figure_mgr.render_rain_occlusion(
1442 &mut occlusion_pass.draw_figure_shadows(),
1443 state,
1444 tick,
1445 camera_data,
1446 );
1447 }
1448 }
1449
1450 prof_span!(guard, "main pass");
1451 if let Some(mut first_pass) = drawer.first_pass() {
1452 self.figure_mgr.render_viewpoint(
1453 &mut first_pass.draw_figures(),
1454 state,
1455 viewpoint_entity,
1456 tick,
1457 camera_data,
1458 );
1459
1460 self.terrain
1461 .render(&mut first_pass, focus_pos, culling_mode);
1462
1463 self.figure_mgr.render(
1464 &mut first_pass.draw_figures(),
1465 state,
1466 viewpoint_entity,
1467 tick,
1468 camera_data,
1469 );
1470
1471 self.lod.render(&mut first_pass, culling_mode);
1472
1473 first_pass.draw_skybox(&self.skybox.model);
1475
1476 let mut sprite_drawer = first_pass.draw_sprites(
1478 &self.terrain.sprite_globals,
1479 &self.terrain.sprite_render_state.sprite_atlas_textures,
1480 );
1481 self.figure_mgr.render_sprites(
1482 &mut sprite_drawer,
1483 state,
1484 cam_pos,
1485 scene_data.sprite_render_distance,
1486 );
1487 self.terrain.render_sprites(
1488 &mut sprite_drawer,
1489 focus_pos,
1490 cam_pos,
1491 scene_data.sprite_render_distance,
1492 culling_mode,
1493 );
1494 drop(sprite_drawer);
1495
1496 self.tether_mgr.render(&mut first_pass);
1498
1499 self.terrain.render_translucent(&mut first_pass, focus_pos);
1501
1502 self.particle_mgr
1504 .render(&mut first_pass.draw_particles(), scene_data);
1505
1506 self.debug.render(&mut first_pass.draw_debug());
1508 }
1509 drop(guard);
1510 }
1511
1512 pub fn maintain_debug_hitboxes(
1513 &mut self,
1514 client: &Client,
1515 settings: &Settings,
1516 hitboxes: &mut HashMap<specs::Entity, DebugShapeId>,
1517 tracks: &mut HashMap<Vec2<i32>, Vec<DebugShapeId>>,
1518 ) {
1519 let ecs = client.state().ecs();
1520 {
1521 let mut current_chunks = hashbrown::HashSet::new();
1522 let terrain_grid = ecs.read_resource::<TerrainGrid>();
1523 for (key, chunk) in terrain_grid.iter() {
1524 current_chunks.insert(key);
1525 tracks.entry(key).or_insert_with(|| {
1526 let mut ret = Vec::new();
1527 for bezier in chunk.meta().tracks().iter() {
1528 let shape_id = self.debug.add_shape(DebugShape::TrainTrack {
1529 path: *bezier,
1530 rail_width: 0.25,
1531 rail_sep: 1.0,
1532 plank_width: 0.5,
1533 plank_height: 0.125,
1534 plank_sep: 2.0,
1535 });
1536 ret.push(shape_id);
1537 self.debug
1538 .set_context(shape_id, [0.0; 4], [1.0; 4], [0.0, 0.0, 0.0, 1.0]);
1539 }
1540 for point in chunk.meta().debug_points().iter() {
1541 let shape_id = self.debug.add_shape(DebugShape::Cylinder {
1542 radius: 0.1,
1543 height: 0.1,
1544 });
1545 ret.push(shape_id);
1546 self.debug.set_context(
1547 shape_id,
1548 point.with_w(0.0).into_array(),
1549 [1.0; 4],
1550 [0.0, 0.0, 0.0, 1.0],
1551 );
1552 }
1553 for line in chunk.meta().debug_lines().iter() {
1554 let shape_id = self
1555 .debug
1556 .add_shape(DebugShape::Line([line.start, line.end], 0.1));
1557 ret.push(shape_id);
1558 self.debug
1559 .set_context(shape_id, [0.0; 4], [1.0; 4], [0.0, 0.0, 0.0, 1.0]);
1560 }
1561 ret
1562 });
1563 }
1564 tracks.retain(|k, v| {
1565 let keep = current_chunks.contains(k);
1566 if !keep {
1567 for shape in v.iter() {
1568 self.debug.remove_shape(*shape);
1569 }
1570 }
1571 keep
1572 });
1573 }
1574 let mut current_entities = hashbrown::HashSet::new();
1575 if settings.interface.toggle_hitboxes {
1576 let positions = ecs.read_component::<comp::Pos>();
1577 let colliders = ecs.read_component::<comp::Collider>();
1578 let orientations = ecs.read_component::<comp::Ori>();
1579 let scales = ecs.read_component::<comp::Scale>();
1580 let groups = ecs.read_component::<comp::Group>();
1581 for (entity, pos, collider, ori, scale, group) in (
1582 &ecs.entities(),
1583 &positions,
1584 &colliders,
1585 &orientations,
1586 scales.maybe(),
1587 groups.maybe(),
1588 )
1589 .join()
1590 {
1591 match collider {
1592 comp::Collider::CapsulePrism {
1593 p0,
1594 p1,
1595 radius,
1596 z_min,
1597 z_max,
1598 } => {
1599 let scale = scale.map_or(1.0, |s| s.0);
1600 current_entities.insert(entity);
1601
1602 let shape = DebugShape::CapsulePrism {
1603 p0: *p0 * scale,
1604 p1: *p1 * scale,
1605 radius: *radius * scale,
1606 height: (*z_max - *z_min) * scale,
1607 };
1608
1609 if let Some(shape_id) = hitboxes.get(&entity) {
1611 if self.debug.get_shape(*shape_id).is_some_and(|s| s != &shape) {
1612 self.debug.remove_shape(*shape_id);
1613 hitboxes.remove(&entity);
1614 }
1615 }
1616
1617 let shape_id = hitboxes
1618 .entry(entity)
1619 .or_insert_with(|| self.debug.add_shape(shape));
1620 let hb_pos = [pos.0.x, pos.0.y, pos.0.z + *z_min * scale, 0.0];
1621 let color = if group == Some(&comp::group::ENEMY) {
1622 [1.0, 0.0, 0.0, 0.5]
1623 } else if group == Some(&comp::group::NPC) {
1624 [0.0, 0.0, 1.0, 0.5]
1625 } else {
1626 [0.0, 1.0, 0.0, 0.5]
1627 };
1628 let ori = ori.to_quat();
1630 let hb_ori = [ori.x, ori.y, ori.z, ori.w];
1631 self.debug.set_context(*shape_id, hb_pos, color, hb_ori);
1632 },
1633 comp::Collider::Voxel { .. }
1634 | comp::Collider::Volume(_)
1635 | comp::Collider::Point => {
1636 },
1638 }
1639 }
1640 }
1641 hitboxes.retain(|k, v| {
1642 let keep = current_entities.contains(k);
1643 if !keep {
1644 self.debug.remove_shape(*v);
1645 }
1646 keep
1647 });
1648 }
1649
1650 pub fn maintain_debug_vectors(&mut self, client: &Client, lines: &mut PlayerDebugLines) {
1651 lines
1652 .chunk_normal
1653 .take()
1654 .map(|id| self.debug.remove_shape(id));
1655 lines.fluid_vel.take().map(|id| self.debug.remove_shape(id));
1656 lines.wind.take().map(|id| self.debug.remove_shape(id));
1657 lines.vel.take().map(|id| self.debug.remove_shape(id));
1658 if self.debug_vectors_enabled {
1659 let ecs = client.state().ecs();
1660
1661 let vels = &ecs.read_component::<comp::Vel>();
1662 let Some(vel) = vels.get(client.entity()) else {
1663 return;
1664 };
1665
1666 let phys_states = &ecs.read_component::<comp::PhysicsState>();
1667 let Some(phys) = phys_states.get(client.entity()) else {
1668 return;
1669 };
1670
1671 let positions = &ecs.read_component::<comp::Pos>();
1672 let Some(pos) = positions.get(client.entity()) else {
1673 return;
1674 };
1675
1676 let weather = ecs.read_resource::<WeatherGrid>();
1677 const LINE_WIDTH: f32 = 0.05;
1680 {
1682 let Some(fluid) = phys.in_fluid else {
1683 return;
1684 };
1685 let shape = DebugShape::Line([pos.0, pos.0 + fluid.flow_vel().0 / 2.], LINE_WIDTH);
1686 let id = self.debug.add_shape(shape);
1687 lines.fluid_vel = Some(id);
1688 self.debug
1689 .set_context(id, [0.0; 4], [0.18, 0.72, 0.87, 0.8], [0.0, 0.0, 0.0, 1.0]);
1690 }
1691 {
1693 let Some(chunk) = client.current_chunk() else {
1694 return;
1695 };
1696 let shape = DebugShape::Line(
1697 [
1698 pos.0,
1699 pos.0
1700 + chunk
1701 .meta()
1702 .approx_chunk_terrain_normal()
1703 .unwrap_or(Vec3::unit_z())
1704 * 2.5,
1705 ],
1706 LINE_WIDTH,
1707 );
1708 let id = self.debug.add_shape(shape);
1709 lines.chunk_normal = Some(id);
1710 self.debug
1711 .set_context(id, [0.0; 4], [0.22, 0.63, 0.1, 0.8], [0.0, 0.0, 0.0, 1.0]);
1712 }
1713 {
1715 let wind = weather.get_interpolated(pos.0.xy()).wind_vel();
1716 let shape = DebugShape::Line([pos.0, pos.0 + wind * 5.0], LINE_WIDTH);
1717 let id = self.debug.add_shape(shape);
1718 lines.wind = Some(id);
1719 self.debug
1720 .set_context(id, [0.0; 4], [0.76, 0.76, 0.76, 0.8], [0.0, 0.0, 0.0, 1.0]);
1721 }
1722 {
1724 let shape = DebugShape::Line([pos.0, pos.0 + vel.0 / 2.0], LINE_WIDTH);
1725 let id = self.debug.add_shape(shape);
1726 lines.vel = Some(id);
1727 self.debug
1728 .set_context(id, [0.0; 4], [0.98, 0.76, 0.01, 0.8], [0.0, 0.0, 0.0, 1.0]);
1729 }
1730 }
1731 }
1732}