Skip to content

Commit

Permalink
Merge pull request #28 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 committed Aug 11, 2023
2 parents 704a5ab + e7ce56c commit 5df3bd5
Show file tree
Hide file tree
Showing 44 changed files with 333 additions and 219 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ keywords = ["gamedev", "bevy", "sdf"]
license = "MIT OR Apache-2.0"
name = "bevy_smud"
repository = "https://github.com/johanhelsing/bevy_smud"
version = "0.5.0"
version = "0.6.0"

[dependencies]
bevy = {version = "0.10", default-features = false, features = [
bevy = { version = "0.11", default-features = false, features = [
"bevy_core_pipeline",
"bevy_render",
"bevy_asset", # needed for handle ids
]}
bytemuck = { version = "1.7", features = ["derive"] }
copyless = "0.1"
bitflags = "1.3"
bitflags = "2.4"

[dev-dependencies]
bevy = {version = "0.10", default-features = false, features = [
bevy = {version = "0.11", default-features = false, features = [
"bevy_winit",
"x11", # github actions runners don't have libxkbcommon installed, so can't use wayland
"filesystem_watcher",
]}
bevy_asset_loader = "0.15"
bevy_lospec = "0.4"
bevy_pancam = "0.8"
bevy_asset_loader = "0.17"
bevy_lospec = "0.5"
bevy_pancam = "0.9"
rand = "0.8"

[profile.dev]
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn setup(

Make sure you reuse the shaders, i.e. don't call `add_sdf_expr` every frame.

You can also define shapes in .wgsl files. Note that in order to use the built-in shapes, you have to import [`bevy_smud::shapes`](https://github.com/johanhelsing/bevy_smud/blob/main/assets/shapes.wgsl), and you must create a function named `sdf` that takes a `vec2<f32>` and returns `f32`.
You can also define shapes in .wgsl files. Note that in order to use the built-in shapes, you have to import [`smud`](https://github.com/johanhelsing/bevy_smud/blob/main/assets/smud.wgsl), and you must create a function named `sdf` that takes a `vec2<f32>` and returns `f32`.

Other than that, make sure you understand how to combine shapes, use symmetries and change domains. For instance, the [bevy](https://github.com/johanhelsing/bevy_smud/blob/main/assets/bevy.wgsl) in the screenshot above is built up of several circles, ellipses, and a vesica for the beak.

Expand All @@ -91,7 +91,8 @@ I intend to support the `main` branch of Bevy in the `bevy-main` branch.

|bevy|bevy_smud|
|----|---------|
|0.10|0.5, main|
|0.11|0.6, main|
|0.10|0.5 |
|0.9 |0.4 |
|0.8 |0.3 |
|0.7 |0.2 |
Expand Down
30 changes: 16 additions & 14 deletions assets/bevy.wgsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#import bevy_smud::shapes
#define_import_path smud::bevy

#import smud

fn bevy_head(p: vec2<f32>) -> f32 {
let skull = sd_ellipse(p, 0.22, 0.20);
let p_beak = sd_rotate_rad(p - vec2<f32>(0.12, 0.02), 1.2);
let beak = sd_vesica(p_beak, 0.3, 0.2);
let skull = smud::sd_ellipse(p, 0.22, 0.20);
let p_beak = smud::rotate_rad(p - vec2<f32>(0.12, 0.02), 1.2);
let beak = smud::sd_vesica(p_beak, 0.3, 0.2);
return min(skull, beak);
}

Expand All @@ -13,13 +15,13 @@ fn sdf(p: vec2<f32>) -> f32 {

let p_upper_wing = p - vec2<f32>(-0.3, -0.25);
let upper_wing = max(
sd_ellipse(p_upper_wing, 0.7, 0.6),
-sd_rotate_rad(p, 0.40).y - 0.03
smud::sd_ellipse(p_upper_wing, 0.7, 0.6),
-smud::rotate_rad(p, 0.40).y - 0.03
// -sd_circle(p_upper_wing - vec2<f32>(-0.35, -0.05), 0.6)
);
let p_lower_wing = p - vec2<f32>(-0.3, -0.35);
let lower_wing = max(
sd_ellipse(p_lower_wing, 0.7, 0.5),
smud::sd_ellipse(p_lower_wing, 0.7, 0.5),
-p.y - 0.5
);

Expand All @@ -30,25 +32,25 @@ fn sdf(p: vec2<f32>) -> f32 {

let head = bevy_head(p - vec2<f32>(0.18, 0.40));

let chest = sd_smooth_intersect(
sd_ellipse(p - vec2<f32>(-0.8, -0.05), 1.3, 0.7),
let chest = smud::op_smooth_intersect(
smud::sd_ellipse(p - vec2<f32>(-0.8, -0.05), 1.3, 0.7),
max(-chest_clip, -tail_clip),
0.04
// -sd_ellipse(p - vec2<f32>(-0.8, 0.15), 0.9, 0.8)
);

let tail_wing_hole = sd_ellipse(sd_rotate_rad(p -vec2<f32>(-0.8, -0.4), -0.1), 0.63, 0.25);
let tail_wing_hole = smud::sd_ellipse(smud::rotate_rad(p -vec2<f32>(-0.8, -0.4), -0.1), 0.63, 0.25);

let chest_head = sd_smooth_union(chest, head, 0.07);
let chest_head_tail = sd_smooth_subtract(tail_wing_hole, chest_head, 0.07);
let chest_head = smud::op_smooth_union(chest, head, 0.07);
let chest_head_tail = smud::op_smooth_subtract(tail_wing_hole, chest_head, 0.07);

let body = sd_smooth_union(
let body = smud::op_smooth_union(
chest_head_tail,
max(wings, -tail_wing_hole + 0.01),
0.01
);

let eye = sd_circle(p - vec2<f32>(0.20, 0.45), 0.05);
let eye = smud::sd_circle(p - vec2<f32>(0.20, 0.45), 0.05);
let bevy = max(body, -eye);

return bevy * scale;
Expand Down
2 changes: 2 additions & 0 deletions assets/fills/cubic_falloff.wgsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define_import_path smud::default_fill

fn fill(d: f32, color: vec4<f32>) -> vec4<f32> {
let d2 = 1. - (d * 0.13);
let alpha = clamp(d2 * d2 * d2, 0., 1.) * color.a;
Expand Down
6 changes: 5 additions & 1 deletion assets/fills/outline.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#define_import_path smud::outline

#import smud

fn fill(d: f32, color: vec4<f32>) -> vec4<f32> {
let d_2 = abs(d - 1.) - 1.;
let a = sd_fill_alpha_fwidth(d_2);
let a = smud::sd_fill_alpha_fwidth(d_2);
return vec4<f32>(color.rgb, a * color.a);
}
6 changes: 5 additions & 1 deletion assets/fills/simple.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#define_import_path smud::simple_fill

#import smud

fn fill(d: f32, color: vec4<f32>) -> vec4<f32> {
let a = sd_fill_alpha_fwidth(d);
let a = smud::sd_fill_alpha_fwidth(d);
return vec4<f32>(color.rgb, a * color.a);
}
9 changes: 7 additions & 2 deletions assets/fragment.wgsl
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#define_import_path smud::fragment

#import smud::sdf as sdf
#import smud::fill as fill

struct FragmentInput {
@location(0) color: vec4<f32>,
@location(1) pos: vec2<f32>,
};

@fragment
fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
let d = sdf(in.pos);
return fill(d, in.color);
let d = sdf::sdf(in.pos);
return fill::fill(d, in.color);
}
6 changes: 4 additions & 2 deletions assets/gallery/blobby_cross.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::blobby_cross

#import smud

fn sdf(p: vec2<f32>) -> f32 {
let s = 20.;
return (sd_blobby_cross(p / s, 0.7) * s) - 4.;
return (smud::sd_blobby_cross(p / s, 0.7) * s) - 4.;
}
6 changes: 4 additions & 2 deletions assets/gallery/box.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::box

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_box(p, vec2<f32>(30., 20.));
return smud::sd_box(p, vec2<f32>(30., 20.));
}
6 changes: 4 additions & 2 deletions assets/gallery/circle.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::circle

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_circle(p, 25.);
return smud::sd_circle(p, 25.);
}
6 changes: 4 additions & 2 deletions assets/gallery/donut.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::donut

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return abs(sd_circle(p, 18.)) - 3.;
return abs(smud::sd_circle(p, 18.)) - 3.;
}
6 changes: 4 additions & 2 deletions assets/gallery/egg.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::egg

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_egg(p, 25., 10.);
return smud::sd_egg(p, 25., 10.);
}
6 changes: 4 additions & 2 deletions assets/gallery/ellipse.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::ellipse

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_ellipse(p, 25., 15.);
return smud::sd_ellipse(p, 25., 15.);
}
6 changes: 4 additions & 2 deletions assets/gallery/heart.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::heart

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_heart((p / 40.) - vec2<f32>(0., -0.5)) * 40.;
return smud::sd_heart((p / 40.) - vec2<f32>(0., -0.5)) * 40.;
}
6 changes: 4 additions & 2 deletions assets/gallery/hexagon.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::hexagon

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_hexagon(p, 20.);
return smud::sd_hexagon(p, 20.);
}
6 changes: 4 additions & 2 deletions assets/gallery/horseshoe.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::horseshoe

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_horseshoe(p, sin_cos(0.4), 17., vec2<f32>(6., 4.));
return smud::sd_horseshoe(p, smud::sin_cos(0.4), 17., vec2<f32>(6., 4.));
}
6 changes: 4 additions & 2 deletions assets/gallery/moon.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::moon

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_moon(p, 10., 25., 20.);
return smud::sd_moon(p, 10., 25., 20.);
}
6 changes: 4 additions & 2 deletions assets/gallery/pie.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::pie

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_pie(p, sin_cos(0.8), 25.);
return smud::sd_pie(p, smud::sin_cos(0.8), 25.);
}
6 changes: 4 additions & 2 deletions assets/gallery/rounded_x.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::horseshoe

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_rounded_x(p, 30., 4.);
return smud::sd_rounded_x(p, 30., 4.);
}
6 changes: 4 additions & 2 deletions assets/gallery/segment.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::segment

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_segment(p, vec2<f32>(-13.), vec2<f32>(13.)) - 3.;
return smud::sd_segment(p, vec2<f32>(-13.), vec2<f32>(13.)) - 3.;
}
6 changes: 4 additions & 2 deletions assets/gallery/stairs.wgsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::stairs

