A creator library for procedural 2D noises and patterns in Rust.

Overview

A curated list of common 2D noises and patterns in computer graphics. Mostly taken from implementations on Shadertoy. All implementations are under the MIT or similar.

This library is intended for users who need access to raw, unprocessed noise values in Rust. This is not intended to be an effect or post processing library. All returned values are between [0..1].

Noise classes support both raw noises as well as smooth FBM variants.

This library uses the nalgebra-glm crate as the math library (and is also its only dependency). The API however has no external dependencies.

Precision

By default the library compiles to f32 via a type definition of FP in lib.rs, you can change the type as instructed and compile the library to f64 if needed.

The Traits

The traits for noises and patterns are very simple.

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);
}

Noises

Value Noise

Based on 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);
    }
}
Value 2D Value 2D FBM

VoronoiBasic

Based on Voronoi Basic. Thanks 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);
    }
}

Voronoi

Patterns

Bricks

Based on Bricks and Tiles. Used here with permission from Fabrice under the MIT. Thanks 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);
    }
}

Bricks

Supported properties:

// 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);
You might also like...
Tooling and library for generation, validation and verification of supply chain metadata documents and frameworks

Spector Spector is both tooling and a library for the generation, validation and verification of supply chain metadata documents and frameworks. Many

This library provides a convenient derive macro for the standard library's std::error::Error trait.

derive(Error) This library provides a convenient derive macro for the standard library's std::error::Error trait. [dependencies] therror = "1.0" Compi

A small Rust library that let's you get position and size of the active window on Windows and MacOS

active-win-pos-rs A small Rust library that let's you get position and size of the active window on Windows and MacOS Build % git clone https://github

Ember is a minimalistic Rust library for creating 2D graphics, games, and interactive visualizations with ease and simplicity.
Ember is a minimalistic Rust library for creating 2D graphics, games, and interactive visualizations with ease and simplicity.

Ember Ember is a simple and fun 2D rendering library for Rust, allowing you to quickly create graphics and interactive applications with ease. It uses

Utility library for some Lenovo IdeaPad laptops. Supports IdeaPad Intel and AMD Models (15IIL05 and 15ARE05)

ideapad A Rust utility library for some Lenovo IdeaPad specific functionality. A Fair Warning This crate calls raw ACPI methods, which on the best cas

Peakrs Dataframe is a library and framework facilitates the extraction, transformation, and loading (ETL) of data.

Peakrs Dataframe Peakrs Dataframe is a library and framework facilitates the extraction, transformation, and loading (ETL) of data. Its first applicat

A library that allows for the arbitrary inspection and manipulation of the memory and code of a process on a Linux system.
A library that allows for the arbitrary inspection and manipulation of the memory and code of a process on a Linux system.

raminspect raminspect is a crate that allows for the inspection and manipulation of the memory and code of a running process on a Linux system. It pro

Rust library for ANSI terminal colours and styles (bold, underline)

rust-ansi-term This is a library for controlling colours and formatting, such as red bold text or blue underlined text, on ANSI terminals. View the Ru

Cross-platform Rust library for coloring and formatting terminal output
Cross-platform Rust library for coloring and formatting terminal output

Coloring terminal output Documentation term-painter is a cross-platform (i.e. also non-ANSI terminals) Rust library for coloring and formatting termin

Owner
Markus Moenig
Creator of Open Source graphic applications and games using Rust, Swift and Metal. Enjoying life in Thailand.
Markus Moenig
A parser and matcher for route patterns in Rust 🦀

Route Pattern A parser and matcher for a popular way to create route patterns. Patterns like these that include regular expressions, delimited in this

Dotan J. Nahum 3 Nov 24, 2022
Count your code by tokens, types of syntax tree nodes, and patterns in the syntax tree. A tokei/scc/cloc alternative.

tcount (pronounced "tee-count") Count your code by tokens, types of syntax tree nodes, and patterns in the syntax tree. Quick Start Simply run tcount

Adam P. Regasz-Rethy 48 Dec 7, 2022
Shellfirm - Intercept any risky patterns (default or defined by you) and prompt you a small challenge for double verification

shellfirm Opppppsss you did it again? ?? ?? ?? Protect yourself from yourself! rm -rf * git reset --hard before saving? kubectl delete ns which going

elad 652 Dec 29, 2022
Service-Oriented Design Patterns for Rust

SOD: Service-Oriented Design Overview This crate provides Service, MutService, and AsyncService traits and associated utilities to facilitiate service

Eric Thill 3 Apr 26, 2023
Game of life rendered in your terminal with over 500+ unique patterns to choose from.

Controls a: play animation n: next generation s: stop j or down arrow: go down next pattern (note: you have to stop the animation to browse the patter

Omar Magdy 20 Dec 22, 2022
List key patterns of a JSON file for jq.

jqk jqk lists key patterns of a JSON file for jq. Why? jq is a useful command line tool to filter values from a JSON file quickly on a terminal; howev

Kentaro Wada 8 Jun 25, 2023
auto-rust is an experimental project that aims to automatically generate Rust code with LLM (Large Language Models) during compilation, utilizing procedural macros.

Auto Rust auto-rust is an experimental project that aims to automatically generate Rust code with LLM (Large Language Models) during compilation, util

Minsky 6 May 14, 2023
Js-macros - Quickly prototype Rust procedural macros using JavaScript or TypeScript!

js-macros Quickly prototype Rust procedural macros using JavaScript or TypeScript! Have you ever thought "this would be a great use case for a procedu

null 15 Jun 17, 2022
A procedural macro that copy-pastes match arms for new type variant enums.

All the same! If you ever had code that looks like this: use std::io; use std::pin::Pin; use std::task::{Context, Poll}; use tokio::io::AsyncWrite; us

Ivan Nikulin 15 Feb 20, 2024
Rust Imaging Library's Python binding: A performant and high-level image processing library for Python written in Rust

ril-py Rust Imaging Library for Python: Python bindings for ril, a performant and high-level image processing library written in Rust. What's this? Th

Cryptex 13 Dec 6, 2022