Skip to content

Commit

Permalink
upgrade to bevy 0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Nov 13, 2023
1 parent 77ffa99 commit ddcf296
Show file tree
Hide file tree
Showing 11 changed files with 458 additions and 421 deletions.
660 changes: 363 additions & 297 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ repository.workspace = true
edition.workspace = true

[dependencies]
bevy = "0.11.3"
bevy_rapier3d = { version = "0.22.0", features = ["simd-stable"] }
bevy_vrm = "0.0.4"
bevy = "0.12.0"
bevy_rapier3d = { version = "0.23.0", features = ["simd-stable"] }
bevy_vrm = "0.0.5"
cfg-if = "1.0.0"
tracing = "0.1.40"
106 changes: 32 additions & 74 deletions app/examples/demo.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
use std::f32::consts::PI;

use bevy::{
prelude::*,
render::{
mesh::VertexAttributeValues,
render_resource::{AddressMode, Extent3d, SamplerDescriptor},
texture::ImageSampler,
},
};
use bevy_rapier3d::prelude::*;
use bevy::{prelude::*, render::mesh::VertexAttributeValues};
use bevy_rapier3d::prelude::{Real, *};

fn main() {
let options = unavi_app::StartOptions {
asset_folder: "../assets".to_string(),
file_path: "../assets".to_string(),
callback: Some(Box::new(callback)),
..default()
};
Expand All @@ -21,8 +14,7 @@ fn main() {
}

fn callback(app: &mut App) {
app.add_systems(Startup, setup_world)
.add_systems(Update, set_texture_tiled);
app.add_systems(Startup, setup_world);
}

const GROUND_SIZE: f32 = 40.0;
Expand All @@ -37,39 +29,40 @@ fn setup_world(
mut materials: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
let dev_texture = asset_server.load("images/dev/texture_07.png");

let ground_texture_scale = GROUND_SIZE / 4.0;
{
let ground_texture = asset_server.load("images/dev/texture_07.png");
let ground_texture_scale = GROUND_SIZE / 4.0;

let mut ground_mesh = Mesh::from(shape::Plane {
size: GROUND_SIZE,
..default()
});
let mut ground_mesh = Mesh::from(shape::Plane {
size: GROUND_SIZE,
..default()
});

match ground_mesh.attribute_mut(Mesh::ATTRIBUTE_UV_0).unwrap() {
VertexAttributeValues::Float32x2(uvs) => {
for uv in uvs {
uv[0] *= ground_texture_scale;
uv[1] *= ground_texture_scale;
match ground_mesh.attribute_mut(Mesh::ATTRIBUTE_UV_0).unwrap() {
VertexAttributeValues::Float32x2(uvs) => {
for uv in uvs {
uv[0] *= ground_texture_scale;
uv[1] *= ground_texture_scale;
}
}
_ => panic!(),
}
_ => panic!(),
}

commands.spawn((
RigidBody::Fixed,
Collider::cuboid(GROUND_SIZE / 2.0, GROUND_THICKNESS / 2.0, GROUND_SIZE / 2.0),
PbrBundle {
mesh: meshes.add(ground_mesh),
material: materials.add(StandardMaterial {
base_color_texture: Some(dev_texture.clone()),
commands.spawn((
RigidBody::Fixed,
Collider::cuboid(GROUND_SIZE / 2.0, GROUND_THICKNESS / 2.0, GROUND_SIZE / 2.0),
PbrBundle {
mesh: meshes.add(ground_mesh),
material: materials.add(StandardMaterial {
base_color_texture: Some(ground_texture.clone()),
..default()
}),
transform: Transform::from_xyz(0.0, -0.1, 0.0),
..default()
}),
transform: Transform::from_xyz(0.0, -0.1, 0.0),
..default()
},
RepeatTexture,
));
},
RepeatTexture,
));
}

commands.spawn((SceneBundle {
scene: asset_server.load("models/catbot.vrm#Scene0"),
Expand Down Expand Up @@ -125,41 +118,6 @@ fn setup_world(
}
}

// https://github.com/bevyengine/bevy/issues/399
pub fn set_texture_tiled(
mut commands: Commands,
repeat_textures: Query<(Entity, &Handle<StandardMaterial>), With<RepeatTexture>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut images: ResMut<Assets<Image>>,
) {
for (entity, handle) in repeat_textures.iter() {
if let Some(material) = materials.get_mut(handle) {
if let Some(base_color_texture_handle) = &material.base_color_texture {
if let Some(image) = images.get_mut(base_color_texture_handle) {
match &mut image.sampler_descriptor {
ImageSampler::Default => {
image.sampler_descriptor =
ImageSampler::Descriptor(SamplerDescriptor {
address_mode_u: AddressMode::Repeat,
address_mode_v: AddressMode::Repeat,
address_mode_w: AddressMode::Repeat,
..default()
});
}
ImageSampler::Descriptor(sampler_descriptor) => {
sampler_descriptor.address_mode_u = AddressMode::Repeat;
sampler_descriptor.address_mode_v = AddressMode::Repeat;
sampler_descriptor.address_mode_w = AddressMode::Repeat;
}
}

commands.entity(entity).remove::<RepeatTexture>();
}
}
}
}
}

#[derive(Bundle)]
struct PhysShapeBundle {
rigid_body: RigidBody,
Expand Down
9 changes: 4 additions & 5 deletions app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ mod world;
const FIXED_TIMESTEP: f32 = 1.0 / 60.0;

pub struct StartOptions {
pub asset_folder: String,
pub file_path: String,
pub callback: Option<Box<dyn FnOnce(&mut App)>>,
}

impl Default for StartOptions {
fn default() -> Self {
Self {
asset_folder: "assets".to_string(),
file_path: "assets".to_string(),
callback: None,
}
}
Expand All @@ -35,7 +35,7 @@ pub fn start(options: StartOptions) {
..default()
})
.set(AssetPlugin {
asset_folder: options.asset_folder,
file_path: options.file_path,
..default()
}),
RapierPhysicsPlugin::<NoUserData>::default(),
Expand All @@ -46,8 +46,7 @@ pub fn start(options: StartOptions) {
player::PlayerPlugin,
// bevy::diagnostic::LogDiagnosticsPlugin::default(),
// bevy::diagnostic::FrameTimeDiagnosticsPlugin::default(),
))
.insert_resource(FixedTime::new_from_secs(FIXED_TIMESTEP));
));

if let Some(callback) = options.callback {
callback(&mut app);
Expand Down
8 changes: 4 additions & 4 deletions app/src/player/controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn spawn_player(mut commands: Commands) {
}

pub fn apply_yaw(mut yaws: EventReader<YawEvent>, mut query: Query<&mut Transform, With<YawTag>>) {
if let Some(yaw) = yaws.iter().next() {
if let Some(yaw) = yaws.read().next() {
for mut transform in query.iter_mut() {
transform.rotation = Quat::from_rotation_y(**yaw);
}
Expand All @@ -105,7 +105,7 @@ pub fn apply_pitch(
mut pitches: EventReader<PitchEvent>,
mut query: Query<&mut Transform, With<PitchTag>>,
) {
if let Some(pitch) = pitches.iter().next() {
if let Some(pitch) = pitches.read().next() {
for mut transform in query.iter_mut() {
transform.rotation = Quat::from_rotation_x(**pitch);
}
Expand All @@ -127,7 +127,7 @@ pub fn move_player(
look_directions: Query<&LookDirection>,
) {
let xz = Vec3::new(1.0, 0.0, 1.0);
let dt = time.raw_elapsed_seconds() - *last_time;
let dt = time.elapsed_seconds() - *last_time;

for (mut player, look_entity, mut controller, output) in players.iter_mut() {
let look_direction = look_directions
Expand Down Expand Up @@ -185,7 +185,7 @@ pub fn move_player(
player.input = InputState::default();
}

*last_time = time.raw_elapsed_seconds();
*last_time = time.elapsed_seconds();
}

const VOID_LEVEL: f32 = -50.0;
Expand Down
2 changes: 1 addition & 1 deletion app/src/settings/shadows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn set_shadow_config(
mut directional_shadow_config: ResMut<DirectionalLightShadowMap>,
mut directional_lights: Query<&mut CascadeShadowConfig, With<DirectionalLight>>,
) {
info!("Updating shadow config to {:?}", settings.shadow_quality);
info!("Setting shadow config to {:?}", settings.shadow_quality);

directional_shadow_config.size = settings.shadow_quality.size();

Expand Down
7 changes: 5 additions & 2 deletions app/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ pub struct WorldPlugin;

impl Plugin for WorldPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, (setup_world, skybox::setup_skybox))
.add_systems(Update, (skybox::create_skybox, skybox::process_skybox));
app.add_systems(Startup, (setup_world, skybox::create_skybox))
.add_systems(
Update,
(skybox::add_skybox_to_cameras, skybox::process_cubemap),
);
}
}

Expand Down
54 changes: 21 additions & 33 deletions app/src/world/skybox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,34 @@ use bevy::asset::LoadState;
use bevy::core_pipeline::Skybox;
use bevy::prelude::*;
use bevy::render::render_resource::{TextureViewDescriptor, TextureViewDimension};
use bevy_vrm::mtoon::MtoonMainCamera;

#[derive(Resource)]
pub struct Cubemap {
is_loaded: bool,
image_handle: Handle<Image>,
}

pub fn setup_skybox(mut commands: Commands) {
pub fn create_skybox(asset_server: Res<AssetServer>, mut commands: Commands) {
commands.insert_resource(Cubemap {
is_loaded: false,
image_handle: default(),
image_handle: asset_server.load("images/skybox-1-512.png"),
});
}

const SKYBOX_URI: &str = "images/skybox-1-512.png";

pub fn create_skybox(
asset_server: Res<AssetServer>,
pub fn add_skybox_to_cameras(
mut commands: Commands,
mut cubemap: ResMut<Cubemap>,
cameras: Query<Entity, (With<Camera3d>, Without<Skybox>)>,
cubemap: ResMut<Cubemap>,
cameras: Query<Entity, (With<MtoonMainCamera>, Without<Skybox>)>,
) {
if cameras.is_empty() {
return;
for camera in cameras.iter() {
commands
.entity(camera)
.insert(Skybox(cubemap.image_handle.clone()));
}

info!("Loading skybox {}", SKYBOX_URI);

let skybox_handle = asset_server.load(SKYBOX_URI);

let ent = cameras.single();

commands.entity(ent).insert(Skybox(skybox_handle.clone()));

cubemap.is_loaded = false;
cubemap.image_handle = skybox_handle;
}

pub fn process_skybox(
pub fn process_cubemap(
asset_server: Res<AssetServer>,
mut images: ResMut<Assets<Image>>,
mut cubemap: ResMut<Cubemap>,
Expand All @@ -50,27 +39,26 @@ pub fn process_skybox(
return;
}

if asset_server.get_load_state(cubemap.image_handle.clone_weak()) != LoadState::Loaded {
return;
match asset_server.get_load_state(&cubemap.image_handle) {
Some(load_state) => {
if load_state != LoadState::Loaded {
return;
}
}
None => return,
}

// Load cubemap
let image = images.get_mut(&cubemap.image_handle);

let image = match image {
let image = match images.get_mut(&cubemap.image_handle) {
Some(image) => image,
None => {
warn!("Failed to load skybox image");
warn!("Failed to get skybox image");
return;
}
};

// NOTE: PNGs do not have any metadata that could indicate they contain a cubemap texture,
// so they appear as one texture. The following code reconfigures the texture as necessary.
if image.texture_descriptor.array_layer_count() == 1 {
image.reinterpret_stacked_2d_as_array(
image.texture_descriptor.size.height / image.texture_descriptor.size.width,
);
image.reinterpret_stacked_2d_as_array(image.height() / image.width());
image.texture_view_descriptor = Some(TextureViewDescriptor {
dimension: Some(TextureViewDimension::Cube),
..default()
Expand Down
24 changes: 24 additions & 0 deletions assets/images/dev/texture_07.png.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(
meta_format_version: "1.0",
asset: Load(
loader: "bevy_render::texture::image_loader::ImageLoader",
settings: (
format: FromExtension,
is_srgb: true,
sampler: Descriptor((
label: None,
address_mode_u: Repeat,
address_mode_v: Repeat,
address_mode_w: Repeat,
mag_filter: Nearest,
min_filter: Nearest,
mipmap_filter: Nearest,
lod_min_clamp: 0.0,
lod_max_clamp: 32.0,
compare: None,
anisotropy_clamp: 1,
border_color: None,
)),
),
),
)
1 change: 0 additions & 1 deletion native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ repository.workspace = true
edition.workspace = true

[dependencies]
bevy = "0.11.3"
unavi-app = { path = "../app" }
2 changes: 1 addition & 1 deletion native/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

fn main() {
unavi_app::start(unavi_app::StartOptions {
asset_folder: "../assets".to_string(),
file_path: "../assets".to_string(),
..Default::default()
});
}

0 comments on commit ddcf296

Please sign in to comment.