Skip to content

Commit

Permalink
Merge pull request #63 from EmiOnGit/remove_explizit
Browse files Browse the repository at this point in the history
Remove explizit
  • Loading branch information
EmiOnGit committed Sep 1, 2023
2 parents 7db7bfd + 29c1be5 commit 9fb9f4e
Show file tree
Hide file tree
Showing 19 changed files with 63 additions and 490 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## 0.4
* Support for bevy 0.11
* The color of the grass is now a `Component`, meaning it can be configured on a `Chunk` basis.
* Remove the `WarblersExplicitBundle`. Only textures are now supported.
## 0.3.2
This release mainly includes proper support for wasm builds,
as well as simplifications in the code and better documentation.
Expand Down
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ features = [ "bevy_core_pipeline", "bevy_render", "bevy_asset", "bevy_pbr" , "pn
[dev-dependencies]
bevy = { version = "0.11", default-features = false, features = ["bevy_winit","x11", "ktx2", "zstd", "tonemapping_luts"] }

[[example]]
name = "load_explicit"
path = "examples/load_explicit.rs"

[[example]]
name = "editor"
path = "examples/editor.rs"
Expand Down
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# <img src="branding/warbler_display.svg" width="500">
[![crates.io](https://img.shields.io/badge/crates.io-v0.3.2-orange)](https://crates.io/crates/warbler_grass)
[![docs.io](https://img.shields.io/badge/docs-v0.3.2-green)](https://docs.rs/warbler_grass/0.3.2/warbler_grass/)
[![crates.io](https://img.shields.io/badge/crates.io-v0.4-orange)](https://crates.io/crates/warbler_grass)
[![docs.io](https://img.shields.io/badge/docs-v0.4-green)](https://docs.rs/warbler_grass/0.4/warbler_grass/)

A `bevy` plugin for ergonomic integration of grass in 3d games.

Expand All @@ -19,7 +19,7 @@ Another cool project using this crate is the [foxtrot](https://github.com/janhoh
Add `warbler_grass` as dependency to your project
```toml
[dependencies]
warbler_grass = "0.3.1"
warbler_grass = "0.4"
```
### Add grass to your game:
```rust
Expand Down Expand Up @@ -94,12 +94,6 @@ Note that you can press `TAB` to change the mesh in the demo
```shell
cargo run --example grass_mesh
```
### Load explicit
You don't want to work with all the weird maps. All you want to do is define explicit positions for the grass blades to spawn in?
You can definitly also do that
```shell
cargo run --example load_explicit
```
### Many chunks
You'd like to see what this crate can do? Run this demo to see many chunks loaded at once.
This example is also great to demonstrate the frustum culling of the meshes
Expand Down
50 changes: 0 additions & 50 deletions examples/load_explicit.rs

This file was deleted.

101 changes: 0 additions & 101 deletions src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use bevy::{
asset::Handle,
ecs::{bundle::Bundle, component::Component, query::QueryItem},
math::Vec3,
prelude::Color,
render::{
extract_component::ExtractComponent, mesh::Mesh, prelude::SpatialBundle, primitives::Aabb,
Expand All @@ -12,11 +11,6 @@ use bevy::{
use crate::{density_map::DensityMap, height_map::HeightMap, warblers_plugin::GRASS_MESH_HANDLE};

/// This [`Bundle`] spawns a grass chunk in the world.
///
/// This is the recommended way to spawn grass in games.
/// # Note
/// If you only want to input explicit positions of the grass blades you can also use
/// the [`WarblersExplicitBundle`].
#[derive(Bundle)]
pub struct WarblersBundle {
/// The [`Mesh`] of the grass blades
Expand Down Expand Up @@ -97,98 +91,3 @@ impl ExtractComponent for WarblerHeight {
}
}
}

/// Used to define the positions of all the grass blades explicitly
///
/// Can be used with the [`WarblersExplicitBundle`]
///
/// # Example
/// ```rust
/// use warbler_grass::prelude::Grass;
/// use bevy::prelude::Vec3;
///
/// let mut positions = Vec::with_capacity(10 * 10);
/// // let's make a simple 10x10 grid
/// for x in 0..10 {
/// for y in 0..10 {
/// positions.push(Vec3::new(x as f32,0., y as f32));
/// }
/// }
/// let height = 2.;
///
/// // One way to create grass
/// let grass1 = Grass::new(positions.clone(), height);
///
/// // Another way
/// let grass2 = Grass::from(&positions[..]).with_height(height);
/// assert_eq!(grass1, grass2);
/// ```
#[derive(Component, Clone, PartialEq, Debug)]
pub struct Grass {
/// The positions of each grass blade defined
///
/// The positions are always relative to the entity [`Transform`] component.
pub positions: Vec<Vec3>,
/// The height of the grass blades
pub height: f32,
}
impl Default for Grass {
fn default() -> Self {
Self {
positions: Default::default(),
height: 1.,
}
}
}
impl Grass {
/// Creates a new [`Grass`] instance
pub fn new(positions: Vec<Vec3>, height: f32) -> Self {
Grass { positions, height }
}
/// sets the [`Grass`] height and returns itself after
pub fn with_height(mut self, height: f32) -> Self {
self.height = height;
self
}
}
/// Can be used to create grass from a slice of positions
///
/// The height will be set to the default height
impl From<&[Vec3]> for Grass {
fn from(value: &[Vec3]) -> Self {
Self {
positions: value.into(),
height: Default::default(),
}
}
}
/// A bundle spawning a grass chunk in the world
///
/// It uses explicit positions of all grass blades to generate the them
/// For an example take a look at the `load_explicit` example
#[derive(Bundle)]
pub struct WarblersExplicitBundle {
/// The [`Mesh`] of the grass blades
///
/// Defaults to the mesh seen in the examples.
/// The mesh may also be changed at runtime.
/// You might want to take a look at the
/// `grass_mesh` example for that
pub grass_mesh: Handle<Mesh>,
/// The explicit positions of the grass blades
pub grass: Grass,
/// The color of the grass
pub grass_color: GrassColor,
pub spatial: SpatialBundle,
}

impl Default for WarblersExplicitBundle {
fn default() -> Self {
Self {
grass_mesh: GRASS_MESH_HANDLE.typed(),
grass_color: GrassColor::default(),
grass: Grass::default(),
spatial: Default::default(),
}
}
}
14 changes: 2 additions & 12 deletions src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::{
prelude::{Assets, ComputedVisibility, Handle, Plugin, Query, Res, Update},
};

use crate::{dithering::DitheredBuffer, prelude::Grass};
use crate::dithering::DitheredBuffer;

/// A [`Plugin`] that logs the blades drawn in each frame.
///
Expand Down Expand Up @@ -41,7 +41,6 @@ impl WarblerDiagnosticsPlugin {
/// Calculates the amount of blades that are drawn this frame and logs them
fn measure_blades(
blades: Query<(&Handle<DitheredBuffer>, &ComputedVisibility)>,
explicit_blades: Query<(&Grass, &ComputedVisibility)>,
dither: Res<Assets<DitheredBuffer>>,
mut diagnostics: Diagnostics,
) {
Expand All @@ -54,15 +53,6 @@ impl WarblerDiagnosticsPlugin {
.map(|buffer| buffer.positions.len() as u32)
.sum();

// entities spawned with the WarblersExplicitBundle
let count_explicit: u32 = explicit_blades
.iter()
.filter(|(_grass, visible)| visible.is_visible())
.map(|(grass, _visible)| grass.positions.len() as u32)
.sum();

diagnostics.add_measurement(Self::GRASS_BLADE_COUNT, || {
count as f64 + count_explicit as f64
});
diagnostics.add_measurement(Self::GRASS_BLADE_COUNT, || count as f64);
}
}
4 changes: 2 additions & 2 deletions src/dithering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) fn dither_density_map(
if field_size.length() < 0.0001 {
return None;
}
let Ok(dynamic_image) = image.clone().try_into_dynamic() else {
let Ok(dynamic_image) = image.clone().try_into_dynamic() else {
return None;
};
// Capacity is not precise but should be a good estimate
Expand Down Expand Up @@ -130,7 +130,7 @@ pub(crate) fn add_dither_to_density(
let xz = aabb.half_extents.xz() * 2.;
let Some(buffer) = dither_density_map(image, density_map.density, xz) else {
warn!("Couldn't dither density map. Maybe the image format is not supported?");
continue
continue;
};
let handle = dithered.add(buffer);
commands.entity(e).insert(handle);
Expand Down
4 changes: 2 additions & 2 deletions src/editor/brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Stencil;

impl BrushBehavior for Stencil {
fn draw(&self, image: &mut Image, position: Vec2, brush_size: u32, strength: f32) {
let Ok(dynamic_image) = image.clone().try_into_dynamic() else {
let Ok(dynamic_image) = image.clone().try_into_dynamic() else {
warn!("couldn't convert image");
return;
};
Expand All @@ -32,7 +32,7 @@ impl BrushBehavior for Stencil {
pub struct Airbrush;
impl BrushBehavior for Airbrush {
fn draw(&self, image: &mut Image, position: Vec2, brush_size: u32, strength: f32) {
let Ok(dynamic_image) = image.clone().try_into_dynamic() else {
let Ok(dynamic_image) = image.clone().try_into_dynamic() else {
warn!("couldn't convert image");
return;
};
Expand Down
2 changes: 1 addition & 1 deletion src/editor/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn check_for_save_files(
};
let Some(image) = images.get(image_handle) else {
info!("Image was not yet loaded. Saving failed");
return
return;
};
match saver.save(image) {
Ok(_) => info!("Successfully saved image to {:?}", saver.path),
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub mod editor;

mod density_map;
mod height_map;
mod update;

/// Contains the [`HeightMap`](crate::maps::HeightMap) and [`DensityMap`](crate::maps::DensityMap) component
pub mod maps {
Expand Down
Loading

0 comments on commit 9fb9f4e

Please sign in to comment.