veloren_voxygen/scene/terrain/
mod.rs

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
52// For rain occlusion we only need to render the closest chunks.
53/// How many chunks are maximally rendered for rain occlusion.
54pub 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    /// Should the chunk actually get rendered?
64    fn is_visible(&self) -> bool {
65        // Currently, we don't take into account in_range to allow all chunks to do
66        // pop-in. This isn't really a problem because we no longer have VD mist
67        // or anything like that. Also, we don't load chunks outside of the VD
68        // anyway so this literally just controls which chunks get actually
69        // rendered.
70        /* self.in_range && */
71        self.in_frustum
72    }
73}
74
75/// Type of closure used for light mapping.
76type LightMapFn = Arc<dyn Fn(Vec3<i32>) -> f32 + Send + Sync>;
77
78pub struct TerrainChunkData {
79    // GPU data
80    load_time: f32,
81    opaque_model: Option<Model<TerrainVertex>>,
82    fluid_model: Option<Model<FluidVertex>>,
83    /// If this is `None`, this texture is not allocated in the current atlas,
84    /// and therefore there is no need to free its allocation.
85    atlas_alloc: Option<guillotiere::AllocId>,
86    /// The actual backing texture for this chunk.  Use this for rendering
87    /// purposes.  The texture is reference-counted, so it will be
88    /// automatically freed when no chunks are left that need it (though
89    /// shadow chunks will still keep it alive; we could deal with this by
90    /// making this an `Option`, but it probably isn't worth it since they
91    /// shouldn't be that much more nonlocal than regular chunks).
92    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
109/// The depth at which the intermediate zone between underground and surface
110/// begins
111pub const SHALLOW_ALT: f32 = 24.0;
112/// The depth at which the intermediate zone between underground and surface
113/// ends
114pub const DEEP_ALT: f32 = 96.0;
115/// The depth below the surface altitude at which the camera switches from
116/// displaying surface elements to underground elements
117pub const UNDERGROUND_ALT: f32 = (SHALLOW_ALT + DEEP_ALT) * 0.5;
118
119// The distance (in chunks) within which all levels of the chunks will be drawn
120// to minimise cull-related popping.
121const 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    // If this is set, we skip the actual meshing part of the update.
129    skip_remesh: bool,
130}
131
132/// Just the mesh part of a mesh worker response.
133pub 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
145/// A type produced by mesh worker threads corresponding to the position and
146/// mesh of a chunk.
147struct MeshWorkerResponse {
148    pos: Vec2<i32>,
149    sprite_instances: [(Vec<SpriteInstance>, AltIndices); SPRITE_LOD_LEVELS],
150    /// If None, this update was requested without meshing.
151    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        // Short-circuit and skip hashmap interaction since this is every fluid block
172        // (including air)
173        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)); // Awful PRNG
191
192        let rot = block
193            .sprite_z_rot()
194            // % 4 is non uniform, So we take % 17 and divide by four, giving the chances:
195            // 0..=2: 4/17
196            // 3: 5/17
197            // Then multiply by π/2 rad to get axis aligned rotations.
198            .unwrap_or((seed % 17 / 4).min(3) as f32 / 2.0 * std::f32::consts::PI);
199        let mirror = block.sprite_mirror_vec();
200        // try to make the variation more uniform as the PRNG is highly unfair
201        let variation = match data.variations.len() {
202            1 => 0,
203            2 => (seed as usize % 4) / 3,
204            3 => (seed as usize % 5) / 2,
205            // for four use a different seed than for ori to not have them match always
206            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            // TODO: worth precomputing the constant parts of this?
216            let mat = Mat4::identity()
217                // Scaling for different LOD resolutions
218                .scaled_3d(model_data.scale)
219                // Offset
220                .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            // Add an instance for each page in the sprite model
227            for page in model_data.vert_pages.clone() {
228                // TODO: could be more efficient to create once outside this loop and clone
229                // while modifying vert_page?
230                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
247/// Function executed by worker threads dedicated to chunk meshing.
248///
249/// skip_remesh is either None (do the full remesh, including recomputing the
250/// light map), or Some((light_map, glow_map)).
251fn 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            // TODO: Take sprite bounds into account somehow?
299            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        // Pointer juggling so borrows work out.
310        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        // Extract sprite locations from volume
319        sprite_instances: {
320            prof_span!("extract sprite_instances");
321            let mut instances = [(); SPRITE_LOD_LEVELS].map(|()| {
322                (
323                    Vec::new(), // Deep
324                    Vec::new(), // Shallow
325                    Vec::new(), // Surface
326                )
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    /// This is always the *current* atlas into which data is being allocated.
385    /// Once an atlas is too full to allocate the next texture, we always
386    /// allocate a fresh texture and start allocating into that.  Trying to
387    /// keep more than one texture available for allocation doesn't seem
388    /// worth it, because our allocation patterns are heavily spatial (so all
389    /// data allocated around the same time should have a very similar lifetime,
390    /// even in pathological cases).  As a result, fragmentation effects
391    /// should be minimal.
392    ///
393    /// TODO: Consider "moving GC" style allocation to deal with spatial
394    /// fragmentation effects due to odd texture sizes, which in some cases
395    /// might significantly reduce the number of textures we need for
396    /// particularly difficult locations.
397    atlas: AtlasAllocator,
398    chunks: HashMap<Vec2<i32>, TerrainChunkData>,
399    /// Temporary storage for dead chunks that might still be shadowing chunks
400    /// in view.  We wait until either the chunk definitely cannot be
401    /// shadowing anything the player can see, the chunk comes back into
402    /// view, or for daylight to end, before removing it (whichever comes
403    /// first).
404    ///
405    /// Note that these chunks are not complete; for example, they are missing
406    /// texture data (they still currently hold onto a reference to their
407    /// backing texture, but it generally can't be trusted for rendering
408    /// purposes).
409    shadow_chunks: Vec<(Vec2<i32>, TerrainChunkData)>,
410    /* /// Secondary index into the terrain chunk table, used to sort through chunks by z index from
411    /// the top down.
412    z_index_down: BTreeSet<Vec3<i32>>,
413    /// Secondary index into the terrain chunk table, used to sort through chunks by z index from
414    /// the bottom up.
415    z_index_up: BTreeSet<Vec3<i32>>, */
416    // The mpsc sender and receiver used for talking to meshing worker threads.
417    // We keep the sender component for no reason other than to clone it and send it to new
418    // workers.
419    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    // GPU data
426    // Maps sprite kind + variant to data detailing how to render it
427    pub(super) sprite_render_state: Arc<SpriteRenderState>,
428    pub(super) sprite_globals: SpriteGlobalsBindGroup,
429    /// As stated previously, this is always the very latest texture into which
430    /// we allocate.  Code cannot assume that this is the assigned texture
431    /// for any particular chunk; look at the `texture` field in
432    /// `TerrainChunkData` for that.
433    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(super) struct SpriteRenderState {
443    // TODO: This could be an `AssetHandle<SpriteSpec>`, to get hot-reloading. However, this would
444    // need to regenerate `sprite_data` and `sprite_atlas_textures`, and re-run
445    // `get_sprite_instances` for any meshed chunks.
446    //pub sprite_config: Arc<SpriteSpec>,
447
448    // Maps sprite kind + variant to data detailing how to render it
449    pub sprite_data: HashMap<SpriteKind, FilteredSpriteData>,
450    pub missing_sprite_placeholder: SpriteData,
451    pub sprite_atlas_textures: AtlasTextures<pipelines::sprite::Locals, FigureSpriteAtlasData>,
452}
453
454#[derive(Clone)]
455pub struct SpriteRenderContext {
456    pub(super) state: Arc<SpriteRenderState>,
457    pub(super) sprite_verts_buffer: Arc<SpriteVerts>,
458}
459
460pub type SpriteRenderContextLazy = Box<dyn FnMut(&mut Renderer) -> SpriteRenderContext>;
461
462impl SpriteRenderContext {
463    pub fn new(renderer: &mut Renderer) -> SpriteRenderContextLazy {
464        let max_texture_size = renderer.max_texture_size();
465
466        struct SpriteWorkerResponse {
467            //sprite_config: Arc<SpriteSpec>,
468            sprite_data: HashMap<SpriteKind, FilteredSpriteData>,
469            missing_sprite_placeholder: SpriteData,
470            sprite_atlas_texture_data: FigureSpriteAtlasData,
471            sprite_atlas_size: Vec2<u16>,
472            sprite_mesh: Mesh<SpriteVertex>,
473        }
474
475        let join_handle = std::thread::spawn(move || {
476            prof_span!("mesh all sprites");
477            // Load all the sprite config data.
478            let sprite_config =
479                Arc::<SpriteSpec>::load_expect("voxygen.voxel.sprite_manifest").cloned();
480
481            let max_size = Vec2::from(u16::try_from(max_texture_size).unwrap_or(u16::MAX));
482            let mut greedy = GreedyMesh::<FigureSpriteAtlasData, SpriteAtlasAllocator>::new(
483                max_size,
484                crate::mesh::greedy::sprite_config(),
485            );
486            let mut sprite_mesh = Mesh::new();
487
488            let mut config_to_data = |sprite_model_config: &_| {
489                let SpriteModelConfig {
490                    model,
491                    offset,
492                    lod_axes,
493                } = sprite_model_config;
494                let scaled = [1.0, 0.8, 0.6, 0.4, 0.2];
495                let offset = Vec3::from(*offset);
496                let lod_axes = Vec3::from(*lod_axes);
497                let model = DotVoxAsset::load_expect(model);
498                let zero = Vec3::zero();
499                let model = &model.read().0;
500                let model_size = if let Some(model) = model.models.first() {
501                    let dot_vox::Size { x, y, z } = model.size;
502                    Vec3::new(x, y, z)
503                } else {
504                    zero
505                };
506                let max_model_size = Vec3::new(31.0, 31.0, 63.0);
507                let model_scale = max_model_size.map2(model_size, |max_sz: f32, cur_sz| {
508                    let scale = max_sz / max_sz.max(cur_sz as f32);
509                    if scale < 1.0 && (cur_sz as f32 * scale).ceil() > max_sz {
510                        scale - 0.001
511                    } else {
512                        scale
513                    }
514                });
515                prof_span!(guard, "mesh sprite");
516                let lod_sprite_data = scaled.map(|lod_scale_orig| {
517                    let lod_scale = model_scale
518                        * if lod_scale_orig == 1.0 {
519                            Vec3::broadcast(1.0)
520                        } else {
521                            lod_axes * lod_scale_orig
522                                + lod_axes.map(|e| if e == 0.0 { 1.0 } else { 0.0 })
523                        };
524
525                    // Get starting page count of opaque mesh
526                    let start_page_num =
527                        sprite_mesh.vertices().len() / SPRITE_VERT_PAGE_SIZE as usize;
528                    // Mesh generation exclusively acts using side effects; it
529                    // has no interesting return value, but updates the mesh.
530                    generate_mesh_base_vol_sprite(
531                        Segment::from_vox_model_index(model, 0).scaled_by(lod_scale),
532                        (&mut greedy, &mut sprite_mesh, false),
533                        offset.map(|e: f32| e.floor()) * lod_scale,
534                    );
535                    // Get the number of pages after the model was meshed
536                    let end_page_num = sprite_mesh
537                        .vertices()
538                        .len()
539                        .div_ceil(SPRITE_VERT_PAGE_SIZE as usize);
540                    // Fill the current last page up with degenerate verts
541                    sprite_mesh.vertices_mut_vec().resize_with(
542                        end_page_num * SPRITE_VERT_PAGE_SIZE as usize,
543                        SpriteVertex::default,
544                    );
545
546                    let sprite_scale = Vec3::one() / lod_scale;
547
548                    SpriteModelData {
549                        vert_pages: start_page_num as u32..end_page_num as u32,
550                        scale: sprite_scale,
551                        offset: offset.map(|e| e.rem_euclid(1.0)),
552                    }
553                });
554                drop(guard);
555
556                lod_sprite_data
557            };
558
559            let sprite_data = sprite_config.map_to_data(&mut config_to_data);
560
561            // TODO: test appearance of this
562            let missing_sprite_placeholder = SpriteData {
563                variations: vec![config_to_data(&SpriteModelConfig {
564                    model: "voxygen.voxel.not_found".into(),
565                    offset: (-5.5, -5.5, 0.0),
566                    lod_axes: (1.0, 1.0, 1.0),
567                })]
568                .into(),
569                wind_sway: 1.0,
570            };
571
572            let (sprite_atlas_texture_data, sprite_atlas_size) = {
573                prof_span!("finalize");
574                greedy.finalize()
575            };
576
577            SpriteWorkerResponse {
578                //sprite_config,
579                sprite_data,
580                missing_sprite_placeholder,
581                sprite_atlas_texture_data,
582                sprite_atlas_size,
583                sprite_mesh,
584            }
585        });
586
587        let init = core::cell::OnceCell::new();
588        let mut join_handle = Some(join_handle);
589        let mut closure = move |renderer: &mut Renderer| {
590            // The second unwrap can only fail if the sprite meshing thread panics, which
591            // implies that our sprite assets either were not found or did not
592            // satisfy the size requirements for meshing, both of which are
593            // considered invariant violations.
594            let SpriteWorkerResponse {
595                //sprite_config,
596                sprite_data,
597                missing_sprite_placeholder,
598                sprite_atlas_texture_data,
599                sprite_atlas_size,
600                sprite_mesh,
601            } = join_handle
602                .take()
603                .expect(
604                    "Closure should only be called once (in a `OnceCell::get_or_init`) in the \
605                     absence of caught panics!",
606                )
607                .join()
608                .unwrap();
609
610            let [sprite_col_lights] =
611                sprite_atlas_texture_data.create_textures(renderer, sprite_atlas_size);
612            let sprite_atlas_textures = renderer.sprite_bind_atlas_textures(sprite_col_lights);
613
614            // Write sprite model to a 1D texture
615            let sprite_verts_buffer = renderer.create_sprite_verts(sprite_mesh);
616
617            Self {
618                state: Arc::new(SpriteRenderState {
619                    // TODO: these are all Arcs, would it makes sense to factor out the Arc?
620                    //sprite_config: Arc::clone(&sprite_config),
621                    sprite_data,
622                    missing_sprite_placeholder,
623                    sprite_atlas_textures,
624                }),
625                sprite_verts_buffer: Arc::new(sprite_verts_buffer),
626            }
627        };
628        Box::new(move |renderer| init.get_or_init(|| closure(renderer)).clone())
629    }
630}
631
632impl<V: RectRasterableVol> Terrain<V> {
633    pub fn new(
634        renderer: &mut Renderer,
635        global_model: &GlobalModel,
636        lod_data: &LodData,
637        sprite_render_context: SpriteRenderContext,
638    ) -> Self {
639        // Create a new mpsc (Multiple Produced, Single Consumer) pair for communicating
640        // with worker threads that are meshing chunks.
641        let (send, recv) = channel::unbounded();
642
643        let (atlas, atlas_textures) =
644            Self::make_atlas(renderer).expect("Failed to create atlas texture");
645
646        Self {
647            atlas,
648            chunks: HashMap::default(),
649            shadow_chunks: Vec::default(),
650            mesh_send_tmp: send,
651            mesh_recv: recv,
652            mesh_todo: HashMap::default(),
653            mesh_todos_active: Arc::new(AtomicU64::new(0)),
654            mesh_recv_overflow: 0.0,
655            sprite_render_state: sprite_render_context.state,
656            sprite_globals: renderer.bind_sprite_globals(
657                global_model,
658                lod_data,
659                &sprite_render_context.sprite_verts_buffer,
660            ),
661            atlas_textures: Arc::new(atlas_textures),
662            phantom: PhantomData,
663        }
664    }
665
666    fn make_atlas(
667        renderer: &mut Renderer,
668    ) -> Result<
669        (
670            AtlasAllocator,
671            AtlasTextures<pipelines::terrain::Locals, TerrainAtlasData>,
672        ),
673        RenderError,
674    > {
675        span!(_guard, "make_atlas", "Terrain::make_atlas");
676        let max_texture_size = renderer.max_texture_size();
677        let atlas_size = guillotiere::Size::new(max_texture_size as i32, max_texture_size as i32);
678        let atlas = AtlasAllocator::with_options(atlas_size, &guillotiere::AllocatorOptions {
679            // TODO: Verify some good empirical constants.
680            small_size_threshold: 128,
681            large_size_threshold: 1024,
682            ..guillotiere::AllocatorOptions::default()
683        });
684        let [col_lights, kinds] = [wgpu::TextureFormat::Rgba8Unorm, wgpu::TextureFormat::R8Uint]
685            .map(|fmt| {
686                renderer.create_texture_raw(
687                    &wgpu::TextureDescriptor {
688                        label: Some("Terrain atlas texture"),
689                        size: wgpu::Extent3d {
690                            width: max_texture_size,
691                            height: max_texture_size,
692                            depth_or_array_layers: 1,
693                        },
694                        mip_level_count: 1,
695                        sample_count: 1,
696                        dimension: wgpu::TextureDimension::D2,
697                        format: fmt,
698                        usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
699                        view_formats: &[],
700                    },
701                    &wgpu::TextureViewDescriptor {
702                        label: Some("Terrain atlas texture view"),
703                        format: Some(fmt),
704                        dimension: Some(wgpu::TextureViewDimension::D2),
705                        aspect: wgpu::TextureAspect::All,
706                        base_mip_level: 0,
707                        mip_level_count: None,
708                        base_array_layer: 0,
709                        array_layer_count: None,
710                    },
711                    &wgpu::SamplerDescriptor {
712                        label: Some("Terrain atlas sampler"),
713                        address_mode_u: wgpu::AddressMode::ClampToEdge,
714                        address_mode_v: wgpu::AddressMode::ClampToEdge,
715                        address_mode_w: wgpu::AddressMode::ClampToEdge,
716                        mag_filter: wgpu::FilterMode::Nearest,
717                        min_filter: wgpu::FilterMode::Nearest,
718                        mipmap_filter: wgpu::FilterMode::Nearest,
719                        ..Default::default()
720                    },
721                )
722            });
723        let textures = renderer.terrain_bind_atlas_textures(col_lights, kinds);
724        Ok((atlas, textures))
725    }
726
727    fn remove_chunk_meta(&mut self, _pos: Vec2<i32>, chunk: &TerrainChunkData) {
728        // No need to free the allocation if the chunk is not allocated in the current
729        // atlas, since we don't bother tracking it at that point.
730        if let Some(atlas_alloc) = chunk.atlas_alloc {
731            self.atlas.deallocate(atlas_alloc);
732        }
733        /* let (zmin, zmax) = chunk.z_bounds;
734        self.z_index_up.remove(Vec3::from(zmin, pos.x, pos.y));
735        self.z_index_down.remove(Vec3::from(zmax, pos.x, pos.y)); */
736    }
737
738    fn insert_chunk(&mut self, pos: Vec2<i32>, chunk: TerrainChunkData) {
739        if let Some(old) = self.chunks.insert(pos, chunk) {
740            self.remove_chunk_meta(pos, &old);
741        }
742        /* let (zmin, zmax) = chunk.z_bounds;
743        self.z_index_up.insert(Vec3::from(zmin, pos.x, pos.y));
744        self.z_index_down.insert(Vec3::from(zmax, pos.x, pos.y)); */
745    }
746
747    fn remove_chunk(&mut self, pos: Vec2<i32>) {
748        if let Some(chunk) = self.chunks.remove(&pos) {
749            self.remove_chunk_meta(pos, &chunk);
750            // Temporarily remember dead chunks for shadowing purposes.
751            self.shadow_chunks.push((pos, chunk));
752        }
753
754        if let Some(_todo) = self.mesh_todo.remove(&pos) {
755            //Do nothing on todo mesh removal.
756        }
757    }
758
759    /// Find the light level (sunlight) at the given world position.
760    pub fn light_at_wpos(&self, wpos: Vec3<i32>) -> f32 {
761        let chunk_pos = Vec2::from(wpos).map2(TerrainChunk::RECT_SIZE, |e: i32, sz| {
762            e.div_euclid(sz as i32)
763        });
764        self.chunks
765            .get(&chunk_pos)
766            .map(|c| (c.light_map)(wpos))
767            .unwrap_or(1.0)
768    }
769
770    /// Determine whether a given block change actually require remeshing.
771    ///
772    /// Returns (skip_color, skip_lights) where
773    ///
774    /// skip_color means no textures were recolored (i.e. this was a sprite only
775    /// change).
776    ///
777    /// skip_lights means no remeshing or relighting was required
778    /// (i.e. the block opacity / lighting info / block kind didn't change).
779    fn skip_remesh(old_block: Block, new_block: Block) -> (bool, bool) {
780        let same_mesh =
781            // Both blocks are of the same opacity and same liquidity (since these are what we use
782            // to determine mesh boundaries).
783            new_block.is_liquid() == old_block.is_liquid() &&
784            new_block.is_opaque() == old_block.is_opaque();
785        let skip_lights = same_mesh &&
786            // Block glow and sunlight handling are the same (so we don't have to redo
787            // lighting).
788            new_block.get_glow() == old_block.get_glow() &&
789            new_block.get_max_sunlight() == old_block.get_max_sunlight();
790        let skip_color = same_mesh &&
791            // Both blocks are uncolored
792            !new_block.has_color() && !old_block.has_color();
793        (skip_color, skip_lights)
794    }
795
796    /// Find the glow level (light from lamps) at the given world position.
797    pub fn glow_at_wpos(&self, wpos: Vec3<i32>) -> f32 {
798        let chunk_pos = Vec2::from(wpos).map2(TerrainChunk::RECT_SIZE, |e: i32, sz| {
799            e.div_euclid(sz as i32)
800        });
801        self.chunks
802            .get(&chunk_pos)
803            .map(|c| (c.glow_map)(wpos))
804            .unwrap_or(0.0)
805    }
806
807    pub fn glow_normal_at_wpos(&self, wpos: Vec3<f32>) -> (Vec3<f32>, f32) {
808        let wpos_chunk = wpos.xy().map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
809            (e as i32).div_euclid(sz as i32)
810        });
811
812        const AMBIANCE: f32 = 0.15; // 0-1, the proportion of light that should illuminate the rear of an object
813
814        let (bias, total) = Spiral2d::new()
815            .take(9)
816            .flat_map(|rpos| {
817                let chunk_pos = wpos_chunk + rpos;
818                self.chunks
819                    .get(&chunk_pos)
820                    .into_iter()
821                    .flat_map(|c| c.blocks_of_interest.lights.iter())
822                    .filter_map(move |(lpos, level)| {
823                        if (*lpos - wpos_chunk).map(|e| e.abs()).reduce_min() < SUNLIGHT as i32 + 2
824                        {
825                            Some((
826                                Vec3::<i32>::from(
827                                    chunk_pos * TerrainChunk::RECT_SIZE.map(|e| e as i32),
828                                ) + *lpos,
829                                level,
830                            ))
831                        } else {
832                            None
833                        }
834                    })
835            })
836            .fold(
837                (Vec3::broadcast(0.001), 0.0),
838                |(bias, total), (lpos, level)| {
839                    let rpos = lpos.map(|e| e as f32 + 0.5) - wpos;
840                    let level = (*level as f32 - rpos.magnitude()).max(0.0) * SUNLIGHT_INV;
841                    (
842                        bias + rpos.try_normalized().unwrap_or_else(Vec3::zero) * level,
843                        total + level,
844                    )
845                },
846            );
847
848        let bias_factor = bias.magnitude() * (1.0 - AMBIANCE) / total.max(0.001);
849
850        (
851            bias.try_normalized().unwrap_or_else(Vec3::zero) * bias_factor.powf(0.5),
852            self.glow_at_wpos(wpos.map(|e| e.floor() as i32)),
853        )
854    }
855
856    /// Maintain terrain data. To be called once per tick.
857    ///
858    /// The returned visible bounding volumes take into account the current
859    /// camera position (i.e: when underground, surface structures will be
860    /// culled from the volume).
861    pub fn maintain(
862        &mut self,
863        renderer: &mut Renderer,
864        scene_data: &SceneData,
865        focus_pos: Vec3<f32>,
866        loaded_distance: f32,
867        camera: &Camera,
868    ) -> (
869        Aabb<f32>,
870        Vec<math::Vec3<f32>>,
871        math::Aabr<f32>,
872        Vec<math::Vec3<f32>>,
873        math::Aabr<f32>,
874    ) {
875        let camera::Dependents {
876            view_mat,
877            proj_mat_treeculler,
878            cam_pos,
879            ..
880        } = camera.dependents();
881
882        // Remove any models for chunks that have been recently removed.
883        // Note: Does this before adding to todo list just in case removed chunks were
884        // replaced with new chunks (although this would probably be recorded as
885        // modified chunks)
886        for &pos in &scene_data.state.terrain_changes().removed_chunks {
887            self.remove_chunk(pos);
888            // Remove neighbors from meshing todo
889            for i in -1..2 {
890                for j in -1..2 {
891                    if i != 0 || j != 0 {
892                        self.mesh_todo.remove(&(pos + Vec2::new(i, j)));
893                    }
894                }
895            }
896        }
897
898        span!(_guard, "maintain", "Terrain::maintain");
899        let current_tick = scene_data.tick;
900        let current_time = scene_data.state.get_time();
901        // The visible bounding box of all chunks, not including culled regions
902        let mut visible_bounding_box: Option<Aabb<f32>> = None;
903
904        // Add any recently created or changed chunks to the list of chunks to be
905        // meshed.
906        span!(guard, "Add new/modified chunks to mesh todo list");
907        for (modified, pos) in scene_data
908            .state
909            .terrain_changes()
910            .modified_chunks
911            .iter()
912            .map(|c| (true, c))
913            .chain(
914                scene_data
915                    .state
916                    .terrain_changes()
917                    .new_chunks
918                    .iter()
919                    .map(|c| (false, c)),
920            )
921        {
922            // TODO: ANOTHER PROBLEM HERE!
923            // What happens if the block on the edge of a chunk gets modified? We need to
924            // spawn a mesh worker to remesh its neighbour(s) too since their
925            // ambient occlusion and face elision information changes too!
926            for i in -1..2 {
927                for j in -1..2 {
928                    let pos = pos + Vec2::new(i, j);
929
930                    if !(self.chunks.contains_key(&pos) || self.mesh_todo.contains_key(&pos))
931                        || modified
932                    {
933                        let mut neighbours = true;
934                        for i in -1..2 {
935                            for j in -1..2 {
936                                neighbours &= scene_data
937                                    .state
938                                    .terrain()
939                                    .contains_key_real(pos + Vec2::new(i, j));
940                            }
941                        }
942
943                        if neighbours {
944                            self.mesh_todo.insert(pos, ChunkMeshState {
945                                pos,
946                                started_tick: current_tick,
947                                is_worker_active: false,
948                                skip_remesh: false,
949                            });
950                        }
951                    }
952                }
953            }
954        }
955        drop(guard);
956
957        // Add the chunks belonging to recently changed blocks to the list of chunks to
958        // be meshed
959        span!(guard, "Add chunks with modified blocks to mesh todo list");
960        // TODO: would be useful if modified blocks were grouped by chunk
961        for (&pos, &old_block) in scene_data.state.terrain_changes().modified_blocks.iter() {
962            // terrain_changes() are both set and applied during the same tick on the
963            // client, so the current state is the new state and modified_blocks
964            // stores the old state.
965            let new_block = scene_data.state.get_block(pos);
966
967            let (skip_color, skip_lights) = if let Some(new_block) = new_block {
968                Self::skip_remesh(old_block, new_block)
969            } else {
970                // The block coordinates of a modified block should be in bounds, since they are
971                // only retained if setting the block was successful during the state tick in
972                // client.  So this is definitely a bug, but we can recover safely by just
973                // conservatively doing a full remesh in this case, rather than crashing the
974                // game.
975                warn!(
976                    "Invariant violation: pos={:?} should be a valid block position.  This is a \
977                     bug; please contact the developers if you see this error message!",
978                    pos
979                );
980                (false, false)
981            };
982
983            // Currently, we can only skip remeshing if both lights and
984            // colors don't need to be reworked.
985            let skip_remesh = skip_color && skip_lights;
986
987            // TODO: Be cleverer about this to avoid remeshing all neighbours. There are a
988            // few things that can create an 'effect at a distance'. These are
989            // as follows:
990            // - A glowing block is added or removed, thereby causing a lighting
991            //   recalculation proportional to its glow radius.
992            // - An opaque block that was blocking sunlight from entering a cavity is
993            //   removed (or added) thereby
994            // changing the way that sunlight propagates into the cavity.
995            //
996            // We can and should be cleverer about this, but it's non-trivial. For now, we
997            // don't remesh if only a block color changed or a sprite was
998            // altered in a way that doesn't affect its glow, but we make no
999            // attempt to do smarter cavity checking (to see if altering the
1000            // block changed the sunlight neighbors could get).
1001            // let block_effect_radius = block.get_glow().unwrap_or(0).max(1);
1002            let block_effect_radius = crate::mesh::terrain::MAX_LIGHT_DIST;
1003
1004            // Handle block changes on chunk borders
1005            // Remesh all neighbours because we have complex lighting now
1006            // TODO: if lighting is on the server this can be updated to only remesh when
1007            // lighting changes in that neighbouring chunk or if the block
1008            // change was on the border
1009            for x in -1..2 {
1010                for y in -1..2 {
1011                    let neighbour_pos = pos + Vec3::new(x, y, 0) * block_effect_radius;
1012                    let neighbour_chunk_pos = scene_data.state.terrain().pos_key(neighbour_pos);
1013
1014                    if skip_lights && !(x == 0 && y == 0) {
1015                        // We don't need to remesh neighboring chunks if this block change doesn't
1016                        // require relighting.
1017                        continue;
1018                    }
1019
1020                    // Only remesh if this chunk has all its neighbors
1021                    let mut neighbours = true;
1022                    for i in -1..2 {
1023                        for j in -1..2 {
1024                            neighbours &= scene_data
1025                                .state
1026                                .terrain()
1027                                .contains_key_real(neighbour_chunk_pos + Vec2::new(i, j));
1028                        }
1029                    }
1030                    if neighbours {
1031                        let todo =
1032                            self.mesh_todo
1033                                .entry(neighbour_chunk_pos)
1034                                .or_insert(ChunkMeshState {
1035                                    pos: neighbour_chunk_pos,
1036                                    started_tick: current_tick,
1037                                    is_worker_active: false,
1038                                    skip_remesh,
1039                                });
1040
1041                        // Make sure not to skip remeshing a chunk if it already had to be
1042                        // fully meshed for other reasons.  Even if the mesh is currently active
1043                        // (so relighting would be redundant), we currently have to remesh
1044                        // everything unless the previous mesh was also able to skip remeshing,
1045                        // since otherwise the active remesh is computing new lighting values
1046                        // that we don't have yet.
1047                        todo.skip_remesh &= skip_remesh;
1048                        todo.is_worker_active = false;
1049                        todo.started_tick = current_tick;
1050                    }
1051                }
1052            }
1053        }
1054        drop(guard);
1055
1056        // Limit ourselves to u16::MAX even if larger textures are supported.
1057        let max_texture_size = renderer.max_texture_size();
1058        let meshing_cores = match num_cpus::get() as u64 {
1059            n if n < 4 => 1,
1060            n if n < 8 => n - 3,
1061            n => n - 4,
1062        };
1063
1064        span!(guard, "Queue meshing from todo list");
1065        let mesh_focus_pos = focus_pos.map(|e| e.trunc()).xy().as_::<i64>();
1066        //TODO: this is actually no loop, it just runs for a single entry because of
1067        // the `min_by_key`. Evaluate actually looping here
1068        while let Some((todo, chunk)) = self
1069            .mesh_todo
1070            .values_mut()
1071            .filter(|todo| !todo.is_worker_active)
1072            .min_by_key(|todo| ((todo.pos.as_::<i64>() * TerrainChunk::RECT_SIZE.as_::<i64>()).distance_squared(mesh_focus_pos), todo.started_tick))
1073            // Find a reference to the actual `TerrainChunk` we're meshing
1074            .and_then(|todo| {
1075                let pos = todo.pos;
1076                Some((todo, scene_data.state
1077                    .terrain()
1078                    .get_key_arc(pos)
1079                    .cloned()
1080                    .or_else(|| {
1081                        warn!("Invariant violation: a chunk whose neighbors have not been fetched was found in the todo list,
1082                              which could halt meshing entirely.");
1083                        None
1084                    })?))
1085            })
1086        {
1087            if self.mesh_todos_active.load(Ordering::Relaxed) > meshing_cores {
1088                break;
1089            }
1090
1091            // like ambient occlusion and edge elision, we also need the borders
1092            // of the chunk's neighbours too (hence the `- 1` and `+ 1`).
1093            let aabr = Aabr {
1094                min: todo
1095                    .pos
1096                    .map2(VolGrid2d::<V>::chunk_size(), |e, sz| e * sz as i32 - 1),
1097                max: todo.pos.map2(VolGrid2d::<V>::chunk_size(), |e, sz| {
1098                    (e + 1) * sz as i32 + 1
1099                }),
1100            };
1101
1102            // Copy out the chunk data we need to perform the meshing. We do this by taking
1103            // a sample of the terrain that includes both the chunk we want and
1104            // its neighbours.
1105            let volume = match scene_data.state.terrain().sample(aabr) {
1106                Ok(sample) => sample, /* TODO: Ensure that all of the chunk's neighbours still
1107                                        * exist to avoid buggy shadow borders */
1108                // Either this chunk or its neighbours doesn't yet exist, so we keep it in the
1109                // queue to be processed at a later date when we have its neighbours.
1110                Err(VolGrid2dError::NoSuchChunk) => {
1111                    continue;
1112                },
1113                _ => panic!("Unhandled edge case"),
1114            };
1115
1116            // The region to actually mesh
1117            let min_z = volume
1118                .iter()
1119                .fold(i32::MAX, |min, (_, chunk)| chunk.get_min_z().min(min));
1120            let max_z = volume
1121                .iter()
1122                .fold(i32::MIN, |max, (_, chunk)| chunk.get_max_z().max(max));
1123
1124            let aabb = Aabb {
1125                min: Vec3::from(aabr.min) + Vec3::unit_z() * (min_z - 2),
1126                max: Vec3::from(aabr.max) + Vec3::unit_z() * (max_z + 2),
1127            };
1128
1129            // Clone various things so that they can be moved into the thread.
1130            let send = self.mesh_send_tmp.clone();
1131            let pos = todo.pos;
1132
1133            let chunks = &self.chunks;
1134            let skip_remesh = todo
1135                .skip_remesh
1136                .then_some(())
1137                .and_then(|_| chunks.get(&pos))
1138                .map(|chunk| (Arc::clone(&chunk.light_map), Arc::clone(&chunk.glow_map)));
1139
1140            // Queue the worker thread.
1141            let started_tick = todo.started_tick;
1142            let sprite_render_state = Arc::clone(&self.sprite_render_state);
1143            let cnt = Arc::clone(&self.mesh_todos_active);
1144            cnt.fetch_add(1, Ordering::Relaxed);
1145            scene_data
1146                .state
1147                .slow_job_pool()
1148                .spawn("TERRAIN_MESHING", move || {
1149                    let _ = send.send(mesh_worker(
1150                        pos,
1151                        (min_z as f32, max_z as f32),
1152                        skip_remesh,
1153                        started_tick,
1154                        volume,
1155                        max_texture_size as u16,
1156                        chunk,
1157                        aabb,
1158                        &sprite_render_state,
1159                    ));
1160                    cnt.fetch_sub(1, Ordering::Relaxed);
1161                });
1162            todo.is_worker_active = true;
1163        }
1164        drop(guard);
1165
1166        // Receive a chunk mesh from a worker thread and upload it to the GPU, then
1167        // store it. Vary the rate at which we pull items out to correlate with the
1168        // framerate, preventing tail latency.
1169        span!(guard, "Get/upload meshed chunk");
1170        const CHUNKS_PER_SECOND: f32 = 240.0;
1171        let recv_count =
1172            scene_data.state.get_delta_time() * CHUNKS_PER_SECOND + self.mesh_recv_overflow;
1173        self.mesh_recv_overflow = recv_count.fract();
1174        let incoming_chunks =
1175            std::iter::from_fn(|| self.mesh_recv.recv_timeout(Duration::new(0, 0)).ok())
1176                .take(recv_count.floor() as usize)
1177                .collect::<Vec<_>>(); // Avoid ownership issue
1178        for response in incoming_chunks {
1179            match self.mesh_todo.get(&response.pos) {
1180                // It's the mesh we want, insert the newly finished model into the terrain model
1181                // data structure (convert the mesh to a model first of course).
1182                Some(todo) if response.started_tick <= todo.started_tick => {
1183                    let started_tick = todo.started_tick;
1184
1185                    let sprite_instances =
1186                        response.sprite_instances.map(|(instances, alt_indices)| {
1187                            (renderer.create_instances(&instances), alt_indices)
1188                        });
1189
1190                    if let Some(mesh) = response.mesh {
1191                        // Full update, insert the whole chunk.
1192
1193                        let load_time = self
1194                            .chunks
1195                            .get(&response.pos)
1196                            .map(|chunk| chunk.load_time)
1197                            .unwrap_or(current_time as f32);
1198                        // TODO: Allocate new atlas on allocation failure.
1199                        let atlas = &mut self.atlas;
1200                        let chunks = &mut self.chunks;
1201                        let atlas_textures = &mut self.atlas_textures;
1202                        let alloc_size = guillotiere::Size::new(
1203                            i32::from(mesh.atlas_size.x),
1204                            i32::from(mesh.atlas_size.y),
1205                        );
1206
1207                        let allocation = atlas.allocate(alloc_size).unwrap_or_else(|| {
1208                            // Atlas allocation failure: try allocating a new texture and atlas.
1209                            let (new_atlas, new_atlas_textures) =
1210                                Self::make_atlas(renderer).expect("Failed to create atlas texture");
1211
1212                            // We reset the atlas and clear allocations from existing chunks,
1213                            // even though we haven't yet
1214                            // checked whether the new allocation can fit in
1215                            // the texture.  This is reasonable because we don't have a fallback
1216                            // if a single chunk can't fit in an empty atlas of maximum size.
1217                            //
1218                            // TODO: Consider attempting defragmentation first rather than just
1219                            // always moving everything into the new chunk.
1220                            chunks.iter_mut().for_each(|(_, chunk)| {
1221                                chunk.atlas_alloc = None;
1222                            });
1223                            *atlas = new_atlas;
1224                            *atlas_textures = Arc::new(new_atlas_textures);
1225
1226                            atlas
1227                                .allocate(alloc_size)
1228                                .expect("Chunk data does not fit in a texture of maximum size.")
1229                        });
1230
1231                        // NOTE: Cast is safe since the origin was a u16.
1232                        let atlas_offs = Vec2::new(
1233                            allocation.rectangle.min.x as u32,
1234                            allocation.rectangle.min.y as u32,
1235                        );
1236                        // Update col_lights texture
1237                        renderer.update_texture(
1238                            &atlas_textures.textures[0],
1239                            atlas_offs.into_array(),
1240                            mesh.atlas_size.as_().into_array(),
1241                            &mesh.atlas_texture_data.col_lights,
1242                        );
1243                        // Update kinds texture
1244                        renderer.update_texture(
1245                            &atlas_textures.textures[1],
1246                            atlas_offs.into_array(),
1247                            mesh.atlas_size.as_().into_array(),
1248                            &mesh.atlas_texture_data.kinds,
1249                        );
1250
1251                        self.insert_chunk(response.pos, TerrainChunkData {
1252                            load_time,
1253                            opaque_model: renderer.create_model(&mesh.opaque_mesh),
1254                            fluid_model: renderer.create_model(&mesh.fluid_mesh),
1255                            atlas_alloc: Some(allocation.id),
1256                            atlas_textures: Arc::clone(&self.atlas_textures),
1257                            light_map: mesh.light_map,
1258                            glow_map: mesh.glow_map,
1259                            sprite_instances,
1260                            locals: renderer.create_terrain_bound_locals(&[TerrainLocals::new(
1261                                Vec3::from(
1262                                    response.pos.map2(VolGrid2d::<V>::chunk_size(), |e, sz| {
1263                                        e as f32 * sz as f32
1264                                    }),
1265                                ),
1266                                Quaternion::identity(),
1267                                atlas_offs,
1268                                load_time,
1269                            )]),
1270                            visible: Visibility {
1271                                in_range: false,
1272                                in_frustum: false,
1273                            },
1274                            can_shadow_point: false,
1275                            can_shadow_sun: false,
1276                            blocks_of_interest: response.blocks_of_interest,
1277                            z_bounds: mesh.z_bounds,
1278                            sun_occluder_z_bounds: mesh.sun_occluder_z_bounds,
1279                            frustum_last_plane_index: 0,
1280                            alt_indices: mesh.alt_indices,
1281                        });
1282                    } else if let Some(chunk) = self.chunks.get_mut(&response.pos) {
1283                        // There was an update that didn't require a remesh (probably related to
1284                        // non-glowing sprites) so we just update those.
1285                        chunk.sprite_instances = sprite_instances;
1286                        chunk.blocks_of_interest = response.blocks_of_interest;
1287                    }
1288
1289                    if response.started_tick == started_tick {
1290                        self.mesh_todo.remove(&response.pos);
1291                    }
1292                },
1293                // Chunk must have been removed, or it was spawned on an old tick. Drop the mesh
1294                // since it's either out of date or no longer needed.
1295                Some(_todo) => {},
1296                None => {},
1297            }
1298        }
1299        drop(guard);
1300
1301        // Construct view frustum
1302        span!(guard, "Construct view frustum");
1303        let focus_off = focus_pos.map(|e| e.trunc());
1304        let frustum = Frustum::from_modelview_projection(
1305            (proj_mat_treeculler * view_mat * Mat4::translation_3d(-focus_off)).into_col_arrays(),
1306        );
1307        drop(guard);
1308
1309        // Update chunk visibility
1310        span!(guard, "Update chunk visibility");
1311        let chunk_sz = V::RECT_SIZE.x as f32;
1312        for (pos, chunk) in &mut self.chunks {
1313            let chunk_pos = pos.as_::<f32>() * chunk_sz;
1314
1315            chunk.can_shadow_sun = false;
1316
1317            // Limit focus_pos to chunk bounds and ensure the chunk is within the fog
1318            // boundary
1319            let nearest_in_chunk = Vec2::from(focus_pos).clamped(chunk_pos, chunk_pos + chunk_sz);
1320            let distance_2 = Vec2::<f32>::from(focus_pos).distance_squared(nearest_in_chunk);
1321            let in_range = distance_2 < loaded_distance.powi(2);
1322
1323            chunk.visible.in_range = in_range;
1324
1325            // Ensure the chunk is within the view frustum
1326            let chunk_min = [chunk_pos.x, chunk_pos.y, chunk.z_bounds.0];
1327            let chunk_max = [
1328                chunk_pos.x + chunk_sz,
1329                chunk_pos.y + chunk_sz,
1330                chunk.sun_occluder_z_bounds.1,
1331            ];
1332
1333            let (in_frustum, last_plane_index) = AABB::new(chunk_min, chunk_max)
1334                .coherent_test_against_frustum(&frustum, chunk.frustum_last_plane_index);
1335
1336            chunk.frustum_last_plane_index = last_plane_index;
1337            chunk.visible.in_frustum = in_frustum;
1338            let chunk_area = Aabr {
1339                min: chunk_pos,
1340                max: chunk_pos + chunk_sz,
1341            };
1342
1343            if in_frustum {
1344                let visible_box = Aabb {
1345                    min: chunk_area.min.with_z(chunk.sun_occluder_z_bounds.0),
1346                    max: chunk_area.max.with_z(chunk.sun_occluder_z_bounds.1),
1347                };
1348                visible_bounding_box = visible_bounding_box
1349                    .map(|e| e.union(visible_box))
1350                    .or(Some(visible_box));
1351            }
1352            // FIXME: Hack that only works when only the lantern casts point shadows
1353            // (and hardcodes the shadow distance).  Should ideally exist per-light, too.
1354            chunk.can_shadow_point = distance_2 < (128.0 * 128.0);
1355        }
1356        drop(guard);
1357
1358        span!(guard, "Shadow magic");
1359        // PSRs: potential shadow receivers
1360        let visible_bounding_box = visible_bounding_box.unwrap_or(Aabb {
1361            min: focus_pos - 2.0,
1362            max: focus_pos + 2.0,
1363        });
1364        let inv_proj_view =
1365            math::Mat4::from_col_arrays((proj_mat_treeculler * view_mat).into_col_arrays())
1366                .as_::<f64>()
1367                .inverted();
1368
1369        // PSCs: Potential shadow casters
1370        let ray_direction = scene_data.get_sun_dir();
1371        let collides_with_aabr = |a: math::Aabb<f32>, b: math::Aabr<f32>| {
1372            let min = math::Vec4::new(a.min.x, a.min.y, b.min.x, b.min.y);
1373            let max = math::Vec4::new(b.max.x, b.max.y, a.max.x, a.max.y);
1374            #[cfg(feature = "simd")]
1375            return min.partial_cmple_simd(max).reduce_and();
1376            #[cfg(not(feature = "simd"))]
1377            return min.partial_cmple(&max).reduce_and();
1378        };
1379
1380        let (visible_light_volume, visible_psr_bounds) = if ray_direction.z < 0.0
1381            && renderer.pipeline_modes().shadow.is_map()
1382        {
1383            let visible_bounding_box = math::Aabb::<f32> {
1384                min: (visible_bounding_box.min - focus_off),
1385                max: (visible_bounding_box.max - focus_off),
1386            };
1387            let visible_bounds_fine = visible_bounding_box.as_::<f64>();
1388            // NOTE: We use proj_mat_treeculler here because
1389            // calc_focused_light_volume_points makes the assumption that the
1390            // near plane lies before the far plane.
1391            let visible_light_volume = math::calc_focused_light_volume_points(
1392                inv_proj_view,
1393                ray_direction.as_::<f64>(),
1394                visible_bounds_fine,
1395                1e-6,
1396            )
1397            .map(|v| v.as_::<f32>())
1398            .collect::<Vec<_>>();
1399
1400            let up: math::Vec3<f32> = { math::Vec3::unit_y() };
1401            let ray_mat = math::Mat4::look_at_rh(cam_pos, cam_pos + ray_direction, up);
1402            let visible_bounds = math::Aabr::from(math::fit_psr(
1403                ray_mat,
1404                visible_light_volume.iter().copied(),
1405                |p| p,
1406            ));
1407            let ray_mat = ray_mat * math::Mat4::translation_3d(-focus_off);
1408
1409            let can_shadow_sun = |pos: Vec2<i32>, chunk: &TerrainChunkData| {
1410                let chunk_pos = pos.as_::<f32>() * chunk_sz;
1411
1412                // Ensure the chunk is within the PSR set.
1413                let chunk_box = math::Aabb {
1414                    min: math::Vec3::new(chunk_pos.x, chunk_pos.y, chunk.z_bounds.0),
1415                    max: math::Vec3::new(
1416                        chunk_pos.x + chunk_sz,
1417                        chunk_pos.y + chunk_sz,
1418                        chunk.z_bounds.1,
1419                    ),
1420                };
1421
1422                let chunk_from_light = math::fit_psr(
1423                    ray_mat,
1424                    math::aabb_to_points(chunk_box).iter().copied(),
1425                    |p| p,
1426                );
1427                collides_with_aabr(chunk_from_light, visible_bounds)
1428            };
1429
1430            // Handle potential shadow casters (chunks that aren't visible, but are still in
1431            // range) to see if they could cast shadows.
1432            self.chunks.iter_mut()
1433                // NOTE: We deliberately avoid doing this computation for chunks we already know
1434                // are visible, since by definition they'll always intersect the visible view
1435                // frustum.
1436                .filter(|chunk| !chunk.1.visible.in_frustum)
1437                .for_each(|(&pos, chunk)| {
1438                    chunk.can_shadow_sun = can_shadow_sun(pos, chunk);
1439                });
1440
1441            // Handle dead chunks that we kept around only to make sure shadows don't blink
1442            // out when a chunk disappears.
1443            //
1444            // If the sun can currently cast shadows, we retain only those shadow chunks
1445            // that both: 1. have not been replaced by a real chunk instance,
1446            // and 2. are currently potential shadow casters (as witnessed by
1447            // `can_shadow_sun` returning true).
1448            //
1449            // NOTE: Please make sure this runs *after* any code that could insert a chunk!
1450            // Otherwise we may end up with multiple instances of the chunk trying to cast
1451            // shadows at the same time.
1452            let chunks = &self.chunks;
1453            self.shadow_chunks
1454                .retain(|(pos, chunk)| !chunks.contains_key(pos) && can_shadow_sun(*pos, chunk));
1455
1456            (visible_light_volume, visible_bounds)
1457        } else {
1458            // There's no daylight or no shadows, so there's no reason to keep any
1459            // shadow chunks around.
1460            self.shadow_chunks.clear();
1461            (Vec::new(), math::Aabr {
1462                min: math::Vec2::zero(),
1463                max: math::Vec2::zero(),
1464            })
1465        };
1466        drop(guard);
1467        span!(guard, "Rain occlusion magic");
1468        // Check if there is rain near the camera
1469        let max_weather = scene_data
1470            .state
1471            .max_weather_near(focus_off.xy() + cam_pos.xy());
1472        let (visible_occlusion_volume, visible_por_bounds) = if max_weather.rain > RAIN_THRESHOLD {
1473            let visible_bounding_box = math::Aabb::<f32> {
1474                min: (visible_bounding_box.min - focus_off),
1475                max: (visible_bounding_box.max - focus_off),
1476            };
1477            let visible_bounds_fine = math::Aabb {
1478                min: visible_bounding_box.min.as_::<f64>(),
1479                max: visible_bounding_box.max.as_::<f64>(),
1480            };
1481            let weather = scene_data.client.weather_at_player();
1482            let ray_direction = weather.rain_vel().normalized();
1483
1484            // NOTE: We use proj_mat_treeculler here because
1485            // calc_focused_light_volume_points makes the assumption that the
1486            // near plane lies before the far plane.
1487            let visible_volume = math::calc_focused_light_volume_points(
1488                inv_proj_view,
1489                ray_direction.as_::<f64>(),
1490                visible_bounds_fine,
1491                1e-6,
1492            )
1493            .map(|v| v.as_::<f32>())
1494            .collect::<Vec<_>>();
1495            let ray_mat =
1496                math::Mat4::look_at_rh(cam_pos, cam_pos + ray_direction, math::Vec3::unit_y());
1497            let visible_bounds = math::Aabr::from(math::fit_psr(
1498                ray_mat,
1499                visible_volume.iter().copied(),
1500                |p| p,
1501            ));
1502
1503            (visible_volume, visible_bounds)
1504        } else {
1505            (Vec::new(), math::Aabr::default())
1506        };
1507
1508        drop(guard);
1509        (
1510            visible_bounding_box,
1511            visible_light_volume,
1512            visible_psr_bounds,
1513            visible_occlusion_volume,
1514            visible_por_bounds,
1515        )
1516    }
1517
1518    pub fn get(&self, chunk_key: Vec2<i32>) -> Option<&TerrainChunkData> {
1519        self.chunks.get(&chunk_key)
1520    }
1521
1522    pub fn chunk_count(&self) -> usize { self.chunks.len() }
1523
1524    pub fn visible_chunk_count(&self) -> usize {
1525        self.chunks
1526            .iter()
1527            .filter(|(_, c)| c.visible.is_visible())
1528            .count()
1529    }
1530
1531    pub fn shadow_chunk_count(&self) -> usize { self.shadow_chunks.len() }
1532
1533    pub fn render_shadows<'a>(
1534        &'a self,
1535        drawer: &mut TerrainShadowDrawer<'_, 'a>,
1536        focus_pos: Vec3<f32>,
1537        culling_mode: CullingMode,
1538    ) {
1539        span!(_guard, "render_shadows", "Terrain::render_shadows");
1540        let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
1541            (e as i32).div_euclid(sz as i32)
1542        });
1543
1544        let chunk_iter = Spiral2d::new()
1545            .filter_map(|rpos| {
1546                let pos = focus_chunk + rpos;
1547                self.chunks.get(&pos)
1548            })
1549            .take(self.chunks.len());
1550
1551        // Directed shadows
1552        //
1553        // NOTE: We also render shadows for dead chunks that were found to still be
1554        // potential shadow casters, to avoid shadows suddenly disappearing at
1555        // very steep sun angles (e.g. sunrise / sunset).
1556        chunk_iter
1557            .filter(|chunk| chunk.can_shadow_sun())
1558            .chain(self.shadow_chunks.iter().map(|(_, chunk)| chunk))
1559            .filter_map(|chunk| {
1560                Some((
1561                    chunk.opaque_model.as_ref()?,
1562                    &chunk.locals,
1563                    &chunk.alt_indices,
1564                ))
1565            })
1566            .for_each(|(model, locals, alt_indices)| {
1567                drawer.draw(model, locals, alt_indices, culling_mode)
1568            });
1569    }
1570
1571    pub fn render_rain_occlusion<'a>(
1572        &'a self,
1573        drawer: &mut TerrainShadowDrawer<'_, 'a>,
1574        focus_pos: Vec3<f32>,
1575    ) {
1576        span!(_guard, "render_occlusion", "Terrain::render_occlusion");
1577        let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
1578            (e as i32).div_euclid(sz as i32)
1579        });
1580        let chunk_iter = Spiral2d::new()
1581            .filter_map(|rpos| {
1582                let pos = focus_chunk + rpos;
1583                self.chunks.get(&pos)
1584            })
1585            .take(self.chunks.len().min(RAIN_OCCLUSION_CHUNKS));
1586
1587        chunk_iter
1588            // Find a way to keep this?
1589            // .filter(|chunk| chunk.can_shadow_sun())
1590            .filter_map(|chunk| Some((
1591                chunk
1592                    .opaque_model
1593                    .as_ref()?,
1594                &chunk.locals,
1595                &chunk.alt_indices,
1596            )))
1597            .for_each(|(model, locals, alt_indices)| drawer.draw(model, locals, alt_indices, CullingMode::None));
1598    }
1599
1600    pub fn chunks_for_point_shadows(
1601        &self,
1602        focus_pos: Vec3<f32>,
1603    ) -> impl Clone
1604    + Iterator<
1605        Item = (
1606            &Model<pipelines::terrain::Vertex>,
1607            &pipelines::terrain::BoundLocals,
1608        ),
1609    > {
1610        let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
1611            (e as i32).div_euclid(sz as i32)
1612        });
1613
1614        let chunk_iter = Spiral2d::new()
1615            .filter_map(move |rpos| {
1616                let pos = focus_chunk + rpos;
1617                self.chunks.get(&pos)
1618            })
1619            .take(self.chunks.len());
1620
1621        // Point shadows
1622        //
1623        // NOTE: We don't bother retaining chunks unless they cast sun shadows, so we
1624        // don't use `shadow_chunks` here.
1625        chunk_iter
1626            .filter(|chunk| chunk.can_shadow_point)
1627            .filter_map(|chunk| {
1628                chunk
1629                    .opaque_model
1630                    .as_ref()
1631                    .map(|model| (model, &chunk.locals))
1632            })
1633    }
1634
1635    pub fn render<'a>(
1636        &'a self,
1637        drawer: &mut FirstPassDrawer<'a>,
1638        focus_pos: Vec3<f32>,
1639        culling_mode: CullingMode,
1640    ) {
1641        span!(_guard, "render", "Terrain::render");
1642        let mut drawer = drawer.draw_terrain();
1643
1644        let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
1645            (e as i32).div_euclid(sz as i32)
1646        });
1647
1648        Spiral2d::new()
1649            .filter_map(|rpos| {
1650                let pos = focus_chunk + rpos;
1651                Some((rpos, self.chunks.get(&pos)?))
1652            })
1653            .take(self.chunks.len())
1654            .filter(|(_, chunk)| chunk.visible.is_visible())
1655            .filter_map(|(rpos, chunk)| {
1656                Some((
1657                    rpos,
1658                    chunk.opaque_model.as_ref()?,
1659                    &chunk.atlas_textures,
1660                    &chunk.locals,
1661                    &chunk.alt_indices,
1662                ))
1663            })
1664            .for_each(|(rpos, model, atlas_textures, locals, alt_indices)| {
1665                // Always draw all of close chunks to avoid terrain 'popping'
1666                let culling_mode = if rpos.magnitude_squared() < NEVER_CULL_DIST.pow(2) {
1667                    CullingMode::None
1668                } else {
1669                    culling_mode
1670                };
1671                drawer.draw(model, atlas_textures, locals, alt_indices, culling_mode)
1672            });
1673    }
1674
1675    pub fn render_sprites<'a>(
1676        &'a self,
1677        sprite_drawer: &mut SpriteDrawer<'_, 'a>,
1678        focus_pos: Vec3<f32>,
1679        cam_pos: Vec3<f32>,
1680        sprite_render_distance: f32,
1681        culling_mode: CullingMode,
1682    ) {
1683        span!(_guard, "render_sprites", "Terrain::render_sprites");
1684
1685        let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
1686            (e as i32).div_euclid(sz as i32)
1687        });
1688
1689        // Avoid switching textures
1690        let chunk_iter = Spiral2d::new()
1691            .filter_map(|rpos| {
1692                let pos = focus_chunk + rpos;
1693                Some((rpos, pos, self.chunks.get(&pos)?))
1694            })
1695            .take(self.chunks.len());
1696
1697        let chunk_size = V::RECT_SIZE.map(|e| e as f32);
1698
1699        let sprite_low_detail_distance = sprite_render_distance * 0.75;
1700        let sprite_mid_detail_distance = sprite_render_distance * 0.5;
1701        let sprite_hid_detail_distance = sprite_render_distance * 0.35;
1702        let sprite_high_detail_distance = sprite_render_distance * 0.15;
1703
1704        chunk_iter
1705            .clone()
1706            .filter(|(_, _, c)| c.visible.is_visible())
1707            .for_each(|(rpos, pos, chunk)| {
1708                // Skip chunk if it has no sprites
1709                if chunk.sprite_instances[0].0.count() == 0 {
1710                    return;
1711                }
1712
1713                let chunk_center = pos.map2(chunk_size, |e, sz| (e as f32 + 0.5) * sz);
1714                let focus_dist_sqrd = Vec2::from(focus_pos).distance_squared(chunk_center);
1715                let dist_sqrd = Aabr {
1716                    min: chunk_center - chunk_size * 0.5,
1717                    max: chunk_center + chunk_size * 0.5,
1718                }
1719                .projected_point(cam_pos.xy())
1720                .distance_squared(cam_pos.xy());
1721
1722                if focus_dist_sqrd < sprite_render_distance.powi(2) {
1723                    let lod_level = if dist_sqrd < sprite_high_detail_distance.powi(2) {
1724                        0
1725                    } else if dist_sqrd < sprite_hid_detail_distance.powi(2) {
1726                        1
1727                    } else if dist_sqrd < sprite_mid_detail_distance.powi(2) {
1728                        2
1729                    } else if dist_sqrd < sprite_low_detail_distance.powi(2) {
1730                        3
1731                    } else {
1732                        4
1733                    };
1734
1735                    // Always draw all of close chunks to avoid terrain 'popping'
1736                    let culling_mode = if rpos.magnitude_squared() < NEVER_CULL_DIST.pow(2) {
1737                        CullingMode::None
1738                    } else {
1739                        culling_mode
1740                    };
1741
1742                    sprite_drawer.draw(
1743                        &chunk.locals,
1744                        &chunk.sprite_instances[lod_level].0,
1745                        &chunk.sprite_instances[lod_level].1,
1746                        culling_mode,
1747                    );
1748                }
1749            });
1750    }
1751
1752    pub fn render_translucent<'a>(
1753        &'a self,
1754        drawer: &mut FirstPassDrawer<'a>,
1755        focus_pos: Vec3<f32>,
1756    ) {
1757        span!(_guard, "render_translucent", "Terrain::render_translucent");
1758        let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
1759            (e as i32).div_euclid(sz as i32)
1760        });
1761
1762        // Avoid switching textures
1763        let chunk_iter = Spiral2d::new()
1764            .filter_map(|rpos| {
1765                let pos = focus_chunk + rpos;
1766                self.chunks.get(&pos).map(|c| (pos, c))
1767            })
1768            .take(self.chunks.len());
1769
1770        // Translucent
1771        span!(guard, "Fluid chunks");
1772        let mut fluid_drawer = drawer.draw_fluid();
1773        chunk_iter
1774            .filter(|(_, chunk)| chunk.visible.is_visible())
1775            .filter_map(|(_, chunk)| {
1776                chunk
1777                    .fluid_model
1778                    .as_ref()
1779                    .map(|model| (model, &chunk.locals))
1780            })
1781            .collect::<Vec<_>>()
1782            .into_iter()
1783            .rev() // Render back-to-front
1784            .for_each(|(model, locals)| {
1785                fluid_drawer.draw(
1786                    model,
1787                    locals,
1788                )
1789            });
1790        drop(fluid_drawer);
1791        drop(guard);
1792    }
1793}