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

Port to Bevy 0.12 #9

Merged
merged 2 commits into from
Nov 6, 2023
Merged
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: 0 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ jobs:

- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable

- name: Cache
uses: Swatinem/rust-cache@v2
Expand All @@ -34,8 +32,6 @@ jobs:

- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable

- name: Cache
uses: Swatinem/rust-cache@v2
Expand Down
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ keywords = ["gamedev", "bevy", "noise", "pcg", "simplex_noise"]
license = "MIT"
name = "noisy_bevy"
repository = "https://github.com/johanhelsing/noisy_bevy"
version = "0.4.0"
version = "0.5.0"

[dependencies]
bevy = { version = "0.11", features = ["bevy_asset", "bevy_render"], default-features = false }
bevy = { version = "0.12", default-features = false, features = [
"bevy_asset",
"bevy_render"
] }

[dev-dependencies]
bevy = { version = "0.11", default-features = false, features = [
"bevy_render",
bevy = { version = "0.12", default-features = false, features = [
"bevy_sprite",
"bevy_asset",
"bevy_winit",
"filesystem_watcher",
"x11", # github actions runners don't have libxkbcommon installed, so can't use wayland
] }
rand = "0.8"
bevy_egui = {version = "0.21.0", default-features = false, features = ["default_fonts"]}
bevy_egui = { version = "0.23", default-features = false, features = ["default_fonts"] }
# bevy-inspector-egui = {version = "0.19.1", default-features = false}
bevy_pancam = {version = "0.9", features = ["bevy_egui"] }
bevy_pancam = { version = "0.10", features = ["bevy_egui"] }
insta = "1.21"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ The `main` branch targets the latest bevy release.

|bevy|noisy_bevy|
|----|----------|
|0.11| 0.4, main|
|0.12| 0.5, main|
|0.11| 0.4 |
|0.10| 0.3 |
|0.9 | 0.2 |
|0.8 | 0.1 |
Expand Down
37 changes: 29 additions & 8 deletions assets/examples/asteroid_background.wgsl
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
#import noisy_bevy fbm_simplex_2d_seeded
#import bevy_sprite::mesh2d_bindings mesh
#import bevy_sprite::mesh2d_vertex_output MeshVertexOutput
#import noisy_bevy::fbm_simplex_2d_seeded
#import bevy_sprite::mesh2d_functions::{
get_model_matrix,
mesh2d_position_local_to_clip,
}

struct AsteroidMaterial {
params: vec4<f32>
}

@group(1) @binding(0)
var<uniform> material: AsteroidMaterial;
@group(1) @binding(0) var<uniform> material: AsteroidMaterial;

struct Vertex {
@builtin(instance_index) instance_index: u32,
@location(0) position: vec3<f32>,
@location(1) blend_color: vec4<f32>,
};

struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) object_position: vec2<f32>,
};

@vertex
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
// Project the world position of the mesh into screen position
let model = get_model_matrix(vertex.instance_index);
out.clip_position = mesh2d_position_local_to_clip(model, vec4<f32>(vertex.position, 1.0));
out.object_position = vertex.position.xy;
return out;
}

@fragment
fn fragment(
vertex_output: MeshVertexOutput,
vertex_output: VertexOutput,
) -> @location(0) vec4<f32> {
// perf: better to do in vertex shader!
var p = vertex_output.world_position.xy - vec2(mesh.model[3].x, mesh.model[3].y); // ignoring rotation
var p = vertex_output.object_position;
let params = material.params;
let freq_scale = params.x;
let amp_scale = params.y;
Expand Down
21 changes: 9 additions & 12 deletions examples/asteroids.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::time::Duration;

use bevy::{
asset::ChangeWatcher,
math::{vec2, vec4},
prelude::*,
reflect::{TypePath, TypeUuid},
render::{camera::ScalingMode, render_resource::AsBindGroup},
sprite::{Material2d, Material2dPlugin, MaterialMesh2dBundle},
};
Expand All @@ -17,10 +13,7 @@ fn main() {
.register_type::<AsteroidParams>()
.insert_resource(ClearColor(Color::BLACK))
.add_plugins((
DefaultPlugins.set(AssetPlugin {
watch_for_changes: ChangeWatcher::with_delay(Duration::from_millis(200)),
..default()
}),
DefaultPlugins,
NoisyShaderPlugin,
PanCamPlugin::default(),
Material2dPlugin::<AsteroidBackgroundMaterial>::default(),
Expand All @@ -40,14 +33,16 @@ fn setup(mut commands: Commands) {
commands.spawn(AsteroidBundle::default());
}

#[derive(AsBindGroup, TypeUuid, Clone, TypePath)]
#[uuid = "1e449d2e-6901-4bff-95fa-d7407ad62b58"]
#[derive(Asset, AsBindGroup, Reflect, Debug, Clone)]
struct AsteroidBackgroundMaterial {
#[uniform(0)]
params: Vec4,
}

impl Material2d for AsteroidBackgroundMaterial {
fn vertex_shader() -> bevy::render::render_resource::ShaderRef {
"examples/asteroid_background.wgsl".into()
}
fn fragment_shader() -> bevy::render::render_resource::ShaderRef {
"examples/asteroid_background.wgsl".into()
}
Expand Down Expand Up @@ -79,7 +74,8 @@ struct AsteroidBundle {
transform: Transform,
global_transform: GlobalTransform,
visibility: Visibility,
computed_visibility: ComputedVisibility,
view_visibility: ViewVisibility,
inherited_visibility: InheritedVisibility,
params: AsteroidParams,
}

Expand All @@ -90,8 +86,9 @@ impl Default for AsteroidBundle {
transform: default(),
global_transform: default(),
visibility: default(),
computed_visibility: default(),
params: default(),
view_visibility: default(),
inherited_visibility: default(),
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![doc = include_str!("../README.md")]

use bevy::{
asset::HandleId,
asset::load_internal_asset,
math::{vec2, vec3, vec4, Vec2Swizzles, Vec3Swizzles, Vec4Swizzles},
prelude::*,
};
Expand All @@ -18,18 +18,18 @@ pub struct NoisyShaderPlugin;

impl Plugin for NoisyShaderPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, load_shaders);
// workaround: embedded_asset is broken in bevy 0.12.0
load_internal_asset!(
app,
NOISY_SHADER_HANDLE,
"../assets/noisy_bevy.wgsl",
Shader::from_wgsl
);
}
}

fn load_shaders(mut shaders: ResMut<Assets<Shader>>) {
let shader = Shader::from_wgsl(
include_str!("../assets/noisy_bevy.wgsl",),
"noisy_bevy.wgsl",
);
let handle_id = HandleId::random::<Shader>();
shaders.set_untracked(handle_id, shader);
}
const NOISY_SHADER_HANDLE: Handle<Shader> =
Handle::weak_from_u128(224136012015454690045205738992444526155);

fn permute_3(x: Vec3) -> Vec3 {
(((x * 34.) + 1.) * x) % Vec3::splat(289.)
Expand Down
Loading