Skip to content

Commit

Permalink
FBM
Browse files Browse the repository at this point in the history
  • Loading branch information
makscee committed Mar 31, 2024
1 parent fe09e6f commit 3cba7f7
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ lazy_static = "1.4.0"
convert_case = "0.6.0"
indexmap = { version = "2.2.5", features = ["serde"] }
egui_extras = { version = "0.23.0", features = ["svg"] }
noisy_bevy = "0.4.0"

[dependencies.winit]
# version = "*"
Expand Down
13 changes: 12 additions & 1 deletion assets/shaders/sdf_shape.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import bevy_sprite::mesh2d_view_bindings globals
#import bevy_sprite::mesh2d_view_bindings view
#import bevy_sprite::mesh2d_vertex_output MeshVertexOutput
#import noisy_bevy fbm_simplex_2d

struct ShapeMaterial {
colors: array<vec4<f32>, 11>,
Expand Down Expand Up @@ -40,7 +41,17 @@ fn fragment(in: MeshVertexOutput) -> @location(0) vec4<f32> {
let alpha = material.data[10].z;
let thickness = material.data[10].w * 0.03;

let uv = (in.uv - vec2(0.5)) * size * 2.0;
var uv = (in.uv - vec2(0.5)) * size * 2.0;
#ifdef FBM
let octaves = i32(material.data[9].x);
let lacunarity = material.data[9].y;
let gain = material.data[9].z;
let offset = material.data[9].w;
uv += vec2(
fbm_simplex_2d(uv, octaves, lacunarity, gain),
fbm_simplex_2d(uv + vec2(offset), octaves, lacunarity, gain)
);
#endif
let sdf = sdf(uv, size) - AA;
var v = f32(sdf > -thickness);
#ifdef LINE
Expand Down
93 changes: 76 additions & 17 deletions src/components/representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub enum RepresentationMaterial {
shape_type: RepShapeType,
#[serde(default)]
fill: RepFill,
#[serde(default)]
fbm: Option<RepFbm>,
#[serde(default = "f32_one_e")]
alpha: Expression,
},
Expand Down Expand Up @@ -96,6 +98,9 @@ pub enum RepresentationMaterial {
fn font_size() -> f32 {
32.0
}
fn i32_one_e() -> Expression {
Expression::Int(1)
}
fn f32_one_e() -> Expression {
Expression::Float(1.0)
}
Expand Down Expand Up @@ -172,6 +177,18 @@ pub enum RepShapeType {
},
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct RepFbm {
#[serde(default = "i32_one_e")]
pub octaves: Expression,
#[serde(default = "f32_one_e")]
pub lacunarity: Expression,
#[serde(default = "f32_one_e")]
pub gain: Expression,
#[serde(default = "f32_one_e")]
pub offset: Expression,
}

impl Default for RepShape {
fn default() -> Self {
Self::Circle {
Expand Down Expand Up @@ -230,13 +247,15 @@ impl RepresentationMaterial {
shape,
shape_type,
fill,
fbm,
..
} => {
let mut materials = world.resource_mut::<Assets<ShapeMaterial>>();
let material = ShapeMaterial {
shape: shape.shader_shape(),
shape_type: shape_type.shader_shape_type(),
shape_fill: fill.shader_fill(),
fbm: fbm.is_some(),
..default()
};
let material = materials.add(material);
Expand Down Expand Up @@ -298,9 +317,10 @@ impl RepresentationMaterial {
shape_type,
fill,
alpha,
fbm,
} => {
let handle = world.get::<Handle<ShapeMaterial>>(entity).unwrap().clone();
if let Some(mut mat) = world
if let Some(mut material) = world
.get_resource_mut::<Assets<ShapeMaterial>>()
.unwrap()
.remove(&handle)
Expand All @@ -309,15 +329,15 @@ impl RepresentationMaterial {
match shape {
RepShape::Circle { radius } => {
let radius = radius.get_float(context, world).unwrap_or(1.0);
let t = &mut mat.data[10];
let t = &mut material.data[10];
if radius != t.x {
refresh_mesh = true;
}
*t = vec4(radius, radius, 0.0, 0.0);
}
RepShape::Rectangle { size } => {
let size = size.get_vec2(context, world).unwrap_or(vec2(1.0, 1.0));
let t = &mut mat.data[10];
let t = &mut material.data[10];
if t.xy() != size {
refresh_mesh = true;
}
Expand All @@ -326,13 +346,13 @@ impl RepresentationMaterial {
}
match shape_type {
RepShapeType::Line { thickness } => {
mat.data[10].w = thickness.get_float(context, world).unwrap_or(1.0)
material.data[10].w = thickness.get_float(context, world).unwrap_or(1.0)
}
RepShapeType::Opaque => {}
}
match fill {
RepFill::Solid { color } => {
mat.colors[0] =
material.colors[0] =
color.get_color(context, world).unwrap_or(Color::FUCHSIA)
}
RepFill::GradientLinear {
Expand All @@ -343,10 +363,10 @@ impl RepresentationMaterial {
} => {
let point1 = point1.get_vec2(context, world).unwrap_or_default();
let point2 = point2.get_vec2(context, world).unwrap_or_default();
mat.data[0].x = point1.x;
mat.data[0].y = point1.y;
mat.data[1].x = point2.x;
mat.data[1].y = point2.y;
material.data[0].x = point1.x;
material.data[0].y = point1.y;
material.data[1].x = point2.x;
material.data[1].y = point2.y;
}
RepFill::GradientRadial {
center,
Expand All @@ -355,10 +375,10 @@ impl RepresentationMaterial {
colors: _,
} => {
let center = center.get_vec2(context, world).unwrap_or_default();
mat.data[0].x = center.x;
mat.data[0].y = center.y;
material.data[0].x = center.x;
material.data[0].y = center.y;
let radius = radius.get_float(context, world).unwrap_or(1.0);
mat.data[0].z = radius;
material.data[0].z = radius;
}
}
match fill {
Expand All @@ -368,27 +388,44 @@ impl RepresentationMaterial {
let color =
color.get_color(context, world).unwrap_or(Color::FUCHSIA);
let part = parts[i].get_float(context, world).unwrap_or(0.5);
mat.colors[i] = color;
mat.data[i].w = part;
material.colors[i] = color;
material.data[i].w = part;
}
}
RepFill::Solid { .. } => {}
}
mat.data[10].z = alpha.get_float(context, world).unwrap_or(1.0);
material.data[10].z = alpha.get_float(context, world).unwrap_or(1.0);

if let Some(RepFbm {
octaves,
lacunarity,
gain,
offset,
}) = fbm
{
let octaves = octaves.get_int(context, world).unwrap_or(1);
let lacunarity = lacunarity.get_float(context, world).unwrap_or(1.0);
let gain = gain.get_float(context, world).unwrap_or(1.0);
let offset = offset.get_float(context, world).unwrap_or(1.0);
material.data[9].x = octaves as f32;
material.data[9].y = lacunarity;
material.data[9].z = gain;
material.data[9].w = offset;
}
if refresh_mesh {
let mesh = world.entity(entity).get::<Mesh2dHandle>().unwrap().clone();
if let Some(mesh) = world
.get_resource_mut::<Assets<Mesh>>()
.unwrap()
.get_mut(&mesh.0)
{
*mesh = shape.shader_shape().mesh(mat.data[10].xy());
*mesh = shape.shader_shape().mesh(material.data[10].xy());
}
}
let _ = world
.get_resource_mut::<Assets<ShapeMaterial>>()
.unwrap()
.set(handle, mat);
.set(handle, material);
}
}
RepresentationMaterial::Text {
Expand Down Expand Up @@ -527,6 +564,7 @@ impl RepresentationMaterial {
shape_type,
fill,
alpha,
fbm,
} => {
ui.horizontal(|ui| {
ComboBox::from_label("shape")
Expand Down Expand Up @@ -569,6 +607,14 @@ impl RepresentationMaterial {
}
}
});
let mut fbm_enabled = fbm.is_some();
if ui.checkbox(&mut fbm_enabled, "fbm").changed() {
*fbm = if fbm_enabled {
Some(ron::from_str("()").unwrap())
} else {
None
};
}
});
show_tree("alpha:", alpha, context, ui, world);
match shape {
Expand Down Expand Up @@ -639,6 +685,19 @@ impl RepresentationMaterial {
}
RepFill::Solid { .. } => {}
}

if let Some(RepFbm {
octaves,
lacunarity,
gain,
offset,
}) = fbm
{
show_tree("octaves:", octaves, context, ui, world);
show_tree("lacunarity:", lacunarity, context, ui, world);
show_tree("gain:", gain, context, ui, world);
show_tree("offset:", offset, context, ui, world);
}
}
RepresentationMaterial::Text {
size,
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod prelude;
pub mod resourses;
mod utils;

use noisy_bevy::NoisyShaderPlugin;
pub use prelude::*;

#[derive(Parser, Debug, Default)]
Expand Down Expand Up @@ -112,6 +113,7 @@ fn main() {
.add_plugins(bevy_egui::EguiPlugin)
.add_plugins(DefaultPickingPlugins)
.add_plugins(bevy_kira_audio::AudioPlugin)
.add_plugins(NoisyShaderPlugin)
.add_plugins(Material2dPlugin::<ShapeMaterial>::default())
.add_plugins(Material2dPlugin::<CurveMaterial>::default())
.add_plugins(RonAssetPlugin::<PackedUnit>::new(&["unit.ron"]))
Expand Down
8 changes: 8 additions & 0 deletions src/materials/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct ShapeMaterial {
pub shape: ShaderShape,
pub shape_type: ShaderShapeType,
pub shape_fill: ShaderShapeFill,
pub fbm: bool,
}

#[derive(
Expand Down Expand Up @@ -91,6 +92,10 @@ impl Material2d for ShapeMaterial {
fragment
.shader_defs
.push(key.bind_group_data.fill_color.def().into());
if key.bind_group_data.fbm {
debug!("fbm def push");
fragment.shader_defs.push("FBM".into());
}
Ok(())
}
}
Expand All @@ -100,6 +105,7 @@ pub struct ShapeMaterialKey {
shape: ShaderShape,
fill: ShaderShapeType,
fill_color: ShaderShapeFill,
fbm: bool,
}

impl From<&ShapeMaterial> for ShapeMaterialKey {
Expand All @@ -108,6 +114,7 @@ impl From<&ShapeMaterial> for ShapeMaterialKey {
shape: material.shape,
fill: material.shape_type,
fill_color: material.shape_fill,
fbm: material.fbm,
}
}
}
Expand All @@ -120,6 +127,7 @@ impl Default for ShapeMaterial {
shape_fill: default(),
colors: default(),
data: default(),
fbm: default(),
}
}
}

0 comments on commit 3cba7f7

Please sign in to comment.