Skip to content

Commit

Permalink
Make work with bevy 0.9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkponk committed Feb 15, 2023
1 parent 9181f0b commit 55780be
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 45 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = "0.8.0"
bevy = "0.9.1"
bytemuck = "1.11.0"
bevy_pbr = "0.8.0"
bevy_pbr = "0.9.1"
itertools = "*"
rand = "*"
# bevy_shader_utils = { path = "../bevy-examples/libs/bevy_shader_utils" }
wasm-bindgen = { version = "= 0.2.81" }
noise = "0.7" #Procedural Noise Generation library for Rust
iyes_loopless = "0.7.1"
iyes_progress = "0.5.0"
wasm-bindgen = { version = "0.2.84" }
noise = "0.8.2" #Procedural Noise Generation library for Rust
iyes_loopless = "0.9.1"
iyes_progress = "0.7.1"

bevy_asset_loader = { version = "0.12.0", features = [
bevy_asset_loader = { version = "0.14.1", features = [
"stageless",
] } #Needed to turn on a bunch of features in order to get assetloader to work with iyes_loopless+progress

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ I could render 10 million grass straws at 70fps on my gaming pc using this appro
I'm making a game for the web in which you have to code the behavior of forest animals in order to balance an ecosystem. Follow my devlogs if you are interested: https://www.youtube.com/channel/UCqzbiRaNXJa50J4kvJvKpAg

Compatible Bevy versions:
- Bevy 0.8
- Bevy 0.9 (main)
- Bevy 0.8 (branch bevy-0.8.1 or tag v0.1.0)

Forest:
Total instanced objects 192_000 (Not all visable at once, culling)
Expand Down
12 changes: 7 additions & 5 deletions assets/shaders/chunk_instancing.wgsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#import bevy_pbr::mesh_types
#import bevy_pbr::mesh_view_bindings

Expand All @@ -7,7 +8,8 @@ var<uniform> mesh: Mesh;
// NOTE: Bindings must come before functions that use them!
#import bevy_pbr::mesh_functions

#import bevy_pbr::pbr_types
#import bevy_pbr::pbr_bindings

#import bevy_pbr::utils
#import bevy_pbr::clustered_forward
#import bevy_pbr::lighting
Expand Down Expand Up @@ -89,6 +91,7 @@ var diffuse_sampler: sampler;




struct FragmentInput {
@builtin(front_facing) is_front: bool,
@builtin(position) frag_coord: vec4<f32>,
Expand All @@ -113,13 +116,12 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {

pbr_input.is_orthographic = view.projection[3].w == 1.0;

pbr_input.N = prepare_normal(
pbr_input.N = apply_normal_mapping(
pbr_input.material.flags,
in.world_normal,
pbr_input.world_normal,
in.uv,
in.is_front,
);
pbr_input.V = calculate_view(in.world_position, pbr_input.is_orthographic);

return tone_mapping(pbr(pbr_input));
return pbr(pbr_input);
}
50 changes: 27 additions & 23 deletions examples/forest.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bevy::{
asset::AssetServerSettings,
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
gltf::{Gltf, GltfMesh},
math::prelude::*,
Expand Down Expand Up @@ -39,7 +38,7 @@ pub enum GameState {
InGame,
}

#[derive(AssetCollection)]
#[derive(AssetCollection, Resource)]
pub struct MyGltfAssets {
#[asset(path = "mushroom.glb")]
mushroom: Handle<Gltf>,
Expand All @@ -51,7 +50,7 @@ pub struct MyGltfAssets {
rock: Handle<Gltf>,
}

#[derive(AssetCollection)]
#[derive(AssetCollection, Resource)]
pub struct MyImageAssets {
#[asset(path = "grass_ground_texture.png")]
grass: Handle<Image>,
Expand All @@ -74,19 +73,24 @@ fn main() {
.with_collection::<MyGltfAssets>()
.with_collection::<MyImageAssets>(),
)
.insert_resource(WindowDescriptor {
position: WindowPosition::At(vec2(1450., 550.0)),
width: 1000.0,
height: 1000.0,
present_mode: bevy::window::PresentMode::AutoNoVsync, //Dont cap at 60 fps
..default()
})
.add_plugins(
DefaultPlugins
.set(AssetPlugin {
watch_for_changes: true,
..default()
})
.set(WindowPlugin {
window: WindowDescriptor {
position: WindowPosition::At(vec2(1450., 550.0)),
width: 1000.0,
height: 1000.0,
present_mode: bevy::window::PresentMode::AutoNoVsync, //Dont cap at 60 fps
..default()
},
..default()
}),
)
.insert_resource(ClearColor(Color::rgb(0.7, 0.8, 0.8)))
.insert_resource(AssetServerSettings {
watch_for_changes: true,
..default()
})
.add_plugins(DefaultPlugins)
.add_plugin(OrbitCameraPlugin)
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(FrameTimeDiagnosticsPlugin::default())
Expand Down Expand Up @@ -125,7 +129,7 @@ fn setup(
// color: Color::WHITE,
// brightness: 3.05,
// });
commands.spawn_bundle(DirectionalLightBundle {
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: 30000.0,
shadows_enabled: false, //Weird things happen
Expand Down Expand Up @@ -162,7 +166,7 @@ fn setup(

// Ground
commands
.spawn_bundle(PbrBundle {
.spawn(PbrBundle {
transform: Transform {
translation: (Vec3::new(0., 0., 0.)),
rotation: Quat::from_rotation_x(0.0_f32.to_radians()),
Expand Down Expand Up @@ -237,7 +241,7 @@ fn setup(
chunk_xy: [chunk_x, chunk_y],
};

commands.spawn_bundle(ChunkInstancingBundle {
commands.spawn(ChunkInstancingBundle {
transform: Transform::from_xyz(chunk_x_pos, chunk_y_pos, 0.0),
mesh_handle: mushroom_mesh_handle.clone(),
aabb: Aabb {
Expand All @@ -257,7 +261,7 @@ fn setup(
});
tot_instances += nr_instances / 5;

commands.spawn_bundle(ChunkInstancingBundle {
commands.spawn(ChunkInstancingBundle {
transform: Transform::from_xyz(chunk_x_pos, chunk_y_pos, 0.0),
mesh_handle: tree_mesh_handle.clone(),
aabb: Aabb {
Expand All @@ -277,7 +281,7 @@ fn setup(
});
tot_instances += nr_instances / 15;

commands.spawn_bundle(ChunkInstancingBundle {
commands.spawn(ChunkInstancingBundle {
transform: Transform::from_xyz(chunk_x_pos, chunk_y_pos, 0.0),
mesh_handle: bush_mesh_handle.clone(),
aabb: Aabb {
Expand All @@ -297,7 +301,7 @@ fn setup(
});
tot_instances += nr_instances / 6;

commands.spawn_bundle(ChunkInstancingBundle {
commands.spawn(ChunkInstancingBundle {
transform: Transform::from_xyz(chunk_x_pos, chunk_y_pos, 0.0),
mesh_handle: rock_mesh_handle.clone(),
aabb: Aabb {
Expand All @@ -317,7 +321,7 @@ fn setup(
});
tot_instances += nr_instances / 10;

commands.spawn_bundle(ChunkGrassBundle {
commands.spawn(ChunkGrassBundle {
transform: Transform::from_xyz(chunk_x_pos, chunk_y_pos, 0.0),
mesh_handle: meshes.add(get_grass_straw_mesh()),
aabb: Aabb {
Expand Down Expand Up @@ -353,7 +357,7 @@ fn setup(

// Camera
commands
.spawn_bundle(Camera3dBundle {
.spawn(Camera3dBundle {
..Default::default()
})
.insert(OrbitCamera {
Expand Down
17 changes: 9 additions & 8 deletions src/rendering/chunk_grass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use bevy::{
};
use bytemuck::{Pod, Zeroable};

use noise::{NoiseFn, Perlin, Seedable};
use noise::{NoiseFn, Perlin};

use super::{Chunk, DistanceCulling};

Expand All @@ -53,7 +53,7 @@ pub struct ChunkGrassBundle {

fn update_time_for_custom_material(mut grass_chunks: Query<&mut ChunkGrass>, time: Res<Time>) {
for mut grass_chunk in grass_chunks.iter_mut() {
grass_chunk.time = time.seconds_since_startup() as f32;
grass_chunk.time = time.elapsed_seconds();
}
}

Expand Down Expand Up @@ -130,7 +130,7 @@ impl Plugin for ChunkGrassPlugin {
}
}

#[derive(Clone, Component, Default)]
#[derive(Clone, Component, Default, Resource)]
pub struct GrowthTextures {
pub growth_texture_array_handle: Option<Handle<Image>>,
}
Expand All @@ -143,7 +143,7 @@ impl GrowthTextures {
let pattern_scale = 0.1;
let nr_textures = 2;
for i in 0..nr_textures {
let perlin = Perlin::new().set_seed(i + 1); // from -1 to 1
let perlin = Perlin::new(i + 1); // from -1 to 1

for y in 0..size {
for x in 0..size {
Expand Down Expand Up @@ -173,7 +173,7 @@ impl GrowthTextures {
}
}

#[derive(Clone, Default)]
#[derive(Clone, Default, Resource)]
pub struct GridConfig {
pub grid_center_xy: [f32; 2], //Assume axis aligned grid otherwise need to calc homogenous coordinate matrix
pub grid_half_extents: [f32; 2],
Expand Down Expand Up @@ -335,7 +335,7 @@ fn prepare_grass_chunk_bind_group(
}
}

#[derive(Default)]
#[derive(Default, Resource)]
pub struct GrowthTexturesBindGroup {
pub bind_group: Option<BindGroup>,
}
Expand Down Expand Up @@ -371,7 +371,7 @@ pub fn prepare_growth_textures_bind_group(
}
}

#[derive(Default)]
#[derive(Default, Resource)]
pub struct GridConfigBindGroup {
pub grid_config_bind_group: Option<BindGroup>,
}
Expand Down Expand Up @@ -488,6 +488,7 @@ fn queue_custom_pipeline(
// █░░░░░░█████████░░░░░░░░░░█░░░░░░█████████░░░░░░░░░░░░░░█░░░░░░░░░░░░░░█░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░░░█
// █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████

#[derive(Resource)]
pub struct CustomPipeline {
shader: Handle<Shader>,
mesh_pipeline: MeshPipeline,
Expand Down Expand Up @@ -563,7 +564,7 @@ impl FromWorld for CustomPipeline {
//grid END

let asset_server = world.resource::<AssetServer>();
asset_server.watch_for_changes().unwrap();
// asset_server.watch_for_changes().unwrap();
let shader = asset_server.load("shaders/grass.wgsl");

let mesh_pipeline = world.resource::<MeshPipeline>();
Expand Down
3 changes: 2 additions & 1 deletion src/rendering/chunk_instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ fn queue_custom(
// █░░░░░░█████████░░░░░░░░░░█░░░░░░█████████░░░░░░░░░░░░░░█░░░░░░░░░░░░░░█░░░░░░░░░░█░░░░░░██████████░░░░░░█░░░░░░░░░░░░░░█
// █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████

#[derive(Resource)]
pub struct CustomPipeline {
shader: Handle<Shader>,
mesh_pipeline: MeshPipeline,
Expand Down Expand Up @@ -413,7 +414,7 @@ impl FromWorld for CustomPipeline {
});

let asset_server = world.resource::<AssetServer>();
asset_server.watch_for_changes().unwrap();
// asset_server.watch_for_changes().unwrap();
let shader = asset_server.load("shaders/chunk_instancing.wgsl");

let mesh_pipeline = world.resource::<MeshPipeline>();
Expand Down

0 comments on commit 55780be

Please sign in to comment.