#pattern #noise #procedural #graphics #game

noises-and-patterns

用于2D噪声和模式的程序化创建库

4个版本

0.1.3 2022年11月16日
0.1.2 2022年11月14日
0.1.1 2022年11月13日
0.1.0 2022年11月12日

#667 in 图形API

MIT 许可证

12KB
176 代码行

计算机图形中常见的2D噪声和模式的精选列表。主要来自 Shadertoy 上的实现。所有实现均受MIT或类似协议的保护。

该库旨在为需要访问Rust中原始未处理噪声值的用户而设计。这不是一个效果或后期处理库。所有返回值都在[0..1]之间。

噪声类支持原始噪声以及平滑的FBM变体。

该库使用nalgebra-glm crate作为数学库(也是其唯一的依赖项)。然而,API没有外部依赖。

精度

默认情况下,库通过在lib.rs中的FP类型定义中编译为f32,您可以根据说明更改类型,并在需要时将库编译为f64

特质

噪声和模式的特质非常简单。

pub trait Noise {

    fn new() -> Self;

    /// 2D noise for the given position
    fn noise_2d(&self, p: (FP, FP)) -> FP;

    // 2D fbm for the given position and the octaves
    fn fbm_2d(&self, p: (FP, FP), octaves: i32) -> FP;
}

pub trait Pattern {

    fn new() -> Self;

    /// 2D pattern for the given position, returns mask and id
    fn pattern_2d(&self, p: (FP, FP)) -> (FP, FP);

    /// For setting pattern properties
    fn set_property(&mut self, name: &str, value: FP);
}

噪声

值噪声

基于 1D, 2D & 3D Value Noise

let mut pixels = vec![0;width * height * 4];
let noise = Value::new();

for y in 0..height {
    for x in 0..width {
        let scale = 8.0;
        // let v = noise.get_2d((((x as FP) / width as FP) * scale, ((y as FP) / height as FP) * scale));
        let v = noise.fbm_2d((((x as FP) / width as FP) * scale, ((y as FP) / height as FP) * scale), 5);
        let v_u8 = (v * 255.0) as u8;
        let color = [v_u8, v_u8, v_u8, 255];
        let d = x * 4 + y * width * 4;
        pixels[d..d+4].copy_from_slice(&color);
    }
}

沃罗诺伊基本

基于 Voronoi Basic。感谢 IQ!

let mut pixels = vec![0;width * height * 4];
let noise = VoronoiBasic::new();

for y in 0..height {
    for x in 0..width {
        let scale = 8.0;
        let v = noise.get_2d((((x as FP) / width as FP) * scale, ((y as FP) / height as FP) * scale));
        let v_u8 = (v * 255.0) as u8;
        let color = [v_u8, v_u8, v_u8, 255];
        let d = x * 4 + y * width * 4;
        pixels[d..d+4].copy_from_slice(&color);
    }
}

模式

砖块

基于 Bricks and Tiles。在此处使用时,已获得Fabrice的许可,并遵循MIT协议。感谢 Fabrice!

let mut pixels = vec![0;width * height * 4];
let mut bricks = Bricks::new();
bricks.set_property("round", 0.0);

for y in 0..height {
    for x in 0..width {

        let v = noise.pattern_2d(((x as FP / width as FP), (y as FP / height as FP)));
        let v_u8 = (v.0 * v.1 * 255.0) as u8;
        let color = [v_u8, v_u8, v_u8, 255];
        let d = x * 4 + y * width * 4;
        pixels[d..d+4].copy_from_slice(&color);
    }
}

支持的属性

// The ratio of width / height of the bricks. 1.0 means they are square, larger values increases the width, default 2.0.
bricks.set_property("ratio", 1.0);
// A value of 1.0 (default) offsets every second line, otherwise aligns the bricks vertically.
bricks.set_property("brick", 0.0);
// The cell size, a global scaling factor, default 16.0
bricks.set_property("cell", 16.0);
// The gap between bricks, default 0.08.
bricks.set_property("gap", 0.1);
// The bevel of the brick, larger values provide a smoother transition, by default 0.07.
bricks.set_property("bevel", 0.1);
// The roundness of the brick, 0.0 creates square bricks, 1.0 circular ones. Default is 0.25.
bricks.set_property("round", 0.25);

依赖项

~3MB
~60K SLoC