Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for bevy-0.13 #7

Merged
merged 7 commits into from
Feb 22, 2024
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 @@ -4,7 +4,7 @@ description = "A Bevy engine plugin for loading Magica Voxel world files and ren
keywords = ["bevy", "voxel", "Magica-Voxel"]
categories = ["game-development", "graphics", "rendering", "rendering::data-formats"]
license = "MIT"
version = "0.12.1"
version = "0.13.0"
repository = "https://github.com/Utsira/bevy_vox_scene"
authors = ["Oliver Dew <[email protected]>"]
edition = "2021"
Expand All @@ -31,7 +31,7 @@ name = "voxel-generation"
required-features = ["generate_voxels"]

[dependencies]
bevy = { version = "0.12.0", default-features = false, features = [
bevy = { version = "0.13.0", default-features = false, features = [
"bevy_render",
"bevy_asset",
"bevy_pbr",
Expand All @@ -46,7 +46,7 @@ thiserror = "1.0.50"
serde = "1.0.193"

[dev-dependencies]
bevy = { version = "0.12.0" }
bevy_panorbit_camera = "0.10.0"
bevy = "0.13.0"
bevy_panorbit_camera = "0.14.0"
rand = "0.8.5"
async-std = { version = "1.12.0", features = ["attributes"] }
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ All Magica Voxel material types except "cloud" are supported. Bevy's screen spac

```toml
[dependencies]
bevy_vox_scene = "0.12.0"
bevy_vox_scene = "0.13.0"
```

Then in code:
Expand Down Expand Up @@ -76,7 +76,8 @@ cargo run --example <example name>

| Bevy version | Magica Voxel version | `bevy-vox-scene` version |
| ------------ | -------------- | --- |
| 0.12 | 0.99.6 | 0.9, 0.10, 0.11, 0.12 |
| 0.12 | 0.99.6 | 0.9, 0.10, 0.11, 0.12 |
| 0.13 | | 0.13 |

## Limitations and workarounds

Expand Down
1 change: 1 addition & 0 deletions examples/basic-model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
EnvironmentMapLight {
diffuse_map: assets.load("pisa_diffuse.ktx2"),
specular_map: assets.load("pisa_specular.ktx2"),
intensity: 500.0,
},
));

Expand Down
1 change: 1 addition & 0 deletions examples/emissive-model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
EnvironmentMapLight {
diffuse_map: assets.load("pisa_diffuse.ktx2"),
specular_map: assets.load("pisa_specular.ktx2"),
intensity: 500.0,
},
));

