Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ documentation = "https://docs.rs/bevy_generative"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = { version = "0.14.0", default-features = false, features = [
bevy = { version = "0.16.1", default-features = false, features = [
"bevy_core_pipeline",
"bevy_pbr",
"bevy_ui",
] }
colorgrad = "0.6.2"
colorgrad = "0.7.2"
gltf = "1.3.0"
image = "0.25"
noise = { version = "0.9.0", git = "https://github.com/Razaekel/noise-rs.git" }
rfd = "0.12.1"
rfd = "0.15.4"
serde = "1.0.195"
serde_json = "1.0.111"
wasm-bindgen = "0.2.89"

[dev-dependencies]
bevy = "0.14.0"
bevy = "0.16.1"

# Enable max optimizations for dependencies, but not for our code:
[profile.dev.package."*"]
Expand Down
68 changes: 38 additions & 30 deletions examples/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,47 @@ fn main() {
}

fn setup(mut commands: Commands) {
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
let light_bundle = (
PointLight::default(),
Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
);

commands.spawn(light_bundle);

let camera_bundle = (
Camera3d::default(),
Projection::Perspective(PerspectiveProjection::default()),
Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
);
commands.spawn(camera_bundle);

commands.spawn(TerrainBundle::default());

commands
.spawn(ButtonBundle {
style: Style {
padding: UiRect::all(Val::Px(12.)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
margin: UiRect::all(Val::Px(12.)),
..default()
},
border_radius: BorderRadius::all(Val::Px(5.)),
background_color: NORMAL_BUTTON.into(),
let button_bundle = (
Button,
Node {
padding: UiRect::all(Val::Px(12.)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
margin: UiRect::all(Val::Px(12.)),
..default()
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Export",
TextStyle {
font_size: 30.0,
color: BUTTON_TEXT.into(),
..default()
},
));
});
},
BorderRadius::all(Val::Px(5.)),
BackgroundColor(NORMAL_BUTTON.into()),
);

commands.spawn(button_bundle).with_children(|parent| {
let text_bundle = (
Text::from("Export"),
TextFont {
font_size: 30.0,
..Default::default()
},
TextColor(BUTTON_TEXT.into()),
);

parent.spawn(text_bundle);
});
}

fn export_button(
Expand Down
3 changes: 2 additions & 1 deletion examples/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn main() {
}

fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
let camera_bundle = (Camera2d::default(),);
commands.spawn(camera_bundle);
commands.spawn(MapBundle::default());
}
21 changes: 13 additions & 8 deletions examples/planet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ fn main() {
}

fn setup(mut commands: Commands) {
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
let light_bundle = (
PointLight::default(),
Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
);

commands.spawn(light_bundle);

let camera_bundle = (
Camera3d::default(),
Projection::Perspective(PerspectiveProjection::default()),
Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
);
commands.spawn(camera_bundle);
commands.spawn(PlanetBundle::default());
}
21 changes: 13 additions & 8 deletions examples/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ fn main() {
}

fn setup(mut commands: Commands) {
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
let light_bundle = (
PointLight::default(),
Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
);

commands.spawn(light_bundle);

let camera_bundle = (
Camera3d::default(),
Projection::Perspective(PerspectiveProjection::default()),
Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
);
commands.spawn(camera_bundle);
commands.spawn(TerrainBundle {
terrain: bevy_generative::terrain::Terrain {
resolution: 4,
Expand Down
40 changes: 20 additions & 20 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@
//! }
//!
//! fn setup(mut commands: Commands) {
//! commands.spawn(Camera2dBundle::default());
//! commands.spawn(MapBundle::default());
//! let camera_bundle = (
//! Camera2d::default(),
//! );
//! commands.spawn(camera_bundle);
//! commands.spawn(MapBundle::default());
//! }
//! ```
use bevy::{
prelude::*,
render::{render_asset::RenderAssetUsages, render_resource::TextureFormat},
};
use colorgrad::{Gradient, LinearGradient};
use image::{imageops::FilterType, DynamicImage, Pixel};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -64,7 +68,7 @@ pub struct MapBundle {
/// See [`Map`](./struct.Map.html)
pub map: Map,
/// See [`ImageBundle`](../../bevy/prelude/struct.ImageBundle.html)
pub image_bundle: ImageBundle,
pub image_bundle: ImageNode,
}

impl Default for Map {
Expand All @@ -79,38 +83,34 @@ impl Default for Map {
}
}
}
fn generate_map(mut images: ResMut<Assets<Image>>, mut query: Query<(&mut Map, &mut UiImage)>) {
fn generate_map(mut images: ResMut<Assets<Image>>, mut query: Query<(&mut Map, &mut ImageNode)>) {
for (mut map, mut ui_image) in &mut query {
map.noise.size = map.size;
let noise_values = generate_noise_map(&map.noise);
let noise = &mut map.noise;

let mut colors: Vec<colorgrad::Color> = Vec::with_capacity(noise.regions.len());
let mut domain: Vec<f64> = Vec::with_capacity(noise.regions.len());
let mut domain: Vec<f32> = Vec::with_capacity(noise.regions.len());
for region in &noise.regions {
colors.push(colorgrad::Color {
r: f64::from(region.color[0]) / 255.0,
g: f64::from(region.color[1]) / 255.0,
b: f64::from(region.color[2]) / 255.0,
a: f64::from(region.color[3]) / 255.0,
r: f32::from(region.color[0]) / 255.0,
g: f32::from(region.color[1]) / 255.0,
b: f32::from(region.color[2]) / 255.0,
a: f32::from(region.color[3]) / 255.0,
});
domain.push(region.position);
}
let mut grad = colorgrad::CustomGradient::new()
let grad = colorgrad::GradientBuilder::new()
.colors(&colors)
.domain(&domain)
.build()
.build::<LinearGradient>()
.unwrap_or_else(|_| {
colorgrad::CustomGradient::new()
colorgrad::GradientBuilder::new()
.colors(&colors)
.build()
.build::<LinearGradient>()
.expect("Gradient generation failed")
});

if noise.gradient.segments != 0 {
grad = grad.sharp(noise.gradient.segments, noise.gradient.smoothness);
}

let mut gradient_buffer = image::ImageBuffer::from_pixel(
noise.gradient.size[0],
noise.gradient.size[1],
Expand All @@ -119,7 +119,7 @@ fn generate_map(mut images: ResMut<Assets<Image>>, mut query: Query<(&mut Map, &

for (x, _, pixel) in gradient_buffer.enumerate_pixels_mut() {
let rgba = grad
.at(f64::from(x) * 100.0 / f64::from(noise.gradient.size[0]))
.at((f64::from(x) * 100.0 / f64::from(noise.gradient.size[0])) as f32)
.to_rgba8();
pixel.blend(&image::Rgba(rgba));
}
Expand All @@ -142,7 +142,7 @@ fn generate_map(mut images: ResMut<Assets<Image>>, mut query: Query<(&mut Map, &

for (x, y, pixel) in image_buffer.enumerate_pixels_mut() {
let height = noise_values[x as usize][y as usize];
let target_color = grad.at(height).to_rgba8();
let target_color = grad.at(height as f32).to_rgba8();
pixel.blend(&image::Rgba(target_color));
}
if !map.same_size {
Expand All @@ -167,6 +167,6 @@ fn generate_map(mut images: ResMut<Assets<Image>>, mut query: Query<(&mut Map, &
.convert(TextureFormat::Rgba8UnormSrgb)
.expect("Could not convert to Rgba8UnormSrgb");

ui_image.texture = images.add(map_texture);
ui_image.image = images.add(map_texture);
}
}
2 changes: 1 addition & 1 deletion src/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub struct Region {
/// Label of the region
pub label: String,
/// Percentage below which the region should render
pub position: f64,
pub position: f32,
/// Color representing the region
pub color: [u8; 4],
}
Expand Down
Loading
Loading