Skip to content

Commit

Permalink
Update to Bevy 0.14 (#18)
Browse files Browse the repository at this point in the history
Update to Bevy 0.14 with minimal changes. Still depends on bevy_xpbd
0.5, which is the last version of bevy_xpbd published (only published to
ease migration to Bevy 0.14).

Follow-up update which replaces bevy_xpbd with Avian is WIP and will be
released tomorrow.
  • Loading branch information
mbrea-c authored Jul 8, 2024
1 parent 090f2f3 commit 60df157
Show file tree
Hide file tree
Showing 10 changed files with 734 additions and 447 deletions.
969 changes: 619 additions & 350 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ keywords = ["bevy", "gamedev", "particles", "graphics"]
resolver = "2"

[dependencies]
bevy = "0.13"
bevy = "0.14"
bytemuck = "1.14.3"
rand = "0.8.5"
bevy_utilitarian = "0.4"
bevy_xpbd_3d = { version = "0.4", features = ["serialize"], optional = true }
serde = { version = "1.0.197", features = ["derive"] }
bevy_utilitarian = { version = "0.5" }
bevy_xpbd_3d = { version = "0.5", features = ["serialize"], optional = true }
serde = { version = "1.0", features = ["derive"] }

[features]
default = ["physics_xpbd"]
Expand Down
12 changes: 6 additions & 6 deletions examples/collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ fn setup(
scale_curve: ParamCurve::constant(1.),
linear_drag: 0.15,
color: Gradient::linear(vec![
(0., Color::rgba(10., 7., 1., 1.).into()),
(0.7, Color::rgba(3., 1., 1., 1.).into()),
(0.8, Color::rgba(1., 0.3, 0.3, 1.).into()),
(0.9, Color::rgba(0.3, 0.3, 0.3, 1.).into()),
(1., Color::rgba(0.1, 0.1, 0.1, 0.).into()),
(0., LinearRgba::new(10., 7., 1., 1.)),
(0.7, LinearRgba::new(3., 1., 1., 1.)),
(0.8, LinearRgba::new(1., 0.3, 0.3, 1.)),
(0.9, LinearRgba::new(0.3, 0.3, 0.3, 1.)),
(1., LinearRgba::new(0.1, 0.1, 0.1, 0.)),
]),
blend_mode: BlendMode::Blend,
pbr: true,
Expand All @@ -105,7 +105,7 @@ fn setup(
commands
.spawn(PbrBundle {
mesh: meshes.add(Cuboid::from_size(Vec3::ONE)),
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
material: materials.add(Color::from(LinearRgba::new(0.8, 0.7, 0.6, 1.))),
transform: Transform {
translation: Vec3::new(0., 0.5, 0.),
rotation: Quat::from_rotation_x(PI / 4.) * Quat::from_rotation_y(PI / 4.),
Expand Down
20 changes: 15 additions & 5 deletions examples/pbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
// to the MSAA setting.
app.add_plugins(ParticleSystemPlugin)
.add_systems(Startup, setup)
.add_systems(Update, adjust_time_scale);
.add_systems(Update, (adjust_time_scale, rotate_point_light));
#[cfg(feature = "physics_xpbd")]
app.add_plugins(bevy_xpbd_3d::prelude::PhysicsPlugins::default());

Expand Down Expand Up @@ -74,9 +74,9 @@ fn setup(
initial_scale: RandF32 { min: 0.5, max: 1.3 },
scale_curve: ParamCurve::linear_uniform(vec![1., 2.]),
color: Gradient::linear(vec![
(0., Color::rgba(0.6, 0.3, 0., 0.).into()),
(0.1, Color::rgba(0.6, 0.3, 0., 0.5).into()),
(1., Color::rgba(0.6, 0.3, 0., 0.0).into()),
(0., LinearRgba::new(0.6, 0.3, 0., 0.)),
(0.1, LinearRgba::new(0.6, 0.3, 0., 0.5)),
(1., LinearRgba::new(0.6, 0.3, 0., 0.0)),
]),
blend_mode: BlendMode::Blend,
linear_drag: 0.7,
Expand All @@ -90,7 +90,7 @@ fn setup(
// cube
commands.spawn(PbrBundle {
mesh: meshes.add(Cuboid::from_size(Vec3::ONE)),
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
material: materials.add(Color::LinearRgba(LinearRgba::rgb(0.8, 0.7, 0.6))),
transform: Transform::from_xyz(1.0, 1.5, 0.0),
..default()
});
Expand Down Expand Up @@ -121,6 +121,16 @@ fn setup(
));
}

fn rotate_point_light(mut point_lights: Query<&mut Transform, With<PointLight>>, time: Res<Time>) {
for mut transform in &mut point_lights {
transform.translation = Vec3::new(
4. * time.elapsed_seconds().sin(),
8. * ((time.elapsed_seconds() * 0.78932).sin() + 1.) / 2.,
4. * time.elapsed_seconds().cos(),
);
}
}

fn adjust_time_scale(
mut slowmo: Local<bool>,
mut time: ResMut<Time<Virtual>>,
Expand Down
11 changes: 5 additions & 6 deletions examples/sparks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ fn setup(
},
scale_curve: ParamCurve::constant(1.),
color: Gradient::linear(vec![
(0., Color::rgba(10., 7., 1., 1.).into()),
(0.7, Color::rgba(3., 1., 1., 1.).into()),
(0.8, Color::rgba(1., 0.3, 0.3, 1.).into()),
(0.9, Color::rgba(0.3, 0.3, 0.3, 1.).into()),
(1., Color::rgba(0.1, 0.1, 0.1, 0.).into()),
(0., LinearRgba::new(150., 100., 15., 1.)),
(0.7, LinearRgba::new(3., 1., 1., 1.)),
(0.8, LinearRgba::new(1., 0.3, 0.3, 1.)),
(0.9, LinearRgba::new(0.3, 0.3, 0.3, 1.)),
(1., LinearRgba::new(0.1, 0.1, 0.1, 0.)),
]),
blend_mode: BlendMode::Blend,
linear_drag: 0.1,
Expand All @@ -110,7 +110,6 @@ fn setup(
transform: Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
camera: Camera {
hdr: true,

..default()
},
..default()
Expand Down
10 changes: 5 additions & 5 deletions examples/stress_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ fn setup(
},
scale_curve: ParamCurve::constant(1.),
color: Gradient::linear(vec![
(0., Color::rgba(10., 7., 1., 1.).into()),
(0.7, Color::rgba(3., 1., 1., 1.).into()),
(0.8, Color::rgba(1., 0.3, 0.3, 1.).into()),
(0.9, Color::rgba(0.3, 0.3, 0.3, 1.).into()),
(1., Color::rgba(0.1, 0.1, 0.1, 0.).into()),
(0., LinearRgba::new(10., 7., 1., 1.)),
(0.7, LinearRgba::new(3., 1., 1., 1.)),
(0.8, LinearRgba::new(1., 0.3, 0.3, 1.)),
(0.9, LinearRgba::new(0.3, 0.3, 0.3, 1.)),
(1., LinearRgba::new(0.1, 0.1, 0.1, 0.)),
]),
blend_mode: BlendMode::Blend,
linear_drag: 0.1,
Expand Down
12 changes: 6 additions & 6 deletions examples/stress_test_collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ fn setup(
scale_curve: ParamCurve::constant(1.),
linear_drag: 0.15,
color: Gradient::linear(vec![
(0., Color::rgba(10., 7., 1., 1.).into()),
(0.7, Color::rgba(3., 1., 1., 1.).into()),
(0.8, Color::rgba(1., 0.3, 0.3, 1.).into()),
(0.9, Color::rgba(0.3, 0.3, 0.3, 1.).into()),
(1., Color::rgba(0.1, 0.1, 0.1, 0.).into()),
(0., LinearRgba::new(100., 70., 10., 1.)),
(0.7, LinearRgba::new(3., 1., 1., 1.)),
(0.8, LinearRgba::new(1., 0.3, 0.3, 1.)),
(0.9, LinearRgba::new(0.3, 0.3, 0.3, 1.)),
(1., LinearRgba::new(0.1, 0.1, 0.1, 0.)),
]),
blend_mode: BlendMode::Blend,
pbr: true,
Expand All @@ -167,7 +167,7 @@ fn setup(
commands
.spawn(PbrBundle {
mesh: meshes.add(Cuboid::from_size(Vec3::ONE)),
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
material: materials.add(Color::from(LinearRgba::new(0.8, 0.7, 0.6, 1.))),
transform: Transform {
translation: Vec3::new(0., 0.5, 0.),
rotation: Quat::from_rotation_x(PI / 4.) * Quat::from_rotation_y(PI / 4.),
Expand Down
16 changes: 8 additions & 8 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Default for ParticleSpawnerSettings {
initial_scale: RandF32::constant(1.),
scale_curve: ParamCurve::linear_uniform(vec![1., 1.]),
acceleration: Vec3::new(0., -9.81, 0.),
color: Gradient::constant(Color::WHITE.into()),
color: Gradient::constant(LinearRgba::WHITE),
blend_mode: BlendMode::Blend,
linear_drag: 0.,
pbr: false,
Expand Down Expand Up @@ -193,7 +193,7 @@ pub struct ParticleData {
pub scale: f32,
pub age: f32,
pub lifetime: f32,
pub color: Color,
pub color: LinearRgba,
pub pbr: bool,
}

Expand Down Expand Up @@ -283,7 +283,7 @@ pub fn spawn_particles(
scale: initial_scale,
velocity,
age: 0.,
color: *settings.color.get(0.),
color: settings.color.get(0.),
pbr: settings.pbr,
})
}
Expand Down Expand Up @@ -342,7 +342,7 @@ pub fn update_particles(
particle.velocity += (settings.acceleration
- particle.velocity * settings.linear_drag)
* time.delta_seconds();
particle.color = *settings.color.get(age_percent);
particle.color = settings.color.get(age_percent);

Some(particle)
})
Expand All @@ -367,8 +367,8 @@ pub fn propagate_particle_spawner_modifier(

pub fn setup_default_mesh(mut meshes: ResMut<Assets<Mesh>>) {
meshes.insert(
DEFAULT_MESH.clone(),
Rectangle::from_size(Vec2::new(1., 1.)).mesh(),
&mut DEFAULT_MESH.clone(),
Rectangle::from_size(Vec2::new(1., 1.)).mesh().into(),
);
}

Expand Down Expand Up @@ -424,9 +424,9 @@ fn particle_collision(
while delta > 0. && n_steps < 4 {
if let Some(hit) = spatial_query.cast_ray(
pos,
match Direction3d::try_from(vel) {
match Dir3::try_from(vel) {
Ok(dir) => dir,
Err(_) => Direction3d::Y,
Err(_) => Dir3::Y,
},
vel.length() * delta,
true,
Expand Down
37 changes: 11 additions & 26 deletions src/particles.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
MESH_FLAGS_TRANSMITTED_SHADOW_RECEIVER_BIT
};
#import bevy_pbr::pbr_functions as fns
#import bevy_pbr::pbr_bindings,

struct Vertex {
@location(3) i_pos_scale: vec4<f32>,
Expand Down Expand Up @@ -50,6 +51,7 @@ struct VertexOutput {
@location(1) world_normal: vec3<f32>,
@location(2) color: vec4<f32>,
@location(3) uv: vec2<f32>,
@location(4) world_tangent: vec3<f32>,
};

fn extract_rot(bigmat: mat4x4<f32>) -> mat3x3<f32> {
Expand Down Expand Up @@ -82,6 +84,7 @@ fn vertex(vertex: Vertex) -> VertexOutput {
out.position = position_world_to_clip(position_world);
out.color = vertex.i_color;
out.world_normal = direction_view_to_world(vec3(0., 0., 1.));
out.world_tangent = direction_view_to_world(vec3(1., 0., 0.));
out.uv = uvs[vertex.index];

return out;
Expand Down Expand Up @@ -114,7 +117,7 @@ fn fragment(in: VertexOutput, @builtin(front_facing) is_front: bool) -> @locatio
if firework_uniform.pbr == 0u {
return color;
} else {
return pbr_stuff(color, in.position, in.world_position, in.world_normal, in.uv, is_front);
return pbr_stuff(color, in.position, in.world_position, in.world_normal, in.uv, in.world_tangent, is_front);
}
}

Expand All @@ -124,19 +127,15 @@ fn pbr_stuff(
world_position: vec4<f32>,
world_normal: vec3<f32>,
uv: vec2<f32>,
world_tangent: vec3<f32>,
is_front: bool,
) -> vec4<f32> {
var pbr_input = pbr_input_new();
//if alpha_mode == 0u {
pbr_input.material.flags =
(STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND
| STANDARD_MATERIAL_FLAGS_ATTENUATION_ENABLED_BIT
| STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT);
//& ~STANDARD_MATERIAL_FLAGS_UNLIT_BIT;
// } else if alpha_mode == 1u {
// pbr_input.material.flags = STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MULTIPLY;
// }
pbr_input.is_orthographic = view.projection[3].w == 1.0;
pbr_input.is_orthographic = view.clip_from_view[3].w == 1.0;
pbr_input.material.base_color = base_color;
pbr_input.material.metallic = 0.0;
pbr_input.material.perceptual_roughness = 1.0;
Expand All @@ -146,29 +145,15 @@ fn pbr_stuff(

let double_sided = (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u;

pbr_input.world_normal = fns::prepare_world_normal(
pbr_input.world_normal = normalize(fns::prepare_world_normal(
world_normal,
double_sided,
is_front,
);
));

pbr_input.N = fns::apply_normal_mapping(
pbr_input.material.flags,
world_normal,
double_sided,
is_front,
#ifdef VERTEX_TANGENTS
#ifdef STANDARDMATERIAL_NORMAL_MAP
// TODO: Sort out the tangents stuff
vec3(0.,0.,1.),
#endif
#endif
#ifdef VERTEX_UVS
uv,
#endif
view.mip_bias,
);
pbr_input.V = fns::calculate_view(world_position, pbr_input.is_orthographic);
pbr_input.N = pbr_input.world_normal;

pbr_input.V = fns::calculate_view(pbr_input.world_position, pbr_input.is_orthographic);

var out: vec4<f32>;

Expand Down
Loading

0 comments on commit 60df157

Please sign in to comment.