1mod sprite;
2mod watcher;
3
4pub use self::watcher::{BlocksOfInterest, FireplaceType, Interaction};
5use sprite::{FilteredSpriteData, SpriteData, SpriteModelData, SpriteSpec};
6
7use crate::{
8 mesh::{
9 greedy::{GreedyMesh, SpriteAtlasAllocator},
10 segment::generate_mesh_base_vol_sprite,
11 terrain::{SUNLIGHT, SUNLIGHT_INV, generate_mesh},
12 },
13 render::{
14 AltIndices, CullingMode, FigureSpriteAtlasData, FirstPassDrawer, FluidVertex, GlobalModel,
15 Instances, LodData, Mesh, Model, RenderError, Renderer, SPRITE_VERT_PAGE_SIZE,
16 SpriteDrawer, SpriteGlobalsBindGroup, SpriteInstance, SpriteVertex, SpriteVerts,
17 TerrainAtlasData, TerrainLocals, TerrainShadowDrawer, TerrainVertex,
18 pipelines::{self, AtlasData, AtlasTextures},
19 },
20 scene::terrain::sprite::SpriteModelConfig,
21};
22
23use super::{
24 RAIN_THRESHOLD, SceneData,
25 camera::{self, Camera},
26 math,
27};
28use common::{
29 assets::{AssetExt, DotVoxAsset},
30 figure::Segment,
31 spiral::Spiral2d,
32 terrain::{Block, SpriteKind, TerrainChunk},
33 vol::{BaseVol, ReadVol, RectRasterableVol, SampleVol},
34 volumes::vol_grid_2d::{VolGrid2d, VolGrid2dError},
35};
36use common_base::{prof_span, span};
37use core::{f32, fmt::Debug, marker::PhantomData, time::Duration};
38use crossbeam_channel as channel;
39use guillotiere::AtlasAllocator;
40use hashbrown::HashMap;
41use std::sync::{
42 Arc,
43 atomic::{AtomicU64, Ordering},
44};
45use tracing::warn;
46use treeculler::{AABB, BVol, Frustum};
47use vek::*;
48
49const SPRITE_SCALE: Vec3<f32> = Vec3::new(1.0 / 11.0, 1.0 / 11.0, 1.0 / 11.0);
50pub const SPRITE_LOD_LEVELS: usize = 5;
51
52pub const RAIN_OCCLUSION_CHUNKS: usize = 25;
55
56#[derive(Clone, Copy, Debug)]
57struct Visibility {
58 in_range: bool,
59 in_frustum: bool,
60}
61
62impl Visibility {
63 fn is_visible(&self) -> bool {
65 self.in_frustum
72 }
73}
74
75type LightMapFn = Arc<dyn Fn(Vec3<i32>) -> f32 + Send + Sync>;
77
78pub struct TerrainChunkData {
79 load_time: f32,
81 opaque_model: Option<Model<TerrainVertex>>,
82 fluid_model: Option<Model<FluidVertex>>,
83 atlas_alloc: Option<guillotiere::AllocId>,
86 atlas_textures: Arc<AtlasTextures<pipelines::terrain::Locals, TerrainAtlasData>>,
93 light_map: LightMapFn,
94 glow_map: LightMapFn,
95 sprite_instances: [(Instances<SpriteInstance>, AltIndices); SPRITE_LOD_LEVELS],
96 locals: pipelines::terrain::BoundLocals,
97 pub blocks_of_interest: BlocksOfInterest,
98
99 visible: Visibility,
100 can_shadow_point: bool,
101 can_shadow_sun: bool,
102 z_bounds: (f32, f32),
103 sun_occluder_z_bounds: (f32, f32),
104 frustum_last_plane_index: u8,
105
106 alt_indices: AltIndices,
107}
108
109pub const SHALLOW_ALT: f32 = 24.0;
112pub const DEEP_ALT: f32 = 96.0;
115pub const UNDERGROUND_ALT: f32 = (SHALLOW_ALT + DEEP_ALT) * 0.5;
118
119const NEVER_CULL_DIST: i32 = 3;
122
123#[derive(Copy, Clone)]
124struct ChunkMeshState {
125 pos: Vec2<i32>,
126 started_tick: u64,
127 is_worker_active: bool,
128 skip_remesh: bool,
130}
131
132pub struct MeshWorkerResponseMesh {
134 z_bounds: (f32, f32),
135 sun_occluder_z_bounds: (f32, f32),
136 opaque_mesh: Mesh<TerrainVertex>,
137 fluid_mesh: Mesh<FluidVertex>,
138 atlas_texture_data: TerrainAtlasData,
139 atlas_size: Vec2<u16>,
140 light_map: LightMapFn,
141 glow_map: LightMapFn,
142 alt_indices: AltIndices,
143}
144
145struct MeshWorkerResponse {
148 pos: Vec2<i32>,
149 sprite_instances: [(Vec<SpriteInstance>, AltIndices); SPRITE_LOD_LEVELS],
150 mesh: Option<MeshWorkerResponseMesh>,
152 started_tick: u64,
153 blocks_of_interest: BlocksOfInterest,
154}
155
156pub(super) fn get_sprite_instances<'a, I: 'a>(
157 lod_levels: &'a mut [I; SPRITE_LOD_LEVELS],
158 set_instance: impl Fn(&mut I, SpriteInstance, Vec3<i32>),
159 blocks: impl Iterator<Item = (Vec3<f32>, Block)>,
160 mut to_wpos: impl FnMut(Vec3<f32>) -> Vec3<i32>,
161 mut light_map: impl FnMut(Vec3<i32>) -> f32,
162 mut glow_map: impl FnMut(Vec3<i32>) -> f32,
163 sprite_data: &HashMap<SpriteKind, FilteredSpriteData>,
164 missing_sprite_placeholder: &SpriteData,
165) {
166 prof_span!("extract sprite_instances");
167 for (rel_pos, block) in blocks {
168 let Some(sprite) = block.get_sprite() else {
169 continue;
170 };
171 if matches!(sprite, SpriteKind::Empty) {
174 continue;
175 }
176
177 let data = sprite_data
178 .get(&sprite)
179 .and_then(|filtered| filtered.for_block(&block))
180 .unwrap_or(missing_sprite_placeholder);
181
182 if data.variations.is_empty() {
183 continue;
184 }
185
186 let wpos = to_wpos(rel_pos);
187 let seed = (wpos.x as u64)
188 .wrapping_mul(3)
189 .wrapping_add((wpos.y as u64).wrapping_mul(7))
190 .wrapping_add((wpos.x as u64).wrapping_mul(wpos.y as u64)); let rot = block
193 .sprite_z_rot()
194 .unwrap_or((seed % 17 / 4).min(3) as f32 / 2.0 * std::f32::consts::PI);
199 let mirror = block.sprite_mirror_vec();
200 let variation = match data.variations.len() {
202 1 => 0,
203 2 => (seed as usize % 4) / 3,
204 3 => (seed as usize % 5) / 2,
205 4 => ((seed.wrapping_add(wpos.x as u64)) as usize % 7).div_ceil(2),
207 _ => seed as usize % data.variations.len(),
208 };
209 let variant = &data.variations[variation];
210
211 let light = light_map(wpos);
212 let glow = glow_map(wpos);
213
214 for (lod_level, model_data) in lod_levels.iter_mut().zip(variant) {
215 let mat = Mat4::identity()
217 .scaled_3d(model_data.scale)
219 .translated_3d(model_data.offset)
221 .scaled_3d(SPRITE_SCALE * mirror)
222 .rotated_z(rot)
223 .translated_3d(
224 rel_pos + Vec3::new(0.5, 0.5, 0.0)
225 );
226 for page in model_data.vert_pages.clone() {
228 let instance = SpriteInstance::new(
231 mat,
232 data.wind_sway,
233 model_data.scale.z,
234 rel_pos.as_(),
235 light,
236 glow,
237 page,
238 sprite.is_door(),
239 mirror.map(|e| (e < 0.0) as u32).sum() % 2 == 1,
240 );
241 set_instance(lod_level, instance, wpos);
242 }
243 }
244 }
245}
246
247fn mesh_worker(
252 pos: Vec2<i32>,
253 z_bounds: (f32, f32),
254 skip_remesh: Option<(LightMapFn, LightMapFn)>,
255 started_tick: u64,
256 volume: <VolGrid2d<TerrainChunk> as SampleVol<Aabr<i32>>>::Sample,
257 max_texture_size: u16,
258 chunk: Arc<TerrainChunk>,
259 range: Aabb<i32>,
260 sprite_render_state: &SpriteRenderState,
261) -> MeshWorkerResponse {
262 span!(_guard, "mesh_worker");
263 let blocks_of_interest = BlocksOfInterest::from_blocks(
264 chunk.iter_changed().map(|(pos, block)| (pos, *block)),
265 chunk.meta().river_velocity(),
266 chunk.meta().temp(),
267 chunk.meta().humidity(),
268 &*chunk,
269 );
270
271 let mesh;
272 let (light_map, glow_map) = if let Some((light_map, glow_map)) = &skip_remesh {
273 mesh = None;
274 (&**light_map, &**glow_map)
275 } else {
276 let (
277 opaque_mesh,
278 fluid_mesh,
279 _shadow_mesh,
280 (
281 bounds,
282 atlas_texture_data,
283 atlas_size,
284 light_map,
285 glow_map,
286 alt_indices,
287 sun_occluder_z_bounds,
288 ),
289 ) = generate_mesh(
290 &volume,
291 (
292 range,
293 Vec2::new(max_texture_size, max_texture_size),
294 &blocks_of_interest,
295 ),
296 );
297 mesh = Some(MeshWorkerResponseMesh {
298 z_bounds: (bounds.min.z, bounds.max.z),
300 sun_occluder_z_bounds,
301 opaque_mesh,
302 fluid_mesh,
303 atlas_texture_data,
304 atlas_size,
305 light_map,
306 glow_map,
307 alt_indices,
308 });
309 let mesh = mesh.as_ref().unwrap();
311 (&*mesh.light_map, &*mesh.glow_map)
312 };
313 let to_wpos = |rel_pos: Vec3<f32>| {
314 Vec3::from(pos * TerrainChunk::RECT_SIZE.map(|e: u32| e as i32)) + rel_pos.as_()
315 };
316 MeshWorkerResponse {
317 pos,
318 sprite_instances: {
320 prof_span!("extract sprite_instances");
321 let mut instances = [(); SPRITE_LOD_LEVELS].map(|()| {
322 (
323 Vec::new(), Vec::new(), Vec::new(), )
327 });
328
329 let (underground_alt, deep_alt) = volume
330 .get_key(volume.pos_key((range.min + range.max) / 2))
331 .map_or((0.0, 0.0), |c| {
332 (c.meta().alt() - SHALLOW_ALT, c.meta().alt() - DEEP_ALT)
333 });
334
335 get_sprite_instances(
336 &mut instances,
337 |(deep_level, shallow_level, surface_level), instance, wpos| {
338 if (wpos.z as f32) < deep_alt {
339 deep_level.push(instance);
340 } else if wpos.z as f32 > underground_alt {
341 surface_level.push(instance);
342 } else {
343 shallow_level.push(instance);
344 }
345 },
346 (0..TerrainChunk::RECT_SIZE.x as i32)
347 .flat_map(|x| {
348 (0..TerrainChunk::RECT_SIZE.y as i32).flat_map(move |y| {
349 (z_bounds.0 as i32..z_bounds.1 as i32)
350 .map(move |z| Vec3::new(x, y, z).as_())
351 })
352 })
353 .filter_map(|rel_pos| Some((rel_pos, *volume.get(to_wpos(rel_pos)).ok()?))),
354 to_wpos,
355 light_map,
356 glow_map,
357 &sprite_render_state.sprite_data,
358 &sprite_render_state.missing_sprite_placeholder,
359 );
360
361 instances.map(|(deep_level, shallow_level, surface_level)| {
362 let deep_end = deep_level.len();
363 let alt_indices = AltIndices {
364 deep_end,
365 underground_end: deep_end + shallow_level.len(),
366 };
367 (
368 deep_level
369 .into_iter()
370 .chain(shallow_level)
371 .chain(surface_level)
372 .collect(),
373 alt_indices,
374 )
375 })
376 },
377 mesh,
378 blocks_of_interest,
379 started_tick,
380 }
381}
382
383pub struct Terrain<V: RectRasterableVol = TerrainChunk> {
384 atlas: AtlasAllocator,
398 chunks: HashMap<Vec2<i32>, TerrainChunkData>,
399 shadow_chunks: Vec<(Vec2<i32>, TerrainChunkData)>,
410 mesh_send_tmp: channel::Sender<MeshWorkerResponse>,
420 mesh_recv: channel::Receiver<MeshWorkerResponse>,
421 mesh_todo: HashMap<Vec2<i32>, ChunkMeshState>,
422 mesh_todos_active: Arc<AtomicU64>,
423 mesh_recv_overflow: f32,
424
425 pub(super) sprite_render_state: Arc<SpriteRenderState>,
428 pub(super) sprite_globals: SpriteGlobalsBindGroup,
429 atlas_textures: Arc<AtlasTextures<pipelines::terrain::Locals, TerrainAtlasData>>,
434
435 phantom: PhantomData<V>,
436}
437
438impl TerrainChunkData {
439 pub fn can_shadow_sun(&self) -> bool { self.visible.is_visible() || self.can_shadow_sun }
440}
441
442pub(crate) struct SpriteRenderState {
443 pub(super) sprite_data: HashMap<SpriteKind, FilteredSpriteData>,
450 pub(super) missing_sprite_placeholder: SpriteData,
451 pub(super) sprite_atlas_textures:
452 AtlasTextures<pipelines::sprite::Locals, FigureSpriteAtlasData>,
453}
454
455#[derive(Clone)]
456pub struct SpriteRenderContext {
457 pub(super) state: Arc<SpriteRenderState>,
458 pub(super) sprite_verts_buffer: Arc<SpriteVerts>,
459}
460
461pub type SpriteRenderContextLazy = Box<dyn FnMut(&mut Renderer) -> SpriteRenderContext>;
462
463impl SpriteRenderContext {
464 pub fn new(renderer: &mut Renderer) -> SpriteRenderContextLazy {
465 let max_texture_size = renderer.max_texture_size();
466
467 struct SpriteWorkerResponse {
468 sprite_data: HashMap<SpriteKind, FilteredSpriteData>,
470 missing_sprite_placeholder: SpriteData,
471 sprite_atlas_texture_data: FigureSpriteAtlasData,
472 sprite_atlas_size: Vec2<u16>,
473 sprite_mesh: Mesh<SpriteVertex>,
474 }
475
476 let join_handle = std::thread::spawn(move || {
477 prof_span!("mesh all sprites");
478 let sprite_config =
480 Arc::<SpriteSpec>::load_expect("voxygen.voxel.sprite_manifest").cloned();
481
482 let max_size = Vec2::from(u16::try_from(max_texture_size).unwrap_or(u16::MAX));
483 let mut greedy = GreedyMesh::<FigureSpriteAtlasData, SpriteAtlasAllocator>::new(
484 max_size,
485 crate::mesh::greedy::sprite_config(),
486 );
487 let mut sprite_mesh = Mesh::new();
488
489 let mut config_to_data = |sprite_model_config: &_| {
490 let SpriteModelConfig {
491 model,
492 offset,
493 lod_axes,
494 } = sprite_model_config;
495 let scaled = [1.0, 0.8, 0.6, 0.4, 0.2];
496 let offset = Vec3::from(*offset);
497 let lod_axes = Vec3::from(*lod_axes);
498 let model = DotVoxAsset::load_expect(model);
499 let zero = Vec3::zero();
500 let model = &model.read().0;
501 let model_size = if let Some(model) = model.models.first() {
502 let dot_vox::Size { x, y, z } = model.size;
503 Vec3::new(x, y, z)
504 } else {
505 zero
506 };
507 let max_model_size = Vec3::new(31.0, 31.0, 63.0);
508 let model_scale = max_model_size.map2(model_size, |max_sz: f32, cur_sz| {
509 let scale = max_sz / max_sz.max(cur_sz as f32);
510 if scale < 1.0 && (cur_sz as f32 * scale).ceil() > max_sz {
511 scale - 0.001
512 } else {
513 scale
514 }
515 });
516 prof_span!(guard, "mesh sprite");
517 let lod_sprite_data = scaled.map(|lod_scale_orig| {
518 let lod_scale = model_scale
519 * if lod_scale_orig == 1.0 {
520 Vec3::broadcast(1.0)
521 } else {
522 lod_axes * lod_scale_orig
523 + lod_axes.map(|e| if e == 0.0 { 1.0 } else { 0.0 })
524 };
525
526 let start_page_num =
528 sprite_mesh.vertices().len() / SPRITE_VERT_PAGE_SIZE as usize;
529 generate_mesh_base_vol_sprite(
532 Segment::from_vox_model_index(model, 0).scaled_by(lod_scale),
533 (&mut greedy, &mut sprite_mesh, false),
534 offset.map(|e: f32| e.floor()) * lod_scale,
535 );
536 let end_page_num = sprite_mesh
538 .vertices()
539 .len()
540 .div_ceil(SPRITE_VERT_PAGE_SIZE as usize);
541 sprite_mesh.vertices_mut_vec().resize_with(
543 end_page_num * SPRITE_VERT_PAGE_SIZE as usize,
544 SpriteVertex::default,
545 );
546
547 let sprite_scale = Vec3::one() / lod_scale;
548
549 SpriteModelData {
550 vert_pages: start_page_num as u32..end_page_num as u32,
551 scale: sprite_scale,
552 offset: offset.map(|e| e.rem_euclid(1.0)),
553 }
554 });
555 drop(guard);
556
557 lod_sprite_data
558 };
559
560 let sprite_data = sprite_config.map_to_data(&mut config_to_data);
561
562 let missing_sprite_placeholder = SpriteData {
564 variations: vec![config_to_data(&SpriteModelConfig {
565 model: "voxygen.voxel.not_found".into(),
566 offset: (-5.5, -5.5, 0.0),
567 lod_axes: (1.0, 1.0, 1.0),
568 })]
569 .into(),
570 wind_sway: 1.0,
571 };
572
573 let (sprite_atlas_texture_data, sprite_atlas_size) = {
574 prof_span!("finalize");
575 greedy.finalize()
576 };
577
578 SpriteWorkerResponse {
579 sprite_data,
581 missing_sprite_placeholder,
582 sprite_atlas_texture_data,
583 sprite_atlas_size,
584 sprite_mesh,
585 }
586 });
587
588 let init = core::cell::OnceCell::new();
589 let mut join_handle = Some(join_handle);
590 let mut closure = move |renderer: &mut Renderer| {
591 let SpriteWorkerResponse {
596 sprite_data,
598 missing_sprite_placeholder,
599 sprite_atlas_texture_data,
600 sprite_atlas_size,
601 sprite_mesh,
602 } = join_handle
603 .take()
604 .expect(
605 "Closure should only be called once (in a `OnceCell::get_or_init`) in the \
606 absence of caught panics!",
607 )
608 .join()
609 .unwrap();
610
611 let [sprite_col_lights] =
612 sprite_atlas_texture_data.create_textures(renderer, sprite_atlas_size);
613 let sprite_atlas_textures = renderer.sprite_bind_atlas_textures(sprite_col_lights);
614
615 let sprite_verts_buffer = renderer.create_sprite_verts(sprite_mesh);
617
618 Self {
619 state: Arc::new(SpriteRenderState {
620 sprite_data,
623 missing_sprite_placeholder,
624 sprite_atlas_textures,
625 }),
626 sprite_verts_buffer: Arc::new(sprite_verts_buffer),
627 }
628 };
629 Box::new(move |renderer| init.get_or_init(|| closure(renderer)).clone())
630 }
631}
632
633impl<V: RectRasterableVol> Terrain<V> {
634 pub fn new(
635 renderer: &mut Renderer,
636 global_model: &GlobalModel,
637 lod_data: &LodData,
638 sprite_render_context: SpriteRenderContext,
639 ) -> Self {
640 let (send, recv) = channel::unbounded();
643
644 let (atlas, atlas_textures) =
645 Self::make_atlas(renderer).expect("Failed to create atlas texture");
646
647 Self {
648 atlas,
649 chunks: HashMap::default(),
650 shadow_chunks: Vec::default(),
651 mesh_send_tmp: send,
652 mesh_recv: recv,
653 mesh_todo: HashMap::default(),
654 mesh_todos_active: Arc::new(AtomicU64::new(0)),
655 mesh_recv_overflow: 0.0,
656 sprite_render_state: sprite_render_context.state,
657 sprite_globals: renderer.bind_sprite_globals(
658 global_model,
659 lod_data,
660 &sprite_render_context.sprite_verts_buffer,
661 ),
662 atlas_textures: Arc::new(atlas_textures),
663 phantom: PhantomData,
664 }
665 }
666
667 fn make_atlas(
668 renderer: &mut Renderer,
669 ) -> Result<
670 (
671 AtlasAllocator,
672 AtlasTextures<pipelines::terrain::Locals, TerrainAtlasData>,
673 ),
674 RenderError,
675 > {
676 span!(_guard, "make_atlas", "Terrain::make_atlas");
677 let max_texture_size = renderer.max_texture_size();
678 let atlas_size = guillotiere::Size::new(max_texture_size as i32, max_texture_size as i32);
679 let atlas = AtlasAllocator::with_options(atlas_size, &guillotiere::AllocatorOptions {
680 small_size_threshold: 128,
682 large_size_threshold: 1024,
683 ..guillotiere::AllocatorOptions::default()
684 });
685 let [col_lights, kinds] = [wgpu::TextureFormat::Rgba8Unorm, wgpu::TextureFormat::R8Uint]
686 .map(|fmt| {
687 renderer.create_texture_raw(
688 &wgpu::TextureDescriptor {
689 label: Some("Terrain atlas texture"),
690 size: wgpu::Extent3d {
691 width: max_texture_size,
692 height: max_texture_size,
693 depth_or_array_layers: 1,
694 },
695 mip_level_count: 1,
696 sample_count: 1,
697 dimension: wgpu::TextureDimension::D2,
698 format: fmt,
699 usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
700 view_formats: &[],
701 },
702 &wgpu::TextureViewDescriptor {
703 label: Some("Terrain atlas texture view"),
704 format: Some(fmt),
705 dimension: Some(wgpu::TextureViewDimension::D2),
706 aspect: wgpu::TextureAspect::All,
707 base_mip_level: 0,
708 mip_level_count: None,
709 base_array_layer: 0,
710 array_layer_count: None,
711 },
712 &wgpu::SamplerDescriptor {
713 label: Some("Terrain atlas sampler"),
714 address_mode_u: wgpu::AddressMode::ClampToEdge,
715 address_mode_v: wgpu::AddressMode::ClampToEdge,
716 address_mode_w: wgpu::AddressMode::ClampToEdge,
717 mag_filter: wgpu::FilterMode::Nearest,
718 min_filter: wgpu::FilterMode::Nearest,
719 mipmap_filter: wgpu::FilterMode::Nearest,
720 ..Default::default()
721 },
722 )
723 });
724 let textures = renderer.terrain_bind_atlas_textures(col_lights, kinds);
725 Ok((atlas, textures))
726 }
727
728 fn remove_chunk_meta(&mut self, _pos: Vec2<i32>, chunk: &TerrainChunkData) {
729 if let Some(atlas_alloc) = chunk.atlas_alloc {
732 self.atlas.deallocate(atlas_alloc);
733 }
734 }
738
739 fn insert_chunk(&mut self, pos: Vec2<i32>, chunk: TerrainChunkData) {
740 if let Some(old) = self.chunks.insert(pos, chunk) {
741 self.remove_chunk_meta(pos, &old);
742 }
743 }
747
748 fn remove_chunk(&mut self, pos: Vec2<i32>) {
749 if let Some(chunk) = self.chunks.remove(&pos) {
750 self.remove_chunk_meta(pos, &chunk);
751 self.shadow_chunks.push((pos, chunk));
753 }
754
755 if let Some(_todo) = self.mesh_todo.remove(&pos) {
756 }
758 }
759
760 pub fn light_at_wpos(&self, wpos: Vec3<i32>) -> f32 {
762 let chunk_pos = Vec2::from(wpos).map2(TerrainChunk::RECT_SIZE, |e: i32, sz| {
763 e.div_euclid(sz as i32)
764 });
765 self.chunks
766 .get(&chunk_pos)
767 .map(|c| (c.light_map)(wpos))
768 .unwrap_or(1.0)
769 }
770
771 fn skip_remesh(old_block: Block, new_block: Block) -> (bool, bool) {
781 let same_mesh =
782 new_block.is_liquid() == old_block.is_liquid() &&
785 new_block.is_opaque() == old_block.is_opaque();
786 let skip_lights = same_mesh &&
787 new_block.get_glow() == old_block.get_glow() &&
790 new_block.get_max_sunlight() == old_block.get_max_sunlight();
791 let skip_color = same_mesh &&
792 !new_block.has_color() && !old_block.has_color();
794 (skip_color, skip_lights)
795 }
796
797 pub fn glow_at_wpos(&self, wpos: Vec3<i32>) -> f32 {
799 let chunk_pos = Vec2::from(wpos).map2(TerrainChunk::RECT_SIZE, |e: i32, sz| {
800 e.div_euclid(sz as i32)
801 });
802 self.chunks
803 .get(&chunk_pos)
804 .map(|c| (c.glow_map)(wpos))
805 .unwrap_or(0.0)
806 }
807
808 pub fn glow_normal_at_wpos(&self, wpos: Vec3<f32>) -> (Vec3<f32>, f32) {
809 let wpos_chunk = wpos.xy().map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
810 (e as i32).div_euclid(sz as i32)
811 });
812
813 const AMBIANCE: f32 = 0.15; let (bias, total) = Spiral2d::new()
816 .take(9)
817 .flat_map(|rpos| {
818 let chunk_pos = wpos_chunk + rpos;
819 self.chunks
820 .get(&chunk_pos)
821 .into_iter()
822 .flat_map(|c| c.blocks_of_interest.lights.iter())
823 .filter_map(move |(lpos, level)| {
824 if (*lpos - wpos_chunk).map(|e| e.abs()).reduce_min() < SUNLIGHT as i32 + 2
825 {
826 Some((
827 Vec3::<i32>::from(
828 chunk_pos * TerrainChunk::RECT_SIZE.map(|e| e as i32),
829 ) + *lpos,
830 level,
831 ))
832 } else {
833 None
834 }
835 })
836 })
837 .fold(
838 (Vec3::broadcast(0.001), 0.0),
839 |(bias, total), (lpos, level)| {
840 let rpos = lpos.map(|e| e as f32 + 0.5) - wpos;
841 let level = (*level as f32 - rpos.magnitude()).max(0.0) * SUNLIGHT_INV;
842 (
843 bias + rpos.try_normalized().unwrap_or_else(Vec3::zero) * level,
844 total + level,
845 )
846 },
847 );
848
849 let bias_factor = bias.magnitude() * (1.0 - AMBIANCE) / total.max(0.001);
850
851 (
852 bias.try_normalized().unwrap_or_else(Vec3::zero) * bias_factor.powf(0.5),
853 self.glow_at_wpos(wpos.map(|e| e.floor() as i32)),
854 )
855 }
856
857 pub fn maintain(
863 &mut self,
864 renderer: &mut Renderer,
865 scene_data: &SceneData,
866 focus_pos: Vec3<f32>,
867 loaded_distance: f32,
868 camera: &Camera,
869 ) -> (
870 Aabb<f32>,
871 Vec<math::Vec3<f32>>,
872 math::Aabr<f32>,
873 Vec<math::Vec3<f32>>,
874 math::Aabr<f32>,
875 ) {
876 let camera::Dependents {
877 view_mat,
878 proj_mat_treeculler,
879 cam_pos,
880 ..
881 } = camera.dependents();
882
883 for &pos in &scene_data.state.terrain_changes().removed_chunks {
888 self.remove_chunk(pos);
889 for i in -1..2 {
891 for j in -1..2 {
892 if i != 0 || j != 0 {
893 self.mesh_todo.remove(&(pos + Vec2::new(i, j)));
894 }
895 }
896 }
897 }
898
899 span!(_guard, "maintain", "Terrain::maintain");
900 let current_tick = scene_data.tick;
901 let current_time = scene_data.state.get_time();
902 let mut visible_bounding_box: Option<Aabb<f32>> = None;
904
905 span!(guard, "Add new/modified chunks to mesh todo list");
908 for (modified, pos) in scene_data
909 .state
910 .terrain_changes()
911 .modified_chunks
912 .iter()
913 .map(|c| (true, c))
914 .chain(
915 scene_data
916 .state
917 .terrain_changes()
918 .new_chunks
919 .iter()
920 .map(|c| (false, c)),
921 )
922 {
923 for i in -1..2 {
928 for j in -1..2 {
929 let pos = pos + Vec2::new(i, j);
930
931 if !(self.chunks.contains_key(&pos) || self.mesh_todo.contains_key(&pos))
932 || modified
933 {
934 let mut neighbours = true;
935 for i in -1..2 {
936 for j in -1..2 {
937 neighbours &= scene_data
938 .state
939 .terrain()
940 .contains_key_real(pos + Vec2::new(i, j));
941 }
942 }
943
944 if neighbours {
945 self.mesh_todo.insert(pos, ChunkMeshState {
946 pos,
947 started_tick: current_tick,
948 is_worker_active: false,
949 skip_remesh: false,
950 });
951 }
952 }
953 }
954 }
955 }
956 drop(guard);
957
958 span!(guard, "Add chunks with modified blocks to mesh todo list");
961 for (&pos, &old_block) in scene_data.state.terrain_changes().modified_blocks.iter() {
963 let new_block = scene_data.state.get_block(pos);
967
968 let (skip_color, skip_lights) = if let Some(new_block) = new_block {
969 Self::skip_remesh(old_block, new_block)
970 } else {
971 warn!(
977 "Invariant violation: pos={:?} should be a valid block position. This is a \
978 bug; please contact the developers if you see this error message!",
979 pos
980 );
981 (false, false)
982 };
983
984 let skip_remesh = skip_color && skip_lights;
987
988 let block_effect_radius = crate::mesh::terrain::MAX_LIGHT_DIST;
1004
1005 for x in -1..2 {
1011 for y in -1..2 {
1012 let neighbour_pos = pos + Vec3::new(x, y, 0) * block_effect_radius;
1013 let neighbour_chunk_pos = scene_data.state.terrain().pos_key(neighbour_pos);
1014
1015 if skip_lights && !(x == 0 && y == 0) {
1016 continue;
1019 }
1020
1021 let mut neighbours = true;
1023 for i in -1..2 {
1024 for j in -1..2 {
1025 neighbours &= scene_data
1026 .state
1027 .terrain()
1028 .contains_key_real(neighbour_chunk_pos + Vec2::new(i, j));
1029 }
1030 }
1031 if neighbours {
1032 let todo =
1033 self.mesh_todo
1034 .entry(neighbour_chunk_pos)
1035 .or_insert(ChunkMeshState {
1036 pos: neighbour_chunk_pos,
1037 started_tick: current_tick,
1038 is_worker_active: false,
1039 skip_remesh,
1040 });
1041
1042 todo.skip_remesh &= skip_remesh;
1049 todo.is_worker_active = false;
1050 todo.started_tick = current_tick;
1051 }
1052 }
1053 }
1054 }
1055 drop(guard);
1056
1057 let max_texture_size = renderer.max_texture_size();
1059 let meshing_cores = match num_cpus::get() as u64 {
1060 n if n < 4 => 1,
1061 n if n < 8 => n - 3,
1062 n => n - 4,
1063 };
1064
1065 span!(guard, "Queue meshing from todo list");
1066 let mesh_focus_pos = focus_pos.map(|e| e.trunc()).xy().as_::<i64>();
1067 while let Some((todo, chunk)) = self
1070 .mesh_todo
1071 .values_mut()
1072 .filter(|todo| !todo.is_worker_active)
1073 .min_by_key(|todo| ((todo.pos.as_::<i64>() * TerrainChunk::RECT_SIZE.as_::<i64>()).distance_squared(mesh_focus_pos), todo.started_tick))
1074 .and_then(|todo| {
1076 let pos = todo.pos;
1077 Some((todo, scene_data.state
1078 .terrain()
1079 .get_key_arc(pos)
1080 .cloned()
1081 .or_else(|| {
1082 warn!("Invariant violation: a chunk whose neighbors have not been fetched was found in the todo list,
1083 which could halt meshing entirely.");
1084 None
1085 })?))
1086 })
1087 {
1088 if self.mesh_todos_active.load(Ordering::Relaxed) > meshing_cores {
1089 break;
1090 }
1091
1092 let aabr = Aabr {
1095 min: todo
1096 .pos
1097 .map2(VolGrid2d::<V>::chunk_size(), |e, sz| e * sz as i32 - 1),
1098 max: todo.pos.map2(VolGrid2d::<V>::chunk_size(), |e, sz| {
1099 (e + 1) * sz as i32 + 1
1100 }),
1101 };
1102
1103 let volume = match scene_data.state.terrain().sample(aabr) {
1107 Ok(sample) => sample, Err(VolGrid2dError::NoSuchChunk) => {
1112 continue;
1113 },
1114 _ => panic!("Unhandled edge case"),
1115 };
1116
1117 let min_z = volume
1119 .iter()
1120 .fold(i32::MAX, |min, (_, chunk)| chunk.get_min_z().min(min));
1121 let max_z = volume
1122 .iter()
1123 .fold(i32::MIN, |max, (_, chunk)| chunk.get_max_z().max(max));
1124
1125 let aabb = Aabb {
1126 min: Vec3::from(aabr.min) + Vec3::unit_z() * (min_z - 2),
1127 max: Vec3::from(aabr.max) + Vec3::unit_z() * (max_z + 2),
1128 };
1129
1130 let send = self.mesh_send_tmp.clone();
1132 let pos = todo.pos;
1133
1134 let chunks = &self.chunks;
1135 let skip_remesh = todo
1136 .skip_remesh
1137 .then_some(())
1138 .and_then(|_| chunks.get(&pos))
1139 .map(|chunk| (Arc::clone(&chunk.light_map), Arc::clone(&chunk.glow_map)));
1140
1141 let started_tick = todo.started_tick;
1143 let sprite_render_state = Arc::clone(&self.sprite_render_state);
1144 let cnt = Arc::clone(&self.mesh_todos_active);
1145 cnt.fetch_add(1, Ordering::Relaxed);
1146 scene_data
1147 .state
1148 .slow_job_pool()
1149 .spawn("TERRAIN_MESHING", move || {
1150 let _ = send.send(mesh_worker(
1151 pos,
1152 (min_z as f32, max_z as f32),
1153 skip_remesh,
1154 started_tick,
1155 volume,
1156 max_texture_size as u16,
1157 chunk,
1158 aabb,
1159 &sprite_render_state,
1160 ));
1161 cnt.fetch_sub(1, Ordering::Relaxed);
1162 });
1163 todo.is_worker_active = true;
1164 }
1165 drop(guard);
1166
1167 span!(guard, "Get/upload meshed chunk");
1171 const CHUNKS_PER_SECOND: f32 = 240.0;
1172 let recv_count =
1173 scene_data.state.get_delta_time() * CHUNKS_PER_SECOND + self.mesh_recv_overflow;
1174 self.mesh_recv_overflow = recv_count.fract();
1175 let incoming_chunks =
1176 std::iter::from_fn(|| self.mesh_recv.recv_timeout(Duration::new(0, 0)).ok())
1177 .take(recv_count.floor() as usize)
1178 .collect::<Vec<_>>(); for response in incoming_chunks {
1180 match self.mesh_todo.get(&response.pos) {
1181 Some(todo) if response.started_tick <= todo.started_tick => {
1184 let started_tick = todo.started_tick;
1185
1186 let sprite_instances =
1187 response.sprite_instances.map(|(instances, alt_indices)| {
1188 (renderer.create_instances(&instances), alt_indices)
1189 });
1190
1191 if let Some(mesh) = response.mesh {
1192 let load_time = self
1195 .chunks
1196 .get(&response.pos)
1197 .map(|chunk| chunk.load_time)
1198 .unwrap_or(current_time as f32);
1199 let atlas = &mut self.atlas;
1201 let chunks = &mut self.chunks;
1202 let atlas_textures = &mut self.atlas_textures;
1203 let alloc_size = guillotiere::Size::new(
1204 i32::from(mesh.atlas_size.x),
1205 i32::from(mesh.atlas_size.y),
1206 );
1207
1208 let allocation = atlas.allocate(alloc_size).unwrap_or_else(|| {
1209 let (new_atlas, new_atlas_textures) =
1211 Self::make_atlas(renderer).expect("Failed to create atlas texture");
1212
1213 chunks.iter_mut().for_each(|(_, chunk)| {
1222 chunk.atlas_alloc = None;
1223 });
1224 *atlas = new_atlas;
1225 *atlas_textures = Arc::new(new_atlas_textures);
1226
1227 atlas
1228 .allocate(alloc_size)
1229 .expect("Chunk data does not fit in a texture of maximum size.")
1230 });
1231
1232 let atlas_offs = Vec2::new(
1234 allocation.rectangle.min.x as u32,
1235 allocation.rectangle.min.y as u32,
1236 );
1237 renderer.update_texture(
1239 &atlas_textures.textures[0],
1240 atlas_offs.into_array(),
1241 mesh.atlas_size.as_().into_array(),
1242 &mesh.atlas_texture_data.col_lights,
1243 );
1244 renderer.update_texture(
1246 &atlas_textures.textures[1],
1247 atlas_offs.into_array(),
1248 mesh.atlas_size.as_().into_array(),
1249 &mesh.atlas_texture_data.kinds,
1250 );
1251
1252 self.insert_chunk(response.pos, TerrainChunkData {
1253 load_time,
1254 opaque_model: renderer.create_model(&mesh.opaque_mesh),
1255 fluid_model: renderer.create_model(&mesh.fluid_mesh),
1256 atlas_alloc: Some(allocation.id),
1257 atlas_textures: Arc::clone(&self.atlas_textures),
1258 light_map: mesh.light_map,
1259 glow_map: mesh.glow_map,
1260 sprite_instances,
1261 locals: renderer.create_terrain_bound_locals(&[TerrainLocals::new(
1262 Vec3::from(
1263 response.pos.map2(VolGrid2d::<V>::chunk_size(), |e, sz| {
1264 e as f32 * sz as f32
1265 }),
1266 ),
1267 Quaternion::identity(),
1268 atlas_offs,
1269 load_time,
1270 )]),
1271 visible: Visibility {
1272 in_range: false,
1273 in_frustum: false,
1274 },
1275 can_shadow_point: false,
1276 can_shadow_sun: false,
1277 blocks_of_interest: response.blocks_of_interest,
1278 z_bounds: mesh.z_bounds,
1279 sun_occluder_z_bounds: mesh.sun_occluder_z_bounds,
1280 frustum_last_plane_index: 0,
1281 alt_indices: mesh.alt_indices,
1282 });
1283 } else if let Some(chunk) = self.chunks.get_mut(&response.pos) {
1284 chunk.sprite_instances = sprite_instances;
1287 chunk.blocks_of_interest = response.blocks_of_interest;
1288 }
1289
1290 if response.started_tick == started_tick {
1291 self.mesh_todo.remove(&response.pos);
1292 }
1293 },
1294 Some(_todo) => {},
1297 None => {},
1298 }
1299 }
1300 drop(guard);
1301
1302 span!(guard, "Construct view frustum");
1304 let focus_off = focus_pos.map(|e| e.trunc());
1305 let frustum = Frustum::from_modelview_projection(
1306 (proj_mat_treeculler * view_mat * Mat4::translation_3d(-focus_off)).into_col_arrays(),
1307 );
1308 drop(guard);
1309
1310 span!(guard, "Update chunk visibility");
1312 let chunk_sz = V::RECT_SIZE.x as f32;
1313 for (pos, chunk) in &mut self.chunks {
1314 let chunk_pos = pos.as_::<f32>() * chunk_sz;
1315
1316 chunk.can_shadow_sun = false;
1317
1318 let nearest_in_chunk = Vec2::from(focus_pos).clamped(chunk_pos, chunk_pos + chunk_sz);
1321 let distance_2 = Vec2::<f32>::from(focus_pos).distance_squared(nearest_in_chunk);
1322 let in_range = distance_2 < loaded_distance.powi(2);
1323
1324 chunk.visible.in_range = in_range;
1325
1326 let chunk_min = [chunk_pos.x, chunk_pos.y, chunk.z_bounds.0];
1328 let chunk_max = [
1329 chunk_pos.x + chunk_sz,
1330 chunk_pos.y + chunk_sz,
1331 chunk.sun_occluder_z_bounds.1,
1332 ];
1333
1334 let (in_frustum, last_plane_index) = AABB::new(chunk_min, chunk_max)
1335 .coherent_test_against_frustum(&frustum, chunk.frustum_last_plane_index);
1336
1337 chunk.frustum_last_plane_index = last_plane_index;
1338 chunk.visible.in_frustum = in_frustum;
1339 let chunk_area = Aabr {
1340 min: chunk_pos,
1341 max: chunk_pos + chunk_sz,
1342 };
1343
1344 if in_frustum {
1345 let visible_box = Aabb {
1346 min: chunk_area.min.with_z(chunk.sun_occluder_z_bounds.0),
1347 max: chunk_area.max.with_z(chunk.sun_occluder_z_bounds.1),
1348 };
1349 visible_bounding_box = visible_bounding_box
1350 .map(|e| e.union(visible_box))
1351 .or(Some(visible_box));
1352 }
1353 chunk.can_shadow_point = distance_2 < (128.0 * 128.0);
1356 }
1357 drop(guard);
1358
1359 span!(guard, "Shadow magic");
1360 let visible_bounding_box = visible_bounding_box.unwrap_or(Aabb {
1362 min: focus_pos - 2.0,
1363 max: focus_pos + 2.0,
1364 });
1365 let inv_proj_view =
1366 math::Mat4::from_col_arrays((proj_mat_treeculler * view_mat).into_col_arrays())
1367 .as_::<f64>()
1368 .inverted();
1369
1370 let ray_direction = scene_data.get_sun_dir();
1372 let collides_with_aabr = |a: math::Aabb<f32>, b: math::Aabr<f32>| {
1373 let min = math::Vec4::new(a.min.x, a.min.y, b.min.x, b.min.y);
1374 let max = math::Vec4::new(b.max.x, b.max.y, a.max.x, a.max.y);
1375 #[cfg(feature = "simd")]
1376 return min.partial_cmple_simd(max).reduce_and();
1377 #[cfg(not(feature = "simd"))]
1378 return min.partial_cmple(&max).reduce_and();
1379 };
1380
1381 let (visible_light_volume, visible_psr_bounds) = if ray_direction.z < 0.0
1382 && renderer.pipeline_modes().shadow.is_map()
1383 {
1384 let visible_bounding_box = math::Aabb::<f32> {
1385 min: (visible_bounding_box.min - focus_off),
1386 max: (visible_bounding_box.max - focus_off),
1387 };
1388 let visible_bounds_fine = visible_bounding_box.as_::<f64>();
1389 let visible_light_volume = math::calc_focused_light_volume_points(
1393 inv_proj_view,
1394 ray_direction.as_::<f64>(),
1395 visible_bounds_fine,
1396 1e-6,
1397 )
1398 .map(|v| v.as_::<f32>())
1399 .collect::<Vec<_>>();
1400
1401 let up: math::Vec3<f32> = { math::Vec3::unit_y() };
1402 let ray_mat = math::Mat4::look_at_rh(cam_pos, cam_pos + ray_direction, up);
1403 let visible_bounds = math::Aabr::from(math::fit_psr(
1404 ray_mat,
1405 visible_light_volume.iter().copied(),
1406 |p| p,
1407 ));
1408 let ray_mat = ray_mat * math::Mat4::translation_3d(-focus_off);
1409
1410 let can_shadow_sun = |pos: Vec2<i32>, chunk: &TerrainChunkData| {
1411 let chunk_pos = pos.as_::<f32>() * chunk_sz;
1412
1413 let chunk_box = math::Aabb {
1415 min: math::Vec3::new(chunk_pos.x, chunk_pos.y, chunk.z_bounds.0),
1416 max: math::Vec3::new(
1417 chunk_pos.x + chunk_sz,
1418 chunk_pos.y + chunk_sz,
1419 chunk.z_bounds.1,
1420 ),
1421 };
1422
1423 let chunk_from_light = math::fit_psr(
1424 ray_mat,
1425 math::aabb_to_points(chunk_box).iter().copied(),
1426 |p| p,
1427 );
1428 collides_with_aabr(chunk_from_light, visible_bounds)
1429 };
1430
1431 self.chunks.iter_mut()
1434 .filter(|chunk| !chunk.1.visible.in_frustum)
1438 .for_each(|(&pos, chunk)| {
1439 chunk.can_shadow_sun = can_shadow_sun(pos, chunk);
1440 });
1441
1442 let chunks = &self.chunks;
1454 self.shadow_chunks
1455 .retain(|(pos, chunk)| !chunks.contains_key(pos) && can_shadow_sun(*pos, chunk));
1456
1457 (visible_light_volume, visible_bounds)
1458 } else {
1459 self.shadow_chunks.clear();
1462 (Vec::new(), math::Aabr {
1463 min: math::Vec2::zero(),
1464 max: math::Vec2::zero(),
1465 })
1466 };
1467 drop(guard);
1468 span!(guard, "Rain occlusion magic");
1469 let max_weather = scene_data
1471 .state
1472 .max_weather_near(focus_off.xy() + cam_pos.xy());
1473 let (visible_occlusion_volume, visible_por_bounds) = if max_weather.rain > RAIN_THRESHOLD {
1474 let visible_bounding_box = math::Aabb::<f32> {
1475 min: (visible_bounding_box.min - focus_off),
1476 max: (visible_bounding_box.max - focus_off),
1477 };
1478 let visible_bounds_fine = math::Aabb {
1479 min: visible_bounding_box.min.as_::<f64>(),
1480 max: visible_bounding_box.max.as_::<f64>(),
1481 };
1482 let weather = scene_data.client.weather_at_player();
1483 let ray_direction = weather.rain_vel().normalized();
1484
1485 let visible_volume = math::calc_focused_light_volume_points(
1489 inv_proj_view,
1490 ray_direction.as_::<f64>(),
1491 visible_bounds_fine,
1492 1e-6,
1493 )
1494 .map(|v| v.as_::<f32>())
1495 .collect::<Vec<_>>();
1496 let ray_mat =
1497 math::Mat4::look_at_rh(cam_pos, cam_pos + ray_direction, math::Vec3::unit_y());
1498 let visible_bounds = math::Aabr::from(math::fit_psr(
1499 ray_mat,
1500 visible_volume.iter().copied(),
1501 |p| p,
1502 ));
1503
1504 (visible_volume, visible_bounds)
1505 } else {
1506 (Vec::new(), math::Aabr::default())
1507 };
1508
1509 drop(guard);
1510 (
1511 visible_bounding_box,
1512 visible_light_volume,
1513 visible_psr_bounds,
1514 visible_occlusion_volume,
1515 visible_por_bounds,
1516 )
1517 }
1518
1519 pub fn get(&self, chunk_key: Vec2<i32>) -> Option<&TerrainChunkData> {
1520 self.chunks.get(&chunk_key)
1521 }
1522
1523 pub fn chunk_count(&self) -> usize { self.chunks.len() }
1524
1525 pub fn visible_chunk_count(&self) -> usize {
1526 self.chunks
1527 .iter()
1528 .filter(|(_, c)| c.visible.is_visible())
1529 .count()
1530 }
1531
1532 pub fn shadow_chunk_count(&self) -> usize { self.shadow_chunks.len() }
1533
1534 pub fn render_shadows<'a>(
1535 &'a self,
1536 drawer: &mut TerrainShadowDrawer<'_, 'a>,
1537 focus_pos: Vec3<f32>,
1538 culling_mode: CullingMode,
1539 ) {
1540 span!(_guard, "render_shadows", "Terrain::render_shadows");
1541 let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
1542 (e as i32).div_euclid(sz as i32)
1543 });
1544
1545 let chunk_iter = Spiral2d::new()
1546 .filter_map(|rpos| {
1547 let pos = focus_chunk + rpos;
1548 self.chunks.get(&pos)
1549 })
1550 .take(self.chunks.len());
1551
1552 chunk_iter
1558 .filter(|chunk| chunk.can_shadow_sun())
1559 .chain(self.shadow_chunks.iter().map(|(_, chunk)| chunk))
1560 .filter_map(|chunk| {
1561 Some((
1562 chunk.opaque_model.as_ref()?,
1563 &chunk.locals,
1564 &chunk.alt_indices,
1565 ))
1566 })
1567 .for_each(|(model, locals, alt_indices)| {
1568 drawer.draw(model, locals, alt_indices, culling_mode)
1569 });
1570 }
1571
1572 pub fn render_rain_occlusion<'a>(
1573 &'a self,
1574 drawer: &mut TerrainShadowDrawer<'_, 'a>,
1575 focus_pos: Vec3<f32>,
1576 ) {
1577 span!(_guard, "render_occlusion", "Terrain::render_occlusion");
1578 let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
1579 (e as i32).div_euclid(sz as i32)
1580 });
1581 let chunk_iter = Spiral2d::new()
1582 .filter_map(|rpos| {
1583 let pos = focus_chunk + rpos;
1584 self.chunks.get(&pos)
1585 })
1586 .take(self.chunks.len().min(RAIN_OCCLUSION_CHUNKS));
1587
1588 chunk_iter
1589 .filter_map(|chunk| Some((
1592 chunk
1593 .opaque_model
1594 .as_ref()?,
1595 &chunk.locals,
1596 &chunk.alt_indices,
1597 )))
1598 .for_each(|(model, locals, alt_indices)| drawer.draw(model, locals, alt_indices, CullingMode::None));
1599 }
1600
1601 pub fn chunks_for_point_shadows(
1602 &self,
1603 focus_pos: Vec3<f32>,
1604 ) -> impl Clone
1605 + Iterator<
1606 Item = (
1607 &Model<pipelines::terrain::Vertex>,
1608 &pipelines::terrain::BoundLocals,
1609 ),
1610 > {
1611 let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
1612 (e as i32).div_euclid(sz as i32)
1613 });
1614
1615 let chunk_iter = Spiral2d::new()
1616 .filter_map(move |rpos| {
1617 let pos = focus_chunk + rpos;
1618 self.chunks.get(&pos)
1619 })
1620 .take(self.chunks.len());
1621
1622 chunk_iter
1627 .filter(|chunk| chunk.can_shadow_point)
1628 .filter_map(|chunk| {
1629 chunk
1630 .opaque_model
1631 .as_ref()
1632 .map(|model| (model, &chunk.locals))
1633 })
1634 }
1635
1636 pub fn render<'a>(
1637 &'a self,
1638 drawer: &mut FirstPassDrawer<'a>,
1639 focus_pos: Vec3<f32>,
1640 culling_mode: CullingMode,
1641 ) {
1642 span!(_guard, "render", "Terrain::render");
1643 let mut drawer = drawer.draw_terrain();
1644
1645 let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
1646 (e as i32).div_euclid(sz as i32)
1647 });
1648
1649 Spiral2d::new()
1650 .filter_map(|rpos| {
1651 let pos = focus_chunk + rpos;
1652 Some((rpos, self.chunks.get(&pos)?))
1653 })
1654 .take(self.chunks.len())
1655 .filter(|(_, chunk)| chunk.visible.is_visible())
1656 .filter_map(|(rpos, chunk)| {
1657 Some((
1658 rpos,
1659 chunk.opaque_model.as_ref()?,
1660 &chunk.atlas_textures,
1661 &chunk.locals,
1662 &chunk.alt_indices,
1663 ))
1664 })
1665 .for_each(|(rpos, model, atlas_textures, locals, alt_indices)| {
1666 let culling_mode = if rpos.magnitude_squared() < NEVER_CULL_DIST.pow(2) {
1668 CullingMode::None
1669 } else {
1670 culling_mode
1671 };
1672 drawer.draw(model, atlas_textures, locals, alt_indices, culling_mode)
1673 });
1674 }
1675
1676 pub fn render_sprites<'a>(
1677 &'a self,
1678 sprite_drawer: &mut SpriteDrawer<'_, 'a>,
1679 focus_pos: Vec3<f32>,
1680 cam_pos: Vec3<f32>,
1681 sprite_render_distance: f32,
1682 culling_mode: CullingMode,
1683 ) {
1684 span!(_guard, "render_sprites", "Terrain::render_sprites");
1685
1686 let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
1687 (e as i32).div_euclid(sz as i32)
1688 });
1689
1690 let chunk_iter = Spiral2d::new()
1692 .filter_map(|rpos| {
1693 let pos = focus_chunk + rpos;
1694 Some((rpos, pos, self.chunks.get(&pos)?))
1695 })
1696 .take(self.chunks.len());
1697
1698 let chunk_size = V::RECT_SIZE.map(|e| e as f32);
1699
1700 let sprite_low_detail_distance = sprite_render_distance * 0.75;
1701 let sprite_mid_detail_distance = sprite_render_distance * 0.5;
1702 let sprite_hid_detail_distance = sprite_render_distance * 0.35;
1703 let sprite_high_detail_distance = sprite_render_distance * 0.15;
1704
1705 chunk_iter
1706 .clone()
1707 .filter(|(_, _, c)| c.visible.is_visible())
1708 .for_each(|(rpos, pos, chunk)| {
1709 if chunk.sprite_instances[0].0.count() == 0 {
1711 return;
1712 }
1713
1714 let chunk_center = pos.map2(chunk_size, |e, sz| (e as f32 + 0.5) * sz);
1715 let focus_dist_sqrd = Vec2::from(focus_pos).distance_squared(chunk_center);
1716 let dist_sqrd = Aabr {
1717 min: chunk_center - chunk_size * 0.5,
1718 max: chunk_center + chunk_size * 0.5,
1719 }
1720 .projected_point(cam_pos.xy())
1721 .distance_squared(cam_pos.xy());
1722
1723 if focus_dist_sqrd < sprite_render_distance.powi(2) {
1724 let lod_level = if dist_sqrd < sprite_high_detail_distance.powi(2) {
1725 0
1726 } else if dist_sqrd < sprite_hid_detail_distance.powi(2) {
1727 1
1728 } else if dist_sqrd < sprite_mid_detail_distance.powi(2) {
1729 2
1730 } else if dist_sqrd < sprite_low_detail_distance.powi(2) {
1731 3
1732 } else {
1733 4
1734 };
1735
1736 let culling_mode = if rpos.magnitude_squared() < NEVER_CULL_DIST.pow(2) {
1738 CullingMode::None
1739 } else {
1740 culling_mode
1741 };
1742
1743 sprite_drawer.draw(
1744 &chunk.locals,
1745 &chunk.sprite_instances[lod_level].0,
1746 &chunk.sprite_instances[lod_level].1,
1747 culling_mode,
1748 );
1749 }
1750 });
1751 }
1752
1753 pub fn render_translucent<'a>(
1754 &'a self,
1755 drawer: &mut FirstPassDrawer<'a>,
1756 focus_pos: Vec3<f32>,
1757 ) {
1758 span!(_guard, "render_translucent", "Terrain::render_translucent");
1759 let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
1760 (e as i32).div_euclid(sz as i32)
1761 });
1762
1763 let chunk_iter = Spiral2d::new()
1765 .filter_map(|rpos| {
1766 let pos = focus_chunk + rpos;
1767 self.chunks.get(&pos).map(|c| (pos, c))
1768 })
1769 .take(self.chunks.len());
1770
1771 span!(guard, "Fluid chunks");
1773 let mut fluid_drawer = drawer.draw_fluid();
1774 chunk_iter
1775 .filter(|(_, chunk)| chunk.visible.is_visible())
1776 .filter_map(|(_, chunk)| {
1777 chunk
1778 .fluid_model
1779 .as_ref()
1780 .map(|model| (model, &chunk.locals))
1781 })
1782 .collect::<Vec<_>>()
1783 .into_iter()
1784 .rev() .for_each(|(model, locals)| {
1786 fluid_drawer.draw(
1787 model,
1788 locals,
1789 )
1790 });
1791 drop(fluid_drawer);
1792 drop(guard);
1793 }
1794}