Expand Down
11 changes: 8 additions & 3 deletions examples/modify-scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
EnvironmentMapLight {
diffuse_map: assets.load("pisa_diffuse.ktx2"),
specular_map: assets.load("pisa_specular.ktx2"),
intensity: 500.0,
},
));
let asset_server = assets.clone();
Expand All @@ -75,7 +76,9 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {

// This closure will be run against every child Entity that gets spawned in the scene
hook: VoxelSceneHook::new(move |entity, commands| {
let Some(name) = entity.get::<Name>() else { return };
let Some(name) = entity.get::<Name>() else {
return;
};
match name.as_str() {
// Node names give the path to the asset, with components separated by /. Here, "black-light" belongs to the "tank" group
"tank/black-light" => {
Expand All @@ -100,13 +103,15 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {

fn toggle_black_light(
mut commands: Commands,
keys: Res<Input<KeyCode>>,
keys: Res<ButtonInput<KeyCode>>,
mut query: Query<(Entity, &mut EmissiveToggle)>,
) {
if keys.get_just_pressed().next().is_none() {
return;
};
let Ok((entity, mut emissive_toggle)) = query.get_single_mut() else { return };
let Ok((entity, mut emissive_toggle)) = query.get_single_mut() else {
return;
};
emissive_toggle.toggle();
commands
.entity(entity)
Expand Down
11 changes: 8 additions & 3 deletions examples/modify-voxels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
EnvironmentMapLight {
diffuse_map: assets.load("pisa_diffuse.ktx2"),
specular_map: assets.load("pisa_specular.ktx2"),
intensity: 500.0,
},
));

commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: 10000.0,
illuminance: 5000.0,
shadows_enabled: true,
..Default::default()
},
Expand All @@ -60,7 +61,9 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
commands.spawn(VoxelSceneHookBundle {
scene: assets.load("study.vox"),
hook: VoxelSceneHook::new(move |entity, commands| {
let Some(name) = entity.get::<Name>() else { return };
let Some(name) = entity.get::<Name>() else {
return;
};
if name.as_str() == "floor" {
commands.insert(Floor);
}
Expand All @@ -72,7 +75,9 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {

fn grow_grass(mut commands: Commands, query: Query<&VoxelModelInstance, With<Floor>>) {
// All the floor tiles are instances of the same model, so we only need one instance
let Some(instance) = query.iter().next() else { return };
let Some(instance) = query.iter().next() else {
return;
};
let region = VoxelRegion {
origin: IVec3::new(0, 4, 0),
size: IVec3::new(64, 8, 64),
Expand Down
1 change: 1 addition & 0 deletions examples/scene-slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
EnvironmentMapLight {
diffuse_map: assets.load("pisa_diffuse.ktx2"),
specular_map: assets.load("pisa_specular.ktx2"),
intensity: 500.0,
},
));

Expand Down
7 changes: 5 additions & 2 deletions examples/ssao-model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
EnvironmentMapLight {
diffuse_map: asset_server.load("pisa_diffuse.ktx2"),
specular_map: asset_server.load("pisa_specular.ktx2"),
intensity: 500.0,
},
SSAOVisible(true),
))
Expand All @@ -70,10 +71,12 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {

fn toggle_ssao(
mut commands: Commands,
keys: Res<Input<KeyCode>>,
keys: Res<ButtonInput<KeyCode>>,
mut query: Query<(Entity, &mut SSAOVisible)>,
) {
let Ok((entity, mut ssao_visible)) = query.get_single_mut() else { return };
let Ok((entity, mut ssao_visible)) = query.get_single_mut() else {
return;
};
if keys.get_just_pressed().next().is_some() {
ssao_visible.0 = !ssao_visible.0;
match ssao_visible.0 {
Expand Down
3 changes: 2 additions & 1 deletion examples/transmission-scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
EnvironmentMapLight {
diffuse_map: assets.load("pisa_diffuse.ktx2"),
specular_map: assets.load("pisa_specular.ktx2"),
intensity: 500.0,
},
));

commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: 10000.0,
illuminance: 5000.0,
shadows_enabled: true,
..Default::default()
},
Expand Down
13 changes: 10 additions & 3 deletions examples/voxel-collisions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
EnvironmentMapLight {
diffuse_map: assets.load("pisa_diffuse.ktx2"),
specular_map: assets.load("pisa_specular.ktx2"),
intensity: 500.0,
},
));
commands.insert_resource(Scenes {
Expand Down Expand Up @@ -108,13 +109,19 @@ fn update_snow(
continue;
}
for (item_xform, item_instance) in scenery.iter() {
let Some(collection) = model_collections.get(item_instance.collection.id()) else { continue };
let Some(model) = collection.model(&item_instance.model_name) else { continue };
let Some(collection) = model_collections.get(item_instance.collection.id()) else {
continue;
};
let Some(model) = collection.model(&item_instance.model_name) else {
continue;
};
let vox_pos =
model.global_point_to_voxel_space(snowflake_xform.translation, item_xform);
// check whether snowflake has landed on something solid
let pos_below_snowflake = vox_pos - IVec3::Y;
let Ok(voxel) = model.get_voxel_at_point(pos_below_snowflake) else { continue };
let Ok(voxel) = model.get_voxel_at_point(pos_below_snowflake) else {
continue;
};
if voxel == Voxel::EMPTY {
continue;
};
Expand Down
59 changes: 30 additions & 29 deletions examples/voxel-generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,40 @@ fn setup_camera(mut commands: Commands, assets: Res<AssetServer>) {
EnvironmentMapLight {
diffuse_map: assets.load("pisa_diffuse.ktx2"),
specular_map: assets.load("pisa_specular.ktx2"),
intensity: 500.0,
},
));
}

fn setup(world: &mut World) {
world.resource_scope(
|world, mut collections: Mut<Assets<VoxelModelCollection>>| {
let palette =
VoxelPalette::new_from_colors(vec![Color::BLUE, Color::ALICE_BLUE, Color::BISQUE]);
let data = SDF::cuboid(Vec3::splat(13.0))
.subtract(SDF::sphere(16.0))
.map_to_voxels(UVec3::splat(32), |d, _| match d {
x if x < -1.0 => Voxel(2),
x if x < 0.0 => Voxel(1),
x if x >= 0.0 => Voxel::EMPTY,
_ => Voxel::EMPTY,
});
let Some(mut collection) = VoxelModelCollection::new(world, palette) else { return };
let model_name = "my sdf model";
let Some(model) = collection.add(data, model_name, world) else { return };
let collection_handle = collections.add(collection);
// The [`VoxelModelInstance`] component is only needed if you want to be able to modify the model at a later time:
world.spawn((
PbrBundle {
mesh: model.mesh,
material: model.material,
..default()
},
VoxelModelInstance {
collection: collection_handle,
model_name: model_name.to_string(),
},
));
let palette = VoxelPalette::from_colors(vec![Color::BLUE, Color::ALICE_BLUE, Color::BISQUE]);
let data = SDF::cuboid(Vec3::splat(13.0))
.subtract(SDF::sphere(16.0))
.map_to_voxels(UVec3::splat(32), |d, _| match d {
x if x < -1.0 => Voxel(2),
x if x < 0.0 => Voxel(1),
x if x >= 0.0 => Voxel::EMPTY,
_ => Voxel::EMPTY,
});
let Some(collection) = VoxelModelCollection::new(world, palette) else {
return;
};
let model_name = "my sdf model";
let Some(model) =
VoxelModelCollection::add(world, data, model_name.to_string(), collection.clone())
else {
return;
};
world.spawn((
PbrBundle {
mesh: model.mesh,
material: model.material,
..default()
},
// The [`VoxelModelInstance`] component is only needed if you want to be able to modify the model at a later time:
VoxelModelInstance {
collection,
model_name: model_name.to_string(),
},
);
));
}
4 changes: 2 additions & 2 deletions src/load/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Default for VoxLoaderSettings {
fn default() -> Self {
Self {
mesh_outer_faces: true,
emission_strength: 2.0,
emission_strength: 4000.0,
uses_srgb: true,
diffuse_roughness: 0.8,
}
Expand Down Expand Up @@ -98,7 +98,7 @@ impl VoxSceneLoader {
info!("Loading {}", load_context.asset_path());

// Palette
let palette = VoxelPalette::new_from_data(
let palette = VoxelPalette::from_data(
&file,
settings.diffuse_roughness,
settings.emission_strength,
Expand Down
4 changes: 3 additions & 1 deletion src/load/parse_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ fn parse_bool(value: Option<String>) -> bool {
}

fn transform_from_frame(frame: &Frame) -> Mat4 {
let Some(position) = frame.position() else { return Mat4::IDENTITY };
let Some(position) = frame.position() else {
return Mat4::IDENTITY;
};
let position = [-position.x as f32, position.z as f32, position.y as f32];
let translation = Mat4::from_translation(Vec3::from_array(position));
let rotation = if let Some(orientation) = frame.orientation() {
Expand Down
8 changes: 6 additions & 2 deletions src/model/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bevy::{
math::Vec3,
render::{
mesh::{Indices, Mesh, VertexAttributeValues},
render_asset::RenderAssetUsages,
render_resource::PrimitiveTopology,
},
};
Expand Down Expand Up @@ -33,7 +34,10 @@ pub(crate) fn mesh_model(voxels: &[VisibleVoxel], data: &VoxelData) -> Mesh {
let mut normals = Vec::with_capacity(num_vertices);
let mut uvs = Vec::with_capacity(num_vertices);

let mut render_mesh = Mesh::new(PrimitiveTopology::TriangleList);
let mut render_mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::default(),
);

for (group, face) in greedy_quads_buffer
.quads
Expand Down Expand Up @@ -74,7 +78,7 @@ pub(crate) fn mesh_model(voxels: &[VisibleVoxel], data: &VoxelData) -> Mesh {
);
render_mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, VertexAttributeValues::Float32x2(uvs));

render_mesh.set_indices(Some(Indices::U32(indices.clone())));
render_mesh.insert_indices(Indices::U32(indices.clone()));

render_mesh
}
Loading
Loading