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

Updated to Bevy 0.13.0 #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ anyhow = "1.0"
glyph_brush_layout = "0.2.3"

[dependencies.bevy]
version = "0.12.0"
version = "0.13.0"
default-features = false
features = ["bevy_render", "bevy_text", "bevy_pbr", "bevy_asset", "bevy_sprite"]

[dev-dependencies]
rand = "0.8.4"

[dev-dependencies.bevy]
version = "0.12.0"
version = "0.13.0"
default-features = false
features = [
"bevy_winit",
Expand Down
4 changes: 2 additions & 2 deletions examples/2d_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: 60.0,
color: Color::WHITE,
};
let text_alignment = TextAlignment::Center;
let text_alignment = JustifyText::Center;

commands.spawn(Camera2dBundle::default());
commands
.spawn(Text2dBundle {
text: Text::from_section("standard 2d text works too", text_style.clone())
.with_alignment(text_alignment),
.with_justify(text_alignment),
..default()
})
.insert(AnimateRotation);
Expand Down
10 changes: 6 additions & 4 deletions examples/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,15 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
mesh: meshes.add(Mesh::from(Plane3d::default().mesh().size(5.0, 5.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3)),
..Default::default()
});
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
mesh: meshes.add(Mesh::from(Cuboid {
half_size: Vec3::new(1.0, 0.5, 1.0),
})),
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
});
Expand Down
15 changes: 7 additions & 8 deletions examples/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Duration;

use bevy::{
diagnostic::{
Diagnostic, DiagnosticId, Diagnostics, DiagnosticsStore, FrameTimeDiagnosticsPlugin,
Diagnostic, DiagnosticPath, Diagnostics, DiagnosticsStore, FrameTimeDiagnosticsPlugin,
LogDiagnosticsPlugin, RegisterDiagnostic,
},
prelude::*,
Expand Down Expand Up @@ -35,7 +35,7 @@ fn main() {
FrameTimeDiagnosticsPlugin::default(),
LogDiagnosticsPlugin::default(),
))
.register_diagnostic(Diagnostic::new(TEXT_MESH_UPDATES, "text_mesh_updates", 20))
.register_diagnostic(Diagnostic::new(TEXT_MESH_UPDATES).with_max_history_length(20)) // , "text_mesh_updates", 20))
.add_systems(Startup, (setup, setup_text_mesh))
.add_systems(Update, (spawn_meshes, update_text_mesh, rotate_camera))
.add_systems(PostUpdate, update_frame_rate)
Expand Down Expand Up @@ -66,8 +66,7 @@ struct FPS;
#[derive(Component)]
struct TextCount;

pub const TEXT_MESH_UPDATES: DiagnosticId =
DiagnosticId::from_u128(1082410928401928501928509128509125);
pub const TEXT_MESH_UPDATES: DiagnosticPath = DiagnosticPath::const_new("measurement");

fn setup_text_mesh(
mut materials: ResMut<Assets<StandardMaterial>>,
Expand Down Expand Up @@ -204,7 +203,7 @@ fn update_text_mesh(
}

state.text_update_count += update_count;
diagnostics.add_measurement(TEXT_MESH_UPDATES, || state.text_update_count as f64);
diagnostics.add_measurement(&TEXT_MESH_UPDATES, || state.text_update_count as f64);
}

fn rotate_camera(mut camera: Query<&mut Transform, With<Camera>>, time: Res<Time>) {
Expand Down Expand Up @@ -236,7 +235,7 @@ fn update_frame_rate(
if timer.fps_update_timer.tick(time.delta()).just_finished() {
if fps.is_some() {
let fps = diagnostics
.get_measurement(FrameTimeDiagnosticsPlugin::FPS)
.get_measurement(&FrameTimeDiagnosticsPlugin::FPS)
.unwrap();

text_mesh.text = format!("FPS={}", fps.value.round() as usize);
Expand All @@ -261,8 +260,8 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
mesh: meshes.add(Mesh::from(Plane3d::default().mesh().size(5.0, 5.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3)),
..Default::default()
});
commands.spawn(PointLightBundle {
Expand Down
6 changes: 3 additions & 3 deletions src/font_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fmt::Display;
use anyhow::Result;
use bevy::asset::io::Reader;
use bevy::asset::{Asset, AssetLoader, BoxedFuture, LoadContext};
use bevy::reflect::{TypePath, TypeUuid};
use bevy::reflect::TypePath;

#[derive(Debug)]
pub struct FontLoaderError;
Expand Down Expand Up @@ -59,8 +59,8 @@ impl AssetLoader for FontLoader {
}
}

#[derive(TypeUuid, TypePath, Asset)]
#[uuid = "5415ac03-d009-471e-89ab-dc0d4e31a8c4"]
#[derive(TypePath, Asset)]
// #[uuid = "5415ac03-d009-471e-89ab-dc0d4e31a8c4"]
pub struct TextMeshFont {
pub(crate) ttf_font: ttf2mesh::TTFFile,
}
Expand Down
58 changes: 32 additions & 26 deletions src/mesh_system.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::render::render_asset::RenderAssetUsages;
use bevy::render::render_resource::PrimitiveTopology;
use bevy::{prelude::*, render::mesh::Indices};

Expand Down Expand Up @@ -57,34 +58,39 @@ pub(crate) fn text_mesh(
}
};

let ttf2_mesh = generate_text_mesh(&text_mesh, &mut font.ttf_font, Some(&mut cache));
let mut new_mesh = true;

match mesh {
Some(mesh) => {
let mesh = meshes.get_mut(mesh).unwrap();
apply_mesh(ttf2_mesh, mesh);

// TODO: handle color updates
}
None => {
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);

apply_mesh(ttf2_mesh, &mut mesh);

commands.entity(entity).insert(PbrBundle {
mesh: meshes.add(mesh),
material: material.map(|m| m.clone()).unwrap_or_else(|| {
materials.add(StandardMaterial {
base_color: text_mesh.style.color,
..Default::default()
})
}),
transform: transform.clone(),
global_transform: global_transform.clone(),
..Default::default()
});
if let Some(mesh) = mesh {
if let Some(asset_mesh) = meshes.get_mut(mesh) {
new_mesh = false;
let ttf2_mesh =
generate_text_mesh(&text_mesh, &mut font.ttf_font, Some(&mut cache));
apply_mesh(ttf2_mesh, asset_mesh);
}
}

if new_mesh {
let mut mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD,
);

let ttf2_mesh = generate_text_mesh(&text_mesh, &mut font.ttf_font, Some(&mut cache));
apply_mesh(ttf2_mesh, &mut mesh);

commands.entity(entity).insert(PbrBundle {
mesh: meshes.add(mesh),
material: material.map(|m| m.clone()).unwrap_or_else(|| {
materials.add(StandardMaterial {
base_color: text_mesh.style.color,
..Default::default()
})
}),
transform: transform.clone(),
global_transform: global_transform.clone(),
..Default::default()
});
}
}
}

Expand Down Expand Up @@ -141,5 +147,5 @@ fn apply_mesh(mesh_data: MeshData, mesh: &mut Mesh) {
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, mesh_data.vertices);
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, mesh_data.normals);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, mesh_data.uvs);
mesh.set_indices(Some(Indices::U32(mesh_data.indices)));
mesh.insert_indices(Indices::U32(mesh_data.indices));
}