#noise #shaders #bevy #perlin #worley #bevy-plugin

bevy_compute_noise

A Bevy plugin for generating tilable 2D/3D noise textures using compute shaders

2 releases

0.1.1 Jun 1, 2024
0.1.0 May 29, 2024

#219 in Game dev

Download history 277/week @ 2024-05-27 30/week @ 2024-06-03 11/week @ 2024-06-10

318 downloads per month

MIT/Apache

42KB
999 lines

bevy_compute_noise

crates.io Doc

A plugin for bevy 0.13.2 for generating tilable 2D/3D noise textures using compute shaders.

bevy_compute_noise

Usage

Add the bevy_compute_noise dependency to Cargo.toml:

[dependencies]
bevy_compute_noise = "0.1.1"

Generate noise texture

use bevy_compute_noise::prelude::*;

App::default()
    .add_plugins(DefaultPlugins)
    .add_plugins(ComputeNoisePlugin::<Perlin2D>::default()) // add new plugin for each type of noise needed
    .run();

Queue noise generation

fn setup(
    mut images: ResMut<Assets<Image>>,
    mut perlin_2d_queue: ResMut<ComputeNoiseQueue<Perlin2D>>
) {
    // Create and queue noise image
    let noise_image: Handle<Image> = perlin_2d_queue.add(
        &mut images, 
        ComputeNoiseSize::D2(128, 128), // Use ComputeNoiseSize::D3 for 3D noise
        Perlin2d {
            seed: 0,
            frequency: 5,
            octaves: 4
        }
    );
}

Alternatively, you can add ComputeNoiseComponent<T: ComputeNoise> to an entity and it will be automatically queued whenever it has been updated:

fn setup(
    commands: Commands,
    mut images: ResMut<Assets<Image>>,
) {
    // Manually create noise image, so it can be used elsewhere.
    let noise_image = ComputeNoiseImage::create_image(ComputeNoiseSize::D2(128, 128));
    
    commands.spawn(ComputeNoiseComponent::<Perlin2d> {
        image: noise_image.clone();
        noise: Perlin2d {
            seed: 0,
            frequency: 5,
            octaves: 4
        }
    });
}

Noise Types

  • Worley2D
  • Worley3D
  • Perlin2D

TODO

  • Add more noise types.
  • Allow combination of noise.
  • Allow writing into specific texture channels.

Readback

If you need to readback the noise texture to the CPU, you can clone the readback branch and view the example in there. I'm not completely happy with the implementation and it's better to just generate the noise on the CPU using another crate anyway.

Dependencies

~41–79MB
~1M SLoC