Skip to content

Commit

Permalink
Add proximity dithering
Browse files Browse the repository at this point in the history
  • Loading branch information
Waridley committed Feb 8, 2024
1 parent 28a492e commit 1094e85
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
4 changes: 1 addition & 3 deletions rs/assets/shaders/terrain.mat.ron
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
ExtendedMaterial(
extension: DistanceDither(

),
extension: ( near_start: 0.0 ),
base: StandardMaterial(
base_color: Rgba ( red: 0.1, green: 0.1, blue: 0.1, alpha: 1.0),
reflectance: 0.3,
Expand Down
21 changes: 14 additions & 7 deletions rs/assets/shaders/util.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
#endif

struct DistanceDither {
start: f32,
end: f32,
_pad_12b: u32,
_pad_16b: u32,
far_start: f32,
far_end: f32,
near_start: f32,
near_end: f32,
};

@group(1) @binding(100)
Expand All @@ -36,9 +36,16 @@ fn distance_dither(
let world_dist = length(view.world_position.xyz - in.world_position.xyz);
let uv = in.position.xy / 16.0;
let thresh = textureSample(matrix_texture, matrix_sampler, uv).x;
let d = world_dist - material.start;
let range = material.end - material.start;
if d > thresh * range {

let d_far = world_dist - material.far_start;
let far_range = material.far_end - material.far_start;
if d_far > thresh * far_range {
discard;
}

let d_near = world_dist - material.near_end;
let near_range = material.near_start - material.near_end;
if d_near < thresh * near_range {
discard;
}
}
31 changes: 16 additions & 15 deletions rs/src/mats/fog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,19 @@ impl Plugin for MatterPlugin {
#[reflect(Default)]
#[uuid = "ff77386c-1d31-4afd-b52f-481b0845de0d"]
pub struct DistanceDither {
/// Distance from the camera at which the mesh starts to fade into the background.
#[uniform(100)]
pub start: f32,
pub far_start: f32,
/// Distance from the camera at which the mesh is fully discarded.
#[uniform(100)]
pub end: f32,
pub far_end: f32,

// Public just to allow `DistanceDither { ..default() }` construction
/// Padding to force 16-byte alignment on WASM
/// Distance from the camera at which the mesh starts to fade away.
#[uniform(100)]
pub _pad_12b: u32,
/// Padding to force 16-byte alignment on WASM
pub near_start: f32,
/// Distance from the camera at which the mesh is fully discarded.
#[uniform(100)]
pub _pad_16b: u32,
pub near_end: f32,

#[serde(skip)]
#[texture(101)]
Expand All @@ -98,10 +99,10 @@ pub struct DistanceDither {
impl DistanceDither {
pub fn new(start: f32, end: f32, matrix: Handle<Image>) -> Self {
Self {
start,
end,
_pad_12b: 0,
_pad_16b: 0,
far_start: start,
far_end: end,
near_start: 32.0,
near_end: 0.0,
matrix
}
}
Expand Down Expand Up @@ -131,8 +132,8 @@ pub fn update_matter_globals(mut materials: ResMut<Assets<Matter>>, fog: Res<Wea
}

for (_, mat) in materials.iter_mut() {
mat.extension.start = fog.fog_start;
mat.extension.end = fog.fog_end;
mat.extension.far_start = fog.fog_start;
mat.extension.far_end = fog.fog_end;
}
}

Expand All @@ -145,7 +146,7 @@ pub fn update_matter_extensions<M: MaterialExtension>(
}

for (_, mat) in materials.iter_mut() {
mat.base.extension.start = fog.fog_start;
mat.base.extension.end = fog.fog_end;
mat.base.extension.far_start = fog.fog_start;
mat.base.extension.far_end = fog.fog_end;
}
}
2 changes: 1 addition & 1 deletion sond-bevy-enum-components
2 changes: 1 addition & 1 deletion sond-bevy-particles
Submodule sond-bevy-particles updated 2 files
+14 −13 src/lib.rs
+14 −14 src/update.rs

0 comments on commit 1094e85

Please sign in to comment.