//! Shows how to swap out the default grass mesh. //! You can use the TAB key to swap between the new mesh and the default grass mesh use bevy::{prelude::*, render::primitives::Aabb}; use warbler_grass::{prelude::*, warblers_plugin::GRASS_MESH_HANDLE}; mod helper; fn main() { App::new() .add_plugins(( DefaultPlugins, // This plugin is needed to initialize everything for the grass render pipeline WarblersPlugin, // Just a helper plugin for spawning a camera // As in all examples, you can use the wasd keys for movement and qe for rotation helper::SimpleCamera, )) .add_systems(Startup, setup_grass) .add_systems(Update, swap_grass_mesh) .run(); } fn setup_grass( mut commands: Commands, mut meshes: ResMut>, asset_server: Res, ) { // The interesting part in this example // We could use any mesh we want. We should choose a low poly mesh however since we draw a lot of them let grass_mesh: Handle = meshes.add(Capsule3d { radius: 0.3, half_length: 1., ..default() }); // we use a resource to keep track of the handles // so we can swap them later let store = GrassMeshStore { custom_handle: grass_mesh.clone(), default_handle: GRASS_MESH_HANDLE, }; commands.insert_resource(store); let y_map_image = asset_server.load("grass_y_map.png"); let y_map = YMap { y_map: y_map_image }; let density_map = asset_server.load("grass_density_map.png"); let density_map = DensityMap { density_map, density: 1., }; // simple add the grass mesh in the bundle, instead of using the default commands.spawn(WarblersBundle { grass_mesh, density_map, y_map, aabb: Aabb::from_min_max(Vec3::ZERO, Vec3::new(100., 10., 100.)), ..default() }); } /// Used to keep track of the standard mesh and the custom mesh for the grass #[derive(Resource)] struct GrassMeshStore { pub custom_handle: Handle, pub default_handle: Handle, } // Swap the mesh type if TAB is pressed fn swap_grass_mesh( mut commands: Commands, mut queue: Query<(Entity, &mut Handle)>, input: Res>, store: Res, ) { if input.just_pressed(KeyCode::Tab) { for (e, mesh_handle) in queue.iter_mut() { if store.default_handle.id() == mesh_handle.id() { commands.entity(e).insert(store.custom_handle.clone()); } else if store.custom_handle.id() == mesh_handle.id() { commands.entity(e).insert(store.default_handle.clone()); } } } }