use std::ops::Deref; use bevy::prelude::*; use crate::{ bloxel::{block::BlockTexture, prelude::*}, TerrainAtlas, TerrainMaterial, }; mod mesh_generator; pub use mesh_generator::*; /// Generates meshes for chunks that have `ChunkStorage` but no `Mesh3d` yet. pub fn generate_chunk_mesh( mut commands: Commands, mut meshes: ResMut>, terrain_material: Option>, terrain_atlas: Option>, atlas_layouts: Res>, chunks_without_meshes: Query<(Entity, &ChunkData), (With, Without)>, block_lookup: Query<&BlockTexture, With>, ) { // This system will run before `TerrainMaterial` is available, so to avoid // the system failing, we'll make the material and atlas an `Option`. // TODO: Should be able to be replaced with `When<>` once available in Bevy. let Some(terrain_material) = terrain_material else { return; }; // Atlas and layout should exist at this point, if material does. let terrain_atlas = terrain_atlas.unwrap(); let terrain_atlas_layout = atlas_layouts.get(&terrain_atlas.layout).unwrap(); for (entity, storage) in chunks_without_meshes.iter() { let mesh = create_bloxel_mesh( storage.deref(), terrain_atlas_layout, &terrain_atlas.sources, &block_lookup, ); commands.entity(entity).insert(( Mesh3d(meshes.add(mesh)), MeshMaterial3d(terrain_material.0.clone()), )); } }