Skip to content

Commit

Permalink
Merge pull request #7 from johanhelsing/bevy-0.11
Browse files Browse the repository at this point in the history
Port to Bevy 0.11
  • Loading branch information
johanhelsing authored Jul 13, 2023
2 parents 488ebdb + 86c1568 commit 8dff288
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 46 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ keywords = ["gamedev", "bevy", "noise", "pcg", "simplex_noise"]
license = "MIT"
name = "noisy_bevy"
repository = "https://github.com/johanhelsing/noisy_bevy"
version = "0.3.0"
version = "0.4.0"

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

[dev-dependencies]
bevy = { version = "0.10", default-features = false, features = [
bevy = { version = "0.11", default-features = false, features = [
"bevy_render",
"bevy_sprite",
"bevy_asset",
Expand All @@ -22,7 +22,7 @@ bevy = { version = "0.10", default-features = false, features = [
"x11", # github actions runners don't have libxkbcommon installed, so can't use wayland
] }
rand = "0.8"
bevy_egui = {version = "0.20.1", default-features = false, features = ["default_fonts"]}
bevy-inspector-egui = {version = "0.18.1", default-features = false}
bevy_pancam = {version = "0.8", features = ["bevy_egui"] }
bevy_egui = {version = "0.21.0", default-features = false, features = ["default_fonts"]}
# bevy-inspector-egui = {version = "0.19.1", default-features = false}
bevy_pancam = {version = "0.9", features = ["bevy_egui"] }
insta = "1.21"
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ App::new()
And import it and use it in your shaders, with the same API as on the CPU-side:

```wgsl
#import noisy_bevy::prelude
#import noisy_bevy simplex_noise_2d
// ...
Expand All @@ -64,7 +64,8 @@ The `main` branch targets the latest bevy release.

|bevy|noisy_bevy|
|----|----------|
|0.10| 0.3, main|
|0.11| 0.4, main|
|0.10| 0.3 |
|0.9 | 0.2 |
|0.8 | 0.1 |

Expand Down
14 changes: 5 additions & 9 deletions assets/examples/asteroid_background.wgsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#import bevy_render::view
#import noisy_bevy::prelude
// #import bevy_pbr::mesh_functions
#import bevy_sprite::mesh2d_bindings
#import noisy_bevy fbm_simplex_2d_seeded
#import bevy_sprite::mesh2d_bindings mesh
#import bevy_sprite::mesh2d_vertex_output MeshVertexOutput

struct AsteroidMaterial {
params: vec4<f32>
Expand All @@ -12,13 +11,10 @@ var<uniform> material: AsteroidMaterial;

@fragment
fn fragment(
@builtin(position) position: vec4<f32>,
#import bevy_sprite::mesh2d_vertex_output
vertex_output: MeshVertexOutput,
) -> @location(0) vec4<f32> {
// perf: better to do in vertex shader!
var p = world_position.xy - vec2(mesh.model[3].x, mesh.model[3].y); // ignoring rotation
// sample noise
p = p.xy;
var p = vertex_output.world_position.xy - vec2(mesh.model[3].x, mesh.model[3].y); // ignoring rotation
let params = material.params;
let freq_scale = params.x;
let amp_scale = params.y;
Expand Down
18 changes: 9 additions & 9 deletions assets/simple_noise_prelude.wgsl → assets/noisy_bevy.wgsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// MIT License. © Ian McEwan, Stefan Gustavson, Munrocket, Johan Helsing

#define_import_path noisy_bevy::prelude
#define_import_path noisy_bevy

fn permute_3(x: vec3<f32>) -> vec3<f32> {
fn permute_3_(x: vec3<f32>) -> vec3<f32> {
return (((x * 34.) + 1.) * x) % vec3(289.);
}

Expand All @@ -25,7 +25,7 @@ fn simplex_noise_2d(v: vec2<f32>) -> f32 {
// permutations
i = i % vec2(289.);

let p = permute_3(permute_3(i.y + vec3(0., i1.y, 1.)) + i.x + vec3(0., i1.x, 1.));
let p = permute_3_(permute_3_(i.y + vec3(0., i1.y, 1.)) + i.x + vec3(0., i1.x, 1.));
var m = max(0.5 - vec3(dot(x0, x0), dot(x12.xy, x12.xy), dot(x12.zw, x12.zw)), vec3(0.));
m *= m;
m *= m;
Expand Down Expand Up @@ -65,8 +65,8 @@ fn simplex_noise_2d_seeded(v: vec2<f32>, seed: f32) -> f32 {
// permutations
i = i % vec2(289.);

var p = permute_3(permute_3(i.y + vec3(0., i1.y, 1.)) + i.x + vec3(0., i1.x, 1.));
p = permute_3(p + vec3(seed));
var p = permute_3_(permute_3_(i.y + vec3(0., i1.y, 1.)) + i.x + vec3(0., i1.x, 1.));
p = permute_3_(p + vec3(seed));
var m = max(0.5 - vec3(dot(x0, x0), dot(x12.xy, x12.xy), dot(x12.zw, x12.zw)), vec3(0.));
m *= m;
m *= m;
Expand All @@ -87,11 +87,11 @@ fn simplex_noise_2d_seeded(v: vec2<f32>, seed: f32) -> f32 {
return 130. * dot(m, g);
}

fn permute_4(x: vec4<f32>) -> vec4<f32> {
fn permute_4_(x: vec4<f32>) -> vec4<f32> {
return ((x * 34. + 1.) * x) % vec4<f32>(289.);
}

fn taylor_inv_sqrt_4(r: vec4<f32>) -> vec4<f32> {
fn taylor_inv_sqrt_4_(r: vec4<f32>) -> vec4<f32> {
return 1.79284291400159 - 0.85373472095314 * r;
}

Expand All @@ -116,7 +116,7 @@ fn simplex_noise_3d(v: vec3<f32>) -> f32 {

// permutations
i = i % vec3(289.);
let p = permute_4(permute_4(permute_4(
let p = permute_4_(permute_4_(permute_4_(
i.z + vec4(0., i1.z, i2.z, 1.)) +
i.y + vec4(0., i1.y, i2.y, 1.)) +
i.x + vec4(0., i1.x, i2.x, 1.)
Expand Down Expand Up @@ -151,7 +151,7 @@ fn simplex_noise_3d(v: vec3<f32>) -> f32 {
var p3 = vec3(a1.zw, h.w);

// normalize gradients
let norm = taylor_inv_sqrt_4(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
let norm = taylor_inv_sqrt_4_(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
p0 = p0 * norm.x;
p1 = p1 * norm.y;
p2 = p2 * norm.z;
Expand Down
31 changes: 18 additions & 13 deletions examples/asteroids.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
use std::time::Duration;

use bevy::{
asset::ChangeWatcher,
math::{vec2, vec4},
prelude::*,
reflect::TypeUuid,
reflect::{TypePath, TypeUuid},
render::{camera::ScalingMode, render_resource::AsBindGroup},
sprite::{Material2d, Material2dPlugin, MaterialMesh2dBundle},
};
use bevy_inspector_egui::quick::WorldInspectorPlugin;
// use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_pancam::{PanCam, PanCamPlugin};
use noisy_bevy::{simplex_noise_2d_seeded, NoisyShaderPlugin};

fn main() {
App::new()
.register_type::<AsteroidParams>()
.insert_resource(ClearColor(Color::BLACK))
.add_plugins(DefaultPlugins.set(AssetPlugin {
watch_for_changes: true,
..default()
}))
.add_plugin(NoisyShaderPlugin)
.add_plugin(PanCamPlugin::default())
.add_plugin(Material2dPlugin::<AsteroidBackgroundMaterial>::default())
.add_plugin(WorldInspectorPlugin::new())
.add_startup_system(setup)
.add_system(expand_asteroids)
.add_plugins((
DefaultPlugins.set(AssetPlugin {
watch_for_changes: ChangeWatcher::with_delay(Duration::from_millis(200)),
..default()
}),
NoisyShaderPlugin,
PanCamPlugin::default(),
Material2dPlugin::<AsteroidBackgroundMaterial>::default(),
// WorldInspectorPlugin::new()
))
.add_systems(Startup, setup)
.add_systems(Update, expand_asteroids)
.run();
}

Expand All @@ -35,7 +40,7 @@ fn setup(mut commands: Commands) {
commands.spawn(AsteroidBundle::default());
}

#[derive(AsBindGroup, TypeUuid, Clone)]
#[derive(AsBindGroup, TypeUuid, Clone, TypePath)]
#[uuid = "1e449d2e-6901-4bff-95fa-d7407ad62b58"]
struct AsteroidBackgroundMaterial {
#[uniform(0)]
Expand Down
5 changes: 2 additions & 3 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ use noisy_bevy::fbm_simplex_2d;
fn main() {
App::new()
.insert_resource(ClearColor(Color::BLACK))
.add_plugins(DefaultPlugins)
.add_plugin(PanCamPlugin::default())
.add_startup_system(setup)
.add_plugins((DefaultPlugins, PanCamPlugin::default()))
.add_systems(Startup, setup)
.run();
}

Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ use bevy::{

/// Adds noise library as a wgsl import
///
/// General functionality can be included through:
/// General functionality can be imported through:
///
/// ```wgsl
/// #import noisy_bevy::prelude
/// #import noisy_bevy
/// ```
pub struct NoisyShaderPlugin;

impl Plugin for NoisyShaderPlugin {
fn build(&self, app: &mut App) {
app.add_startup_system(load_shaders);
app.add_systems(Startup, load_shaders);
}
}

fn load_shaders(mut shaders: ResMut<Assets<Shader>>) {
let shader = Shader::from_wgsl(include_str!("../assets/simple_noise_prelude.wgsl"));
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);
}
Expand Down

0 comments on commit 8dff288

Please sign in to comment.