#import smud

fn sdf(p: vec2<f32>) -> f32 {
let s = 5.;
let p = p - vec2<f32>(-20.);
return sd_stairs(p / s, vec2<f32>(1.), 8.) * s;
return smud::sd_stairs(p / s, vec2<f32>(1.), 8.) * s;
}
6 changes: 4 additions & 2 deletions assets/gallery/star_4.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::star_4

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_star(p * 0.5, 10., 4, 3.0);
return smud::sd_star(p * 0.5, 10., 4, 3.0);
}
6 changes: 4 additions & 2 deletions assets/gallery/star_5.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::star_5

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_star_5(p, 10., 2.);
return smud::sd_star_5_(p, 10., 2.);
}
6 changes: 4 additions & 2 deletions assets/gallery/triangle.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::triangle

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_equilateral_triangle(p, 20.);
return smud::sd_equilateral_triangle(p, 20.);
}
6 changes: 4 additions & 2 deletions assets/gallery/vesica.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import bevy_smud::shapes
#define_import_path smud::gallery::vesica

#import smud

fn sdf(p: vec2<f32>) -> f32 {
return sd_vesica(p, 30., 15.);
return smud::sd_vesica(p, 30., 15.);
}
8 changes: 5 additions & 3 deletions assets/prelude.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
type float4 = vec4<f32>;
type float3 = vec3<f32>;
type float = f32;
#define_import_path smud::prelude

alias float4 = vec4<f32>;
alias float3 = vec3<f32>;
alias float = f32;

const PI: f32 = 3.141592653589793;
Loading

0 comments on commit 5df3bd5

Please sign in to comment.