Skip to content

Commit

Permalink
flux: add configurable noise multiplier and variable noise channels
Browse files Browse the repository at this point in the history
  • Loading branch information
sandydoo committed Aug 11, 2024
1 parent 6ee35a7 commit a83b7c9
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 97 deletions.
29 changes: 20 additions & 9 deletions flux/shader/generate_noise.comp.wgsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
struct NoiseUniforms {
multiplier: f32,
}

struct Channel {
scale: vec2<f32>,
offset_1: f32,
Expand All @@ -7,12 +11,9 @@ struct Channel {
padding: vec2<f32>,
}

struct Channels {
channels: array<Channel, 3>,
}

@group(0) @binding(0) var<uniform> global: Channels;
@group(0) @binding(1) var out_texture: texture_storage_2d<rg32float, write>;
@group(0) @binding(0) var<uniform> uniforms: NoiseUniforms;
@group(0) @binding(1) var<storage, read> channels: array<Channel>;
@group(0) @binding(2) var out_texture: texture_storage_2d<rg32float, write>;

fn permute(x: vec4<f32>) -> vec4<f32> {
return (((x * 34.0) + 1.0) * x) % 289.0;
Expand Down Expand Up @@ -116,11 +117,21 @@ fn main(
let texel_position = vec2<f32>(global_id.xy) / size;

var noise = vec2f(0.0);
for (var i = 0; i < 3; i = i + 1) {
let channel = global.channels[i];
let numChannels = arrayLength(&channels);
var i : u32 = 0u;
loop {
if (i >= numChannels) {
break;
}

let channel = channels[i];
noise += make_noise(texel_position, channel);

continuing {
i = i + 1u;
}
}

// TODO: make strength factor configurable
textureStore(out_texture, global_id.xy, vec4<f32>(noise * 0.45, 0.0, 0.0));
textureStore(out_texture, global_id.xy, vec4<f32>(noise * uniforms.multiplier, 0.0, 0.0));
}
9 changes: 6 additions & 3 deletions flux/src/flux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Flux {
self.settings = Arc::clone(settings);
self.fluid
.update(device, queue, self.grid.scaling_ratio, &self.settings);
self.noise_generator.update(&self.settings.noise_channels);
self.noise_generator.update(&self.settings);
self.lines
.update(device, queue, self.screen_size, &self.grid, &self.settings);
}
Expand Down Expand Up @@ -96,8 +96,11 @@ impl Flux {
settings,
);

let mut noise_generator_builder =
render::noise::NoiseGeneratorBuilder::new(2 * settings.fluid_size, grid.scaling_ratio);
let mut noise_generator_builder = render::noise::NoiseGeneratorBuilder::new(
2 * settings.fluid_size,
grid.scaling_ratio,
settings,
);
settings.noise_channels.iter().for_each(|channel| {
noise_generator_builder.add_channel(channel);
});
Expand Down
Loading

0 comments on commit a83b7c9

Please sign in to comment.