Giter VIP home page Giter VIP logo

go-squirrelnoise's Introduction

SquirrelNoise5 - Squirrel's Raw Noise utilities (version 5)

Erik Karlsson: I'm not the original author of this code. Just wanted to convert this into Go for a side project. Will make this a module at some point to be importable.

This code is made available under the Creative Commons attribution 3.0 license (CC-BY-3.0 US): Attribution in source code comments (even closed-source/commercial code) is sufficient. License summary and text available at: https://creativecommons.org/licenses/by/3.0/us/

These noise functions were written by Squirrel Eiserloh as a cheap and simple substitute for the [sometimes awful] bit-noise sample code functions commonly found on the web, many of which are hugely biased or terribly patterned, e.g. having bits which are on (or off) 75% or even 100% of the time (or are excessively overkill/slow for our needs, such as MD5 or SHA).

Note: This is work in progress; not all functions have been tested. Use at your own risk. Please report any bugs, issues, or bothersome cases to SquirrelEiserloh at gmail.com.

The following functions are all based on a simple bit-noise hash function which returns an unsigned integer containing 32 reasonably-well-scrambled bits, based on a given (signed) integer input parameter (position/index) and [optional] seed. Kind of like looking up a value in an infinitely large [non-existent] table of previously rolled random numbers.

These functions are deterministic and random-access / order-independent (i.e. state-free), so they are particularly well-suited for use in smoothed/fractal/simplex/Perlin noise functions and out-of-order (or or-demand) procedural content generation (i.e. that mountain village is the same whether you generated it first or last, ahead of time or just now).

The N-dimensional variations simply hash their multidimensional coordinates down to a single 32-bit index and then proceed as usual, so while results are not unique they should (hopefully) not seem locally predictable or repetitive.


Raw pseudorandom noise functions (random-access / deterministic). Basis of all other noise.

func Get1dNoiseUint( index int32, seed uint32 ) uint32
func Get2dNoiseUint( indexX int32, indexY int32, seed uint32 ) uint32
func Get3dNoiseUint( indexX int32, indexY int32, indexZ int32, seed uint32 ) uint32
func Get4dNoiseUint( indexX int32, indexY int32, indexZ int32, indexT int32, seed uint32 ) uint32

Same functions, mapped to floats in [0,1] for convenience.

func Get1dNoiseZeroToOne( index int32, seed uint32 ) float32
func Get2dNoiseZeroToOne( indexX int32, indexY int32, seed uint32 ) float32
func Get3dNoiseZeroToOne( indexX int32, indexY int32, indexZ int32, seed uint32 ) float32
func Get4dNoiseZeroToOne( indexX int32, indexY int32, indexZ int32, indexT int32, seed uint32 ) float32

Same functions, mapped to floats in [-1,1] for convenience.

func Get1dNoiseNegOneToOne( index int32, seed uint32 ) float32
func Get2dNoiseNegOneToOne( indexX int32, indexY int32, seed uint32 ) float32
func Get3dNoiseNegOneToOne( indexX int32, indexY int32, indexZ int32, seed uint32 ) float32
func Get4dNoiseNegOneToOne( indexX int32, indexY int32, indexZ int32, indexT int32, seed uint32 ) float32

Inline function definitions below


Fast hash of an int32 into a different (unrecognizable) uint32.

Returns an unsigned integer containing 32 reasonably-well-scrambled bits, based on the hash of a given (signed) integer input parameter (position/index) and [optional] seed. Kind of like looking up a value in an infinitely large table of previously generated random numbers.

I call this particular approach SquirrelNoise5 (5th iteration of my 1D raw noise function).

Many thanks to Peter Schmidt-Nielsen whose outstanding analysis helped identify a weakness in the SquirrelNoise3 code I originally used in my GDC 2017 talk, "Noise-based RNG". Version 5 avoids a noise repetition found in version 3 at extremely high position values caused by a lack of influence by some of the high input bits onto some of the low output bits.

The revised SquirrelNoise5 function ensures all input bits affect all output bits, and to (for me) a statistically acceptable degree. I believe the worst-case here is in the amount of influence input position bit #30 has on output noise bit #0 (49.99%, vs. 50% ideal).

go-squirrelnoise's People

Contributors

edkarlsson avatar

Stargazers

Andrian Lutsu avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.