Skip to content

Commit

Permalink
Migrate to bevy 0.14 (#14)
Browse files Browse the repository at this point in the history
* Migrate to bevy 0.14

Signed-off-by: Luca Della Vedova <[email protected]>

* Cargo fmt

Signed-off-by: Luca Della Vedova <[email protected]>

* Cleanup dependencies

Signed-off-by: Luca Della Vedova <[email protected]>

* Add format check to workflow

* Color::rgb -> Color::srgb in wireframe example

---------

Signed-off-by: Luca Della Vedova <[email protected]>
Co-authored-by: Niklas Cathor <[email protected]>
  • Loading branch information
luca-della-vedova and nilclass authored Jul 19, 2024
1 parent 70e56af commit ea25e80
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Format check
run: cargo fmt --verbose --check
- name: Build
run: cargo build --verbose
- name: Build example
Expand Down
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ resolver = "2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = { git = "https://github.com/bevyengine/bevy", default-features = false, features = [
bevy = { version = "0.14", default-features = false, features = [
"bevy_asset",
"bevy_render",
] }
anyhow = "1.0"
thiserror = "1.0"
stl_io = "0.7.0"
futures-lite = "2.0.1"

[dev-dependencies]
bevy = { git = "https://github.com/bevyengine/bevy", default-features = false, features = [
bevy = { version = "0.14", default-features = false, features = [
"bevy_asset",
"bevy_core_pipeline",
"bevy_pbr",
Expand Down
9 changes: 6 additions & 3 deletions examples/spinning_disc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ fn main() {
.insert_resource(Msaa::Sample4)
.add_plugins(DefaultPlugins)
.add_plugins(StlPlugin)
.insert_resource(SpinTimer(Timer::from_seconds(1.0 / 60.0, TimerMode::Repeating)))
.insert_resource(SpinTimer(Timer::from_seconds(
1.0 / 60.0,
TimerMode::Repeating,
)))
.add_systems(Startup, setup)
.add_systems(Update, spin_disc)
.run();
Expand All @@ -29,11 +32,11 @@ fn setup(
commands.spawn((
PbrBundle {
mesh: asset_server.load("models/disc.stl"),
material: materials.add(Color::rgb(0.9, 0.4, 0.3)),
material: materials.add(Color::srgb(0.9, 0.4, 0.3)),
transform: Transform::from_rotation(Quat::from_rotation_z(0.0)),
..Default::default()
},
Disc { angle: 0.0 }
Disc { angle: 0.0 },
));
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(30.0, 0.0, 20.0),
Expand Down
9 changes: 6 additions & 3 deletions examples/spinning_disc_wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ fn main() {
.insert_resource(Msaa::Sample4)
.add_plugins(DefaultPlugins)
.add_plugins(StlPlugin)
.insert_resource(SpinTimer(Timer::from_seconds(1.0 / 60.0, TimerMode::Repeating)))
.insert_resource(SpinTimer(Timer::from_seconds(
1.0 / 60.0,
TimerMode::Repeating,
)))
.add_systems(Startup, setup)
.add_systems(Update, spin_disc)
.run();
Expand All @@ -29,11 +32,11 @@ fn setup(
commands.spawn((
PbrBundle {
mesh: asset_server.load("models/disc.stl#wireframe"),
material: materials.add(Color::rgb(0.9, 0.4, 0.3)),
material: materials.add(Color::srgb(0.9, 0.4, 0.3)),
transform: Transform::from_rotation(Quat::from_rotation_z(0.0)),
..Default::default()
},
Disc { angle: 0.0 }
Disc { angle: 0.0 },
));
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(30.0, 0.0, 20.0),
Expand Down
45 changes: 21 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use anyhow::Result;
use futures_lite::AsyncReadExt;
use std::io::Cursor;
use thiserror::Error;

use bevy::{
asset::{io::Reader, AssetLoader, LoadContext},
asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext},
prelude::*,
render::{
mesh::{Indices, Mesh, VertexAttributeValues},
render_resource::PrimitiveTopology,
render_asset::RenderAssetUsages,
render_resource::PrimitiveTopology,
},
utils::BoxedFuture,
};

pub struct StlPlugin;
Expand All @@ -29,26 +26,23 @@ impl AssetLoader for StlLoader {
type Asset = Mesh;
type Settings = ();
type Error = StlError;
fn load<'a>(
async fn load<'a>(
&'a self,
reader: &'a mut Reader,
reader: &'a mut Reader<'_>,
_settings: &'a (),
#[allow(unused)]
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
let mut reader = Cursor::new(bytes);
let stl = stl_io::read_stl(&mut reader)?;

#[cfg(feature = "wireframe")]
load_context.labeled_asset_scope("wireframe".to_string(), |_load_context| {
stl_to_wireframe_mesh(&stl)
});

Ok(stl_to_triangle_mesh(&stl))
})
#[allow(unused)] load_context: &'a mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
let mut reader = Cursor::new(bytes);
let stl = stl_io::read_stl(&mut reader)?;

#[cfg(feature = "wireframe")]
load_context.labeled_asset_scope("wireframe".to_string(), |_load_context| {
stl_to_wireframe_mesh(&stl)
});

Ok(stl_to_triangle_mesh(&stl))
}

fn extensions(&self) -> &[&str] {
Expand All @@ -64,7 +58,10 @@ enum StlError {
}

fn stl_to_triangle_mesh(stl: &stl_io::IndexedMesh) -> Mesh {
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::default());
let mut mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::default(),
);

let vertex_count = stl.faces.len() * 3;

Expand Down

0 comments on commit ea25e80

Please sign in to comment.