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(crate) 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(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_config: Arc<SpriteSpec>,
469            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            // Load all the sprite config data.
479            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                    // Get starting page count of opaque mesh
527                    let start_page_num =
528                        sprite_mesh.vertices().len() / SPRITE_VERT_PAGE_SIZE as usize;
529                    // Mesh generation exclusively acts using side effects; it
530                    // has no interesting return value, but updates the mesh.
531                    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                    // Get the number of pages after the model was meshed
537                    let end_page_num = sprite_mesh
538                        .vertices()
539                        .len()
540                        .div_ceil(SPRITE_VERT_PAGE_SIZE as usize);
541                    // Fill the current last page up with degenerate verts
542                    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            // TODO: test appearance of this
563            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_config,
580                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            // The second unwrap can only fail if the sprite meshing thread panics, which
592            // implies that our sprite assets either were not found or did not
593            // satisfy the size requirements for meshing, both of which are
594            // considered invariant violations.
595            let SpriteWorkerResponse {
596                //sprite_config,
597                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            // Write sprite model to a 1D texture
616            let sprite_verts_buffer = renderer.create_sprite_verts(sprite_mesh);
617
618            Self {
619                state: Arc::new(SpriteRenderState {
620                    // TODO: these are all Arcs, would it makes sense to factor out the Arc?
621                    //sprite_config: Arc::clone(&sprite_config),
622                    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        // Create a new mpsc (Multiple Produced, Single Consumer) pair for communicating
641        // with worker threads that are meshing chunks.
642        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            // TODO: Verify some good empirical constants.
681            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        // No need to free the allocation if the chunk is not allocated in the current
730        // atlas, since we don't bother tracking it at that point.
731        if let Some(atlas_alloc) = chunk.atlas_alloc {
732            self.atlas.deallocate(atlas_alloc);
733        }
734        /* let (zmin, zmax) = chunk.z_bounds;
735        self.z_index_up.remove(Vec3::from(zmin, pos.x, pos.y));
736        self.z_index_down.remove(Vec3::from(zmax, pos.x, pos.y)); */
737    }
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        /* let (zmin, zmax) = chunk.z_bounds;
744        self.z_index_up.insert(Vec3::from(zmin, pos.x, pos.y));
745        self.z_index_down.insert(Vec3::from(zmax, pos.x, pos.y)); */
746    }
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            // Temporarily remember dead chunks for shadowing purposes.
752            self.shadow_chunks.push((pos, chunk));
753        }
754
755        if let Some(_todo) = self.mesh_todo.remove(&pos) {
756            //Do nothing on todo mesh removal.
757        }
758    }
759
760    /// Find the light level (sunlight) at the given world position.
761    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    /// Determine whether a given block change actually require remeshing.
772    ///
773    /// Returns (skip_color, skip_lights) where
774    ///
775    /// skip_color means no textures were recolored (i.e. this was a sprite only
776    /// change).
777    ///
778    /// skip_lights means no remeshing or relighting was required
779    /// (i.e. the block opacity / lighting info / block kind didn't change).
780    fn skip_remesh(old_block: Block, new_block: Block) -> (bool, bool) {
781        let same_mesh =
782            // Both blocks are of the same opacity and same liquidity (since these are what we use
783            // to determine mesh boundaries).
784            new_block.is_liquid() == old_block.is_liquid() &&
785            new_block.is_opaque() == old_block.is_opaque();
786        let skip_lights = same_mesh &&
787            // Block glow and sunlight handling are the same (so we don't have to redo
788            // lighting).
789            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            // Both blocks are uncolored
793            !new_block.has_color() && !old_block.has_color();
794        (skip_color, skip_lights)
795    }
796
797    /// Find the glow level (light from lamps) at the given world position.
798    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; // 0-1, the proportion of light that should illuminate the rear of an object
814
815        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    /// Maintain terrain data. To be called once per tick.
858    ///
859    /// The returned visible bounding volumes take into account the current
860    /// camera position (i.e: when underground, surface structures will be
861    /// culled from the volume).
862    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        // Remove any models for chunks that have been recently removed.
884        // Note: Does this before adding to todo list just in case removed chunks were
885        // replaced with new chunks (although this would probably be recorded as
886        // modified chunks)
887        for &pos in &scene_data.state.terrain_changes().removed_chunks {
888            self.remove_chunk(pos);
889            // Remove neighbors from meshing todo
890            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        // The visible bounding box of all chunks, not including culled regions
903        let mut visible_bounding_box: Option<Aabb<f32>> = None;
904
905        // Add any recently created or changed chunks to the list of chunks to be
906        // meshed.
907        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            // TODO: ANOTHER PROBLEM HERE!
924            // What happens if the block on the edge of a chunk gets modified? We need to
925            // spawn a mesh worker to remesh its neighbour(s) too since their
926            // ambient occlusion and face elision information changes too!
927            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        // Add the chunks belonging to recently changed blocks to the list of chunks to
959        // be meshed
960        span!(guard, "Add chunks with modified blocks to mesh todo list");
961        // TODO: would be useful if modified blocks were grouped by chunk
962        for (&pos, &old_block) in scene_data.state.terrain_changes().modified_blocks.iter() {
963            // terrain_changes() are both set and applied during the same tick on the
964            // client, so the current state is the new state and modified_blocks
965            // stores the old state.
966            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                // The block coordinates of a modified block should be in bounds, since they are
972                // only retained if setting the block was successful during the state tick in
973                // client.  So this is definitely a bug, but we can recover safely by just
974                // conservatively doing a full remesh in this case, rather than crashing the
975                // game.
976                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            // Currently, we can only skip remeshing if both lights and
985            // colors don't need to be reworked.
986            let skip_remesh = skip_color && skip_lights;
987
988            // TODO: Be cleverer about this to avoid remeshing all neighbours. There are a
989            // few things that can create an 'effect at a distance'. These are
990            // as follows:
991            // - A glowing block is added or removed, thereby causing a lighting
992            //   recalculation proportional to its glow radius.
993            // - An opaque block that was blocking sunlight from entering a cavity is
994            //   removed (or added) thereby
995            // changing the way that sunlight propagates into the cavity.
996            //
997            // We can and should be cleverer about this, but it's non-trivial. For now, we
998            // don't remesh if only a block color changed or a sprite was
999            // altered in a way that doesn't affect its glow, but we make no
1000            // attempt to do smarter cavity checking (to see if altering the
1001            // block changed the sunlight neighbors could get).
1002            // let block_effect_radius = block.get_glow().unwrap_or(0).max(1);
1003            let block_effect_radius = crate::mesh::terrain::MAX_LIGHT_DIST;
1004
1005            // Handle block changes on chunk borders
1006            // Remesh all neighbours because we have complex lighting now
1007            // TODO: if lighting is on the server this can be updated to only remesh when
1008            // lighting changes in that neighbouring chunk or if the block
1009            // change was on the border
1010            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                        // We don't need to remesh neighboring chunks if this block change doesn't
1017                        // require relighting.
1018                        continue;
1019                    }
1020
1021                    // Only remesh if this chunk has all its neighbors
1022                    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                        // Make sure not to skip remeshing a chunk if it already had to be
1043                        // fully meshed for other reasons.  Even if the mesh is currently active
1044                        // (so relighting would be redundant), we currently have to remesh
1045                        // everything unless the previous mesh was also able to skip remeshing,
1046                        // since otherwise the active remesh is computing new lighting values
1047                        // that we don't have yet.
1048                        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        // Limit ourselves to u16::MAX even if larger textures are supported.
1058        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        //TODO: this is actually no loop, it just runs for a single entry because of
1068        // the `min_by_key`. Evaluate actually looping here
1069        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            // Find a reference to the actual `TerrainChunk` we're meshing
1075            .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            // like ambient occlusion and edge elision, we also need the borders
1093            // of the chunk's neighbours too (hence the `- 1` and `+ 1`).
1094            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            // Copy out the chunk data we need to perform the meshing. We do this by taking
1104            // a sample of the terrain that includes both the chunk we want and
1105            // its neighbours.
1106            let volume = match scene_data.state.terrain().sample(aabr) {
1107                Ok(sample) => sample, /* TODO: Ensure that all of the chunk's neighbours still
1108                                        * exist to avoid buggy shadow borders */
1109                // Either this chunk or its neighbours doesn't yet exist, so we keep it in the
1110                // queue to be processed at a later date when we have its neighbours.
1111                Err(VolGrid2dError::NoSuchChunk) => {
1112                    continue;
1113                },
1114                _ => panic!("Unhandled edge case"),
1115            };
1116
1117            // The region to actually mesh
1118            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            // Clone various things so that they can be moved into the thread.
1131            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            // Queue the worker thread.
1142            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        // Receive a chunk mesh from a worker thread and upload it to the GPU, then
1168        // store it. Vary the rate at which we pull items out to correlate with the
1169        // framerate, preventing tail latency.
1170        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<_>>(); // Avoid ownership issue
1179        for response in incoming_chunks {
1180            match self.mesh_todo.get(&response.pos) {
1181                // It's the mesh we want, insert the newly finished model into the terrain model
1182                // data structure (convert the mesh to a model first of course).
1183                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                        // Full update, insert the whole chunk.
1193
1194                        let load_time = self
1195                            .chunks
1196                            .get(&response.pos)
1197                            .map(|chunk| chunk.load_time)
1198                            .unwrap_or(current_time as f32);
1199                        // TODO: Allocate new atlas on allocation failure.
1200                        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                            // Atlas allocation failure: try allocating a new texture and atlas.
1210                            let (new_atlas, new_atlas_textures) =
1211                                Self::make_atlas(renderer).expect("Failed to create atlas texture");
1212
1213                            // We reset the atlas and clear allocations from existing chunks,
1214                            // even though we haven't yet
1215                            // checked whether the new allocation can fit in
1216                            // the texture.  This is reasonable because we don't have a fallback
1217                            // if a single chunk can't fit in an empty atlas of maximum size.
1218                            //
1219                            // TODO: Consider attempting defragmentation first rather than just
1220                            // always moving everything into the new chunk.
1221                            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                        // NOTE: Cast is safe since the origin was a u16.
1233                        let atlas_offs = Vec2::new(
1234                            allocation.rectangle.min.x as u32,
1235                            allocation.rectangle.min.y as u32,
1236                        );
1237                        // Update col_lights texture
1238                        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                        // Update kinds texture
1245                        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                        // There was an update that didn't require a remesh (probably related to
1285                        // non-glowing sprites) so we just update those.
1286                        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                // Chunk must have been removed, or it was spawned on an old tick. Drop the mesh
1295                // since it's either out of date or no longer needed.
1296                Some(_todo) => {},
1297                None => {},
1298            }
1299        }
1300        drop(guard);
1301
1302        // Construct view frustum
1303        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        // Update chunk visibility
1311        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            // Limit focus_pos to chunk bounds and ensure the chunk is within the fog
1319            // boundary
1320            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            // Ensure the chunk is within the view frustum
1327            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            // FIXME: Hack that only works when only the lantern casts point shadows
1354            // (and hardcodes the shadow distance).  Should ideally exist per-light, too.
1355            chunk.can_shadow_point = distance_2 < (128.0 * 128.0);
1356        }
1357        drop(guard);
1358
1359        span!(guard, "Shadow magic");
1360        // PSRs: potential shadow receivers
1361        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        // PSCs: Potential shadow casters
1371        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            // NOTE: We use proj_mat_treeculler here because
1390            // calc_focused_light_volume_points makes the assumption that the
1391            // near plane lies before the far plane.
1392            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                // Ensure the chunk is within the PSR set.
1414                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            // Handle potential shadow casters (chunks that aren't visible, but are still in
1432            // range) to see if they could cast shadows.
1433            self.chunks.iter_mut()
1434                // NOTE: We deliberately avoid doing this computation for chunks we already know
1435                // are visible, since by definition they'll always intersect the visible view
1436                // frustum.
1437                .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            // Handle dead chunks that we kept around only to make sure shadows don't blink
1443            // out when a chunk disappears.
1444            //
1445            // If the sun can currently cast shadows, we retain only those shadow chunks
1446            // that both: 1. have not been replaced by a real chunk instance,
1447            // and 2. are currently potential shadow casters (as witnessed by
1448            // `can_shadow_sun` returning true).
1449            //
1450            // NOTE: Please make sure this runs *after* any code that could insert a chunk!
1451            // Otherwise we may end up with multiple instances of the chunk trying to cast
1452            // shadows at the same time.
1453            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            // There's no daylight or no shadows, so there's no reason to keep any
1460            // shadow chunks around.
1461            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        // Check if there is rain near the camera
1470        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            // NOTE: We use proj_mat_treeculler here because
1486            // calc_focused_light_volume_points makes the assumption that the
1487            // near plane lies before the far plane.
1488            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        // Directed shadows
1553        //
1554        // NOTE: We also render shadows for dead chunks that were found to still be
1555        // potential shadow casters, to avoid shadows suddenly disappearing at
1556        // very steep sun angles (e.g. sunrise / sunset).
1557        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            // Find a way to keep this?
1590            // .filter(|chunk| chunk.can_shadow_sun())
1591            .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        // Point shadows
1623        //
1624        // NOTE: We don't bother retaining chunks unless they cast sun shadows, so we
1625        // don't use `shadow_chunks` here.
1626        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                // Always draw all of close chunks to avoid terrain 'popping'
1667                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        // Avoid switching textures
1691        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                // Skip chunk if it has no sprites
1710                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                    // Always draw all of close chunks to avoid terrain 'popping'
1737                    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        // Avoid switching textures
1764        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        // Translucent
1772        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() // Render back-to-front
1785            .for_each(|(model, locals)| {
1786                fluid_drawer.draw(
1787                    model,
1788                    locals,
1789                )
1790            });
1791        drop(fluid_drawer);
1792        drop(guard);
1793    }
1794}